diff --git a/CMakeLists.txt b/CMakeLists.txt index 3de504d..df1d544 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,7 +112,8 @@ add_library(aaruformat SHARED include/aaruformat/consts.h include/aaruformat/enu src/index/index_v2.c src/blocks/data.c src/ddt/ddt_v1.c - src/blocks/metadata.c) + src/blocks/metadata.c + src/blocks/optical.c) include_directories(include include/aaruformat) diff --git a/include/internal.h b/include/internal.h index 70e733b..98298b8 100644 --- a/include/internal.h +++ b/include/internal.h @@ -29,5 +29,6 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry); int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt); 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); #endif // LIBAARUFORMAT_INTERNAL_H diff --git a/src/blocks/optical.c b/src/blocks/optical.c new file mode 100644 index 0000000..830fd46 --- /dev/null +++ b/src/blocks/optical.c @@ -0,0 +1,121 @@ +/* + * This file is part of the Aaru Data Preservation Suite. + * Copyright (c) 2019-2025 Natalia Portillo. + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of the + * License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +#include +#include +#include +#include + +#include "aaruformat.h" + +void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) +{ + int pos = 0; + size_t readBytes = 0; + uint64_t crc64 = 0; + int j = 0, k = 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->tracksHeader, 1, sizeof(TracksHeader), ctx->imageStream); + + if(readBytes != sizeof(TracksHeader)) + { + memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); + fprintf(stderr, "libaaruformat: Could not read tracks header, continuing...\n"); + return; + } + + if(ctx->tracksHeader.identifier != TracksBlock) + { + memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); + fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", entry->offset); + } + + ctx->imageInfo.ImageSize += sizeof(TrackEntry) * ctx->tracksHeader.entries; + + ctx->trackEntries = (TrackEntry *)malloc(sizeof(TrackEntry) * ctx->tracksHeader.entries); + + if(ctx->trackEntries == NULL) + { + memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); + fprintf(stderr, "libaaruformat: Could not allocate memory for metadata block, continuing...\n"); + return; + } + + readBytes = fread(ctx->trackEntries, sizeof(TrackEntry), ctx->tracksHeader.entries, ctx->imageStream); + + if(readBytes != ctx->tracksHeader.entries) + { + memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); + free(ctx->trackEntries); + fprintf(stderr, "libaaruformat: Could not read metadata block, continuing...\n"); + + return; + } + + crc64 = aaruf_crc64_data((const uint8_t *)ctx->trackEntries, ctx->tracksHeader.entries * sizeof(TrackEntry)); + + // Due to how C# wrote it, it is effectively reversed + if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64); + + if(crc64 != ctx->tracksHeader.crc64) + { + fprintf(stderr, + "libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...\n", + crc64, ctx->tracksHeader.crc64); + return; + } + + fprintf(stderr, "libaaruformat: Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries, + entry->offset); + + ctx->imageInfo.HasPartitions = true; + ctx->imageInfo.HasSessions = true; + + ctx->numberOfDataTracks = 0; + + for(j = 0; j < ctx->tracksHeader.entries; j++) + { + if(ctx->trackEntries[j].sequence > 0 && ctx->trackEntries[j].sequence <= 99) ctx->numberOfDataTracks++; + } + + ctx->dataTracks = malloc(sizeof(TrackEntry) * ctx->numberOfDataTracks); + + k = 0; + for(j = 0; j < ctx->tracksHeader.entries; j++) + { + if(ctx->trackEntries[j].sequence > 0 && ctx->trackEntries[j].sequence <= 99) + memcpy(&ctx->dataTracks[k++], &ctx->trackEntries[j], sizeof(TrackEntry)); + } +} \ No newline at end of file diff --git a/src/open.c b/src/open.c index d34ed44..371dda3 100644 --- a/src/open.c +++ b/src/open.c @@ -39,7 +39,7 @@ void *aaruf_open(const char *filepath) long pos = 0; uint8_t *data = NULL; uint64_t crc64 = 0; - int i = 0, j = 0, k = 0; + int i = 0, j = 0; uint16_t e = 0; ChecksumHeader checksum_header; ChecksumEntry const *checksum_entry = NULL; @@ -233,79 +233,7 @@ void *aaruf_open(const char *filepath) break; case TracksBlock: - readBytes = fread(&ctx->tracksHeader, 1, sizeof(TracksHeader), ctx->imageStream); - - if(readBytes != sizeof(TracksHeader)) - { - memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Could not read tracks header, continuing...\n"); - break; - } - - if(ctx->tracksHeader.identifier != TracksBlock) - { - memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n", - entry->offset); - } - - ctx->imageInfo.ImageSize += sizeof(TrackEntry) * ctx->tracksHeader.entries; - - ctx->trackEntries = (TrackEntry *)malloc(sizeof(TrackEntry) * ctx->tracksHeader.entries); - - if(ctx->trackEntries == NULL) - { - memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - fprintf(stderr, "libaaruformat: Could not allocate memory for metadata block, continuing...\n"); - break; - } - - readBytes = fread(ctx->trackEntries, sizeof(TrackEntry), ctx->tracksHeader.entries, ctx->imageStream); - - if(readBytes != ctx->tracksHeader.entries) - { - memset(&ctx->tracksHeader, 0, sizeof(TracksHeader)); - free(ctx->trackEntries); - fprintf(stderr, "libaaruformat: Could not read metadata block, continuing...\n"); - } - - crc64 = aaruf_crc64_data((const uint8_t *)ctx->trackEntries, - ctx->tracksHeader.entries * sizeof(TrackEntry)); - - // Due to how C# wrote it, it is effectively reversed - if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64); - - if(crc64 != ctx->tracksHeader.crc64) - { - fprintf(stderr, - "libaaruformat: Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 - ", continuing...\n", - crc64, ctx->tracksHeader.crc64); - break; - } - - fprintf(stderr, "libaaruformat: Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries, - entry->offset); - - ctx->imageInfo.HasPartitions = true; - ctx->imageInfo.HasSessions = true; - - ctx->numberOfDataTracks = 0; - - for(j = 0; j < ctx->tracksHeader.entries; j++) - { - if(ctx->trackEntries[j].sequence > 0 && ctx->trackEntries[j].sequence <= 99) - ctx->numberOfDataTracks++; - } - - ctx->dataTracks = malloc(sizeof(TrackEntry) * ctx->numberOfDataTracks); - - k = 0; - for(j = 0; j < ctx->tracksHeader.entries; j++) - { - if(ctx->trackEntries[j].sequence > 0 && ctx->trackEntries[j].sequence <= 99) - memcpy(&ctx->dataTracks[k++], &ctx->trackEntries[j], sizeof(TrackEntry)); - } + process_tracks_block(ctx, entry); break; // CICM XML metadata block