mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Refactor variable names for consistency and readability across multiple files
This commit is contained in:
@@ -67,7 +67,7 @@ CheckOptions:
|
||||
- key: readability-identifier-naming.LocalVariableCase
|
||||
value: 'lower_case'
|
||||
- key: readability-identifier-naming.StructCase
|
||||
value: 'lower_case'
|
||||
value: 'CamelCase'
|
||||
- key: readability-identifier-naming.UnionCase
|
||||
value: 'lower_case'
|
||||
- key: readability-identifier-naming.EnumCase
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
/**
|
||||
* Contains an enumeration of all known types of media.
|
||||
*/
|
||||
// NOLINTBEGIN(readability-identifier-naming)
|
||||
typedef enum
|
||||
{
|
||||
// Generics, types 0 to 9
|
||||
@@ -995,6 +996,8 @@ typedef enum
|
||||
//
|
||||
} MediaType;
|
||||
|
||||
// NOLINTEND(readability-identifier-naming)
|
||||
|
||||
/**
|
||||
* Contains information about a dump image and its contents
|
||||
*/
|
||||
@@ -1090,6 +1093,7 @@ typedef enum
|
||||
/*
|
||||
* Metadata present for each media.
|
||||
*/
|
||||
// NOLINTBEGIN(readability-identifier-naming)
|
||||
typedef enum
|
||||
{
|
||||
/* CD table of contents */
|
||||
@@ -1167,6 +1171,8 @@ typedef enum
|
||||
CD_LeadIn = 68
|
||||
} MediaTagType;
|
||||
|
||||
// NOLINTEND(readability-identifier-naming)
|
||||
|
||||
#endif // LIBAARUFORMAT_AARU_H
|
||||
|
||||
#ifndef _MSC_VER
|
||||
|
||||
@@ -45,10 +45,10 @@ typedef struct Crc64Context
|
||||
|
||||
typedef struct CdEccContext
|
||||
{
|
||||
bool initedEdc;
|
||||
uint8_t *eccBTable;
|
||||
uint8_t *eccFTable;
|
||||
uint32_t *edcTable;
|
||||
bool inited_edc;
|
||||
uint8_t *ecc_b_table;
|
||||
uint8_t *ecc_f_table;
|
||||
uint32_t *edc_table;
|
||||
} CdEccContext;
|
||||
|
||||
typedef struct Checksums
|
||||
|
||||
@@ -58,15 +58,15 @@
|
||||
|
||||
AARU_EXPORT int AARU_CALL aaruf_identify(const char *filename);
|
||||
|
||||
AARU_EXPORT int AARU_CALL aaruf_identify_stream(FILE *imageStream);
|
||||
AARU_EXPORT int AARU_CALL aaruf_identify_stream(FILE *image_stream);
|
||||
|
||||
AARU_EXPORT void *AARU_CALL aaruf_open(const char *filepath);
|
||||
|
||||
AARU_EXPORT void *AARU_CALL aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize,
|
||||
uint64_t userSectors, uint64_t negativeSectors, uint64_t overflowSectors,
|
||||
const char *options, const uint8_t *applicationName,
|
||||
uint8_t applicationNameLength, uint8_t applicationMajorVersion,
|
||||
uint8_t applicationMinorVersion);
|
||||
AARU_EXPORT void *AARU_CALL aaruf_create(const char *filepath, uint32_t media_type, uint32_t sector_size,
|
||||
uint64_t user_sectors, uint64_t negative_sectors, uint64_t overflow_sectors,
|
||||
const char *options, const uint8_t *application_name,
|
||||
uint8_t application_name_length, uint8_t application_major_version,
|
||||
uint8_t application_minor_version);
|
||||
|
||||
AARU_EXPORT int AARU_CALL aaruf_close(void *context);
|
||||
|
||||
@@ -79,12 +79,13 @@ AARU_EXPORT void AARU_CALL aaruf_crc64_free(crc64_ctx *ctx);
|
||||
AARU_EXPORT void AARU_CALL aaruf_crc64_slicing(uint64_t *previous_crc, const uint8_t *data, uint32_t len);
|
||||
AARU_EXPORT uint64_t AARU_CALL aaruf_crc64_data(const uint8_t *data, uint32_t len);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint32_t *length);
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_sector(void *context, uint64_t sector_address, uint8_t *data,
|
||||
uint32_t *length);
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(void *context, uint64_t sector_address, uint8_t *data,
|
||||
uint32_t *length);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t *data,
|
||||
uint8_t sectorStatus, uint32_t length);
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sector_address, const uint8_t *data,
|
||||
uint8_t sector_status, uint32_t length);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context);
|
||||
|
||||
@@ -99,18 +100,18 @@ AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_is_suffix_correct(void *context, const u
|
||||
AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector);
|
||||
|
||||
AARU_EXPORT bool AARU_CALL aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data,
|
||||
uint32_t majorCount, uint32_t minorCount, uint32_t majorMult,
|
||||
uint32_t minorInc, const uint8_t *ecc, int32_t addressOffset,
|
||||
int32_t dataOffset, int32_t eccOffset);
|
||||
uint32_t major_count, uint32_t minor_count, uint32_t major_mult,
|
||||
uint32_t minor_inc, const uint8_t *ecc, int32_t address_offset,
|
||||
int32_t data_offset, int32_t ecc_offset);
|
||||
|
||||
AARU_EXPORT void AARU_CALL aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data,
|
||||
uint32_t majorCount, uint32_t minorCount, uint32_t majorMult,
|
||||
uint32_t minorInc, uint8_t *ecc, int32_t addressOffset,
|
||||
int32_t dataOffset, int32_t eccOffset);
|
||||
uint32_t major_count, uint32_t minor_count, uint32_t major_mult,
|
||||
uint32_t minor_inc, uint8_t *ecc, int32_t address_offset,
|
||||
int32_t data_offset, int32_t ecc_offset);
|
||||
|
||||
AARU_EXPORT void AARU_CALL aaruf_ecc_cd_write_sector(void *context, const uint8_t *address, const uint8_t *data,
|
||||
uint8_t *ecc, int32_t addressOffset, int32_t dataOffset,
|
||||
int32_t eccOffset);
|
||||
uint8_t *ecc, int32_t address_offset, int32_t data_offset,
|
||||
int32_t ecc_offset);
|
||||
|
||||
AARU_LOCAL void AARU_CALL aaruf_cd_lba_to_msf(int64_t pos, uint8_t *minute, uint8_t *second, uint8_t *frame);
|
||||
|
||||
@@ -120,7 +121,7 @@ AARU_EXPORT void AARU_CALL aaruf_ecc_cd_reconstruct(void *context, uint8_t *sect
|
||||
|
||||
AARU_EXPORT uint32_t AARU_CALL aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, int size, int pos);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_track_sector(void *context, uint8_t *data, uint64_t sectorAddress,
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_read_track_sector(void *context, uint8_t *data, uint64_t sector_address,
|
||||
uint32_t *length, uint8_t track);
|
||||
|
||||
AARU_LOCAL int32_t AARU_CALL aaruf_get_media_tag_type_for_datatype(int32_t type);
|
||||
@@ -148,12 +149,12 @@ AARU_EXPORT size_t AARU_CALL aaruf_flac_encode_redbook_buffer(
|
||||
uint32_t application_id_len);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_lzma_decode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer,
|
||||
size_t *src_size, const uint8_t *props, size_t propsSize);
|
||||
size_t *src_size, const uint8_t *props, size_t props_size);
|
||||
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_lzma_encode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer,
|
||||
size_t src_size, uint8_t *outProps, size_t *outPropsSize,
|
||||
int32_t level, uint32_t dictSize, int32_t lc, int32_t lp,
|
||||
int32_t pb, int32_t fb, int32_t numThreads);
|
||||
size_t src_size, uint8_t *out_props, size_t *out_props_size,
|
||||
int32_t level, uint32_t dict_size, int32_t lc, int32_t lp,
|
||||
int32_t pb, int32_t fb, int32_t num_threads);
|
||||
|
||||
#if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \
|
||||
defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86)
|
||||
|
||||
@@ -28,8 +28,8 @@ int32_t verify_index_v2(aaruformatContext *ctx);
|
||||
UT_array *process_index_v3(aaruformatContext *ctx);
|
||||
int32_t verify_index_v3(aaruformatContext *ctx);
|
||||
int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry);
|
||||
int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt);
|
||||
int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt);
|
||||
int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *found_user_data_ddt);
|
||||
int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *found_user_data_ddt);
|
||||
void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
@@ -37,20 +37,20 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry);
|
||||
void add_subindex_entries(aaruformatContext *ctx, UT_array *index_entries, IndexEntry *subindex_entry);
|
||||
int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, uint64_t *blockOffset,
|
||||
uint8_t *sectorStatus);
|
||||
int32_t decode_ddt_entry_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, uint64_t *blockOffset,
|
||||
uint8_t *sectorStatus);
|
||||
int32_t decode_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset,
|
||||
uint64_t *blockOffset, uint8_t *sectorStatus);
|
||||
int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset,
|
||||
uint64_t *blockOffset, uint8_t *sectorStatus);
|
||||
void set_ddt_entry_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t offset, uint64_t blockOffset,
|
||||
uint8_t sectorStatus);
|
||||
void set_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool negative, uint64_t offset,
|
||||
uint64_t blockOffset, uint8_t sectorStatus);
|
||||
void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool negative, uint64_t offset,
|
||||
uint64_t blockOffset, uint8_t sectorStatus);
|
||||
int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
|
||||
uint8_t *sector_status);
|
||||
int32_t decode_ddt_entry_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
|
||||
uint8_t *sector_status);
|
||||
int32_t decode_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset,
|
||||
uint64_t *block_offset, uint8_t *sector_status);
|
||||
int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset,
|
||||
uint64_t *block_offset, uint8_t *sector_status);
|
||||
void set_ddt_entry_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t offset, uint64_t block_offset,
|
||||
uint8_t sector_status);
|
||||
void set_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sector_address, bool negative, uint64_t offset,
|
||||
uint64_t block_offset, uint8_t sector_status);
|
||||
void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sector_address, bool negative, uint64_t offset,
|
||||
uint64_t block_offset, uint8_t sector_status);
|
||||
aaru_options parse_options(const char *options);
|
||||
uint64_t get_filetime_uint64();
|
||||
int32_t aaruf_close_current_block(aaruformatContext *ctx);
|
||||
|
||||
@@ -37,7 +37,7 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
TRACE("Entering process_checksum_block(%p, %p)", ctx, entry);
|
||||
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
ChecksumHeader checksum_header;
|
||||
ChecksumEntry const *checksum_entry = NULL;
|
||||
uint8_t *data = NULL;
|
||||
@@ -61,9 +61,9 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
TRACE("Reading checksum block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
|
||||
read_bytes = fread(&checksum_header, 1, sizeof(ChecksumHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(ChecksumHeader))
|
||||
if(read_bytes != sizeof(ChecksumHeader))
|
||||
{
|
||||
memset(&checksum_header, 0, sizeof(ChecksumHeader));
|
||||
FATAL("Could not read checksums block header, continuing...\n");
|
||||
@@ -87,9 +87,9 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
}
|
||||
|
||||
TRACE("Reading checksum block data at position %" PRIu64, entry->offset + sizeof(ChecksumHeader));
|
||||
readBytes = fread(data, 1, checksum_header.length, ctx->imageStream);
|
||||
read_bytes = fread(data, 1, checksum_header.length, ctx->imageStream);
|
||||
|
||||
if(readBytes != checksum_header.length)
|
||||
if(read_bytes != checksum_header.length)
|
||||
{
|
||||
memset(&checksum_header, 0, sizeof(ChecksumHeader));
|
||||
free(data);
|
||||
|
||||
@@ -37,17 +37,17 @@
|
||||
int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
{
|
||||
TRACE("Entering process_data_block(%p, %p)", ctx, entry);
|
||||
BlockHeader blockHeader;
|
||||
BlockHeader block_header;
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t lzmaSize = 0;
|
||||
uint8_t *cmpData = NULL;
|
||||
uint8_t *cstData = NULL;
|
||||
mediaTagEntry *oldMediaTag = NULL;
|
||||
mediaTagEntry *mediaTag = NULL;
|
||||
size_t read_bytes = 0;
|
||||
size_t lzma_size = 0;
|
||||
uint8_t *cmp_data = NULL;
|
||||
uint8_t *cst_data = NULL;
|
||||
mediaTagEntry *old_media_tag = NULL;
|
||||
mediaTagEntry *media_tag = NULL;
|
||||
uint8_t *data = NULL;
|
||||
int errorNo = 0;
|
||||
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
||||
int error_no = 0;
|
||||
uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
|
||||
uint64_t crc64 = 0;
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
@@ -74,9 +74,9 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
if(entry->dataType == NoData) return AARUF_STATUS_OK;
|
||||
|
||||
TRACE("Reading block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||
read_bytes = fread(&block_header, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(BlockHeader))
|
||||
if(read_bytes != sizeof(BlockHeader))
|
||||
{
|
||||
FATAL("Could not read block header at %" PRIu64, entry->offset);
|
||||
|
||||
@@ -84,22 +84,22 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
ctx->imageInfo.ImageSize += blockHeader.cmpLength;
|
||||
ctx->imageInfo.ImageSize += block_header.cmpLength;
|
||||
|
||||
// Unused, skip
|
||||
if(entry->dataType == UserData)
|
||||
{
|
||||
if(blockHeader.sectorSize > ctx->imageInfo.SectorSize)
|
||||
if(block_header.sectorSize > ctx->imageInfo.SectorSize)
|
||||
{
|
||||
TRACE("Setting sector size to %" PRIu64 " bytes", blockHeader.sectorSize);
|
||||
ctx->imageInfo.SectorSize = blockHeader.sectorSize;
|
||||
TRACE("Setting sector size to %" PRIu64 " bytes", block_header.sectorSize);
|
||||
ctx->imageInfo.SectorSize = block_header.sectorSize;
|
||||
}
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
if(blockHeader.identifier != entry->blockType)
|
||||
if(block_header.identifier != entry->blockType)
|
||||
{
|
||||
TRACE("Incorrect identifier for data block at position %" PRIu64, entry->offset);
|
||||
|
||||
@@ -107,10 +107,10 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
if(blockHeader.type != entry->dataType)
|
||||
if(block_header.type != entry->dataType)
|
||||
{
|
||||
TRACE("Expected block with data type %4.4s at position %" PRIu64 " but found data type %4.4s",
|
||||
(char *)&entry->blockType, entry->offset, (char *)&blockHeader.type);
|
||||
(char *)&entry->blockType, entry->offset, (char *)&block_header.type);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
@@ -118,21 +118,21 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
|
||||
TRACE("Found data block with type %4.4s at position %" PRIu64, (char *)&entry->blockType, entry->offset);
|
||||
|
||||
if(blockHeader.compression == Lzma || blockHeader.compression == LzmaClauniaSubchannelTransform)
|
||||
if(block_header.compression == Lzma || block_header.compression == LzmaClauniaSubchannelTransform)
|
||||
{
|
||||
if(blockHeader.compression == LzmaClauniaSubchannelTransform && blockHeader.type != CdSectorSubchannel)
|
||||
if(block_header.compression == LzmaClauniaSubchannelTransform && block_header.type != CdSectorSubchannel)
|
||||
{
|
||||
TRACE("Invalid compression type %d for block with data type %d, continuing...", blockHeader.compression,
|
||||
blockHeader.type);
|
||||
TRACE("Invalid compression type %d for block with data type %d, continuing...", block_header.compression,
|
||||
block_header.type);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
lzmaSize = blockHeader.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
lzma_size = block_header.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
|
||||
cmpData = (uint8_t *)malloc(lzmaSize);
|
||||
if(cmpData == NULL)
|
||||
cmp_data = (uint8_t *)malloc(lzma_size);
|
||||
if(cmp_data == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for block, continuing...");
|
||||
|
||||
@@ -140,86 +140,86 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
data = (uint8_t *)malloc(blockHeader.length);
|
||||
data = (uint8_t *)malloc(block_header.length);
|
||||
if(data == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for block, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
||||
read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(read_bytes != LZMA_PROPERTIES_LENGTH)
|
||||
{
|
||||
TRACE("Could not read LZMA properties, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
||||
if(readBytes != lzmaSize)
|
||||
read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
|
||||
if(read_bytes != lzma_size)
|
||||
{
|
||||
TRACE("Could not read compressed block, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
readBytes = blockHeader.length;
|
||||
errorNo =
|
||||
aaruf_lzma_decode_buffer(data, &readBytes, cmpData, &lzmaSize, lzmaProperties, LZMA_PROPERTIES_LENGTH);
|
||||
read_bytes = block_header.length;
|
||||
error_no =
|
||||
aaruf_lzma_decode_buffer(data, &read_bytes, cmp_data, &lzma_size, lzma_properties, LZMA_PROPERTIES_LENGTH);
|
||||
|
||||
if(errorNo != 0)
|
||||
if(error_no != 0)
|
||||
{
|
||||
TRACE("Got error %d from LZMA, continuing...", errorNo);
|
||||
free(cmpData);
|
||||
TRACE("Got error %d from LZMA, continuing...", error_no);
|
||||
free(cmp_data);
|
||||
free(data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(readBytes != blockHeader.length)
|
||||
if(read_bytes != block_header.length)
|
||||
{
|
||||
TRACE("Error decompressing block, should be {0} bytes but got {1} bytes., continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(blockHeader.compression == LzmaClauniaSubchannelTransform)
|
||||
if(block_header.compression == LzmaClauniaSubchannelTransform)
|
||||
{
|
||||
cstData = malloc(blockHeader.length);
|
||||
if(cstData == NULL)
|
||||
cst_data = malloc(block_header.length);
|
||||
if(cst_data == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for block, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(data);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
aaruf_cst_untransform(data, cstData, blockHeader.length);
|
||||
aaruf_cst_untransform(data, cst_data, block_header.length);
|
||||
free(data);
|
||||
data = cstData;
|
||||
cstData = NULL;
|
||||
data = cst_data;
|
||||
cst_data = NULL;
|
||||
}
|
||||
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
}
|
||||
else if(blockHeader.compression == None)
|
||||
else if(block_header.compression == None)
|
||||
{
|
||||
data = (uint8_t *)malloc(blockHeader.length);
|
||||
data = (uint8_t *)malloc(block_header.length);
|
||||
if(data == NULL)
|
||||
{
|
||||
fprintf(stderr, "Cannot allocate memory for block, continuing...");
|
||||
@@ -228,9 +228,9 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
readBytes = fread(data, 1, blockHeader.length, ctx->imageStream);
|
||||
read_bytes = fread(data, 1, block_header.length, ctx->imageStream);
|
||||
|
||||
if(readBytes != blockHeader.length)
|
||||
if(read_bytes != block_header.length)
|
||||
{
|
||||
free(data);
|
||||
fprintf(stderr, "Could not read block, continuing...");
|
||||
@@ -241,23 +241,23 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE("Found unknown compression type %d, continuing...", blockHeader.compression);
|
||||
TRACE("Found unknown compression type %d, continuing...", block_header.compression);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
if(blockHeader.length > 0)
|
||||
if(block_header.length > 0)
|
||||
{
|
||||
crc64 = aaruf_crc64_data(data, blockHeader.length);
|
||||
crc64 = aaruf_crc64_data(data, block_header.length);
|
||||
|
||||
// Due to how C# wrote it, it is effectively reversed
|
||||
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64);
|
||||
|
||||
if(crc64 != blockHeader.crc64)
|
||||
if(crc64 != block_header.crc64)
|
||||
{
|
||||
TRACE("Incorrect CRC found: 0x%" PRIx64 " found, expected 0x%" PRIx64 ", continuing...", crc64,
|
||||
blockHeader.crc64);
|
||||
block_header.crc64);
|
||||
|
||||
TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
@@ -304,27 +304,27 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
|
||||
ctx->mode2Subheaders = data;
|
||||
break;
|
||||
default:
|
||||
mediaTag = (mediaTagEntry *)malloc(sizeof(mediaTagEntry));
|
||||
media_tag = (mediaTagEntry *)malloc(sizeof(mediaTagEntry));
|
||||
|
||||
if(mediaTag == NULL)
|
||||
if(media_tag == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for media tag entry.");
|
||||
break;
|
||||
}
|
||||
memset(mediaTag, 0, sizeof(mediaTagEntry));
|
||||
memset(media_tag, 0, sizeof(mediaTagEntry));
|
||||
|
||||
mediaTag->type = aaruf_get_media_tag_type_for_datatype(blockHeader.type);
|
||||
mediaTag->data = data;
|
||||
mediaTag->length = blockHeader.length;
|
||||
media_tag->type = aaruf_get_media_tag_type_for_datatype(block_header.type);
|
||||
media_tag->data = data;
|
||||
media_tag->length = block_header.length;
|
||||
|
||||
HASH_REPLACE_INT(ctx->mediaTags, type, mediaTag, oldMediaTag);
|
||||
HASH_REPLACE_INT(ctx->mediaTags, type, media_tag, old_media_tag);
|
||||
|
||||
if(oldMediaTag != NULL)
|
||||
if(old_media_tag != NULL)
|
||||
{
|
||||
TRACE("Replaced media tag with type %d", oldMediaTag->type);
|
||||
free(oldMediaTag->data);
|
||||
free(oldMediaTag);
|
||||
oldMediaTag = NULL;
|
||||
TRACE("Replaced media tag with type %d", old_media_tag->type);
|
||||
free(old_media_tag->data);
|
||||
free(old_media_tag);
|
||||
old_media_tag = NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
@@ -36,7 +36,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
TRACE("Entering process_dumphw_block(%p, %p)", ctx, entry);
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
uint64_t crc64 = 0;
|
||||
uint16_t e = 0;
|
||||
uint8_t *data = NULL;
|
||||
@@ -62,9 +62,9 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
TRACE("Reading dump hardware block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&ctx->dumpHardwareHeader, 1, sizeof(DumpHardwareHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->dumpHardwareHeader, 1, sizeof(DumpHardwareHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(DumpHardwareHeader))
|
||||
if(read_bytes != sizeof(DumpHardwareHeader))
|
||||
{
|
||||
memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader));
|
||||
TRACE("Could not read dump hardware block header, continuing...");
|
||||
@@ -89,9 +89,9 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
return;
|
||||
}
|
||||
|
||||
readBytes = fread(data, 1, ctx->dumpHardwareHeader.length, ctx->imageStream);
|
||||
read_bytes = fread(data, 1, ctx->dumpHardwareHeader.length, ctx->imageStream);
|
||||
|
||||
if(readBytes == ctx->dumpHardwareHeader.length)
|
||||
if(read_bytes == ctx->dumpHardwareHeader.length)
|
||||
{
|
||||
crc64 = aaruf_crc64_data(data, ctx->dumpHardwareHeader.length);
|
||||
|
||||
@@ -109,7 +109,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
}
|
||||
|
||||
free(data);
|
||||
fseek(ctx->imageStream, -(long)readBytes, SEEK_CUR);
|
||||
fseek(ctx->imageStream, -(long)read_bytes, SEEK_CUR);
|
||||
|
||||
ctx->dumpHardwareEntriesWithData =
|
||||
(DumpHardwareEntriesWithData *)malloc(sizeof(DumpHardwareEntriesWithData) * ctx->dumpHardwareHeader.entries);
|
||||
@@ -127,9 +127,9 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
TRACE("Processing %u dump hardware block entries", ctx->dumpHardwareHeader.entries);
|
||||
for(e = 0; e < ctx->dumpHardwareHeader.entries; e++)
|
||||
{
|
||||
readBytes = fread(&ctx->dumpHardwareEntriesWithData[e].entry, 1, sizeof(DumpHardwareEntry), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->dumpHardwareEntriesWithData[e].entry, 1, sizeof(DumpHardwareEntry), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(DumpHardwareEntry))
|
||||
if(read_bytes != sizeof(DumpHardwareEntry))
|
||||
{
|
||||
ctx->dumpHardwareHeader.entries = e;
|
||||
TRACE("Could not read dump hardware block entry, continuing...");
|
||||
@@ -145,10 +145,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e]
|
||||
.manufacturer[ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength] = 0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].manufacturer, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].manufacturer, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].manufacturer);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength = 0;
|
||||
@@ -166,10 +166,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
if(ctx->dumpHardwareEntriesWithData[e].model != NULL)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e].model[ctx->dumpHardwareEntriesWithData[e].entry.modelLength] = 0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].model, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].model, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.modelLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.modelLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.modelLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].model);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.modelLength = 0;
|
||||
@@ -187,10 +187,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e].revision[ctx->dumpHardwareEntriesWithData[e].entry.revisionLength] =
|
||||
0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].revision, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].revision, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.revisionLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.revisionLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.revisionLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].revision);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.revisionLength = 0;
|
||||
@@ -209,10 +209,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e].firmware[ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength] =
|
||||
0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].firmware, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].firmware, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].firmware);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength = 0;
|
||||
@@ -230,10 +230,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
if(ctx->dumpHardwareEntriesWithData[e].serial != NULL)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e].serial[ctx->dumpHardwareEntriesWithData[e].entry.serialLength] = 0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].serial, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].serial, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.serialLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.serialLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.serialLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].serial);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.serialLength = 0;
|
||||
@@ -251,10 +251,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e]
|
||||
.softwareName[ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength] = 0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].softwareName, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].softwareName, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].softwareName);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength = 0;
|
||||
@@ -273,10 +273,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
ctx->dumpHardwareEntriesWithData[e]
|
||||
.softwareVersion[ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength] = 0;
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].softwareVersion, 1,
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].softwareVersion, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].softwareVersion);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength = 0;
|
||||
@@ -296,11 +296,11 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
ctx->dumpHardwareEntriesWithData[e]
|
||||
.softwareOperatingSystem[ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength] =
|
||||
0;
|
||||
readBytes =
|
||||
read_bytes =
|
||||
fread(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem, 1,
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem);
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength = 0;
|
||||
@@ -320,10 +320,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
continue;
|
||||
}
|
||||
|
||||
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].extents, sizeof(DumpExtent),
|
||||
read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].extents, sizeof(DumpExtent),
|
||||
ctx->dumpHardwareEntriesWithData[e].entry.extents, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->dumpHardwareEntriesWithData->entry.extents)
|
||||
if(read_bytes != ctx->dumpHardwareEntriesWithData->entry.extents)
|
||||
{
|
||||
free(ctx->dumpHardwareEntriesWithData[e].extents);
|
||||
TRACE("Could not read dump hardware block extents, continuing...");
|
||||
|
||||
@@ -36,7 +36,7 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
TRACE("Entering process_metadata_block(%p, %p)", ctx, entry);
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
if(ctx == NULL || ctx->imageStream == NULL)
|
||||
@@ -59,9 +59,9 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
TRACE("Reading metadata block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&ctx->metadataBlockHeader, 1, sizeof(MetadataBlockHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->metadataBlockHeader, 1, sizeof(MetadataBlockHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(MetadataBlockHeader))
|
||||
if(read_bytes != sizeof(MetadataBlockHeader))
|
||||
{
|
||||
memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
|
||||
FATAL("Could not read metadata block header, continuing...");
|
||||
@@ -94,9 +94,9 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
TRACE("Reading metadata block of size %u at position %" PRIu64, ctx->metadataBlockHeader.blockSize,
|
||||
entry->offset + sizeof(MetadataBlockHeader));
|
||||
readBytes = fread(ctx->metadataBlock, 1, ctx->metadataBlockHeader.blockSize, ctx->imageStream);
|
||||
read_bytes = fread(ctx->metadataBlock, 1, ctx->metadataBlockHeader.blockSize, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->metadataBlockHeader.blockSize)
|
||||
if(read_bytes != ctx->metadataBlockHeader.blockSize)
|
||||
{
|
||||
memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
|
||||
free(ctx->metadataBlock);
|
||||
@@ -249,7 +249,7 @@ void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
TRACE("Entering process_geometry_block(%p, %p)", ctx, entry);
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
if(ctx == NULL || ctx->imageStream == NULL)
|
||||
@@ -270,9 +270,9 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
}
|
||||
|
||||
TRACE("Reading geometry block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&ctx->geometryBlock, 1, sizeof(GeometryBlockHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->geometryBlock, 1, sizeof(GeometryBlockHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(GeometryBlockHeader))
|
||||
if(read_bytes != sizeof(GeometryBlockHeader))
|
||||
{
|
||||
memset(&ctx->geometryBlock, 0, sizeof(GeometryBlockHeader));
|
||||
TRACE("Could not read geometry block header, continuing...");
|
||||
@@ -310,7 +310,7 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
TRACE("Entering process_cicm_block(%p, %p)", ctx, entry);
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
if(ctx == NULL || ctx->imageStream == NULL)
|
||||
@@ -334,9 +334,9 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
TRACE("Reading CICM XML metadata block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&ctx->cicmBlockHeader, 1, sizeof(CicmMetadataBlock), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->cicmBlockHeader, 1, sizeof(CicmMetadataBlock), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(CicmMetadataBlock))
|
||||
if(read_bytes != sizeof(CicmMetadataBlock))
|
||||
{
|
||||
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
|
||||
TRACE("Could not read CICM XML metadata header, continuing...");
|
||||
@@ -364,9 +364,9 @@ void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
|
||||
TRACE("Reading CICM XML metadata block of size %u at position %" PRIu64, ctx->cicmBlockHeader.length,
|
||||
entry->offset + sizeof(CicmMetadataBlock));
|
||||
readBytes = fread(ctx->cicmBlock, 1, ctx->cicmBlockHeader.length, ctx->imageStream);
|
||||
read_bytes = fread(ctx->cicmBlock, 1, ctx->cicmBlockHeader.length, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->metadataBlockHeader.blockSize)
|
||||
if(read_bytes != ctx->metadataBlockHeader.blockSize)
|
||||
{
|
||||
memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
|
||||
free(ctx->cicmBlock);
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
{
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
size_t read_bytes = 0;
|
||||
uint64_t crc64 = 0;
|
||||
int j = 0, k = 0;
|
||||
|
||||
@@ -56,9 +56,9 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
}
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
readBytes = fread(&ctx->tracksHeader, 1, sizeof(TracksHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->tracksHeader, 1, sizeof(TracksHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(TracksHeader))
|
||||
if(read_bytes != sizeof(TracksHeader))
|
||||
{
|
||||
memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
|
||||
TRACE("Could not read tracks header, continuing...\n");
|
||||
@@ -82,9 +82,9 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
|
||||
return;
|
||||
}
|
||||
|
||||
readBytes = fread(ctx->trackEntries, sizeof(TrackEntry), ctx->tracksHeader.entries, ctx->imageStream);
|
||||
read_bytes = fread(ctx->trackEntries, sizeof(TrackEntry), ctx->tracksHeader.entries, ctx->imageStream);
|
||||
|
||||
if(readBytes != ctx->tracksHeader.entries)
|
||||
if(read_bytes != ctx->tracksHeader.entries)
|
||||
{
|
||||
memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
|
||||
free(ctx->trackEntries);
|
||||
|
||||
@@ -44,32 +44,32 @@ void *aaruf_ecc_cd_init()
|
||||
if(context == NULL) return NULL;
|
||||
|
||||
TRACE("Allocating memory for ECC F table");
|
||||
context->eccFTable = (uint8_t *)malloc(sizeof(uint8_t) * 256);
|
||||
context->ecc_f_table = (uint8_t *)malloc(sizeof(uint8_t) * 256);
|
||||
|
||||
if(context->eccFTable == NULL)
|
||||
if(context->ecc_f_table == NULL)
|
||||
{
|
||||
free(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRACE("Allocating memory for ECC B table");
|
||||
context->eccBTable = (uint8_t *)malloc(sizeof(uint8_t) * 256);
|
||||
context->ecc_b_table = (uint8_t *)malloc(sizeof(uint8_t) * 256);
|
||||
|
||||
if(context->eccBTable == NULL)
|
||||
if(context->ecc_b_table == NULL)
|
||||
{
|
||||
free(context->eccFTable);
|
||||
free(context->ecc_f_table);
|
||||
free(context);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TRACE("Allocating memory for EDC table");
|
||||
|
||||
context->edcTable = (uint32_t *)malloc(sizeof(uint32_t) * 256);
|
||||
context->edc_table = (uint32_t *)malloc(sizeof(uint32_t) * 256);
|
||||
|
||||
if(context->edcTable == NULL)
|
||||
if(context->edc_table == NULL)
|
||||
{
|
||||
free(context->eccFTable);
|
||||
free(context->eccBTable);
|
||||
free(context->ecc_f_table);
|
||||
free(context->ecc_b_table);
|
||||
free(context);
|
||||
return NULL;
|
||||
}
|
||||
@@ -79,13 +79,13 @@ void *aaruf_ecc_cd_init()
|
||||
{
|
||||
edc = i;
|
||||
j = (uint32_t)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
|
||||
context->eccFTable[i] = (uint8_t)j;
|
||||
context->eccBTable[i ^ j] = (uint8_t)i;
|
||||
context->ecc_f_table[i] = (uint8_t)j;
|
||||
context->ecc_b_table[i ^ j] = (uint8_t)i;
|
||||
for(j = 0; j < 8; j++) edc = (edc >> 1) ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
|
||||
context->edcTable[i] = edc;
|
||||
context->edc_table[i] = edc;
|
||||
}
|
||||
|
||||
context->initedEdc = true;
|
||||
context->inited_edc = true;
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_init()");
|
||||
return context;
|
||||
@@ -102,7 +102,7 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
|
||||
CdEccContext *ctx;
|
||||
uint32_t storedEdc, edc, calculatedEdc;
|
||||
uint32_t stored_edc, edc, calculated_edc;
|
||||
int size, pos;
|
||||
|
||||
if(context == NULL || sector == NULL)
|
||||
@@ -112,7 +112,7 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
|
||||
}
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
|
||||
return false;
|
||||
@@ -127,29 +127,32 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool correctEccP = aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
|
||||
if(!correctEccP)
|
||||
bool correct_ecc_p = aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
|
||||
if(!correct_ecc_p)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool correctEccQ = aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
|
||||
if(!correctEccQ)
|
||||
bool correct_ecc_q = aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
|
||||
if(!correct_ecc_q)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
|
||||
return false;
|
||||
}
|
||||
|
||||
storedEdc = (sector[0x813] << 24) + (sector[0x812] << 16) + (sector[0x811] << 8) + sector[0x810];
|
||||
edc = 0;
|
||||
size = 0x810;
|
||||
pos = 0;
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edcTable[(edc ^ sector[pos++]) & 0xFF];
|
||||
calculatedEdc = edc;
|
||||
stored_edc = (uint32_t)sector[0x808] | (uint32_t)sector[0x809] << 8 | (uint32_t)sector[0x80A] << 16 |
|
||||
(uint32_t)sector[0x80B] << 24;
|
||||
calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculatedEdc, storedEdc);
|
||||
return calculatedEdc == storedEdc;
|
||||
if(stored_edc != calculated_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
|
||||
return false;
|
||||
}
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculated_edc, stored_edc);
|
||||
return calculated_edc == stored_edc;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +166,7 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
|
||||
CdEccContext *ctx;
|
||||
uint32_t storedEdc, edc, calculatedEdc;
|
||||
uint32_t stored_edc, edc, calculated_edc;
|
||||
int size, pos;
|
||||
uint8_t zeroaddress[4];
|
||||
|
||||
@@ -175,7 +178,7 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
|
||||
return false;
|
||||
@@ -183,27 +186,25 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
|
||||
|
||||
memset(&zeroaddress, 4, sizeof(uint8_t));
|
||||
|
||||
bool correctEccP = aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
|
||||
if(!correctEccP)
|
||||
bool correct_ecc_p = aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
|
||||
if(!correct_ecc_p)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
|
||||
return false;
|
||||
}
|
||||
bool correctEccQ = aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC);
|
||||
if(!correctEccQ)
|
||||
bool correct_ecc_q =
|
||||
aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC);
|
||||
if(!correct_ecc_q)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
|
||||
return false;
|
||||
}
|
||||
storedEdc = (sector[0x81B] << 24) + (sector[0x81A] << 16) + (sector[0x819] << 8) + sector[0x818];
|
||||
edc = 0;
|
||||
size = 0x808;
|
||||
pos = 0x10;
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edcTable[(edc ^ sector[pos++]) & 0xFF];
|
||||
calculatedEdc = edc;
|
||||
stored_edc = (uint32_t)sector[0x92C] | (uint32_t)sector[0x92D] << 8 | (uint32_t)sector[0x92E] << 16 |
|
||||
(uint32_t)sector[0x92F] << 24;
|
||||
calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculatedEdc, storedEdc);
|
||||
return calculatedEdc == storedEdc;
|
||||
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
|
||||
return calculated_edc == stored_edc;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,26 +213,26 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
|
||||
* @param context Pointer to the ECC context.
|
||||
* @param address Pointer to the address field.
|
||||
* @param data Pointer to the data field.
|
||||
* @param majorCount Number of major iterations.
|
||||
* @param minorCount Number of minor iterations.
|
||||
* @param majorMult Major multiplier.
|
||||
* @param minorInc Minor increment.
|
||||
* @param major_count Number of major iterations.
|
||||
* @param minor_count Number of minor iterations.
|
||||
* @param major_mult Major multiplier.
|
||||
* @param minor_inc Minor increment.
|
||||
* @param ecc Pointer to the ECC field.
|
||||
* @param addressOffset Offset for the address field.
|
||||
* @param dataOffset Offset for the data field.
|
||||
* @param eccOffset Offset for the ECC field.
|
||||
* @param address_offset Offset for the address field.
|
||||
* @param data_offset Offset for the data field.
|
||||
* @param ecc_offset Offset for the ECC field.
|
||||
* @return true if ECC is correct, false otherwise.
|
||||
*/
|
||||
bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data, uint32_t majorCount,
|
||||
uint32_t minorCount, uint32_t majorMult, uint32_t minorInc, const uint8_t *ecc,
|
||||
int32_t addressOffset, int32_t dataOffset, int32_t eccOffset)
|
||||
bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data, uint32_t major_count,
|
||||
uint32_t minor_count, uint32_t major_mult, uint32_t minor_inc, const uint8_t *ecc,
|
||||
int32_t address_offset, int32_t data_offset, int32_t ecc_offset)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data, majorCount,
|
||||
minorCount, majorMult, minorInc, ecc, addressOffset, dataOffset, eccOffset);
|
||||
TRACE("Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
|
||||
major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
|
||||
|
||||
CdEccContext *ctx;
|
||||
uint32_t size, major, idx, minor;
|
||||
uint8_t eccA, eccB, temp;
|
||||
uint8_t ecc_a, ecc_b, temp;
|
||||
|
||||
if(context == NULL || address == NULL || data == NULL || ecc == NULL)
|
||||
{
|
||||
@@ -241,30 +242,30 @@ bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *da
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_check() without initialized context");
|
||||
return false;
|
||||
}
|
||||
|
||||
size = majorCount * minorCount;
|
||||
for(major = 0; major < majorCount; major++)
|
||||
size = major_count * minor_count;
|
||||
for(major = 0; major < major_count; major++)
|
||||
{
|
||||
idx = (major >> 1) * majorMult + (major & 1);
|
||||
eccA = 0;
|
||||
eccB = 0;
|
||||
for(minor = 0; minor < minorCount; minor++)
|
||||
idx = (major >> 1) * major_mult + (major & 1);
|
||||
ecc_a = 0;
|
||||
ecc_b = 0;
|
||||
for(minor = 0; minor < minor_count; minor++)
|
||||
{
|
||||
temp = idx < 4 ? address[idx + addressOffset] : data[idx + dataOffset - 4];
|
||||
idx += minorInc;
|
||||
temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
|
||||
idx += minor_inc;
|
||||
if(idx >= size) idx -= size;
|
||||
eccA ^= temp;
|
||||
eccB ^= temp;
|
||||
eccA = ctx->eccFTable[eccA];
|
||||
ecc_a ^= temp;
|
||||
ecc_b ^= temp;
|
||||
ecc_a = ctx->ecc_f_table[ecc_a];
|
||||
}
|
||||
|
||||
eccA = ctx->eccBTable[ctx->eccFTable[eccA] ^ eccB];
|
||||
if(ecc[major + eccOffset] != eccA || ecc[major + majorCount + eccOffset] != (eccA ^ eccB))
|
||||
ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
|
||||
if(ecc[major + ecc_offset] != ecc_a || ecc[major + major_count + ecc_offset] != (ecc_a ^ ecc_b))
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_check() = false, ECC mismatch at major %u", major);
|
||||
return false;
|
||||
@@ -281,25 +282,25 @@ bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *da
|
||||
* @param context Pointer to the ECC context.
|
||||
* @param address Pointer to the address field.
|
||||
* @param data Pointer to the data field.
|
||||
* @param majorCount Number of major iterations.
|
||||
* @param minorCount Number of minor iterations.
|
||||
* @param majorMult Major multiplier.
|
||||
* @param minorInc Minor increment.
|
||||
* @param major_count Number of major iterations.
|
||||
* @param minor_count Number of minor iterations.
|
||||
* @param major_mult Major multiplier.
|
||||
* @param minor_inc Minor increment.
|
||||
* @param ecc Pointer to the ECC field to write.
|
||||
* @param addressOffset Offset for the address field.
|
||||
* @param dataOffset Offset for the data field.
|
||||
* @param eccOffset Offset for the ECC field.
|
||||
* @param address_offset Offset for the address field.
|
||||
* @param data_offset Offset for the data field.
|
||||
* @param ecc_offset Offset for the ECC field.
|
||||
*/
|
||||
void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data, uint32_t majorCount,
|
||||
uint32_t minorCount, uint32_t majorMult, uint32_t minorInc, uint8_t *ecc, int32_t addressOffset,
|
||||
int32_t dataOffset, int32_t eccOffset)
|
||||
void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data, uint32_t major_count,
|
||||
uint32_t minor_count, uint32_t major_mult, uint32_t minor_inc, uint8_t *ecc,
|
||||
int32_t address_offset, int32_t data_offset, int32_t ecc_offset)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data, majorCount,
|
||||
minorCount, majorMult, minorInc, ecc, addressOffset, dataOffset, eccOffset);
|
||||
TRACE("Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
|
||||
major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
|
||||
|
||||
CdEccContext *ctx;
|
||||
uint32_t size, major, idx, minor;
|
||||
uint8_t eccA, eccB, temp;
|
||||
uint8_t ecc_a, ecc_b, temp;
|
||||
|
||||
if(context == NULL || address == NULL || data == NULL || ecc == NULL)
|
||||
{
|
||||
@@ -309,32 +310,32 @@ void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *da
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_write() without initialized context");
|
||||
return;
|
||||
}
|
||||
|
||||
size = majorCount * minorCount;
|
||||
for(major = 0; major < majorCount; major++)
|
||||
size = major_count * minor_count;
|
||||
for(major = 0; major < major_count; major++)
|
||||
{
|
||||
idx = (major >> 1) * majorMult + (major & 1);
|
||||
eccA = 0;
|
||||
eccB = 0;
|
||||
idx = (major >> 1) * major_mult + (major & 1);
|
||||
ecc_a = 0;
|
||||
ecc_b = 0;
|
||||
|
||||
for(minor = 0; minor < minorCount; minor++)
|
||||
for(minor = 0; minor < minor_count; minor++)
|
||||
{
|
||||
temp = idx < 4 ? address[idx + addressOffset] : data[idx + dataOffset - 4];
|
||||
idx += minorInc;
|
||||
temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
|
||||
idx += minor_inc;
|
||||
if(idx >= size) idx -= size;
|
||||
eccA ^= temp;
|
||||
eccB ^= temp;
|
||||
eccA = ctx->eccFTable[eccA];
|
||||
ecc_a ^= temp;
|
||||
ecc_b ^= temp;
|
||||
ecc_a = ctx->ecc_f_table[ecc_a];
|
||||
}
|
||||
|
||||
eccA = ctx->eccBTable[ctx->eccFTable[eccA] ^ eccB];
|
||||
ecc[major + eccOffset] = eccA;
|
||||
ecc[major + majorCount + eccOffset] = (eccA ^ eccB);
|
||||
ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
|
||||
ecc[major + ecc_offset] = ecc_a;
|
||||
ecc[major + major_count + ecc_offset] = (ecc_a ^ ecc_b);
|
||||
}
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_write()");
|
||||
@@ -347,18 +348,19 @@ void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *da
|
||||
* @param address Pointer to the address field.
|
||||
* @param data Pointer to the data field.
|
||||
* @param ecc Pointer to the ECC field to write.
|
||||
* @param addressOffset Offset for the address field.
|
||||
* @param dataOffset Offset for the data field.
|
||||
* @param eccOffset Offset for the ECC field.
|
||||
* @param address_offset Offset for the address field.
|
||||
* @param data_offset Offset for the data field.
|
||||
* @param ecc_offset Offset for the ECC field.
|
||||
*/
|
||||
void aaruf_ecc_cd_write_sector(void *context, const uint8_t *address, const uint8_t *data, uint8_t *ecc,
|
||||
int32_t addressOffset, int32_t dataOffset, int32_t eccOffset)
|
||||
int32_t address_offset, int32_t data_offset, int32_t ecc_offset)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_write_sector(%p, %p, %p, %p, %d, %d, %d)", context, address, data, ecc, addressOffset,
|
||||
dataOffset, eccOffset);
|
||||
TRACE("Entering aaruf_ecc_cd_write_sector(%p, %p, %p, %p, %d, %d, %d)", context, address, data, ecc, address_offset,
|
||||
data_offset, ecc_offset);
|
||||
|
||||
aaruf_ecc_cd_write(context, address, data, 86, 24, 2, 86, ecc, addressOffset, dataOffset, eccOffset); // P
|
||||
aaruf_ecc_cd_write(context, address, data, 52, 43, 86, 88, ecc, addressOffset, dataOffset, eccOffset + 0xAC); // Q
|
||||
aaruf_ecc_cd_write(context, address, data, 86, 24, 2, 86, ecc, address_offset, data_offset, ecc_offset); // P
|
||||
aaruf_ecc_cd_write(context, address, data, 52, 43, 86, 88, ecc, address_offset, data_offset,
|
||||
ecc_offset + 0xAC); // Q
|
||||
|
||||
TRACE("Exiting aaruf_ecc_cd_write_sector()");
|
||||
}
|
||||
@@ -460,7 +462,7 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type)
|
||||
{
|
||||
TRACE("Entering aaruf_ecc_cd_reconstruct(%p, %p, %u)", context, sector, type);
|
||||
|
||||
uint32_t computedEdc;
|
||||
uint32_t computed_edc;
|
||||
uint8_t zeroaddress[4];
|
||||
|
||||
CdEccContext *ctx;
|
||||
@@ -473,7 +475,7 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type)
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_ecc_cd_reconstruct() without initialized context");
|
||||
return;
|
||||
@@ -485,16 +487,16 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type)
|
||||
// Compute EDC
|
||||
//
|
||||
case CdMode1:
|
||||
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0);
|
||||
memcpy(sector + 0x810, &computedEdc, 4);
|
||||
computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0);
|
||||
memcpy(sector + 0x810, &computed_edc, 4);
|
||||
break;
|
||||
case CdMode2Form1:
|
||||
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x808, 0x10);
|
||||
memcpy(sector + 0x818, &computedEdc, 4);
|
||||
computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x808, 0x10);
|
||||
memcpy(sector + 0x818, &computed_edc, 4);
|
||||
break;
|
||||
case CdMode2Form2:
|
||||
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x91C, 0x10);
|
||||
memcpy(sector + 0x92C, &computedEdc, 4);
|
||||
computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x91C, 0x10);
|
||||
memcpy(sector + 0x92C, &computed_edc, 4);
|
||||
break;
|
||||
default:
|
||||
TRACE("Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type);
|
||||
@@ -560,13 +562,13 @@ uint32_t aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, i
|
||||
|
||||
ctx = (CdEccContext *)context;
|
||||
|
||||
if(!ctx->initedEdc)
|
||||
if(!ctx->inited_edc)
|
||||
{
|
||||
TRACE("Exiting aaruf_edc_cd_compute() without initialized context");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edcTable[(edc ^ src[pos++]) & 0xFF];
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edc_table[(edc ^ src[pos++]) & 0xFF];
|
||||
|
||||
TRACE("Exiting aaruf_edc_cd_compute() = 0x%08X", edc);
|
||||
return edc;
|
||||
|
||||
244
src/close.c
244
src/close.c
@@ -42,8 +42,8 @@ int aaruf_close(void *context)
|
||||
TRACE("Entering aaruf_close(%p)", context);
|
||||
|
||||
int i = 0;
|
||||
mediaTagEntry *mediaTag = NULL;
|
||||
mediaTagEntry *tmpMediaTag = NULL;
|
||||
mediaTagEntry *media_tag = NULL;
|
||||
mediaTagEntry *tmp_media_tag = NULL;
|
||||
|
||||
if(context == NULL)
|
||||
{
|
||||
@@ -90,95 +90,97 @@ int aaruf_close(void *context)
|
||||
|
||||
// Write cached secondary table to file end and update primary table entry with its position
|
||||
// Check if we have a cached table that needs to be written (either it has an offset or exists in memory)
|
||||
bool hasCachedSecondaryDdt = (ctx->userDataDdtHeader.tableShift > 0) &&
|
||||
bool has_cached_secondary_ddt = (ctx->userDataDdtHeader.tableShift > 0) &&
|
||||
((ctx->cachedDdtOffset != 0) ||
|
||||
(ctx->cachedSecondaryDdtSmall != NULL || ctx->cachedSecondaryDdtBig != NULL));
|
||||
|
||||
if(hasCachedSecondaryDdt)
|
||||
if(has_cached_secondary_ddt)
|
||||
{
|
||||
TRACE("Writing cached secondary DDT table to file");
|
||||
|
||||
fseek(ctx->imageStream, 0, SEEK_END);
|
||||
long endOfFile = ftell(ctx->imageStream);
|
||||
long end_of_file = ftell(ctx->imageStream);
|
||||
|
||||
// Align the position according to block alignment shift
|
||||
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
if(endOfFile & alignmentMask)
|
||||
uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
if(end_of_file & alignment_mask)
|
||||
{
|
||||
// Calculate the next aligned position
|
||||
uint64_t alignedPosition = (endOfFile + alignmentMask) & ~alignmentMask;
|
||||
uint64_t aligned_position = (end_of_file + alignment_mask) & ~alignment_mask;
|
||||
|
||||
// Seek to the aligned position and pad with zeros if necessary
|
||||
fseek(ctx->imageStream, alignedPosition, SEEK_SET);
|
||||
endOfFile = alignedPosition;
|
||||
fseek(ctx->imageStream, aligned_position, SEEK_SET);
|
||||
end_of_file = aligned_position;
|
||||
|
||||
TRACE("Aligned DDT write position from %ld to %" PRIu64 " (alignment shift: %d)",
|
||||
ftell(ctx->imageStream) - (alignedPosition - endOfFile), alignedPosition,
|
||||
ftell(ctx->imageStream) - (aligned_position - end_of_file), aligned_position,
|
||||
ctx->userDataDdtHeader.blockAlignmentShift);
|
||||
}
|
||||
|
||||
// Prepare DDT header for the cached table
|
||||
DdtHeader2 ddtHeader = {0};
|
||||
ddtHeader.identifier = DeDuplicationTable2;
|
||||
ddtHeader.type = UserData;
|
||||
ddtHeader.compression = None;
|
||||
ddtHeader.levels = ctx->userDataDdtHeader.levels;
|
||||
ddtHeader.tableLevel = ctx->userDataDdtHeader.tableLevel + 1;
|
||||
ddtHeader.previousLevelOffset = ctx->primaryDdtOffset;
|
||||
ddtHeader.negative = ctx->userDataDdtHeader.negative;
|
||||
ddtHeader.overflow = ctx->userDataDdtHeader.overflow;
|
||||
ddtHeader.blockAlignmentShift = ctx->userDataDdtHeader.blockAlignmentShift;
|
||||
ddtHeader.dataShift = ctx->userDataDdtHeader.dataShift;
|
||||
ddtHeader.tableShift = 0; // Secondary tables are single level
|
||||
ddtHeader.sizeType = ctx->userDataDdtHeader.sizeType;
|
||||
DdtHeader2 ddt_header;
|
||||
memset(&ddt_header, 0, sizeof(DdtHeader2));
|
||||
ddt_header.identifier = DeDuplicationTable2;
|
||||
ddt_header.type = UserData;
|
||||
ddt_header.compression = None;
|
||||
ddt_header.levels = ctx->userDataDdtHeader.levels;
|
||||
ddt_header.tableLevel = ctx->userDataDdtHeader.tableLevel + 1;
|
||||
ddt_header.previousLevelOffset = ctx->primaryDdtOffset;
|
||||
ddt_header.negative = ctx->userDataDdtHeader.negative;
|
||||
ddt_header.overflow = ctx->userDataDdtHeader.overflow;
|
||||
ddt_header.blockAlignmentShift = ctx->userDataDdtHeader.blockAlignmentShift;
|
||||
ddt_header.dataShift = ctx->userDataDdtHeader.dataShift;
|
||||
ddt_header.tableShift = 0; // Secondary tables are single level
|
||||
ddt_header.sizeType = ctx->userDataDdtHeader.sizeType;
|
||||
|
||||
uint64_t itemsPerDdtEntry = 1 << ctx->userDataDdtHeader.tableShift;
|
||||
ddtHeader.blocks = itemsPerDdtEntry;
|
||||
ddtHeader.entries = itemsPerDdtEntry;
|
||||
ddtHeader.start = ctx->cachedDdtPosition * itemsPerDdtEntry;
|
||||
uint64_t items_per_ddt_entry = 1 << ctx->userDataDdtHeader.tableShift;
|
||||
ddt_header.blocks = items_per_ddt_entry;
|
||||
ddt_header.entries = items_per_ddt_entry;
|
||||
ddt_header.start = ctx->cachedDdtPosition * items_per_ddt_entry;
|
||||
|
||||
// Calculate data size
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
ddtHeader.length = itemsPerDdtEntry * sizeof(uint16_t);
|
||||
ddt_header.length = items_per_ddt_entry * sizeof(uint16_t);
|
||||
else
|
||||
ddtHeader.length = itemsPerDdtEntry * sizeof(uint32_t);
|
||||
ddt_header.length = items_per_ddt_entry * sizeof(uint32_t);
|
||||
|
||||
ddtHeader.cmpLength = ddtHeader.length;
|
||||
ddt_header.cmpLength = ddt_header.length;
|
||||
|
||||
// Calculate CRC64 of the data
|
||||
crc64_ctx *crc64_context = aaruf_crc64_init();
|
||||
if(crc64_context != NULL)
|
||||
{
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->cachedSecondaryDdtSmall, ddtHeader.length);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->cachedSecondaryDdtSmall, ddt_header.length);
|
||||
else
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->cachedSecondaryDdtBig, ddtHeader.length);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->cachedSecondaryDdtBig, ddt_header.length);
|
||||
|
||||
uint64_t crc64;
|
||||
aaruf_crc64_final(crc64_context, &crc64);
|
||||
ddtHeader.crc64 = crc64;
|
||||
ddtHeader.cmpCrc64 = crc64;
|
||||
ddt_header.crc64 = crc64;
|
||||
ddt_header.cmpCrc64 = crc64;
|
||||
}
|
||||
|
||||
// Write header
|
||||
if(fwrite(&ddtHeader, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
|
||||
if(fwrite(&ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
|
||||
{
|
||||
// Write data
|
||||
size_t writtenBytes = 0;
|
||||
size_t written_bytes = 0;
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
writtenBytes = fwrite(ctx->cachedSecondaryDdtSmall, ddtHeader.length, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->cachedSecondaryDdtSmall, ddt_header.length, 1, ctx->imageStream);
|
||||
else
|
||||
writtenBytes = fwrite(ctx->cachedSecondaryDdtBig, ddtHeader.length, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->cachedSecondaryDdtBig, ddt_header.length, 1, ctx->imageStream);
|
||||
|
||||
if(writtenBytes == 1)
|
||||
if(written_bytes == 1)
|
||||
{
|
||||
// Update primary table entry to point to new location
|
||||
uint64_t newSecondaryTableBlockOffset = endOfFile >> ctx->userDataDdtHeader.blockAlignmentShift;
|
||||
uint64_t new_secondary_table_block_offset =
|
||||
end_of_file >> ctx->userDataDdtHeader.blockAlignmentShift;
|
||||
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
ctx->userDataDdtMini[ctx->cachedDdtPosition] = (uint16_t)newSecondaryTableBlockOffset;
|
||||
ctx->userDataDdtMini[ctx->cachedDdtPosition] = (uint16_t)new_secondary_table_block_offset;
|
||||
else
|
||||
ctx->userDataDdtBig[ctx->cachedDdtPosition] = (uint32_t)newSecondaryTableBlockOffset;
|
||||
ctx->userDataDdtBig[ctx->cachedDdtPosition] = (uint32_t)new_secondary_table_block_offset;
|
||||
|
||||
// Update index: remove old entry for cached DDT and add new one
|
||||
TRACE("Updating index for cached secondary DDT");
|
||||
@@ -204,35 +206,35 @@ int aaruf_close(void *context)
|
||||
}
|
||||
|
||||
// Add new index entry for the newly written secondary DDT
|
||||
IndexEntry newDdtEntry;
|
||||
newDdtEntry.blockType = DeDuplicationTable2;
|
||||
newDdtEntry.dataType = UserData;
|
||||
newDdtEntry.offset = endOfFile;
|
||||
IndexEntry new_ddt_entry;
|
||||
new_ddt_entry.blockType = DeDuplicationTable2;
|
||||
new_ddt_entry.dataType = UserData;
|
||||
new_ddt_entry.offset = end_of_file;
|
||||
|
||||
utarray_push_back(ctx->indexEntries, &newDdtEntry);
|
||||
TRACE("Added new DDT index entry at offset %" PRIu64, endOfFile);
|
||||
utarray_push_back(ctx->indexEntries, &new_ddt_entry);
|
||||
TRACE("Added new DDT index entry at offset %" PRIu64, end_of_file);
|
||||
|
||||
// Write the updated primary table back to its original position in the file
|
||||
long savedPos = ftell(ctx->imageStream);
|
||||
long saved_pos = ftell(ctx->imageStream);
|
||||
fseek(ctx->imageStream, ctx->primaryDdtOffset + sizeof(DdtHeader2), SEEK_SET);
|
||||
|
||||
size_t primaryTableSize = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
size_t primary_table_size = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
? ctx->userDataDdtHeader.entries * sizeof(uint16_t)
|
||||
: ctx->userDataDdtHeader.entries * sizeof(uint32_t);
|
||||
|
||||
size_t primaryWrittenBytes = 0;
|
||||
size_t primary_written_bytes = 0;
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
primaryWrittenBytes = fwrite(ctx->userDataDdtMini, primaryTableSize, 1, ctx->imageStream);
|
||||
primary_written_bytes = fwrite(ctx->userDataDdtMini, primary_table_size, 1, ctx->imageStream);
|
||||
else
|
||||
primaryWrittenBytes = fwrite(ctx->userDataDdtBig, primaryTableSize, 1, ctx->imageStream);
|
||||
primary_written_bytes = fwrite(ctx->userDataDdtBig, primary_table_size, 1, ctx->imageStream);
|
||||
|
||||
if(primaryWrittenBytes != 1)
|
||||
if(primary_written_bytes != 1)
|
||||
{
|
||||
TRACE("Could not flush primary DDT table to file.");
|
||||
return AARUF_ERROR_CANNOT_WRITE_HEADER;
|
||||
}
|
||||
|
||||
fseek(ctx->imageStream, savedPos, SEEK_SET);
|
||||
fseek(ctx->imageStream, saved_pos, SEEK_SET);
|
||||
}
|
||||
else
|
||||
TRACE("Failed to write cached secondary DDT data");
|
||||
@@ -266,14 +268,14 @@ int aaruf_close(void *context)
|
||||
crc64_ctx *crc64_context = aaruf_crc64_init();
|
||||
if(crc64_context != NULL)
|
||||
{
|
||||
size_t primaryTableSize = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
size_t primary_table_size = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
? ctx->userDataDdtHeader.entries * sizeof(uint16_t)
|
||||
: ctx->userDataDdtHeader.entries * sizeof(uint32_t);
|
||||
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtMini, primaryTableSize);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtMini, primary_table_size);
|
||||
else
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtBig, primaryTableSize);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtBig, primary_table_size);
|
||||
|
||||
uint64_t crc64;
|
||||
aaruf_crc64_final(crc64_context, &crc64);
|
||||
@@ -286,8 +288,8 @@ int aaruf_close(void *context)
|
||||
// dataShift, tableShift, sizeType, entries, blocks, start are already set during creation
|
||||
ctx->userDataDdtHeader.crc64 = crc64;
|
||||
ctx->userDataDdtHeader.cmpCrc64 = crc64;
|
||||
ctx->userDataDdtHeader.length = primaryTableSize;
|
||||
ctx->userDataDdtHeader.cmpLength = primaryTableSize;
|
||||
ctx->userDataDdtHeader.length = primary_table_size;
|
||||
ctx->userDataDdtHeader.cmpLength = primary_table_size;
|
||||
|
||||
TRACE("Calculated CRC64 for primary DDT: 0x%16lX", crc64);
|
||||
}
|
||||
@@ -303,30 +305,30 @@ int aaruf_close(void *context)
|
||||
}
|
||||
|
||||
// Then write the table data (position is already after the header)
|
||||
size_t primaryTableSize = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
size_t primary_table_size = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
? ctx->userDataDdtHeader.entries * sizeof(uint16_t)
|
||||
: ctx->userDataDdtHeader.entries * sizeof(uint32_t);
|
||||
|
||||
// Write the primary table data
|
||||
size_t writtenBytes = 0;
|
||||
size_t written_bytes = 0;
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
writtenBytes = fwrite(ctx->userDataDdtMini, primaryTableSize, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->userDataDdtMini, primary_table_size, 1, ctx->imageStream);
|
||||
else
|
||||
writtenBytes = fwrite(ctx->userDataDdtBig, primaryTableSize, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->userDataDdtBig, primary_table_size, 1, ctx->imageStream);
|
||||
|
||||
if(writtenBytes == 1)
|
||||
if(written_bytes == 1)
|
||||
{
|
||||
TRACE("Successfully wrote primary DDT header and table to file (%" PRIu64 " entries, %zu bytes)",
|
||||
ctx->userDataDdtHeader.entries, primaryTableSize);
|
||||
ctx->userDataDdtHeader.entries, primary_table_size);
|
||||
|
||||
// Add primary DDT to index
|
||||
TRACE("Adding primary DDT to index");
|
||||
IndexEntry primaryDdtEntry;
|
||||
primaryDdtEntry.blockType = DeDuplicationTable2;
|
||||
primaryDdtEntry.dataType = UserData;
|
||||
primaryDdtEntry.offset = ctx->primaryDdtOffset;
|
||||
IndexEntry primary_ddt_entry;
|
||||
primary_ddt_entry.blockType = DeDuplicationTable2;
|
||||
primary_ddt_entry.dataType = UserData;
|
||||
primary_ddt_entry.offset = ctx->primaryDdtOffset;
|
||||
|
||||
utarray_push_back(ctx->indexEntries, &primaryDdtEntry);
|
||||
utarray_push_back(ctx->indexEntries, &primary_ddt_entry);
|
||||
TRACE("Added primary DDT index entry at offset %" PRIu64, ctx->primaryDdtOffset);
|
||||
}
|
||||
else
|
||||
@@ -342,14 +344,14 @@ int aaruf_close(void *context)
|
||||
crc64_ctx *crc64_context = aaruf_crc64_init();
|
||||
if(crc64_context != NULL)
|
||||
{
|
||||
size_t primaryTableSize = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
size_t primary_table_size = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
? ctx->userDataDdtHeader.entries * sizeof(uint16_t)
|
||||
: ctx->userDataDdtHeader.entries * sizeof(uint32_t);
|
||||
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtMini, primaryTableSize);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtMini, primary_table_size);
|
||||
else
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtBig, primaryTableSize);
|
||||
aaruf_crc64_update(crc64_context, (uint8_t *)ctx->userDataDdtBig, primary_table_size);
|
||||
|
||||
uint64_t crc64;
|
||||
aaruf_crc64_final(crc64_context, &crc64);
|
||||
@@ -365,8 +367,8 @@ int aaruf_close(void *context)
|
||||
// blockAlignmentShift, dataShift, tableShift, sizeType, entries, blocks, start are already set
|
||||
ctx->userDataDdtHeader.crc64 = crc64;
|
||||
ctx->userDataDdtHeader.cmpCrc64 = crc64;
|
||||
ctx->userDataDdtHeader.length = primaryTableSize;
|
||||
ctx->userDataDdtHeader.cmpLength = primaryTableSize;
|
||||
ctx->userDataDdtHeader.length = primary_table_size;
|
||||
ctx->userDataDdtHeader.cmpLength = primary_table_size;
|
||||
|
||||
TRACE("Calculated CRC64 for single-level DDT: 0x%16lX", crc64);
|
||||
}
|
||||
@@ -382,30 +384,30 @@ int aaruf_close(void *context)
|
||||
}
|
||||
|
||||
// Then write the table data (position is already after the header)
|
||||
size_t primaryTableSize = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
size_t primary_table_size = ctx->userDataDdtHeader.sizeType == SmallDdtSizeType
|
||||
? ctx->userDataDdtHeader.entries * sizeof(uint16_t)
|
||||
: ctx->userDataDdtHeader.entries * sizeof(uint32_t);
|
||||
|
||||
// Write the primary table data
|
||||
size_t writtenBytes = 0;
|
||||
size_t written_bytes = 0;
|
||||
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
|
||||
writtenBytes = fwrite(ctx->userDataDdtMini, primaryTableSize, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->userDataDdtMini, primary_table_size, 1, ctx->imageStream);
|
||||
else
|
||||
writtenBytes = fwrite(ctx->userDataDdtBig, primaryTableSize, 1, ctx->imageStream);
|
||||
written_bytes = fwrite(ctx->userDataDdtBig, primary_table_size, 1, ctx->imageStream);
|
||||
|
||||
if(writtenBytes == 1)
|
||||
if(written_bytes == 1)
|
||||
{
|
||||
TRACE("Successfully wrote single-level DDT header and table to file (%" PRIu64 " entries, %zu bytes)",
|
||||
ctx->userDataDdtHeader.entries, primaryTableSize);
|
||||
ctx->userDataDdtHeader.entries, primary_table_size);
|
||||
|
||||
// Add single-level DDT to index
|
||||
TRACE("Adding single-level DDT to index");
|
||||
IndexEntry singleDdtEntry;
|
||||
singleDdtEntry.blockType = DeDuplicationTable2;
|
||||
singleDdtEntry.dataType = UserData;
|
||||
singleDdtEntry.offset = ctx->primaryDdtOffset;
|
||||
IndexEntry single_ddt_entry;
|
||||
single_ddt_entry.blockType = DeDuplicationTable2;
|
||||
single_ddt_entry.dataType = UserData;
|
||||
single_ddt_entry.offset = ctx->primaryDdtOffset;
|
||||
|
||||
utarray_push_back(ctx->indexEntries, &singleDdtEntry);
|
||||
utarray_push_back(ctx->indexEntries, &single_ddt_entry);
|
||||
TRACE("Added single-level DDT index entry at offset %" PRIu64, ctx->primaryDdtOffset);
|
||||
}
|
||||
else
|
||||
@@ -415,46 +417,46 @@ int aaruf_close(void *context)
|
||||
// Write the complete index at the end of the file
|
||||
TRACE("Writing index at the end of the file");
|
||||
fseek(ctx->imageStream, 0, SEEK_END);
|
||||
long indexPosition = ftell(ctx->imageStream);
|
||||
long index_position = ftell(ctx->imageStream);
|
||||
|
||||
// Align index position to block boundary if needed
|
||||
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
if(indexPosition & alignmentMask)
|
||||
uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
if(index_position & alignment_mask)
|
||||
{
|
||||
uint64_t alignedPosition = (indexPosition + alignmentMask) & ~alignmentMask;
|
||||
fseek(ctx->imageStream, alignedPosition, SEEK_SET);
|
||||
indexPosition = alignedPosition;
|
||||
TRACE("Aligned index position to %" PRIu64, alignedPosition);
|
||||
uint64_t aligned_position = (index_position + alignment_mask) & ~alignment_mask;
|
||||
fseek(ctx->imageStream, aligned_position, SEEK_SET);
|
||||
index_position = aligned_position;
|
||||
TRACE("Aligned index position to %" PRIu64, aligned_position);
|
||||
}
|
||||
|
||||
// Prepare index header
|
||||
IndexHeader3 indexHeader;
|
||||
indexHeader.identifier = IndexBlock3;
|
||||
indexHeader.entries = utarray_len(ctx->indexEntries);
|
||||
indexHeader.previous = 0; // No previous index for now
|
||||
IndexHeader3 index_header;
|
||||
index_header.identifier = IndexBlock3;
|
||||
index_header.entries = utarray_len(ctx->indexEntries);
|
||||
index_header.previous = 0; // No previous index for now
|
||||
|
||||
TRACE("Writing index with %" PRIu64 " entries at position %ld", indexHeader.entries, indexPosition);
|
||||
TRACE("Writing index with %" PRIu64 " entries at position %ld", index_header.entries, index_position);
|
||||
|
||||
// Calculate CRC64 of index entries
|
||||
crc64_ctx *indexCrc64Context = aaruf_crc64_init();
|
||||
if(indexCrc64Context != NULL && indexHeader.entries > 0)
|
||||
crc64_ctx *index_crc64_context = aaruf_crc64_init();
|
||||
if(index_crc64_context != NULL && index_header.entries > 0)
|
||||
{
|
||||
size_t indexDataSize = indexHeader.entries * sizeof(IndexEntry);
|
||||
aaruf_crc64_update(indexCrc64Context, (uint8_t *)utarray_front(ctx->indexEntries), indexDataSize);
|
||||
aaruf_crc64_final(indexCrc64Context, &indexHeader.crc64);
|
||||
TRACE("Calculated index CRC64: 0x%16lX", indexHeader.crc64);
|
||||
size_t index_data_size = index_header.entries * sizeof(IndexEntry);
|
||||
aaruf_crc64_update(index_crc64_context, (uint8_t *)utarray_front(ctx->indexEntries), index_data_size);
|
||||
aaruf_crc64_final(index_crc64_context, &index_header.crc64);
|
||||
TRACE("Calculated index CRC64: 0x%16lX", index_header.crc64);
|
||||
}
|
||||
else { indexHeader.crc64 = 0; }
|
||||
else { index_header.crc64 = 0; }
|
||||
|
||||
// Write index header
|
||||
if(fwrite(&indexHeader, sizeof(IndexHeader3), 1, ctx->imageStream) == 1)
|
||||
if(fwrite(&index_header, sizeof(IndexHeader3), 1, ctx->imageStream) == 1)
|
||||
{
|
||||
TRACE("Successfully wrote index header");
|
||||
|
||||
// Write index entries
|
||||
if(indexHeader.entries > 0)
|
||||
if(index_header.entries > 0)
|
||||
{
|
||||
size_t entriesWritten = 0;
|
||||
size_t entries_written = 0;
|
||||
IndexEntry *entry = NULL;
|
||||
|
||||
for(entry = (IndexEntry *)utarray_front(ctx->indexEntries); entry != NULL;
|
||||
@@ -462,23 +464,23 @@ int aaruf_close(void *context)
|
||||
{
|
||||
if(fwrite(entry, sizeof(IndexEntry), 1, ctx->imageStream) == 1)
|
||||
{
|
||||
entriesWritten++;
|
||||
entries_written++;
|
||||
TRACE("Wrote index entry: blockType=0x%08X dataType=%u offset=%" PRIu64, entry->blockType,
|
||||
entry->dataType, entry->offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE("Failed to write index entry %zu", entriesWritten);
|
||||
TRACE("Failed to write index entry %zu", entries_written);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(entriesWritten == indexHeader.entries)
|
||||
if(entries_written == index_header.entries)
|
||||
{
|
||||
TRACE("Successfully wrote all %zu index entries", entriesWritten);
|
||||
TRACE("Successfully wrote all %zu index entries", entries_written);
|
||||
|
||||
// Update header with index offset and rewrite it
|
||||
ctx->header.indexOffset = indexPosition;
|
||||
ctx->header.indexOffset = index_position;
|
||||
TRACE("Updating header with index offset: %" PRIu64, ctx->header.indexOffset);
|
||||
|
||||
// Seek back to beginning and rewrite header
|
||||
@@ -495,8 +497,8 @@ int aaruf_close(void *context)
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE("Failed to write all index entries (wrote %zu of %" PRIu64 ")", entriesWritten,
|
||||
indexHeader.entries);
|
||||
TRACE("Failed to write all index entries (wrote %zu of %" PRIu64 ")", entries_written,
|
||||
index_header.entries);
|
||||
return AARUF_ERROR_CANNOT_WRITE_HEADER;
|
||||
}
|
||||
}
|
||||
@@ -537,11 +539,11 @@ int aaruf_close(void *context)
|
||||
ctx->mode2Subheaders = NULL;
|
||||
|
||||
TRACE("Freeing media tags");
|
||||
if(ctx->mediaTags != NULL) HASH_ITER(hh, ctx->mediaTags, mediaTag, tmpMediaTag)
|
||||
if(ctx->mediaTags != NULL) HASH_ITER(hh, ctx->mediaTags, media_tag, tmp_media_tag)
|
||||
{
|
||||
HASH_DEL(ctx->mediaTags, mediaTag);
|
||||
free(mediaTag->data);
|
||||
free(mediaTag);
|
||||
HASH_DEL(ctx->mediaTags, media_tag);
|
||||
free(media_tag->data);
|
||||
free(media_tag);
|
||||
}
|
||||
|
||||
#ifdef __linux__ // TODO: Implement
|
||||
|
||||
@@ -42,13 +42,13 @@ int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, siz
|
||||
uint8_t *u = NULL;
|
||||
uint8_t *v = NULL;
|
||||
uint8_t *w = NULL;
|
||||
size_t qStart = 0;
|
||||
size_t rStart = 0;
|
||||
size_t sStart = 0;
|
||||
size_t tStart = 0;
|
||||
size_t uStart = 0;
|
||||
size_t vStart = 0;
|
||||
size_t wStart = 0;
|
||||
size_t q_start = 0;
|
||||
size_t r_start = 0;
|
||||
size_t s_start = 0;
|
||||
size_t t_start = 0;
|
||||
size_t u_start = 0;
|
||||
size_t v_start = 0;
|
||||
size_t w_start = 0;
|
||||
size_t i = 0;
|
||||
|
||||
if(interleaved == NULL || sequential == NULL) return AARUF_ERROR_BUFFER_TOO_SMALL;
|
||||
@@ -150,24 +150,24 @@ int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, siz
|
||||
w[i / 8] += interleaved[i + 7] & 0x01;
|
||||
}
|
||||
|
||||
qStart = (length / 8) * 1;
|
||||
rStart = (length / 8) * 2;
|
||||
sStart = (length / 8) * 3;
|
||||
tStart = (length / 8) * 4;
|
||||
uStart = (length / 8) * 5;
|
||||
vStart = (length / 8) * 6;
|
||||
wStart = (length / 8) * 7;
|
||||
q_start = (length / 8) * 1;
|
||||
r_start = (length / 8) * 2;
|
||||
s_start = (length / 8) * 3;
|
||||
t_start = (length / 8) * 4;
|
||||
u_start = (length / 8) * 5;
|
||||
v_start = (length / 8) * 6;
|
||||
w_start = (length / 8) * 7;
|
||||
|
||||
for(i = 0; i < (length / 8); i++)
|
||||
{
|
||||
sequential[i] = p[i];
|
||||
sequential[qStart + i] = q[i];
|
||||
sequential[rStart + i] = r[i];
|
||||
sequential[sStart + i] = s[i];
|
||||
sequential[tStart + i] = t[i];
|
||||
sequential[uStart + i] = u[i];
|
||||
sequential[vStart + i] = v[i];
|
||||
sequential[wStart + i] = w[i];
|
||||
sequential[q_start + i] = q[i];
|
||||
sequential[r_start + i] = r[i];
|
||||
sequential[s_start + i] = s[i];
|
||||
sequential[t_start + i] = t[i];
|
||||
sequential[u_start + i] = u[i];
|
||||
sequential[v_start + i] = v[i];
|
||||
sequential[w_start + i] = w[i];
|
||||
}
|
||||
|
||||
free(p);
|
||||
@@ -193,13 +193,13 @@ int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, siz
|
||||
int32_t aaruf_cst_untransform(const uint8_t *sequential, uint8_t *interleaved, size_t length)
|
||||
{
|
||||
uint8_t *p, *q, *r, *s, *t, *u, *v, *w;
|
||||
size_t qStart;
|
||||
size_t rStart;
|
||||
size_t sStart;
|
||||
size_t tStart;
|
||||
size_t uStart;
|
||||
size_t vStart;
|
||||
size_t wStart;
|
||||
size_t q_start;
|
||||
size_t r_start;
|
||||
size_t s_start;
|
||||
size_t t_start;
|
||||
size_t u_start;
|
||||
size_t v_start;
|
||||
size_t w_start;
|
||||
size_t i;
|
||||
|
||||
if(interleaved == NULL || sequential == NULL) return AARUF_ERROR_BUFFER_TOO_SMALL;
|
||||
@@ -226,24 +226,24 @@ int32_t aaruf_cst_untransform(const uint8_t *sequential, uint8_t *interleaved, s
|
||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
qStart = (length / 8) * 1;
|
||||
rStart = (length / 8) * 2;
|
||||
sStart = (length / 8) * 3;
|
||||
tStart = (length / 8) * 4;
|
||||
uStart = (length / 8) * 5;
|
||||
vStart = (length / 8) * 6;
|
||||
wStart = (length / 8) * 7;
|
||||
q_start = (length / 8) * 1;
|
||||
r_start = (length / 8) * 2;
|
||||
s_start = (length / 8) * 3;
|
||||
t_start = (length / 8) * 4;
|
||||
u_start = (length / 8) * 5;
|
||||
v_start = (length / 8) * 6;
|
||||
w_start = (length / 8) * 7;
|
||||
|
||||
for(i = 0; i < (length / 8); i++)
|
||||
{
|
||||
p[i] = sequential[i];
|
||||
q[i] = sequential[qStart + i];
|
||||
r[i] = sequential[rStart + i];
|
||||
s[i] = sequential[sStart + i];
|
||||
t[i] = sequential[tStart + i];
|
||||
u[i] = sequential[uStart + i];
|
||||
v[i] = sequential[vStart + i];
|
||||
w[i] = sequential[wStart + i];
|
||||
q[i] = sequential[q_start + i];
|
||||
r[i] = sequential[r_start + i];
|
||||
s[i] = sequential[s_start + i];
|
||||
t[i] = sequential[t_start + i];
|
||||
u[i] = sequential[u_start + i];
|
||||
v[i] = sequential[v_start + i];
|
||||
w[i] = sequential[w_start + i];
|
||||
}
|
||||
|
||||
memset(interleaved, 0, length);
|
||||
|
||||
@@ -31,15 +31,15 @@
|
||||
* @param dst_buffer Pointer to the destination buffer.
|
||||
* @param dst_size Pointer to the size of the destination buffer; updated with the actual size.
|
||||
* @param src_buffer Pointer to the source (compressed) buffer.
|
||||
* @param srcLen Pointer to the size of the source buffer; updated with the actual size read.
|
||||
* @param src_len Pointer to the size of the source buffer; updated with the actual size read.
|
||||
* @param props Pointer to the LZMA properties.
|
||||
* @param propsSize Size of the LZMA properties.
|
||||
* @param props_size Size of the LZMA properties.
|
||||
* @return 0 on success, or an error code on failure.
|
||||
*/
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_lzma_decode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer,
|
||||
size_t *srcLen, const uint8_t *props, size_t propsSize)
|
||||
size_t *src_len, const uint8_t *props, size_t props_size)
|
||||
{
|
||||
return LzmaUncompress(dst_buffer, dst_size, src_buffer, srcLen, props, propsSize);
|
||||
return LzmaUncompress(dst_buffer, dst_size, src_buffer, src_len, props, props_size);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,23 +50,23 @@ AARU_EXPORT int32_t AARU_CALL aaruf_lzma_decode_buffer(uint8_t *dst_buffer, size
|
||||
* @param dst_buffer Pointer to the destination buffer.
|
||||
* @param dst_size Pointer to the size of the destination buffer; updated with the actual size.
|
||||
* @param src_buffer Pointer to the source (uncompressed) buffer.
|
||||
* @param srcLen Size of the source buffer.
|
||||
* @param outProps Pointer to the output LZMA properties.
|
||||
* @param outPropsSize Pointer to the size of the output LZMA properties.
|
||||
* @param src_len Size of the source buffer.
|
||||
* @param out_props Pointer to the output LZMA properties.
|
||||
* @param out_props_size Pointer to the size of the output LZMA properties.
|
||||
* @param level Compression level.
|
||||
* @param dictSize Dictionary size.
|
||||
* @param dict_size Dictionary size.
|
||||
* @param lc LZMA literal context bits.
|
||||
* @param lp LZMA literal position bits.
|
||||
* @param pb LZMA position bits.
|
||||
* @param fb Number of fast bytes.
|
||||
* @param numThreads Number of threads to use.
|
||||
* @param num_threads Number of threads to use.
|
||||
* @return 0 on success, or an error code on failure.
|
||||
*/
|
||||
AARU_EXPORT int32_t AARU_CALL aaruf_lzma_encode_buffer(uint8_t *dst_buffer, size_t *dst_size, const uint8_t *src_buffer,
|
||||
size_t srcLen, uint8_t *outProps, size_t *outPropsSize,
|
||||
int32_t level, uint32_t dictSize, int32_t lc, int32_t lp,
|
||||
int32_t pb, int32_t fb, int32_t numThreads)
|
||||
size_t src_len, uint8_t *out_props, size_t *out_props_size,
|
||||
int32_t level, uint32_t dict_size, int32_t lc, int32_t lp,
|
||||
int32_t pb, int32_t fb, int32_t num_threads)
|
||||
{
|
||||
return LzmaCompress(dst_buffer, dst_size, src_buffer, srcLen, outProps, outPropsSize, level, dictSize, lc, lp, pb,
|
||||
fb, numThreads);
|
||||
return LzmaCompress(dst_buffer, dst_size, src_buffer, src_len, out_props, out_props_size, level, dict_size, lc, lp,
|
||||
pb, fb, num_threads);
|
||||
}
|
||||
|
||||
@@ -68,18 +68,19 @@ static const uint8_t shuffleMasks[] = {
|
||||
0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80,
|
||||
};
|
||||
|
||||
CLMUL static void shiftRight128(__m128i in, size_t n, __m128i *outLeft, __m128i *outRight)
|
||||
CLMUL static void shiftRight128(__m128i in, size_t n, __m128i *out_left, __m128i *out_right)
|
||||
{
|
||||
const __m128i maskA = _mm_loadu_si128((const __m128i *)(shuffleMasks + (16 - n)));
|
||||
const __m128i maskB = _mm_xor_si128(maskA, _mm_cmpeq_epi8(_mm_setzero_si128(), _mm_setzero_si128()));
|
||||
const __m128i mask_a = _mm_loadu_si128((const __m128i *)(shuffleMasks + (16 - n)));
|
||||
const __m128i mask_b = _mm_xor_si128(mask_a, _mm_cmpeq_epi8(_mm_setzero_si128(), _mm_setzero_si128()));
|
||||
|
||||
*outLeft = _mm_shuffle_epi8(in, maskB);
|
||||
*outRight = _mm_shuffle_epi8(in, maskA);
|
||||
*out_left = _mm_shuffle_epi8(in, mask_b);
|
||||
*out_right = _mm_shuffle_epi8(in, mask_a);
|
||||
}
|
||||
|
||||
CLMUL static __m128i fold(__m128i in, __m128i foldConstants)
|
||||
CLMUL static __m128i fold(__m128i in, __m128i fold_constants)
|
||||
{
|
||||
return _mm_xor_si128(_mm_clmulepi64_si128(in, foldConstants, 0x00), _mm_clmulepi64_si128(in, foldConstants, 0x11));
|
||||
return _mm_xor_si128(_mm_clmulepi64_si128(in, fold_constants, 0x00),
|
||||
_mm_clmulepi64_si128(in, fold_constants, 0x11));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,122 +100,123 @@ AARU_EXPORT CLMUL uint64_t AARU_CALL aaruf_crc64_clmul(uint64_t crc, const uint8
|
||||
const uint64_t mu = 0x9c3e466c172963d5; // (bitReflect(div129by65(poly)) << 1) | 1;
|
||||
const uint64_t p = 0x92d8af2baf0e1e85; // (bitReflect(poly) << 1) | 1;
|
||||
|
||||
const __m128i foldConstants1 = _mm_set_epi64x(k2, k1);
|
||||
const __m128i foldConstants2 = _mm_set_epi64x(p, mu);
|
||||
const __m128i fold_constants_1 = _mm_set_epi64x(k2, k1);
|
||||
const __m128i fold_constants_2 = _mm_set_epi64x(p, mu);
|
||||
|
||||
const uint8_t *end = data + length;
|
||||
|
||||
// Align pointers
|
||||
const __m128i *alignedData = (const __m128i *)((uintptr_t)data & ~(uintptr_t)15);
|
||||
const __m128i *alignedEnd = (const __m128i *)(((uintptr_t)end + 15) & ~(uintptr_t)15);
|
||||
const __m128i *aligned_data = (const __m128i *)((uintptr_t)data & ~(uintptr_t)15);
|
||||
const __m128i *aligned_end = (const __m128i *)(((uintptr_t)end + 15) & ~(uintptr_t)15);
|
||||
|
||||
const size_t leadInSize = data - (const uint8_t *)alignedData;
|
||||
const size_t leadOutSize = (const uint8_t *)alignedEnd - end;
|
||||
const size_t lead_in_size = data - (const uint8_t *)aligned_data;
|
||||
const size_t lead_out_size = (const uint8_t *)aligned_end - end;
|
||||
|
||||
const size_t alignedLength = alignedEnd - alignedData;
|
||||
const size_t aligned_length = aligned_end - aligned_data;
|
||||
|
||||
const __m128i leadInMask = _mm_loadu_si128((const __m128i *)(shuffleMasks + (16 - leadInSize)));
|
||||
const __m128i data0 = _mm_blendv_epi8(_mm_setzero_si128(), _mm_load_si128(alignedData), leadInMask);
|
||||
const __m128i lead_in_mask = _mm_loadu_si128((const __m128i *)(shuffleMasks + (16 - lead_in_size)));
|
||||
const __m128i data0 = _mm_blendv_epi8(_mm_setzero_si128(), _mm_load_si128(aligned_data), lead_in_mask);
|
||||
|
||||
#if defined(_WIN64)
|
||||
const __m128i initialCrc = _mm_cvtsi64x_si128(~crc);
|
||||
const __m128i initial_crc = _mm_cvtsi64x_si128(~crc);
|
||||
#else
|
||||
const __m128i initialCrc = _mm_set_epi64x(0, ~crc);
|
||||
const __m128i initial_crc = _mm_set_epi64x(0, ~crc);
|
||||
#endif
|
||||
|
||||
__m128i R;
|
||||
if(alignedLength == 1)
|
||||
__m128i r_reg;
|
||||
if(aligned_length == 1)
|
||||
{
|
||||
// Single data block, initial CRC possibly bleeds into zero padding
|
||||
__m128i crc0, crc1;
|
||||
shiftRight128(initialCrc, 16 - length, &crc0, &crc1);
|
||||
shiftRight128(initial_crc, 16 - length, &crc0, &crc1);
|
||||
|
||||
__m128i A, B;
|
||||
shiftRight128(data0, leadOutSize, &A, &B);
|
||||
__m128i a_reg, b_reg;
|
||||
shiftRight128(data0, lead_out_size, &a_reg, &b_reg);
|
||||
|
||||
const __m128i P = _mm_xor_si128(A, crc0);
|
||||
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10),
|
||||
_mm_xor_si128(_mm_srli_si128(P, 8), _mm_slli_si128(crc1, 8)));
|
||||
const __m128i p_reg = _mm_xor_si128(a_reg, crc0);
|
||||
r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10),
|
||||
_mm_xor_si128(_mm_srli_si128(p_reg, 8), _mm_slli_si128(crc1, 8)));
|
||||
}
|
||||
else if(alignedLength == 2)
|
||||
else if(aligned_length == 2)
|
||||
{
|
||||
const __m128i data1 = _mm_load_si128(alignedData + 1);
|
||||
const __m128i data1 = _mm_load_si128(aligned_data + 1);
|
||||
|
||||
if(length < 8)
|
||||
{
|
||||
// Initial CRC bleeds into the zero padding
|
||||
__m128i crc0, crc1;
|
||||
shiftRight128(initialCrc, 16 - length, &crc0, &crc1);
|
||||
shiftRight128(initial_crc, 16 - length, &crc0, &crc1);
|
||||
|
||||
__m128i A, B, C, D;
|
||||
shiftRight128(data0, leadOutSize, &A, &B);
|
||||
shiftRight128(data1, leadOutSize, &C, &D);
|
||||
__m128i a_reg, b_reg, c_reg, d_reg;
|
||||
shiftRight128(data0, lead_out_size, &a_reg, &b_reg);
|
||||
shiftRight128(data1, lead_out_size, &c_reg, &d_reg);
|
||||
|
||||
const __m128i P = _mm_xor_si128(_mm_xor_si128(B, C), crc0);
|
||||
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10),
|
||||
_mm_xor_si128(_mm_srli_si128(P, 8), _mm_slli_si128(crc1, 8)));
|
||||
const __m128i p_reg = _mm_xor_si128(_mm_xor_si128(b_reg, c_reg), crc0);
|
||||
r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10),
|
||||
_mm_xor_si128(_mm_srli_si128(p_reg, 8), _mm_slli_si128(crc1, 8)));
|
||||
}
|
||||
else
|
||||
{
|
||||
// We can fit the initial CRC into the data without bleeding into the zero padding
|
||||
__m128i crc0, crc1;
|
||||
shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
|
||||
shiftRight128(initial_crc, lead_in_size, &crc0, &crc1);
|
||||
|
||||
__m128i A, B, C, D;
|
||||
shiftRight128(_mm_xor_si128(data0, crc0), leadOutSize, &A, &B);
|
||||
shiftRight128(_mm_xor_si128(data1, crc1), leadOutSize, &C, &D);
|
||||
__m128i a_reg, b_reg, c_reg, d_reg;
|
||||
shiftRight128(_mm_xor_si128(data0, crc0), lead_out_size, &a_reg, &b_reg);
|
||||
shiftRight128(_mm_xor_si128(data1, crc1), lead_out_size, &c_reg, &d_reg);
|
||||
|
||||
const __m128i P = _mm_xor_si128(fold(A, foldConstants1), _mm_xor_si128(B, C));
|
||||
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10), _mm_srli_si128(P, 8));
|
||||
const __m128i p_reg = _mm_xor_si128(fold(a_reg, fold_constants_1), _mm_xor_si128(b_reg, c_reg));
|
||||
r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10), _mm_srli_si128(p_reg, 8));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
alignedData++;
|
||||
length -= 16 - leadInSize;
|
||||
aligned_data++;
|
||||
length -= 16 - lead_in_size;
|
||||
|
||||
// Initial CRC can simply be added to data
|
||||
__m128i crc0, crc1;
|
||||
shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
|
||||
shiftRight128(initial_crc, lead_in_size, &crc0, &crc1);
|
||||
|
||||
__m128i accumulator = _mm_xor_si128(fold(_mm_xor_si128(crc0, data0), foldConstants1), crc1);
|
||||
__m128i accumulator = _mm_xor_si128(fold(_mm_xor_si128(crc0, data0), fold_constants_1), crc1);
|
||||
|
||||
while(length >= 32)
|
||||
{
|
||||
accumulator = fold(_mm_xor_si128(_mm_load_si128(alignedData), accumulator), foldConstants1);
|
||||
accumulator = fold(_mm_xor_si128(_mm_load_si128(aligned_data), accumulator), fold_constants_1);
|
||||
|
||||
length -= 16;
|
||||
alignedData++;
|
||||
aligned_data++;
|
||||
}
|
||||
|
||||
__m128i P;
|
||||
if(length == 16) { P = _mm_xor_si128(accumulator, _mm_load_si128(alignedData)); }
|
||||
__m128i p_reg;
|
||||
if(length == 16) { p_reg = _mm_xor_si128(accumulator, _mm_load_si128(aligned_data)); }
|
||||
else
|
||||
{
|
||||
const __m128i end0 = _mm_xor_si128(accumulator, _mm_load_si128(alignedData));
|
||||
const __m128i end1 = _mm_load_si128(alignedData + 1);
|
||||
const __m128i end0 = _mm_xor_si128(accumulator, _mm_load_si128(aligned_data));
|
||||
const __m128i end1 = _mm_load_si128(aligned_data + 1);
|
||||
|
||||
__m128i A, B, C, D;
|
||||
shiftRight128(end0, leadOutSize, &A, &B);
|
||||
shiftRight128(end1, leadOutSize, &C, &D);
|
||||
__m128i a_reg, b_reg, c_reg, d_reg;
|
||||
shiftRight128(end0, lead_out_size, &a_reg, &b_reg);
|
||||
shiftRight128(end1, lead_out_size, &c_reg, &d_reg);
|
||||
|
||||
P = _mm_xor_si128(fold(A, foldConstants1), _mm_or_si128(B, C));
|
||||
p_reg = _mm_xor_si128(fold(a_reg, fold_constants_1), _mm_or_si128(b_reg, c_reg));
|
||||
}
|
||||
|
||||
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10), _mm_srli_si128(P, 8));
|
||||
r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10), _mm_srli_si128(p_reg, 8));
|
||||
}
|
||||
|
||||
// Final Barrett reduction
|
||||
const __m128i T1 = _mm_clmulepi64_si128(R, foldConstants2, 0x00);
|
||||
const __m128i T2 =
|
||||
_mm_xor_si128(_mm_xor_si128(_mm_clmulepi64_si128(T1, foldConstants2, 0x10), _mm_slli_si128(T1, 8)), R);
|
||||
const __m128i t1_reg = _mm_clmulepi64_si128(r_reg, fold_constants_2, 0x00);
|
||||
const __m128i t2_reg = _mm_xor_si128(
|
||||
_mm_xor_si128(_mm_clmulepi64_si128(t1_reg, fold_constants_2, 0x10), _mm_slli_si128(t1_reg, 8)), r_reg);
|
||||
|
||||
TRACE("Exiting aaruf_crc64_clmul()");
|
||||
|
||||
#if defined(_WIN64)
|
||||
return ~_mm_extract_epi64(T2, 1);
|
||||
return ~_mm_extract_epi64(t2_reg, 1);
|
||||
#else
|
||||
return ~(((uint64_t)(uint32_t)_mm_extract_epi32(T2, 3) << 32) | (uint64_t)(uint32_t)_mm_extract_epi32(T2, 2));
|
||||
return ~(((uint64_t)(uint32_t)_mm_extract_epi32(t2_reg, 3) << 32) |
|
||||
(uint64_t)(uint32_t)_mm_extract_epi32(t2_reg, 2));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
62
src/create.c
62
src/create.c
@@ -32,31 +32,31 @@
|
||||
* Allocates and initializes a new aaruformat context and image file with the specified parameters.
|
||||
*
|
||||
* @param filepath Path to the image file to create.
|
||||
* @param mediaType Media type identifier.
|
||||
* @param sectorSize Size of each sector in bytes.
|
||||
* @param userSectors Number of user data sectors.
|
||||
* @param negativeSectors Number of negative sectors.
|
||||
* @param overflowSectors Number of overflow sectors.
|
||||
* @param media_type Media type identifier.
|
||||
* @param sector_size Size of each sector in bytes.
|
||||
* @param user_sectors Number of user data sectors.
|
||||
* @param negative_sectors Number of negative sectors.
|
||||
* @param overflow_sectors Number of overflow sectors.
|
||||
* @param options String with creation options.
|
||||
* @param applicationName Pointer to the application name string.
|
||||
* @param applicationNameLength Length of the application name string.
|
||||
* @param applicationMajorVersion Major version of the application.
|
||||
* @param applicationMinorVersion Minor version of the application.
|
||||
* @param application_name Pointer to the application name string.
|
||||
* @param application_name_length Length of the application name string.
|
||||
* @param application_major_version Major version of the application.
|
||||
* @param application_minor_version Minor version of the application.
|
||||
* @return Pointer to the created aaruformat context, or NULL on failure.
|
||||
*/
|
||||
void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize, uint64_t userSectors,
|
||||
uint64_t negativeSectors, uint64_t overflowSectors, const char *options,
|
||||
const uint8_t *applicationName, uint8_t applicationNameLength, uint8_t applicationMajorVersion,
|
||||
uint8_t applicationMinorVersion)
|
||||
void *aaruf_create(const char *filepath, uint32_t media_type, uint32_t sector_size, uint64_t user_sectors,
|
||||
uint64_t negative_sectors, uint64_t overflow_sectors, const char *options,
|
||||
const uint8_t *application_name, uint8_t application_name_length, uint8_t application_major_version,
|
||||
uint8_t application_minor_version)
|
||||
{
|
||||
TRACE("Entering aaruf_create(%s, %u, %u, %llu, %llu, %llu, %s, %s, %u, %u, %u)", filepath, mediaType, sectorSize,
|
||||
userSectors, negativeSectors, overflowSectors, options,
|
||||
applicationName ? (const char *)applicationName : "NULL", applicationNameLength, applicationMajorVersion,
|
||||
applicationMinorVersion);
|
||||
TRACE("Entering aaruf_create(%s, %u, %u, %llu, %llu, %llu, %s, %s, %u, %u, %u)", filepath, media_type, sector_size,
|
||||
user_sectors, negative_sectors, overflow_sectors, options,
|
||||
application_name ? (const char *)application_name : "NULL", application_name_length,
|
||||
application_major_version, application_minor_version);
|
||||
|
||||
// Parse the options
|
||||
TRACE("Parsing options");
|
||||
aaru_options parsedOptions = parse_options(options);
|
||||
aaru_options parsed_options = parse_options(options);
|
||||
|
||||
// Allocate context
|
||||
TRACE("Allocating memory for context");
|
||||
@@ -86,9 +86,9 @@ void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(applicationNameLength > AARU_HEADER_APP_NAME_LEN)
|
||||
if(application_name_length > AARU_HEADER_APP_NAME_LEN)
|
||||
{
|
||||
FATAL("Application name too long (%u bytes, maximum %u bytes)", applicationNameLength,
|
||||
FATAL("Application name too long (%u bytes, maximum %u bytes)", application_name_length,
|
||||
AARU_HEADER_APP_NAME_LEN);
|
||||
free(ctx);
|
||||
errno = AARUF_ERROR_INVALID_APP_NAME_LENGTH;
|
||||
@@ -100,12 +100,12 @@ void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize
|
||||
// Initialize header
|
||||
TRACE("Initializing header");
|
||||
ctx->header.identifier = AARU_MAGIC;
|
||||
memcpy(ctx->header.application, applicationName, applicationNameLength);
|
||||
memcpy(ctx->header.application, application_name, application_name_length);
|
||||
ctx->header.imageMajorVersion = AARUF_VERSION_V2;
|
||||
ctx->header.imageMinorVersion = 0;
|
||||
ctx->header.applicationMajorVersion = applicationMajorVersion;
|
||||
ctx->header.applicationMinorVersion = applicationMinorVersion;
|
||||
ctx->header.mediaType = mediaType;
|
||||
ctx->header.applicationMajorVersion = application_major_version;
|
||||
ctx->header.applicationMinorVersion = application_minor_version;
|
||||
ctx->header.mediaType = media_type;
|
||||
ctx->header.indexOffset = 0;
|
||||
ctx->header.creationTime = get_filetime_uint64();
|
||||
ctx->header.lastWrittenTime = get_filetime_uint64();
|
||||
@@ -143,7 +143,7 @@ void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize
|
||||
ctx->imageInfo.CreationTime = ctx->header.creationTime;
|
||||
ctx->imageInfo.LastModificationTime = ctx->header.lastWrittenTime;
|
||||
ctx->imageInfo.XmlMediaType = aaruf_get_xml_mediatype(ctx->header.mediaType);
|
||||
ctx->imageInfo.SectorSize = sectorSize;
|
||||
ctx->imageInfo.SectorSize = sector_size;
|
||||
|
||||
// Initialize caches
|
||||
TRACE("Initializing caches");
|
||||
@@ -171,13 +171,13 @@ void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize
|
||||
ctx->userDataDdtHeader.levels = 2;
|
||||
ctx->userDataDdtHeader.tableLevel = 0;
|
||||
ctx->userDataDdtHeader.previousLevelOffset = 0;
|
||||
ctx->userDataDdtHeader.negative = negativeSectors;
|
||||
ctx->userDataDdtHeader.blocks = userSectors + overflowSectors + negativeSectors;
|
||||
ctx->userDataDdtHeader.overflow = overflowSectors;
|
||||
ctx->userDataDdtHeader.negative = negative_sectors;
|
||||
ctx->userDataDdtHeader.blocks = user_sectors + overflow_sectors + negative_sectors;
|
||||
ctx->userDataDdtHeader.overflow = overflow_sectors;
|
||||
ctx->userDataDdtHeader.start = 0;
|
||||
ctx->userDataDdtHeader.blockAlignmentShift = parsedOptions.block_alignment;
|
||||
ctx->userDataDdtHeader.dataShift = parsedOptions.data_shift;
|
||||
ctx->userDataDdtHeader.tableShift = parsedOptions.table_shift;
|
||||
ctx->userDataDdtHeader.blockAlignmentShift = parsed_options.block_alignment;
|
||||
ctx->userDataDdtHeader.dataShift = parsed_options.data_shift;
|
||||
ctx->userDataDdtHeader.tableShift = parsed_options.table_shift;
|
||||
ctx->userDataDdtHeader.sizeType = 1;
|
||||
ctx->userDataDdtHeader.entries = ctx->userDataDdtHeader.blocks / (1 << ctx->userDataDdtHeader.tableShift);
|
||||
|
||||
|
||||
198
src/ddt/ddt_v1.c
198
src/ddt/ddt_v1.c
@@ -38,18 +38,18 @@
|
||||
* @param foundUserDataDdt Pointer to a boolean that will be set to true if a user data DDT was found and loaded.
|
||||
* @return AARUF_STATUS_OK on success, or an error code on failure.
|
||||
*/
|
||||
int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt)
|
||||
int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *found_user_data_ddt)
|
||||
{
|
||||
TRACE("Entering process_ddt_v1(%p, %p, %d)", ctx, entry, *foundUserDataDdt);
|
||||
TRACE("Entering process_ddt_v1(%p, %p, %d)", ctx, entry, *found_user_data_ddt);
|
||||
|
||||
int pos = 0;
|
||||
size_t readBytes = 0;
|
||||
DdtHeader ddtHeader;
|
||||
uint8_t *cmpData = NULL;
|
||||
uint32_t *cdDdt = NULL;
|
||||
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
||||
size_t lzmaSize = 0;
|
||||
int errorNo = 0;
|
||||
size_t read_bytes = 0;
|
||||
DdtHeader ddt_header;
|
||||
uint8_t *cmp_data = NULL;
|
||||
uint32_t *cd_ddt = NULL;
|
||||
uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
|
||||
size_t lzma_size = 0;
|
||||
int error_no = 0;
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
if(ctx == NULL || ctx->imageStream == NULL)
|
||||
@@ -73,9 +73,9 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse
|
||||
|
||||
// Even if those two checks shall have been done before
|
||||
TRACE("Reading DDT block header at position %" PRIu64, entry->offset);
|
||||
readBytes = fread(&ddtHeader, 1, sizeof(DdtHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ddt_header, 1, sizeof(DdtHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(DdtHeader))
|
||||
if(read_bytes != sizeof(DdtHeader))
|
||||
{
|
||||
FATAL("Could not read block header at %" PRIu64 "", entry->offset);
|
||||
|
||||
@@ -83,96 +83,96 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse
|
||||
return AARUF_ERROR_CANNOT_READ_BLOCK;
|
||||
}
|
||||
|
||||
*foundUserDataDdt = true;
|
||||
*found_user_data_ddt = true;
|
||||
|
||||
ctx->imageInfo.ImageSize += ddtHeader.cmpLength;
|
||||
ctx->imageInfo.ImageSize += ddt_header.cmpLength;
|
||||
|
||||
if(entry->dataType == UserData)
|
||||
{
|
||||
ctx->imageInfo.Sectors = ddtHeader.entries;
|
||||
ctx->shift = ddtHeader.shift;
|
||||
ctx->imageInfo.Sectors = ddt_header.entries;
|
||||
ctx->shift = ddt_header.shift;
|
||||
ctx->ddtVersion = 1;
|
||||
|
||||
// Check for DDT compression
|
||||
switch(ddtHeader.compression)
|
||||
switch(ddt_header.compression)
|
||||
{
|
||||
// TODO: Check CRC
|
||||
case Lzma:
|
||||
lzmaSize = ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
lzma_size = ddt_header.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
|
||||
cmpData = (uint8_t *)malloc(lzmaSize);
|
||||
if(cmpData == NULL)
|
||||
cmp_data = (uint8_t *)malloc(lzma_size);
|
||||
if(cmp_data == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for DDT, continuing...");
|
||||
break;
|
||||
}
|
||||
|
||||
ctx->userDataDdt = (uint64_t *)malloc(ddtHeader.length);
|
||||
ctx->userDataDdt = (uint64_t *)malloc(ddt_header.length);
|
||||
if(ctx->userDataDdt == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for DDT, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
||||
read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(read_bytes != LZMA_PROPERTIES_LENGTH)
|
||||
{
|
||||
TRACE("Could not read LZMA properties, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(ctx->userDataDdt);
|
||||
ctx->userDataDdt = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
||||
if(readBytes != lzmaSize)
|
||||
read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
|
||||
if(read_bytes != lzma_size)
|
||||
{
|
||||
TRACE("Could not read compressed block, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(ctx->userDataDdt);
|
||||
ctx->userDataDdt = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = ddtHeader.length;
|
||||
TRACE("Decompressing block of size %zu bytes", ddtHeader.length);
|
||||
errorNo = aaruf_lzma_decode_buffer((uint8_t *)ctx->userDataDdt, &readBytes, cmpData, &lzmaSize,
|
||||
lzmaProperties, LZMA_PROPERTIES_LENGTH);
|
||||
read_bytes = ddt_header.length;
|
||||
TRACE("Decompressing block of size %zu bytes", ddt_header.length);
|
||||
error_no = aaruf_lzma_decode_buffer((uint8_t *)ctx->userDataDdt, &read_bytes, cmp_data, &lzma_size,
|
||||
lzma_properties, LZMA_PROPERTIES_LENGTH);
|
||||
|
||||
if(errorNo != 0)
|
||||
if(error_no != 0)
|
||||
{
|
||||
FATAL("Got error %d from LZMA, stopping...", errorNo);
|
||||
free(cmpData);
|
||||
FATAL("Got error %d from LZMA, stopping...", error_no);
|
||||
free(cmp_data);
|
||||
free(ctx->userDataDdt);
|
||||
ctx->userDataDdt = NULL;
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(readBytes != ddtHeader.length)
|
||||
if(read_bytes != ddt_header.length)
|
||||
{
|
||||
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(ctx->userDataDdt);
|
||||
ctx->userDataDdt = NULL;
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
ctx->inMemoryDdt = true;
|
||||
*foundUserDataDdt = true;
|
||||
*found_user_data_ddt = true;
|
||||
|
||||
break;
|
||||
// TODO: Check CRC
|
||||
case None:
|
||||
#ifdef __linux__
|
||||
TRACE("Memory mapping deduplication table at position %" PRIu64, entry->offset + sizeof(ddtHeader));
|
||||
ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddtHeader.entries;
|
||||
TRACE("Memory mapping deduplication table at position %" PRIu64, entry->offset + sizeof(ddt_header));
|
||||
ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddt_header.entries;
|
||||
ctx->userDataDdt = mmap(NULL, ctx->mappedMemoryDdtSize, PROT_READ, MAP_SHARED, fileno(ctx->imageStream),
|
||||
entry->offset + sizeof(ddtHeader));
|
||||
entry->offset + sizeof(ddt_header));
|
||||
|
||||
if(ctx->userDataDdt == MAP_FAILED)
|
||||
{
|
||||
*foundUserDataDdt = false;
|
||||
*found_user_data_ddt = false;
|
||||
FATAL("Could not read map deduplication table.");
|
||||
break;
|
||||
}
|
||||
@@ -181,115 +181,115 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse
|
||||
break;
|
||||
#else // TODO: Implement
|
||||
TRACE("Uncompressed DDT not yet implemented...");
|
||||
*foundUserDataDdt = false;
|
||||
*found_user_data_ddt = false;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
TRACE("Found unknown compression type %d, continuing...", ddtHeader.compression);
|
||||
*foundUserDataDdt = false;
|
||||
TRACE("Found unknown compression type %d, continuing...", ddt_header.compression);
|
||||
*found_user_data_ddt = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(entry->dataType == CdSectorPrefixCorrected || entry->dataType == CdSectorSuffixCorrected)
|
||||
{
|
||||
switch(ddtHeader.compression)
|
||||
switch(ddt_header.compression)
|
||||
{
|
||||
// TODO: Check CRC
|
||||
case Lzma:
|
||||
lzmaSize = ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
lzma_size = ddt_header.cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
|
||||
cmpData = (uint8_t *)malloc(lzmaSize);
|
||||
if(cmpData == NULL)
|
||||
cmp_data = (uint8_t *)malloc(lzma_size);
|
||||
if(cmp_data == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for DDT, continuing...");
|
||||
break;
|
||||
}
|
||||
|
||||
cdDdt = (uint32_t *)malloc(ddtHeader.length);
|
||||
if(cdDdt == NULL)
|
||||
cd_ddt = (uint32_t *)malloc(ddt_header.length);
|
||||
if(cd_ddt == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for DDT, continuing...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
||||
read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
if(read_bytes != LZMA_PROPERTIES_LENGTH)
|
||||
{
|
||||
TRACE("Could not read LZMA properties, continuing...");
|
||||
free(cmpData);
|
||||
free(cdDdt);
|
||||
free(cmp_data);
|
||||
free(cd_ddt);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
||||
if(readBytes != lzmaSize)
|
||||
read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
|
||||
if(read_bytes != lzma_size)
|
||||
{
|
||||
TRACE("Could not read compressed block, continuing...");
|
||||
free(cmpData);
|
||||
free(cdDdt);
|
||||
free(cmp_data);
|
||||
free(cd_ddt);
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = ddtHeader.length;
|
||||
TRACE("Decompressing block of size %zu bytes", ddtHeader.length);
|
||||
errorNo = aaruf_lzma_decode_buffer((uint8_t *)cdDdt, &readBytes, cmpData, &lzmaSize, lzmaProperties,
|
||||
LZMA_PROPERTIES_LENGTH);
|
||||
read_bytes = ddt_header.length;
|
||||
TRACE("Decompressing block of size %zu bytes", ddt_header.length);
|
||||
error_no = aaruf_lzma_decode_buffer((uint8_t *)cd_ddt, &read_bytes, cmp_data, &lzma_size,
|
||||
lzma_properties, LZMA_PROPERTIES_LENGTH);
|
||||
|
||||
if(errorNo != 0)
|
||||
if(error_no != 0)
|
||||
{
|
||||
FATAL("Got error %d from LZMA, stopping...", errorNo);
|
||||
free(cmpData);
|
||||
free(cdDdt);
|
||||
FATAL("Got error %d from LZMA, stopping...", error_no);
|
||||
free(cmp_data);
|
||||
free(cd_ddt);
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(readBytes != ddtHeader.length)
|
||||
if(read_bytes != ddt_header.length)
|
||||
{
|
||||
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...");
|
||||
free(cmpData);
|
||||
free(cdDdt);
|
||||
free(cmp_data);
|
||||
free(cd_ddt);
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(entry->dataType == CdSectorPrefixCorrected)
|
||||
ctx->sectorPrefixDdt = cdDdt;
|
||||
ctx->sectorPrefixDdt = cd_ddt;
|
||||
else if(entry->dataType == CdSectorSuffixCorrected)
|
||||
ctx->sectorSuffixDdt = cdDdt;
|
||||
ctx->sectorSuffixDdt = cd_ddt;
|
||||
else
|
||||
free(cdDdt);
|
||||
free(cd_ddt);
|
||||
|
||||
break;
|
||||
|
||||
// TODO: Check CRC
|
||||
case None:
|
||||
cdDdt = (uint32_t *)malloc(ddtHeader.entries * sizeof(uint32_t));
|
||||
cd_ddt = (uint32_t *)malloc(ddt_header.entries * sizeof(uint32_t));
|
||||
|
||||
if(cdDdt == NULL)
|
||||
if(cd_ddt == NULL)
|
||||
{
|
||||
TRACE("Cannot allocate memory for deduplication table.");
|
||||
break;
|
||||
}
|
||||
|
||||
readBytes = fread(cdDdt, 1, ddtHeader.entries * sizeof(uint32_t), ctx->imageStream);
|
||||
read_bytes = fread(cd_ddt, 1, ddt_header.entries * sizeof(uint32_t), ctx->imageStream);
|
||||
|
||||
if(readBytes != ddtHeader.entries * sizeof(uint32_t))
|
||||
if(read_bytes != ddt_header.entries * sizeof(uint32_t))
|
||||
{
|
||||
free(cdDdt);
|
||||
free(cd_ddt);
|
||||
TRACE("Could not read deduplication table, continuing...");
|
||||
break;
|
||||
}
|
||||
|
||||
if(entry->dataType == CdSectorPrefixCorrected)
|
||||
ctx->sectorPrefixDdt = cdDdt;
|
||||
ctx->sectorPrefixDdt = cd_ddt;
|
||||
else if(entry->dataType == CdSectorSuffixCorrected)
|
||||
ctx->sectorSuffixDdt = cdDdt;
|
||||
ctx->sectorSuffixDdt = cd_ddt;
|
||||
else
|
||||
free(cdDdt);
|
||||
free(cd_ddt);
|
||||
|
||||
break;
|
||||
default:
|
||||
TRACE("Found unknown compression type %d, continuing...", ddtHeader.compression);
|
||||
TRACE("Found unknown compression type %d, continuing...", ddt_header.compression);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -304,38 +304,38 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse
|
||||
* Determines the offset and block offset for a sector using the DDT v1 table.
|
||||
*
|
||||
* @param ctx Pointer to the aaruformat context.
|
||||
* @param sectorAddress Logical sector address to decode.
|
||||
* @param sector_address Logical sector address to decode.
|
||||
* @param offset Pointer to store the resulting offset.
|
||||
* @param blockOffset Pointer to store the resulting block offset.
|
||||
* @param sectorStatus Pointer to store the sector status.
|
||||
* @param block_offset Pointer to store the resulting block offset.
|
||||
* @param sector_status Pointer to store the sector status.
|
||||
* @return AARUF_STATUS_OK on success, or an error code on failure.
|
||||
*/
|
||||
int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, uint64_t *blockOffset,
|
||||
uint8_t *sectorStatus)
|
||||
int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
|
||||
uint8_t *sector_status)
|
||||
{
|
||||
TRACE("Entering decode_ddt_entry_v1(%p, %" PRIu64 ", %llu, %llu, %d)", ctx, sectorAddress, *offset, *blockOffset,
|
||||
*sectorStatus);
|
||||
TRACE("Entering decode_ddt_entry_v1(%p, %" PRIu64 ", %p, %p, %p)", ctx, sector_address, offset, block_offset,
|
||||
sector_status);
|
||||
|
||||
// Check if the context and image stream are valid
|
||||
if(ctx == NULL || ctx->imageStream == NULL)
|
||||
{
|
||||
FATAL("Invalid context or image stream.");
|
||||
TRACE("Exiting decode_ddt_entry_v1() = AARUF_ERROR_NOT_AARUFORMAT");
|
||||
return AARUF_ERROR_NOT_AARUFORMAT;
|
||||
}
|
||||
|
||||
const uint64_t ddtEntry = ctx->userDataDdt[sectorAddress];
|
||||
const uint32_t offsetMask = (uint32_t)((1 << ctx->shift) - 1);
|
||||
*offset = ddtEntry & offsetMask;
|
||||
*blockOffset = ddtEntry >> ctx->shift;
|
||||
const uint64_t ddt_entry = ctx->userDataDdt[sector_address];
|
||||
const uint32_t offset_mask = (uint32_t)((1 << ctx->shift) - 1);
|
||||
*offset = ddt_entry & offset_mask;
|
||||
*block_offset = ddt_entry >> ctx->shift;
|
||||
|
||||
// Partially written image... as we can't know the real sector size just assume it's common :/
|
||||
if(ddtEntry == 0)
|
||||
*sectorStatus = SectorStatusNotDumped;
|
||||
if(ddt_entry == 0)
|
||||
*sector_status = SectorStatusNotDumped;
|
||||
else
|
||||
*sectorStatus = SectorStatusDumped;
|
||||
|
||||
TRACE("Exiting decode_ddt_entry_v1(%p, %" PRIu64 ", %llu, %llu, %d) = AARUF_STATUS_OK", ctx, sectorAddress, *offset,
|
||||
*blockOffset, *sectorStatus);
|
||||
*sector_status = SectorStatusDumped;
|
||||
|
||||
TRACE("Exiting decode_ddt_entry_v1(%p, %" PRIu64 ", %llu, %llu, %d) = AARUF_STATUS_OK", ctx, sector_address,
|
||||
*offset, *block_offset, *sector_status);
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
797
src/ddt/ddt_v2.c
797
src/ddt/ddt_v2.c
File diff suppressed because it is too large
Load Diff
@@ -49,16 +49,16 @@ int aaruf_identify(const char *filename)
|
||||
*
|
||||
* Determines if the provided stream is an AaruFormat image.
|
||||
*
|
||||
* @param imageStream Stream of the file to identify.
|
||||
* @param image_stream Stream of the file to identify.
|
||||
* @return If positive, confidence value (100 = maximum confidence, 0 = not recognized). If negative, error value.
|
||||
*/
|
||||
int aaruf_identify_stream(FILE *imageStream)
|
||||
int aaruf_identify_stream(FILE *image_stream)
|
||||
{
|
||||
fseek(imageStream, 0, SEEK_SET);
|
||||
fseek(image_stream, 0, SEEK_SET);
|
||||
|
||||
AaruHeader header;
|
||||
|
||||
size_t ret = fread(&header, sizeof(AaruHeader), 1, imageStream);
|
||||
size_t ret = fread(&header, sizeof(AaruHeader), 1, image_stream);
|
||||
|
||||
if(ret != 1) return 0;
|
||||
|
||||
|
||||
14
src/lru.c
14
src/lru.c
@@ -67,12 +67,12 @@ void add_to_cache(struct CacheHeader *cache, const char *key, void *value)
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE char *int64_to_string(uint64_t number)
|
||||
FORCE_INLINE char *uint64_to_string(uint64_t number)
|
||||
{
|
||||
char *charKey = malloc(17); // 16 hex digits + null terminator
|
||||
if(!charKey) return NULL;
|
||||
snprintf(charKey, 17, "%016" PRIX64, number);
|
||||
return charKey;
|
||||
char *char_key = malloc(17); // 16 hex digits + null terminator
|
||||
if(!char_key) return NULL;
|
||||
snprintf(char_key, 17, "%016" PRIX64, number);
|
||||
return char_key;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +86,7 @@ FORCE_INLINE char *int64_to_string(uint64_t number)
|
||||
*/
|
||||
void *find_in_cache_uint64(struct CacheHeader *cache, uint64_t key)
|
||||
{
|
||||
return find_in_cache(cache, int64_to_string(key));
|
||||
return find_in_cache(cache, uint64_to_string(key));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,5 +100,5 @@ void *find_in_cache_uint64(struct CacheHeader *cache, uint64_t key)
|
||||
*/
|
||||
void add_to_cache_uint64(struct CacheHeader *cache, uint64_t key, void *value)
|
||||
{
|
||||
return add_to_cache(cache, int64_to_string(key), value);
|
||||
return add_to_cache(cache, uint64_to_string(key), value);
|
||||
}
|
||||
42
src/open.c
42
src/open.c
@@ -39,8 +39,8 @@
|
||||
void *aaruf_open(const char *filepath)
|
||||
{
|
||||
aaruformatContext *ctx = NULL;
|
||||
int errorNo = 0;
|
||||
size_t readBytes = 0;
|
||||
int error_no = 0;
|
||||
size_t read_bytes = 0;
|
||||
long pos = 0;
|
||||
int i = 0;
|
||||
uint32_t signature = 0;
|
||||
@@ -75,9 +75,9 @@ void *aaruf_open(const char *filepath)
|
||||
if(ctx->imageStream == NULL)
|
||||
{
|
||||
FATAL("Error %d opening file %s for reading", errno, filepath);
|
||||
errorNo = errno;
|
||||
error_no = errno;
|
||||
free(ctx);
|
||||
errno = errorNo;
|
||||
errno = error_no;
|
||||
|
||||
TRACE("Exiting aaruf_open() = NULL");
|
||||
return NULL;
|
||||
@@ -85,9 +85,9 @@ void *aaruf_open(const char *filepath)
|
||||
|
||||
TRACE("Reading header at position 0");
|
||||
fseek(ctx->imageStream, 0, SEEK_SET);
|
||||
readBytes = fread(&ctx->header, 1, sizeof(AaruHeader), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->header, 1, sizeof(AaruHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(AaruHeader))
|
||||
if(read_bytes != sizeof(AaruHeader))
|
||||
{
|
||||
FATAL("Could not read header");
|
||||
free(ctx);
|
||||
@@ -112,9 +112,9 @@ void *aaruf_open(const char *filepath)
|
||||
{
|
||||
TRACE("Reading new header version at position 0");
|
||||
fseek(ctx->imageStream, 0, SEEK_SET);
|
||||
readBytes = fread(&ctx->header, 1, sizeof(AaruHeaderV2), ctx->imageStream);
|
||||
read_bytes = fread(&ctx->header, 1, sizeof(AaruHeaderV2), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(AaruHeaderV2))
|
||||
if(read_bytes != sizeof(AaruHeaderV2))
|
||||
{
|
||||
free(ctx);
|
||||
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
||||
@@ -188,9 +188,9 @@ void *aaruf_open(const char *filepath)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
readBytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
|
||||
read_bytes = fread(&signature, 1, sizeof(uint32_t), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(uint32_t) ||
|
||||
if(read_bytes != sizeof(uint32_t) ||
|
||||
(signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3))
|
||||
{
|
||||
FATAL("Could not read index header or incorrect identifier %4.4s", (char *)&signature);
|
||||
@@ -228,7 +228,7 @@ void *aaruf_open(const char *filepath)
|
||||
entry->dataType, entry->offset);
|
||||
}
|
||||
|
||||
bool foundUserDataDdt = false;
|
||||
bool found_user_data_ddt = false;
|
||||
ctx->imageInfo.ImageSize = 0;
|
||||
for(i = 0; i < utarray_len(index_entries); i++)
|
||||
{
|
||||
@@ -247,13 +247,13 @@ void *aaruf_open(const char *filepath)
|
||||
switch(entry->blockType)
|
||||
{
|
||||
case DataBlock:
|
||||
errorNo = process_data_block(ctx, entry);
|
||||
error_no = process_data_block(ctx, entry);
|
||||
|
||||
if(errorNo != AARUF_STATUS_OK)
|
||||
if(error_no != AARUF_STATUS_OK)
|
||||
{
|
||||
utarray_free(index_entries);
|
||||
free(ctx);
|
||||
errno = errorNo;
|
||||
errno = error_no;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -261,26 +261,26 @@ void *aaruf_open(const char *filepath)
|
||||
break;
|
||||
|
||||
case DeDuplicationTable:
|
||||
errorNo = process_ddt_v1(ctx, entry, &foundUserDataDdt);
|
||||
error_no = process_ddt_v1(ctx, entry, &found_user_data_ddt);
|
||||
|
||||
if(errorNo != AARUF_STATUS_OK)
|
||||
if(error_no != AARUF_STATUS_OK)
|
||||
{
|
||||
utarray_free(index_entries);
|
||||
free(ctx);
|
||||
errno = errorNo;
|
||||
errno = error_no;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
case DeDuplicationTable2:
|
||||
errorNo = process_ddt_v2(ctx, entry, &foundUserDataDdt);
|
||||
error_no = process_ddt_v2(ctx, entry, &found_user_data_ddt);
|
||||
|
||||
if(errorNo != AARUF_STATUS_OK)
|
||||
if(error_no != AARUF_STATUS_OK)
|
||||
{
|
||||
utarray_free(index_entries);
|
||||
free(ctx);
|
||||
errno = errorNo;
|
||||
errno = error_no;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -320,7 +320,7 @@ void *aaruf_open(const char *filepath)
|
||||
|
||||
utarray_free(index_entries);
|
||||
|
||||
if(!foundUserDataDdt)
|
||||
if(!found_user_data_ddt)
|
||||
{
|
||||
FATAL("Could not find user data deduplication table, aborting...");
|
||||
aaruf_close(ctx);
|
||||
|
||||
157
src/read.c
157
src/read.c
@@ -88,21 +88,21 @@ int32_t aaruf_read_media_tag(void *context, uint8_t *data, int32_t tag, uint32_t
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data, uint32_t *length)
|
||||
int32_t aaruf_read_sector(void *context, uint64_t sector_address, uint8_t *data, uint32_t *length)
|
||||
{
|
||||
TRACE("Entering aaruf_read_sector(%p, %" PRIu64 ", %p, %u)", context, sectorAddress, data, *length);
|
||||
TRACE("Entering aaruf_read_sector(%p, %" PRIu64 ", %p, %u)", context, sector_address, data, *length);
|
||||
|
||||
aaruformatContext *ctx = NULL;
|
||||
uint64_t offset = 0;
|
||||
uint64_t blockOffset = 0;
|
||||
BlockHeader *blockHeader = NULL;
|
||||
uint64_t block_offset = 0;
|
||||
BlockHeader *block_header = NULL;
|
||||
uint8_t *block = NULL;
|
||||
size_t readBytes = 0;
|
||||
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH];
|
||||
size_t lzmaSize = 0;
|
||||
uint8_t *cmpData = NULL;
|
||||
int errorNo = 0;
|
||||
uint8_t sectorStatus = 0;
|
||||
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;
|
||||
|
||||
if(context == NULL)
|
||||
{
|
||||
@@ -123,7 +123,7 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
return AARUF_ERROR_NOT_AARUFORMAT;
|
||||
}
|
||||
|
||||
if(sectorAddress > ctx->imageInfo.Sectors - 1)
|
||||
if(sector_address > ctx->imageInfo.Sectors - 1)
|
||||
{
|
||||
FATAL("Sector address out of bounds");
|
||||
|
||||
@@ -132,20 +132,20 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
}
|
||||
|
||||
if(ctx->ddtVersion == 1)
|
||||
errorNo = decode_ddt_entry_v1(ctx, sectorAddress, &offset, &blockOffset, §orStatus);
|
||||
error_no = decode_ddt_entry_v1(ctx, sector_address, &offset, &block_offset, §or_status);
|
||||
else if(ctx->ddtVersion == 2)
|
||||
errorNo = decode_ddt_entry_v2(ctx, sectorAddress, &offset, &blockOffset, §orStatus);
|
||||
error_no = decode_ddt_entry_v2(ctx, sector_address, &offset, &block_offset, §or_status);
|
||||
|
||||
if(errorNo != AARUF_STATUS_OK)
|
||||
if(error_no != AARUF_STATUS_OK)
|
||||
{
|
||||
FATAL("Error %d decoding DDT entry", errorNo);
|
||||
FATAL("Error %d decoding DDT entry", error_no);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = %d", errorNo);
|
||||
return errorNo;
|
||||
TRACE("Exiting aaruf_read_sector() = %d", error_no);
|
||||
return error_no;
|
||||
}
|
||||
|
||||
// Partially written image... as we can't know the real sector size just assume it's common :/
|
||||
if(sectorStatus == SectorStatusNotDumped)
|
||||
if(sector_status == SectorStatusNotDumped)
|
||||
{
|
||||
*length = ctx->imageInfo.SectorSize;
|
||||
|
||||
@@ -155,14 +155,14 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
|
||||
// Check if block header is cached
|
||||
TRACE("Checking if block header is cached");
|
||||
blockHeader = find_in_cache_uint64(&ctx->blockHeaderCache, blockOffset);
|
||||
block_header = find_in_cache_uint64(&ctx->blockHeaderCache, block_offset);
|
||||
|
||||
// Read block header
|
||||
if(blockHeader == NULL)
|
||||
if(block_header == NULL)
|
||||
{
|
||||
TRACE("Allocating memory for block header");
|
||||
blockHeader = malloc(sizeof(BlockHeader));
|
||||
if(blockHeader == NULL)
|
||||
block_header = malloc(sizeof(BlockHeader));
|
||||
if(block_header == NULL)
|
||||
{
|
||||
FATAL("Not enough memory for block header");
|
||||
|
||||
@@ -171,10 +171,10 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
}
|
||||
|
||||
TRACE("Reading block header");
|
||||
fseek(ctx->imageStream, blockOffset, SEEK_SET);
|
||||
readBytes = fread(blockHeader, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||
fseek(ctx->imageStream, block_offset, SEEK_SET);
|
||||
read_bytes = fread(block_header, 1, sizeof(BlockHeader), ctx->imageStream);
|
||||
|
||||
if(readBytes != sizeof(BlockHeader))
|
||||
if(read_bytes != sizeof(BlockHeader))
|
||||
{
|
||||
FATAL("Error reading block header");
|
||||
|
||||
@@ -183,15 +183,15 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
}
|
||||
|
||||
TRACE("Adding block header to cache");
|
||||
add_to_cache_uint64(&ctx->blockHeaderCache, blockOffset, blockHeader);
|
||||
add_to_cache_uint64(&ctx->blockHeaderCache, block_offset, block_header);
|
||||
}
|
||||
else
|
||||
fseek(ctx->imageStream, blockOffset + sizeof(BlockHeader), SEEK_SET); // Advance as if reading the header
|
||||
fseek(ctx->imageStream, block_offset + sizeof(BlockHeader), SEEK_SET); // Advance as if reading the header
|
||||
|
||||
if(data == NULL || *length < blockHeader->sectorSize)
|
||||
if(data == NULL || *length < block_header->sectorSize)
|
||||
{
|
||||
TRACE("Buffer too small for sector, required %u bytes", blockHeader->sectorSize);
|
||||
*length = blockHeader->sectorSize;
|
||||
TRACE("Buffer too small for sector, required %u bytes", block_header->sectorSize);
|
||||
*length = block_header->sectorSize;
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_BUFFER_TOO_SMALL");
|
||||
return AARUF_ERROR_BUFFER_TOO_SMALL;
|
||||
@@ -199,24 +199,24 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
|
||||
// Check if block is cached
|
||||
TRACE("Checking if block is cached");
|
||||
block = find_in_cache_uint64(&ctx->blockCache, blockOffset);
|
||||
block = find_in_cache_uint64(&ctx->blockCache, block_offset);
|
||||
|
||||
if(block != NULL)
|
||||
{
|
||||
TRACE("Getting data from cache");
|
||||
memcpy(data, block + offset * blockHeader->sectorSize, blockHeader->sectorSize);
|
||||
*length = blockHeader->sectorSize;
|
||||
memcpy(data, block + offset * block_header->sectorSize, block_header->sectorSize);
|
||||
*length = block_header->sectorSize;
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
}
|
||||
|
||||
// Decompress block
|
||||
switch(blockHeader->compression)
|
||||
switch(block_header->compression)
|
||||
{
|
||||
case None:
|
||||
TRACE("Allocating memory for block");
|
||||
block = (uint8_t *)malloc(blockHeader->length);
|
||||
block = (uint8_t *)malloc(block_header->length);
|
||||
if(block == NULL)
|
||||
{
|
||||
FATAL("Not enough memory for block");
|
||||
@@ -226,9 +226,9 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
}
|
||||
|
||||
TRACE("Reading block into memory");
|
||||
readBytes = fread(block, 1, blockHeader->length, ctx->imageStream);
|
||||
read_bytes = fread(block, 1, block_header->length, ctx->imageStream);
|
||||
|
||||
if(readBytes != blockHeader->length)
|
||||
if(read_bytes != block_header->length)
|
||||
{
|
||||
FATAL("Could not read block");
|
||||
free(block);
|
||||
@@ -239,11 +239,11 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
|
||||
break;
|
||||
case Lzma:
|
||||
lzmaSize = blockHeader->cmpLength - LZMA_PROPERTIES_LENGTH;
|
||||
TRACE("Allocating memory for compressed data of size %zu bytes", lzmaSize);
|
||||
cmpData = malloc(lzmaSize);
|
||||
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);
|
||||
|
||||
if(cmpData == NULL)
|
||||
if(cmp_data == NULL)
|
||||
{
|
||||
FATAL("Cannot allocate memory for block...");
|
||||
|
||||
@@ -251,73 +251,73 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
TRACE("Allocating memory for block of size %zu bytes", blockHeader->length);
|
||||
block = malloc(blockHeader->length);
|
||||
TRACE("Allocating memory for block of size %zu bytes", block_header->length);
|
||||
block = malloc(block_header->length);
|
||||
if(block == NULL)
|
||||
{
|
||||
FATAL("Cannot allocate memory for block...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
readBytes = fread(lzmaProperties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
read_bytes = fread(lzma_properties, 1, LZMA_PROPERTIES_LENGTH, ctx->imageStream);
|
||||
|
||||
if(readBytes != LZMA_PROPERTIES_LENGTH)
|
||||
if(read_bytes != LZMA_PROPERTIES_LENGTH)
|
||||
{
|
||||
FATAL("Could not read LZMA properties...");
|
||||
free(block);
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream);
|
||||
if(readBytes != lzmaSize)
|
||||
read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
|
||||
if(read_bytes != lzma_size)
|
||||
{
|
||||
FATAL("Could not read compressed block...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(block);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
TRACE("Decompressing block of size %zu bytes", blockHeader->length);
|
||||
readBytes = blockHeader->length;
|
||||
errorNo =
|
||||
aaruf_lzma_decode_buffer(block, &readBytes, cmpData, &lzmaSize, lzmaProperties, LZMA_PROPERTIES_LENGTH);
|
||||
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);
|
||||
|
||||
if(errorNo != 0)
|
||||
if(error_no != 0)
|
||||
{
|
||||
FATAL("Got error %d from LZMA...", errorNo);
|
||||
free(cmpData);
|
||||
FATAL("Got error %d from LZMA...", error_no);
|
||||
free(cmp_data);
|
||||
free(block);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
if(readBytes != blockHeader->length)
|
||||
if(read_bytes != block_header->length)
|
||||
{
|
||||
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(block);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
break;
|
||||
case Flac:
|
||||
TRACE("Allocating memory for compressed data of size %zu bytes", blockHeader->cmpLength);
|
||||
cmpData = malloc(blockHeader->cmpLength);
|
||||
TRACE("Allocating memory for compressed data of size %zu bytes", block_header->cmpLength);
|
||||
cmp_data = malloc(block_header->cmpLength);
|
||||
|
||||
if(cmpData == NULL)
|
||||
if(cmp_data == NULL)
|
||||
{
|
||||
FATAL("Cannot allocate memory for block...");
|
||||
|
||||
@@ -325,57 +325,58 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
|
||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
TRACE("Allocating memory for block of size %zu bytes", blockHeader->length);
|
||||
block = malloc(blockHeader->length);
|
||||
TRACE("Allocating memory for block of size %zu bytes", block_header->length);
|
||||
block = malloc(block_header->length);
|
||||
if(block == NULL)
|
||||
{
|
||||
FATAL("Cannot allocate memory for block...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
|
||||
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||
}
|
||||
|
||||
TRACE("Reading compressed data into memory");
|
||||
readBytes = fread(cmpData, 1, blockHeader->cmpLength, ctx->imageStream);
|
||||
if(readBytes != blockHeader->cmpLength)
|
||||
read_bytes = fread(cmp_data, 1, block_header->cmpLength, ctx->imageStream);
|
||||
if(read_bytes != block_header->cmpLength)
|
||||
{
|
||||
FATAL("Could not read compressed block...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(block);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
TRACE("Decompressing block of size %zu bytes", blockHeader->length);
|
||||
readBytes = aaruf_flac_decode_redbook_buffer(block, blockHeader->length, cmpData, blockHeader->cmpLength);
|
||||
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);
|
||||
|
||||
if(readBytes != blockHeader->length)
|
||||
if(read_bytes != block_header->length)
|
||||
{
|
||||
FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
free(block);
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
|
||||
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
|
||||
}
|
||||
|
||||
free(cmpData);
|
||||
free(cmp_data);
|
||||
|
||||
break;
|
||||
default:
|
||||
FATAL("Unsupported compression %d", blockHeader->compression);
|
||||
FATAL("Unsupported compression %d", block_header->compression);
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_UNSUPPORTED_COMPRESSION");
|
||||
return AARUF_ERROR_UNSUPPORTED_COMPRESSION;
|
||||
}
|
||||
|
||||
// Add block to cache
|
||||
TRACE("Adding block to cache");
|
||||
add_to_cache_uint64(&ctx->blockCache, blockOffset, block);
|
||||
add_to_cache_uint64(&ctx->blockCache, block_offset, block);
|
||||
|
||||
memcpy(data, block + (offset * blockHeader->sectorSize), blockHeader->sectorSize);
|
||||
*length = blockHeader->sectorSize;
|
||||
memcpy(data, block + (offset * block_header->sectorSize), block_header->sectorSize);
|
||||
*length = block_header->sectorSize;
|
||||
|
||||
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
|
||||
return AARUF_STATUS_OK;
|
||||
|
||||
@@ -37,7 +37,8 @@ uint64_t get_filetime_uint64()
|
||||
/**
|
||||
* @brief Gets the current time as a 64-bit FILETIME value.
|
||||
*
|
||||
* Returns the current system time as a 64-bit value compatible with Windows FILETIME (number of 100-nanosecond intervals since January 1, 1601 UTC).
|
||||
* Returns the current system time as a 64-bit value compatible with Windows FILETIME (number of 100-nanosecond
|
||||
* intervals since January 1, 1601 UTC).
|
||||
*
|
||||
* @return The current time as a 64-bit FILETIME value.
|
||||
*/
|
||||
@@ -46,9 +47,8 @@ uint64_t get_filetime_uint64()
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL); // seconds + microseconds since 1970
|
||||
|
||||
const uint64_t EPOCH_DIFF = 11644473600ULL; // seconds between 1601 and 1970
|
||||
uint64_t ft = (tv.tv_sec + EPOCH_DIFF) * 10000000ULL + tv.tv_usec * 10;
|
||||
const uint64_t epoch_diff = 11644473600ULL; // seconds between 1601 and 1970
|
||||
uint64_t ft = (tv.tv_sec + epoch_diff) * 10000000ULL + tv.tv_usec * 10;
|
||||
return ft;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
34
src/write.c
34
src/write.c
@@ -32,16 +32,16 @@
|
||||
* Writes the given data to the specified sector address in the image, with the given status and length.
|
||||
*
|
||||
* @param context Pointer to the aaruformat context.
|
||||
* @param sectorAddress Logical sector address to write.
|
||||
* @param sector_address Logical sector address to write.
|
||||
* @param data Pointer to the data buffer to write.
|
||||
* @param sectorStatus Status of the sector to write.
|
||||
* @param sector_status Status of the sector to write.
|
||||
* @param length Length of the data buffer.
|
||||
* @return AARUF_STATUS_OK on success, or an error code on failure.
|
||||
*/
|
||||
int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t *data, uint8_t sectorStatus,
|
||||
int32_t aaruf_write_sector(void *context, uint64_t sector_address, const uint8_t *data, uint8_t sector_status,
|
||||
uint32_t length)
|
||||
{
|
||||
TRACE("Entering aaruf_write_sector(%p, %" PRIu64 ", %p, %u, %u)", context, sectorAddress, data, sectorStatus,
|
||||
TRACE("Entering aaruf_write_sector(%p, %" PRIu64 ", %p, %u, %u)", context, sector_address, data, sector_status,
|
||||
length);
|
||||
|
||||
// Check context is correct AaruFormat context
|
||||
@@ -98,7 +98,7 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t
|
||||
}
|
||||
}
|
||||
|
||||
set_ddt_entry_v2(ctx, sectorAddress, ctx->currentBlockOffset, ctx->nextBlockPosition, sectorStatus);
|
||||
set_ddt_entry_v2(ctx, sector_address, ctx->currentBlockOffset, ctx->nextBlockPosition, sector_status);
|
||||
|
||||
// No block set
|
||||
if(ctx->writingBufferPosition == 0)
|
||||
@@ -110,11 +110,11 @@ int32_t aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t
|
||||
ctx->currentBlockHeader.sectorSize = length;
|
||||
|
||||
// TODO: Optical discs
|
||||
uint32_t maxBufferSize = (1 << ctx->userDataDdtHeader.dataShift) * ctx->currentBlockHeader.sectorSize;
|
||||
TRACE("Setting max buffer size to %u bytes", maxBufferSize);
|
||||
uint32_t max_buffer_size = (1 << ctx->userDataDdtHeader.dataShift) * ctx->currentBlockHeader.sectorSize;
|
||||
TRACE("Setting max buffer size to %u bytes", max_buffer_size);
|
||||
|
||||
TRACE("Allocating memory for writing buffer");
|
||||
ctx->writingBuffer = (uint8_t *)calloc(1, maxBufferSize);
|
||||
ctx->writingBuffer = (uint8_t *)calloc(1, max_buffer_size);
|
||||
if(ctx->writingBuffer == NULL)
|
||||
{
|
||||
FATAL("Could not allocate memory");
|
||||
@@ -160,13 +160,13 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx)
|
||||
|
||||
// Add to index
|
||||
TRACE("Adding block to index");
|
||||
IndexEntry indexEntry;
|
||||
indexEntry.blockType = DataBlock;
|
||||
indexEntry.dataType = UserData;
|
||||
indexEntry.offset = ctx->nextBlockPosition;
|
||||
IndexEntry index_entry;
|
||||
index_entry.blockType = DataBlock;
|
||||
index_entry.dataType = UserData;
|
||||
index_entry.offset = ctx->nextBlockPosition;
|
||||
|
||||
utarray_push_back(ctx->indexEntries, &indexEntry);
|
||||
TRACE("Block added to index at offset %" PRIu64, indexEntry.offset);
|
||||
utarray_push_back(ctx->indexEntries, &index_entry);
|
||||
TRACE("Block added to index at offset %" PRIu64, index_entry.offset);
|
||||
|
||||
// Write block header to file
|
||||
|
||||
@@ -182,9 +182,9 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx)
|
||||
return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA;
|
||||
|
||||
// Update nextBlockPosition to point to the next available aligned position
|
||||
uint64_t blockTotalSize = sizeof(BlockHeader) + ctx->currentBlockHeader.cmpLength;
|
||||
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
ctx->nextBlockPosition = (ctx->nextBlockPosition + blockTotalSize + alignmentMask) & ~alignmentMask;
|
||||
uint64_t block_total_size = sizeof(BlockHeader) + ctx->currentBlockHeader.cmpLength;
|
||||
uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
|
||||
ctx->nextBlockPosition = (ctx->nextBlockPosition + block_total_size + alignment_mask) & ~alignment_mask;
|
||||
TRACE("Updated nextBlockPosition to %" PRIu64, ctx->nextBlockPosition);
|
||||
|
||||
// Clear values
|
||||
|
||||
@@ -83,7 +83,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
|
||||
edc = 0;
|
||||
size = 0x810;
|
||||
pos = 0;
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edcTable[(edc ^ sector[pos++]) & 0xFF];
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edc_table[(edc ^ sector[pos++]) & 0xFF];
|
||||
calculatedEdc = edc;
|
||||
|
||||
*edc_correct = calculatedEdc == storedEdc;
|
||||
@@ -128,7 +128,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
|
||||
edc = 0;
|
||||
size = 0x808;
|
||||
pos = 0x10;
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edcTable[(edc ^ sector[pos++]) & 0xFF];
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edc_table[(edc ^ sector[pos++]) & 0xFF];
|
||||
calculatedEdc = edc;
|
||||
|
||||
*edc_correct = calculatedEdc == storedEdc;
|
||||
@@ -151,7 +151,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
|
||||
edc = 0;
|
||||
size = 0x808;
|
||||
pos = 0x10;
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edcTable[(edc ^ sector[pos++]) & 0xFF];
|
||||
for(; size > 0; size--) edc = (edc >> 8) ^ context->edc_table[(edc ^ sector[pos++]) & 0xFF];
|
||||
calculatedEdc = edc;
|
||||
|
||||
*edc_correct = calculatedEdc == storedEdc;
|
||||
|
||||
Reference in New Issue
Block a user