Refactor variable declarations for improved clarity and consistency in multiple source files

This commit is contained in:
2025-10-01 02:39:37 +01:00
parent a7434322ea
commit a1b3b5db92
10 changed files with 97 additions and 141 deletions

View File

@@ -78,7 +78,7 @@ void *aaruf_ecc_cd_init()
for(i = 0; i < 256; i++)
{
edc = i;
j = (uint32_t)((i << 1) ^ ((i & 0x80) == 0x80 ? 0x11D : 0));
j = i << 1 ^ ((i & 0x80) == 0x80 ? 0x11D : 0);
context->ecc_f_table[i] = (uint8_t)j;
context->ecc_b_table[i ^ j] = (uint8_t)i;
for(j = 0; j < 8; j++) edc = (edc >> 1) ^ ((edc & 1) > 0 ? 0xD8018001 : 0);
@@ -101,9 +101,8 @@ void *aaruf_ecc_cd_init()
bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
{
TRACE("Entering aaruf_ecc_cd_is_suffix_correct(%p, %p)", context, sector);
CdEccContext *ctx;
uint32_t stored_edc, edc, calculated_edc;
int size, pos;
uint32_t edc;
int size, pos;
if(context == NULL || sector == NULL)
{
@@ -111,7 +110,7 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
return false;
}
ctx = (CdEccContext *)context;
const CdEccContext *ctx = context;
if(!ctx->inited_edc)
{
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct() without initialized context");
@@ -142,9 +141,9 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
return false;
}
stored_edc = (uint32_t)sector[0x808] | (uint32_t)sector[0x809] << 8 | (uint32_t)sector[0x80A] << 16 |
(uint32_t)sector[0x80B] << 24;
calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
uint32_t stored_edc = (uint32_t)sector[0x808] | (uint32_t)sector[0x809] << 8 | (uint32_t)sector[0x80A] << 16 |
(uint32_t)sector[0x80B] << 24;
uint32_t calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
if(stored_edc != calculated_edc)
{
@@ -166,10 +165,9 @@ bool aaruf_ecc_cd_is_suffix_correct(void *context, const uint8_t *sector)
bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
{
TRACE("Entering aaruf_ecc_cd_is_suffix_correct_mode2(%p, %p)", context, sector);
CdEccContext *ctx;
uint32_t stored_edc, edc, calculated_edc;
int size, pos;
uint8_t zeroaddress[4];
uint32_t edc;
int size, pos;
uint8_t zeroaddress[4];
if(context == NULL || sector == NULL)
{
@@ -177,7 +175,7 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
return false;
}
ctx = (CdEccContext *)context;
CdEccContext *ctx = context;
if(!ctx->inited_edc)
{
@@ -201,9 +199,9 @@ bool aaruf_ecc_cd_is_suffix_correct_mode2(void *context, const uint8_t *sector)
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = false");
return false;
}
stored_edc = (uint32_t)sector[0x92C] | (uint32_t)sector[0x92D] << 8 | (uint32_t)sector[0x92E] << 16 |
(uint32_t)sector[0x92F] << 24;
calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
uint32_t stored_edc = (uint32_t)sector[0x92C] | (uint32_t)sector[0x92D] << 8 | (uint32_t)sector[0x92E] << 16 |
(uint32_t)sector[0x92F] << 24;
uint32_t calculated_edc = aaruf_edc_cd_compute(context, 0, sector + 16, size, pos);
TRACE("Exiting aaruf_ecc_cd_is_suffix_correct_mode2() = %u == %u", calculated_edc, stored_edc);
return calculated_edc == stored_edc;
@@ -233,17 +231,13 @@ bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *da
TRACE("Entering aaruf_ecc_cd_check(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
CdEccContext *ctx;
uint32_t size, major, idx, minor;
uint8_t ecc_a, ecc_b, temp;
if(context == NULL || address == NULL || data == NULL || ecc == NULL)
{
TRACE("Exiting aaruf_ecc_cd_check() with missing data");
return false;
}
ctx = (CdEccContext *)context;
CdEccContext *ctx = context;
if(!ctx->inited_edc)
{
@@ -251,15 +245,15 @@ bool aaruf_ecc_cd_check(void *context, const uint8_t *address, const uint8_t *da
return false;
}
size = major_count * minor_count;
for(major = 0; major < major_count; major++)
uint32_t size = major_count * minor_count;
for(uint32_t major = 0; major < major_count; major++)
{
idx = (major >> 1) * major_mult + (major & 1);
ecc_a = 0;
ecc_b = 0;
for(minor = 0; minor < minor_count; minor++)
uint32_t idx = (major >> 1) * major_mult + (major & 1);
uint8_t ecc_a = 0;
uint8_t ecc_b = 0;
for(uint32_t minor = 0; minor < minor_count; minor++)
{
temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
idx += minor_inc;
if(idx >= size) idx -= size;
ecc_a ^= temp;
@@ -301,17 +295,13 @@ void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *da
TRACE("Entering aaruf_ecc_cd_write(%p, %p, %p, %u, %u, %u, %u, %p, %d, %d, %d)", context, address, data,
major_count, minor_count, major_mult, minor_inc, ecc, address_offset, data_offset, ecc_offset);
CdEccContext *ctx;
uint32_t size, major, idx, minor;
uint8_t ecc_a, ecc_b, temp;
if(context == NULL || address == NULL || data == NULL || ecc == NULL)
{
TRACE("Exiting aaruf_ecc_cd_write() with missing data");
return;
}
ctx = (CdEccContext *)context;
CdEccContext *ctx = context;
if(!ctx->inited_edc)
{
@@ -319,16 +309,16 @@ void aaruf_ecc_cd_write(void *context, const uint8_t *address, const uint8_t *da
return;
}
size = major_count * minor_count;
for(major = 0; major < major_count; major++)
uint32_t size = major_count * minor_count;
for(uint32_t major = 0; major < major_count; major++)
{
idx = (major >> 1) * major_mult + (major & 1);
ecc_a = 0;
ecc_b = 0;
uint32_t idx = (major >> 1) * major_mult + (major & 1);
uint8_t ecc_a = 0;
uint8_t ecc_b = 0;
for(minor = 0; minor < minor_count; minor++)
for(uint32_t minor = 0; minor < minor_count; minor++)
{
temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
uint8_t temp = idx < 4 ? address[idx + address_offset] : data[idx + data_offset - 4];
idx += minor_inc;
if(idx >= size) idx -= size;
ecc_a ^= temp;
@@ -468,15 +458,13 @@ void aaruf_ecc_cd_reconstruct(void *context, uint8_t *sector, const uint8_t type
uint32_t computed_edc;
uint8_t zeroaddress[4];
CdEccContext *ctx;
if(context == NULL || sector == NULL)
{
TRACE("Exiting aaruf_ecc_cd_reconstruct() with missing data");
return;
}
ctx = (CdEccContext *)context;
CdEccContext *ctx = context;
if(!ctx->inited_edc)
{
@@ -555,15 +543,13 @@ uint32_t aaruf_edc_cd_compute(void *context, uint32_t edc, const uint8_t *src, i
{
TRACE("Entering aaruf_edc_cd_compute(%p, %u, %p, %d, %d)", context, edc, src, size, pos);
CdEccContext *ctx;
if(context == NULL || src == NULL)
{
TRACE("Exiting aaruf_edc_cd_compute() with missing data");
return 0;
}
ctx = (CdEccContext *)context;
CdEccContext *ctx = context;
if(!ctx->inited_edc)
{

View File

@@ -121,12 +121,10 @@ int have_clmul()
TRACE("Entering have_clmul()");
unsigned eax, ebx, ecx, edx;
int has_pclmulqdq;
int has_sse41;
cpuid(1 /* feature bits */, &eax, &ebx, &ecx, &edx);
has_pclmulqdq = ecx & 0x2; /* bit 1 */
has_sse41 = ecx & 0x80000; /* bit 19 */
int has_pclmulqdq = ecx & 0x2; /* bit 1 */
int has_sse41 = ecx & 0x80000; /* bit 19 */
TRACE("Exiting have_clmul() = %d", has_pclmulqdq && has_sse41);
return has_pclmulqdq && has_sse41;

View File

@@ -192,26 +192,18 @@ int32_t aaruf_cst_transform(const uint8_t *interleaved, uint8_t *sequential, con
*/
int32_t aaruf_cst_untransform(const uint8_t *sequential, uint8_t *interleaved, const size_t length)
{
uint8_t *p, *q, *r, *s, *t, *u, *v, *w;
size_t q_start;
size_t r_start;
size_t s_start;
size_t t_start;
size_t u_start;
size_t v_start;
size_t w_start;
size_t i;
size_t i;
if(interleaved == NULL || sequential == NULL) return AARUF_ERROR_BUFFER_TOO_SMALL;
p = malloc(length / 8);
q = malloc(length / 8);
r = malloc(length / 8);
s = malloc(length / 8);
t = malloc(length / 8);
u = malloc(length / 8);
v = malloc(length / 8);
w = malloc(length / 8);
uint8_t *p = malloc(length / 8);
uint8_t *q = malloc(length / 8);
uint8_t *r = malloc(length / 8);
uint8_t *s = malloc(length / 8);
uint8_t *t = malloc(length / 8);
uint8_t *u = malloc(length / 8);
uint8_t *v = malloc(length / 8);
uint8_t *w = malloc(length / 8);
if(p == NULL || q == NULL || r == NULL || s == NULL || t == NULL || u == NULL || v == NULL || w == NULL)
{
@@ -226,13 +218,13 @@ int32_t aaruf_cst_untransform(const uint8_t *sequential, uint8_t *interleaved, c
return AARUF_ERROR_NOT_ENOUGH_MEMORY;
}
q_start = (length / 8) * 1;
r_start = (length / 8) * 2;
s_start = (length / 8) * 3;
t_start = (length / 8) * 4;
u_start = (length / 8) * 5;
v_start = (length / 8) * 6;
w_start = (length / 8) * 7;
size_t q_start = (length / 8) * 1;
size_t r_start = (length / 8) * 2;
size_t s_start = (length / 8) * 3;
size_t t_start = (length / 8) * 4;
size_t u_start = (length / 8) * 5;
size_t v_start = (length / 8) * 6;
size_t w_start = (length / 8) * 7;
for(i = 0; i < (length / 8); i++)
{

View File

@@ -48,10 +48,10 @@ static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecod
AARU_EXPORT size_t AARU_CALL aaruf_flac_decode_redbook_buffer(uint8_t *dst_buffer, size_t dst_size,
const uint8_t *src_buffer, size_t src_size)
{
FLAC__StreamDecoder *decoder = NULL;
FLAC__StreamDecoder *decoder = NULL;
FLAC__StreamDecoderInitStatus init_status = FLAC__STREAM_DECODER_INIT_STATUS_OK;
aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
size_t ret_size = 0;
aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
size_t ret_size = 0;
memset(ctx, 0, sizeof(aaru_flac_ctx));
@@ -112,12 +112,11 @@ static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *de
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
const FLAC__int32 *const buffer[], void *client_data)
{
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
size_t i;
aaru_flac_ctx *ctx = (aaru_flac_ctx *)client_data;
uint16_t *buffer16 = (uint16_t *)(ctx->dst_buffer + ctx->dst_pos);
// Why FLAC does not interleave the channels as PCM do, oh the mistery, we could use memcpy instead of looping
for(i = 0; i < frame->header.blocksize && ctx->dst_pos < ctx->dst_len; i++)
for(size_t i = 0; i < frame->header.blocksize && ctx->dst_pos < ctx->dst_len; i++)
{
// Left channel
*(buffer16++) = (FLAC__int16)buffer[0][i];
@@ -180,13 +179,13 @@ AARU_EXPORT size_t AARU_CALL aaruf_flac_encode_redbook_buffer(
uint32_t min_residual_partition_order, uint32_t max_residual_partition_order, const char *application_id,
uint32_t application_id_len)
{
FLAC__StreamEncoder *encoder = NULL;
aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
FLAC__StreamEncoder *encoder = NULL;
aaru_flac_ctx *ctx = (aaru_flac_ctx *)malloc(sizeof(aaru_flac_ctx));
FLAC__StreamEncoderInitStatus init_status = FLAC__STREAM_ENCODER_INIT_STATUS_OK;
size_t ret_size = 0;
FLAC__int32 *pcm = NULL;
int i = 0;
int16_t *buffer16 = (int16_t *)src_buffer;
size_t ret_size = 0;
FLAC__int32 *pcm = NULL;
int i = 0;
int16_t *buffer16 = (int16_t *)src_buffer;
FLAC__StreamMetadata *metadata[1];
memset(ctx, 0, sizeof(aaru_flac_ctx));

View File

@@ -54,8 +54,7 @@ static uint64_t div129by65(uint64_t poly)
{
uint64_t q = 0;
uint64_t h = poly;
uint32_t i;
for(i = 0; i < 64; ++i)
for(uint32_t i = 0; i < 64; ++i)
{
q |= (h & (1ull << 63)) >> i;
h = (h << 1) ^ (poly & ((int64_t)h >> 63));

View File

@@ -86,8 +86,7 @@ int32_t aaruf_read_media_tag(void *context, uint8_t *data, const int32_t tag, ui
{
TRACE("Entering aaruf_read_media_tag(%p, %p, %d, %u)", context, data, tag, *length);
aaruformatContext *ctx;
mediaTagEntry *item;
mediaTagEntry *item;
if(context == NULL)
{
@@ -96,7 +95,7 @@ int32_t aaruf_read_media_tag(void *context, uint8_t *data, const int32_t tag, ui
return AARUF_ERROR_NOT_AARUFORMAT;
}
ctx = context;
const aaruformatContext *ctx = context;
// Not a libaaruformat context
if(ctx->magic != AARU_MAGIC)
@@ -607,9 +606,6 @@ int32_t aaruf_read_track_sector(void *context, uint8_t *data, const uint64_t sec
TRACE("Entering aaruf_read_track_sector(%p, %p, %" PRIu64 ", %u, %d)", context, data, sector_address, *length,
track);
aaruformatContext *ctx;
int i;
if(context == NULL)
{
FATAL("Invalid context");
@@ -618,7 +614,7 @@ int32_t aaruf_read_track_sector(void *context, uint8_t *data, const uint64_t sec
return AARUF_ERROR_NOT_AARUFORMAT;
}
ctx = context;
aaruformatContext *ctx = context;
// Not a libaaruformat context
if(ctx->magic != AARU_MAGIC)
@@ -637,7 +633,7 @@ int32_t aaruf_read_track_sector(void *context, uint8_t *data, const uint64_t sec
return AARUF_ERROR_INCORRECT_MEDIA_TYPE;
}
for(i = 0; i < ctx->numberOfDataTracks; i++)
for(int i = 0; i < ctx->numberOfDataTracks; i++)
if(ctx->dataTracks[i].sequence == track)
return aaruf_read_sector(context, ctx->dataTracks[i].start + sector_address, data, length);

View File

@@ -28,13 +28,13 @@ extern "C"
{
uint32_t localHashInt = CRC32_ISO_SEED;
uint32_t localTable[256];
int i, j;
int i;
for(i = 0; i < 256; i++)
{
uint32_t entry = (uint32_t)i;
for(j = 0; j < 8; j++)
for(int j = 0; j < 8; j++)
if((entry & 1) == 1)
entry = (entry >> 1) ^ CRC32_ISO_POLY;
else

View File

@@ -74,36 +74,30 @@ TEST_F(flacFixture, flac)
TEST_F(flacFixture, flacCompress)
{
size_t original_len = 9633792;
uint cmp_len = original_len;
uint decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
FILE *file;
uint32_t original_crc, decmp_crc;
uint8_t *original;
uint8_t *cmp_buffer;
uint8_t *decmp_buffer;
size_t newSize;
size_t original_len = 9633792;
uint cmp_len = original_len;
uint decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
// Allocate buffers
original = static_cast<uint8_t *>(malloc(original_len));
cmp_buffer = static_cast<uint8_t *>(malloc(cmp_len));
decmp_buffer = static_cast<uint8_t *>(malloc(decmp_len));
uint8_t *original = static_cast<uint8_t *>(malloc(original_len));
uint8_t *cmp_buffer = static_cast<uint8_t *>(malloc(cmp_len));
uint8_t *decmp_buffer = static_cast<uint8_t *>(malloc(decmp_len));
// Read the file
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/audio.bin", path);
file = fopen(filename, "rb");
FILE *file = fopen(filename, "rb");
fread(original, 1, original_len, file);
fclose(file);
// Calculate the CRC
original_crc = crc32_data(original, original_len);
uint32_t original_crc = crc32_data(original, original_len);
// Compress
newSize = aaruf_flac_encode_redbook_buffer(
size_t newSize = aaruf_flac_encode_redbook_buffer(
cmp_buffer, cmp_len, original, original_len, 4608, 1, 0, "partial_tukey(0/1.0/1.0)", 12, 0, 1, false, 0, 8,
"Aaru.Compression.Native.Tests", strlen("Aaru.Compression.Native.Tests"));
cmp_len = newSize;
@@ -114,7 +108,7 @@ TEST_F(flacFixture, flacCompress)
EXPECT_EQ(decmp_len, original_len);
decmp_crc = crc32_data(decmp_buffer, decmp_len);
uint32_t decmp_crc = crc32_data(decmp_buffer, decmp_len);
// Free buffers
free(original);

View File

@@ -77,39 +77,33 @@ TEST_F(lzmaFixture, lzma)
TEST_F(lzmaFixture, lzmaCompress)
{
size_t original_len = 8388608;
size_t cmp_len = original_len;
size_t decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
FILE *file;
uint32_t original_crc, decmp_crc;
uint8_t *original;
uint8_t *cmp_buffer;
uint8_t *decmp_buffer;
int err;
uint8_t props[5];
size_t props_len = 5;
size_t original_len = 8388608;
size_t cmp_len = original_len;
size_t decmp_len = original_len;
char path[PATH_MAX];
char filename[PATH_MAX * 2];
uint8_t props[5];
size_t props_len = 5;
// Allocate buffers
original = static_cast<uint8_t *>(malloc(original_len));
cmp_buffer = static_cast<uint8_t *>(malloc(cmp_len));
decmp_buffer = static_cast<uint8_t *>(malloc(decmp_len));
uint8_t *original = static_cast<uint8_t *>(malloc(original_len));
uint8_t *cmp_buffer = static_cast<uint8_t *>(malloc(cmp_len));
uint8_t *decmp_buffer = static_cast<uint8_t *>(malloc(decmp_len));
// Read the file
getcwd(path, PATH_MAX);
snprintf(filename, PATH_MAX, "%s/data/data.bin", path);
file = fopen(filename, "rb");
FILE *file = fopen(filename, "rb");
fread(original, 1, original_len, file);
fclose(file);
// Calculate the CRC
original_crc = crc32_data(original, original_len);
uint32_t original_crc = crc32_data(original, original_len);
// Compress
err = aaruf_lzma_encode_buffer(cmp_buffer, &cmp_len, original, original_len, props, &props_len, 9, 1048576, 3, 0, 2,
273, 2);
int err = aaruf_lzma_encode_buffer(cmp_buffer, &cmp_len, original, original_len, props, &props_len, 9, 1048576, 3,
0, 2, 273, 2);
EXPECT_EQ(err, 0);
// Decompress
@@ -118,7 +112,7 @@ TEST_F(lzmaFixture, lzmaCompress)
EXPECT_EQ(decmp_len, original_len);
decmp_crc = crc32_data(decmp_buffer, decmp_len);
uint32_t decmp_crc = crc32_data(decmp_buffer, decmp_len);
// Free buffers
free(original);

View File

@@ -66,15 +66,13 @@ protected:
TEST_F(sha256Fixture, sha256)
{
SHA256_CTX *ctx;
unsigned char hash[SHA256_DIGEST_LENGTH];
int ret;
ctx = static_cast<SHA256_CTX *>(malloc(sizeof(SHA256_CTX)));
SHA256_CTX *ctx = static_cast<SHA256_CTX *>(malloc(sizeof(SHA256_CTX)));
EXPECT_NE(nullptr, ctx);
ret = SHA256_Init(ctx);
int ret = SHA256_Init(ctx);
EXPECT_EQ(ret, 1);