2022-05-28 12:57:21 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Aaru Data Preservation Suite.
|
2025-08-01 21:19:45 +01:00
|
|
|
* Copyright (c) 2019-2025 Natalia Portillo.
|
2022-05-28 12:57:21 +01:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
2019-03-17 20:39:40 +00:00
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-03-17 21:14:40 +00:00
|
|
|
#include <inttypes.h>
|
2019-03-31 20:52:06 +01:00
|
|
|
#include <stdio.h>
|
2022-10-03 18:15:13 +01:00
|
|
|
#include <stdlib.h>
|
2019-03-17 21:14:40 +00:00
|
|
|
#include <string.h>
|
2022-06-21 21:08:19 +01:00
|
|
|
|
2022-05-28 12:10:04 +01:00
|
|
|
#include <aaruformat.h>
|
|
|
|
|
|
2025-08-01 21:10:36 +01:00
|
|
|
#include "internal.h"
|
2025-08-13 20:16:42 +01:00
|
|
|
#include "log.h"
|
2025-08-01 21:10:36 +01:00
|
|
|
#include "utarray.h"
|
|
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
static void cleanup_open_failure(aaruformat_context *ctx)
|
2025-10-08 19:37:04 +01:00
|
|
|
{
|
|
|
|
|
if(ctx == NULL) return;
|
|
|
|
|
|
|
|
|
|
if(ctx->imageStream != NULL)
|
|
|
|
|
{
|
|
|
|
|
fclose(ctx->imageStream);
|
|
|
|
|
ctx->imageStream = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(ctx->readableSectorTags);
|
|
|
|
|
ctx->readableSectorTags = NULL;
|
|
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 13:08:45 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Opens an existing AaruFormat image file.
|
|
|
|
|
*
|
|
|
|
|
* Opens the specified image file and returns a pointer to the initialized aaruformat context.
|
2025-09-30 16:03:34 +01:00
|
|
|
* This function performs comprehensive validation of the image file format, reads and processes
|
|
|
|
|
* all index entries, initializes data structures for reading operations, and sets up caches
|
|
|
|
|
* for optimal performance. It supports multiple AaruFormat versions and handles various block
|
|
|
|
|
* types including data blocks, deduplication tables, metadata, and checksums.
|
2025-09-30 13:08:45 +01:00
|
|
|
*
|
|
|
|
|
* @param filepath Path to the image file to open.
|
2025-09-30 16:03:34 +01:00
|
|
|
*
|
|
|
|
|
* @return Returns one of the following:
|
|
|
|
|
* @retval aaruformatContext* Successfully opened and initialized context. The returned pointer contains:
|
|
|
|
|
* - Validated AaruFormat headers and metadata
|
|
|
|
|
* - Processed index entries with all discoverable blocks
|
|
|
|
|
* - Loaded deduplication tables (DDT) for efficient sector access
|
|
|
|
|
* - Initialized block and header caches for performance
|
|
|
|
|
* - Open file stream ready for reading operations
|
|
|
|
|
* - Populated image information and geometry data
|
|
|
|
|
* - ECC context initialized for error correction support
|
|
|
|
|
*
|
|
|
|
|
* @retval NULL Opening failed. The specific error can be determined by checking errno, which will be set to:
|
|
|
|
|
* - AARUF_ERROR_NOT_ENOUGH_MEMORY (-9) when memory allocation fails for:
|
|
|
|
|
* * Context allocation
|
|
|
|
|
* * Readable sector tags bitmap allocation
|
|
|
|
|
* * Application version string allocation
|
|
|
|
|
* * Image version string allocation
|
|
|
|
|
* - AARUF_ERROR_FILE_TOO_SMALL (-2) when file reading fails:
|
|
|
|
|
* * Cannot read the AaruFormat header (file too small or corrupted)
|
|
|
|
|
* * Cannot read the extended header for version 2+ formats
|
|
|
|
|
* - AARUF_ERROR_NOT_AARUFORMAT (-1) when format validation fails:
|
|
|
|
|
* * File identifier doesn't match DIC_MAGIC or AARU_MAGIC
|
|
|
|
|
* * File is not a valid AaruFormat image
|
|
|
|
|
* - AARUF_ERROR_INCOMPATIBLE_VERSION (-3) when:
|
|
|
|
|
* * Image major version exceeds the maximum supported version
|
|
|
|
|
* * Future format versions that cannot be read by this library
|
|
|
|
|
* - AARUF_ERROR_CANNOT_READ_INDEX (-4) when index processing fails:
|
|
|
|
|
* * Cannot seek to the index offset specified in the header
|
|
|
|
|
* * Cannot read the index signature
|
|
|
|
|
* * Index signature is not a recognized index block type
|
|
|
|
|
* * Index processing functions return NULL (corrupted index)
|
|
|
|
|
* - Other error codes may be propagated from block processing functions:
|
|
|
|
|
* * Data block processing errors
|
|
|
|
|
* * DDT processing errors
|
|
|
|
|
* * Metadata processing errors
|
|
|
|
|
*
|
|
|
|
|
* @note Format Support:
|
|
|
|
|
* - Supports AaruFormat versions 1.x and 2.x
|
|
|
|
|
* - Automatically detects and handles different index formats (v1, v2, v3)
|
|
|
|
|
* - Backwards compatible with older DIC format identifiers
|
|
|
|
|
* - Handles both small and large deduplication tables
|
|
|
|
|
*
|
|
|
|
|
* @note Block Processing:
|
2025-10-01 02:54:51 +01:00
|
|
|
* - Processes all indexed blocks including data, DDT, geometry, metadata, tracks, CICM, dump hardware, and
|
|
|
|
|
* checksums
|
2025-09-30 16:03:34 +01:00
|
|
|
* - Non-critical block processing errors are logged but don't prevent opening
|
|
|
|
|
* - Critical errors (DDT processing failures) cause opening to fail
|
|
|
|
|
* - Unknown block types are logged but ignored
|
|
|
|
|
*
|
|
|
|
|
* @note Memory Management:
|
|
|
|
|
* - Allocates memory for various context structures and caches
|
|
|
|
|
* - On failure, all previously allocated memory is properly cleaned up
|
|
|
|
|
* - The returned context must be freed using aaruf_close()
|
|
|
|
|
*
|
|
|
|
|
* @note Performance Optimization:
|
|
|
|
|
* - Initializes block and header caches based on sector size and available memory
|
|
|
|
|
* - Cache sizes are calculated to optimize memory usage and access patterns
|
|
|
|
|
* - ECC context is pre-initialized for Compact Disc support
|
|
|
|
|
*
|
|
|
|
|
* @warning The function requires a valid user data deduplication table to be present.
|
|
|
|
|
* Images without a DDT will fail to open even if otherwise valid.
|
|
|
|
|
*
|
|
|
|
|
* @warning File access is performed in binary read mode. The file must be accessible
|
|
|
|
|
* and not locked by other processes.
|
|
|
|
|
*
|
|
|
|
|
* @warning Some memory allocations (version strings) are optional and failure doesn't
|
|
|
|
|
* prevent opening, but may affect functionality that depends on version information.
|
2025-09-30 13:08:45 +01:00
|
|
|
*/
|
2025-10-11 03:30:19 +01:00
|
|
|
AARU_EXPORT void AARU_CALL *aaruf_open(const char *filepath) // NOLINT(readability-function-size)
|
2019-03-17 20:39:40 +00:00
|
|
|
{
|
2025-10-09 02:33:35 +01:00
|
|
|
aaruformat_context *ctx = NULL;
|
|
|
|
|
int error_no = 0;
|
|
|
|
|
size_t read_bytes = 0;
|
|
|
|
|
long pos = 0;
|
|
|
|
|
int i = 0;
|
|
|
|
|
uint32_t signature = 0;
|
|
|
|
|
UT_array *index_entries = NULL;
|
2025-08-13 20:16:42 +01:00
|
|
|
|
|
|
|
|
#ifdef USE_SLOG
|
|
|
|
|
#include "slog.h"
|
|
|
|
|
|
|
|
|
|
slog_init("aaruformat.log", SLOG_FLAGS_ALL, 0);
|
|
|
|
|
#endif
|
2020-03-01 19:51:13 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Logging initialized");
|
|
|
|
|
|
|
|
|
|
TRACE("Entering aaruf_open(%s)", filepath);
|
|
|
|
|
|
|
|
|
|
TRACE("Allocating memory for context");
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx = (aaruformat_context *)malloc(sizeof(aaruformat_context));
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2022-10-02 17:55:45 +01:00
|
|
|
if(ctx == NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Not enough memory to create context");
|
2022-10-02 17:55:45 +01:00
|
|
|
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2022-10-02 17:55:45 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
memset(ctx, 0, sizeof(aaruformat_context));
|
2025-10-08 19:37:36 +01:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Opening file %s", filepath);
|
2019-03-17 20:39:40 +00:00
|
|
|
ctx->imageStream = fopen(filepath, "rb");
|
|
|
|
|
|
|
|
|
|
if(ctx->imageStream == NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Error %d opening file %s for reading", errno, filepath);
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = errno;
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2025-09-30 15:11:27 +01:00
|
|
|
errno = error_no;
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 20:39:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Reading header at position 0");
|
2019-03-17 20:39:40 +00:00
|
|
|
fseek(ctx->imageStream, 0, SEEK_SET);
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(&ctx->header, 1, sizeof(AaruHeader), ctx->imageStream);
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != sizeof(AaruHeader))
|
2019-03-17 20:39:40 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read header");
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 20:39:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 19:58:09 +00:00
|
|
|
if(ctx->header.identifier != DIC_MAGIC && ctx->header.identifier != AARU_MAGIC)
|
2019-03-17 20:39:40 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Incorrect identifier for AaruFormat file: %8.8s", (char *)&ctx->header.identifier);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_NOT_AARUFORMAT;
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 20:39:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:23:32 +01:00
|
|
|
// Read new header version
|
|
|
|
|
if(ctx->header.imageMajorVersion >= AARUF_VERSION_V2)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Reading new header version at position 0");
|
2025-08-02 20:23:32 +01:00
|
|
|
fseek(ctx->imageStream, 0, SEEK_SET);
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(&ctx->header, 1, sizeof(AaruHeaderV2), ctx->imageStream);
|
2025-08-02 20:23:32 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != sizeof(AaruHeaderV2))
|
2025-08-02 20:23:32 +01:00
|
|
|
{
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2025-08-02 20:23:32 +01:00
|
|
|
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(ctx->header.imageMajorVersion > AARUF_VERSION)
|
2019-03-17 20:39:40 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Incompatible AaruFormat version %d.%d found, maximum supported is %d.%d", ctx->header.imageMajorVersion,
|
|
|
|
|
ctx->header.imageMinorVersion, AARUF_VERSION_V2, 0);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_INCOMPATIBLE_VERSION;
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 20:39:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Opening image version %d.%d", ctx->header.imageMajorVersion, ctx->header.imageMinorVersion);
|
2019-03-17 20:39:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Allocating memory for readable sector tags bitmap");
|
2024-04-30 15:51:32 +01:00
|
|
|
ctx->readableSectorTags = (bool *)malloc(sizeof(bool) * MaxSectorTag);
|
2019-03-31 14:56:03 +01:00
|
|
|
|
|
|
|
|
if(ctx->readableSectorTags == NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not allocate memory for readable sector tags bitmap");
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
2019-03-31 14:56:03 +01:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-31 14:56:03 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(ctx->readableSectorTags, 0, sizeof(bool) * MaxSectorTag);
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Setting up image info");
|
2025-10-09 02:24:09 +01:00
|
|
|
|
2025-10-11 01:26:17 +01:00
|
|
|
// Handle application name based on image version
|
|
|
|
|
memset(ctx->image_info.Application, 0, 64);
|
2025-10-09 02:24:09 +01:00
|
|
|
|
2025-10-11 01:26:17 +01:00
|
|
|
if(ctx->header.imageMajorVersion >= AARUF_VERSION_V2)
|
2019-03-20 22:31:50 +00:00
|
|
|
{
|
2025-10-11 01:26:17 +01:00
|
|
|
// Version 2+: application name is UTF-8, direct copy
|
|
|
|
|
TRACE("Converting application name (v2+): UTF-8 direct copy");
|
|
|
|
|
size_t copy_len = AARU_HEADER_APP_NAME_LEN < 63 ? AARU_HEADER_APP_NAME_LEN : 63;
|
|
|
|
|
memcpy(ctx->image_info.Application, ctx->header.application, copy_len);
|
|
|
|
|
ctx->image_info.Application[63] = '\0';
|
2019-03-20 22:31:50 +00:00
|
|
|
}
|
2025-10-09 02:24:09 +01:00
|
|
|
else
|
2019-03-20 22:31:50 +00:00
|
|
|
{
|
2025-10-11 01:26:17 +01:00
|
|
|
// Version 1: application name is UTF-16LE, convert by taking every other byte
|
|
|
|
|
TRACE("Converting application name (v1): UTF-16LE to ASCII");
|
|
|
|
|
int dest_idx = 0;
|
|
|
|
|
for(int j = 0; j < AARU_HEADER_APP_NAME_LEN && dest_idx < 63; j += 2)
|
|
|
|
|
// Take the low byte, skip the high byte (assuming it's 0x00 for ASCII)
|
|
|
|
|
if(ctx->header.application[j] != 0)
|
|
|
|
|
ctx->image_info.Application[dest_idx++] = ctx->header.application[j];
|
|
|
|
|
else
|
|
|
|
|
// Stop at null terminator
|
|
|
|
|
break;
|
|
|
|
|
ctx->image_info.Application[dest_idx] = '\0';
|
2019-03-20 22:31:50 +00:00
|
|
|
}
|
2025-10-09 02:24:09 +01:00
|
|
|
|
|
|
|
|
// Set application version string directly in the fixed-size array
|
2025-10-09 02:33:35 +01:00
|
|
|
memset(ctx->image_info.ApplicationVersion, 0, 32);
|
|
|
|
|
sprintf(ctx->image_info.ApplicationVersion, "%d.%d", ctx->header.applicationMajorVersion,
|
2025-10-09 02:24:09 +01:00
|
|
|
ctx->header.applicationMinorVersion);
|
|
|
|
|
|
|
|
|
|
// Set image version string directly in the fixed-size array
|
2025-10-09 02:33:35 +01:00
|
|
|
memset(ctx->image_info.Version, 0, 32);
|
|
|
|
|
sprintf(ctx->image_info.Version, "%d.%d", ctx->header.imageMajorVersion, ctx->header.imageMinorVersion);
|
2025-10-09 02:24:09 +01:00
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->image_info.MediaType = ctx->header.mediaType;
|
2019-03-20 22:31:50 +00:00
|
|
|
|
2019-03-17 21:14:40 +00:00
|
|
|
// Read the index header
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Reading index header at position %" PRIu64, ctx->header.indexOffset);
|
2022-10-02 18:12:35 +01:00
|
|
|
pos = fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
2019-03-17 21:14:40 +00:00
|
|
|
if(pos < 0)
|
|
|
|
|
{
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
2019-03-17 21:14:40 +00:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos = ftell(ctx->imageStream);
|
|
|
|
|
if(pos != ctx->header.indexOffset)
|
|
|
|
|
{
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
2019-03-17 21:14:40 +00:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
|
2019-03-17 21:14:40 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != sizeof(uint32_t) ||
|
2025-08-13 20:16:42 +01:00
|
|
|
(signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3))
|
2019-03-17 21:14:40 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read index header or incorrect identifier %4.4s", (char *)&signature);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
2019-03-17 21:14:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 21:14:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 20:16:42 +01:00
|
|
|
if(signature == IndexBlock)
|
|
|
|
|
index_entries = process_index_v1(ctx);
|
|
|
|
|
else if(signature == IndexBlock2)
|
|
|
|
|
index_entries = process_index_v2(ctx);
|
|
|
|
|
else if(signature == IndexBlock3)
|
|
|
|
|
index_entries = process_index_v3(ctx);
|
2019-03-17 21:14:40 +00:00
|
|
|
|
2025-08-01 21:10:36 +01:00
|
|
|
if(index_entries == NULL)
|
2019-03-17 21:14:40 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not process index.");
|
2025-08-01 21:10:36 +01:00
|
|
|
utarray_free(index_entries);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2020-03-01 19:53:05 +00:00
|
|
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
2019-03-17 21:14:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-17 21:14:40 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Index at %" PRIu64 " contains %d entries", ctx->header.indexOffset, utarray_len(index_entries));
|
2025-08-01 21:10:36 +01:00
|
|
|
|
|
|
|
|
for(i = 0; i < utarray_len(index_entries); i++)
|
2019-03-17 21:14:40 +00:00
|
|
|
{
|
2025-10-01 02:54:51 +01:00
|
|
|
IndexEntry *entry = utarray_eltptr(index_entries, i);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Block type %4.4s with data type %d is indexed to be at %" PRIu64 "", (char *)&entry->blockType,
|
2025-08-13 20:16:42 +01:00
|
|
|
entry->dataType, entry->offset);
|
2019-03-17 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
bool found_user_data_ddt = false;
|
|
|
|
|
ctx->image_info.ImageSize = 0;
|
2025-08-01 21:10:36 +01:00
|
|
|
for(i = 0; i < utarray_len(index_entries); i++)
|
2019-03-17 21:32:02 +00:00
|
|
|
{
|
2025-10-01 02:54:51 +01:00
|
|
|
IndexEntry *entry = utarray_eltptr(index_entries, i);
|
2025-08-01 21:10:36 +01:00
|
|
|
pos = fseek(ctx->imageStream, entry->offset, SEEK_SET);
|
2019-03-17 21:32:02 +00:00
|
|
|
|
2025-08-01 21:10:36 +01:00
|
|
|
if(pos < 0 || ftell(ctx->imageStream) != entry->offset)
|
2019-03-17 21:32:02 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...", entry->offset, i);
|
2019-03-17 21:32:02 +00:00
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Processing block type %4.4s with data type %d at position %" PRIu64 "", (char *)&entry->blockType,
|
|
|
|
|
entry->dataType, entry->offset);
|
2025-08-01 21:10:36 +01:00
|
|
|
switch(entry->blockType)
|
2019-03-17 21:32:02 +00:00
|
|
|
{
|
|
|
|
|
case DataBlock:
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = process_data_block(ctx, entry);
|
2019-03-17 22:41:04 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(error_no != AARUF_STATUS_OK)
|
2019-03-17 22:41:04 +00:00
|
|
|
{
|
2025-08-02 14:25:15 +01:00
|
|
|
utarray_free(index_entries);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2025-09-30 15:11:27 +01:00
|
|
|
errno = error_no;
|
2019-03-17 22:41:04 +00:00
|
|
|
|
2025-08-02 14:25:15 +01:00
|
|
|
return NULL;
|
2019-03-17 22:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2025-08-02 14:25:15 +01:00
|
|
|
|
2019-03-31 20:52:06 +01:00
|
|
|
case DeDuplicationTable:
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = process_ddt_v1(ctx, entry, &found_user_data_ddt);
|
2019-03-17 23:19:13 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(error_no != AARUF_STATUS_OK)
|
2019-03-17 23:19:13 +00:00
|
|
|
{
|
2025-08-02 14:52:11 +01:00
|
|
|
utarray_free(index_entries);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2025-09-30 15:11:27 +01:00
|
|
|
errno = error_no;
|
2019-03-17 23:19:13 +00:00
|
|
|
|
2025-08-02 14:52:11 +01:00
|
|
|
return NULL;
|
2019-03-17 23:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 16:31:29 +01:00
|
|
|
break;
|
|
|
|
|
case DeDuplicationTable2:
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = process_ddt_v2(ctx, entry, &found_user_data_ddt);
|
2025-08-04 16:31:29 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(error_no != AARUF_STATUS_OK)
|
2025-08-04 16:31:29 +01:00
|
|
|
{
|
|
|
|
|
utarray_free(index_entries);
|
2025-10-08 19:37:04 +01:00
|
|
|
cleanup_open_failure(ctx);
|
2025-09-30 15:11:27 +01:00
|
|
|
errno = error_no;
|
2025-08-04 16:31:29 +01:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 16:11:26 +01:00
|
|
|
break;
|
2019-03-31 20:52:06 +01:00
|
|
|
case GeometryBlock:
|
2025-08-02 16:11:26 +01:00
|
|
|
process_geometry_block(ctx, entry);
|
2019-03-17 23:29:29 +00:00
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2019-03-31 20:52:06 +01:00
|
|
|
case MetadataBlock:
|
2025-08-02 16:04:55 +01:00
|
|
|
process_metadata_block(ctx, entry);
|
2019-03-20 22:31:50 +00:00
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2019-03-31 20:52:06 +01:00
|
|
|
case TracksBlock:
|
2025-08-02 16:17:57 +01:00
|
|
|
process_tracks_block(ctx, entry);
|
2022-10-12 16:19:48 +01:00
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2019-03-31 20:52:06 +01:00
|
|
|
case CicmBlock:
|
2025-08-02 16:21:50 +01:00
|
|
|
process_cicm_block(ctx, entry);
|
2019-03-18 22:06:10 +00:00
|
|
|
|
2025-10-05 05:06:30 +01:00
|
|
|
break;
|
|
|
|
|
case AaruMetadataJsonBlock:
|
|
|
|
|
process_aaru_metadata_json_block(ctx, entry);
|
|
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2025-08-04 16:31:29 +01:00
|
|
|
// Dump hardware block
|
2019-03-31 20:52:06 +01:00
|
|
|
case DumpHardwareBlock:
|
2025-08-02 16:46:04 +01:00
|
|
|
process_dumphw_block(ctx, entry);
|
2019-03-20 00:23:30 +00:00
|
|
|
|
2022-10-04 19:44:34 +01:00
|
|
|
break;
|
|
|
|
|
case ChecksumBlock:
|
2025-08-02 16:49:53 +01:00
|
|
|
process_checksum_block(ctx, entry);
|
2022-10-04 19:44:34 +01:00
|
|
|
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
2025-10-07 11:30:11 +01:00
|
|
|
case TapeFileBlock:
|
|
|
|
|
process_tape_files_block(ctx, entry);
|
|
|
|
|
|
2025-10-08 19:37:49 +01:00
|
|
|
break;
|
2025-10-07 14:40:57 +01:00
|
|
|
case TapePartitionBlock:
|
|
|
|
|
process_tape_partitions_block(ctx, entry);
|
|
|
|
|
|
2025-10-08 19:37:49 +01:00
|
|
|
break;
|
2019-03-17 21:32:02 +00:00
|
|
|
default:
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "",
|
|
|
|
|
(char *)&entry->blockType, entry->dataType, entry->offset);
|
2019-03-17 21:32:02 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-21 13:49:06 +01:00
|
|
|
ctx->index_entries = index_entries;
|
2019-03-20 22:45:05 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(!found_user_data_ddt)
|
2019-03-20 00:35:11 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not find user data deduplication table, aborting...");
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_close(ctx);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_open() = NULL");
|
2019-03-20 00:35:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->image_info.CreationTime = ctx->header.creationTime;
|
|
|
|
|
ctx->image_info.LastModificationTime = ctx->header.lastWrittenTime;
|
|
|
|
|
ctx->image_info.MetadataMediaType = aaruf_get_xml_mediatype(ctx->header.mediaType);
|
2019-03-20 22:31:50 +00:00
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
if(ctx->geometry_block.identifier != GeometryBlock && ctx->image_info.MetadataMediaType == BlockMedia)
|
2019-03-20 22:31:50 +00:00
|
|
|
{
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->cylinders = (uint32_t)(ctx->image_info.Sectors / 16 / 63);
|
|
|
|
|
ctx->heads = 16;
|
|
|
|
|
ctx->sectors_per_track = 63;
|
2019-03-20 22:31:50 +00:00
|
|
|
}
|
2019-03-20 00:35:11 +00:00
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
// Initialize caches
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Initializing caches");
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->block_header_cache.cache = NULL;
|
|
|
|
|
ctx->block_cache.cache = NULL;
|
2025-10-08 19:39:05 +01:00
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
const uint64_t cache_divisor = (uint64_t)ctx->image_info.SectorSize * (1ULL << ctx->shift);
|
2025-10-08 19:39:05 +01:00
|
|
|
if(cache_divisor == 0)
|
|
|
|
|
{
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->block_header_cache.max_items = 0;
|
|
|
|
|
ctx->block_cache.max_items = 0;
|
2025-10-08 19:39:05 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->block_header_cache.max_items = MAX_CACHE_SIZE / cache_divisor;
|
|
|
|
|
ctx->block_cache.max_items = ctx->block_header_cache.max_items;
|
2025-10-08 19:39:05 +01:00
|
|
|
}
|
2019-03-20 00:35:11 +00:00
|
|
|
|
|
|
|
|
// TODO: Cache tracks and sessions?
|
2019-03-23 22:59:36 +00:00
|
|
|
|
|
|
|
|
// Initialize ECC for Compact Disc
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Initializing ECC for Compact Disc");
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->ecc_cd_context = (CdEccContext *)aaruf_ecc_cd_init();
|
2019-03-20 00:35:11 +00:00
|
|
|
|
2025-10-09 02:33:35 +01:00
|
|
|
ctx->magic = AARU_MAGIC;
|
|
|
|
|
ctx->library_major_version = LIBAARUFORMAT_MAJOR_VERSION;
|
|
|
|
|
ctx->library_minor_version = LIBAARUFORMAT_MINOR_VERSION;
|
2019-03-17 21:14:40 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_open() = %p", ctx);
|
2019-03-17 20:39:40 +00:00
|
|
|
return ctx;
|
2025-10-09 02:24:09 +01:00
|
|
|
}
|