diff --git a/include/dicformat/context.h b/include/dicformat/context.h index cd43398..52ceacc 100644 --- a/include/dicformat/context.h +++ b/include/dicformat/context.h @@ -46,22 +46,24 @@ typedef struct dicformatContext struct dataLinkedList *mediaTagsHead; struct dataLinkedList *mediaTagsTail; unsigned char *sectorPrefix; - unsigned char *sectorPrefixCorrected; - unsigned char *sectorSuffix; - unsigned char *sectorSuffixCorrected; - unsigned char *sectorSubchannel; - unsigned char *mode2Subheaders; - unsigned char shift; - bool inMemoryDdt; - uint64_t *userDataDdt; - size_t mappedMemoryDdtSize; - uint32_t *sectorPrefixDdt; - uint32_t *sectorSuffixDdt; - GeometryBlockHeader geometryBlock; - MetadataBlockHeader metadataBlockHeader; - unsigned char *metadataBlock; - TracksHeader tracksHeader; - TrackEntry *trackEntries; + unsigned char *sectorPrefixCorrected; + unsigned char *sectorSuffix; + unsigned char *sectorSuffixCorrected; + unsigned char *sectorSubchannel; + unsigned char *mode2Subheaders; + unsigned char shift; + bool inMemoryDdt; + uint64_t *userDataDdt; + size_t mappedMemoryDdtSize; + uint32_t *sectorPrefixDdt; + uint32_t *sectorSuffixDdt; + GeometryBlockHeader geometryBlock; + MetadataBlockHeader metadataBlockHeader; + unsigned char *metadataBlock; + TracksHeader tracksHeader; + TrackEntry *trackEntries; + CicmMetadataBlock cicmBlockHeader; + unsigned char *cicmBlock; } dicformatContext; typedef struct dataLinkedList diff --git a/src/close.c b/src/close.c index 7e208a7..70b0cfd 100644 --- a/src/close.c +++ b/src/close.c @@ -46,7 +46,7 @@ int close(void *context) dicformatContext *ctx = context; - // TODO: Cast this field without casting the whole structure, as this can buffer overlow + // TODO: Cast this field without casting the whole structure, as this can buffer overflow // Not a libdicformat context if(ctx->magic != DIC_MAGIC) { @@ -93,6 +93,7 @@ int close(void *context) free(ctx->metadataBlock); free(ctx->trackEntries); + free(ctx->cicmBlock); free(context); diff --git a/src/open.c b/src/open.c index e1288ed..216a948 100644 --- a/src/open.c +++ b/src/open.c @@ -570,8 +570,44 @@ void *open(const char *filepath) // TODO: ImageInfo break; - case CicmBlock: - // TODO + // CICM XML metadata block + case CicmBlock:readBytes = fread(&ctx->cicmBlockHeader, sizeof(CicmMetadataBlock), 1, ctx->imageStream); + + if(readBytes != sizeof(CicmMetadataBlock)) + { + memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); + fprintf(stderr, "libdicformat: Could not read CICM XML metadata header, continuing..."); + break; + } + + if(ctx->cicmBlockHeader.identifier != CicmBlock) + { + memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); + fprintf(stderr, + "libdicformat: Incorrect identifier for data block at position %"PRIu64"", + idxEntries[i].offset); + } + + ctx->cicmBlock = malloc(ctx->cicmBlockHeader.length); + + if(ctx->cicmBlock == NULL) + { + memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); + fprintf(stderr, + "libdicformat: Could not allocate memory for CICM XML metadata block, continuing..."); + break; + } + + readBytes = fread(ctx->cicmBlock, ctx->cicmBlockHeader.length, 1, ctx->imageStream); + + if(readBytes != ctx->metadataBlockHeader.blockSize) + { + memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock)); + free(ctx->cicmBlock); + fprintf(stderr, "libdicformat: Could not read CICM XML metadata block, continuing..."); + } + + fprintf(stderr, "libdicformat: Found CICM XML metadata block %"PRIu64".", idxEntries[i].offset); break; case DumpHardwareBlock: // TODO