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>
|
|
|
|
|
|
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
|
|
|
{
|
2024-04-30 15:51:32 +01:00
|
|
|
aaruformatContext *ctx;
|
|
|
|
|
mediaTagEntry *item;
|
2019-03-21 00:18:48 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(context == NULL) 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
|
2020-03-01 19:58:09 +00:00
|
|
|
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
2019-03-20 00:45:47 +00:00
|
|
|
|
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
|
|
|
{
|
2022-10-04 20:32:26 +01:00
|
|
|
*length = 0;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
*length = item->length;
|
|
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*length = item->length;
|
|
|
|
|
memcpy(data, item->data, item->length);
|
|
|
|
|
|
|
|
|
|
return AARUF_STATUS_OK;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint32_t *length)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2025-08-01 15:34:36 +01:00
|
|
|
aaruformatContext *ctx = NULL;
|
|
|
|
|
uint64_t ddtEntry = 0;
|
|
|
|
|
uint32_t offsetMask = 0;
|
|
|
|
|
uint64_t offset = 0;
|
|
|
|
|
uint64_t blockOffset = 0;
|
|
|
|
|
BlockHeader *blockHeader = NULL;
|
|
|
|
|
uint8_t *block = NULL;
|
|
|
|
|
size_t readBytes = 0;
|
2022-10-12 13:10:44 +01:00
|
|
|
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
2025-08-01 15:34:36 +01:00
|
|
|
size_t lzmaSize = 0;
|
|
|
|
|
uint8_t *cmpData = NULL;
|
|
|
|
|
int errorNo = 0;
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(context == NULL) 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
|
2020-03-01 19:58:09 +00:00
|
|
|
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(sectorAddress > ctx->imageInfo.Sectors - 1) return AARUF_ERROR_SECTOR_OUT_OF_BOUNDS;
|
2019-03-21 01:06:21 +00:00
|
|
|
|
|
|
|
|
ddtEntry = ctx->userDataDdt[sectorAddress];
|
|
|
|
|
offsetMask = (uint32_t)((1 << ctx->shift) - 1);
|
|
|
|
|
offset = ddtEntry & offsetMask;
|
|
|
|
|
blockOffset = ddtEntry >> ctx->shift;
|
|
|
|
|
|
|
|
|
|
// Partially written image... as we can't know the real sector size just assume it's common :/
|
|
|
|
|
if(ddtEntry == 0)
|
|
|
|
|
{
|
2019-03-22 21:22:36 +00:00
|
|
|
memset(data, 0, ctx->imageInfo.SectorSize);
|
2019-03-21 01:06:21 +00:00
|
|
|
*length = ctx->imageInfo.SectorSize;
|
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
|
|
|
|
|
blockHeader = find_in_cache_uint64(&ctx->blockHeaderCache, blockOffset);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
|
|
|
|
// Read block header
|
2022-10-02 16:05:25 +01:00
|
|
|
if(blockHeader == NULL)
|
|
|
|
|
{
|
|
|
|
|
blockHeader = malloc(sizeof(BlockHeader));
|
|
|
|
|
if(blockHeader == NULL) return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
|
|
|
|
|
fseek(ctx->imageStream, blockOffset, SEEK_SET);
|
2022-10-02 17:55:45 +01:00
|
|
|
readBytes = fread(blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
if(readBytes != sizeof(BlockHeader)) return AARUF_ERROR_CANNOT_READ_HEADER;
|
2019-03-22 21:22:36 +00:00
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
add_to_cache_uint64(&ctx->blockHeaderCache, blockOffset, blockHeader);
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-04-30 15:51:32 +01:00
|
|
|
fseek(ctx->imageStream, blockOffset + sizeof(BlockHeader), SEEK_SET); // Advance as if reading the header
|
2022-10-02 16:05:25 +01:00
|
|
|
|
|
|
|
|
if(data == NULL || *length < blockHeader->sectorSize)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2022-10-02 16:05:25 +01:00
|
|
|
*length = blockHeader->sectorSize;
|
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
|
|
|
|
|
block = find_in_cache_uint64(&ctx->blockCache, blockOffset);
|
|
|
|
|
|
|
|
|
|
if(block != NULL)
|
|
|
|
|
{
|
|
|
|
|
memcpy(data, block + offset, blockHeader->sectorSize);
|
|
|
|
|
*length = blockHeader->sectorSize;
|
|
|
|
|
return AARUF_STATUS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 01:06:21 +00:00
|
|
|
// Decompress block
|
2022-10-02 16:05:25 +01:00
|
|
|
switch(blockHeader->compression)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
2019-03-31 20:52:06 +01:00
|
|
|
case None:
|
2024-04-30 15:51:32 +01:00
|
|
|
block = (uint8_t *)malloc(blockHeader->length);
|
2020-03-01 19:53:05 +00:00
|
|
|
if(block == NULL) return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2022-10-02 17:55:45 +01:00
|
|
|
readBytes = fread(block, 1, blockHeader->length, ctx->imageStream);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2022-10-02 16:05:25 +01:00
|
|
|
if(readBytes != blockHeader->length)
|
2019-03-21 01:06:21 +00:00
|
|
|
{
|
|
|
|
|
free(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:
|
|
|
|
|
lzmaSize = blockHeader->cmpLength - LZMA_PROPERTIES_LENGTH;
|
|
|
|
|
cmpData = malloc(lzmaSize);
|
|
|
|
|
|
|
|
|
|
if(cmpData == NULL)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Cannot allocate memory for block...\n");
|
2022-10-12 13:10:44 +01:00
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block = malloc(blockHeader->length);
|
|
|
|
|
if(block == NULL)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Cannot allocate memory for block...\n");
|
2022-10-12 13:10:44 +01:00
|
|
|
free(cmpData);
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
|
|
|
|
|
|
|
|
|
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Could not read LZMA properties...\n");
|
2022-10-12 13:10:44 +01:00
|
|
|
free(block);
|
|
|
|
|
free(cmpData);
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
|
|
|
|
if(readBytes != lzmaSize)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Could not read compressed block...\n");
|
2022-10-12 13:10:44 +01:00
|
|
|
free(cmpData);
|
|
|
|
|
free(block);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBytes = blockHeader->length;
|
|
|
|
|
errorNo =
|
|
|
|
|
aaruf_lzma_decode_buffer(block, &readBytes, cmpData, &lzmaSize, lzmaProperties, LZMA_PROPERTIES_LENGTH);
|
|
|
|
|
|
|
|
|
|
if(errorNo != 0)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Got error %d from LZMA...\n", errorNo);
|
2022-10-12 13:10:44 +01:00
|
|
|
free(cmpData);
|
|
|
|
|
free(block);
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(readBytes != blockHeader->length)
|
|
|
|
|
{
|
2022-10-12 13:16:44 +01:00
|
|
|
fprintf(stderr, "Error decompressing block, should be {0} bytes but got {1} bytes...\n");
|
|
|
|
|
free(cmpData);
|
|
|
|
|
free(block);
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(cmpData);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Flac:
|
|
|
|
|
cmpData = malloc(blockHeader->cmpLength);
|
|
|
|
|
|
|
|
|
|
if(cmpData == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Cannot allocate memory for block...\n");
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block = malloc(blockHeader->length);
|
|
|
|
|
if(block == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Cannot allocate memory for block...\n");
|
|
|
|
|
free(cmpData);
|
|
|
|
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBytes = fread(cmpData, 1, blockHeader->cmpLength, ctx->imageStream);
|
|
|
|
|
if(readBytes != blockHeader->cmpLength)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Could not read compressed block...\n");
|
|
|
|
|
free(cmpData);
|
|
|
|
|
free(block);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBytes = aaruf_flac_decode_redbook_buffer(block, blockHeader->length, cmpData, blockHeader->cmpLength);
|
|
|
|
|
|
|
|
|
|
if(readBytes != blockHeader->length)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error decompressing block, should be {0} bytes but got {1} bytes...\n");
|
2022-10-12 13:10:44 +01:00
|
|
|
free(cmpData);
|
|
|
|
|
free(block);
|
|
|
|
|
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(cmpData);
|
|
|
|
|
|
2019-03-21 01:06:21 +00:00
|
|
|
break;
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
|
|
|
|
return AARUF_ERROR_UNSUPPORTED_COMPRESSION;
|
2019-03-21 01:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add block to cache
|
2022-10-02 16:05:25 +01:00
|
|
|
add_to_cache_uint64(&ctx->blockCache, blockOffset, block);
|
2019-03-21 01:06:21 +00:00
|
|
|
|
2022-10-12 13:10:26 +01:00
|
|
|
memcpy(data, block + (offset * blockHeader->sectorSize), blockHeader->sectorSize);
|
2022-10-02 16:05:25 +01:00
|
|
|
*length = blockHeader->sectorSize;
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_STATUS_OK;
|
2019-03-23 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(context == NULL) 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
|
2020-03-01 19:58:09 +00:00
|
|
|
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
2019-03-23 23:31:04 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(ctx->imageInfo.XmlMediaType != OpticalDisc) return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-23 23:31:04 +00:00
|
|
|
|
|
|
|
|
for(i = 0; i < ctx->numberOfDataTracks; i++)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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-01 15:34:36 +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-01 15:34:36 +01:00
|
|
|
int i = 0;
|
|
|
|
|
bool trkFound = false;
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(context == NULL) 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
|
2020-03-01 19:58:09 +00:00
|
|
|
if(ctx->magic != AARU_MAGIC) 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;
|
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
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
bareData = (uint8_t *)malloc(bareLength);
|
2019-03-24 23:17:56 +00:00
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(bareData == NULL) 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);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(!trkFound) 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
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
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
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
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
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_REACHED_UNREACHABLE_CODE;
|
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:
|
|
|
|
|
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:
|
|
|
|
|
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;
|
2020-03-01 19:53:05 +00:00
|
|
|
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bareData = malloc(bareLength);
|
|
|
|
|
|
2020-03-01 19:53:05 +00:00
|
|
|
if(bareData == NULL) 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
|
|
|
|
2019-03-31 20:52:06 +01:00
|
|
|
if(bareLength != 512) return res;
|
2019-03-24 23:17:56 +00:00
|
|
|
|
|
|
|
|
memcpy(data, ctx->sectorSubchannel + sectorAddress * tagLength, tagLength);
|
|
|
|
|
memcpy(data, bareData, 512);
|
|
|
|
|
|
|
|
|
|
free(bareData);
|
|
|
|
|
|
|
|
|
|
return res;
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
|
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
2024-04-30 15:51:32 +01:00
|
|
|
default:
|
|
|
|
|
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
|
2019-03-24 23:17:56 +00:00
|
|
|
}
|
|
|
|
|
}
|