libdmg-hfsplus/dmg/dmgfile.c

258 строки
6.4 KiB
C
Исходник Постоянная ссылка Обычный вид История

2008-05-21 17:59:40 +04:00
/*
* dmgfile.c
* libdmg-hfsplus
*/
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
2008-05-21 18:48:02 +04:00
#include <dmg/dmg.h>
2015-04-11 08:02:41 +03:00
#include <dmg/adc.h>
2008-05-21 18:48:02 +04:00
#include <dmg/dmgfile.h>
2008-05-21 17:59:40 +04:00
static void cacheRun(DMG* dmg, BLKXTable* blkx, int run) {
size_t bufferSize;
z_stream strm;
void* inBuffer;
int ret;
size_t have;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
if(dmg->runData) {
free(dmg->runData);
}
2015-04-11 08:02:41 +03:00
2008-11-09 17:39:02 +03:00
bufferSize = SECTOR_SIZE * blkx->runs[run].sectorCount;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg->runData = (void*) malloc(bufferSize);
inBuffer = (void*) malloc(bufferSize);
memset(dmg->runData, 0, bufferSize);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
ASSERT(dmg->dmg->seek(dmg->dmg, blkx->dataStart + blkx->runs[run].compOffset) == 0, "fseeko");
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
switch(blkx->runs[run].type) {
case BLOCK_ADC:
2015-04-11 08:02:41 +03:00
{
size_t bufferRead = 0;
do {
strm.avail_in = dmg->dmg->read(dmg->dmg, inBuffer, blkx->runs[run].compLength);
strm.avail_out = adc_decompress(strm.avail_in, inBuffer, bufferSize, dmg->runData, &have);
bufferRead+=strm.avail_out;
} while (bufferRead < blkx->runs[run].compLength);
break;
2015-04-11 08:02:41 +03:00
}
2008-05-21 17:59:40 +04:00
case BLOCK_ZLIB:
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
ASSERT(inflateInit(&strm) == Z_OK, "inflateInit");
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
ASSERT((strm.avail_in = dmg->dmg->read(dmg->dmg, inBuffer, blkx->runs[run].compLength)) == blkx->runs[run].compLength, "fread");
strm.next_in = (unsigned char*) inBuffer;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
do {
strm.avail_out = bufferSize;
strm.next_out = (unsigned char*) dmg->runData;
ASSERT((ret = inflate(&strm, Z_NO_FLUSH)) != Z_STREAM_ERROR, "inflate/Z_STREAM_ERROR");
if(ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_STREAM_END) {
ASSERT(FALSE, "inflate");
}
have = bufferSize - strm.avail_out;
} while (strm.avail_out == 0);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
ASSERT(inflateEnd(&strm) == Z_OK, "inflateEnd");
break;
case BLOCK_RAW:
ASSERT((have = dmg->dmg->read(dmg->dmg, dmg->runData, blkx->runs[run].compLength)) == blkx->runs[run].compLength, "fread");
break;
case BLOCK_IGNORE:
break;
case BLOCK_COMMENT:
break;
case BLOCK_TERMINATOR:
break;
default:
break;
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg->runStart = (blkx->runs[run].sectorStart + blkx->firstSectorNumber) * SECTOR_SIZE;
dmg->runEnd = dmg->runStart + (blkx->runs[run].sectorCount * SECTOR_SIZE);
}
static void cacheOffset(DMG* dmg, off_t location) {
int i;
int j;
uint64_t sector;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
sector = (uint64_t)(location / SECTOR_SIZE);
for(i = 0; i < dmg->numBLKX; i++) {
if(sector >= dmg->blkx[i]->firstSectorNumber && sector < (dmg->blkx[i]->firstSectorNumber + dmg->blkx[i]->sectorCount)) {
for(j = 0; j < dmg->blkx[i]->blocksRunCount; j++) {
if(sector >= (dmg->blkx[i]->firstSectorNumber + dmg->blkx[i]->runs[j].sectorStart) &&
sector < (dmg->blkx[i]->firstSectorNumber + dmg->blkx[i]->runs[j].sectorStart + dmg->blkx[i]->runs[j].sectorCount)) {
cacheRun(dmg, dmg->blkx[i], j);
}
}
}
}
}
static int dmgFileRead(io_func* io, off_t location, size_t size, void *buffer) {
DMG* dmg;
size_t toRead;
dmg = (DMG*) io->data;
location += dmg->offset;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
if(size == 0) {
return TRUE;
}
if(location < dmg->runStart || location >= dmg->runEnd) {
cacheOffset(dmg, location);
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
if((location + size) > dmg->runEnd) {
toRead = dmg->runEnd - location;
} else {
toRead = size;
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
memcpy(buffer, (void*)((uint8_t*)dmg->runData + (uint32_t)(location - dmg->runStart)), toRead);
size -= toRead;
location += toRead;
buffer = (void*)((uint8_t*)buffer + toRead);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
if(size > 0) {
2008-11-09 17:39:02 +03:00
return dmgFileRead(io, location - dmg->offset, size, buffer);
2008-05-21 17:59:40 +04:00
} else {
return TRUE;
}
}
static int dmgFileWrite(io_func* io, off_t location, size_t size, void *buffer) {
fprintf(stderr, "Error: writing to DMGs is not supported (impossible to achieve with compressed images and retain asr multicast ordering).\n");
return FALSE;
}
static void closeDmgFile(io_func* io) {
DMG* dmg;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg = (DMG*) io->data;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
if(dmg->runData) {
free(dmg->runData);
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
free(dmg->blkx);
releaseResources(dmg->resources);
dmg->dmg->close(dmg->dmg);
free(dmg);
free(io);
}
io_func* openDmgFile(AbstractFile* abstractIn) {
off_t fileLength;
UDIFResourceFile resourceFile;
2015-04-11 08:02:41 +03:00
DMG* dmg;
2008-05-21 17:59:40 +04:00
ResourceData* blkx;
ResourceData* curData;
int i;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
io_func* toReturn;
if(abstractIn == NULL) {
return NULL;
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
fileLength = abstractIn->getLength(abstractIn);
abstractIn->seek(abstractIn, fileLength - sizeof(UDIFResourceFile));
readUDIFResourceFile(abstractIn, &resourceFile);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg = (DMG*) malloc(sizeof(DMG));
dmg->dmg = abstractIn;
dmg->resources = readResources(abstractIn, &resourceFile);
dmg->numBLKX = 0;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
blkx = (getResourceByKey(dmg->resources, "blkx"))->data;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
curData = blkx;
while(curData != NULL) {
dmg->numBLKX++;
2015-04-11 08:02:41 +03:00
curData = curData->next;
2008-05-21 17:59:40 +04:00
}
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg->blkx = (BLKXTable**) malloc(sizeof(BLKXTable*) * dmg->numBLKX);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
i = 0;
while(blkx != NULL) {
dmg->blkx[i] = (BLKXTable*)(blkx->data);
i++;
blkx = blkx->next;
}
dmg->offset = 0;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
dmg->runData = NULL;
cacheOffset(dmg, 0);
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
toReturn = (io_func*) malloc(sizeof(io_func));
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
toReturn->data = dmg;
toReturn->read = &dmgFileRead;
toReturn->write = &dmgFileWrite;
toReturn->close = &closeDmgFile;
2015-04-11 08:02:41 +03:00
2008-05-21 17:59:40 +04:00
return toReturn;
}
io_func* openDmgFilePartition(AbstractFile* abstractIn, int partition) {
io_func* toReturn;
Partition* partitions;
2008-11-09 17:39:02 +03:00
uint8_t ddmBuffer[SECTOR_SIZE];
DriverDescriptorRecord* ddm;
2008-05-21 17:59:40 +04:00
int numPartitions;
int i;
2008-11-09 17:39:02 +03:00
unsigned int BlockSize;
2008-05-21 17:59:40 +04:00
toReturn = openDmgFile(abstractIn);
if(toReturn == NULL) {
return NULL;
}
2008-11-09 17:39:02 +03:00
toReturn->read(toReturn, 0, SECTOR_SIZE, ddmBuffer);
ddm = (DriverDescriptorRecord*) ddmBuffer;
flipDriverDescriptorRecord(ddm, FALSE);
BlockSize = ddm->sbBlkSize;
partitions = (Partition*) malloc(BlockSize);
toReturn->read(toReturn, BlockSize, BlockSize, partitions);
flipPartitionMultiple(partitions, FALSE, FALSE, BlockSize);
2008-05-21 17:59:40 +04:00
numPartitions = partitions->pmMapBlkCnt;
2008-11-09 17:39:02 +03:00
partitions = (Partition*) realloc(partitions, numPartitions * BlockSize);
2015-04-11 08:02:41 +03:00
toReturn->read(toReturn, BlockSize, numPartitions * BlockSize, partitions);
2008-11-09 17:39:02 +03:00
flipPartition(partitions, FALSE, BlockSize);
2008-05-21 17:59:40 +04:00
if(partition >= 0) {
2008-11-09 17:39:02 +03:00
((DMG*)toReturn->data)->offset = partitions[partition].pmPyPartStart * BlockSize;
2008-05-21 17:59:40 +04:00
} else {
for(i = 0; i < numPartitions; i++) {
2008-11-09 17:39:02 +03:00
if(strcmp((char*)partitions->pmParType, "Apple_HFSX") == 0 || strcmp((char*)partitions->pmParType, "Apple_HFS") == 0) {
((DMG*)toReturn->data)->offset = partitions->pmPyPartStart * BlockSize;
2008-05-21 17:59:40 +04:00
break;
}
2008-11-09 17:39:02 +03:00
partitions = (Partition*)((uint8_t*)partitions + BlockSize);
2008-05-21 17:59:40 +04:00
}
}
return toReturn;
}