From 3be40f84fee35783ecc623a0f281f2c9e5208714 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 23 Mar 2019 23:31:04 +0000 Subject: [PATCH] Implement read_track_sector(). --- include/dicformat/decls.h | 4 +++- include/dicformat/enums.h | 38 +++++++++++++++++++++++++++++--------- include/dicformat/errors.h | 2 ++ src/read.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/include/dicformat/decls.h b/include/dicformat/decls.h index c2780fb..80d2f1c 100644 --- a/include/dicformat/decls.h +++ b/include/dicformat/decls.h @@ -2,7 +2,7 @@ // The Disc Image Chef // ---------------------------------------------------------------------------- // -// Filename : decsl.h +// Filename : decls.h // Author(s) : Natalia Portillo // // Component : libdicformat. @@ -111,4 +111,6 @@ void ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type); uint32_t edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, int size, int pos); +int32_t read_track_sector(void *context, uint8_t *data, uint64_t sectorAddress, uint32_t *length, uint8_t track); + #endif //LIBDICFORMAT_DECLS_H diff --git a/include/dicformat/enums.h b/include/dicformat/enums.h index bd05277..15096ac 100644 --- a/include/dicformat/enums.h +++ b/include/dicformat/enums.h @@ -1,4 +1,3 @@ - #pragma clang diagnostic push #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" #ifndef LIBDICFORMAT_ENUMS_H @@ -183,21 +182,21 @@ typedef enum typedef enum { /** Block containing data */ - DataBlock = 0x4B4C4244, + DataBlock = 0x4B4C4244, /** Block containing a deduplication table */ - DeDuplicationTable = 0X2A544444, + DeDuplicationTable = 0X2A544444, /** Block containing the index */ - IndexBlock = 0X58444E49, + IndexBlock = 0X58444E49, /** Block containing logical geometry */ - GeometryBlock = 0x4D4F4547, + GeometryBlock = 0x4D4F4547, /** Block containing metadata */ - MetadataBlock = 0x4154454D, + MetadataBlock = 0x4154454D, /** Block containing optical disc tracks */ - TracksBlock = 0x534B5254, + TracksBlock = 0x534B5254, /** Block containing CICM XML metadata */ - CicmBlock = 0x4D434943, + CicmBlock = 0x4D434943, /** Block containing contents checksums */ - ChecksumBlock = 0x4D534B43, + ChecksumBlock = 0x4D534B43, /** TODO: Block containing data position measurements */ DataPositionMeasurementBlock = 0x2A4D5044, /** TODO: Block containing a snapshot index */ @@ -241,6 +240,27 @@ typedef enum DICF_STATUS_INVALID_CONTEXT = -1, } DicformatStatus; +/** + * Enumeration of media types defined in CICM metadata + */ +typedef enum +{ + /** + * Purely optical discs + */ + OpticalDisc = 0, /** + * Media that is physically block-based or abstracted like that + + */ + BlockMedia = 1, /** + * Media that can be accessed by-byte or by-bit, like chips + */ + LinearMedia = 2, /** + * Media that can only store data when it is modulated to audio + */ + AudioMedia = 3 +} XmlMediaType; + #endif //LIBDICFORMAT_ENUMS_H #pragma clang diagnostic pop \ No newline at end of file diff --git a/include/dicformat/errors.h b/include/dicformat/errors.h index 2d944b1..12953c6 100644 --- a/include/dicformat/errors.h +++ b/include/dicformat/errors.h @@ -16,6 +16,8 @@ #define DICF_ERROR_NOT_ENOUGH_MEMORY -9 #define DICF_ERROR_BUFFER_TOO_SMALL -10 #define DICF_ERROR_MEDIA_TAG_NOT_PRESENT -11 +#define DICF_ERROR_INCORRECT_MEDIA_TYPE -12 +#define DICF_ERROR_TRACK_NOT_FOUND -13 #define DICF_STATUS_OK 0 #define DICF_STATUS_SECTOR_NEVER_WRITTEN 1 diff --git a/src/read.c b/src/read.c index 2f312c5..83d5812 100644 --- a/src/read.c +++ b/src/read.c @@ -154,4 +154,33 @@ int32_t read_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint32 *length = blockHeader.sectorSize; free(block); return DICF_STATUS_OK; -} \ No newline at end of file +} + +int32_t read_track_sector(void *context, uint8_t *data, uint64_t sectorAddress, uint32_t *length, uint8_t track) +{ + dicformatContext *ctx; + int i; + + if(context == NULL) + return DICF_ERROR_NOT_DICFORMAT; + + ctx = context; + + // TODO: Cast this field without casting the whole structure, as this can buffer overflow + // Not a libdicformat context + if(ctx->magic != DIC_MAGIC) + return DICF_ERROR_NOT_DICFORMAT; + + if(ctx->imageInfo.XmlMediaType != OpticalDisc) + return DICF_ERROR_INCORRECT_MEDIA_TYPE; + + for(i = 0; i < ctx->numberOfDataTracks; i++) + { + if(ctx->dataTracks[i].sequence == track) + { + return read_sector(context, ctx->dataTracks[i].start + sectorAddress, data, length); + } + } + + return DICF_ERROR_TRACK_NOT_FOUND; +}