Split CICM XML metadata block processing from open to a separate file.

This commit is contained in:
2025-08-02 16:21:50 +01:00
parent 793de582ca
commit 0d28399041
3 changed files with 64 additions and 39 deletions

View File

@@ -30,5 +30,6 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundU
void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry);
#endif // LIBAARUFORMAT_INTERNAL_H

View File

@@ -285,4 +285,66 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry)
ctx->imageInfo.Cylinders = ctx->geometryBlock.cylinders;
ctx->imageInfo.Heads = ctx->geometryBlock.heads;
ctx->imageInfo.SectorsPerTrack = ctx->geometryBlock.sectorsPerTrack;
}
// CICM XML metadata block
void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry)
{
int pos = 0;
size_t readBytes = 0;
// Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL)
{
fprintf(stderr, "Invalid context or image stream.\n");
return;
}
// Seek to block
pos = fseek(ctx->imageStream, entry->offset, SEEK_SET);
if(pos < 0 || ftell(ctx->imageStream) != entry->offset)
{
fprintf(stderr, "libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry...\n", entry->offset);
return;
}
// Even if those two checks shall have been done before
readBytes = fread(&ctx->cicmBlockHeader, 1, sizeof(CicmMetadataBlock), ctx->imageStream);
if(readBytes != sizeof(CicmMetadataBlock))
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata header, continuing...\n");
return;
}
if(ctx->cicmBlockHeader.identifier != CicmBlock)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset);
}
ctx->imageInfo.ImageSize += ctx->cicmBlockHeader.length;
ctx->cicmBlock = (uint8_t *)malloc(ctx->cicmBlockHeader.length);
if(ctx->cicmBlock == NULL)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr, "libaaruformat: Could not allocate memory for CICM XML metadata block, continuing...\n");
return;
}
readBytes = fread(ctx->cicmBlock, 1, ctx->cicmBlockHeader.length, ctx->imageStream);
if(readBytes != ctx->metadataBlockHeader.blockSize)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
free(ctx->cicmBlock);
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata block, continuing...\n");
}
fprintf(stderr, "libaaruformat: Found CICM XML metadata block %" PRIu64 ".\n", entry->offset);
}

View File

@@ -227,7 +227,6 @@ void *aaruf_open(const char *filepath)
process_geometry_block(ctx, entry);
break;
// Metadata block
case MetadataBlock:
process_metadata_block(ctx, entry);
@@ -236,46 +235,9 @@ void *aaruf_open(const char *filepath)
process_tracks_block(ctx, entry);
break;
// CICM XML metadata block
case CicmBlock:
readBytes = fread(&ctx->cicmBlockHeader, 1, sizeof(CicmMetadataBlock), ctx->imageStream);
process_cicm_block(ctx, entry);
if(readBytes != sizeof(CicmMetadataBlock))
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata header, continuing...\n");
break;
}
if(ctx->cicmBlockHeader.identifier != CicmBlock)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
entry->offset);
}
ctx->imageInfo.ImageSize += ctx->cicmBlockHeader.length;
ctx->cicmBlock = (uint8_t *)malloc(ctx->cicmBlockHeader.length);
if(ctx->cicmBlock == NULL)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
fprintf(stderr,
"libaaruformat: Could not allocate memory for CICM XML metadata block, continuing...\n");
break;
}
readBytes = fread(ctx->cicmBlock, 1, ctx->cicmBlockHeader.length, ctx->imageStream);
if(readBytes != ctx->metadataBlockHeader.blockSize)
{
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
free(ctx->cicmBlock);
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata block, continuing...\n");
}
fprintf(stderr, "libaaruformat: Found CICM XML metadata block %" PRIu64 ".\n", entry->offset);
break;
// Dump hardware block
case DumpHardwareBlock: