Skip to content

Commit

Permalink
fix wrong memcpy offset
Browse files Browse the repository at this point in the history
  • Loading branch information
iceman1001 committed Jul 16, 2023
1 parent e22d8ae commit b36d5fa
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions client/src/cmdhfmfhard.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ static void init_bitflip_bitarrays(void) {
}
uint32_t filesize = (uint32_t)fsize;
rewind(statesfile);
char *compressed_data = malloc(filesize);
char *compressed_data = calloc(filesize, sizeof(uint8_t));
if (compressed_data == NULL) {
PrintAndLogEx(ERR, "Out of memory error in init_bitflip_statelists(). Aborting...\n");
fclose(statesfile);
Expand All @@ -372,50 +372,53 @@ static void init_bitflip_bitarrays(void) {
fclose(statesfile);
exit(5);
}
char *uncompressed_data = malloc((sizeof(uint32_t) * (1 << 19)) + sizeof(uint32_t));
fclose(statesfile);

char *uncompressed_data = calloc((sizeof(uint32_t) * (1 << 19)) + sizeof(uint32_t), sizeof(uint8_t));
if (uncompressed_data == NULL) {
PrintAndLogEx(ERR, "Out of memory error in init_bitflip_statelists(). Aborting...\n");
PrintAndLogEx(ERR, "Out of memory error in init_bitflip_statelists(). Aborting...\n");
free(compressed_data);
fclose(statesfile);
exit(4);
}

LZ4F_decompressionContext_t ctx;
LZ4F_errorCode_t result = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
if (LZ4F_isError(result)) {
PrintAndLogEx(ERR, "File read error with %s (3) Failed to create decompression context: %s. Aborting...\n", state_file_name, LZ4F_getErrorName(result));
free(compressed_data);
free(uncompressed_data);
fclose(statesfile);
exit(5);
}

size_t expected_output_size = (sizeof(uint32_t) * (1 << 19)) + sizeof(uint32_t);
size_t consumed_input_size = filesize;
size_t generated_output_size = expected_output_size;
result = LZ4F_decompress(ctx, uncompressed_data, &generated_output_size, compressed_data, &consumed_input_size, NULL);

LZ4F_freeDecompressionContext(ctx);
free(compressed_data);

if (LZ4F_isError(result)) {
PrintAndLogEx(ERR, "File read error with %s (3) %s. Aborting...\n", state_file_name, LZ4F_getErrorName(result));
free(uncompressed_data);
fclose(statesfile);
exit(5);
}
if (generated_output_size != expected_output_size) {
PrintAndLogEx(ERR, "File read error with %s (3) got %lu instead of %lu bytes. Aborting...\n", state_file_name, generated_output_size, expected_output_size);
free(uncompressed_data);
fclose(statesfile);
exit(5);
}

uint32_t count = ((uint32_t *)uncompressed_data)[0];

if ((float)count / (1 << 24) < IGNORE_BITFLIP_THRESHOLD) {
uint32_t *bitset = (uint32_t *)malloc_bitarray(sizeof(uint32_t) * (1 << 19));
if (bitset == NULL) {
PrintAndLogEx(ERR, "Out of memory error in init_bitflip_statelists(). Aborting...\n");
free(uncompressed_data);
fclose(statesfile);
exit(4);
}
memcpy(bitset, uncompressed_data + 1, sizeof(uint32_t) * (1 << 19));
memcpy(bitset, uncompressed_data + sizeof(uint32_t), sizeof(uint32_t) * (1 << 19));
effective_bitflip[odd_even][num_effective_bitflips[odd_even]++] = bitflip;
bitflip_bitarrays[odd_even][bitflip] = bitset;
count_bitflip_bitarrays[odd_even][bitflip] = count;
Expand All @@ -432,7 +435,7 @@ static void init_bitflip_bitarrays(void) {
nlz4++;
continue;
} else if (open_bz2compressed) {
bz_stream compressed_stream;

fseek(statesfile, 0, SEEK_END);
int fsize = ftell(statesfile);
if (fsize == -1) {
Expand All @@ -447,11 +450,12 @@ static void init_bitflip_bitarrays(void) {
if (bytesread != filesize) {
PrintAndLogEx(ERR, "File read error with %s. Aborting...\n", state_file_name);
fclose(statesfile);
//BZ2_bzDecompressEnd(&compressed_stream);
exit(5);
}
fclose(statesfile);

uint32_t count = 0;
bz_stream compressed_stream;
init_bunzip2(&compressed_stream, input_buffer, filesize, (char *)&count, sizeof(count));
int res = BZ2_bzDecompress(&compressed_stream);
if (res != BZ_OK) {
Expand Down

0 comments on commit b36d5fa

Please sign in to comment.