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; }
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; }
value camlzip_bzCompress(value vzs, value srcbuf, value srcpos, value srclen, value dstbuf, value dstpos, value dstlen, value vflush) { #ifdef USE_BZIP2 bz_stream * zs = BZStream_val(vzs); int retcode; long used_in, used_out; value res; zs->next_in = &Byte(srcbuf, Long_val(srcpos)); zs->avail_in = Long_val(srclen); zs->next_out = &Byte(dstbuf, Long_val(dstpos)); zs->avail_out = Long_val(dstlen); retcode = BZ2_bzCompress(zs, camlzip_action_table[Int_val(vflush)]); if (retcode < 0) camlzip_bzerror("Bzlib.compress", retcode); used_in = Long_val(srclen) - zs->avail_in; used_out = Long_val(dstlen) - zs->avail_out; zs->next_in = NULL; /* not required, but cleaner */ zs->next_out = NULL; /* (avoid dangling pointers into Caml heap) */ res = alloc_small(3, 0); Field(res, 0) = Val_bool(retcode == BZ_STREAM_END); Field(res, 1) = Val_int(used_in); Field(res, 2) = Val_int(used_out); return res; #else failwith("Bzip2 compression not supported"); #endif }
CompressionStream<BZip2>::~CompressionStream(void) { char buf[256]; // Completed! Finish writing anything that remains to be written, and keep going // as long as zlib fills up the proffered output buffer. for (;;) { impl->strm.avail_in = 0; impl->strm.next_in = nullptr; impl->strm.next_out = buf; impl->strm.avail_out = static_cast<unsigned int>(sizeof(buf)); int ret = BZ2_bzCompress(&impl->strm, BZ_FINISH); switch (ret) { case BZ_STREAM_END: // Return case when we are at the end case BZ_FINISH_OK: // Return case when more data exists to be written // Handoff to lower level stream to complete the write os->Write(buf, sizeof(buf) - impl->strm.avail_out); if (ret == BZ_STREAM_END) // Clean return return; break; default: // Something went wrong, and we can't throw from here, so we just have to give up return; } } }
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); }
// Save to a file FXuval FXBZFileStream::writeBuffer(FXuval){ register FXival m,n; int bzerror; if(dir!=FXStreamSave){fxerror("FXBZFileStream::writeBuffer: wrong stream direction.\n");} FXASSERT(begptr<=rdptr); FXASSERT(rdptr<=wrptr); FXASSERT(wrptr<=endptr); while(rdptr<wrptr || ac==BZ_FINISH || ac==BZ_FLUSH){ bz->stream.next_in=(char*)rdptr; bz->stream.avail_in=wrptr-rdptr; bz->stream.next_out=bz->buffer; bz->stream.avail_out=BUFFERSIZE; bzerror=BZ2_bzCompress(&bz->stream,ac); // if(bzerror!=BZ_OK) break; if(bzerror<0) break; // break on error condition m=bz->stream.next_out-bz->buffer; n=file.writeBlock(bz->buffer,m); if(n<m) break; rdptr=(FXuchar*)bz->stream.next_in; if(bzerror==BZ_STREAM_END) break; // break from FINISH if(ac==BZ_FLUSH && bzerror==BZ_RUN_OK) break; // break from FLUSH } if(rdptr<wrptr){memmove(begptr,rdptr,wrptr-rdptr);} wrptr=begptr+(wrptr-rdptr); rdptr=begptr; return endptr-wrptr; }
static dsk_boolean dsk_bz2lib_compressor_process(DskOctetFilter *filter, DskBuffer *out, unsigned in_length, const uint8_t *in_data, DskError **error) { DskBz2libCompressor *compressor = (DskBz2libCompressor *) filter; DskBufferFragment *prev_last_frag; dsk_boolean added_fragment = DSK_FALSE; while (in_length > 0) { DskBufferFragment *f; uint8_t *out_start; int zrv; if (out->last_frag == NULL || !fragment_has_empty_space (out->last_frag)) { added_fragment = DSK_TRUE; prev_last_frag = out->last_frag; dsk_buffer_append_empty_fragment (out); } compressor->bz2lib.next_in = (char*)in_data; compressor->bz2lib.avail_in = in_length; f = out->last_frag; out_start = f->buf + f->buf_start + f->buf_length; compressor->bz2lib.next_out = (char *) out_start; compressor->bz2lib.avail_out = f->buf_max_size - f->buf_start - f->buf_length; zrv = BZ2_bzCompress (&compressor->bz2lib, BZ_RUN); if (zrv == BZ_OK) { unsigned amt_in = compressor->bz2lib.next_in - (char*)in_data; unsigned amt_out = compressor->bz2lib.next_out - (char*)out_start; in_length -= amt_in; in_data += amt_in; f->buf_length += amt_out; out->size += amt_out; } else { dsk_set_error (error, "error compressing: %s", bzrv_to_string (zrv)); dsk_buffer_maybe_remove_empty_fragment (out); return DSK_FALSE; } } /* If we added a fragment that we didn't use, remove it. */ if (added_fragment && out->last_frag->buf_length == 0) { dsk_buffer_fragment_free (out->last_frag); out->last_frag = prev_last_frag; if (out->last_frag == NULL) out->first_frag = NULL; } return DSK_TRUE; }
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; }
/* * Utility function to push input data through compressor, writing * full output blocks as necessary. * * Note that this handles both the regular write case (finishing == * false) and the end-of-archive case (finishing == true). */ static int drive_compressor(struct archive_write *a, struct private_data *state, int finishing) { ssize_t bytes_written; int ret; for (;;) { if (state->stream.avail_out == 0) { bytes_written = (a->client_writer)(&a->archive, a->client_data, state->compressed, state->compressed_buffer_size); if (bytes_written <= 0) { /* TODO: Handle this write failure */ return (ARCHIVE_FATAL); } else if ((size_t)bytes_written < state->compressed_buffer_size) { /* Short write: Move remainder to * front and keep filling */ memmove(state->compressed, state->compressed + bytes_written, state->compressed_buffer_size - bytes_written); } a->archive.raw_position += bytes_written; state->stream.next_out = state->compressed + state->compressed_buffer_size - bytes_written; state->stream.avail_out = bytes_written; } /* If there's nothing to do, we're done. */ if (!finishing && state->stream.avail_in == 0) return (ARCHIVE_OK); ret = BZ2_bzCompress(&(state->stream), finishing ? BZ_FINISH : BZ_RUN); switch (ret) { case BZ_RUN_OK: /* In non-finishing case, did compressor * consume everything? */ if (!finishing && state->stream.avail_in == 0) return (ARCHIVE_OK); break; case BZ_FINISH_OK: /* Finishing: There's more work to do */ break; case BZ_STREAM_END: /* Finishing: all done */ /* Only occurs in finishing case */ return (ARCHIVE_OK); default: /* Any other return value indicates an error */ archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, "Bzip2 compression failed;" " BZ2_bzCompress() returned %d", ret); return (ARCHIVE_FATAL); } } }
static dsk_boolean dsk_bz2lib_compressor_finish (DskOctetFilter *filter, DskBuffer *out, DskError **error) { DskBz2libCompressor *compressor = (DskBz2libCompressor *) filter; DskBufferFragment *prev_last_frag; dsk_boolean added_fragment = DSK_FALSE; prev_last_frag = NULL; // silence GCC for (;;) { DskBufferFragment *f; uint8_t *out_start; int zrv; if (out->last_frag == NULL || !fragment_has_empty_space (out->last_frag)) { added_fragment = DSK_TRUE; prev_last_frag = out->last_frag; dsk_buffer_append_empty_fragment (out); } compressor->bz2lib.next_in = NULL; compressor->bz2lib.avail_in = 0; f = out->last_frag; out_start = f->buf + f->buf_start + f->buf_length; compressor->bz2lib.next_out = (char*) out_start; compressor->bz2lib.avail_out = f->buf_max_size - f->buf_start - f->buf_length; zrv = BZ2_bzCompress (&compressor->bz2lib, BZ_FINISH); if (zrv == BZ_OK || zrv == BZ_STREAM_END) { unsigned amt_out = compressor->bz2lib.next_out - (char*)out_start; f->buf_length += amt_out; out->size += amt_out; if (zrv == BZ_STREAM_END) break; } else { dsk_set_error (error, "error finishing compression: %s", bzrv_to_string (zrv)); dsk_buffer_maybe_remove_empty_fragment (out); return DSK_FALSE; } } /* If we added a fragment that we didn't use, remove it. */ if (added_fragment && out->last_frag->buf_length == 0) { dsk_buffer_fragment_free (out->last_frag); out->last_frag = prev_last_frag; if (out->last_frag == NULL) out->first_frag = NULL; } return DSK_TRUE; }
int bz2compress(bz_stream *s, int action, char *in, unsigned *inlen, char *out, unsigned *outlen) { s->next_in = in; s->avail_in = *inlen; s->next_out = out, s->avail_out = *outlen; int r = BZ2_bzCompress(s, action); *inlen -= s->avail_in; *outlen -= s->avail_out; return r; }
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(); }
/* ====================================================================== * subroutine functions for KBZipFile methods */ static rc_t KBZipFileWriteInt (KBZipFile *self, int action, size_t *pnumwrit) { bz_stream *strm; unsigned avail_in; int ret; rc_t rc = 0; assert (self); assert (pnumwrit); *pnumwrit = 0; strm = &self->strm; avail_in = strm->avail_in; ret = 0; /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ do { uint32_t num_comp; size_t written; int zret; /* compress one internal buffers worth */ strm->next_out = self->buff; strm->avail_out = sizeof (self->buff); zret = BZ2_bzCompress (strm, action); /* no bad return value */ /* state not clobbered */ assert(zret == BZ_OK || zret == BZ_RUN_OK || zret == BZ_FINISH_OK || zret == BZ_STREAM_END); /* compression used the sizeof of the outbuffer - the amount * it says it didn't use */ num_comp = sizeof(self->buff) - strm->avail_out; rc = KFileWrite (self->file, self->filePosition, self->buff, num_comp, &written); self->filePosition += written; *pnumwrit = avail_in - strm->avail_in; } while (strm->avail_out == 0); assert (strm->avail_in == 0); /* all input will be used */ return rc; }
static int o_stream_bzlib_send_flush(struct bzlib_ostream *zstream) { bz_stream *zs = &zstream->zs; unsigned int len; bool done = FALSE; int ret; if (zs->avail_in != 0) { i_assert(zstream->ostream.ostream.last_failed_errno != 0); zstream->ostream.ostream.stream_errno = zstream->ostream.ostream.last_failed_errno; return -1; } if (zstream->flushed) return 0; if ((ret = o_stream_flush_parent_if_needed(&zstream->ostream)) <= 0) return ret; if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0) return ret; i_assert(zstream->outbuf_used == 0); do { len = sizeof(zstream->outbuf) - zs->avail_out; if (len != 0) { zs->next_out = zstream->outbuf; zs->avail_out = sizeof(zstream->outbuf); zstream->outbuf_used = len; if ((ret = o_stream_zlib_send_outbuf(zstream)) <= 0) return ret; if (done) break; } ret = BZ2_bzCompress(zs, BZ_FINISH); switch (ret) { case BZ_STREAM_END: done = TRUE; break; case BZ_FINISH_OK: break; default: i_unreached(); } } while (zs->avail_out != sizeof(zstream->outbuf)); zstream->flushed = TRUE; return 0; }
static int stream_bzip2_flush(server *srv, connection *con, handler_ctx *hctx, int end) { bz_stream * const bz = &(hctx->u.bz); const plugin_data *p = hctx->plugin_data; size_t len; int rc; int done; /* compress data */ do { done = 1; if (end) { rc = BZ2_bzCompress(bz, BZ_FINISH); if (rc == BZ_FINISH_OK) { done = 0; } else if (rc != BZ_STREAM_END) { return -1; } } else if (bz->avail_in > 0) { /* p->conf.sync_flush not implemented here, * which would loop on BZ_FLUSH while BZ_FLUSH_OK * until BZ_RUN_OK returned */ rc = BZ2_bzCompress(bz, BZ_RUN); if (rc != BZ_RUN_OK) { return -1; } } len = hctx->output->size - bz->avail_out; if (bz->avail_out == 0 || (len > 0 && (end || p->conf.sync_flush))) { hctx->bytes_out += len; stream_http_chunk_append_mem(srv, con, hctx, len); bz->next_out = hctx->output->ptr; bz->avail_out = hctx->output->size; } } while (bz->avail_in != 0 || !done); return 0; }
/* * Utility function to push input data through compressor, writing * full output blocks as necessary. * * Note that this handles both the regular write case (finishing == * false) and the end-of-archive case (finishing == true). */ static int drive_compressor(struct archive_write_filter *f, struct private_data *data, int finishing) { int ret; for (;;) { if (data->stream.avail_out == 0) { ret = __archive_write_filter(f->next_filter, data->compressed, data->compressed_buffer_size); if (ret != ARCHIVE_OK) { /* TODO: Handle this write failure */ return (ARCHIVE_FATAL); } data->stream.next_out = data->compressed; data->stream.avail_out = data->compressed_buffer_size; } /* If there's nothing to do, we're done. */ if (!finishing && data->stream.avail_in == 0) return (ARCHIVE_OK); ret = BZ2_bzCompress(&(data->stream), finishing ? BZ_FINISH : BZ_RUN); switch (ret) { case BZ_RUN_OK: /* In non-finishing case, did compressor * consume everything? */ if (!finishing && data->stream.avail_in == 0) return (ARCHIVE_OK); break; case BZ_FINISH_OK: /* Finishing: There's more work to do */ break; case BZ_STREAM_END: /* Finishing: all done */ /* Only occurs in finishing case */ return (ARCHIVE_OK); default: /* Any other return value indicates an error */ archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER, "Bzip2 compression failed;" " BZ2_bzCompress() returned %d", ret); return (ARCHIVE_FATAL); } } }
bool CompressionStream<BZip2>::Transform(const void* input, size_t& ncbIn, void* output, size_t& ncbOut, bool flush) { impl->strm.avail_in = static_cast<unsigned int>(ncbIn); impl->strm.next_in = const_cast<char*>(reinterpret_cast<const char*>(input)); // Compress one, update results impl->strm.next_out = reinterpret_cast<char*>(output); impl->strm.avail_out = static_cast<unsigned int>(ncbOut); int rs; do { rs = BZ2_bzCompress(&impl->strm, flush ? BZ_FLUSH : BZ_RUN); } while (rs == BZ_FLUSH_OK && impl->strm.avail_out > 0); ncbIn -= impl->strm.avail_in; ncbOut -= impl->strm.avail_out; return rs == BZ_RUN_OK; }
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; }
size_t wxBZipOutputStream::OnSysWrite(const void* buffer, size_t bufsize) { bz_stream* hZip = (bz_stream*)m_hZip; hZip->next_in = (char*)buffer; hZip->avail_in = bufsize; hZip->next_out = m_pBuffer; hZip->avail_out = WXBZBS; size_t nWrote = 0; int nRet = BZ_RUN_OK; // for the 'while' statement while( nRet== BZ_RUN_OK && hZip->avail_in > 0 ) { // Compress the data in buffer, resulting data -> pbuffer nRet = BZ2_bzCompress(hZip, BZ_RUN); if (nRet != BZ_RUN_OK) { wxLogDebug(wxT("Error from BZ2_bzCompress in Write()")); return 0; } // This is how much newly compressed data is available size_t nCurWrite = WXBZBS - hZip->avail_out; if (nCurWrite != 0) { // Deliver the compressed data to the parent stream WriteRaw(m_pBuffer, nCurWrite); if (m_parent_o_stream->LastWrite() != nCurWrite) { wxLogDebug(wxT("Error writing to underlying stream")); break; } //Reset the buffer hZip->avail_out = WXBZBS; hZip->next_out = m_pBuffer; nWrote += nCurWrite; } } // I'm not sure if this is necessary as, if it worked, // the loop continued until avail_in was zero nWrote = bufsize - hZip->avail_in ; return nWrote; }
/*++ 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 off_t bz_wwrite(iow_t *iow, const char *buffer, off_t len) { if (DATA(iow)->err == ERR_EOF) { return 0; /* EOF */ } if (DATA(iow)->err == ERR_ERROR) { return -1; /* ERROR! */ } DATA(iow)->strm.next_in = (char*)buffer; DATA(iow)->strm.avail_in = len; while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) { while (DATA(iow)->strm.avail_out <= 0) { int bytes_written = wandio_wwrite(DATA(iow)->child, DATA(iow)->outbuff, sizeof(DATA(iow)->outbuff)); if (bytes_written <= 0) { /* Error */ DATA(iow)->err = ERR_ERROR; /* Return how much data we managed to write ok */ if (DATA(iow)->strm.avail_in != (uint32_t)len) { return len-DATA(iow)->strm.avail_in; } /* Now return error */ return -1; } DATA(iow)->strm.next_out = DATA(iow)->outbuff; DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); } /* Decompress some data into the output buffer */ int err=BZ2_bzCompress(&DATA(iow)->strm, 0); switch(err) { case BZ_RUN_OK: case BZ_OK: DATA(iow)->err = ERR_OK; break; default: DATA(iow)->err = ERR_ERROR; break; } } /* Return the number of bytes compressed */ return len-DATA(iow)->strm.avail_in; }
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 zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length) { struct ctx *ctx = (struct ctx *)ud; int ret; if (ctx->zstr.avail_in == 0 && !ctx->end_of_input) { *length = 0; return ZIP_COMPRESSION_NEED_DATA; } ctx->zstr.avail_out = (unsigned int)ZIP_MIN(UINT_MAX, *length); ctx->zstr.next_out = (char *)data; if (ctx->compress) { ret = BZ2_bzCompress(&ctx->zstr, ctx->end_of_input ? BZ_FINISH : BZ_RUN); } else { ret = BZ2_bzDecompress(&ctx->zstr); } *length = *length - ctx->zstr.avail_out; switch (ret) { case BZ_FINISH_OK: /* compression */ return ZIP_COMPRESSION_OK; case BZ_OK: /* decompression */ case BZ_RUN_OK: /* compression */ if (ctx->zstr.avail_in == 0) { return ZIP_COMPRESSION_NEED_DATA; } return ZIP_COMPRESSION_OK; case BZ_STREAM_END: return ZIP_COMPRESSION_END; default: zip_error_set(ctx->error, map_error(ret), 0); return ZIP_COMPRESSION_ERROR; } }
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 gboolean gsf_output_bzip_write (GsfOutput *output, size_t num_bytes, guint8 const *data) { #ifdef HAVE_BZ2 GsfOutputBzip *bzip = GSF_OUTPUT_BZIP (output); g_return_val_if_fail (data, FALSE); bzip->stream.next_in = (unsigned char *) data; bzip->stream.avail_in = num_bytes; while (bzip->stream.avail_in > 0) { int zret; if (bzip->stream.avail_out == 0) { if (!bzip_output_block (bzip)) return FALSE; } zret = BZ2_bzCompress (&bzip->stream, BZ_RUN); if (zret != BZ_RUN_OK) { g_warning ("Unexpected error code %d from bzlib during compression.", zret); return FALSE; } } if (bzip->stream.avail_out == 0) { if (!bzip_output_block (bzip)) return FALSE; } return TRUE; #else (void)output; (void)num_bytes; (void)data; return FALSE; #endif }
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; }
bool Samurai::IO::BZip2Compressor::exec(char* input, size_t& input_len, char* output, size_t& output_len) { if (!output_len || !d) return false; d->stream->avail_in = input_len; d->stream->next_in = input; d->stream->avail_out = output_len; d->stream->next_out = output; int action = (input_len) ? BZ_RUN : BZ_FINISH; int retval = BZ2_bzCompress(d->stream, action); if (retval == BZ_RUN_OK || retval == BZ_FINISH_OK || retval == BZ_STREAM_END) { output_len -= d->stream->avail_out; input_len -= d->stream->avail_in; return true; } return false; }
bool wxBZipOutputStream::Close() // Flushes any remaining compressed data { bz_stream* hZip = (bz_stream*)m_hZip; int nRet = BZ_FINISH_OK; while ( nRet == BZ_FINISH_OK ) { hZip->next_out = m_pBuffer; hZip->avail_out = WXBZBS; // Call BZ2_bzCompress with the parameter BZ_FINISH int nRet = BZ2_bzCompress(hZip, BZ_FINISH); if (nRet != BZ_FINISH_OK && nRet != BZ_STREAM_END) { wxLogDebug(wxT("BZ2_bzCompress error in Close()")); break; } size_t nCurWrite = WXBZBS - hZip->avail_out; if (nCurWrite != 0) { WriteRaw(m_pBuffer, nCurWrite); if (m_parent_o_stream->LastWrite() != nCurWrite) { wxLogDebug(wxT("Error writing to underlying ") wxT("stream during the Close() phase")); break; } } if (nRet == BZ_STREAM_END) { return true; //hrm, restructure here??? } } return false; }