12 Commits

8 changed files with 64 additions and 56 deletions

View File

@@ -287,9 +287,9 @@ endif()
# Check for math library
include(CheckLibraryExists)
check_library_exists(m log "" HAVE_LIB_M)
check_library_exists(m cos "" HAVE_LIB_M)
if(HAVE_LIB_M)
TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE(aaruformat m)
target_link_libraries(aaruformat m)
endif()
# Find Doxygen for documentation generation

View File

@@ -292,6 +292,7 @@ typedef struct aaruformat_context
bool is_writing; ///< True if context opened/created for writing.
bool rewinded; ///< True if stream has been rewound after open (write path).
bool writing_long; ///< True if writing long sectors
bool block_zero_written; ///< True if block zero has been written (writing path).
/* Options */
uint32_t lzma_dict_size; ///< LZMA dictionary size (writing path).

View File

@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>libaaruformat</id>
<version>1.0.0-alpha.3</version>
<version>1.0.0-alpha.7</version>
<description>Library for management of AaruFormat images.</description>
<authors>claunia</authors>
<projectUrl>https://github.com/aaru-dps/libaaruformat</projectUrl>

View File

@@ -61,19 +61,19 @@ static void reset_dump_hardware_context(aaruformat_context *ctx)
memset(&ctx->dump_hardware_header, 0, sizeof(ctx->dump_hardware_header));
}
static bool read_dump_string(FILE *stream, const char *field_name, uint32_t length, uint32_t *remaining,
static bool read_dump_string(FILE *stream, const char *field_name, const uint32_t length, uint32_t *remaining,
uint8_t **destination)
{
if(length == 0) return true;
if(remaining == NULL || *remaining < length)
if(*remaining < length)
{
TRACE("Dump hardware %s length %u exceeds remaining payload %u", field_name, length,
remaining == NULL ? 0 : *remaining);
return false;
}
uint8_t *buffer = (uint8_t *)malloc((size_t)length + 1);
uint8_t *buffer = malloc(length);
if(buffer == NULL)
{
@@ -81,7 +81,7 @@ static bool read_dump_string(FILE *stream, const char *field_name, uint32_t leng
return false;
}
size_t bytes_read = fread(buffer, 1, length, stream);
const size_t bytes_read = fread(buffer, 1, length, stream);
if(bytes_read != length)
{
@@ -90,7 +90,6 @@ static bool read_dump_string(FILE *stream, const char *field_name, uint32_t leng
return false;
}
buffer[length] = 0;
*remaining -= length;
*destination = buffer;
@@ -125,7 +124,7 @@ void process_dumphw_block(aaruformat_context *ctx, const IndexEntry *entry)
return;
}
if(fseek(ctx->imageStream, (long)entry->offset, SEEK_SET) < 0 || ftell(ctx->imageStream) != entry->offset)
if(fseek(ctx->imageStream, entry->offset, SEEK_SET) < 0 || ftell(ctx->imageStream) != entry->offset)
{
FATAL("Could not seek to %" PRIu64 " as indicated by index entry...", entry->offset);
reset_dump_hardware_context(ctx);
@@ -166,7 +165,7 @@ void process_dumphw_block(aaruformat_context *ctx, const IndexEntry *entry)
if(payload_length > 0)
{
uint8_t *payload = (uint8_t *)malloc(payload_length);
uint8_t *payload = malloc(payload_length);
if(payload == NULL)
{
@@ -230,8 +229,7 @@ void process_dumphw_block(aaruformat_context *ctx, const IndexEntry *entry)
return;
}
DumpHardwareEntriesWithData *entries =
(DumpHardwareEntriesWithData *)calloc(header.entries, sizeof(DumpHardwareEntriesWithData));
DumpHardwareEntriesWithData *entries = calloc(header.entries, sizeof(DumpHardwareEntriesWithData));
if(entries == NULL)
{

View File

@@ -1645,6 +1645,8 @@ static void write_sector_subchannel(const aaruformat_context *ctx)
else
subchannel_block.cmpCrc64 = aaruf_crc64_data(buffer, subchannel_block.cmpLength);
if(subchannel_block.compression != None) subchannel_block.cmpLength += LZMA_PROPERTIES_LENGTH;
// Write header
if(fwrite(&subchannel_block, sizeof(BlockHeader), 1, ctx->imageStream) == 1)
{
@@ -3456,7 +3458,7 @@ static void write_dumphw_block(aaruformat_context *ctx)
if(buffer == NULL) return;
// Start to iterate and copy the data
size_t offset = 0;
size_t offset = sizeof(DumpHardwareHeader);
for(int i = 0; i < ctx->dump_hardware_header.entries; i++)
{
size_t entry_size = sizeof(DumpHardwareEntry) +

View File

@@ -249,7 +249,7 @@ AARU_EXPORT int32_t AARU_CALL aaruf_get_dumphw(void *context, uint8_t *buffer, s
*length = required_length;
// Start to iterate and copy the data
size_t offset = 0;
size_t offset = sizeof(DumpHardwareHeader);
for(uint32_t i = 0; i < ctx->dump_hardware_header.entries; i++)
{
size_t entry_size = sizeof(DumpHardwareEntry);
@@ -620,17 +620,20 @@ AARU_EXPORT int32_t AARU_CALL aaruf_set_dumphw(void *context, uint8_t *data, siz
size_t pos = sizeof(DumpHardwareHeader);
#define COPY_STRING_FIELD(field) \
do { \
const size_t field##_length = copy[e].entry.field##Length; \
if(field##_length > 0) \
{ \
if(field##_length > length - pos) goto invalid_data; \
copy[e].field = (uint8_t *)calloc(1, field##_length + 1); \
if(copy[e].field == NULL) goto free_copy_and_error; \
memcpy(copy[e].field, data + pos, field##_length); \
pos += field##_length; \
} \
#define COPY_STRING_FIELD(field) \
do { \
const size_t field##_length = copy[e].entry.field##Length; \
if(field##_length > 0) \
{ \
if(field##_length > length - pos) goto invalid_data; \
/* Allocate only field##_length bytes, since input is NUL-terminated */ \
copy[e].field = (uint8_t *)calloc(1, field##_length); \
if(copy[e].field == NULL) goto free_copy_and_error; \
memcpy(copy[e].field, data + pos, field##_length); \
/* Ensure NUL-termination in case input is malformed */ \
copy[e].field[field##_length - 1] = '\0'; \
pos += field##_length; \
} \
} while(0)
for(uint32_t e = 0; e < header.entries; e++)

View File

@@ -150,19 +150,24 @@ AARU_EXPORT int32_t AARU_CALL aaruf_write_sector(void *context, uint64_t sector_
{
if(sector_address <= ctx->last_written_block)
{
TRACE("Rewinded");
ctx->rewinded = true;
if(sector_address == 0 && !ctx->block_zero_written)
ctx->block_zero_written = true;
else
{
TRACE("Rewinded");
ctx->rewinded = true;
// Disable MD5 calculation
if(ctx->calculating_md5) ctx->calculating_md5 = false;
// Disable SHA1 calculation
if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
// Disable SHA256 calculation
if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
// Disable SpamSum calculation
if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
// Disable BLAKE3 calculation
if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
// Disable MD5 calculation
if(ctx->calculating_md5) ctx->calculating_md5 = false;
// Disable SHA1 calculation
if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
// Disable SHA256 calculation
if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
// Disable SpamSum calculation
if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
// Disable BLAKE3 calculation
if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
}
}
else
ctx->last_written_block = sector_address;
@@ -642,19 +647,24 @@ AARU_EXPORT int32_t AARU_CALL aaruf_write_sector_long(void *context, uint64_t se
{
if(sector_address <= ctx->last_written_block)
{
TRACE("Rewinded");
ctx->rewinded = true;
if(sector_address == 0 && !ctx->block_zero_written)
ctx->block_zero_written = true;
else
{
TRACE("Rewinded");
ctx->rewinded = true;
// Disable MD5 calculation
if(ctx->calculating_md5) ctx->calculating_md5 = false;
// Disable SHA1 calculation
if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
// Disable SHA256 calculation
if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
// Disable SpamSum calculation
if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
// Disable BLAKE3 calculation
if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
// Disable MD5 calculation
if(ctx->calculating_md5) ctx->calculating_md5 = false;
// Disable SHA1 calculation
if(ctx->calculating_sha1) ctx->calculating_sha1 = false;
// Disable SHA256 calculation
if(ctx->calculating_sha256) ctx->calculating_sha256 = false;
// Disable SpamSum calculation
if(ctx->calculating_spamsum) ctx->calculating_spamsum = false;
// Disable BLAKE3 calculation
if(ctx->calculating_blake3) ctx->calculating_blake3 = false;
}
}
else
ctx->last_written_block = sector_address;

View File

@@ -89,13 +89,7 @@ int info(const char *path)
printf("Library version: %d.%d\n", ctx->library_major_version, ctx->library_minor_version);
printf("AaruFormat header:\n");
printf("\tIdentifier: %8.8s\n", (char *)&ctx->header.identifier);
strBuffer = malloc(65);
memset(strBuffer, 0, 65);
ucnv_convert(NULL, "UTF-16LE", strBuffer, 64, ctx->header.application, 64, &u_error_code);
if(u_error_code == U_ZERO_ERROR) printf("\tApplication: %s\n", strBuffer);
free(strBuffer);
printf("\tApplication: %s\n", ctx->header.application);
printf("\tApplication version: %d.%d\n", ctx->header.applicationMajorVersion, ctx->header.applicationMinorVersion);
printf("\tImage format version: %d.%d\n", ctx->header.imageMajorVersion, ctx->header.imageMinorVersion);
printf("\tMedia type: %u (%s)\n", ctx->header.mediaType, media_type_to_string(ctx->header.mediaType));