Skip to content

Commit

Permalink
Removed: bz3_decode_block_bound API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Dec 13, 2024
1 parent c3a78a4 commit 269133b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
14 changes: 1 addition & 13 deletions include/libbz3.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ BZIP3_API int bz3_decompress(const uint8_t * in, uint8_t * out, size_t in_size,
* - Everything is freed after compression completes
*
* 2. bz3_decompress():
* - Allocates additional temporary compression buffer (bz3_decode_block_bound(block_size) bytes)
* - Allocates additional temporary compression buffer (bz3_bound(block_size) bytes)
* in addition to the memory amount returned by this method call.
* - Everything is freed after compression completes
*
Expand Down Expand Up @@ -199,18 +199,6 @@ BZIP3_API void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[
BZIP3_API void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[],
int32_t orig_sizes[], int32_t n);

/**
* @brief Calculate the required output buffer size for decompressing a single block.
*
* When decompressing a block, additional space may be needed to handle internal
* headers from pre-filters like RLE and LZP. This function calculates the exact
* required output buffer size.
*
* @param orig_size The original (uncompressed) size of the block
* @return The required output buffer size for decompression
*/
BZIP3_API size_t bz3_decode_block_bound(size_t orig_size);

#ifdef __cplusplus
} /* extern "C" */
#endif
Expand Down
20 changes: 5 additions & 15 deletions src/libbz3.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,13 +651,13 @@ BZIP3_API s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_s

data_size -= p * 4 + 1;

if (((model & 2) && (lzp_size > bz3_decode_block_bound(state->block_size) || lzp_size < 0)) ||
((model & 4) && (rle_size > bz3_decode_block_bound(state->block_size) || rle_size < 0))) {
if (((model & 2) && (lzp_size > bz3_bound(state->block_size) || lzp_size < 0)) ||
((model & 4) && (rle_size > bz3_bound(state->block_size) || rle_size < 0))) {
state->last_error = BZ3_ERR_MALFORMED_HEADER;
return -1;
}

if (orig_size > bz3_decode_block_bound(state->block_size) || orig_size < 0) {
if (orig_size > bz3_bound(state->block_size) || orig_size < 0) {
state->last_error = BZ3_ERR_MALFORMED_HEADER;
return -1;
}
Expand Down Expand Up @@ -698,7 +698,7 @@ BZIP3_API s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_s

// Undo LZP
if (model & 2) {
size_src = lzp_decompress(b1, b2, lzp_size, bz3_decode_block_bound(state->block_size), state->lzp_lut);
size_src = lzp_decompress(b1, b2, lzp_size, bz3_bound(state->block_size), state->lzp_lut);
if (size_src == -1) {
state->last_error = BZ3_ERR_CRC;
return -1;
Expand Down Expand Up @@ -868,7 +868,7 @@ BZIP3_API int bz3_decompress(const uint8_t * in, uint8_t * out, size_t in_size,
struct bz3_state * state = bz3_new(block_size);
if (!state) return BZ3_ERR_INIT;

u8 * compression_buf = malloc(bz3_decode_block_bound(block_size));
u8 * compression_buf = malloc(bz3_bound(block_size));
if (!compression_buf) {
bz3_free(state);
return BZ3_ERR_INIT;
Expand Down Expand Up @@ -940,13 +940,3 @@ BZIP3_API size_t bz3_memory_needed(int32_t block_size) {
total_size += (1 << LZP_DICTIONARY) * sizeof(int32_t);
return total_size;
}

BZIP3_API size_t bz3_decode_block_bound(size_t orig_size) {
// Block may temporarily exceed the `orig_size` due to
// - RLE not being effective (encoded data pre commit 187b3228c73d4f35916ecb5950f18861ddc853ac)
// - LZP not being effective (encoded data pre commit 187b3228c73d4f35916ecb5950f18861ddc853ac)
// - Burrows Wheeler Transform (added bytes if RLE/LZP didn't save enough space)
// - Arithmetic Coding (on added bytes if prior steps failed)
// The 256-byte padding is claimed by the author as sufficient
return orig_size + 256;
}

0 comments on commit 269133b

Please sign in to comment.