mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Use utarray to process index entries.
This commit is contained in:
27
include/internal.h
Normal file
27
include/internal.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Aaru Data Preservation Suite.
|
||||||
|
* Copyright (c) 2019-2025 Natalia Portillo.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBAARUFORMAT_INTERNAL_H
|
||||||
|
#define LIBAARUFORMAT_INTERNAL_H
|
||||||
|
|
||||||
|
#include "utarray.h"
|
||||||
|
|
||||||
|
UT_array *process_index_v1(aaruformatContext *ctx);
|
||||||
|
int32_t verify_index_v1(aaruformatContext *ctx);
|
||||||
|
|
||||||
|
#endif // LIBAARUFORMAT_INTERNAL_H
|
||||||
120
src/index_v1.c
Normal file
120
src/index_v1.c
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Aaru Data Preservation Suite.
|
||||||
|
* Copyright (c) 2019-2025 Natalia Portillo.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "aaruformat.h"
|
||||||
|
#include "utarray.h"
|
||||||
|
|
||||||
|
UT_array *process_index_v1(aaruformatContext *ctx)
|
||||||
|
{
|
||||||
|
UT_array *index_entries = NULL;
|
||||||
|
IndexEntry entry;
|
||||||
|
|
||||||
|
if(ctx == NULL || ctx->imageStream == NULL) return NULL;
|
||||||
|
|
||||||
|
// Initialize the index entries array
|
||||||
|
UT_icd index_entry_icd = {sizeof(IndexEntry), NULL, NULL, NULL};
|
||||||
|
|
||||||
|
utarray_new(index_entries, &index_entry_icd);
|
||||||
|
|
||||||
|
// Read the index header
|
||||||
|
fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
||||||
|
IndexHeader idx_header;
|
||||||
|
fread(&idx_header, sizeof(IndexHeader), 1, ctx->imageStream);
|
||||||
|
|
||||||
|
// Check if the index header is valid
|
||||||
|
if(idx_header.identifier != IndexBlock)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Incorrect index identifier.\n");
|
||||||
|
utarray_free(index_entries);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < idx_header.entries; i++)
|
||||||
|
{
|
||||||
|
fread(&entry, sizeof(IndexEntry), 1, ctx->imageStream);
|
||||||
|
utarray_push_back(index_entries, &entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return index_entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t verify_index_v1(aaruformatContext *ctx)
|
||||||
|
{
|
||||||
|
size_t read_bytes = 0;
|
||||||
|
IndexHeader index_header;
|
||||||
|
uint64_t crc64 = 0;
|
||||||
|
IndexEntry *index_entries = NULL;
|
||||||
|
|
||||||
|
if(ctx == NULL || ctx->imageStream == NULL) return AARUF_ERROR_NOT_AARUFORMAT;
|
||||||
|
|
||||||
|
// This will traverse all blocks and check their CRC64 without uncompressing them
|
||||||
|
fprintf(stderr, "Checking index integrity at %llu.\n", ctx->header.indexOffset);
|
||||||
|
fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
||||||
|
|
||||||
|
// Read the index header
|
||||||
|
read_bytes = fread(&index_header, 1, sizeof(IndexHeader), ctx->imageStream);
|
||||||
|
|
||||||
|
if(read_bytes != sizeof(IndexHeader))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not read index header.\n");
|
||||||
|
return AARUF_ERROR_CANNOT_READ_HEADER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(index_header.identifier != IndexBlock)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Incorrect index identifier.\n");
|
||||||
|
return AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "Index at %llu contains %d entries.\n", ctx->header.indexOffset, index_header.entries);
|
||||||
|
|
||||||
|
index_entries = malloc(sizeof(IndexEntry) * index_header.entries);
|
||||||
|
|
||||||
|
if(index_entries == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Cannot allocate memory for index entries.\n");
|
||||||
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
|
read_bytes = fread(index_entries, 1, sizeof(IndexEntry) * index_header.entries, ctx->imageStream);
|
||||||
|
|
||||||
|
if(read_bytes != sizeof(IndexEntry) * index_header.entries)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not read index entries.\n");
|
||||||
|
free(index_entries);
|
||||||
|
return AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
}
|
||||||
|
|
||||||
|
crc64 = aaruf_crc64_data((const uint8_t *)index_entries, sizeof(IndexEntry) * index_header.entries);
|
||||||
|
|
||||||
|
// Due to how C# wrote it, it is effectively reversed
|
||||||
|
if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64);
|
||||||
|
|
||||||
|
if(crc64 != index_header.crc64)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Expected index CRC 0x%16llX but got 0x%16llX.\n", index_header.crc64, crc64);
|
||||||
|
free(index_entries);
|
||||||
|
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
||||||
|
}
|
||||||
|
|
||||||
|
return AARUF_STATUS_OK;
|
||||||
|
}
|
||||||
157
src/open.c
157
src/open.c
@@ -28,27 +28,30 @@
|
|||||||
|
|
||||||
#include <aaruformat.h>
|
#include <aaruformat.h>
|
||||||
|
|
||||||
|
#include "internal.h"
|
||||||
|
#include "utarray.h"
|
||||||
|
|
||||||
void *aaruf_open(const char *filepath)
|
void *aaruf_open(const char *filepath)
|
||||||
{
|
{
|
||||||
aaruformatContext *ctx = NULL;
|
aaruformatContext *ctx = NULL;
|
||||||
int errorNo = 0;
|
int errorNo = 0;
|
||||||
size_t readBytes = 0;
|
size_t readBytes = 0;
|
||||||
long pos = 0;
|
long pos = 0;
|
||||||
IndexHeader idxHeader;
|
uint8_t *data = NULL;
|
||||||
IndexEntry *idxEntries = NULL;
|
uint8_t *cmpData = NULL;
|
||||||
uint8_t *data = NULL;
|
uint8_t *cstData = NULL;
|
||||||
uint8_t *cmpData = NULL;
|
uint32_t *cdDdt = NULL;
|
||||||
uint8_t *cstData = NULL;
|
uint64_t crc64 = 0;
|
||||||
uint32_t *cdDdt = NULL;
|
int i = 0, j = 0, k = 0;
|
||||||
uint64_t crc64 = 0;
|
uint16_t e = 0;
|
||||||
int i = 0, j = 0, k = 0;
|
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
||||||
uint16_t e = 0;
|
size_t lzmaSize = 0;
|
||||||
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
ChecksumHeader checksum_header;
|
||||||
size_t lzmaSize = 0;
|
|
||||||
ChecksumHeader checksum_header;
|
|
||||||
ChecksumEntry const *checksum_entry = NULL;
|
ChecksumEntry const *checksum_entry = NULL;
|
||||||
mediaTagEntry *mediaTag = NULL;
|
mediaTagEntry *mediaTag = NULL;
|
||||||
mediaTagEntry *oldMediaTag = NULL;
|
mediaTagEntry *oldMediaTag = NULL;
|
||||||
|
uint32_t signature = 0;
|
||||||
|
UT_array *index_entries = NULL;
|
||||||
|
|
||||||
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
|
ctx = (aaruformatContext *)malloc(sizeof(aaruformatContext));
|
||||||
memset(ctx, 0, sizeof(aaruformatContext));
|
memset(ctx, 0, sizeof(aaruformatContext));
|
||||||
@@ -147,9 +150,9 @@ void *aaruf_open(const char *filepath)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
readBytes = fread(&idxHeader, 1, sizeof(IndexHeader), ctx->imageStream);
|
readBytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
|
||||||
|
|
||||||
if(readBytes != sizeof(IndexHeader) || idxHeader.identifier != IndexBlock)
|
if(readBytes != sizeof(uint32_t) || signature != IndexBlock)
|
||||||
{
|
{
|
||||||
free(ctx);
|
free(ctx);
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
@@ -157,49 +160,40 @@ void *aaruf_open(const char *filepath)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
index_entries = process_index_v1(ctx);
|
||||||
|
|
||||||
|
if(index_entries == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not process index.\n");
|
||||||
|
utarray_free(index_entries);
|
||||||
|
free(ctx);
|
||||||
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stderr, "libaaruformat: Index at %" PRIu64 " contains %d entries\n", ctx->header.indexOffset,
|
fprintf(stderr, "libaaruformat: Index at %" PRIu64 " contains %d entries\n", ctx->header.indexOffset,
|
||||||
idxHeader.entries);
|
utarray_len(index_entries));
|
||||||
|
|
||||||
idxEntries = (IndexEntry *)malloc(sizeof(IndexEntry) * idxHeader.entries);
|
for(i = 0; i < utarray_len(index_entries); i++)
|
||||||
|
|
||||||
if(idxEntries == NULL)
|
|
||||||
{
|
|
||||||
errorNo = errno;
|
|
||||||
free(ctx);
|
|
||||||
errno = errorNo;
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(idxEntries, 0, sizeof(IndexEntry) * idxHeader.entries);
|
|
||||||
readBytes = fread(idxEntries, sizeof(IndexEntry), idxHeader.entries, ctx->imageStream);
|
|
||||||
|
|
||||||
if(readBytes != idxHeader.entries)
|
|
||||||
{
|
|
||||||
free(idxEntries);
|
|
||||||
free(ctx);
|
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i = 0; i < idxHeader.entries; i++)
|
|
||||||
{
|
{
|
||||||
|
IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i);
|
||||||
fprintf(stderr, "libaaruformat: Block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
|
||||||
(char *)&idxEntries[i].blockType, idxEntries[i].dataType, idxEntries[i].offset);
|
(char *)&entry->blockType, entry->dataType, entry->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool foundUserDataDdt = false;
|
bool foundUserDataDdt = false;
|
||||||
ctx->imageInfo.ImageSize = 0;
|
ctx->imageInfo.ImageSize = 0;
|
||||||
for(i = 0; i < idxHeader.entries; i++)
|
for(i = 0; i < utarray_len(index_entries); i++)
|
||||||
{
|
{
|
||||||
pos = fseek(ctx->imageStream, idxEntries[i].offset, SEEK_SET);
|
IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i);
|
||||||
|
pos = fseek(ctx->imageStream, entry->offset, SEEK_SET);
|
||||||
|
|
||||||
if(pos < 0 || ftell(ctx->imageStream) != idxEntries[i].offset)
|
if(pos < 0 || ftell(ctx->imageStream) != entry->offset)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...\n",
|
"libaaruformat: Could not seek to %" PRIu64 " as indicated by index entry %d, continuing...\n",
|
||||||
idxEntries[i].offset, i);
|
entry->offset, i);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -207,18 +201,17 @@ void *aaruf_open(const char *filepath)
|
|||||||
BlockHeader blockHeader;
|
BlockHeader blockHeader;
|
||||||
DdtHeader ddtHeader;
|
DdtHeader ddtHeader;
|
||||||
|
|
||||||
switch(idxEntries[i].blockType)
|
switch(entry->blockType)
|
||||||
{
|
{
|
||||||
case DataBlock:
|
case DataBlock:
|
||||||
// NOP block, skip
|
// NOP block, skip
|
||||||
if(idxEntries[i].dataType == NoData) break;
|
if(entry->dataType == NoData) break;
|
||||||
|
|
||||||
readBytes = fread(&blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
readBytes = fread(&blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||||
|
|
||||||
if(readBytes != sizeof(BlockHeader))
|
if(readBytes != sizeof(BlockHeader))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset);
|
||||||
idxEntries[i].offset);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -226,7 +219,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
ctx->imageInfo.ImageSize += blockHeader.cmpLength;
|
ctx->imageInfo.ImageSize += blockHeader.cmpLength;
|
||||||
|
|
||||||
// Unused, skip
|
// Unused, skip
|
||||||
if(idxEntries[i].dataType == UserData)
|
if(entry->dataType == UserData)
|
||||||
{
|
{
|
||||||
if(blockHeader.sectorSize > ctx->imageInfo.SectorSize)
|
if(blockHeader.sectorSize > ctx->imageInfo.SectorSize)
|
||||||
ctx->imageInfo.SectorSize = blockHeader.sectorSize;
|
ctx->imageInfo.SectorSize = blockHeader.sectorSize;
|
||||||
@@ -234,24 +227,24 @@ void *aaruf_open(const char *filepath)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(blockHeader.identifier != idxEntries[i].blockType)
|
if(blockHeader.identifier != entry->blockType)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(blockHeader.type != idxEntries[i].dataType)
|
if(blockHeader.type != entry->dataType)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"libaaruformat: Expected block with data type %4.4s at position %" PRIu64
|
"libaaruformat: Expected block with data type %4.4s at position %" PRIu64
|
||||||
" but found data type %4.4s\n",
|
" but found data type %4.4s\n",
|
||||||
(char *)&idxEntries[i].blockType, idxEntries[i].offset, (char *)&blockHeader.type);
|
(char *)&entry->blockType, entry->offset, (char *)&blockHeader.type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "libaaruformat: Found data block with type %4.4s at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Found data block with type %4.4s at position %" PRIu64 "\n",
|
||||||
(char *)&idxEntries[i].blockType, idxEntries[i].offset);
|
(char *)&entry->blockType, entry->offset);
|
||||||
|
|
||||||
if(blockHeader.compression == Lzma || blockHeader.compression == LzmaClauniaSubchannelTransform)
|
if(blockHeader.compression == Lzma || blockHeader.compression == LzmaClauniaSubchannelTransform)
|
||||||
{
|
{
|
||||||
@@ -387,11 +380,11 @@ void *aaruf_open(const char *filepath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's not a media tag, but a sector tag, and fill the appropriate table then
|
// Check if it's not a media tag, but a sector tag, and fill the appropriate table then
|
||||||
switch(idxEntries[i].dataType)
|
switch(entry->dataType)
|
||||||
{
|
{
|
||||||
case CdSectorPrefix:
|
case CdSectorPrefix:
|
||||||
case CdSectorPrefixCorrected:
|
case CdSectorPrefixCorrected:
|
||||||
if(idxEntries[i].dataType == CdSectorPrefixCorrected) { ctx->sectorPrefixCorrected = data; }
|
if(entry->dataType == CdSectorPrefixCorrected) { ctx->sectorPrefixCorrected = data; }
|
||||||
else
|
else
|
||||||
ctx->sectorPrefix = data;
|
ctx->sectorPrefix = data;
|
||||||
|
|
||||||
@@ -401,7 +394,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
break;
|
break;
|
||||||
case CdSectorSuffix:
|
case CdSectorSuffix:
|
||||||
case CdSectorSuffixCorrected:
|
case CdSectorSuffixCorrected:
|
||||||
if(idxEntries[i].dataType == CdSectorSuffixCorrected)
|
if(entry->dataType == CdSectorSuffixCorrected)
|
||||||
ctx->sectorSuffixCorrected = data;
|
ctx->sectorSuffixCorrected = data;
|
||||||
else
|
else
|
||||||
ctx->sectorSuffix = data;
|
ctx->sectorSuffix = data;
|
||||||
@@ -458,8 +451,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
|
|
||||||
if(readBytes != sizeof(DdtHeader))
|
if(readBytes != sizeof(DdtHeader))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Could not read block header at %" PRIu64 "\n", entry->offset);
|
||||||
idxEntries[i].offset);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -468,7 +460,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
|
|
||||||
ctx->imageInfo.ImageSize += ddtHeader.cmpLength;
|
ctx->imageInfo.ImageSize += ddtHeader.cmpLength;
|
||||||
|
|
||||||
if(idxEntries[i].dataType == UserData)
|
if(entry->dataType == UserData)
|
||||||
{
|
{
|
||||||
ctx->imageInfo.Sectors = ddtHeader.entries;
|
ctx->imageInfo.Sectors = ddtHeader.entries;
|
||||||
ctx->shift = ddtHeader.shift;
|
ctx->shift = ddtHeader.shift;
|
||||||
@@ -554,7 +546,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddtHeader.entries;
|
ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddtHeader.entries;
|
||||||
ctx->userDataDdt = mmap(NULL, ctx->mappedMemoryDdtSize, PROT_READ, MAP_SHARED,
|
ctx->userDataDdt = mmap(NULL, ctx->mappedMemoryDdtSize, PROT_READ, MAP_SHARED,
|
||||||
fileno(ctx->imageStream), idxEntries[i].offset + sizeof(ddtHeader));
|
fileno(ctx->imageStream), entry->offset + sizeof(ddtHeader));
|
||||||
|
|
||||||
if(ctx->userDataDdt == MAP_FAILED)
|
if(ctx->userDataDdt == MAP_FAILED)
|
||||||
{
|
{
|
||||||
@@ -577,8 +569,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(idxEntries[i].dataType == CdSectorPrefixCorrected ||
|
else if(entry->dataType == CdSectorPrefixCorrected || entry->dataType == CdSectorSuffixCorrected)
|
||||||
idxEntries[i].dataType == CdSectorSuffixCorrected)
|
|
||||||
{
|
{
|
||||||
switch(ddtHeader.compression)
|
switch(ddtHeader.compression)
|
||||||
{
|
{
|
||||||
@@ -651,9 +642,9 @@ void *aaruf_open(const char *filepath)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(idxEntries[i].dataType == CdSectorPrefixCorrected)
|
if(entry->dataType == CdSectorPrefixCorrected)
|
||||||
ctx->sectorPrefixDdt = cdDdt;
|
ctx->sectorPrefixDdt = cdDdt;
|
||||||
else if(idxEntries[i].dataType == CdSectorSuffixCorrected)
|
else if(entry->dataType == CdSectorSuffixCorrected)
|
||||||
ctx->sectorSuffixDdt = cdDdt;
|
ctx->sectorSuffixDdt = cdDdt;
|
||||||
else
|
else
|
||||||
free(cdDdt);
|
free(cdDdt);
|
||||||
@@ -679,9 +670,9 @@ void *aaruf_open(const char *filepath)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(idxEntries[i].dataType == CdSectorPrefixCorrected)
|
if(entry->dataType == CdSectorPrefixCorrected)
|
||||||
ctx->sectorPrefixDdt = cdDdt;
|
ctx->sectorPrefixDdt = cdDdt;
|
||||||
else if(idxEntries[i].dataType == CdSectorSuffixCorrected)
|
else if(entry->dataType == CdSectorSuffixCorrected)
|
||||||
ctx->sectorSuffixDdt = cdDdt;
|
ctx->sectorSuffixDdt = cdDdt;
|
||||||
else
|
else
|
||||||
free(cdDdt);
|
free(cdDdt);
|
||||||
@@ -729,11 +720,11 @@ void *aaruf_open(const char *filepath)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ctx->metadataBlockHeader.identifier != idxEntries[i].blockType)
|
if(ctx->metadataBlockHeader.identifier != entry->blockType)
|
||||||
{
|
{
|
||||||
memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
|
memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -944,7 +935,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
|
memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->imageInfo.ImageSize += sizeof(TrackEntry) * ctx->tracksHeader.entries;
|
ctx->imageInfo.ImageSize += sizeof(TrackEntry) * ctx->tracksHeader.entries;
|
||||||
@@ -983,7 +974,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "libaaruformat: Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries,
|
fprintf(stderr, "libaaruformat: Found %d tracks at position %" PRIu64 ".\n", ctx->tracksHeader.entries,
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
|
|
||||||
ctx->imageInfo.HasPartitions = true;
|
ctx->imageInfo.HasPartitions = true;
|
||||||
ctx->imageInfo.HasSessions = true;
|
ctx->imageInfo.HasSessions = true;
|
||||||
@@ -1021,7 +1012,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
|
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->imageInfo.ImageSize += ctx->cicmBlockHeader.length;
|
ctx->imageInfo.ImageSize += ctx->cicmBlockHeader.length;
|
||||||
@@ -1045,7 +1036,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata block, continuing...\n");
|
fprintf(stderr, "libaaruformat: Could not read CICM XML metadata block, continuing...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "libaaruformat: Found CICM XML metadata block %" PRIu64 ".\n", idxEntries[i].offset);
|
fprintf(stderr, "libaaruformat: Found CICM XML metadata block %" PRIu64 ".\n", entry->offset);
|
||||||
break;
|
break;
|
||||||
// Dump hardware block
|
// Dump hardware block
|
||||||
case DumpHardwareBlock:
|
case DumpHardwareBlock:
|
||||||
@@ -1062,7 +1053,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader));
|
memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader));
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for data block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
data = (uint8_t *)malloc(ctx->dumpHardwareHeader.length);
|
data = (uint8_t *)malloc(ctx->dumpHardwareHeader.length);
|
||||||
@@ -1348,7 +1339,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
memset(&checksum_header, 0, sizeof(ChecksumHeader));
|
memset(&checksum_header, 0, sizeof(ChecksumHeader));
|
||||||
fprintf(stderr, "libaaruformat: Incorrect identifier for checksum block at position %" PRIu64 "\n",
|
fprintf(stderr, "libaaruformat: Incorrect identifier for checksum block at position %" PRIu64 "\n",
|
||||||
idxEntries[i].offset);
|
entry->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
data = (uint8_t *)malloc(checksum_header.length);
|
data = (uint8_t *)malloc(checksum_header.length);
|
||||||
@@ -1414,12 +1405,12 @@ void *aaruf_open(const char *filepath)
|
|||||||
default:
|
default:
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
|
"libaaruformat: Unhandled block type %4.4s with data type %d is indexed to be at %" PRIu64 "\n",
|
||||||
(char *)&idxEntries[i].blockType, idxEntries[i].dataType, idxEntries[i].offset);
|
(char *)&entry->blockType, entry->dataType, entry->offset);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(idxEntries);
|
utarray_free(index_entries);
|
||||||
|
|
||||||
if(!foundUserDataDdt)
|
if(!foundUserDataDdt)
|
||||||
{
|
{
|
||||||
|
|||||||
103
src/verify.c
103
src/verify.c
@@ -16,26 +16,28 @@
|
|||||||
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <aaruformat.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#include <aaruformat.h>
|
#include "internal.h"
|
||||||
|
#include "utarray.h"
|
||||||
|
|
||||||
#define VERIFY_SIZE 1048576
|
#define VERIFY_SIZE 1048576
|
||||||
|
|
||||||
int32_t aaruf_verify_image(void *context)
|
int32_t aaruf_verify_image(void *context)
|
||||||
{
|
{
|
||||||
aaruformatContext *ctx = NULL;
|
aaruformatContext *ctx = NULL;
|
||||||
uint64_t crc64 = 0;
|
uint64_t crc64 = 0;
|
||||||
int i = 0;
|
size_t read_bytes = 0;
|
||||||
IndexHeader index_header;
|
void *buffer = NULL;
|
||||||
IndexEntry *index_entries = NULL;
|
crc64_ctx *crc64_context = NULL;
|
||||||
size_t read_bytes = 0;
|
|
||||||
void *buffer = NULL;
|
|
||||||
crc64_ctx *crc64_context = NULL;
|
|
||||||
BlockHeader block_header;
|
BlockHeader block_header;
|
||||||
uint64_t verified_bytes = 0;
|
uint64_t verified_bytes = 0;
|
||||||
DdtHeader ddt_header;
|
DdtHeader ddt_header;
|
||||||
TracksHeader tracks_header;
|
TracksHeader tracks_header;
|
||||||
|
uint32_t signature = 0;
|
||||||
|
UT_array *index_entries = NULL;
|
||||||
|
int32_t err = 0;
|
||||||
|
|
||||||
if(context == NULL) return AARUF_ERROR_NOT_AARUFORMAT;
|
if(context == NULL) return AARUF_ERROR_NOT_AARUFORMAT;
|
||||||
|
|
||||||
@@ -44,79 +46,64 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
// Not a libaaruformat context
|
// Not a libaaruformat context
|
||||||
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
if(ctx->magic != AARU_MAGIC) return AARUF_ERROR_NOT_AARUFORMAT;
|
||||||
|
|
||||||
// This will traverse all blocks and check their CRC64 without uncompressing them
|
|
||||||
fprintf(stderr, "Checking index integrity at %llu.\n", ctx->header.indexOffset);
|
|
||||||
fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
||||||
|
|
||||||
read_bytes = fread(&index_header, 1, sizeof(IndexHeader), ctx->imageStream);
|
read_bytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
|
||||||
|
if(read_bytes != sizeof(uint32_t))
|
||||||
if(read_bytes != sizeof(IndexHeader))
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not read index header.\n");
|
fprintf(stderr, "Could not read index signature.\n");
|
||||||
return AARUF_ERROR_CANNOT_READ_HEADER;
|
return AARUF_ERROR_CANNOT_READ_HEADER;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(index_header.identifier != IndexBlock)
|
if(signature != IndexBlock)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Incorrect index identifier.\n");
|
fprintf(stderr, "Incorrect index signature.\n");
|
||||||
return AARUF_ERROR_CANNOT_READ_INDEX;
|
return AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "Index at %llu contains %d entries.\n", ctx->header.indexOffset, index_header.entries);
|
// Check if the index is correct
|
||||||
|
err = verify_index_v1(ctx);
|
||||||
|
|
||||||
index_entries = malloc(sizeof(IndexEntry) * index_header.entries);
|
if(err != AARUF_STATUS_OK)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Index verification failed with error code %d.\n", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the index
|
||||||
|
index_entries = process_index_v1(ctx);
|
||||||
|
|
||||||
if(index_entries == NULL)
|
if(index_entries == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Cannot allocate memory for index entries.\n");
|
fprintf(stderr, "Could not process index.\n");
|
||||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
|
||||||
}
|
|
||||||
|
|
||||||
read_bytes = fread(index_entries, 1, sizeof(IndexEntry) * index_header.entries, ctx->imageStream);
|
|
||||||
|
|
||||||
if(read_bytes != sizeof(IndexEntry) * index_header.entries)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Could not read index entries.\n");
|
|
||||||
free(index_entries);
|
|
||||||
return AARUF_ERROR_CANNOT_READ_INDEX;
|
return AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
crc64 = aaruf_crc64_data((const uint8_t *)index_entries, sizeof(IndexEntry) * index_header.entries);
|
|
||||||
|
|
||||||
// Due to how C# wrote it, it is effectively reversed
|
|
||||||
if(ctx->header.imageMajorVersion <= AARUF_VERSION) crc64 = bswap_64(crc64);
|
|
||||||
|
|
||||||
if(crc64 != index_header.crc64)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Expected index CRC 0x%16llX but got 0x%16llX.\n", index_header.crc64, crc64);
|
|
||||||
free(index_entries);
|
|
||||||
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = malloc(VERIFY_SIZE);
|
buffer = malloc(VERIFY_SIZE);
|
||||||
|
|
||||||
if(buffer == NULL)
|
if(buffer == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Cannot allocate memory for buffer.\n");
|
fprintf(stderr, "Cannot allocate memory for buffer.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < index_header.entries; i++)
|
for(int i = 0; i < utarray_len(index_entries); i++)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Checking block with type %4.4s at position %" PRIu64 "\n", (char *)&index_entries[i].blockType,
|
IndexEntry *entry = (IndexEntry *)utarray_eltptr(index_entries, i);
|
||||||
index_entries[i].offset);
|
fprintf(stderr, "Checking block with type %4.4s at position %" PRIu64 "\n", (char *)&entry->blockType,
|
||||||
|
entry->offset);
|
||||||
|
|
||||||
fseek(ctx->imageStream, index_entries[i].offset, SEEK_SET);
|
fseek(ctx->imageStream, entry->offset, SEEK_SET);
|
||||||
|
|
||||||
switch(index_entries[i].blockType)
|
switch(entry->blockType)
|
||||||
{
|
{
|
||||||
case DataBlock:
|
case DataBlock:
|
||||||
read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
|
read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||||
if(read_bytes != sizeof(BlockHeader))
|
if(read_bytes != sizeof(BlockHeader))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not read block header.\n");
|
fprintf(stderr, "Could not read block header.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +112,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64_context == NULL)
|
if(crc64_context == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not initialize CRC64.\n");
|
fprintf(stderr, "Could not initialize CRC64.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +136,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64 != block_header.cmpCrc64)
|
if(crc64 != block_header.cmpCrc64)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Expected block CRC 0x%16llX but got 0x%16llX.\n", block_header.cmpCrc64, crc64);
|
fprintf(stderr, "Expected block CRC 0x%16llX but got 0x%16llX.\n", block_header.cmpCrc64, crc64);
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +146,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(read_bytes != sizeof(DdtHeader))
|
if(read_bytes != sizeof(DdtHeader))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not read DDT header.\n");
|
fprintf(stderr, "Could not read DDT header.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +155,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64_context == NULL)
|
if(crc64_context == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not initialize CRC64.\n");
|
fprintf(stderr, "Could not initialize CRC64.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +179,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64 != ddt_header.cmpCrc64)
|
if(crc64 != ddt_header.cmpCrc64)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Expected DDT CRC 0x%16llX but got 0x%16llX.\n", ddt_header.cmpCrc64, crc64);
|
fprintf(stderr, "Expected DDT CRC 0x%16llX but got 0x%16llX.\n", ddt_header.cmpCrc64, crc64);
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,7 +189,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(read_bytes != sizeof(TracksHeader))
|
if(read_bytes != sizeof(TracksHeader))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not read tracks header.\n");
|
fprintf(stderr, "Could not read tracks header.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,7 +198,7 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64_context == NULL)
|
if(crc64_context == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not initialize CRC64.\n");
|
fprintf(stderr, "Could not initialize CRC64.\n");
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,13 +213,13 @@ int32_t aaruf_verify_image(void *context)
|
|||||||
if(crc64 != tracks_header.crc64)
|
if(crc64 != tracks_header.crc64)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Expected DDT CRC 0x%16llX but got 0x%16llX.\n", tracks_header.crc64, crc64);
|
fprintf(stderr, "Expected DDT CRC 0x%16llX but got 0x%16llX.\n", tracks_header.crc64, crc64);
|
||||||
free(index_entries);
|
utarray_free(index_entries);
|
||||||
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
return AARUF_ERROR_INVALID_BLOCK_CRC;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Ignoring block type %4.4s.\n", (char *)&index_entries[i].blockType);
|
fprintf(stderr, "Ignoring block type %4.4s.\n", (char *)&entry->blockType);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,12 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "commands.h"
|
|
||||||
#include <argtable3.h>
|
#include <argtable3.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "aaruformattool.h"
|
#include "aaruformattool.h"
|
||||||
|
#include "commands.h"
|
||||||
#include "usage.h"
|
#include "usage.h"
|
||||||
|
|
||||||
int cmd_identify(int argc, char *argv[])
|
int cmd_identify(int argc, char *argv[])
|
||||||
|
|||||||
Reference in New Issue
Block a user