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; }
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(); }
/** * 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; }
wxBZipOutputStream::~wxBZipOutputStream() { bz_stream* hZip = (bz_stream*)m_hZip; BZ2_bzCompressEnd(hZip); delete hZip; }
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; }
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); }
/* * Finish the compression. */ static int archive_compressor_bzip2_close(struct archive_write_filter *f) { struct private_data *data = (struct private_data *)f->data; int ret, r1; /* Finish compression cycle. */ ret = drive_compressor(f, data, 1); if (ret == ARCHIVE_OK) { /* Write the last block */ ret = __archive_write_filter(f->next_filter, data->compressed, data->compressed_buffer_size - data->stream.avail_out); } switch (BZ2_bzCompressEnd(&(data->stream))) { case BZ_OK: break; default: archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER, "Failed to clean up compressor"); ret = ARCHIVE_FATAL; } r1 = __archive_write_close_filter(f->next_filter); return (r1 < ret ? r1 : ret); }
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; }
static int stream_bzip2_end(server *srv, handler_ctx *hctx) { bz_stream * const bz = &(hctx->u.bz); int rc = BZ2_bzCompressEnd(bz); if (BZ_OK == rc || BZ_DATA_ERROR == rc) return 0; log_error_write(srv, __FILE__, __LINE__, "sd", "BZ2_bzCompressEnd error ret=", rc); return -1; }
Samurai::IO::BZip2Compressor::~BZip2Compressor() { #ifdef DATADUMP printf("~BZ2_Compressor, %u/%u = %.04f\n", d->stream->total_out_lo32, d->stream->total_in_lo32, (float) d->stream->total_out_lo32 / (float)(d->stream->total_in_lo32 + 1)); #endif if (d && d->stream) BZ2_bzCompressEnd(d->stream); delete d; }
void bzip2_base::end(bool compress) { ready_ = false; bz_stream* s = static_cast<bz_stream*>(stream_); bzip2_error::check( compress ? BZ2_bzCompressEnd(s) : BZ2_bzDecompressEnd(s) ); }
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 php_bz2_compress_dtor(php_stream_filter *thisfilter) { if (Z_PTR(thisfilter->abstract)) { php_bz2_filter_data *data = Z_PTR(thisfilter->abstract); BZ2_bzCompressEnd(&(data->strm)); pefree(data->inbuf, data->persistent); pefree(data->outbuf, data->persistent); pefree(data, data->persistent); } }
static void o_stream_bzlib_close(struct iostream_private *stream, bool close_parent) { struct bzlib_ostream *zstream = (struct bzlib_ostream *)stream; (void)o_stream_flush(&zstream->ostream.ostream); (void)BZ2_bzCompressEnd(&zstream->zs); if (close_parent) o_stream_close(zstream->ostream.parent); }
/* Destroy * destroy file */ static rc_t CC KBZipFileDestroy (KBZipFile *self) { rc_t rc = 0, orc = 0; if (self) { if (self->file != NULL) { int zret = BZ_OK; if (self->dad.write_enabled) { /* flush out end of compressed data */ /* if (self->completed == false) */ /* { */ size_t ignored; bz_stream* strm = &self->strm; strm->avail_in = 0; strm->next_in = NULL; rc = KBZipFileWriteInt(self, BZ_FINISH, &ignored); /* assert (zret == BZ_STREAM_END); */ /* stream will be complete */ /* } */ zret = BZ2_bzCompressEnd(&self->strm); /* clean up */ self->completed = true; } else if (self->dad.read_enabled) { zret = BZ2_bzDecompressEnd (&self->strm); } else { rc = RC (rcFS, rcFile, rcDestroying, rcSelf, rcInvalid); LOGERR (klogInt, orc, "corrupt object " "closing bzip file object"); } if (zret != BZ_OK) { orc = RC (rcFS, rcFile, rcDestroying, rcParam, rcInvalid); LOGERR (klogInt, orc, "bad parameters - coding error on " "closing bzip file object"); if (rc == 0) rc = orc; } orc = KFileRelease (self->file); if (rc == 0) rc = orc; } free (self); } return rc; }
value camlzip_bzCompressEnd(value stream) { #ifdef USE_BZIP2 int err; if ((err = BZ2_bzCompressEnd(BZStream_val(stream))) != BZ_OK) camlzip_bzerror("Bzlib.compress_end", err); free(BZStream_val(stream)); #else failwith("Bzip2 compression not supported"); #endif return Val_unit; }
static void gst_bz2enc_compress_end (GstBz2enc * b) { g_return_if_fail (GST_IS_BZ2ENC (b)); if (b->ready) { BZ2_bzCompressEnd (&b->stream); memset (&b->stream, 0, sizeof (b->stream)); b->ready = FALSE; } }
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; }
static void squash_bz2_stream_destroy (void* stream) { switch (((SquashStream*) stream)->stream_type) { case SQUASH_STREAM_COMPRESS: BZ2_bzCompressEnd (&(((SquashBZ2Stream*) stream)->stream)); break; case SQUASH_STREAM_DECOMPRESS: BZ2_bzDecompressEnd (&(((SquashBZ2Stream*) stream)->stream)); break; } squash_stream_destroy (stream); }
void MemoryCompressor::Clear(void) { if (output) { free(output); output=0; } if (streamInited) BZ2_bzCompressEnd( &stream ); totalRead=totalWritten=compressedInputLength=0; }
int bz2Compress(int level, const unsigned char *data, size_t avail_in, unsigned char *odata, size_t *avail_out) { int ret; int verbosity = 0; int workFactor = 30; bz_stream strm; strm.bzalloc = NULL; strm.bzfree = NULL; strm.opaque = NULL; ret = BZ2_bzCompressInit(&strm, level, verbosity, workFactor); if (ret != BZ_OK) return ret; strm.next_in = data; strm.next_out = odata; strm.avail_in = avail_in; strm.avail_out = *avail_out; ret = BZ2_bzCompress ( &strm, BZ_FINISH ); if (ret == BZ_FINISH_OK) goto output_overflow; if (ret != BZ_STREAM_END) goto errhandler; /* normal termination */ *avail_out -= strm.avail_out; BZ2_bzCompressEnd ( &strm ); return BZ_OK; output_overflow: BZ2_bzCompressEnd ( &strm ); return BZ_OUTBUFF_FULL; errhandler: BZ2_bzCompressEnd ( &strm ); return ret; }
String BZ2Compress(String s, Gate2<int, int> progress) { if(s.IsEmpty()) return s; bz_stream z; Zero(z); z.bzalloc = bzalloc_new; z.bzfree = bzfree_new; z.opaque = 0; if(BZ2_bzCompressInit(&z, 9, 0, 30) != BZ_OK) return String::GetVoid(); int buf_size = minmax(s.GetLength(), 1024, 65536); Buffer<char> output(buf_size); z.next_in = (char *)s.Begin(); z.avail_in = s.GetLength(); z.next_out = output; z.avail_out = buf_size; String out; int res; while((res = BZ2_bzCompress(&z, z.avail_in ? BZ_RUN : BZ_FINISH)) == BZ_RUN_OK || res == BZ_FINISH_OK) { out.Cat(output, buf_size - z.avail_out); z.next_out = output; z.avail_out = buf_size; if(progress((int)(uintptr_t)((const char *)z.next_in - ~s), s.GetLength())) { BZ2_bzCompressEnd(&z); return String::GetVoid(); } } out.Cat(output, buf_size - z.avail_out); BZ2_bzCompressEnd(&z); if(res != BZ_STREAM_END) out = String::GetVoid(); return out; }
static gboolean gsf_output_bzip_close (GsfOutput *output) { #ifdef HAVE_BZ2 GsfOutputBzip *bzip = GSF_OUTPUT_BZIP (output); gboolean rt; rt = bzip_flush (bzip); BZ2_bzCompressEnd (&bzip->stream); return rt; #else (void)output; return FALSE; #endif }
// Close file stream bool FXBZFileStream::close(){ if(dir){ if(dir==FXStreamLoad){ FXFileStream::close(); BZ2_bzDecompressEnd(&bz->stream); } else{ ac=BZ_FINISH; FXFileStream::close(); BZ2_bzCompressEnd(&bz->stream); } FXFREE(&bz); return true; } return false; }
/*++ 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; }
static void bz_wclose(iow_t *iow) { while (BZ2_bzCompress(&DATA(iow)->strm, BZ_FINISH) == BZ_OK) { /* Need to flush the output buffer */ wandio_wwrite(DATA(iow)->child, DATA(iow)->outbuff, sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); DATA(iow)->strm.next_out = DATA(iow)->outbuff; DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); } BZ2_bzCompressEnd(&DATA(iow)->strm); wandio_wwrite(DATA(iow)->child, DATA(iow)->outbuff, sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); wandio_wdestroy(DATA(iow)->child); free(iow->data); free(iow); }
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; }
static bool end(void *ud) { struct ctx *ctx = (struct ctx *)ud; int err; if (ctx->compress) { err = BZ2_bzCompressEnd(&ctx->zstr); } else { err = BZ2_bzDecompressEnd(&ctx->zstr); } if (err != BZ_OK) { zip_error_set(ctx->error, map_error(err), 0); return false; } return true; }
void import_compress_free(ImportCompress *c) { assert(c); if (c->type == IMPORT_COMPRESS_XZ) lzma_end(&c->xz); else if (c->type == IMPORT_COMPRESS_GZIP) { if (c->encoding) deflateEnd(&c->gzip); else inflateEnd(&c->gzip); } else if (c->type == IMPORT_COMPRESS_BZIP2) { if (c->encoding) BZ2_bzCompressEnd(&c->bzip2); else BZ2_bzDecompressEnd(&c->bzip2); } c->type = IMPORT_COMPRESS_UNKNOWN; }