static SquashStatus squash_bz2_compress_buffer (SquashCodec* codec, uint8_t* compressed, size_t* compressed_length, const uint8_t* uncompressed, size_t uncompressed_length, SquashOptions* options) { int block_size_100k; int work_factor; int bz2_res; unsigned int compressed_length_ui = (unsigned int) *compressed_length; if (options != NULL) { block_size_100k = ((SquashBZ2Options*) options)->block_size_100k; work_factor = ((SquashBZ2Options*) options)->work_factor; } else { block_size_100k = SQUASH_BZ2_DEFAULT_BLOCK_SIZE_100K; work_factor = SQUASH_BZ2_DEFAULT_WORK_FACTOR; } bz2_res = BZ2_bzBuffToBuffCompress ((char*) compressed, &compressed_length_ui, (char*) uncompressed, (unsigned int) uncompressed_length, block_size_100k, 0, work_factor); if (bz2_res == BZ_OK) { *compressed_length = compressed_length_ui; } return squash_bz2_status_to_squash_status (bz2_res); }
static SquashStatus squash_bz2_decompress_buffer (SquashCodec* codec, uint8_t* uncompressed, size_t* uncompressed_length, const uint8_t* compressed, size_t compressed_length, SquashOptions* options) { int small = (options != NULL) ? ((SquashBZ2Options*) options)->small : SQUASH_BZ2_DEFAULT_SMALL; unsigned int uncompressed_length_ui = (unsigned int) *uncompressed_length; int bz2_res; bz2_res = BZ2_bzBuffToBuffDecompress ((char*) uncompressed, &uncompressed_length_ui, (char*) compressed, (unsigned int) compressed_length, small, 0); if (bz2_res == BZ_OK) { *uncompressed_length = uncompressed_length_ui; } return squash_bz2_status_to_squash_status (bz2_res); }
static SquashStatus squash_bz2_decompress_buffer (SquashCodec* codec, size_t* decompressed_length, uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_length)], size_t compressed_length, const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_length)], SquashOptions* options) { int small = squash_codec_get_option_bool_index (codec, options, SQUASH_BZ2_OPT_SMALL) ? 1 : 0; unsigned int decompressed_length_ui = (unsigned int) *decompressed_length; int bz2_res; bz2_res = BZ2_bzBuffToBuffDecompress ((char*) decompressed, &decompressed_length_ui, (char*) compressed, (unsigned int) compressed_length, small, 0); if (bz2_res == BZ_OK) { *decompressed_length = decompressed_length_ui; } return squash_bz2_status_to_squash_status (bz2_res); }
static SquashStatus squash_bz2_compress_buffer (SquashCodec* codec, size_t* compressed_length, uint8_t compressed[SQUASH_ARRAY_PARAM(*compressed_length)], size_t uncompressed_length, const uint8_t uncompressed[SQUASH_ARRAY_PARAM(uncompressed_length)], SquashOptions* options) { int bz2_res; unsigned int compressed_length_ui = (unsigned int) *compressed_length; bz2_res = BZ2_bzBuffToBuffCompress ((char*) compressed, &compressed_length_ui, (char*) uncompressed, (unsigned int) uncompressed_length, squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_LEVEL), 0, squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_WORK_FACTOR)); if (bz2_res == BZ_OK) { *compressed_length = compressed_length_ui; } return squash_bz2_status_to_squash_status (bz2_res); }
static SquashStatus squash_bz2_finish_stream (SquashStream* stream) { bz_stream* bz2_stream; int bz2_res; SquashStatus res; if (stream->stream_type != SQUASH_STREAM_COMPRESS) { return squash_bz2_process_stream_ex (stream, BZ_RUN); } assert (stream != NULL); if (stream->avail_out == 0) return SQUASH_BUFFER_FULL; bz2_stream = &(((SquashBZ2Stream*) stream)->stream); SQUASH_BZ2_STREAM_COPY_TO_BZ_STREAM(stream, bz2_stream); bz2_res = BZ2_bzCompress (bz2_stream, BZ_FINISH); switch (bz2_res) { case BZ_FINISH_OK: res = SQUASH_PROCESSING; break; case BZ_STREAM_END: res = SQUASH_OK; break; default: res = squash_bz2_status_to_squash_status (bz2_res); break; } SQUASH_BZ2_STREAM_COPY_FROM_BZ_STREAM(stream, bz2_stream); return res; }
static SquashStatus squash_bz2_process_stream_ex (SquashStream* stream, int action) { bz_stream* bz2_stream; int bz2_res; SquashStatus res; assert (stream != NULL); if (stream->avail_out == 0) return SQUASH_BUFFER_FULL; bz2_stream = &(((SquashBZ2Stream*) stream)->stream); SQUASH_BZ2_STREAM_COPY_TO_BZ_STREAM(stream, bz2_stream); if (stream->stream_type == SQUASH_STREAM_COMPRESS) { bz2_res = BZ2_bzCompress (bz2_stream, action); } else { bz2_res = BZ2_bzDecompress (bz2_stream); } if (bz2_res == BZ_RUN_OK || bz2_res == BZ_OK) { if (bz2_stream->avail_in == 0) { res = SQUASH_OK; } else { res = SQUASH_PROCESSING; } } else if (bz2_res == BZ_STREAM_END) { res = SQUASH_END_OF_STREAM; } else { res = squash_bz2_status_to_squash_status (bz2_res); } SQUASH_BZ2_STREAM_COPY_FROM_BZ_STREAM(stream, bz2_stream); return res; }