Refactor variable names for consistency and readability across multiple files

This commit is contained in:
2025-09-30 15:11:27 +01:00
parent 352850a698
commit dda0ee89e8
25 changed files with 1271 additions and 1252 deletions

View File

@@ -67,7 +67,7 @@ CheckOptions:
- key: readability-identifier-naming.LocalVariableCase - key: readability-identifier-naming.LocalVariableCase
value: 'lower_case' value: 'lower_case'
- key: readability-identifier-naming.StructCase - key: readability-identifier-naming.StructCase
value: 'lower_case' value: 'CamelCase'
- key: readability-identifier-naming.UnionCase - key: readability-identifier-naming.UnionCase
value: 'lower_case' value: 'lower_case'
- key: readability-identifier-naming.EnumCase - key: readability-identifier-naming.EnumCase

View File

@@ -31,6 +31,7 @@
/** /**
* Contains an enumeration of all known types of media. * Contains an enumeration of all known types of media.
*/ */
// NOLINTBEGIN(readability-identifier-naming)
typedef enum typedef enum
{ {
// Generics, types 0 to 9 // Generics, types 0 to 9
@@ -995,6 +996,8 @@ typedef enum
// //
} MediaType; } MediaType;
// NOLINTEND(readability-identifier-naming)
/** /**
* Contains information about a dump image and its contents * Contains information about a dump image and its contents
*/ */
@@ -1090,6 +1093,7 @@ typedef enum
/* /*
* Metadata present for each media. * Metadata present for each media.
*/ */
// NOLINTBEGIN(readability-identifier-naming)
typedef enum typedef enum
{ {
/* CD table of contents */ /* CD table of contents */
@@ -1167,6 +1171,8 @@ typedef enum
CD_LeadIn = 68 CD_LeadIn = 68
} MediaTagType; } MediaTagType;
// NOLINTEND(readability-identifier-naming)
#endif // LIBAARUFORMAT_AARU_H #endif // LIBAARUFORMAT_AARU_H
#ifndef _MSC_VER #ifndef _MSC_VER

View File

@@ -45,10 +45,10 @@ typedef struct Crc64Context
typedef struct CdEccContext typedef struct CdEccContext
{ {
bool initedEdc; bool inited_edc;
uint8_t *eccBTable; uint8_t *ecc_b_table;
uint8_t *eccFTable; uint8_t *ecc_f_table;
uint32_t *edcTable; uint32_t *edc_table;
} CdEccContext; } CdEccContext;
typedef struct Checksums typedef struct Checksums

View File

@@ -58,15 +58,15 @@
AARU_EXPORT int AARU_CALL aaruf_identify(const char *filename); 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_open(const char *filepath);
AARU_EXPORT void *AARU_CALL aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize, AARU_EXPORT void *AARU_CALL aaruf_create(const char *filepath, uint32_t media_type, uint32_t sector_size,
uint64_t userSectors, uint64_t negativeSectors, uint64_t overflowSectors, uint64_t user_sectors, uint64_t negative_sectors, uint64_t overflow_sectors,
const char *options, const uint8_t *applicationName, const char *options, const uint8_t *application_name,
uint8_t applicationNameLength, uint8_t applicationMajorVersion, uint8_t application_name_length, uint8_t application_major_version,
uint8_t applicationMinorVersion); uint8_t application_minor_version);
AARU_EXPORT int AARU_CALL aaruf_close(void *context); 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 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 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(void *context, uint64_t sector_address, uint8_t *data,
AARU_EXPORT int32_t AARU_CALL aaruf_read_sector_long(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 sector_address, uint8_t *data,
uint32_t *length); uint32_t *length);
AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sectorAddress, const uint8_t *data, AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sector_address, const uint8_t *data,
uint8_t sectorStatus, uint32_t length); uint8_t sector_status, uint32_t length);
AARU_EXPORT int32_t AARU_CALL aaruf_verify_image(void *context); 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_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, 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 major_count, uint32_t minor_count, uint32_t major_mult,
uint32_t minorInc, const uint8_t *ecc, int32_t addressOffset, uint32_t minor_inc, const uint8_t *ecc, int32_t address_offset,
int32_t dataOffset, int32_t eccOffset); 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, 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 major_count, uint32_t minor_count, uint32_t major_mult,
uint32_t minorInc, uint8_t *ecc, int32_t addressOffset, uint32_t minor_inc, uint8_t *ecc, int32_t address_offset,
int32_t dataOffset, int32_t eccOffset); 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, 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, uint8_t *ecc, int32_t address_offset, int32_t data_offset,
int32_t eccOffset); 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); 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 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); uint32_t *length, uint8_t track);
AARU_LOCAL int32_t AARU_CALL aaruf_get_media_tag_type_for_datatype(int32_t type); 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); 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, 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, 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, size_t src_size, uint8_t *out_props, size_t *out_props_size,
int32_t level, uint32_t dictSize, int32_t lc, int32_t lp, int32_t level, uint32_t dict_size, int32_t lc, int32_t lp,
int32_t pb, int32_t fb, int32_t numThreads); int32_t pb, int32_t fb, int32_t num_threads);
#if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \ #if defined(__x86_64__) || defined(__amd64) || defined(_M_AMD64) || defined(_M_X64) || defined(__I386__) || \
defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86) defined(__i386__) || defined(__THW_INTEL) || defined(_M_IX86)

View File

@@ -28,8 +28,8 @@ int32_t verify_index_v2(aaruformatContext *ctx);
UT_array *process_index_v3(aaruformatContext *ctx); UT_array *process_index_v3(aaruformatContext *ctx);
int32_t verify_index_v3(aaruformatContext *ctx); int32_t verify_index_v3(aaruformatContext *ctx);
int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry); int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry);
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);
int32_t process_ddt_v2(aaruformatContext *ctx, IndexEntry *entry, bool *foundUserDataDdt); 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_metadata_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry); void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_tracks_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_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry);
void process_checksum_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); 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, int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
uint8_t *sectorStatus); uint8_t *sector_status);
int32_t decode_ddt_entry_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, uint64_t *blockOffset, int32_t decode_ddt_entry_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
uint8_t *sectorStatus); uint8_t *sector_status);
int32_t decode_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, int32_t decode_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset,
uint64_t *blockOffset, uint8_t *sectorStatus); uint64_t *block_offset, uint8_t *sector_status);
int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t *offset, int32_t decode_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset,
uint64_t *blockOffset, uint8_t *sectorStatus); uint64_t *block_offset, uint8_t *sector_status);
void set_ddt_entry_v2(aaruformatContext *ctx, uint64_t sectorAddress, uint64_t offset, uint64_t blockOffset, void set_ddt_entry_v2(aaruformatContext *ctx, uint64_t sector_address, uint64_t offset, uint64_t block_offset,
uint8_t sectorStatus); uint8_t sector_status);
void set_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool negative, uint64_t offset, void set_ddt_single_level_v2(aaruformatContext *ctx, uint64_t sector_address, bool negative, uint64_t offset,
uint64_t blockOffset, uint8_t sectorStatus); uint64_t block_offset, uint8_t sector_status);
void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sectorAddress, bool negative, uint64_t offset, void set_ddt_multi_level_v2(aaruformatContext *ctx, uint64_t sector_address, bool negative, uint64_t offset,
uint64_t blockOffset, uint8_t sectorStatus); uint64_t block_offset, uint8_t sector_status);
aaru_options parse_options(const char *options); aaru_options parse_options(const char *options);
uint64_t get_filetime_uint64(); uint64_t get_filetime_uint64();
int32_t aaruf_close_current_block(aaruformatContext *ctx); int32_t aaruf_close_current_block(aaruformatContext *ctx);

View File

@@ -36,12 +36,12 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
TRACE("Entering process_checksum_block(%p, %p)", ctx, entry); TRACE("Entering process_checksum_block(%p, %p)", ctx, entry);
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
ChecksumHeader checksum_header; ChecksumHeader checksum_header;
ChecksumEntry const *checksum_entry = NULL; ChecksumEntry const *checksum_entry = NULL;
uint8_t *data = NULL; uint8_t *data = NULL;
int j = 0; int j = 0;
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) if(ctx == NULL || ctx->imageStream == NULL)
@@ -61,9 +61,9 @@ void process_checksum_block(aaruformatContext *ctx, const IndexEntry *entry)
// Even if those two checks shall have been done before // Even if those two checks shall have been done before
TRACE("Reading checksum block header at position %" PRIu64, entry->offset); 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)); memset(&checksum_header, 0, sizeof(ChecksumHeader));
FATAL("Could not read checksums block header, continuing...\n"); 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)); 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)); memset(&checksum_header, 0, sizeof(ChecksumHeader));
free(data); free(data);

View File

@@ -37,17 +37,17 @@
int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry) int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
{ {
TRACE("Entering process_data_block(%p, %p)", ctx, entry); TRACE("Entering process_data_block(%p, %p)", ctx, entry);
BlockHeader blockHeader; BlockHeader block_header;
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
size_t lzmaSize = 0; size_t lzma_size = 0;
uint8_t *cmpData = NULL; uint8_t *cmp_data = NULL;
uint8_t *cstData = NULL; uint8_t *cst_data = NULL;
mediaTagEntry *oldMediaTag = NULL; mediaTagEntry *old_media_tag = NULL;
mediaTagEntry *mediaTag = NULL; mediaTagEntry *media_tag = NULL;
uint8_t *data = NULL; uint8_t *data = NULL;
int errorNo = 0; int error_no = 0;
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH]; uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
uint64_t crc64 = 0; uint64_t crc64 = 0;
// Check if the context and image stream are valid // 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; if(entry->dataType == NoData) return AARUF_STATUS_OK;
TRACE("Reading block header at position %" PRIu64, entry->offset); 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); 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; return AARUF_STATUS_OK;
} }
ctx->imageInfo.ImageSize += blockHeader.cmpLength; ctx->imageInfo.ImageSize += block_header.cmpLength;
// Unused, skip // Unused, skip
if(entry->dataType == UserData) 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); TRACE("Setting sector size to %" PRIu64 " bytes", block_header.sectorSize);
ctx->imageInfo.SectorSize = blockHeader.sectorSize; ctx->imageInfo.SectorSize = block_header.sectorSize;
} }
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return 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); 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; 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", 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"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return 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); 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, TRACE("Invalid compression type %d for block with data type %d, continuing...", block_header.compression,
blockHeader.type); block_header.type);
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return 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); cmp_data = (uint8_t *)malloc(lzma_size);
if(cmpData == NULL) if(cmp_data == NULL)
{ {
TRACE("Cannot allocate memory for block, continuing..."); TRACE("Cannot allocate memory for block, continuing...");
@@ -140,86 +140,86 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
} }
data = (uint8_t *)malloc(blockHeader.length); data = (uint8_t *)malloc(block_header.length);
if(data == NULL) if(data == NULL)
{ {
TRACE("Cannot allocate memory for block, continuing..."); TRACE("Cannot allocate memory for block, continuing...");
free(cmpData); free(cmp_data);
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
} }
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)
{ {
TRACE("Could not read LZMA properties, continuing..."); TRACE("Could not read LZMA properties, continuing...");
free(cmpData); free(cmp_data);
free(data); free(data);
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
} }
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
if(readBytes != lzmaSize) if(read_bytes != lzma_size)
{ {
TRACE("Could not read compressed block, continuing..."); TRACE("Could not read compressed block, continuing...");
free(cmpData); free(cmp_data);
free(data); free(data);
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
} }
readBytes = blockHeader.length; read_bytes = block_header.length;
errorNo = error_no =
aaruf_lzma_decode_buffer(data, &readBytes, cmpData, &lzmaSize, lzmaProperties, LZMA_PROPERTIES_LENGTH); 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); TRACE("Got error %d from LZMA, continuing...", error_no);
free(cmpData); free(cmp_data);
free(data); free(data);
TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return 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..."); TRACE("Error decompressing block, should be {0} bytes but got {1} bytes., continuing...");
free(cmpData); free(cmp_data);
free(data); free(data);
TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting process_data_block() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
if(blockHeader.compression == LzmaClauniaSubchannelTransform) if(block_header.compression == LzmaClauniaSubchannelTransform)
{ {
cstData = malloc(blockHeader.length); cst_data = malloc(block_header.length);
if(cstData == NULL) if(cst_data == NULL)
{ {
TRACE("Cannot allocate memory for block, continuing..."); TRACE("Cannot allocate memory for block, continuing...");
free(cmpData); free(cmp_data);
free(data); free(data);
TRACE("Exiting process_data_block() = AARUF_STATUS_OK"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return 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); free(data);
data = cstData; data = cst_data;
cstData = NULL; 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) if(data == NULL)
{ {
fprintf(stderr, "Cannot allocate memory for block, continuing..."); 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; 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); free(data);
fprintf(stderr, "Could not read block, continuing..."); fprintf(stderr, "Could not read block, continuing...");
@@ -241,23 +241,23 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
} }
else 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"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return 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 // Due to how C# wrote it, it is effectively reversed
if(ctx->header.imageMajorVersion <= AARUF_VERSION_V1) crc64 = bswap_64(crc64); 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, 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"); TRACE("Exiting process_data_block() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
@@ -304,27 +304,27 @@ int32_t process_data_block(aaruformatContext *ctx, IndexEntry *entry)
ctx->mode2Subheaders = data; ctx->mode2Subheaders = data;
break; break;
default: 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."); TRACE("Cannot allocate memory for media tag entry.");
break; break;
} }
memset(mediaTag, 0, sizeof(mediaTagEntry)); memset(media_tag, 0, sizeof(mediaTagEntry));
mediaTag->type = aaruf_get_media_tag_type_for_datatype(blockHeader.type); media_tag->type = aaruf_get_media_tag_type_for_datatype(block_header.type);
mediaTag->data = data; media_tag->data = data;
mediaTag->length = blockHeader.length; 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); TRACE("Replaced media tag with type %d", old_media_tag->type);
free(oldMediaTag->data); free(old_media_tag->data);
free(oldMediaTag); free(old_media_tag);
oldMediaTag = NULL; old_media_tag = NULL;
} }
break; break;

View File

@@ -35,11 +35,11 @@
void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry) void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
TRACE("Entering process_dumphw_block(%p, %p)", ctx, entry); TRACE("Entering process_dumphw_block(%p, %p)", ctx, entry);
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
uint64_t crc64 = 0; uint64_t crc64 = 0;
uint16_t e = 0; uint16_t e = 0;
uint8_t *data = NULL; uint8_t *data = NULL;
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) if(ctx == NULL || ctx->imageStream == NULL)
@@ -62,9 +62,9 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
// Even if those two checks shall have been done before // Even if those two checks shall have been done before
TRACE("Reading dump hardware block header at position %" PRIu64, entry->offset); 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)); memset(&ctx->dumpHardwareHeader, 0, sizeof(DumpHardwareHeader));
TRACE("Could not read dump hardware block header, continuing..."); TRACE("Could not read dump hardware block header, continuing...");
@@ -89,9 +89,9 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
return; 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); crc64 = aaruf_crc64_data(data, ctx->dumpHardwareHeader.length);
@@ -109,7 +109,7 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
} }
free(data); free(data);
fseek(ctx->imageStream, -(long)readBytes, SEEK_CUR); fseek(ctx->imageStream, -(long)read_bytes, SEEK_CUR);
ctx->dumpHardwareEntriesWithData = ctx->dumpHardwareEntriesWithData =
(DumpHardwareEntriesWithData *)malloc(sizeof(DumpHardwareEntriesWithData) * ctx->dumpHardwareHeader.entries); (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); TRACE("Processing %u dump hardware block entries", ctx->dumpHardwareHeader.entries);
for(e = 0; e < ctx->dumpHardwareHeader.entries; e++) 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; ctx->dumpHardwareHeader.entries = e;
TRACE("Could not read dump hardware block entry, continuing..."); 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] ctx->dumpHardwareEntriesWithData[e]
.manufacturer[ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength] = 0; .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); 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); free(ctx->dumpHardwareEntriesWithData[e].manufacturer);
ctx->dumpHardwareEntriesWithData[e].entry.manufacturerLength = 0; 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) if(ctx->dumpHardwareEntriesWithData[e].model != NULL)
{ {
ctx->dumpHardwareEntriesWithData[e].model[ctx->dumpHardwareEntriesWithData[e].entry.modelLength] = 0; 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); 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); free(ctx->dumpHardwareEntriesWithData[e].model);
ctx->dumpHardwareEntriesWithData[e].entry.modelLength = 0; 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] = ctx->dumpHardwareEntriesWithData[e].revision[ctx->dumpHardwareEntriesWithData[e].entry.revisionLength] =
0; 0;
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].revision, 1, read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].revision, 1,
ctx->dumpHardwareEntriesWithData[e].entry.revisionLength, ctx->imageStream); 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); free(ctx->dumpHardwareEntriesWithData[e].revision);
ctx->dumpHardwareEntriesWithData[e].entry.revisionLength = 0; 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] = ctx->dumpHardwareEntriesWithData[e].firmware[ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength] =
0; 0;
readBytes = fread(ctx->dumpHardwareEntriesWithData[e].firmware, 1, read_bytes = fread(ctx->dumpHardwareEntriesWithData[e].firmware, 1,
ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength, ctx->imageStream); 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); free(ctx->dumpHardwareEntriesWithData[e].firmware);
ctx->dumpHardwareEntriesWithData[e].entry.firmwareLength = 0; 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) if(ctx->dumpHardwareEntriesWithData[e].serial != NULL)
{ {
ctx->dumpHardwareEntriesWithData[e].serial[ctx->dumpHardwareEntriesWithData[e].entry.serialLength] = 0; 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); 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); free(ctx->dumpHardwareEntriesWithData[e].serial);
ctx->dumpHardwareEntriesWithData[e].entry.serialLength = 0; ctx->dumpHardwareEntriesWithData[e].entry.serialLength = 0;
@@ -251,10 +251,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
ctx->dumpHardwareEntriesWithData[e] ctx->dumpHardwareEntriesWithData[e]
.softwareName[ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength] = 0; .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); 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); free(ctx->dumpHardwareEntriesWithData[e].softwareName);
ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength = 0; ctx->dumpHardwareEntriesWithData[e].entry.softwareNameLength = 0;
@@ -273,10 +273,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
ctx->dumpHardwareEntriesWithData[e] ctx->dumpHardwareEntriesWithData[e]
.softwareVersion[ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength] = 0; .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); 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); free(ctx->dumpHardwareEntriesWithData[e].softwareVersion);
ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength = 0; ctx->dumpHardwareEntriesWithData[e].entry.softwareVersionLength = 0;
@@ -296,11 +296,11 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
ctx->dumpHardwareEntriesWithData[e] ctx->dumpHardwareEntriesWithData[e]
.softwareOperatingSystem[ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength] = .softwareOperatingSystem[ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength] =
0; 0;
readBytes = read_bytes =
fread(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem, 1, fread(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem, 1,
ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength, ctx->imageStream); 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); free(ctx->dumpHardwareEntriesWithData[e].softwareOperatingSystem);
ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength = 0; ctx->dumpHardwareEntriesWithData[e].entry.softwareOperatingSystemLength = 0;
@@ -320,10 +320,10 @@ void process_dumphw_block(aaruformatContext *ctx, const IndexEntry *entry)
continue; 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); 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); free(ctx->dumpHardwareEntriesWithData[e].extents);
TRACE("Could not read dump hardware block extents, continuing..."); TRACE("Could not read dump hardware block extents, continuing...");

View File

@@ -35,8 +35,8 @@
void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry) void process_metadata_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
TRACE("Entering process_metadata_block(%p, %p)", ctx, entry); TRACE("Entering process_metadata_block(%p, %p)", ctx, entry);
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) 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 // Even if those two checks shall have been done before
TRACE("Reading metadata block header at position %" PRIu64, entry->offset); 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)); memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
FATAL("Could not read metadata block header, continuing..."); 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, TRACE("Reading metadata block of size %u at position %" PRIu64, ctx->metadataBlockHeader.blockSize,
entry->offset + sizeof(MetadataBlockHeader)); 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)); memset(&ctx->metadataBlockHeader, 0, sizeof(MetadataBlockHeader));
free(ctx->metadataBlock); 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) void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
TRACE("Entering process_geometry_block(%p, %p)", ctx, 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 // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) 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); 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)); memset(&ctx->geometryBlock, 0, sizeof(GeometryBlockHeader));
TRACE("Could not read geometry block header, continuing..."); TRACE("Could not read geometry block header, continuing...");
@@ -309,8 +309,8 @@ void process_geometry_block(aaruformatContext *ctx, const IndexEntry *entry)
void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry) void process_cicm_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
TRACE("Entering process_cicm_block(%p, %p)", ctx, entry); TRACE("Entering process_cicm_block(%p, %p)", ctx, entry);
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) 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 // Even if those two checks shall have been done before
TRACE("Reading CICM XML metadata block header at position %" PRIu64, entry->offset); 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)); memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
TRACE("Could not read CICM XML metadata header, continuing..."); 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, TRACE("Reading CICM XML metadata block of size %u at position %" PRIu64, ctx->cicmBlockHeader.length,
entry->offset + sizeof(CicmMetadataBlock)); 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)); memset(&ctx->cicmBlockHeader, 0, sizeof(CicmMetadataBlock));
free(ctx->cicmBlock); free(ctx->cicmBlock);

View File

@@ -34,9 +34,9 @@
*/ */
void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry) void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
{ {
int pos = 0; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
uint64_t crc64 = 0; uint64_t crc64 = 0;
int j = 0, k = 0; int j = 0, k = 0;
// Check if the context and image stream are valid // Check if the context and image stream are valid
@@ -56,9 +56,9 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
} }
// Even if those two checks shall have been done before // 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)); memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
TRACE("Could not read tracks header, continuing...\n"); TRACE("Could not read tracks header, continuing...\n");
@@ -82,9 +82,9 @@ void process_tracks_block(aaruformatContext *ctx, const IndexEntry *entry)
return; 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)); memset(&ctx->tracksHeader, 0, sizeof(TracksHeader));
free(ctx->trackEntries); free(ctx->trackEntries);

View File

@@ -44,32 +44,32 @@ void *aaruf_ecc_cd_init()
if(context == NULL) return NULL; if(context == NULL) return NULL;
TRACE("Allocating memory for ECC F table"); 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); free(context);
return NULL; return NULL;
} }
TRACE("Allocating memory for ECC B table"); 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); free(context);
return NULL; return NULL;
} }
TRACE("Allocating memory for EDC table"); 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->ecc_f_table);
free(context->eccBTable); free(context->ecc_b_table);
free(context); free(context);
return NULL; return NULL;
} }
@@ -77,15 +77,15 @@ void *aaruf_ecc_cd_init()
TRACE("Initializing EDC tables"); TRACE("Initializing EDC tables");
for(i = 0; i < 256; i++) for(i = 0; i < 256; i++)
{ {
edc = i; edc = i;
j = (uint32_t)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0)); j = (uint32_t)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
context->eccFTable[i] = (uint8_t)j; context->ecc_f_table[i] = (uint8_t)j;
context->eccBTable[i ^ j] = (uint8_t)i; context->ecc_b_table[i ^ j] = (uint8_t)i;
for(j = 0; j < 8; j++) edc = (edc >> 1) ^ ((edc & 1) > 0 ? 0xD8018001 : 0); 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()"); TRACE("Exiting aaruf_ecc_cd_init()");
return context; 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); TRACE("Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
CdEccContext *ctx; CdEccContext *ctx;
uint32_t storedEdc, edc, calculatedEdc; uint32_t stored_edc, edc, calculated_edc;
int size, pos; int size, pos;
if(context == NULL || sector == NULL) 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; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
return false; return false;
@@ -127,29 +127,32 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
return false; return false;
} }
bool correctEccP = aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C); bool correct_ecc_p = aaruf_ecc_cd_check(context, sector, sector, 86, 24, 2, 86, sector, 0xC, 0x10, 0x81C);
if(!correctEccP) if(!correct_ecc_p)
{ {
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
return false; return false;
} }
bool correctEccQ = aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC); bool correct_ecc_q = aaruf_ecc_cd_check(context, sector, sector, 52, 43, 86, 88, sector, 0xC, 0x10, 0x81C + 0xAC);
if(!correctEccQ) if(!correct_ecc_q)
{ {
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = false");
return false; return false;
} }
storedEdc = (sector[0x813] << 24) + (sector[0x812] << 16) + (sector[0x811] << 8) + sector[0x810]; stored_edc = (uint32_t)sector[0x808] | (uint32_t)sector[0x809] << 8 | (uint32_t)sector[0x80A] << 16 |
edc = 0; (uint32_t)sector[0x80B] << 24;
size = 0x810; calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
pos = 0;
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edcTable[(edc ^ sector[pos++]) & 0xFF];
calculatedEdc = edc;
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() = %u == %u", calculatedEdc, storedEdc); if(stored_edc != calculated_edc)
return calculatedEdc == storedEdc; {
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); TRACE("Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
CdEccContext *ctx; CdEccContext *ctx;
uint32_t storedEdc, edc, calculatedEdc; uint32_t stored_edc, edc, calculated_edc;
int size, pos; int size, pos;
uint8_t zeroaddress[4]; 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; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() without initialized context");
return false; 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)); memset(&zeroaddress, 4, sizeof(uint8_t));
bool correctEccP = aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C); bool correct_ecc_p = aaruf_ecc_cd_check(context, zeroaddress, sector, 86, 24, 2, 86, sector, 0, 0x10, 0x81C);
if(!correctEccP) if(!correct_ecc_p)
{ {
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
return false; return false;
} }
bool correctEccQ = aaruf_ecc_cd_check(context, zeroaddress, sector, 52, 43, 86, 88, sector, 0, 0x10, 0x81C + 0xAC); bool correct_ecc_q =
if(!correctEccQ) 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"); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
return false; return false;
} }
storedEdc = (sector[0x81B] << 24) + (sector[0x81A] << 16) + (sector[0x819] << 8) + sector[0x818]; stored_edc = (uint32_t)sector[0x92C] | (uint32_t)sector[0x92D] << 8 | (uint32_t)sector[0x92E] << 16 |
edc = 0; (uint32_t)sector[0x92F] << 24;
size = 0x808; calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
pos = 0x10;
for(; size > 0; size--) edc = (edc >> 8) ^ ctx->edcTable[(edc ^ sector[pos++]) & 0xFF];
calculatedEdc = edc;
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculatedEdc, storedEdc); TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
return calculatedEdc == storedEdc; 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 context Pointer to the ECC context.
* @param address Pointer to the address field. * @param address Pointer to the address field.
* @param data Pointer to the data field. * @param data Pointer to the data field.
* @param majorCount Number of major iterations. * @param major_count Number of major iterations.
* @param minorCount Number of minor iterations. * @param minor_count Number of minor iterations.
* @param majorMult Major multiplier. * @param major_mult Major multiplier.
* @param minorInc Minor increment. * @param minor_inc Minor increment.
* @param ecc Pointer to the ECC field. * @param ecc Pointer to the ECC field.
* @param addressOffset Offset for the address field. * @param address_offset Offset for the address field.
* @param dataOffset Offset for the data field. * @param data_offset Offset for the data field.
* @param eccOffset Offset for the ECC field. * @param ecc_offset Offset for the ECC field.
* @return true if ECC is correct, false otherwise. * @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, bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *data, uint32_t major_count,
uint32_t minorCount, uint32_t majorMult, uint32_t minorInc, const uint8_t *ecc, uint32_t minor_count, uint32_t major_mult, uint32_t minor_inc, const 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_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data, majorCount, TRACE("Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
minorCount, majorMult, minorInc, ecc, addressOffset, dataOffset, eccOffset); major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
CdEccContext *ctx; CdEccContext *ctx;
uint32_t size, major, idx, minor; 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) 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; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_ecc_cd_check() without initialized context"); TRACE("Exiting aaruf_ecc_cd_check() without initialized context");
return false; return false;
} }
size = majorCount * minorCount; size = major_count * minor_count;
for(major = 0; major < majorCount; major++) for(major = 0; major < major_count; major++)
{ {
idx = (major >> 1) * majorMult + (major & 1); idx = (major >> 1) * major_mult + (major & 1);
eccA = 0; ecc_a = 0;
eccB = 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]; temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
idx += minorInc; idx += minor_inc;
if(idx >= size) idx -= size; if(idx >= size) idx -= size;
eccA ^= temp; ecc_a ^= temp;
eccB ^= temp; ecc_b ^= temp;
eccA = ctx->eccFTable[eccA]; ecc_a = ctx->ecc_f_table[ecc_a];
} }
eccA = ctx->eccBTable[ctx->eccFTable[eccA] ^ eccB]; ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
if(ecc[major + eccOffset] != eccA || ecc[major + majorCount + eccOffset] != (eccA ^ eccB)) 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); TRACE("Exiting aaruf_ecc_cd_check() = false, ECC mismatch at major %u", major);
return false; 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 context Pointer to the ECC context.
* @param address Pointer to the address field. * @param address Pointer to the address field.
* @param data Pointer to the data field. * @param data Pointer to the data field.
* @param majorCount Number of major iterations. * @param major_count Number of major iterations.
* @param minorCount Number of minor iterations. * @param minor_count Number of minor iterations.
* @param majorMult Major multiplier. * @param major_mult Major multiplier.
* @param minorInc Minor increment. * @param minor_inc Minor increment.
* @param ecc Pointer to the ECC field to write. * @param ecc Pointer to the ECC field to write.
* @param addressOffset Offset for the address field. * @param address_offset Offset for the address field.
* @param dataOffset Offset for the data field. * @param data_offset Offset for the data field.
* @param eccOffset Offset for the ECC 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, void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *data, uint32_t major_count,
uint32_t minorCount, uint32_t majorMult, uint32_t minorInc, uint8_t *ecc, int32_t addressOffset, uint32_t minor_count, uint32_t major_mult, uint32_t minor_inc, uint8_t *ecc,
int32_t dataOffset, int32_t eccOffset) 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, TRACE("Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
minorCount, majorMult, minorInc, ecc, addressOffset, dataOffset, eccOffset); major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
CdEccContext *ctx; CdEccContext *ctx;
uint32_t size, major, idx, minor; 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) 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; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_ecc_cd_write() without initialized context"); TRACE("Exiting aaruf_ecc_cd_write() without initialized context");
return; return;
} }
size = majorCount * minorCount; size = major_count * minor_count;
for(major = 0; major < majorCount; major++) for(major = 0; major < major_count; major++)
{ {
idx = (major >> 1) * majorMult + (major & 1); idx = (major >> 1) * major_mult + (major & 1);
eccA = 0; ecc_a = 0;
eccB = 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]; temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
idx += minorInc; idx += minor_inc;
if(idx >= size) idx -= size; if(idx >= size) idx -= size;
eccA ^= temp; ecc_a ^= temp;
eccB ^= temp; ecc_b ^= temp;
eccA = ctx->eccFTable[eccA]; ecc_a = ctx->ecc_f_table[ecc_a];
} }
eccA = ctx->eccBTable[ctx->eccFTable[eccA] ^ eccB]; ecc_a = ctx->ecc_b_table[ctx->ecc_f_table[ecc_a] ^ ecc_b];
ecc[major + eccOffset] = eccA; ecc[major + ecc_offset] = ecc_a;
ecc[major + majorCount + eccOffset] = (eccA ^ eccB); ecc[major + major_count + ecc_offset] = (ecc_a ^ ecc_b);
} }
TRACE("Exiting aaruf_ecc_cd_write()"); 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 address Pointer to the address field.
* @param data Pointer to the data field. * @param data Pointer to the data field.
* @param ecc Pointer to the ECC field to write. * @param ecc Pointer to the ECC field to write.
* @param addressOffset Offset for the address field. * @param address_offset Offset for the address field.
* @param dataOffset Offset for the data field. * @param data_offset Offset for the data field.
* @param eccOffset Offset for the ECC 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, 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, TRACE("Entering aaruf_ecc_cd_write_sector(%p, %p, %p, %p, %d, %d, %d)", context, address, data, ecc, address_offset,
dataOffset, eccOffset); 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, 86, 24, 2, 86, ecc, address_offset, data_offset, ecc_offset); // 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, 52, 43, 86, 88, ecc, address_offset, data_offset,
ecc_offset + 0xAC); // Q
TRACE("Exiting aaruf_ecc_cd_write_sector()"); 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); TRACE("Entering aaruf_ecc_cd_reconstruct(%p, %p, %u)", context, sector, type);
uint32_t computedEdc; uint32_t computed_edc;
uint8_t zeroaddress[4]; uint8_t zeroaddress[4];
CdEccContext *ctx; CdEccContext *ctx;
@@ -473,7 +475,7 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type)
ctx = (CdEccContext *)context; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_ecc_cd_reconstruct() without initialized context"); TRACE("Exiting aaruf_ecc_cd_reconstruct() without initialized context");
return; return;
@@ -485,16 +487,16 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, uint8_t type)
// Compute EDC // Compute EDC
// //
case CdMode1: case CdMode1:
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0); computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x810, 0);
memcpy(sector + 0x810, &computedEdc, 4); memcpy(sector + 0x810, &computed_edc, 4);
break; break;
case CdMode2Form1: case CdMode2Form1:
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x808, 0x10); computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x808, 0x10);
memcpy(sector + 0x818, &computedEdc, 4); memcpy(sector + 0x818, &computed_edc, 4);
break; break;
case CdMode2Form2: case CdMode2Form2:
computedEdc = aaruf_edc_cd_compute(context, 0, sector, 0x91C, 0x10); computed_edc = aaruf_edc_cd_compute(context, 0, sector, 0x91C, 0x10);
memcpy(sector + 0x92C, &computedEdc, 4); memcpy(sector + 0x92C, &computed_edc, 4);
break; break;
default: default:
TRACE("Exiting aaruf_ecc_cd_reconstruct() with unknown type %u", type); 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; ctx = (CdEccContext *)context;
if(!ctx->initedEdc) if(!ctx->inited_edc)
{ {
TRACE("Exiting aaruf_edc_cd_compute() without initialized context"); TRACE("Exiting aaruf_edc_cd_compute() without initialized context");
return 0; 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); TRACE("Exiting aaruf_edc_cd_compute() = 0x%08X", edc);
return edc; return edc;

View File

@@ -41,9 +41,9 @@ int aaruf_close(void *context)
{ {
TRACE("Entering aaruf_close(%p)", context); TRACE("Entering aaruf_close(%p)", context);
int i = 0; int i = 0;
mediaTagEntry *mediaTag = NULL; mediaTagEntry *media_tag = NULL;
mediaTagEntry *tmpMediaTag = NULL; mediaTagEntry *tmp_media_tag = NULL;
if(context == 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 // 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) // 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->cachedDdtOffset != 0) ||
(ctx->cachedSecondaryDdtSmall != NULL || ctx->cachedSecondaryDdtBig != NULL)); (ctx->cachedSecondaryDdtSmall != NULL || ctx->cachedSecondaryDdtBig != NULL));
if(hasCachedSecondaryDdt) if(has_cached_secondary_ddt)
{ {
TRACE("Writing cached secondary DDT table to file"); TRACE("Writing cached secondary DDT table to file");
fseek(ctx->imageStream, 0, SEEK_END); 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 // Align the position according to block alignment shift
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1; uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
if(endOfFile & alignmentMask) if(end_of_file & alignment_mask)
{ {
// Calculate the next aligned position // 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 // Seek to the aligned position and pad with zeros if necessary
fseek(ctx->imageStream, alignedPosition, SEEK_SET); fseek(ctx->imageStream, aligned_position, SEEK_SET);
endOfFile = alignedPosition; end_of_file = aligned_position;
TRACE("Aligned DDT write position from %ld to %" PRIu64 " (alignment shift: %d)", 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); ctx->userDataDdtHeader.blockAlignmentShift);
} }
// Prepare DDT header for the cached table // Prepare DDT header for the cached table
DdtHeader2 ddtHeader = {0}; DdtHeader2 ddt_header;
ddtHeader.identifier = DeDuplicationTable2; memset(&ddt_header, 0, sizeof(DdtHeader2));
ddtHeader.type = UserData; ddt_header.identifier = DeDuplicationTable2;
ddtHeader.compression = None; ddt_header.type = UserData;
ddtHeader.levels = ctx->userDataDdtHeader.levels; ddt_header.compression = None;
ddtHeader.tableLevel = ctx->userDataDdtHeader.tableLevel + 1; ddt_header.levels = ctx->userDataDdtHeader.levels;
ddtHeader.previousLevelOffset = ctx->primaryDdtOffset; ddt_header.tableLevel = ctx->userDataDdtHeader.tableLevel + 1;
ddtHeader.negative = ctx->userDataDdtHeader.negative; ddt_header.previousLevelOffset = ctx->primaryDdtOffset;
ddtHeader.overflow = ctx->userDataDdtHeader.overflow; ddt_header.negative = ctx->userDataDdtHeader.negative;
ddtHeader.blockAlignmentShift = ctx->userDataDdtHeader.blockAlignmentShift; ddt_header.overflow = ctx->userDataDdtHeader.overflow;
ddtHeader.dataShift = ctx->userDataDdtHeader.dataShift; ddt_header.blockAlignmentShift = ctx->userDataDdtHeader.blockAlignmentShift;
ddtHeader.tableShift = 0; // Secondary tables are single level ddt_header.dataShift = ctx->userDataDdtHeader.dataShift;
ddtHeader.sizeType = ctx->userDataDdtHeader.sizeType; ddt_header.tableShift = 0; // Secondary tables are single level
ddt_header.sizeType = ctx->userDataDdtHeader.sizeType;
uint64_t itemsPerDdtEntry = 1 << ctx->userDataDdtHeader.tableShift; uint64_t items_per_ddt_entry = 1 << ctx->userDataDdtHeader.tableShift;
ddtHeader.blocks = itemsPerDdtEntry; ddt_header.blocks = items_per_ddt_entry;
ddtHeader.entries = itemsPerDdtEntry; ddt_header.entries = items_per_ddt_entry;
ddtHeader.start = ctx->cachedDdtPosition * itemsPerDdtEntry; ddt_header.start = ctx->cachedDdtPosition * items_per_ddt_entry;
// Calculate data size // Calculate data size
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
ddtHeader.length = itemsPerDdtEntry * sizeof(uint16_t); ddt_header.length = items_per_ddt_entry * sizeof(uint16_t);
else 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 // Calculate CRC64 of the data
crc64_ctx *crc64_context = aaruf_crc64_init(); crc64_ctx *crc64_context = aaruf_crc64_init();
if(crc64_context != NULL) if(crc64_context != NULL)
{ {
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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; uint64_t crc64;
aaruf_crc64_final(crc64_context, &crc64); aaruf_crc64_final(crc64_context, &crc64);
ddtHeader.crc64 = crc64; ddt_header.crc64 = crc64;
ddtHeader.cmpCrc64 = crc64; ddt_header.cmpCrc64 = crc64;
} }
// Write header // Write header
if(fwrite(&ddtHeader, sizeof(DdtHeader2), 1, ctx->imageStream) == 1) if(fwrite(&ddt_header, sizeof(DdtHeader2), 1, ctx->imageStream) == 1)
{ {
// Write data // Write data
size_t writtenBytes = 0; size_t written_bytes = 0;
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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 // 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) if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType)
ctx->userDataDdtMini[ctx->cachedDdtPosition] = (uint16_t)newSecondaryTableBlockOffset; ctx->userDataDdtMini[ctx->cachedDdtPosition] = (uint16_t)new_secondary_table_block_offset;
else 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 // Update index: remove old entry for cached DDT and add new one
TRACE("Updating index for cached secondary DDT"); 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 // Add new index entry for the newly written secondary DDT
IndexEntry newDdtEntry; IndexEntry new_ddt_entry;
newDdtEntry.blockType = DeDuplicationTable2; new_ddt_entry.blockType = DeDuplicationTable2;
newDdtEntry.dataType = UserData; new_ddt_entry.dataType = UserData;
newDdtEntry.offset = endOfFile; new_ddt_entry.offset = end_of_file;
utarray_push_back(ctx->indexEntries, &newDdtEntry); utarray_push_back(ctx->indexEntries, &new_ddt_entry);
TRACE("Added new DDT index entry at offset %" PRIu64, endOfFile); 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 // 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); 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(uint16_t)
: ctx->userDataDdtHeader.entries * sizeof(uint32_t); : ctx->userDataDdtHeader.entries * sizeof(uint32_t);
size_t primaryWrittenBytes = 0; size_t primary_written_bytes = 0;
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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."); TRACE("Could not flush primary DDT table to file.");
return AARUF_ERROR_CANNOT_WRITE_HEADER; return AARUF_ERROR_CANNOT_WRITE_HEADER;
} }
fseek(ctx->imageStream, savedPos, SEEK_SET); fseek(ctx->imageStream, saved_pos, SEEK_SET);
} }
else else
TRACE("Failed to write cached secondary DDT data"); TRACE("Failed to write cached secondary DDT data");
@@ -266,14 +268,14 @@ int aaruf_close(void *context)
crc64_ctx *crc64_context = aaruf_crc64_init(); crc64_ctx *crc64_context = aaruf_crc64_init();
if(crc64_context != NULL) 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(uint16_t)
: ctx->userDataDdtHeader.entries * sizeof(uint32_t); : ctx->userDataDdtHeader.entries * sizeof(uint32_t);
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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; uint64_t crc64;
aaruf_crc64_final(crc64_context, &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 // dataShift, tableShift, sizeType, entries, blocks, start are already set during creation
ctx->userDataDdtHeader.crc64 = crc64; ctx->userDataDdtHeader.crc64 = crc64;
ctx->userDataDdtHeader.cmpCrc64 = crc64; ctx->userDataDdtHeader.cmpCrc64 = crc64;
ctx->userDataDdtHeader.length = primaryTableSize; ctx->userDataDdtHeader.length = primary_table_size;
ctx->userDataDdtHeader.cmpLength = primaryTableSize; ctx->userDataDdtHeader.cmpLength = primary_table_size;
TRACE("Calculated CRC64 for primary DDT: 0x%16lX", crc64); 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) // 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(uint16_t)
: ctx->userDataDdtHeader.entries * sizeof(uint32_t); : ctx->userDataDdtHeader.entries * sizeof(uint32_t);
// Write the primary table data // Write the primary table data
size_t writtenBytes = 0; size_t written_bytes = 0;
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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)", 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 // Add primary DDT to index
TRACE("Adding primary DDT to index"); TRACE("Adding primary DDT to index");
IndexEntry primaryDdtEntry; IndexEntry primary_ddt_entry;
primaryDdtEntry.blockType = DeDuplicationTable2; primary_ddt_entry.blockType = DeDuplicationTable2;
primaryDdtEntry.dataType = UserData; primary_ddt_entry.dataType = UserData;
primaryDdtEntry.offset = ctx->primaryDdtOffset; 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); TRACE("Added primary DDT index entry at offset %" PRIu64, ctx->primaryDdtOffset);
} }
else else
@@ -342,14 +344,14 @@ int aaruf_close(void *context)
crc64_ctx *crc64_context = aaruf_crc64_init(); crc64_ctx *crc64_context = aaruf_crc64_init();
if(crc64_context != NULL) 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(uint16_t)
: ctx->userDataDdtHeader.entries * sizeof(uint32_t); : ctx->userDataDdtHeader.entries * sizeof(uint32_t);
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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; uint64_t crc64;
aaruf_crc64_final(crc64_context, &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 // blockAlignmentShift, dataShift, tableShift, sizeType, entries, blocks, start are already set
ctx->userDataDdtHeader.crc64 = crc64; ctx->userDataDdtHeader.crc64 = crc64;
ctx->userDataDdtHeader.cmpCrc64 = crc64; ctx->userDataDdtHeader.cmpCrc64 = crc64;
ctx->userDataDdtHeader.length = primaryTableSize; ctx->userDataDdtHeader.length = primary_table_size;
ctx->userDataDdtHeader.cmpLength = primaryTableSize; ctx->userDataDdtHeader.cmpLength = primary_table_size;
TRACE("Calculated CRC64 for single-level DDT: 0x%16lX", crc64); 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) // 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(uint16_t)
: ctx->userDataDdtHeader.entries * sizeof(uint32_t); : ctx->userDataDdtHeader.entries * sizeof(uint32_t);
// Write the primary table data // Write the primary table data
size_t writtenBytes = 0; size_t written_bytes = 0;
if(ctx->userDataDdtHeader.sizeType == SmallDdtSizeType) 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 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)", 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 // Add single-level DDT to index
TRACE("Adding single-level DDT to index"); TRACE("Adding single-level DDT to index");
IndexEntry singleDdtEntry; IndexEntry single_ddt_entry;
singleDdtEntry.blockType = DeDuplicationTable2; single_ddt_entry.blockType = DeDuplicationTable2;
singleDdtEntry.dataType = UserData; single_ddt_entry.dataType = UserData;
singleDdtEntry.offset = ctx->primaryDdtOffset; 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); TRACE("Added single-level DDT index entry at offset %" PRIu64, ctx->primaryDdtOffset);
} }
else else
@@ -415,70 +417,70 @@ int aaruf_close(void *context)
// Write the complete index at the end of the file // Write the complete index at the end of the file
TRACE("Writing index at the end of the file"); TRACE("Writing index at the end of the file");
fseek(ctx->imageStream, 0, SEEK_END); 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 // Align index position to block boundary if needed
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1; uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
if(indexPosition & alignmentMask) if(index_position & alignment_mask)
{ {
uint64_t alignedPosition = (indexPosition + alignmentMask) & ~alignmentMask; uint64_t aligned_position = (index_position + alignment_mask) & ~alignment_mask;
fseek(ctx->imageStream, alignedPosition, SEEK_SET); fseek(ctx->imageStream, aligned_position, SEEK_SET);
indexPosition = alignedPosition; index_position = aligned_position;
TRACE("Aligned index position to %" PRIu64, alignedPosition); TRACE("Aligned index position to %" PRIu64, aligned_position);
} }
// Prepare index header // Prepare index header
IndexHeader3 indexHeader; IndexHeader3 index_header;
indexHeader.identifier = IndexBlock3; index_header.identifier = IndexBlock3;
indexHeader.entries = utarray_len(ctx->indexEntries); index_header.entries = utarray_len(ctx->indexEntries);
indexHeader.previous = 0; // No previous index for now 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 // Calculate CRC64 of index entries
crc64_ctx *indexCrc64Context = aaruf_crc64_init(); crc64_ctx *index_crc64_context = aaruf_crc64_init();
if(indexCrc64Context != NULL && indexHeader.entries > 0) if(index_crc64_context != NULL && index_header.entries > 0)
{ {
size_t indexDataSize = indexHeader.entries * sizeof(IndexEntry); size_t index_data_size = index_header.entries * sizeof(IndexEntry);
aaruf_crc64_update(indexCrc64Context, (uint8_t *)utarray_front(ctx->indexEntries), indexDataSize); aaruf_crc64_update(index_crc64_context, (uint8_t *)utarray_front(ctx->indexEntries), index_data_size);
aaruf_crc64_final(indexCrc64Context, &indexHeader.crc64); aaruf_crc64_final(index_crc64_context, &index_header.crc64);
TRACE("Calculated index CRC64: 0x%16lX", indexHeader.crc64); TRACE("Calculated index CRC64: 0x%16lX", index_header.crc64);
} }
else { indexHeader.crc64 = 0; } else { index_header.crc64 = 0; }
// Write index header // 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"); TRACE("Successfully wrote index header");
// Write index entries // Write index entries
if(indexHeader.entries > 0) if(index_header.entries > 0)
{ {
size_t entriesWritten = 0; size_t entries_written = 0;
IndexEntry *entry = NULL; IndexEntry *entry = NULL;
for(entry = (IndexEntry *)utarray_front(ctx->indexEntries); entry != NULL; for(entry = (IndexEntry *)utarray_front(ctx->indexEntries); entry != NULL;
entry = (IndexEntry *)utarray_next(ctx->indexEntries, entry)) entry = (IndexEntry *)utarray_next(ctx->indexEntries, entry))
{ {
if(fwrite(entry, sizeof(IndexEntry), 1, ctx->imageStream) == 1) 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, TRACE("Wrote index entry: blockType=0x%08X dataType=%u offset=%" PRIu64, entry->blockType,
entry->dataType, entry->offset); entry->dataType, entry->offset);
} }
else else
{ {
TRACE("Failed to write index entry %zu", entriesWritten); TRACE("Failed to write index entry %zu", entries_written);
break; 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 // 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); TRACE("Updating header with index offset: %" PRIu64, ctx->header.indexOffset);
// Seek back to beginning and rewrite header // Seek back to beginning and rewrite header
@@ -495,8 +497,8 @@ int aaruf_close(void *context)
} }
else else
{ {
TRACE("Failed to write all index entries (wrote %zu of %" PRIu64 ")", entriesWritten, TRACE("Failed to write all index entries (wrote %zu of %" PRIu64 ")", entries_written,
indexHeader.entries); index_header.entries);
return AARUF_ERROR_CANNOT_WRITE_HEADER; return AARUF_ERROR_CANNOT_WRITE_HEADER;
} }
} }
@@ -537,11 +539,11 @@ int aaruf_close(void *context)
ctx->mode2Subheaders = NULL; ctx->mode2Subheaders = NULL;
TRACE("Freeing media tags"); 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); HASH_DEL(ctx->mediaTags, media_tag);
free(mediaTag->data); free(media_tag->data);
free(mediaTag); free(media_tag);
} }
#ifdef __linux__ // TODO: Implement #ifdef __linux__ // TODO: Implement

View File

@@ -34,22 +34,22 @@
*/ */
int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, size_t length) int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, size_t length)
{ {
uint8_t *p = NULL; uint8_t *p = NULL;
uint8_t *q = NULL; uint8_t *q = NULL;
uint8_t *r = NULL; uint8_t *r = NULL;
uint8_t *s = NULL; uint8_t *s = NULL;
uint8_t *t = NULL; uint8_t *t = NULL;
uint8_t *u = NULL; uint8_t *u = NULL;
uint8_t *v = NULL; uint8_t *v = NULL;
uint8_t *w = NULL; uint8_t *w = NULL;
size_t qStart = 0; size_t q_start = 0;
size_t rStart = 0; size_t r_start = 0;
size_t sStart = 0; size_t s_start = 0;
size_t tStart = 0; size_t t_start = 0;
size_t uStart = 0; size_t u_start = 0;
size_t vStart = 0; size_t v_start = 0;
size_t wStart = 0; size_t w_start = 0;
size_t i = 0; size_t i = 0;
if(interleaved == NULL || sequential == NULL) return AARUF_ERROR_BUFFER_TOO_SMALL; 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; w[i / 8] += interleaved[i + 7] & 0x01;
} }
qStart = (length / 8) * 1; q_start = (length / 8) * 1;
rStart = (length / 8) * 2; r_start = (length / 8) * 2;
sStart = (length / 8) * 3; s_start = (length / 8) * 3;
tStart = (length / 8) * 4; t_start = (length / 8) * 4;
uStart = (length / 8) * 5; u_start = (length / 8) * 5;
vStart = (length / 8) * 6; v_start = (length / 8) * 6;
wStart = (length / 8) * 7; w_start = (length / 8) * 7;
for(i = 0; i < (length / 8); i++) for(i = 0; i < (length / 8); i++)
{ {
sequential[i] = p[i]; sequential[i] = p[i];
sequential[qStart + i] = q[i]; sequential[q_start + i] = q[i];
sequential[rStart + i] = r[i]; sequential[r_start + i] = r[i];
sequential[sStart + i] = s[i]; sequential[s_start + i] = s[i];
sequential[tStart + i] = t[i]; sequential[t_start + i] = t[i];
sequential[uStart + i] = u[i]; sequential[u_start + i] = u[i];
sequential[vStart + i] = v[i]; sequential[v_start + i] = v[i];
sequential[wStart + i] = w[i]; sequential[w_start + i] = w[i];
} }
free(p); 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) 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; uint8_t *p, *q, *r, *s, *t, *u, *v, *w;
size_t qStart; size_t q_start;
size_t rStart; size_t r_start;
size_t sStart; size_t s_start;
size_t tStart; size_t t_start;
size_t uStart; size_t u_start;
size_t vStart; size_t v_start;
size_t wStart; size_t w_start;
size_t i; size_t i;
if(interleaved == NULL || sequential == NULL) return AARUF_ERROR_BUFFER_TOO_SMALL; 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; return AARUF_ERROR_NOT_ENOUGH_MEMORY;
} }
qStart = (length / 8) * 1; q_start = (length / 8) * 1;
rStart = (length / 8) * 2; r_start = (length / 8) * 2;
sStart = (length / 8) * 3; s_start = (length / 8) * 3;
tStart = (length / 8) * 4; t_start = (length / 8) * 4;
uStart = (length / 8) * 5; u_start = (length / 8) * 5;
vStart = (length / 8) * 6; v_start = (length / 8) * 6;
wStart = (length / 8) * 7; w_start = (length / 8) * 7;
for(i = 0; i < (length / 8); i++) for(i = 0; i < (length / 8); i++)
{ {
p[i] = sequential[i]; p[i] = sequential[i];
q[i] = sequential[qStart + i]; q[i] = sequential[q_start + i];
r[i] = sequential[rStart + i]; r[i] = sequential[r_start + i];
s[i] = sequential[sStart + i]; s[i] = sequential[s_start + i];
t[i] = sequential[tStart + i]; t[i] = sequential[t_start + i];
u[i] = sequential[uStart + i]; u[i] = sequential[u_start + i];
v[i] = sequential[vStart + i]; v[i] = sequential[v_start + i];
w[i] = sequential[wStart + i]; w[i] = sequential[w_start + i];
} }
memset(interleaved, 0, length); memset(interleaved, 0, length);

View File

@@ -31,15 +31,15 @@
* @param dst_buffer Pointer to the destination buffer. * @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 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 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 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. * @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, 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_buffer Pointer to the destination buffer.
* @param dst_size Pointer to the size of the destination buffer; updated with the actual size. * @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 src_buffer Pointer to the source (uncompressed) buffer.
* @param srcLen Size of the source buffer. * @param src_len Size of the source buffer.
* @param outProps Pointer to the output LZMA properties. * @param out_props Pointer to the output LZMA properties.
* @param outPropsSize Pointer to the size of the output LZMA properties. * @param out_props_size Pointer to the size of the output LZMA properties.
* @param level Compression level. * @param level Compression level.
* @param dictSize Dictionary size. * @param dict_size Dictionary size.
* @param lc LZMA literal context bits. * @param lc LZMA literal context bits.
* @param lp LZMA literal position bits. * @param lp LZMA literal position bits.
* @param pb LZMA position bits. * @param pb LZMA position bits.
* @param fb Number of fast bytes. * @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. * @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, 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, size_t src_len, uint8_t *out_props, size_t *out_props_size,
int32_t level, uint32_t dictSize, int32_t lc, int32_t lp, int32_t level, uint32_t dict_size, int32_t lc, int32_t lp,
int32_t pb, int32_t fb, int32_t numThreads) 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, return LzmaCompress(dst_buffer, dst_size, src_buffer, src_len, out_props, out_props_size, level, dict_size, lc, lp,
fb, numThreads); pb, fb, num_threads);
} }

View File

@@ -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, 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 mask_a = _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_b = _mm_xor_si128(mask_a, _mm_cmpeq_epi8(_mm_setzero_si128(), _mm_setzero_si128()));
*outLeft = _mm_shuffle_epi8(in, maskB); *out_left = _mm_shuffle_epi8(in, mask_b);
*outRight = _mm_shuffle_epi8(in, maskA); *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,123 +100,124 @@ 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 mu = 0x9c3e466c172963d5; // (bitReflect(div129by65(poly)) << 1) | 1;
const uint64_t p = 0x92d8af2baf0e1e85; // (bitReflect(poly) << 1) | 1; const uint64_t p = 0x92d8af2baf0e1e85; // (bitReflect(poly) << 1) | 1;
const __m128i foldConstants1 = _mm_set_epi64x(k2, k1); const __m128i fold_constants_1 = _mm_set_epi64x(k2, k1);
const __m128i foldConstants2 = _mm_set_epi64x(p, mu); const __m128i fold_constants_2 = _mm_set_epi64x(p, mu);
const uint8_t *end = data + length; const uint8_t *end = data + length;
// Align pointers // Align pointers
const __m128i *alignedData = (const __m128i *)((uintptr_t)data & ~(uintptr_t)15); const __m128i *aligned_data = (const __m128i *)((uintptr_t)data & ~(uintptr_t)15);
const __m128i *alignedEnd = (const __m128i *)(((uintptr_t)end + 15) & ~(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 lead_in_size = data - (const uint8_t *)aligned_data;
const size_t leadOutSize = (const uint8_t *)alignedEnd - end; 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 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(alignedData), leadInMask); const __m128i data0 = _mm_blendv_epi8(_mm_setzero_si128(), _mm_load_si128(aligned_data), lead_in_mask);
#if defined(_WIN64) #if defined(_WIN64)
const __m128i initialCrc = _mm_cvtsi64x_si128(~crc); const __m128i initial_crc = _mm_cvtsi64x_si128(~crc);
#else #else
const __m128i initialCrc = _mm_set_epi64x(0, ~crc); const __m128i initial_crc = _mm_set_epi64x(0, ~crc);
#endif #endif
__m128i R; __m128i r_reg;
if(alignedLength == 1) if(aligned_length == 1)
{ {
// Single data block, initial CRC possibly bleeds into zero padding // Single data block, initial CRC possibly bleeds into zero padding
__m128i crc0, crc1; __m128i crc0, crc1;
shiftRight128(initialCrc, 16 - length, &crc0, &crc1); shiftRight128(initial_crc, 16 - length, &crc0, &crc1);
__m128i A, B; __m128i a_reg, b_reg;
shiftRight128(data0, leadOutSize, &A, &B); shiftRight128(data0, lead_out_size, &a_reg, &b_reg);
const __m128i P = _mm_xor_si128(A, crc0); const __m128i p_reg = _mm_xor_si128(a_reg, crc0);
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10), r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10),
_mm_xor_si128(_mm_srli_si128(P, 8), _mm_slli_si128(crc1, 8))); _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) if(length < 8)
{ {
// Initial CRC bleeds into the zero padding // Initial CRC bleeds into the zero padding
__m128i crc0, crc1; __m128i crc0, crc1;
shiftRight128(initialCrc, 16 - length, &crc0, &crc1); shiftRight128(initial_crc, 16 - length, &crc0, &crc1);
__m128i A, B, C, D; __m128i a_reg, b_reg, c_reg, d_reg;
shiftRight128(data0, leadOutSize, &A, &B); shiftRight128(data0, lead_out_size, &a_reg, &b_reg);
shiftRight128(data1, leadOutSize, &C, &D); shiftRight128(data1, lead_out_size, &c_reg, &d_reg);
const __m128i P = _mm_xor_si128(_mm_xor_si128(B, C), crc0); const __m128i p_reg = _mm_xor_si128(_mm_xor_si128(b_reg, c_reg), crc0);
R = _mm_xor_si128(_mm_clmulepi64_si128(P, foldConstants1, 0x10), r_reg = _mm_xor_si128(_mm_clmulepi64_si128(p_reg, fold_constants_1, 0x10),
_mm_xor_si128(_mm_srli_si128(P, 8), _mm_slli_si128(crc1, 8))); _mm_xor_si128(_mm_srli_si128(p_reg, 8), _mm_slli_si128(crc1, 8)));
} }
else else
{ {
// We can fit the initial CRC into the data without bleeding into the zero padding // We can fit the initial CRC into the data without bleeding into the zero padding
__m128i crc0, crc1; __m128i crc0, crc1;
shiftRight128(initialCrc, leadInSize, &crc0, &crc1); shiftRight128(initial_crc, lead_in_size, &crc0, &crc1);
__m128i A, B, C, D; __m128i a_reg, b_reg, c_reg, d_reg;
shiftRight128(_mm_xor_si128(data0, crc0), leadOutSize, &A, &B); shiftRight128(_mm_xor_si128(data0, crc0), lead_out_size, &a_reg, &b_reg);
shiftRight128(_mm_xor_si128(data1, crc1), leadOutSize, &C, &D); 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)); const __m128i p_reg = _mm_xor_si128(fold(a_reg, fold_constants_1), _mm_xor_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));
} }
} }
else else
{ {
alignedData++; aligned_data++;
length -= 16 - leadInSize; length -= 16 - lead_in_size;
// Initial CRC can simply be added to data // Initial CRC can simply be added to data
__m128i crc0, crc1; __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) 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; length -= 16;
alignedData++; aligned_data++;
} }
__m128i P; __m128i p_reg;
if(length == 16) { P = _mm_xor_si128(accumulator, _mm_load_si128(alignedData)); } if(length == 16) { p_reg = _mm_xor_si128(accumulator, _mm_load_si128(aligned_data)); }
else else
{ {
const __m128i end0 = _mm_xor_si128(accumulator, _mm_load_si128(alignedData)); const __m128i end0 = _mm_xor_si128(accumulator, _mm_load_si128(aligned_data));
const __m128i end1 = _mm_load_si128(alignedData + 1); const __m128i end1 = _mm_load_si128(aligned_data + 1);
__m128i A, B, C, D; __m128i a_reg, b_reg, c_reg, d_reg;
shiftRight128(end0, leadOutSize, &A, &B); shiftRight128(end0, lead_out_size, &a_reg, &b_reg);
shiftRight128(end1, leadOutSize, &C, &D); 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 // Final Barrett reduction
const __m128i T1 = _mm_clmulepi64_si128(R, foldConstants2, 0x00); const __m128i t1_reg = _mm_clmulepi64_si128(r_reg, fold_constants_2, 0x00);
const __m128i T2 = const __m128i t2_reg = _mm_xor_si128(
_mm_xor_si128(_mm_xor_si128(_mm_clmulepi64_si128(T1, foldConstants2, 0x10), _mm_slli_si128(T1, 8)), R); _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()"); TRACE("Exiting aaruf_crc64_clmul()");
#if defined(_WIN64) #if defined(_WIN64)
return ~_mm_extract_epi64(T2, 1); return ~_mm_extract_epi64(t2_reg, 1);
#else #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 #endif
} }
#endif #endif

View File

@@ -32,31 +32,31 @@
* Allocates and initializes a new aaruformat context and image file with the specified parameters. * Allocates and initializes a new aaruformat context and image file with the specified parameters.
* *
* @param filepath Path to the image file to create. * @param filepath Path to the image file to create.
* @param mediaType Media type identifier. * @param media_type Media type identifier.
* @param sectorSize Size of each sector in bytes. * @param sector_size Size of each sector in bytes.
* @param userSectors Number of user data sectors. * @param user_sectors Number of user data sectors.
* @param negativeSectors Number of negative sectors. * @param negative_sectors Number of negative sectors.
* @param overflowSectors Number of overflow sectors. * @param overflow_sectors Number of overflow sectors.
* @param options String with creation options. * @param options String with creation options.
* @param applicationName Pointer to the application name string. * @param application_name Pointer to the application name string.
* @param applicationNameLength Length of the application name string. * @param application_name_length Length of the application name string.
* @param applicationMajorVersion Major version of the application. * @param application_major_version Major version of the application.
* @param applicationMinorVersion Minor version of the application. * @param application_minor_version Minor version of the application.
* @return Pointer to the created aaruformat context, or NULL on failure. * @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, void *aaruf_create(const char *filepath, uint32_t media_type, uint32_t sector_size, uint64_t user_sectors,
uint64_t negativeSectors, uint64_t overflowSectors, const char *options, uint64_t negative_sectors, uint64_t overflow_sectors, const char *options,
const uint8_t *applicationName, uint8_t applicationNameLength, uint8_t applicationMajorVersion, const uint8_t *application_name, uint8_t application_name_length, uint8_t application_major_version,
uint8_t applicationMinorVersion) uint8_t application_minor_version)
{ {
TRACE("Entering aaruf_create(%s, %u, %u, %llu, %llu, %llu, %s, %s, %u, %u, %u)", filepath, mediaType, sectorSize, TRACE("Entering aaruf_create(%s, %u, %u, %llu, %llu, %llu, %s, %s, %u, %u, %u)", filepath, media_type, sector_size,
userSectors, negativeSectors, overflowSectors, options, user_sectors, negative_sectors, overflow_sectors, options,
applicationName ? (const char *)applicationName : "NULL", applicationNameLength, applicationMajorVersion, application_name ? (const char *)application_name : "NULL", application_name_length,
applicationMinorVersion); application_major_version, application_minor_version);
// Parse the options // Parse the options
TRACE("Parsing options"); TRACE("Parsing options");
aaru_options parsedOptions = parse_options(options); aaru_options parsed_options = parse_options(options);
// Allocate context // Allocate context
TRACE("Allocating memory for context"); TRACE("Allocating memory for context");
@@ -86,9 +86,9 @@ void *aaruf_create(const char *filepath, uint32_t mediaType, uint32_t sectorSize
return NULL; 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); AARU_HEADER_APP_NAME_LEN);
free(ctx); free(ctx);
errno = AARUF_ERROR_INVALID_APP_NAME_LENGTH; 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 // Initialize header
TRACE("Initializing header"); TRACE("Initializing header");
ctx->header.identifier = AARU_MAGIC; 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.imageMajorVersion = AARUF_VERSION_V2;
ctx->header.imageMinorVersion = 0; ctx->header.imageMinorVersion = 0;
ctx->header.applicationMajorVersion = applicationMajorVersion; ctx->header.applicationMajorVersion = application_major_version;
ctx->header.applicationMinorVersion = applicationMinorVersion; ctx->header.applicationMinorVersion = application_minor_version;
ctx->header.mediaType = mediaType; ctx->header.mediaType = media_type;
ctx->header.indexOffset = 0; ctx->header.indexOffset = 0;
ctx->header.creationTime = get_filetime_uint64(); ctx->header.creationTime = get_filetime_uint64();
ctx->header.lastWrittenTime = 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.CreationTime = ctx->header.creationTime;
ctx->imageInfo.LastModificationTime = ctx->header.lastWrittenTime; ctx->imageInfo.LastModificationTime = ctx->header.lastWrittenTime;
ctx->imageInfo.XmlMediaType = aaruf_get_xml_mediatype(ctx->header.mediaType); ctx->imageInfo.XmlMediaType = aaruf_get_xml_mediatype(ctx->header.mediaType);
ctx->imageInfo.SectorSize = sectorSize; ctx->imageInfo.SectorSize = sector_size;
// Initialize caches // Initialize caches
TRACE("Initializing 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.levels = 2;
ctx->userDataDdtHeader.tableLevel = 0; ctx->userDataDdtHeader.tableLevel = 0;
ctx->userDataDdtHeader.previousLevelOffset = 0; ctx->userDataDdtHeader.previousLevelOffset = 0;
ctx->userDataDdtHeader.negative = negativeSectors; ctx->userDataDdtHeader.negative = negative_sectors;
ctx->userDataDdtHeader.blocks = userSectors + overflowSectors + negativeSectors; ctx->userDataDdtHeader.blocks = user_sectors + overflow_sectors + negative_sectors;
ctx->userDataDdtHeader.overflow = overflowSectors; ctx->userDataDdtHeader.overflow = overflow_sectors;
ctx->userDataDdtHeader.start = 0; ctx->userDataDdtHeader.start = 0;
ctx->userDataDdtHeader.blockAlignmentShift = parsedOptions.block_alignment; ctx->userDataDdtHeader.blockAlignmentShift = parsed_options.block_alignment;
ctx->userDataDdtHeader.dataShift = parsedOptions.data_shift; ctx->userDataDdtHeader.dataShift = parsed_options.data_shift;
ctx->userDataDdtHeader.tableShift = parsedOptions.table_shift; ctx->userDataDdtHeader.tableShift = parsed_options.table_shift;
ctx->userDataDdtHeader.sizeType = 1; ctx->userDataDdtHeader.sizeType = 1;
ctx->userDataDdtHeader.entries = ctx->userDataDdtHeader.blocks / (1 << ctx->userDataDdtHeader.tableShift); ctx->userDataDdtHeader.entries = ctx->userDataDdtHeader.blocks / (1 << ctx->userDataDdtHeader.tableShift);

View File

@@ -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. * @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. * @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; int pos = 0;
size_t readBytes = 0; size_t read_bytes = 0;
DdtHeader ddtHeader; DdtHeader ddt_header;
uint8_t *cmpData = NULL; uint8_t *cmp_data = NULL;
uint32_t *cdDdt = NULL; uint32_t *cd_ddt = NULL;
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH]; uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
size_t lzmaSize = 0; size_t lzma_size = 0;
int errorNo = 0; int error_no = 0;
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) 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 // Even if those two checks shall have been done before
TRACE("Reading DDT block header at position %" PRIu64, entry->offset); 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); 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; 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) if(entry->dataType == UserData)
{ {
ctx->imageInfo.Sectors = ddtHeader.entries; ctx->imageInfo.Sectors = ddt_header.entries;
ctx->shift = ddtHeader.shift; ctx->shift = ddt_header.shift;
ctx->ddtVersion = 1; ctx->ddtVersion = 1;
// Check for DDT compression // Check for DDT compression
switch(ddtHeader.compression) switch(ddt_header.compression)
{ {
// TODO: Check CRC // TODO: Check CRC
case Lzma: case Lzma:
lzmaSize = ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH; lzma_size = ddt_header.cmpLength - LZMA_PROPERTIES_LENGTH;
cmpData = (uint8_t *)malloc(lzmaSize); cmp_data = (uint8_t *)malloc(lzma_size);
if(cmpData == NULL) if(cmp_data == NULL)
{ {
TRACE("Cannot allocate memory for DDT, continuing..."); TRACE("Cannot allocate memory for DDT, continuing...");
break; break;
} }
ctx->userDataDdt = (uint64_t *)malloc(ddtHeader.length); ctx->userDataDdt = (uint64_t *)malloc(ddt_header.length);
if(ctx->userDataDdt == NULL) if(ctx->userDataDdt == NULL)
{ {
TRACE("Cannot allocate memory for DDT, continuing..."); TRACE("Cannot allocate memory for DDT, continuing...");
free(cmpData); free(cmp_data);
break; break;
} }
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)
{ {
TRACE("Could not read LZMA properties, continuing..."); TRACE("Could not read LZMA properties, continuing...");
free(cmpData); free(cmp_data);
free(ctx->userDataDdt); free(ctx->userDataDdt);
ctx->userDataDdt = NULL; ctx->userDataDdt = NULL;
break; break;
} }
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
if(readBytes != lzmaSize) if(read_bytes != lzma_size)
{ {
TRACE("Could not read compressed block, continuing..."); TRACE("Could not read compressed block, continuing...");
free(cmpData); free(cmp_data);
free(ctx->userDataDdt); free(ctx->userDataDdt);
ctx->userDataDdt = NULL; ctx->userDataDdt = NULL;
break; break;
} }
readBytes = ddtHeader.length; read_bytes = ddt_header.length;
TRACE("Decompressing block of size %zu bytes", ddtHeader.length); TRACE("Decompressing block of size %zu bytes", ddt_header.length);
errorNo = aaruf_lzma_decode_buffer((uint8_t *)ctx->userDataDdt, &readBytes, cmpData, &lzmaSize, error_no = aaruf_lzma_decode_buffer((uint8_t *)ctx->userDataDdt, &read_bytes, cmp_data, &lzma_size,
lzmaProperties, LZMA_PROPERTIES_LENGTH); lzma_properties, LZMA_PROPERTIES_LENGTH);
if(errorNo != 0) if(error_no != 0)
{ {
FATAL("Got error %d from LZMA, stopping...", errorNo); FATAL("Got error %d from LZMA, stopping...", error_no);
free(cmpData); free(cmp_data);
free(ctx->userDataDdt); free(ctx->userDataDdt);
ctx->userDataDdt = NULL; ctx->userDataDdt = NULL;
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; 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..."); FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...");
free(cmpData); free(cmp_data);
free(ctx->userDataDdt); free(ctx->userDataDdt);
ctx->userDataDdt = NULL; ctx->userDataDdt = NULL;
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
ctx->inMemoryDdt = true; ctx->inMemoryDdt = true;
*foundUserDataDdt = true; *found_user_data_ddt = true;
break; break;
// TODO: Check CRC // TODO: Check CRC
case None: case None:
#ifdef __linux__ #ifdef __linux__
TRACE("Memory mapping deduplication table at position %" PRIu64, entry->offset + sizeof(ddtHeader)); TRACE("Memory mapping deduplication table at position %" PRIu64, entry->offset + sizeof(ddt_header));
ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddtHeader.entries; ctx->mappedMemoryDdtSize = sizeof(uint64_t) * ddt_header.entries;
ctx->userDataDdt = mmap(NULL, ctx->mappedMemoryDdtSize, PROT_READ, MAP_SHARED, fileno(ctx->imageStream), 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) if(ctx->userDataDdt == MAP_FAILED)
{ {
*foundUserDataDdt = false; *found_user_data_ddt = false;
FATAL("Could not read map deduplication table."); FATAL("Could not read map deduplication table.");
break; break;
} }
@@ -181,115 +181,115 @@ int32_t process_ddt_v1(aaruformatContext *ctx, IndexEntry *entry, bool *foundUse
break; break;
#else // TODO: Implement #else // TODO: Implement
TRACE("Uncompressed DDT not yet implemented..."); TRACE("Uncompressed DDT not yet implemented...");
*foundUserDataDdt = false; *found_user_data_ddt = false;
break; break;
#endif #endif
default: default:
TRACE("Found unknown compression type %d, continuing...", ddtHeader.compression); TRACE("Found unknown compression type %d, continuing...", ddt_header.compression);
*foundUserDataDdt = false; *found_user_data_ddt = false;
break; break;
} }
} }
else if(entry->dataType == CdSectorPrefixCorrected || entry->dataType == CdSectorSuffixCorrected) else if(entry->dataType == CdSectorPrefixCorrected || entry->dataType == CdSectorSuffixCorrected)
{ {
switch(ddtHeader.compression) switch(ddt_header.compression)
{ {
// TODO: Check CRC // TODO: Check CRC
case Lzma: case Lzma:
lzmaSize = ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH; lzma_size = ddt_header.cmpLength - LZMA_PROPERTIES_LENGTH;
cmpData = (uint8_t *)malloc(lzmaSize); cmp_data = (uint8_t *)malloc(lzma_size);
if(cmpData == NULL) if(cmp_data == NULL)
{ {
TRACE("Cannot allocate memory for DDT, continuing..."); TRACE("Cannot allocate memory for DDT, continuing...");
break; break;
} }
cdDdt = (uint32_t *)malloc(ddtHeader.length); cd_ddt = (uint32_t *)malloc(ddt_header.length);
if(cdDdt == NULL) if(cd_ddt == NULL)
{ {
TRACE("Cannot allocate memory for DDT, continuing..."); TRACE("Cannot allocate memory for DDT, continuing...");
free(cmpData); free(cmp_data);
break; break;
} }
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)
{ {
TRACE("Could not read LZMA properties, continuing..."); TRACE("Could not read LZMA properties, continuing...");
free(cmpData); free(cmp_data);
free(cdDdt); free(cd_ddt);
break; break;
} }
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
if(readBytes != lzmaSize) if(read_bytes != lzma_size)
{ {
TRACE("Could not read compressed block, continuing..."); TRACE("Could not read compressed block, continuing...");
free(cmpData); free(cmp_data);
free(cdDdt); free(cd_ddt);
break; break;
} }
readBytes = ddtHeader.length; read_bytes = ddt_header.length;
TRACE("Decompressing block of size %zu bytes", ddtHeader.length); TRACE("Decompressing block of size %zu bytes", ddt_header.length);
errorNo = aaruf_lzma_decode_buffer((uint8_t *)cdDdt, &readBytes, cmpData, &lzmaSize, lzmaProperties, error_no = aaruf_lzma_decode_buffer((uint8_t *)cd_ddt, &read_bytes, cmp_data, &lzma_size,
LZMA_PROPERTIES_LENGTH); lzma_properties, LZMA_PROPERTIES_LENGTH);
if(errorNo != 0) if(error_no != 0)
{ {
FATAL("Got error %d from LZMA, stopping...", errorNo); FATAL("Got error %d from LZMA, stopping...", error_no);
free(cmpData); free(cmp_data);
free(cdDdt); free(cd_ddt);
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; 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..."); FATAL("Error decompressing block, should be {0} bytes but got {1} bytes., stopping...");
free(cmpData); free(cmp_data);
free(cdDdt); free(cd_ddt);
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
if(entry->dataType == CdSectorPrefixCorrected) if(entry->dataType == CdSectorPrefixCorrected)
ctx->sectorPrefixDdt = cdDdt; ctx->sectorPrefixDdt = cd_ddt;
else if(entry->dataType == CdSectorSuffixCorrected) else if(entry->dataType == CdSectorSuffixCorrected)
ctx->sectorSuffixDdt = cdDdt; ctx->sectorSuffixDdt = cd_ddt;
else else
free(cdDdt); free(cd_ddt);
break; break;
// TODO: Check CRC // TODO: Check CRC
case None: 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."); TRACE("Cannot allocate memory for deduplication table.");
break; 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..."); TRACE("Could not read deduplication table, continuing...");
break; break;
} }
if(entry->dataType == CdSectorPrefixCorrected) if(entry->dataType == CdSectorPrefixCorrected)
ctx->sectorPrefixDdt = cdDdt; ctx->sectorPrefixDdt = cd_ddt;
else if(entry->dataType == CdSectorSuffixCorrected) else if(entry->dataType == CdSectorSuffixCorrected)
ctx->sectorSuffixDdt = cdDdt; ctx->sectorSuffixDdt = cd_ddt;
else else
free(cdDdt); free(cd_ddt);
break; break;
default: default:
TRACE("Found unknown compression type %d, continuing...", ddtHeader.compression); TRACE("Found unknown compression type %d, continuing...", ddt_header.compression);
break; 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. * Determines the offset and block offset for a sector using the DDT v1 table.
* *
* @param ctx Pointer to the aaruformat context. * @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 offset Pointer to store the resulting offset.
* @param blockOffset Pointer to store the resulting block offset. * @param block_offset Pointer to store the resulting block offset.
* @param sectorStatus Pointer to store the sector status. * @param sector_status Pointer to store the sector status.
* @return AARUF_STATUS_OK on success, or an error code on failure. * @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, int32_t decode_ddt_entry_v1(aaruformatContext *ctx, uint64_t sector_address, uint64_t *offset, uint64_t *block_offset,
uint8_t *sectorStatus) uint8_t *sector_status)
{ {
TRACE("Entering decode_ddt_entry_v1(%p, %" PRIu64 ", %llu, %llu, %d)", ctx, sectorAddress, *offset, *blockOffset, TRACE("Entering decode_ddt_entry_v1(%p, %" PRIu64 ", %p, %p, %p)", ctx, sector_address, offset, block_offset,
*sectorStatus); sector_status);
// Check if the context and image stream are valid // Check if the context and image stream are valid
if(ctx == NULL || ctx->imageStream == NULL) if(ctx == NULL || ctx->imageStream == NULL)
{ {
FATAL("Invalid context or image stream."); FATAL("Invalid context or image stream.");
TRACE("Exiting decode_ddt_entry_v1() = AARUF_ERROR_NOT_AARUFORMAT");
return AARUF_ERROR_NOT_AARUFORMAT; return AARUF_ERROR_NOT_AARUFORMAT;
} }
const uint64_t ddtEntry = ctx->userDataDdt[sectorAddress]; const uint64_t ddt_entry = ctx->userDataDdt[sector_address];
const uint32_t offsetMask = (uint32_t)((1 << ctx->shift) - 1); const uint32_t offset_mask = (uint32_t)((1 << ctx->shift) - 1);
*offset = ddtEntry & offsetMask; *offset = ddt_entry & offset_mask;
*blockOffset = ddtEntry >> ctx->shift; *block_offset = ddt_entry >> ctx->shift;
// Partially written image... as we can't know the real sector size just assume it's common :/ // Partially written image... as we can't know the real sector size just assume it's common :/
if(ddtEntry == 0) if(ddt_entry == 0)
*sectorStatus = SectorStatusNotDumped; *sector_status = SectorStatusNotDumped;
else else
*sectorStatus = SectorStatusDumped; *sector_status = SectorStatusDumped;
TRACE("Exiting decode_ddt_entry_v1(%p, %" PRIu64 ", %llu, %llu, %d) = AARUF_STATUS_OK", ctx, sectorAddress, *offset,
*blockOffset, *sectorStatus);
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; return AARUF_STATUS_OK;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -49,16 +49,16 @@ int aaruf_identify(const char *filename)
* *
* Determines if the provided stream is an AaruFormat image. * 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. * @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; 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; if(ret != 1) return 0;

View File

@@ -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 char *char_key = malloc(17); // 16 hex digits + null terminator
if(!charKey) return NULL; if(!char_key) return NULL;
snprintf(charKey, 17, "%016" PRIX64, number); snprintf(char_key, 17, "%016" PRIX64, number);
return charKey; 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) 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) 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);
} }

View File

@@ -39,8 +39,8 @@
void *aaruf_open(const char *filepath) void *aaruf_open(const char *filepath)
{ {
aaruformatContext *ctx = NULL; aaruformatContext *ctx = NULL;
int errorNo = 0; int error_no = 0;
size_t readBytes = 0; size_t read_bytes = 0;
long pos = 0; long pos = 0;
int i = 0; int i = 0;
uint32_t signature = 0; uint32_t signature = 0;
@@ -75,9 +75,9 @@ void *aaruf_open(const char *filepath)
if(ctx->imageStream == NULL) if(ctx->imageStream == NULL)
{ {
FATAL("Error %d opening file %s for reading", errno, filepath); FATAL("Error %d opening file %s for reading", errno, filepath);
errorNo = errno; error_no = errno;
free(ctx); free(ctx);
errno = errorNo; errno = error_no;
TRACE("Exiting aaruf_open() = NULL"); TRACE("Exiting aaruf_open() = NULL");
return NULL; return NULL;
@@ -85,9 +85,9 @@ void *aaruf_open(const char *filepath)
TRACE("Reading header at position 0"); TRACE("Reading header at position 0");
fseek(ctx->imageStream, 0, SEEK_SET); 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"); FATAL("Could not read header");
free(ctx); free(ctx);
@@ -112,9 +112,9 @@ void *aaruf_open(const char *filepath)
{ {
TRACE("Reading new header version at position 0"); TRACE("Reading new header version at position 0");
fseek(ctx->imageStream, 0, SEEK_SET); 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); free(ctx);
errno = AARUF_ERROR_FILE_TOO_SMALL; errno = AARUF_ERROR_FILE_TOO_SMALL;
@@ -188,9 +188,9 @@ void *aaruf_open(const char *filepath)
return NULL; 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)) (signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3))
{ {
FATAL("Could not read index header or incorrect identifier %4.4s", (char *)&signature); 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); entry->dataType, entry->offset);
} }
bool foundUserDataDdt = false; bool found_user_data_ddt = false;
ctx->imageInfo.ImageSize = 0; ctx->imageInfo.ImageSize = 0;
for(i = 0; i < utarray_len(index_entries); i++) for(i = 0; i < utarray_len(index_entries); i++)
{ {
@@ -247,13 +247,13 @@ void *aaruf_open(const char *filepath)
switch(entry->blockType) switch(entry->blockType)
{ {
case DataBlock: 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); utarray_free(index_entries);
free(ctx); free(ctx);
errno = errorNo; errno = error_no;
return NULL; return NULL;
} }
@@ -261,26 +261,26 @@ void *aaruf_open(const char *filepath)
break; break;
case DeDuplicationTable: 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); utarray_free(index_entries);
free(ctx); free(ctx);
errno = errorNo; errno = error_no;
return NULL; return NULL;
} }
break; break;
case DeDuplicationTable2: 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); utarray_free(index_entries);
free(ctx); free(ctx);
errno = errorNo; errno = error_no;
return NULL; return NULL;
} }
@@ -320,7 +320,7 @@ void *aaruf_open(const char *filepath)
utarray_free(index_entries); utarray_free(index_entries);
if(!foundUserDataDdt) if(!found_user_data_ddt)
{ {
FATAL("Could not find user data deduplication table, aborting..."); FATAL("Could not find user data deduplication table, aborting...");
aaruf_close(ctx); aaruf_close(ctx);

View File

@@ -88,21 +88,21 @@ int32_t aaruf_read_media_tag(void *context, uint8_t *data, int32_t tag, uint32_t
return AARUF_STATUS_OK; 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; aaruformatContext *ctx = NULL;
uint64_t offset = 0; uint64_t offset = 0;
uint64_t blockOffset = 0; uint64_t block_offset = 0;
BlockHeader *blockHeader = NULL; BlockHeader *block_header = NULL;
uint8_t *block = NULL; uint8_t *block = NULL;
size_t readBytes = 0; size_t read_bytes = 0;
uint8_t lzmaProperties[LZMA_PROPERTIES_LENGTH]; uint8_t lzma_properties[LZMA_PROPERTIES_LENGTH];
size_t lzmaSize = 0; size_t lzma_size = 0;
uint8_t *cmpData = NULL; uint8_t *cmp_data = NULL;
int errorNo = 0; int error_no = 0;
uint8_t sectorStatus = 0; uint8_t sector_status = 0;
if(context == NULL) 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; return AARUF_ERROR_NOT_AARUFORMAT;
} }
if(sectorAddress > ctx->imageInfo.Sectors - 1) if(sector_address > ctx->imageInfo.Sectors - 1)
{ {
FATAL("Sector address out of bounds"); 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) if(ctx->ddtVersion == 1)
errorNo = decode_ddt_entry_v1(ctx, sectorAddress, &offset, &blockOffset, &sectorStatus); error_no = decode_ddt_entry_v1(ctx, sector_address, &offset, &block_offset, &sector_status);
else if(ctx->ddtVersion == 2) else if(ctx->ddtVersion == 2)
errorNo = decode_ddt_entry_v2(ctx, sectorAddress, &offset, &blockOffset, &sectorStatus); error_no = decode_ddt_entry_v2(ctx, sector_address, &offset, &block_offset, &sector_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); TRACE("Exiting aaruf_read_sector() = %d", error_no);
return errorNo; return error_no;
} }
// Partially written image... as we can't know the real sector size just assume it's common :/ // 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; *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 // Check if block header is cached
TRACE("Checking 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 // Read block header
if(blockHeader == NULL) if(block_header == NULL)
{ {
TRACE("Allocating memory for block header"); TRACE("Allocating memory for block header");
blockHeader = malloc(sizeof(BlockHeader)); block_header = malloc(sizeof(BlockHeader));
if(blockHeader == NULL) if(block_header == NULL)
{ {
FATAL("Not enough memory for block header"); 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"); TRACE("Reading block header");
fseek(ctx->imageStream, blockOffset, SEEK_SET); fseek(ctx->imageStream, block_offset, SEEK_SET);
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("Error reading block header"); 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"); 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 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); TRACE("Buffer too small for sector, required %u bytes", block_header->sectorSize);
*length = blockHeader->sectorSize; *length = block_header->sectorSize;
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_BUFFER_TOO_SMALL"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_BUFFER_TOO_SMALL");
return 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 // Check if block is cached
TRACE("Checking 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) if(block != NULL)
{ {
TRACE("Getting data from cache"); TRACE("Getting data from cache");
memcpy(data, block + offset * blockHeader->sectorSize, blockHeader->sectorSize); memcpy(data, block + offset * block_header->sectorSize, block_header->sectorSize);
*length = blockHeader->sectorSize; *length = block_header->sectorSize;
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK"); TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;
} }
// Decompress block // Decompress block
switch(blockHeader->compression) switch(block_header->compression)
{ {
case None: case None:
TRACE("Allocating memory for block"); TRACE("Allocating memory for block");
block = (uint8_t *)malloc(blockHeader->length); block = (uint8_t *)malloc(block_header->length);
if(block == NULL) if(block == NULL)
{ {
FATAL("Not enough memory for block"); 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"); 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"); FATAL("Could not read block");
free(block); free(block);
@@ -239,11 +239,11 @@ int32_t aaruf_read_sector(void *context, uint64_t sectorAddress, uint8_t *data,
break; break;
case Lzma: case Lzma:
lzmaSize = blockHeader->cmpLength - LZMA_PROPERTIES_LENGTH; lzma_size = block_header->cmpLength - LZMA_PROPERTIES_LENGTH;
TRACE("Allocating memory for compressed data of size %zu bytes", lzmaSize); TRACE("Allocating memory for compressed data of size %zu bytes", lzma_size);
cmpData = malloc(lzmaSize); cmp_data = malloc(lzma_size);
if(cmpData == NULL) if(cmp_data == NULL)
{ {
FATAL("Cannot allocate memory for block..."); 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; return AARUF_ERROR_NOT_ENOUGH_MEMORY;
} }
TRACE("Allocating memory for block of size %zu bytes", blockHeader->length); TRACE("Allocating memory for block of size %zu bytes", block_header->length);
block = malloc(blockHeader->length); block = malloc(block_header->length);
if(block == NULL) if(block == NULL)
{ {
FATAL("Cannot allocate memory for block..."); FATAL("Cannot allocate memory for block...");
free(cmpData); free(cmp_data);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
return 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..."); FATAL("Could not read LZMA properties...");
free(block); free(block);
free(cmpData); free(cmp_data);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
readBytes = fread(cmpData, 1, lzmaSize, ctx->imageStream); read_bytes = fread(cmp_data, 1, lzma_size, ctx->imageStream);
if(readBytes != lzmaSize) if(read_bytes != lzma_size)
{ {
FATAL("Could not read compressed block..."); FATAL("Could not read compressed block...");
free(cmpData); free(cmp_data);
free(block); free(block);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
TRACE("Decompressing block of size %zu bytes", blockHeader->length); TRACE("Decompressing block of size %zu bytes", block_header->length);
readBytes = blockHeader->length; read_bytes = block_header->length;
errorNo = error_no = aaruf_lzma_decode_buffer(block, &read_bytes, cmp_data, &lzma_size, lzma_properties,
aaruf_lzma_decode_buffer(block, &readBytes, cmpData, &lzmaSize, lzmaProperties, LZMA_PROPERTIES_LENGTH); LZMA_PROPERTIES_LENGTH);
if(errorNo != 0) if(error_no != 0)
{ {
FATAL("Got error %d from LZMA...", errorNo); FATAL("Got error %d from LZMA...", error_no);
free(cmpData); free(cmp_data);
free(block); free(block);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return 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..."); FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
free(cmpData); free(cmp_data);
free(block); free(block);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
free(cmpData); free(cmp_data);
break; break;
case Flac: case Flac:
TRACE("Allocating memory for compressed data of size %zu bytes", blockHeader->cmpLength); TRACE("Allocating memory for compressed data of size %zu bytes", block_header->cmpLength);
cmpData = malloc(blockHeader->cmpLength); cmp_data = malloc(block_header->cmpLength);
if(cmpData == NULL) if(cmp_data == NULL)
{ {
FATAL("Cannot allocate memory for block..."); 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; return AARUF_ERROR_NOT_ENOUGH_MEMORY;
} }
TRACE("Allocating memory for block of size %zu bytes", blockHeader->length); TRACE("Allocating memory for block of size %zu bytes", block_header->length);
block = malloc(blockHeader->length); block = malloc(block_header->length);
if(block == NULL) if(block == NULL)
{ {
FATAL("Cannot allocate memory for block..."); FATAL("Cannot allocate memory for block...");
free(cmpData); free(cmp_data);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_NOT_ENOUGH_MEMORY");
return AARUF_ERROR_NOT_ENOUGH_MEMORY; return AARUF_ERROR_NOT_ENOUGH_MEMORY;
} }
TRACE("Reading compressed data into memory"); TRACE("Reading compressed data into memory");
readBytes = fread(cmpData, 1, blockHeader->cmpLength, ctx->imageStream); read_bytes = fread(cmp_data, 1, block_header->cmpLength, ctx->imageStream);
if(readBytes != blockHeader->cmpLength) if(read_bytes != block_header->cmpLength)
{ {
FATAL("Could not read compressed block..."); FATAL("Could not read compressed block...");
free(cmpData); free(cmp_data);
free(block); free(block);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
TRACE("Decompressing block of size %zu bytes", blockHeader->length); TRACE("Decompressing block of size %zu bytes", block_header->length);
readBytes = aaruf_flac_decode_redbook_buffer(block, blockHeader->length, cmpData, blockHeader->cmpLength); 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..."); FATAL("Error decompressing block, should be {0} bytes but got {1} bytes...");
free(cmpData); free(cmp_data);
free(block); free(block);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK");
return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK; return AARUF_ERROR_CANNOT_DECOMPRESS_BLOCK;
} }
free(cmpData); free(cmp_data);
break; break;
default: default:
FATAL("Unsupported compression %d", blockHeader->compression); FATAL("Unsupported compression %d", block_header->compression);
TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_UNSUPPORTED_COMPRESSION"); TRACE("Exiting aaruf_read_sector() = AARUF_ERROR_UNSUPPORTED_COMPRESSION");
return AARUF_ERROR_UNSUPPORTED_COMPRESSION; return AARUF_ERROR_UNSUPPORTED_COMPRESSION;
} }
// Add block to cache // Add block to cache
TRACE("Adding 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); memcpy(data, block + (offset * block_header->sectorSize), block_header->sectorSize);
*length = blockHeader->sectorSize; *length = block_header->sectorSize;
TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK"); TRACE("Exiting aaruf_read_sector() = AARUF_STATUS_OK");
return AARUF_STATUS_OK; return AARUF_STATUS_OK;

View File

@@ -37,7 +37,8 @@ uint64_t get_filetime_uint64()
/** /**
* @brief Gets the current time as a 64-bit FILETIME value. * @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. * @return The current time as a 64-bit FILETIME value.
*/ */
@@ -46,9 +47,8 @@ uint64_t get_filetime_uint64()
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); // seconds + microseconds since 1970 gettimeofday(&tv, NULL); // seconds + microseconds since 1970
const uint64_t EPOCH_DIFF = 11644473600ULL; // seconds between 1601 and 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; uint64_t ft = (tv.tv_sec + epoch_diff) * 10000000ULL + tv.tv_usec * 10;
return ft; return ft;
} }
#endif #endif

View File

@@ -32,16 +32,16 @@
* Writes the given data to the specified sector address in the image, with the given status and length. * 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 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 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. * @param length Length of the data buffer.
* @return AARUF_STATUS_OK on success, or an error code on failure. * @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) 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); length);
// Check context is correct AaruFormat context // 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 // No block set
if(ctx->writingBufferPosition == 0) 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; ctx->currentBlockHeader.sectorSize = length;
// TODO: Optical discs // TODO: Optical discs
uint32_t maxBufferSize = (1 << ctx->userDataDdtHeader.dataShift) * ctx->currentBlockHeader.sectorSize; uint32_t max_buffer_size = (1 << ctx->userDataDdtHeader.dataShift) * ctx->currentBlockHeader.sectorSize;
TRACE("Setting max buffer size to %u bytes", maxBufferSize); TRACE("Setting max buffer size to %u bytes", max_buffer_size);
TRACE("Allocating memory for writing buffer"); 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) if(ctx->writingBuffer == NULL)
{ {
FATAL("Could not allocate memory"); FATAL("Could not allocate memory");
@@ -160,13 +160,13 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx)
// Add to index // Add to index
TRACE("Adding block to index"); TRACE("Adding block to index");
IndexEntry indexEntry; IndexEntry index_entry;
indexEntry.blockType = DataBlock; index_entry.blockType = DataBlock;
indexEntry.dataType = UserData; index_entry.dataType = UserData;
indexEntry.offset = ctx->nextBlockPosition; index_entry.offset = ctx->nextBlockPosition;
utarray_push_back(ctx->indexEntries, &indexEntry); utarray_push_back(ctx->indexEntries, &index_entry);
TRACE("Block added to index at offset %" PRIu64, indexEntry.offset); TRACE("Block added to index at offset %" PRIu64, index_entry.offset);
// Write block header to file // Write block header to file
@@ -182,9 +182,9 @@ int32_t aaruf_close_current_block(aaruformatContext *ctx)
return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA; return AARUF_ERROR_CANNOT_WRITE_BLOCK_DATA;
// Update nextBlockPosition to point to the next available aligned position // Update nextBlockPosition to point to the next available aligned position
uint64_t blockTotalSize = sizeof(BlockHeader) + ctx->currentBlockHeader.cmpLength; uint64_t block_total_size = sizeof(BlockHeader) + ctx->currentBlockHeader.cmpLength;
uint64_t alignmentMask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1; uint64_t alignment_mask = (1ULL << ctx->userDataDdtHeader.blockAlignmentShift) - 1;
ctx->nextBlockPosition = (ctx->nextBlockPosition + blockTotalSize + alignmentMask) & ~alignmentMask; ctx->nextBlockPosition = (ctx->nextBlockPosition + block_total_size + alignment_mask) & ~alignment_mask;
TRACE("Updated nextBlockPosition to %" PRIu64, ctx->nextBlockPosition); TRACE("Updated nextBlockPosition to %" PRIu64, ctx->nextBlockPosition);
// Clear values // Clear values

View File

@@ -83,7 +83,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
edc = 0; edc = 0;
size = 0x810; size = 0x810;
pos = 0; 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; calculatedEdc = edc;
*edc_correct = calculatedEdc == storedEdc; *edc_correct = calculatedEdc == storedEdc;
@@ -128,7 +128,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
edc = 0; edc = 0;
size = 0x808; size = 0x808;
pos = 0x10; 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; calculatedEdc = edc;
*edc_correct = calculatedEdc == storedEdc; *edc_correct = calculatedEdc == storedEdc;
@@ -151,7 +151,7 @@ bool check_cd_sector_channel(CdEccContext *context, uint8_t *sector, bool *unkno
edc = 0; edc = 0;
size = 0x808; size = 0x808;
pos = 0x10; 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; calculatedEdc = edc;
*edc_correct = calculatedEdc == storedEdc; *edc_correct = calculatedEdc == storedEdc;