Split tracks block processing from open to a separate file.

This commit is contained in:
2025-08-02 16:17:57 +01:00
parent 7ed1002273
commit 793de582ca
4 changed files with 126 additions and 75 deletions

View File

@@ -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)

View File

@@ -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

121
src/blocks/optical.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#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));
}
}

View File

@@ -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