mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Split data block processing from open to a separate file.
This commit is contained in:
245
src/open.c
245
src/open.c
@@ -39,7 +39,6 @@ void *aaruf_open(const char *filepath)
|
||||
long pos = 0;
|
||||
uint8_t *data = NULL;
|
||||
uint8_t *cmpData = NULL;
|
||||
uint8_t *cstData = NULL;
|
||||
uint32_t *cdDdt = NULL;
|
||||
uint64_t crc64 = 0;
|
||||
int i = 0, j = 0, k = 0;
|
||||
@@ -49,7 +48,6 @@ void *aaruf_open(const char *filepath)
|
||||
ChecksumHeader checksum_header;
|
||||
ChecksumEntry const *checksum_entry = NULL;
|
||||
mediaTagEntry *mediaTag = NULL;
|
||||
mediaTagEntry *oldMediaTag = NULL;
|
||||
uint32_t signature = 0;
|
||||
UT_array *index_entries = NULL;
|
||||
|
||||
@@ -207,248 +205,19 @@ void *aaruf_open(const char *filepath)
|
||||
switch(entry->blockType)
|
||||
{
|
||||
case DataBlock:
|
||||
// NOP block, skip
|
||||
if(entry->dataType == NoData) break;
|
||||
errorNo = process_data_block(ctx, entry);
|
||||
|
||||
readBytes = fread(&blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(BlockHeader))
|
||||
if(errorNo != AARUF_STATUS_OK)
|
||||
{
|
||||
fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset);
|
||||
utarray_free(index_entries);
|
||||
free(ctx);
|
||||
errno = errorNo;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
ctx->imageInfo.ImageSize += blockHeader.cmpLength;
|
||||
|
||||
// Unused, skip
|
||||
if(entry->dataType == UserData)
|
||||
{
|
||||
if(blockHeader.sectorSize > ctx->imageInfo.SectorSize)
|
||||
ctx->imageInfo.SectorSize = blockHeader.sectorSize;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if(blockHeader.identifier != entry->blockType)
|
||||
{
|
||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||
entry->offset);
|
||||
break;
|
||||
}
|
||||
|
||||
if(blockHeader.type != entry->dataType)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"libaaruformat: Expected block with data type %4.4s at position %" PRIu64
|
||||
" but found data type %4.4s\n",
|
||||
(char *)&entry->blockType, entry->offset, (char *)&blockHeader.type);
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "libaaruformat: Found data block with type %4.4s at position %" PRIu64 "\n",
|
||||
(char *)&entry->blockType, entry->offset);
|
||||
|
||||
if(blockHeader.compression == Lzma || blockHeader.compression == LzmaClauniaSubchannelTransform)
|
||||
{
|
||||
if(blockHeader.compression == LzmaClauniaSubchannelTransform &&
|
||||
blockHeader.type != CdSectorSubchannel)
|
||||
{
|
||||
fprintf(stderr, "Invalid compression type %d for block with data type %d, continuing...\n",
|
||||
blockHeader.compression, blockHeader.type);
|
||||
break;
|
||||
}
|
||||
|
||||
lzmaSize = blockHeader.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
|
||||
cmpData = (uint8_t *)malloc(lzmaSize);
|
||||
if(cmpData == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot allocate memory for block, continuing...\n");
|
||||
break;
|
||||
}
|
||||
|
||||
data = (uint8_t *)malloc(blockHeader.length);
|
||||
if(data == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot allocate memory for block, continuing...\n");
|
||||
free(cmpData);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
||||
{
|
||||
fprintf(stderr, "Could not read LZMA properties, continuing...\n");
|
||||
free(cmpData);
|
||||
free(data);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
||||
if(readBytes != lzmaSize)
|
||||
{
|
||||
fprintf(stderr, "Could not read compressed block, continuing...\n");
|
||||
free(cmpData);
|
||||
free(data);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = blockHeader.length;
|
||||
errorNo = aaruf_lzma_decode_buffer(data, &readBytes, cmpData, &lzmaSize, lzmaProperties,
|
||||
LZMA_PROPERTIES_LENGTH);
|
||||
|
||||
if(errorNo != 0)
|
||||
{
|
||||
fprintf(stderr, "Got error %d from LZMA, continuing...\n", errorNo);
|
||||
free(cmpData);
|
||||
free(data);
|
||||
errno = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
|
||||
// TODO: Clean-up all memory!!!
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(readBytes != blockHeader.length)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Error decompressing block, should be {0} bytes but got {1} bytes., continuing...\n");
|
||||
free(cmpData);
|
||||
free(data);
|
||||
errno = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
|
||||
// TODO: Clean-up all memory!!!
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(blockHeader.compression == LzmaClauniaSubchannelTransform)
|
||||
{
|
||||
cstData = malloc(blockHeader.length);
|
||||
if(cstData == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot allocate memory for block, continuing...\n");
|
||||
free(cmpData);
|
||||
free(data);
|
||||
break;
|
||||
}
|
||||
|
||||
aaruf_cst_untransform(data, cstData, blockHeader.length);
|
||||
free(data);
|
||||
data = cstData;
|
||||
cstData = NULL;
|
||||
}
|
||||
|
||||
free(cmpData);
|
||||
}
|
||||
else if(blockHeader.compression == None)
|
||||
{
|
||||
data = (uint8_t *)malloc(blockHeader.length);
|
||||
if(data == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot allocate memory for block, continuing...\n");
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(data, 1, blockHeader.length, ctx->imageStream);
|
||||
|
||||
if(readBytes != blockHeader.length)
|
||||
{
|
||||
free(data);
|
||||
fprintf(stderr, "Could not read block, continuing...\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "libaaruformat: Found unknown compression type %d, continuing...\n",
|
||||
blockHeader.compression);
|
||||
break;
|
||||
}
|
||||
|
||||
if(blockHeader.length > 0)
|
||||
{
|
||||
crc64 = aaruf_crc64_data(data, blockHeader.length);
|
||||
|
||||
// Due to how C# wrote it, it is effectively reversed
|
||||
if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64);
|
||||
|
||||
if(crc64 != blockHeader.crc64)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64
|
||||
", continuing...\n",
|
||||
crc64, blockHeader.crc64);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if it's not a media tag, but a sector tag, and fill the appropriate table then
|
||||
switch(entry->dataType)
|
||||
{
|
||||
case CdSectorPrefix:
|
||||
case CdSectorPrefixCorrected:
|
||||
if(entry->dataType == CdSectorPrefixCorrected) { ctx->sectorPrefixCorrected = data; }
|
||||
else
|
||||
ctx->sectorPrefix = data;
|
||||
|
||||
ctx->readableSectorTags[CdSectorSync] = true;
|
||||
ctx->readableSectorTags[CdSectorHeader] = true;
|
||||
|
||||
break;
|
||||
case CdSectorSuffix:
|
||||
case CdSectorSuffixCorrected:
|
||||
if(entry->dataType == CdSectorSuffixCorrected)
|
||||
ctx->sectorSuffixCorrected = data;
|
||||
else
|
||||
ctx->sectorSuffix = data;
|
||||
|
||||
ctx->readableSectorTags[CdSectorSubHeader] = true;
|
||||
ctx->readableSectorTags[CdSectorEcc] = true;
|
||||
ctx->readableSectorTags[CdSectorEccP] = true;
|
||||
ctx->readableSectorTags[CdSectorEccQ] = true;
|
||||
ctx->readableSectorTags[CdSectorEdc] = true;
|
||||
break;
|
||||
case CdSectorSubchannel:
|
||||
ctx->sectorSubchannel = data;
|
||||
ctx->readableSectorTags[CdSectorSubchannel] = true;
|
||||
break;
|
||||
case AppleProfileTag:
|
||||
case AppleSonyTag:
|
||||
case PriamDataTowerTag:
|
||||
ctx->sectorSubchannel = data;
|
||||
ctx->readableSectorTags[AppleSectorTag] = true;
|
||||
break;
|
||||
case CompactDiscMode2Subheader:
|
||||
ctx->mode2Subheaders = data;
|
||||
break;
|
||||
default:
|
||||
mediaTag = (mediaTagEntry *)malloc(sizeof(mediaTagEntry));
|
||||
|
||||
if(mediaTag == NULL)
|
||||
{
|
||||
fprintf(stderr, "libaaruformat: Cannot allocate memory for media tag entry.\n");
|
||||
break;
|
||||
}
|
||||
memset(mediaTag, 0, sizeof(mediaTagEntry));
|
||||
|
||||
mediaTag->type = aaruf_get_media_tag_type_for_datatype(blockHeader.type);
|
||||
mediaTag->data = data;
|
||||
mediaTag->length = blockHeader.length;
|
||||
|
||||
HASH_REPLACE_INT(ctx->mediaTags, type, mediaTag, oldMediaTag);
|
||||
|
||||
if(oldMediaTag != NULL)
|
||||
{
|
||||
fprintf(stderr, "libaaruformat: Replaced media tag with type %d\n", oldMediaTag->type);
|
||||
free(oldMediaTag->data);
|
||||
free(oldMediaTag);
|
||||
oldMediaTag = NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DeDuplicationTable:
|
||||
readBytes = fread(&ddtHeader, 1, sizeof(DdtHeader), ctx->imageStream);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user