// Try open file stream bool FXBZFileStream::open(const FXString& filename,FXStreamDirection save_or_load,FXuval size){ if(FXFileStream::open(filename,save_or_load,size)){ if(FXCALLOC(&bz,BZBlock,1)){ int bzerror; bz->stream.next_in=NULL; bz->stream.avail_in=0; bz->stream.next_out=NULL; bz->stream.avail_out=0; ac=BZ_RUN; if(save_or_load==FXStreamLoad){ bzerror=BZ2_bzDecompressInit(&bz->stream,VERBOSITY,0); if(bzerror==BZ_OK) return true; code=FXStreamNoRead; } else{ bzerror=BZ2_bzCompressInit(&bz->stream,BLOCKSIZE100K,VERBOSITY,WORKFACTOR); if(bzerror==BZ_OK) return true; code=FXStreamNoWrite; } FXFREE(&bz); } FXFileStream::close(); } return false; }
static SquashBZ2Stream* squash_bz2_stream_new (SquashCodec* codec, SquashStreamType stream_type, SquashBZ2Options* options) { int bz2_e = 0; SquashBZ2Stream* stream; assert (codec != NULL); assert (stream_type == SQUASH_STREAM_COMPRESS || stream_type == SQUASH_STREAM_DECOMPRESS); stream = (SquashBZ2Stream*) malloc (sizeof (SquashBZ2Stream)); squash_bz2_stream_init (stream, codec, stream_type, options, squash_bz2_stream_free); if (stream_type == SQUASH_STREAM_COMPRESS) { bz2_e = BZ2_bzCompressInit (&(stream->stream), squash_bz2_options_get_block_size_100k (options), 0, squash_bz2_options_get_work_factor (options)); } else if (stream_type == SQUASH_STREAM_DECOMPRESS) { bz2_e = BZ2_bzDecompressInit (&(stream->stream), 0, squash_bz2_options_get_small (options) ? 1 : 0); } else { assert (false); } if (bz2_e != BZ_OK) { /* We validate the params so OOM is really the only time this should happen, and that really shouldn't be happening here. */ stream = squash_object_unref (stream); } return stream; }
static SquashBZ2Stream* squash_bz2_stream_new (SquashCodec* codec, SquashStreamType stream_type, SquashOptions* options) { int bz2_e = 0; SquashBZ2Stream* stream; assert (codec != NULL); assert (stream_type == SQUASH_STREAM_COMPRESS || stream_type == SQUASH_STREAM_DECOMPRESS); stream = squash_malloc (sizeof (SquashBZ2Stream)); squash_bz2_stream_init (stream, codec, stream_type, options, squash_bz2_stream_destroy); if (stream_type == SQUASH_STREAM_COMPRESS) { bz2_e = BZ2_bzCompressInit (&(stream->stream), 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)); } else if (stream_type == SQUASH_STREAM_DECOMPRESS) { bz2_e = BZ2_bzDecompressInit (&(stream->stream), 0, squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_SMALL)); } else { squash_assert_unreachable(); } if (bz2_e != BZ_OK) { /* We validate the params so OOM is really the only time this should happen, and that really shouldn't be happening here. */ stream = squash_object_unref (stream); } return stream; }
/** * Create a compress function. * options: * blocksize=[1,9] * workfactor=[0,250] */ static int larc_bzip2_compressor(lua_State *L) { int blocksize = 6, workfactor = 0; bz_userdata *ud; if (lua_gettop(L) > 0) { luaL_checktype(L, 1, LUA_TTABLE); GETINT2OPTION(1,blocksize,level); GETINTOPTION(1,workfactor); } ud = (bz_userdata*)lua_newuserdata(L, sizeof(bz_userdata)); luaL_getmetatable(L, BZ2COMPRESS_MT); lua_setmetatable(L, -2); ud->z.bzalloc = NULL; ud->z.bzfree = NULL; ud->z.opaque = NULL; ud->status = BZ2_bzCompressInit(&ud->z, blocksize, 0, workfactor); if (ud->status != BZ_OK) { lua_pushnil(L); lua_pushstring(L, bz2_error(ud->status)); lua_pushinteger(L, ud->status); return 3; } lua_pushcclosure(L, compress_call, 1); return 1; }
struct ostream *o_stream_create_bz2(struct ostream *output, int level) { struct bzlib_ostream *zstream; int ret; i_assert(level >= 1 && level <= 9); zstream = i_new(struct bzlib_ostream, 1); zstream->ostream.sendv = o_stream_bzlib_sendv; zstream->ostream.flush = o_stream_bzlib_flush; zstream->ostream.iostream.close = o_stream_bzlib_close; ret = BZ2_bzCompressInit(&zstream->zs, level, 0, 0); switch (ret) { case BZ_OK: break; case BZ_MEM_ERROR: i_fatal_status(FATAL_OUTOFMEM, "bzlib: Out of memory"); case BZ_CONFIG_ERROR: i_fatal("Wrong bzlib library version (broken compilation)"); case BZ_PARAM_ERROR: i_fatal("bzlib: Invalid parameters"); default: i_fatal("BZ2_bzCompressInit() failed with %d", ret); } zstream->zs.next_out = zstream->outbuf; zstream->zs.avail_out = sizeof(zstream->outbuf); return o_stream_create(&zstream->ostream, output, o_stream_get_fd(output)); }
void bzip2_base::do_init ( bool compress, #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) bzip2::alloc_func alloc, bzip2::free_func free, #endif void* derived ) { bz_stream* s = static_cast<bz_stream*>(stream_); // Current interface for customizing memory management // is non-conforming and has been disabled: //#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) // s->bzalloc = alloc; // s->bzfree = free; //#else s->bzalloc = 0; s->bzfree = 0; //#endif s->opaque = derived; bzip2_error::check( compress ? BZ2_bzCompressInit( s, params_.block_size, 0, params_.work_factor ) : BZ2_bzDecompressInit( s, 0, params_.small ) ); ready_ = true; }
void stream_read_func(gpointer data, gpointer user_data) { file_part_t *part = (file_part_t*) data; bz_stream bzs; memset(&bzs, 0, sizeof(bz_stream)); int bzerror = BZ_OK; bzerror = BZ2_bzCompressInit(&bzs, 9, 0, 30); if (bzerror == BZ_OK) { bzs.next_in = part->inBuf; bzs.avail_in = part->inBufz; bzs.next_out = part->outBuf; bzs.avail_out = part->outBufz; bzerror = BZ2_bzCompress(&bzs, BZ_FINISH); part->outBufz = bzs.total_out_lo32; } if (bzerror != BZ_STREAM_END) { part->error = TRUE; fprintf(stderr, "BZError %d\n", bzerror); } BZ2_bzCompressEnd(&bzs); g_mutex_lock(PART_LIST_LOCK); PART_LIST = g_slist_insert_sorted(PART_LIST, part, file_part_compare); PART_LIST_SIZE++; g_mutex_unlock(PART_LIST_LOCK); }
static bool start(void *ud) { struct ctx *ctx = (struct ctx *)ud; int ret; ctx->zstr.avail_in = 0; ctx->zstr.next_in = NULL; ctx->zstr.avail_out = 0; ctx->zstr.next_out = NULL; if (ctx->compress) { ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30); } else { ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0); } if (ret != BZ_OK) { zip_error_set(ctx->error, map_error(ret), 0); return false; } return true; }
BZFilter::BZFilter() { memzero(&zs, sizeof(zs)); if(BZ2_bzCompressInit(&zs, 9, 0, 30) != BZ_OK) { throw Exception(STRING(COMPRESSION_ERROR)); } }
iow_t *bz_wopen(iow_t *child, int compress_level) { iow_t *iow; if (!child) return NULL; iow = malloc(sizeof(iow_t)); iow->source = &bz_wsource; iow->data = malloc(sizeof(struct bzw_t)); DATA(iow)->child = child; DATA(iow)->strm.next_in = NULL; DATA(iow)->strm.avail_in = 0; DATA(iow)->strm.next_out = DATA(iow)->outbuff; DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); DATA(iow)->strm.bzalloc = NULL; DATA(iow)->strm.bzfree = NULL; DATA(iow)->strm.opaque = NULL; DATA(iow)->err = ERR_OK; BZ2_bzCompressInit(&DATA(iow)->strm, compress_level, /* Block size */ 0, /* Verbosity */ 30); /* Work factor */ return iow; }
int compress(string method, char* input_data, int input_size, char* output_data, int& output_size){ if(method == "bzip2"){ bz_stream stream; stream.bzalloc = NULL; stream.bzfree = NULL; stream.opaque = NULL; int rv = BZ2_bzCompressInit(&stream, 5, 0, 0); if(rv != BZ_OK) return -1; stream.next_in = input_data; stream.avail_in = input_size; stream.next_out = output_data; stream.avail_out = output_size; rv = BZ2_bzCompress(&stream, BZ_FINISH); while(rv != BZ_STREAM_END){ rv = BZ2_bzCompress(&stream, BZ_FINISH); if(rv != BZ_STREAM_END || rv != BZ_FINISH_OK) return -1; } output_size = stream.total_out_lo32; BZ2_bzCompressEnd(&stream); return 0; } return -1; }
static char *_qdbm_bzencode_impl(const char *ptr, int size, int *sp) { bz_stream zs; char *buf, *swap, obuf[BZIPBUFSIZ]; int rv, asiz, bsiz, osiz; if(size < 0) size = strlen(ptr); zs.bzalloc = NULL; zs.bzfree = NULL; zs.opaque = NULL; if(BZ2_bzCompressInit(&zs, 9, 0, 30) != BZ_OK) return NULL; asiz = size + 16; if(asiz < BZIPBUFSIZ) asiz = BZIPBUFSIZ; if(!(buf = malloc(asiz))) { BZ2_bzCompressEnd(&zs); return NULL; } bsiz = 0; zs.next_in = (char *)ptr; zs.avail_in = size; zs.next_out = obuf; zs.avail_out = BZIPBUFSIZ; while((rv = BZ2_bzCompress(&zs, BZ_FINISH)) == BZ_FINISH_OK) { osiz = BZIPBUFSIZ - zs.avail_out; if(bsiz + osiz > asiz) { asiz = asiz * 2 + osiz; if(!(swap = realloc(buf, asiz))) { free(buf); BZ2_bzCompressEnd(&zs); return NULL; } buf = swap; } memcpy(buf + bsiz, obuf, osiz); bsiz += osiz; zs.next_out = obuf; zs.avail_out = BZIPBUFSIZ; } if(rv != BZ_STREAM_END) { free(buf); BZ2_bzCompressEnd(&zs); return NULL; } osiz = BZIPBUFSIZ - zs.avail_out; if(bsiz + osiz + 1 > asiz) { asiz = asiz * 2 + osiz; if(!(swap = realloc(buf, asiz))) { free(buf); BZ2_bzCompressEnd(&zs); return NULL; } buf = swap; } memcpy(buf + bsiz, obuf, osiz); bsiz += osiz; buf[bsiz] = '\0'; *sp = bsiz; BZ2_bzCompressEnd(&zs); return buf; }
int ipfix_compress(ipfix_exporter *exporter) { bz_stream strm; int ret; int i; strm.bzalloc = NULL; strm.bzfree = NULL; strm.opaque = NULL; // The compression level is the 100k block size - as we deal with packets // which can be at 65536 bytes length at most the compression level should // not make a difference. ret = BZ2_bzCompressInit(&strm, bzip2_compression_level, 0, 0); if (ret != BZ_OK) { return -1; } strm.avail_out = sizeof(exporter->compression_buffer); strm.next_out = (char *) exporter->compression_buffer; for (i = 0; i < exporter->data_sendbuffer->committed; i++) { if (strm.avail_out <= 0) { msg(MSG_ERROR, "Out of buffer space while compressing."); BZ2_bzCompressEnd(&strm); return -1; } struct iovec *vec = &exporter->data_sendbuffer->entries[i]; strm.avail_in = vec->iov_len; strm.next_in = vec->iov_base; ret = BZ2_bzCompress(&strm, BZ_RUN); assert(ret == BZ_RUN_OK); } strm.avail_in = 0; strm.next_in = NULL; ret = BZ2_bzCompress(&strm, BZ_FINISH); assert(ret == BZ_STREAM_END); DPRINTF("(Un-)Compressed length: %d / %d", exporter->data_sendbuffer->committed_data_length, sizeof(exporter->compression_buffer) - strm.avail_out); exporter->data_sendbuffer->entries[0].iov_base = exporter->compression_buffer; exporter->data_sendbuffer->entries[0].iov_len = sizeof(exporter->compression_buffer) - strm.avail_out; exporter->data_sendbuffer->committed = 1; exporter->data_sendbuffer->current = 1; exporter->data_sendbuffer->committed_data_length = exporter->data_sendbuffer->entries[0].iov_len; BZ2_bzCompressEnd(&strm); return 0; }
Samurai::IO::BZip2Compressor::BZip2Compressor() { d = new Bz2Private(); if (BZ2_bzCompressInit(d->stream, 5, 0, 0) != BZ_OK) { delete d; d = 0; } }
// BZip2 Compressor template<> Compressor<BZip2>::Compressor(int level) : impl{leap::make_unique<BZip2>()} { if (level < -1 || 9 < level) throw std::invalid_argument("Compression stream level must be in the range [0, 9]"); if (level == -1) level = 9; BZ2_bzCompressInit(&impl->strm, level, 0, 0); }
/** * Compress a string. * options: * blocksize=[1,9] * workfactor=[0,250] */ static int larc_bzip2_compress(lua_State *L) { int blocksize = 6, workfactor = 0; size_t len; const char *str = luaL_checklstring(L, 1, &len); bz_userdata ud; if (lua_gettop(L) > 1) { luaL_checktype(L, 2, LUA_TTABLE); GETINT2OPTION(2,blocksize,level); GETINTOPTION(2,workfactor); } ud.z.bzalloc = NULL; ud.z.bzfree = NULL; ud.z.opaque = NULL; ud.status = BZ2_bzCompressInit(&ud.z, blocksize, 0, workfactor); if (ud.status != BZ_OK) { lua_pushnil(L); lua_pushstring(L, bz2_error(ud.status)); lua_pushinteger(L, ud.status); return 3; } ud.z.next_in = (char*)str; ud.z.avail_in = len; ud.result = -1; ud.flush = BZ_FINISH; if (0 != lua_cpcall(L, protected_compress_to_buffer, &ud)) { BZ2_bzCompressEnd(&ud.z); return lua_error(L); } BZ2_bzCompressEnd(&ud.z); if (ud.result != -1) { lua_rawgeti(L, LUA_REGISTRYINDEX, ud.result); luaL_unref(L, LUA_REGISTRYINDEX, ud.result); lua_pushinteger(L, len - ud.z.avail_in); } else { lua_pushnil(L); lua_pushstring(L, bz2_error(ud.status)); } lua_pushinteger(L, ud.status); return 3; }
void BZ2Compress(Stream& out, Stream& in, Gate2<int, int> progress) { enum { BUF_SIZE = 65536 }; Buffer<char> input(BUF_SIZE), output(BUF_SIZE); bz_stream z; z.bzalloc = bzalloc_new; z.bzfree = bzfree_new; z.opaque = 0; if(BZ2_bzCompressInit(&z, 9, 0, 30) != BZ_OK) { out.SetError(); return; } z.avail_in = 0; z.avail_out = BUF_SIZE; z.next_out = output; int code; int flush = BZ_RUN; int64 total = in.GetLeft(); int done = 0; do { if(z.avail_in == 0 && flush == BZ_RUN) { z.next_in = input; if((z.avail_in = in.Get(z.next_in = input, BUF_SIZE)) == 0) flush = BZ_FINISH; done += z.avail_in; if(progress(done, (int)total) || in.IsError()) { BZ2_bzCompressEnd(&z); out.SetError(); return; } } code = BZ2_bzCompress(&z, flush); if(z.avail_out == 0) { out.Put(z.next_out = output, z.avail_out = BUF_SIZE); if(out.IsError()) { BZ2_bzCompressEnd(&z); return; } } } while(code == BZ_RUN_OK || code == BZ_FINISH_OK); if(z.avail_out < BUF_SIZE) out.Put(output, BUF_SIZE - z.avail_out); BZ2_bzCompressEnd(&z); if(code != BZ_STREAM_END) out.SetError(); }
value camlzip_bzCompressInit(value blockSize100k, value verbosity, value workFactor) { #ifdef USE_BZIP2 int err; value vbzs = camlzip_new_bzstream(); if ((err = BZ2_bzCompressInit(BZStream_val(vbzs), Int_val(blockSize100k), Int_val(verbosity), Int_val(workFactor))) != BZ_OK) camlzip_bzerror("Zlib.deflateInit", err); return vbzs; #else failwith("Bzip2 compression not supported."); #endif }
DskOctetFilter *dsk_bz2lib_compressor_new (unsigned level) { DskBz2libCompressor *rv = dsk_object_new (&dsk_bz2lib_compressor_class); int zrv; zrv = BZ2_bzCompressInit (&rv->bz2lib, level, DSK_FALSE, 0); if (zrv != BZ_OK) { dsk_warning ("deflateInit2 returned error: %s", bzrv_to_string (zrv)); dsk_object_unref (rv); return NULL; } rv->initialized = DSK_TRUE; return DSK_OCTET_FILTER (rv); }
torch::Data Bz2::Compress(const torch::Data &data) { const int size100k = 100000; int bufsize = size100k * Bz2::block100k; int status = 0; bz_stream stream = {0}; Data buf(bufsize); Data dst; dst.HiddenAlloc(data.GetSize()); /* next_in should point at the data to be compressed * avail_in should indicate how many bytes the library may read * BZ2_bzCompress updates next_in, avail_in and total_in to reflect the number of bytes it has read */ stream.next_in = (char*) data.GetBytes(); stream.avail_in = (int)data.GetSize(); /* next_out should point to a buffer in which the compressed data is to be placed * avail_out indicating how much output space is available. * BZ2_bzCompress updates next_out, avail_out and total_out to reflect the number of bytes output. */ stream.next_out = (char*)buf.GetBytes(); stream.avail_out = (int)buf.GetSize(); status = BZ2_bzCompressInit(&stream, Bz2::block100k, 0, 0); if (status != BZ_OK) { BZ2_bzCompressEnd(&stream); return dst; } do { status = BZ2_bzCompress(&stream, (stream.avail_in) ? BZ_RUN : BZ_FINISH); if (status != BZ_RUN_OK && status != BZ_STREAM_END) break; dst.Append(buf.GetBytes(), bufsize - stream.avail_out); stream.next_out = (char*)buf.GetBytes(); stream.avail_out = (int)buf.GetSize(); }while (status != BZ_STREAM_END); BZ2_bzCompressEnd(&stream); return dst; }
/*++ BzipCompressObj Compresses data using the Bzip2 compression algorithm. Arguments: sourceObj - Pointer to a Tcl object containing the data to be compressed. destObj - Pointer to a Tcl object to receive the compressed data. level - Compression level. Return Value: A Bzip2 status code; BZ_OK is returned if successful. --*/ static int BzipCompressObj( Tcl_Obj *sourceObj, Tcl_Obj *destObj, int level ) { bz_stream stream; int status; unsigned int destLength; // // The bzalloc, bzfree, and opaque data structure members // must be initialised prior to calling BZ2_bzCompressInit(). // stream.bzalloc = BzipAlloc; stream.bzfree = BzipFree; stream.opaque = NULL; status = BZ2_bzCompressInit(&stream, level, 0, 0); if (status != BZ_OK) { return status; } stream.next_in = (char *)Tcl_GetByteArrayFromObj(sourceObj, (int *)&stream.avail_in); // // According to the Bzip2 documentation, the recommended buffer size // is 1% larger than the uncompressed data, plus 600 additional bytes. // stream.avail_out = destLength = (unsigned int)((double)stream.avail_in * 1.01) + 600; stream.next_out = (char *)Tcl_SetByteArrayLength(destObj, stream.avail_out); status = BZ2_bzCompress(&stream, BZ_FINISH); BZ2_bzCompressEnd(&stream); if (status == BZ_STREAM_END) { // Update the object's length. destLength -= stream.avail_out; Tcl_SetByteArrayLength(destObj, (int)destLength); return BZ_OK; } return (status == BZ_FINISH_OK) ? BZ_OUTBUFF_FULL : status; }
int import_compress_init(ImportCompress *c, ImportCompressType t) { int r; assert(c); switch (t) { case IMPORT_COMPRESS_XZ: { lzma_ret xzr; xzr = lzma_easy_encoder(&c->xz, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64); if (xzr != LZMA_OK) return -EIO; c->type = IMPORT_COMPRESS_XZ; break; } case IMPORT_COMPRESS_GZIP: r = deflateInit2(&c->gzip, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY); if (r != Z_OK) return -EIO; c->type = IMPORT_COMPRESS_GZIP; break; case IMPORT_COMPRESS_BZIP2: r = BZ2_bzCompressInit(&c->bzip2, 9, 0, 0); if (r != BZ_OK) return -EIO; c->type = IMPORT_COMPRESS_BZIP2; break; case IMPORT_COMPRESS_UNCOMPRESSED: c->type = IMPORT_COMPRESS_UNCOMPRESSED; break; default: return -EOPNOTSUPP; } c->encoding = true; return 0; }
static void gst_bz2enc_compress_init (GstBz2enc * b) { g_return_if_fail (GST_IS_BZ2ENC (b)); gst_bz2enc_compress_end (b); b->offset = 0; switch (BZ2_bzCompressInit (&b->stream, b->block_size, 0, 0)) { case BZ_OK: b->ready = TRUE; return; default: b->ready = FALSE; GST_ELEMENT_ERROR (b, CORE, FAILED, (NULL), ("Failed to start compression.")); return; } }
static int hb_bz2Compress( const char * szSrc, HB_SIZE nSrc, char * szDst, HB_SIZE * pnDst, int iBlockSize ) { bz_stream stream; int iResult; memset( &stream, 0, sizeof( stream ) ); stream.next_in = ( char * ) szSrc; stream.avail_in = ( unsigned int ) nSrc; stream.next_out = szDst; stream.avail_out = ( unsigned int ) *pnDst; stream.bzalloc = hb_bz2Alloc; stream.bzfree = hb_bz2Free; /* stream.opaque = NULL; */ iResult = BZ2_bzCompressInit( &stream, iBlockSize, 0, 0 ); if( iResult == BZ_OK ) { do { iResult = BZ2_bzCompress( &stream, BZ_FINISH ); } while( iResult == BZ_FINISH_OK ); if( iResult == BZ_STREAM_END ) { #if HB_SIZE_MAX <= UINT_MAX *pnDst = ( HB_SIZE ) stream.total_out_lo32; #else *pnDst = ( ( HB_SIZE ) stream.total_out_hi32 << 32 ) | stream.total_out_lo32; #endif iResult = BZ_OK; } BZ2_bzCompressEnd( &stream ); } return iResult; }
Buffer BZip2Filter::process(const BufferRef& input) { if (input.empty()) return Buffer(); int rv = BZ2_bzCompressInit(&bz_, level(), // compression level 0, // no output 0 // work factor ); if (rv != BZ_OK) return Buffer(); bz_.next_in = input.begin(); bz_.avail_in = input.size(); bz_.total_in_lo32 = 0; bz_.total_in_hi32 = 0; Buffer output(input.size() * 1.1 + 12); bz_.next_out = output.end(); bz_.avail_out = output.capacity(); bz_.total_out_lo32 = 0; bz_.total_out_hi32 = 0; rv = BZ2_bzCompress(&bz_, BZ_FINISH); if (rv != BZ_STREAM_END) { BZ2_bzCompressEnd(&bz_); return Buffer(); } if (bz_.total_out_hi32) return Buffer(); // file too large output.resize(bz_.total_out_lo32); rv = BZ2_bzCompressEnd(&bz_); if (rv != BZ_OK) return Buffer(); return output; }
static void Compress_BZIP2( char * pbOutBuffer, int * pcbOutBuffer, char * pbInBuffer, int cbInBuffer, int * /* pCmpType */, int /* nCmpLevel */) { bz_stream strm; int blockSize100k = 9; int workFactor = 30; int bzError; // Initialize the BZIP2 compression strm.bzalloc = NULL; strm.bzfree = NULL; // Blizzard uses 9 as blockSize100k, (0x30 as workFactor) // Last checked on Starcraft II if(BZ2_bzCompressInit(&strm, blockSize100k, 0, workFactor) == BZ_OK) { strm.next_in = pbInBuffer; strm.avail_in = cbInBuffer; strm.next_out = pbOutBuffer; strm.avail_out = *pcbOutBuffer; // Perform the compression for(;;) { bzError = BZ2_bzCompress(&strm, (strm.avail_in != 0) ? BZ_RUN : BZ_FINISH); if(bzError == BZ_STREAM_END || bzError < 0) break; } // Put the stream into idle state BZ2_bzCompressEnd(&strm); if(bzError > 0) *pcbOutBuffer = strm.total_out_lo32; } }
static int BZ2_bzBuffToBuffCompress(char* dest, unsigned int* destLen, char* source, unsigned int sourceLen, int blockSize100k) { bz_stream strm; int ret; if (dest == NULL || destLen == NULL || source == NULL || blockSize100k < 1 || blockSize100k > 9 ) { return BZ_PARAM_ERROR; } BZ2_bzCompressInit(&strm, blockSize100k); strm.next_in = source; strm.next_out = dest; strm.avail_in = sourceLen; strm.avail_out = *destLen; ret = BZ2_bzCompress(&strm, BZ_FINISH); if (ret == BZ_FINISH_OK) goto output_overflow; if (ret != BZ_STREAM_END) goto errhandler; /* normal termination */ *destLen -= strm.avail_out; BZ2_bzCompressEnd(&strm); return BZ_OK; output_overflow: BZ2_bzCompressEnd(&strm); return BZ_OUTBUFF_FULL; errhandler: BZ2_bzCompressEnd(&strm); return ret; }
wxBZipOutputStream::wxBZipOutputStream(wxOutputStream& Stream, wxInt32 nCompressionFactor) : wxFilterOutputStream(Stream) { m_hZip = new bz_stream; bz_stream* hZip = (bz_stream*)m_hZip; hZip->bzalloc = NULL; hZip->bzfree = NULL; hZip->opaque = NULL; //param 2 - compression factor = 1-9 9 more compression but slower //param 3 - verbosity = 0-4, 4 more stuff to stdio (ignored) //param 4 - workfactor = reliance on standard comp alg, 0-250, // 0==30 default if (BZ2_bzCompressInit(hZip, nCompressionFactor, 0, 0)!= BZ_OK) { delete hZip; wxLogSysError(wxT("Could not initialize bzip compression engine!")); } }
static gboolean init_bzip (GsfOutputBzip *bzip, GError **err) { int ret; ret = BZ2_bzCompressInit (&bzip->stream, 6, 0, 0); if (ret != BZ_OK) { if (err != NULL) *err = g_error_new (gsf_output_error_id (), 0, "Unable to initialize BZ2 library"); return FALSE; } if (!bzip->buf) { bzip->buf_size = BZ_BUFSIZE; bzip->buf = g_new (guint8, bzip->buf_size); } bzip->stream.next_out = bzip->buf; bzip->stream.avail_out = bzip->buf_size; return TRUE; }
static void init_compress( compress_filter_context_t *zfx, bz_stream *bzs ) { int rc; int level; if( opt.bz2_compress_level >= 1 && opt.bz2_compress_level <= 9 ) level = opt.bz2_compress_level; else if( opt.bz2_compress_level == -1 ) level = 6; /* no particular reason, but it seems reasonable */ else { log_error("invalid compression level; using default level\n"); level = 6; } if((rc=BZ2_bzCompressInit(bzs,level,0,0))!=BZ_OK) log_fatal("bz2lib problem: %d\n",rc); zfx->outbufsize = 8192; zfx->outbuf = xmalloc( zfx->outbufsize ); }