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-20 00:45:47 +00:00
|
|
|
|
2022-10-03 18:15:13 +01:00
|
|
|
#include <stdlib.h>
|
2019-03-21 01:06:21 +00:00
|
|
|
#include <string.h>
|
2019-03-20 00:45:47 +00:00
|
|
|
|
2022-05-28 12:10:04 +01:00
|
|
|
#include <aaruformat.h>
|
|
|
|
|
|
2025-08-05 01:05:31 +01:00
|
|
|
#include "internal.h"
|
2025-08-14 00:38:28 +01:00
|
|
|
#include "log.h"
|
2025-08-05 01:05:31 +01:00
|
|
|
|
2025-09-30 13:08:45 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Reads a media tag from the AaruFormat image.
|
|
|
|
|
*
|
|
|
|
|
* Reads the specified media tag from the image and stores it in the provided buffer.
|
2025-09-30 15:20:15 +01:00
|
|
|
* Media tags contain metadata information about the storage medium such as disc
|
|
|
|
|
* information, lead-in/lead-out data, or manufacturer-specific information.
|
2025-09-30 13:08:45 +01:00
|
|
|
*
|
|
|
|
|
* @param context Pointer to the aaruformat context.
|
2025-09-30 15:20:15 +01:00
|
|
|
* @param data Pointer to the buffer to store the tag data. Can be NULL to query tag length.
|
2025-09-30 13:08:45 +01:00
|
|
|
* @param tag Tag identifier to read.
|
2025-09-30 15:20:15 +01:00
|
|
|
* @param length Pointer to the length of the buffer on input; updated with actual tag length on output.
|
|
|
|
|
*
|
|
|
|
|
* @return AARUF_STATUS_OK on success,
|
|
|
|
|
* AARUF_ERROR_NOT_AARUFORMAT if context is NULL or invalid (magic number mismatch),
|
|
|
|
|
* AARUF_ERROR_MEDIA_TAG_NOT_PRESENT if the requested media tag identifier does not exist in the image,
|
|
|
|
|
* AARUF_ERROR_BUFFER_TOO_SMALL if data is NULL or provided buffer size is insufficient for the tag data.
|
2025-09-30 13:08:45 +01:00
|
|
|
*/
|
2024-04-30 15:51:32 +01:00
|
|
|
int32_t aaruf_read_media_tag(void *context, uint8_t *data, int32_t tag, uint32_t *length)
|
2019-03-20 00:45:47 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Entering aaruf_read_media_tag(%p, %p, %d, %u)", context, data, tag, *length);
|
|
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
aaruformatContext *ctx;
|
|
|
|
|
mediaTagEntry *item;
|
2019-03-21 00:18:48 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(context == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
TRACE("Exiting aaruf_read_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-20 00:45:47 +00:00
|
|
|
|
2019-03-21 01:06:21 +00:00
|
|
|
ctx = context;
|
2019-03-20 00:45:47 +00:00
|
|
|
|
2020-03-01 19:51:13 +00:00
|
|
|
// Not a libaaruformat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->magic != AARU_MAGIC)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
TRACE("Exiting aaruf_read_media_tag() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-20 00:45:47 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Finding media tag %d", tag);
|
2022-10-04 20:32:26 +01:00
|
|
|
HASH_FIND_INT(ctx->mediaTags, &tag, item);
|
2019-03-20 00:45:47 +00:00
|
|
|
|
2022-10-04 20:32:26 +01:00
|
|
|
if(item == NULL)
|
2019-03-20 00:45:47 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Media tag not found");
|
2022-10-04 20:32:26 +01:00
|
|
|
*length = 0;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_media_tag() = AARUF_ERROR_MEDIA_TAG_NOT_PRESENT");
|
2022-10-04 20:32:26 +01:00
|
|
|
return AARUF_ERROR_MEDIA_TAG_NOT_PRESENT;
|
2019-03-20 00:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 20:32:26 +01:00
|
|
|
if(data == NULL || *length < item->length)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Buffer too small for media tag %d, required %u bytes", tag, item->length);
|
2022-10-04 20:32:26 +01:00
|
|
|
*length = item->length;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_media_tag() = AARUF_ERROR_BUFFER_TOO_SMALL");
|
2022-10-04 20:32:26 +01:00
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*length = item->length;
|
|
|
|
|
memcpy(data, item->data, item->length);
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Media tag %d read successfully, length %u", tag, *length);
|
|
|
|
|
TRACE("Exiting aaruf_read_media_tag() = AARUF_STATUS_OK");
|
2022-10-04 20:32:26 +01:00
|
|
|
return AARUF_STATUS_OK;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:20:15 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Reads a sector from the AaruFormat image.
|
|
|
|
|
*
|
|
|
|
|
* Reads user data from the specified sector address in the image. This function
|
|
|
|
|
* reads only the user data portion of the sector, without any additional metadata
|
|
|
|
|
* or ECC/EDC information.
|
|
|
|
|
*
|
|
|
|
|
* @param context Pointer to the aaruformat context.
|
|
|
|
|
* @param sector_address The logical sector address to read from.
|
|
|
|
|
* @param data Pointer to buffer where sector data will be stored. Can be NULL to query length.
|
|
|
|
|
* @param length Pointer to variable containing buffer size on input, actual data length on output.
|
|
|
|
|
*
|
|
|
|
|
* @return AARUF_STATUS_OK on success,
|
|
|
|
|
* AARUF_STATUS_SECTOR_NOT_DUMPED if sector was not dumped during imaging,
|
|
|
|
|
* AARUF_ERROR_NOT_AARUFORMAT if context is NULL or invalid (magic number mismatch),
|
|
|
|
|
* AARUF_ERROR_SECTOR_OUT_OF_BOUNDS if sector address exceeds image sector count,
|
|
|
|
|
* AARUF_ERROR_BUFFER_TOO_SMALL if data is NULL or buffer size is insufficient,
|
|
|
|
|
* AARUF_ERROR_NOT_ENOUGH_MEMORY if memory allocation fails for block operations,
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_HEADER if block header cannot be read from image stream,
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_BLOCK if block data cannot be read from image stream,
|
|
|
|
|
* AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK if LZMA or FLAC decompression fails,
|
|
|
|
|
* AARUF_ERROR_UNSUPPORTED_COMPRESSION if block uses unsupported compression algorithm,
|
|
|
|
|
* or other error codes from DDT decoding functions.
|
|
|
|
|
*/
|
2025-09-30 15:11:27 +01:00
|
|
|
int32_t aaruf_read_sector(void *context, uint64_t sector_address, uint8_t *data, uint32_t *length)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Entering aaruf_read_sector(%p, %" PRIu64 ", %p, %u)", context, sector_address, data, *length);
|
|
|
|
|
|
|
|
|
|
aaruformatContext *ctx = NULL;
|
|
|
|
|
uint64_t offset = 0;
|
|
|
|
|
uint64_t block_offset = 0;
|
|
|
|
|
BlockHeader *block_header = NULL;
|
|
|
|
|
uint8_t *block = NULL;
|
|
|
|
|
size_t read_bytes = 0;
|
|
|
|
|
uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
|
|
|
|
|
size_t lzma_size = 0;
|
|
|
|
|
uint8_t *cmp_data = NULL;
|
|
|
|
|
int error_no = 0;
|
|
|
|
|
uint8_t sector_status = 0;
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(context == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-21 01:06:21 +00:00
|
|
|
|
|
|
|
|
ctx = context;
|
|
|
|
|
|
2020-03-01 19:51:13 +00:00
|
|
|
// Not a libaaruformat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->magic != AARU_MAGIC)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(sector_address > ctx->imageInfo.Sectors - 1)
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
FATAL("Sector address out of bounds");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_SECTOR_OUT_OF_BOUNDS");
|
|
|
|
|
return AARUF_ERROR_SECTOR_OUT_OF_BOUNDS;
|
|
|
|
|
}
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-08-05 01:05:31 +01:00
|
|
|
if(ctx->ddtVersion == 1)
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = decode_ddt_entry_v1(ctx, sector_address, &offset, &block_offset, §or_status);
|
2025-08-05 01:05:31 +01:00
|
|
|
else if(ctx->ddtVersion == 2)
|
2025-09-30 15:11:27 +01:00
|
|
|
error_no = decode_ddt_entry_v2(ctx, sector_address, &offset, &block_offset, §or_status);
|
2025-08-05 01:05:31 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(error_no != AARUF_STATUS_OK)
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
2025-09-30 15:11:27 +01:00
|
|
|
FATAL("Error %d decoding DDT entry", error_no);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Exiting aaruf_read_sector() = %d", error_no);
|
|
|
|
|
return error_no;
|
2025-08-14 00:38:28 +01:00
|
|
|
}
|
2019-03-21 01:06:21 +00:00
|
|
|
|
|
|
|
|
// Partially written image... as we can't know the real sector size just assume it's common :/
|
2025-09-30 15:11:27 +01:00
|
|
|
if(sector_status == SectorStatusNotDumped)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
|
|
|
|
*length = ctx->imageInfo.SectorSize;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_SECTOR_NOT_DUMPED");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_STATUS_SECTOR_NOT_DUMPED;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
// Check if block header is cached
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Checking if block header is cached");
|
2025-09-30 15:11:27 +01:00
|
|
|
block_header = find_in_cache_uint64(&ctx->blockHeaderCache, block_offset);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
|
|
|
|
// Read block header
|
2025-09-30 15:11:27 +01:00
|
|
|
if(block_header == NULL)
|
2022-10-02 16:05:25 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Allocating memory for block header");
|
2025-09-30 15:11:27 +01:00
|
|
|
block_header = malloc(sizeof(BlockHeader));
|
|
|
|
|
if(block_header == NULL)
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
FATAL("Not enough memory for block header");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
2022-10-02 16:05:25 +01:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Reading block header");
|
2025-09-30 15:11:27 +01:00
|
|
|
fseek(ctx->imageStream, block_offset, SEEK_SET);
|
|
|
|
|
read_bytes = fread(block_header, 1, sizeof(BlockHeader), ctx->imageStream);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != sizeof(BlockHeader))
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
FATAL("Error reading block header");
|
2019-03-22 21:22:36 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_READ_HEADER");
|
|
|
|
|
return AARUF_ERROR_CANNOT_READ_HEADER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TRACE("Adding block header to cache");
|
2025-09-30 15:11:27 +01:00
|
|
|
add_to_cache_uint64(&ctx->blockHeaderCache, block_offset, block_header);
|
2022-10-02 16:05:25 +01:00
|
|
|
}
|
|
|
|
|
else
|
2025-09-30 15:11:27 +01:00
|
|
|
fseek(ctx->imageStream, block_offset + sizeof(BlockHeader), SEEK_SET); // Advance as if reading the header
|
2022-10-02 16:05:25 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(data == NULL || *length < block_header->sectorSize)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Buffer too small for sector, required %u bytes", block_header->sectorSize);
|
|
|
|
|
*length = block_header->sectorSize;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_BUFFER_TOO_SMALL");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
// Check if block is cached
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Checking if block is cached");
|
2025-09-30 15:11:27 +01:00
|
|
|
block = find_in_cache_uint64(&ctx->blockCache, block_offset);
|
2022-10-02 16:05:25 +01:00
|
|
|
|
|
|
|
|
if(block != NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Getting data from cache");
|
2025-09-30 15:11:27 +01:00
|
|
|
memcpy(data, block + offset * block_header->sectorSize, block_header->sectorSize);
|
|
|
|
|
*length = block_header->sectorSize;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
|
2022-10-02 16:05:25 +01:00
|
|
|
return AARUF_STATUS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 01:06:21 +00:00
|
|
|
// Decompress block
|
2025-09-30 15:11:27 +01:00
|
|
|
switch(block_header->compression)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2019-03-31 20:52:06 +01:00
|
|
|
case None:
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Allocating memory for block");
|
2025-09-30 15:11:27 +01:00
|
|
|
block = (uint8_t *)malloc(block_header->length);
|
2025-08-14 00:38:28 +01:00
|
|
|
if(block == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Not enough memory for block");
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TRACE("Reading block into memory");
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(block, 1, block_header->length, ctx->imageStream);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != block_header->length)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read block");
|
2019-03-21 01:06:21 +00:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_READ_BLOCK");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-12 13:10:44 +01:00
|
|
|
break;
|
|
|
|
|
case Lzma:
|
2025-09-30 15:11:27 +01:00
|
|
|
lzma_size = block_header->cmpLength - LZMA_PROPERTIES_LENGTH;
|
|
|
|
|
TRACE("Allocating memory for compressed data of size %zu bytes", lzma_size);
|
|
|
|
|
cmp_data = malloc(lzma_size);
|
2022-10-12 13:10:44 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(cmp_data == NULL)
|
2022-10-12 13:10:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Cannot allocate memory for block...");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Allocating memory for block of size %zu bytes", block_header->length);
|
|
|
|
|
block = malloc(block_header->length);
|
2022-10-12 13:10:44 +01:00
|
|
|
if(block == NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Cannot allocate memory for block...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
2022-10-12 13:10:44 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != LZMA_PROPERTIES_LENGTH)
|
2022-10-12 13:10:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read LZMA properties...");
|
2022-10-12 13:10:44 +01:00
|
|
|
free(block);
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
|
|
|
|
|
if(read_bytes != lzma_size)
|
2022-10-12 13:10:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read compressed block...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:10:44 +01:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
2022-10-12 13:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Decompressing block of size %zu bytes", block_header->length);
|
|
|
|
|
read_bytes = block_header->length;
|
|
|
|
|
error_no = aaruf_lzma_decode_buffer(block, &read_bytes, cmp_data, &lzma_size, lzma_properties,
|
|
|
|
|
LZMA_PROPERTIES_LENGTH);
|
2022-10-12 13:10:44 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(error_no != 0)
|
2022-10-12 13:10:44 +01:00
|
|
|
{
|
2025-09-30 15:11:27 +01:00
|
|
|
FATAL("Got error %d from LZMA...", error_no);
|
|
|
|
|
free(cmp_data);
|
2022-10-12 13:10:44 +01:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != block_header->length)
|
2022-10-12 13:10:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:16:44 +01:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
2022-10-12 13:16:44 +01:00
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:16:44 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Flac:
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Allocating memory for compressed data of size %zu bytes", block_header->cmpLength);
|
|
|
|
|
cmp_data = malloc(block_header->cmpLength);
|
2022-10-12 13:16:44 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(cmp_data == NULL)
|
2022-10-12 13:16:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Cannot allocate memory for block...");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
2022-10-12 13:16:44 +01:00
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Allocating memory for block of size %zu bytes", block_header->length);
|
|
|
|
|
block = malloc(block_header->length);
|
2022-10-12 13:16:44 +01:00
|
|
|
if(block == NULL)
|
|
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Cannot allocate memory for block...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
2022-10-12 13:16:44 +01:00
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Reading compressed data into memory");
|
2025-09-30 15:11:27 +01:00
|
|
|
read_bytes = fread(cmp_data, 1, block_header->cmpLength, ctx->imageStream);
|
|
|
|
|
if(read_bytes != block_header->cmpLength)
|
2022-10-12 13:16:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Could not read compressed block...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:16:44 +01:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
2022-10-12 13:16:44 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
TRACE("Decompressing block of size %zu bytes", block_header->length);
|
|
|
|
|
read_bytes =
|
|
|
|
|
aaruf_flac_decode_redbook_buffer(block, block_header->length, cmp_data, block_header->cmpLength);
|
2022-10-12 13:16:44 +01:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
if(read_bytes != block_header->length)
|
2022-10-12 13:16:44 +01:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:10:44 +01:00
|
|
|
free(block);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
free(cmp_data);
|
2022-10-12 13:10:44 +01:00
|
|
|
|
2019-03-21 01:06:21 +00:00
|
|
|
break;
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
2025-09-30 15:11:27 +01:00
|
|
|
FATAL("Unsupported compression %d", block_header->compression);
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_UNSUPPORTED_COMPRESSION");
|
2024-04-30 15:51:32 +01:00
|
|
|
return AARUF_ERROR_UNSUPPORTED_COMPRESSION;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add block to cache
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Adding block to cache");
|
2025-09-30 15:11:27 +01:00
|
|
|
add_to_cache_uint64(&ctx->blockCache, block_offset, block);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2025-09-30 15:11:27 +01:00
|
|
|
memcpy(data, block + (offset * block_header->sectorSize), block_header->sectorSize);
|
|
|
|
|
*length = block_header->sectorSize;
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_STATUS_OK;
|
2019-03-23 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:20:15 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Reads a sector from a specific track in the AaruFormat image.
|
|
|
|
|
*
|
|
|
|
|
* Reads user data from the specified sector address within a particular track.
|
|
|
|
|
* This function is specifically designed for optical disc images where sectors
|
|
|
|
|
* are organized by tracks. The sector address is relative to the start of the track.
|
|
|
|
|
*
|
|
|
|
|
* @param context Pointer to the aaruformat context.
|
|
|
|
|
* @param data Pointer to buffer where sector data will be stored.
|
|
|
|
|
* @param sector_address The sector address relative to the track start.
|
|
|
|
|
* @param length Pointer to variable containing buffer size on input, actual data length on output.
|
|
|
|
|
* @param track The track number to read from.
|
|
|
|
|
*
|
|
|
|
|
* @return AARUF_STATUS_OK on success,
|
|
|
|
|
* AARUF_STATUS_SECTOR_NOT_DUMPED if sector was not dumped during imaging,
|
|
|
|
|
* AARUF_ERROR_NOT_AARUFORMAT if context is NULL or invalid (magic number mismatch),
|
|
|
|
|
* AARUF_ERROR_INCORRECT_MEDIA_TYPE if media is not an optical disc,
|
|
|
|
|
* AARUF_ERROR_TRACK_NOT_FOUND if the specified track sequence does not exist,
|
|
|
|
|
* AARUF_ERROR_SECTOR_OUT_OF_BOUNDS if calculated sector address exceeds image bounds,
|
|
|
|
|
* AARUF_ERROR_BUFFER_TOO_SMALL if data buffer is NULL or insufficient size,
|
|
|
|
|
* AARUF_ERROR_NOT_ENOUGH_MEMORY if memory allocation fails during sector reading,
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_HEADER if block header cannot be read from image stream,
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_BLOCK if block data cannot be read from image stream,
|
|
|
|
|
* AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK if LZMA or FLAC decompression fails,
|
|
|
|
|
* AARUF_ERROR_UNSUPPORTED_COMPRESSION if block uses unsupported compression,
|
|
|
|
|
* or other error codes from underlying aaruf_read_sector function.
|
|
|
|
|
*/
|
2024-04-30 15:51:32 +01:00
|
|
|
int32_t aaruf_read_track_sector(void *context, uint8_t *data, uint64_t sectorAddress, uint32_t *length, uint8_t track)
|
2019-03-23 23:31:04 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Entering aaruf_read_track_sector(%p, %p, %" PRIu64 ", %u, %d)", context, data, sectorAddress, *length,
|
|
|
|
|
track);
|
|
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
aaruformatContext *ctx;
|
2020-03-01 19:51:13 +00:00
|
|
|
int i;
|
2019-03-23 23:31:04 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(context == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_track_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-23 23:31:04 +00:00
|
|
|
|
|
|
|
|
ctx = context;
|
|
|
|
|
|
2020-03-01 19:51:13 +00:00
|
|
|
// Not a libaaruformat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->magic != AARU_MAGIC)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
2019-03-23 23:31:04 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_read_track_sector() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-23 23:31:04 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->imageInfo.XmlMediaType != OpticalDisc)
|
2019-03-23 23:31:04 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Incorrect media type %d, expected OpticalDisc", ctx->imageInfo.XmlMediaType);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_track_sector() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
|
|
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < ctx->numberOfDataTracks; i++)
|
2019-03-23 23:31:04 +00:00
|
|
|
if(ctx->dataTracks[i].sequence == track)
|
2022-05-28 12:10:04 +01:00
|
|
|
return aaruf_read_sector(context, ctx->dataTracks[i].start + sectorAddress, data, length);
|
2019-03-23 23:31:04 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Track %d not found", track);
|
|
|
|
|
TRACE("Exiting aaruf_read_track_sector() = AARUF_ERROR_TRACK_NOT_FOUND");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_TRACK_NOT_FOUND;
|
2019-03-23 23:31:04 +00:00
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2025-09-30 15:20:15 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Reads a complete sector with all metadata from the AaruFormat image.
|
|
|
|
|
*
|
|
|
|
|
* Reads the complete sector data including user data, ECC/EDC, subchannel data,
|
|
|
|
|
* and other metadata depending on the media type. For optical discs, this returns
|
|
|
|
|
* a full 2352-byte sector with sync, header, user data, and ECC/EDC. For block
|
|
|
|
|
* media with tags, this includes both the user data and tag information.
|
|
|
|
|
*
|
|
|
|
|
* @param context Pointer to the aaruformat context.
|
|
|
|
|
* @param sectorAddress The logical sector address to read from.
|
|
|
|
|
* @param data Pointer to buffer where complete sector data will be stored. Can be NULL to query length.
|
|
|
|
|
* @param length Pointer to variable containing buffer size on input, actual data length on output.
|
|
|
|
|
*
|
|
|
|
|
* @return AARUF_STATUS_OK on success,
|
|
|
|
|
* AARUF_STATUS_SECTOR_NOT_DUMPED if sector was not dumped during imaging,
|
|
|
|
|
* AARUF_ERROR_NOT_AARUFORMAT if context is NULL or invalid (magic number mismatch),
|
|
|
|
|
* AARUF_ERROR_BUFFER_TOO_SMALL if data is NULL or buffer size insufficient for complete sector,
|
|
|
|
|
* AARUF_ERROR_NOT_ENOUGH_MEMORY if memory allocation fails for bare data operations,
|
|
|
|
|
* AARUF_ERROR_TRACK_NOT_FOUND if sector's track cannot be found in track list,
|
|
|
|
|
* AARUF_ERROR_INCORRECT_MEDIA_TYPE if media type doesn't support long sector reading,
|
|
|
|
|
* AARUF_ERROR_INVALID_TRACK_FORMAT if track has an unsupported or invalid format,
|
|
|
|
|
* AARUF_ERROR_REACHED_UNREACHABLE_CODE if internal logic reaches unexpected state,
|
|
|
|
|
* AARUF_ERROR_SECTOR_OUT_OF_BOUNDS if sector address exceeds image bounds (from aaruf_read_sector),
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_HEADER if block header cannot be read (from aaruf_read_sector),
|
|
|
|
|
* AARUF_ERROR_CANNOT_READ_BLOCK if block data cannot be read (from aaruf_read_sector),
|
|
|
|
|
* AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK if decompression fails (from aaruf_read_sector),
|
|
|
|
|
* AARUF_ERROR_UNSUPPORTED_COMPRESSION if compression algorithm not supported (from aaruf_read_sector),
|
|
|
|
|
* or other error codes from underlying aaruf_read_sector function calls.
|
|
|
|
|
*/
|
2024-04-30 15:51:32 +01:00
|
|
|
int32_t aaruf_read_sector_long(void *context, uint64_t sectorAddress, uint8_t *data, uint32_t *length)
|
2019-03-24 23:17:56 +00:00
|
|
|
{
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Entering aaruf_read_sector_long(%p, %" PRIu64 ", %p, %u)", context, sectorAddress, data, *length);
|
|
|
|
|
|
2025-08-05 01:05:31 +01:00
|
|
|
aaruformatContext *ctx = NULL;
|
|
|
|
|
uint32_t bareLength = 0;
|
|
|
|
|
uint32_t tagLength = 0;
|
|
|
|
|
uint8_t *bareData = NULL;
|
|
|
|
|
int32_t res = 0;
|
2020-03-01 19:59:51 +00:00
|
|
|
TrackEntry trk;
|
2025-08-05 01:05:31 +01:00
|
|
|
int i = 0;
|
|
|
|
|
bool trkFound = false;
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(context == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
ctx = context;
|
|
|
|
|
|
2020-03-01 19:51:13 +00:00
|
|
|
// Not a libaaruformat context
|
2025-08-14 00:38:28 +01:00
|
|
|
if(ctx->magic != AARU_MAGIC)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Invalid context");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_NOT_AARUFORMAT");
|
|
|
|
|
return AARUF_ERROR_NOT_AARUFORMAT;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
switch(ctx->imageInfo.XmlMediaType)
|
|
|
|
|
{
|
|
|
|
|
case OpticalDisc:
|
|
|
|
|
if(*length < 2352 || data == NULL)
|
|
|
|
|
{
|
|
|
|
|
*length = 2352;
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Buffer too small for sector, required %u bytes", *length);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_BUFFER_TOO_SMALL");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
if((ctx->sectorSuffix == NULL || ctx->sectorPrefix == NULL) &&
|
|
|
|
|
(ctx->sectorSuffixCorrected == NULL || ctx->sectorPrefixCorrected == NULL))
|
2022-05-28 12:01:55 +01:00
|
|
|
return aaruf_read_sector(context, sectorAddress, data, length);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
bareLength = 0;
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_read_sector(context, sectorAddress, NULL, &bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Allocating memory for bare data");
|
2024-04-30 15:51:32 +01:00
|
|
|
bareData = (uint8_t *)malloc(bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(bareData == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Could not allocate memory for bare data");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2022-05-28 12:01:55 +01:00
|
|
|
res = aaruf_read_sector(context, sectorAddress, bareData, &bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2022-10-12 16:21:25 +01:00
|
|
|
if(res < AARUF_STATUS_OK)
|
|
|
|
|
{
|
|
|
|
|
free(bareData);
|
2025-08-14 00:38:28 +01:00
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = %d", res);
|
2022-10-12 16:21:25 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
trkFound = false;
|
|
|
|
|
|
|
|
|
|
for(i = 0; i < ctx->numberOfDataTracks; i++)
|
2022-10-12 16:21:56 +01:00
|
|
|
if(sectorAddress >= ctx->dataTracks[i].start && sectorAddress <= ctx->dataTracks[i].end)
|
2019-03-24 23:17:56 +00:00
|
|
|
{
|
|
|
|
|
trkFound = true;
|
|
|
|
|
trk = ctx->dataTracks[i];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(!trkFound)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Track not found");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_TRACK_NOT_FOUND");
|
|
|
|
|
return AARUF_ERROR_TRACK_NOT_FOUND;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
switch(trk.type)
|
|
|
|
|
{
|
|
|
|
|
case Audio:
|
2024-04-30 15:51:32 +01:00
|
|
|
case Data:
|
|
|
|
|
memcpy(data, bareData, bareLength);
|
|
|
|
|
return res;
|
2019-03-31 20:52:06 +01:00
|
|
|
case CdMode1:
|
2022-10-12 16:22:12 +01:00
|
|
|
memcpy(data + 16, bareData, 2048);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
if(ctx->sectorPrefix != NULL)
|
|
|
|
|
memcpy(data, ctx->sectorPrefix + (sectorAddress * 16), 16);
|
2019-03-24 23:17:56 +00:00
|
|
|
else if(ctx->sectorPrefixDdt != NULL)
|
|
|
|
|
{
|
|
|
|
|
if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
|
|
|
|
|
{
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_ecc_cd_reconstruct_prefix(data, trk.type, sectorAddress);
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_OK;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
else if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_SECTOR_NOT_DUMPED;
|
2019-03-24 23:17:56 +00:00
|
|
|
else
|
|
|
|
|
memcpy(data,
|
|
|
|
|
ctx->sectorPrefixCorrected +
|
2019-03-31 20:52:06 +01:00
|
|
|
((ctx->sectorPrefixDdt[sectorAddress] & CD_DFIX_MASK) - 1) * 16,
|
2019-03-24 23:17:56 +00:00
|
|
|
16);
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_REACHED_UNREACHABLE_CODE");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
2025-08-14 00:38:28 +01:00
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2022-10-12 16:22:33 +01:00
|
|
|
if(res != AARUF_STATUS_OK) return res;
|
|
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
if(ctx->sectorSuffix != NULL)
|
|
|
|
|
memcpy(data + 2064, ctx->sectorSuffix + sectorAddress * 288, 288);
|
2019-03-24 23:17:56 +00:00
|
|
|
else if(ctx->sectorSuffixDdt != NULL)
|
|
|
|
|
{
|
|
|
|
|
if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
|
|
|
|
|
{
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_ecc_cd_reconstruct(ctx->eccCdContext, data, trk.type);
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_OK;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
else if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_SECTOR_NOT_DUMPED;
|
2019-03-24 23:17:56 +00:00
|
|
|
else
|
|
|
|
|
memcpy(data + 2064,
|
|
|
|
|
ctx->sectorSuffixCorrected +
|
2019-03-31 20:52:06 +01:00
|
|
|
((ctx->sectorSuffixDdt[sectorAddress] & CD_DFIX_MASK) - 1) * 288,
|
2019-03-24 23:17:56 +00:00
|
|
|
288);
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_REACHED_UNREACHABLE_CODE");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
2025-08-14 00:38:28 +01:00
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
case CdMode2Formless:
|
|
|
|
|
case CdMode2Form1:
|
|
|
|
|
case CdMode2Form2:
|
2024-04-30 15:51:32 +01:00
|
|
|
if(ctx->sectorPrefix != NULL)
|
|
|
|
|
memcpy(data, ctx->sectorPrefix + sectorAddress * 16, 16);
|
2019-03-24 23:17:56 +00:00
|
|
|
else if(ctx->sectorPrefixDdt != NULL)
|
|
|
|
|
{
|
|
|
|
|
if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
|
|
|
|
|
{
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_ecc_cd_reconstruct_prefix(data, trk.type, sectorAddress);
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_OK;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
else if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_SECTOR_NOT_DUMPED;
|
2019-03-24 23:17:56 +00:00
|
|
|
else
|
|
|
|
|
memcpy(data,
|
|
|
|
|
ctx->sectorPrefixCorrected +
|
2019-03-31 20:52:06 +01:00
|
|
|
((ctx->sectorPrefixDdt[sectorAddress] & CD_DFIX_MASK) - 1) * 16,
|
2019-03-24 23:17:56 +00:00
|
|
|
16);
|
|
|
|
|
}
|
|
|
|
|
else
|
2025-08-14 00:38:28 +01:00
|
|
|
{
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_REACHED_UNREACHABLE_CODE");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
2025-08-14 00:38:28 +01:00
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2022-10-12 16:22:33 +01:00
|
|
|
if(res != AARUF_STATUS_OK) return res;
|
|
|
|
|
|
2019-03-24 23:17:56 +00:00
|
|
|
if(ctx->mode2Subheaders != NULL && ctx->sectorSuffixDdt != NULL)
|
|
|
|
|
{
|
|
|
|
|
memcpy(data + 16, ctx->mode2Subheaders + sectorAddress * 8, 8);
|
|
|
|
|
|
|
|
|
|
if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Mode2Form1Ok)
|
|
|
|
|
{
|
|
|
|
|
memcpy(data + 24, bareData, 2048);
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_ecc_cd_reconstruct(ctx->eccCdContext, data, CdMode2Form1);
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
else if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Mode2Form2Ok ||
|
|
|
|
|
(ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Mode2Form2NoCrc)
|
|
|
|
|
{
|
|
|
|
|
memcpy(data + 24, bareData, 2324);
|
|
|
|
|
if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Mode2Form2Ok)
|
2022-05-28 12:01:55 +01:00
|
|
|
aaruf_ecc_cd_reconstruct(ctx->eccCdContext, data, CdMode2Form2);
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
2019-08-15 20:55:54 +01:00
|
|
|
else if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
|
2020-03-01 19:53:05 +00:00
|
|
|
res = AARUF_STATUS_SECTOR_NOT_DUMPED;
|
2019-08-15 20:55:54 +01:00
|
|
|
else
|
|
|
|
|
// Mode 2 where ECC failed
|
|
|
|
|
memcpy(data + 24, bareData, 2328);
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
else if(ctx->mode2Subheaders != NULL)
|
|
|
|
|
{
|
|
|
|
|
memcpy(data + 16, ctx->mode2Subheaders + sectorAddress * 8, 8);
|
|
|
|
|
memcpy(data + 24, bareData, 2328);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
memcpy(data + 16, bareData, 2336);
|
|
|
|
|
|
|
|
|
|
return res;
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Invalid track type %d", trk.type);
|
|
|
|
|
free(bareData);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INVALID_TRACK_FORMAT");
|
2024-04-30 15:51:32 +01:00
|
|
|
return AARUF_ERROR_INVALID_TRACK_FORMAT;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
case BlockMedia:
|
|
|
|
|
switch(ctx->imageInfo.MediaType)
|
|
|
|
|
{
|
|
|
|
|
case AppleFileWare:
|
|
|
|
|
case AppleProfile:
|
|
|
|
|
case AppleSonySS:
|
|
|
|
|
case AppleSonyDS:
|
|
|
|
|
case AppleWidget:
|
|
|
|
|
case PriamDataTower:
|
2022-05-28 12:01:55 +01:00
|
|
|
if(ctx->sectorSubchannel == NULL) return aaruf_read_sector(context, sectorAddress, data, length);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
switch(ctx->imageInfo.MediaType)
|
|
|
|
|
{
|
|
|
|
|
case AppleFileWare:
|
|
|
|
|
case AppleProfile:
|
2024-04-30 15:51:32 +01:00
|
|
|
case AppleWidget:
|
|
|
|
|
tagLength = 20;
|
|
|
|
|
break;
|
2019-03-24 23:17:56 +00:00
|
|
|
case AppleSonySS:
|
2024-04-30 15:51:32 +01:00
|
|
|
case AppleSonyDS:
|
|
|
|
|
tagLength = 12;
|
|
|
|
|
break;
|
|
|
|
|
case PriamDataTower:
|
|
|
|
|
tagLength = 24;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Unsupported media type %d", ctx->imageInfo.MediaType);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
|
2024-04-30 15:51:32 +01:00
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bareLength = 512;
|
|
|
|
|
|
|
|
|
|
if(*length < tagLength + bareLength || data == NULL)
|
|
|
|
|
{
|
|
|
|
|
*length = tagLength + bareLength;
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Buffer too small for sector, required %u bytes", *length);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_BUFFER_TOO_SMALL");
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Allocating memory for bare data of size %u bytes", bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
bareData = malloc(bareLength);
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(bareData == NULL)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Could not allocate memory for bare data");
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2022-05-28 12:01:55 +01:00
|
|
|
res = aaruf_read_sector(context, sectorAddress, bareData, &bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
if(bareLength != 512)
|
|
|
|
|
{
|
|
|
|
|
FATAL("Bare data length is %u, expected 512", bareLength);
|
|
|
|
|
free(bareData);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = %d", res);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
memcpy(data, ctx->sectorSubchannel + sectorAddress * tagLength, tagLength);
|
|
|
|
|
memcpy(data, bareData, 512);
|
|
|
|
|
|
|
|
|
|
free(bareData);
|
|
|
|
|
|
2025-08-14 00:38:28 +01:00
|
|
|
TRACE("Exiting aaruf_read_sector_long() = %d", res);
|
2019-03-24 23:17:56 +00:00
|
|
|
return res;
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Incorrect media type %d for long sector reading", ctx->imageInfo.MediaType);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
|
2024-04-30 15:51:32 +01:00
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
2025-08-14 00:38:28 +01:00
|
|
|
FATAL("Incorrect media type %d for long sector reading", ctx->imageInfo.MediaType);
|
|
|
|
|
|
|
|
|
|
TRACE("Exiting aaruf_read_sector_long() = AARUF_ERROR_INCORRECT_MEDIA_TYPE");
|
2024-04-30 15:51:32 +01:00
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
2025-09-30 15:20:15 +01:00
|
|
|
}
|