Files
libaaruformat/src/read.c

388 lines
14 KiB
C
Raw Normal View History

2019-03-20 00:45:47 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : open.c
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : libdicformat.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles opening DiscImageChef format disk images.
//
// --[ License ] --------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
#include <dicformat.h>
2019-03-21 01:06:21 +00:00
#include <malloc.h>
#include <string.h>
2019-03-20 00:45:47 +00:00
2019-08-03 01:58:19 +01:00
int32_t read_media_tag(void* context, uint8_t* data, int32_t tag, uint32_t* length)
2019-03-20 00:45:47 +00:00
{
2019-08-03 01:58:19 +01:00
dicformatContext* ctx;
dataLinkedList* item;
2019-03-31 20:52:06 +01:00
if(context == NULL) return DICF_ERROR_NOT_DICFORMAT;
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
// Not a libdicformat context
2019-03-31 20:52:06 +01:00
if(ctx->magic != DIC_MAGIC) return DICF_ERROR_NOT_DICFORMAT;
2019-03-20 00:45:47 +00:00
2019-03-21 01:06:21 +00:00
item = ctx->mediaTagsHead;
2019-03-20 00:45:47 +00:00
while(item != NULL)
{
if(item->type == tag)
{
2019-03-22 21:22:36 +00:00
if(data == NULL || *length < item->length)
{
*length = item->length;
return DICF_ERROR_BUFFER_TOO_SMALL;
}
*length = item->length;
2019-03-22 21:22:36 +00:00
memcpy(data, item->data, item->length);
}
2019-03-20 00:45:47 +00:00
item = item->next;
}
2019-03-22 21:22:36 +00:00
*length = 0;
return DICF_ERROR_MEDIA_TAG_NOT_PRESENT;
2019-03-21 01:06:21 +00:00
}
2019-08-03 01:58:19 +01:00
int32_t read_sector(void* context, uint64_t sectorAddress, uint8_t* data, uint32_t* length)
2019-03-21 01:06:21 +00:00
{
2019-08-03 01:58:19 +01:00
dicformatContext* ctx;
2019-03-31 20:52:06 +01:00
uint64_t ddtEntry;
uint32_t offsetMask;
uint64_t offset;
uint64_t blockOffset;
BlockHeader blockHeader;
2019-08-03 01:58:19 +01:00
uint8_t* block;
2019-03-31 20:52:06 +01:00
size_t readBytes;
2019-03-21 01:06:21 +00:00
2019-03-31 20:52:06 +01:00
if(context == NULL) return DICF_ERROR_NOT_DICFORMAT;
2019-03-21 01:06:21 +00:00
ctx = context;
// Not a libdicformat context
2019-03-31 20:52:06 +01:00
if(ctx->magic != DIC_MAGIC) return DICF_ERROR_NOT_DICFORMAT;
2019-03-21 01:06:21 +00:00
2019-03-31 20:52:06 +01:00
if(sectorAddress > ctx->imageInfo.Sectors - 1) return DICF_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;
2019-03-24 23:17:56 +00:00
return DICF_STATUS_SECTOR_NOT_DUMPED;
2019-03-21 01:06:21 +00:00
}
// Check if block is cached
// TODO: Caches
// Read block header
fseek(ctx->imageStream, blockOffset, SEEK_SET);
readBytes = fread(&blockHeader, sizeof(BlockHeader), 1, ctx->imageStream);
2019-03-31 20:52:06 +01:00
if(readBytes != sizeof(BlockHeader)) return DICF_ERROR_CANNOT_READ_HEADER;
2019-03-22 21:22:36 +00:00
if(data == NULL || *length < blockHeader.sectorSize)
2019-03-21 01:06:21 +00:00
{
2019-03-22 21:22:36 +00:00
*length = blockHeader.sectorSize;
return DICF_ERROR_BUFFER_TOO_SMALL;
2019-03-21 01:06:21 +00:00
}
// Decompress block
switch(blockHeader.compression)
{
2019-03-31 20:52:06 +01:00
case None:
2019-08-03 01:58:19 +01:00
block = (uint8_t*)malloc(blockHeader.length);
2019-03-31 20:52:06 +01:00
if(block == NULL) return DICF_ERROR_NOT_ENOUGH_MEMORY;
2019-03-21 01:06:21 +00:00
readBytes = fread(block, blockHeader.length, 1, ctx->imageStream);
if(readBytes != blockHeader.length)
{
free(block);
2019-03-22 21:22:36 +00:00
return DICF_ERROR_CANNOT_READ_BLOCK;
2019-03-21 01:06:21 +00:00
}
break;
2019-03-31 20:52:06 +01:00
default: return DICF_ERROR_UNSUPPORTED_COMPRESSION;
2019-03-21 01:06:21 +00:00
}
// Check if cache needs to be emptied
// TODO: Caches
// Add block to cache
// TODO: Caches
2019-03-22 21:22:36 +00:00
memcpy(data, block + offset, blockHeader.sectorSize);
2019-03-21 01:06:21 +00:00
*length = blockHeader.sectorSize;
free(block);
2019-03-22 21:22:36 +00:00
return DICF_STATUS_OK;
2019-03-23 23:31:04 +00:00
}
2019-08-03 01:58:19 +01:00
int32_t 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
{
2019-08-03 01:58:19 +01:00
dicformatContext* ctx;
2019-03-31 20:52:06 +01:00
int i;
2019-03-23 23:31:04 +00:00
2019-03-31 20:52:06 +01:00
if(context == NULL) return DICF_ERROR_NOT_DICFORMAT;
2019-03-23 23:31:04 +00:00
ctx = context;
// Not a libdicformat context
2019-03-31 20:52:06 +01:00
if(ctx->magic != DIC_MAGIC) return DICF_ERROR_NOT_DICFORMAT;
2019-03-23 23:31:04 +00:00
2019-03-31 20:52:06 +01:00
if(ctx->imageInfo.XmlMediaType != OpticalDisc) return DICF_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)
2019-03-31 20:52:06 +01:00
{ return read_sector(context, ctx->dataTracks[i].start + sectorAddress, data, length); }
2019-03-23 23:31:04 +00:00
}
return DICF_ERROR_TRACK_NOT_FOUND;
}
2019-03-24 23:17:56 +00:00
2019-08-03 01:58:19 +01:00
int32_t read_sector_long(void* context, uint8_t* data, uint64_t sectorAddress, uint32_t* length)
2019-03-24 23:17:56 +00:00
{
2019-08-03 01:58:19 +01:00
dicformatContext* ctx;
2019-03-31 20:52:06 +01:00
uint32_t bareLength;
uint32_t tagLength;
2019-08-03 01:58:19 +01:00
uint8_t* bareData;
2019-03-31 20:52:06 +01:00
int32_t res;
TrackEntry trk;
int i;
bool trkFound;
2019-03-24 23:17:56 +00:00
2019-03-31 20:52:06 +01:00
if(context == NULL) return DICF_ERROR_NOT_DICFORMAT;
2019-03-24 23:17:56 +00:00
ctx = context;
// Not a libdicformat context
2019-03-31 20:52:06 +01:00
if(ctx->magic != DIC_MAGIC) return DICF_ERROR_NOT_DICFORMAT;
2019-03-24 23:17:56 +00:00
switch(ctx->imageInfo.XmlMediaType)
{
case OpticalDisc:
if(*length < 2352 || data == NULL)
{
*length = 2352;
return DICF_ERROR_BUFFER_TOO_SMALL;
}
if((ctx->sectorSuffix == NULL || ctx->sectorPrefix == NULL) &&
(ctx->sectorSuffixCorrected == NULL || ctx->sectorPrefixCorrected == NULL))
return read_sector(context, sectorAddress, data, length);
bareLength = 0;
read_sector(context, sectorAddress, NULL, &bareLength);
2019-08-03 01:58:19 +01:00
bareData = (uint8_t*)malloc(bareLength);
2019-03-24 23:17:56 +00:00
2019-03-31 20:52:06 +01:00
if(bareData == NULL) return DICF_ERROR_NOT_ENOUGH_MEMORY;
2019-03-24 23:17:56 +00:00
res = read_sector(context, sectorAddress, bareData, &bareLength);
2019-03-31 20:52:06 +01:00
if(res < DICF_STATUS_OK) return res;
2019-03-24 23:17:56 +00:00
trkFound = false;
for(i = 0; i < ctx->numberOfDataTracks; i++)
{
if(ctx->dataTracks[i].start >= sectorAddress && ctx->dataTracks[i].end <= sectorAddress)
{
trkFound = true;
trk = ctx->dataTracks[i];
break;
}
}
2019-03-31 20:52:06 +01:00
if(!trkFound) return DICF_ERROR_TRACK_NOT_FOUND;
2019-03-24 23:17:56 +00:00
switch(trk.type)
{
case Audio:
2019-03-31 20:52:06 +01:00
case Data: memcpy(bareData, data, bareLength); return res;
case CdMode1:
memcpy(bareData, data + 16, 2048);
2019-03-24 23:17:56 +00:00
if(ctx->sectorPrefix != NULL)
memcpy(data, ctx->sectorPrefix + (sectorAddress * 16), 16);
else if(ctx->sectorPrefixDdt != NULL)
{
if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
{
ecc_cd_reconstruct_prefix(data, trk.type, sectorAddress);
res = DICF_STATUS_OK;
}
else if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
{
res = DICF_STATUS_SECTOR_NOT_DUMPED;
}
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
return DICF_ERROR_REACHED_UNREACHABLE_CODE;
if(ctx->sectorSuffix != NULL)
memcpy(data + 2064, ctx->sectorSuffix + sectorAddress * 288, 288);
else if(ctx->sectorSuffixDdt != NULL)
{
if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
{
ecc_cd_reconstruct(ctx->eccCdContext, data, trk.type);
res = DICF_STATUS_OK;
}
else if((ctx->sectorSuffixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
{
res = DICF_STATUS_SECTOR_NOT_DUMPED;
}
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
return DICF_ERROR_REACHED_UNREACHABLE_CODE;
return res;
case CdMode2Formless:
case CdMode2Form1:
case CdMode2Form2:
if(ctx->sectorPrefix != NULL)
memcpy(data, ctx->sectorPrefix + sectorAddress * 16, 16);
else if(ctx->sectorPrefixDdt != NULL)
{
if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == Correct)
{
ecc_cd_reconstruct_prefix(data, trk.type, sectorAddress);
res = DICF_STATUS_OK;
}
else if((ctx->sectorPrefixDdt[sectorAddress] & CD_XFIX_MASK) == NotDumped)
{
res = DICF_STATUS_SECTOR_NOT_DUMPED;
}
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
return DICF_ERROR_REACHED_UNREACHABLE_CODE;
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);
ecc_cd_reconstruct(ctx->eccCdContext, data, CdMode2Form1);
}
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)
ecc_cd_reconstruct(ctx->eccCdContext, data, CdMode2Form2);
}
}
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;
2019-03-31 20:52:06 +01:00
default: return DICF_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:
2019-03-31 20:52:06 +01:00
if(ctx->sectorSubchannel == NULL) return read_sector(context, sectorAddress, data, length);
2019-03-24 23:17:56 +00:00
switch(ctx->imageInfo.MediaType)
{
case AppleFileWare:
case AppleProfile:
2019-03-31 20:52:06 +01:00
case AppleWidget: tagLength = 20; break;
2019-03-24 23:17:56 +00:00
case AppleSonySS:
2019-03-31 20:52:06 +01:00
case AppleSonyDS: tagLength = 12; break;
case PriamDataTower: tagLength = 24; break;
default: return DICF_ERROR_INCORRECT_MEDIA_TYPE;
2019-03-24 23:17:56 +00:00
}
bareLength = 512;
if(*length < tagLength + bareLength || data == NULL)
{
*length = tagLength + bareLength;
return DICF_ERROR_BUFFER_TOO_SMALL;
}
bareData = malloc(bareLength);
2019-03-31 20:52:06 +01:00
if(bareData == NULL) return DICF_ERROR_NOT_ENOUGH_MEMORY;
2019-03-24 23:17:56 +00:00
res = read_sector(context, sectorAddress, bareData, &bareLength);
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;
2019-03-31 20:52:06 +01:00
default: return DICF_ERROR_INCORRECT_MEDIA_TYPE;
2019-03-24 23:17:56 +00:00
}
2019-03-31 20:52:06 +01:00
default: return DICF_ERROR_INCORRECT_MEDIA_TYPE;
2019-03-24 23:17:56 +00:00
}
}