Skip to content
Permalink
Browse files Browse the repository at this point in the history
safety checks for decoding/testing when the buffer size and block siz…
…e are inconsistent, tentative CRC32 version for BE systems (needs further verification on SPARC?)
  • Loading branch information
kspalaiologos committed Mar 19, 2023
1 parent 4a9d2c5 commit aae16d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/libbz3.c
Expand Up @@ -61,8 +61,15 @@ static const u32 crc32Table[256] = {
};

static u32 crc32sum(u32 crc, u8 * RESTRICT buf, size_t size) {
while (size--) crc = crc32Table[(crc ^ *(buf++)) & 0xff] ^ (crc >> 8);
return crc;
// Test endianness. The code needs to be different for LE and BE systems.
u32 test = 1;
if (*(u8 *) &test) {
while (size--) crc = crc32Table[(crc ^ *(buf++)) & 0xff] ^ (crc >> 8);
return crc;
} else {
while (size--) crc = crc32Table[((crc >> 24) ^ *(buf++)) & 0xff] ^ (crc << 8);
return crc;
}
}

/* LZP code. These constants were manually tuned to give the best compression ratio while using relatively
Expand Down
19 changes: 19 additions & 0 deletions src/main.c
Expand Up @@ -242,6 +242,10 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
new_size = read_neutral_s32(byteswap_buf);
xread_noeof(&byteswap_buf, 1, 4, input_des);
old_size = read_neutral_s32(byteswap_buf);
if(old_size > block_size + 31) {
fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
return 1;
}
xread_noeof(buffer, 1, new_size, input_des);
bytes_read += 8 + new_size;
if (bz3_decode_block(state, buffer, new_size, old_size) == -1) {
Expand All @@ -259,6 +263,10 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
new_size = read_neutral_s32(byteswap_buf);
xread_noeof(&byteswap_buf, 1, 4, input_des);
old_size = read_neutral_s32(byteswap_buf);
if(old_size > block_size + 31) {
fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
return 1;
}
xread_noeof(buffer, 1, new_size, input_des);
bytes_read += 8 + new_size;
if (bz3_decode_block(state, buffer, new_size, old_size) == -1) {
Expand Down Expand Up @@ -332,6 +340,10 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
sizes[i] = read_neutral_s32(byteswap_buf);
xread_noeof(&byteswap_buf, 1, 4, input_des);
old_sizes[i] = read_neutral_s32(byteswap_buf);
if(old_sizes[i] > block_size + 31) {
fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
return 1;
}
xread_noeof(buffers[i], 1, sizes[i], input_des);
bytes_read += 8 + sizes[i];
}
Expand All @@ -356,6 +368,10 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
sizes[i] = read_neutral_s32(byteswap_buf);
xread_noeof(&byteswap_buf, 1, 4, input_des);
old_sizes[i] = read_neutral_s32(byteswap_buf);
if(old_sizes[i] > block_size + 31) {
fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
return 1;
}
xread_noeof(buffers[i], 1, sizes[i], input_des);
bytes_read += 8 + sizes[i];
}
Expand Down Expand Up @@ -691,6 +707,9 @@ int main(int argc, char * argv[]) {
output_des = mode != MODE_TEST ? open_output(output, force) : NULL;
input_des = open_input(input);

if(output != f2)
free(output);

int r = process(input_des, output_des, mode, block_size, workers, verbose, input);

fclose(input_des);
Expand Down

0 comments on commit aae16d1

Please sign in to comment.