size_t wxBZipInputStream::OnSysRead(void* buffer, size_t bufsize) { bz_stream* hZip = (bz_stream*)m_hZip; hZip->next_out = (char*)buffer; hZip->avail_out = bufsize; while (hZip->avail_out != 0) { if (m_nBufferPos == 0 || m_nBufferPos == WXBZBS) { ReadRaw(m_pBuffer, WXBZBS); m_nBufferPos = 0; hZip->next_in = m_pBuffer; hZip->avail_in = WXBZBS; if (m_parent_i_stream->LastRead() != WXBZBS) { // Full amount not read, so do a last // minute tidy up and decompress what is left hZip->avail_in = m_parent_i_stream->LastRead(); int nRet = BZ2_bzDecompress(hZip); if (nRet == BZ_OK || nRet == BZ_STREAM_END) return bufsize - hZip->avail_out; else return 0; } } // Buffer full, decompress some bytes hZip->next_in = &m_pBuffer[m_nBufferPos]; hZip->avail_in = WXBZBS - m_nBufferPos; int nRet = BZ2_bzDecompress(hZip); if (nRet == BZ_OK) { m_nBufferPos = WXBZBS - hZip->avail_in; } else if(nRet == BZ_STREAM_END) return bufsize - hZip->avail_out; else return 0; } return bufsize - hZip->avail_out; }
size_t wxBZipInputStream::OnSysRead(void* buffer, size_t bufsize) { wxInt32 nRead = 0; ((bz_stream*&)hZip)->next_out = &(((char*&)buffer)[nRead]); ((bz_stream*&)hZip)->avail_out = bufsize - nRead; while (((bz_stream*&)hZip)->avail_out != 0) { //wxMessageBox(wxString::Format("%i %i", nRead, ((bz_stream*&)hZip)->avail_out)); if (nBufferPos == 0 || nBufferPos == WXBZBS) { ReadRaw(pBuffer, WXBZBS); nBufferPos = 0; ((bz_stream*&)hZip)->next_in = &pBuffer[nBufferPos]; ((bz_stream*&)hZip)->avail_in = WXBZBS - nBufferPos; if (m_parent_i_stream->LastRead() != WXBZBS) { ((bz_stream*&)hZip)->avail_in = m_parent_i_stream->LastRead(); int nRet = BZ2_bzDecompress((bz_stream*&)hZip); if (nRet == BZ_OK || nRet == BZ_STREAM_END) return bufsize - ((bz_stream*&)hZip)->avail_out; else return 0; } } ((bz_stream*&)hZip)->next_in = &pBuffer[nBufferPos]; ((bz_stream*&)hZip)->avail_in = WXBZBS - nBufferPos; int nRet = BZ2_bzDecompress((bz_stream*&)hZip); if (nRet == BZ_OK) { nBufferPos += -(nRead - ( nRead += (WXBZBS - nBufferPos - ((bz_stream*&)hZip)->avail_in) )); } else if(nRet == BZ_STREAM_END) return bufsize - ((bz_stream*&)hZip)->avail_out; else return 0; } return bufsize - ((bz_stream*&)hZip)->avail_out; }
static int bzf_read(struct open_file *f, void *buf, size_t size, size_t *resid) { struct bz_file *bzf = (struct bz_file *)f->f_fsdata; int error; bzf->bzf_bzstream.next_out = buf; /* where and how much */ bzf->bzf_bzstream.avail_out = size; while (bzf->bzf_bzstream.avail_out && bzf->bzf_endseen == 0) { if ((bzf->bzf_bzstream.avail_in == 0) && (bzf_fill(bzf) == -1)) { printf("bzf_read: fill error\n"); return(EIO); } if (bzf->bzf_bzstream.avail_in == 0) { /* oops, unexpected EOF */ printf("bzf_read: unexpected EOF\n"); if (bzf->bzf_bzstream.avail_out == size) return(EIO); break; } error = BZ2_bzDecompress(&bzf->bzf_bzstream); /* decompression pass */ if (error == BZ_STREAM_END) { /* EOF, all done */ bzf->bzf_endseen = 1; break; } if (error != BZ_OK) { /* argh, decompression error */ printf("bzf_read: BZ2_bzDecompress returned %d\n", error); return(EIO); } } if (resid != NULL) *resid = bzf->bzf_bzstream.avail_out; return(0); }
// Load from file FXuval FXBZFileStream::readBuffer(FXuval){ register FXival n; int bzerror; if(dir!=FXStreamLoad){fxerror("FXBZFileStream::readBuffer: wrong stream direction.\n");} FXASSERT(begptr<=rdptr); FXASSERT(rdptr<=wrptr); FXASSERT(wrptr<=endptr); if(rdptr<wrptr){memmove(begptr,rdptr,wrptr-rdptr);} wrptr=begptr+(wrptr-rdptr); rdptr=begptr; while(wrptr<endptr){ // n=file.readBlock(bz->buffer,BUFFERSIZE); // if(n<=0) break; // bz->stream.next_in=bz->buffer; // bz->stream.avail_in=n; if(bz->stream.avail_in<=0){ // get more input if buffer is empty n=file.readBlock(bz->buffer,BUFFERSIZE); if(n<0) break; bz->stream.next_in=bz->buffer; bz->stream.avail_in=n; } bz->stream.next_out=(char*)wrptr; bz->stream.avail_out=endptr-wrptr; bzerror=BZ2_bzDecompress(&bz->stream); // if(bzerror!=BZ_OK) break; if(bzerror<0) break; // break on error condition wrptr=(FXuchar*)bz->stream.next_out; if(bzerror==BZ_STREAM_END) break; } return wrptr-rdptr; }
long FileReaderBZ2::Read (void *buffer, long len) { int err; Stream.next_out = (char *)buffer; Stream.avail_out = len; do { err = BZ2_bzDecompress(&Stream); if (Stream.avail_in == 0 && !SawEOF) { FillBuffer (); } } while (err == BZ_OK && Stream.avail_out != 0); if (err != BZ_OK && err != BZ_STREAM_END) { I_Error ("Corrupt bzip2 stream"); } if (Stream.avail_out != 0) { I_Error ("Ran out of data in bzip2 stream"); } return len - Stream.avail_out; }
std::string read() final { std::string output; if (m_buffer) { const size_t buffer_size = 10240; output.resize(buffer_size); m_bzstream.next_out = const_cast<char*>(output.data()); m_bzstream.avail_out = buffer_size; int result = BZ2_bzDecompress(&m_bzstream); if (result != BZ_OK) { m_buffer = nullptr; m_buffer_size = 0; } if (result != BZ_OK && result != BZ_STREAM_END) { std::string message("bzip2 error: decompress failed: "); throw bzip2_error(message, result); } output.resize(static_cast<unsigned long>(m_bzstream.next_out - output.data())); } return output; }
/* this function decompress a stream using bzip2 library. */ int32_t libmpq__decompress_bzip2(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) { /* some common variables. */ int32_t result = 0; int32_t tb = 0; bz_stream strm; /* initialize the bzlib decompression. */ strm.bzalloc = NULL; strm.bzfree = NULL; /* initialize the structure. */ if ((result = BZ2_bzDecompressInit(&strm, 0, 0)) != BZ_OK) { /* something on bzlib initialization failed. */ return result; } /* fill the stream structure for bzlib. */ strm.next_in = (char *)in_buf; strm.avail_in = in_size; strm.next_out = (char *)out_buf; strm.avail_out = out_size; /* do the decompression. */ while (BZ2_bzDecompress(&strm) != BZ_STREAM_END); /* save transferred bytes. */ tb = strm.total_out_lo32; /* cleanup of bzip stream. */ BZ2_bzDecompressEnd(&strm); /* return transferred bytes. */ return tb; }
value camlzip_bzDecompress(value vzs, value srcbuf, value srcpos, value srclen, value dstbuf, value dstpos, value dstlen) { #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_bzDecompress(zs); if (retcode < 0) camlzip_bzerror("Bzlib.decompress", 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 }
// xmlInputReadCallback static int bz2_mem_read(struct bz2_mem *bzmem, char *buffer, int len) { if (len < 1) { // ensure that at least one byte of output space is available at each BZ2_bzDecompress call. return 0; } if (bzmem->eof) { // If we run BZ2_bzDecompress on processed buffer we will get -1 (SEQUENCE_ERROR) return 0; } // next_out should point to a buffer in which the uncompressed output is to be placed bzmem->stream->next_out = buffer; // with avail_out indicating how much output space is available. bzmem->stream->avail_out = len; int bzerror = BZ2_bzDecompress(bzmem->stream); if (bzerror == BZ_STREAM_END) { bzmem->eof = true; } if (bzerror == BZ_OK || bzerror == BZ_STREAM_END) return (len - bzmem->stream->avail_out); else { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not read from bz_stream: BZ2_bzDecompress returns %d", bzerror); return -1; } }
bool bz2_decompress_xml(char *in_data, int in_data_length, BYTE **pDat, int *data_length) { const int BLOCKSIZE = 1024 * 100; bz_stream bzs = {0}; switch(BZ2_bzDecompressInit(&bzs, 0, 0)) { case BZ_CONFIG_ERROR: //MessageBox(0, "Configuration Error", "BZ2 Decompres Init", MB_OK | MB_ICONERROR); ShowError(TranslateT("BZ2 Decompression, configuration error")); return false; case BZ_PARAM_ERROR: //MessageBox(0, "Parameters Error", "BZ2 Decompres Init", MB_OK | MB_ICONERROR); ShowError(TranslateT("BZ2 Decompression, parameter error")); return false; case BZ_MEM_ERROR: //MessageBox(0, "Memory Error", "BZ2 Decompres Init", MB_OK | MB_ICONERROR); ShowError(TranslateT("DB2 Decompression, memory error")); return false; } bzs.avail_in = in_data_length; bzs.next_in = in_data; bzs.avail_out = BLOCKSIZE; *pDat = (BYTE *)malloc(bzs.avail_out + 1); // allocate 100k (at present, xml data is about 87k) (1 byte extra for a terminating 0 for safety) bzs.next_out = (char *)*pDat; int blocknum = 0; int ret; while((ret = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) { if(bzs.avail_out == 0) { blocknum++; *pDat = (BYTE *)realloc(*pDat, (blocknum + 1) * BLOCKSIZE + 1); bzs.next_out = (char *)(*pDat + (blocknum * BLOCKSIZE)); bzs.avail_out = BLOCKSIZE; } } BZ2_bzDecompressEnd(&bzs); if(ret != BZ_STREAM_END) { // char msg[512]; // sprintf(msg, "Error decompressing, code: %d", ret); // MessageBox(0, msg, "Error Decompressing BZ2 XML data", MB_OK); free(*pDat); *pDat = 0; *data_length = 0; return false; } *data_length = bzs.total_out_lo32; // assume it's not too massive! (*pDat)[*data_length] = 0; // for safety - last char shouldn't matter to us //char msg[256]; //sprintf(msg, "Bytes decompressed: %d", data_length); //MessageBox(0, msg, "msg", MB_OK); return true; }
static dsk_boolean dsk_bz2lib_decompressor_finish (DskOctetFilter *filter, DskBuffer *out, DskError **error) { DskBz2libDecompressor *decompressor = (DskBz2libDecompressor *) filter; DskBufferFragment *prev_last_frag; dsk_boolean added_fragment = DSK_FALSE; if (decompressor->input_ended) return DSK_TRUE; 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); } decompressor->bz2lib.next_in = NULL; decompressor->bz2lib.avail_in = 0; f = out->last_frag; out_start = f->buf + f->buf_start + f->buf_length; decompressor->bz2lib.next_out = (char*) out_start; decompressor->bz2lib.avail_out = f->buf_max_size - f->buf_start - f->buf_length; zrv = BZ2_bzDecompress (&decompressor->bz2lib); if (zrv == BZ_OK || zrv == BZ_STREAM_END) { unsigned amt_out = decompressor->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 decompression: %s [%d]", bzrv_to_string (zrv), 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_bzdecode_impl(const char *ptr, int size, int *sp) { bz_stream zs; char *buf, *swap, obuf[BZIPBUFSIZ]; int rv, asiz, bsiz, osiz; zs.bzalloc = NULL; zs.bzfree = NULL; zs.opaque = NULL; if(BZ2_bzDecompressInit(&zs, 0, 0) != BZ_OK) return NULL; asiz = size * 2 + 16; if(asiz < BZIPBUFSIZ) asiz = BZIPBUFSIZ; if(!(buf = malloc(asiz))) { BZ2_bzDecompressEnd(&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_bzDecompress(&zs)) == BZ_OK) { osiz = BZIPBUFSIZ - zs.avail_out; if(bsiz + osiz >= asiz) { asiz = asiz * 2 + osiz; if(!(swap = realloc(buf, asiz))) { free(buf); BZ2_bzDecompressEnd(&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_bzDecompressEnd(&zs); return NULL; } osiz = BZIPBUFSIZ - zs.avail_out; if(bsiz + osiz >= asiz) { asiz = asiz * 2 + osiz; if(!(swap = realloc(buf, asiz))) { free(buf); BZ2_bzDecompressEnd(&zs); return NULL; } buf = swap; } memcpy(buf + bsiz, obuf, osiz); bsiz += osiz; buf[bsiz] = '\0'; if(sp) *sp = bsiz; BZ2_bzDecompressEnd(&zs); return buf; }
/*---------------------------------------------------*/ int BZ_API(BZ2_bzRead) ( int* bzerror, BZFILE* b, void* buf, int len ) { Int32 n, ret; bzFile* bzf = (bzFile*)b; BZ_SETERR(BZ_OK); if (bzf == NULL || buf == NULL || len < 0) { BZ_SETERR(BZ_PARAM_ERROR); return 0; }; if (bzf->writing) { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; }; if (len == 0) { BZ_SETERR(BZ_OK); return 0; }; bzf->strm.avail_out = len; bzf->strm.next_out = buf; while (True) { if (ferror(bzf->handle)) { BZ_SETERR(BZ_IO_ERROR); return 0; }; if (bzf->strm.avail_in == 0 && !bz_feof(bzf->handle)) { n = fread ( bzf->buf, sizeof(UChar), BZ_MAX_UNUSED, bzf->handle ); if (ferror(bzf->handle)) { BZ_SETERR(BZ_IO_ERROR); return 0; }; bzf->bufN = n; bzf->strm.avail_in = bzf->bufN; bzf->strm.next_in = bzf->buf; } ret = BZ2_bzDecompress ( &(bzf->strm) ); if (ret != BZ_OK && ret != BZ_STREAM_END) { BZ_SETERR(ret); return 0; }; if (ret == BZ_OK && bz_feof(bzf->handle) && bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0) { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; }; if (ret == BZ_STREAM_END) { BZ_SETERR(BZ_STREAM_END); return len - bzf->strm.avail_out; }; if (bzf->strm.avail_out == 0) { BZ_SETERR(BZ_OK); return len; }; } }
int bzip2_decompress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen, int level, uchar_t chdr, void *data) { bz_stream bzs; int ret; unsigned int slen, dlen; uint64_t _srclen = srclen; uint64_t _dstlen = *dstlen; uchar_t *dst1 = dst; uchar_t *src1 = src; bzs.bzalloc = slab_alloc_i; bzs.bzfree = slab_free; bzs.opaque = NULL; ret = BZ2_bzDecompressInit(&bzs, 0, 0); if (ret != BZ_OK) { bzerr(ret); return (-1); } while (_srclen > 0) { if (_srclen > SINGLE_CALL_MAX) { slen = SINGLE_CALL_MAX; } else { slen = _srclen; } if (_dstlen > SINGLE_CALL_MAX) { dlen = SINGLE_CALL_MAX; } else { dlen = _dstlen; } bzs.next_in = src1; bzs.avail_in = slen; bzs.next_out = dst1; bzs.avail_out = dlen; ret = BZ2_bzDecompress(&bzs); if (ret != BZ_OK && ret != BZ_STREAM_END) { BZ2_bzDecompressEnd(&bzs); bzerr(ret); return (-1); } dst1 += (dlen - bzs.avail_out); _dstlen -= (dlen - bzs.avail_out); src1 += (slen - bzs.avail_in); _srclen -= (slen - bzs.avail_in); } /* normal termination */ *dstlen = *dstlen - _dstlen; BZ2_bzDecompressEnd(&bzs); return (0); }
void BZ2Decompress(Stream& out, Stream& in, Gate2<int, int> progress) { enum { BUF_SIZE = 65536 }; Buffer<char> input(BUF_SIZE), output(BUF_SIZE); int avail = in.Get(input, BUF_SIZE); if(avail == 0) return; bz_stream z; Zero(z); z.bzalloc = bzalloc_new; z.bzfree = bzfree_new; z.opaque = 0; if(BZ2_bzDecompressInit(&z, 0, 0) != BZ_OK) { out.SetError(); return; } z.next_in = input; z.avail_in = avail; z.next_out = output; z.avail_out = BUF_SIZE; int code; bool running = true; int64 total = in.GetLeft(); int done = 0; do { if(z.avail_in == 0 && running) { if((z.avail_in = in.Get(z.next_in = input, BUF_SIZE)) == 0) running = false; done += z.avail_in; if(progress(done, (int)total) || in.IsError()) { BZ2_bzDecompressEnd(&z); out.SetError(); return; } } code = BZ2_bzDecompress(&z); if(z.avail_out == 0) { out.Put(z.next_out = output, z.avail_out = BUF_SIZE); if(out.IsError()) { BZ2_bzDecompressEnd(&z); return; } } } while(code == BZ_OK); if(z.avail_out < BUF_SIZE) out.Put(output, BUF_SIZE - z.avail_out); BZ2_bzDecompressEnd(&z); }
static int do_uncompress( compress_filter_context_t *zfx, bz_stream *bzs, IOBUF a, size_t *ret_len ) { int zrc; int rc=0; size_t n; int nread, count; int refill = !bzs->avail_in; if( DBG_FILTER ) log_debug("begin bzDecompress: avail_in=%u, avail_out=%u, inbuf=%u\n", (unsigned)bzs->avail_in, (unsigned)bzs->avail_out, (unsigned)zfx->inbufsize ); do { if( bzs->avail_in < zfx->inbufsize && refill ) { n = bzs->avail_in; if( !n ) bzs->next_in = zfx->inbuf; count = zfx->inbufsize - n; nread = iobuf_read( a, zfx->inbuf + n, count ); if( nread == -1 ) nread = 0; n += nread; bzs->avail_in = n; } refill = 1; if( DBG_FILTER ) log_debug("enter bzDecompress: avail_in=%u, avail_out=%u\n", (unsigned)bzs->avail_in, (unsigned)bzs->avail_out); zrc=BZ2_bzDecompress(bzs); if( DBG_FILTER ) log_debug("leave bzDecompress: avail_in=%u, avail_out=%u, zrc=%d\n", (unsigned)bzs->avail_in, (unsigned)bzs->avail_out, zrc); if( zrc == BZ_STREAM_END ) rc = -1; /* eof */ else if( zrc != BZ_OK && zrc != BZ_PARAM_ERROR ) log_fatal("bz2lib inflate problem: rc=%d\n", zrc ); } while( bzs->avail_out && zrc != BZ_STREAM_END && zrc != BZ_PARAM_ERROR ); /* I'm not completely happy with the two uses of BZ_PARAM_ERROR here. The corresponding zlib function is Z_BUF_ERROR, which covers a narrower scope than BZ_PARAM_ERROR. -dshaw */ *ret_len = zfx->outbufsize - bzs->avail_out; if( DBG_FILTER ) log_debug("do_uncompress: returning %u bytes\n", (unsigned)*ret_len ); return rc; }
int ArchiveReader::ExtractItemToStream(const MarItem *item, FILE *fp) { /* decompress the data chunk by chunk */ bz_stream strm; int offset, inlen, outlen, ret = OK; memset(&strm, 0, sizeof(strm)); if (BZ2_bzDecompressInit(&strm, 0, 0) != BZ_OK) return UNEXPECTED_BZIP_ERROR; offset = 0; for (;;) { if (!item->length) { ret = UNEXPECTED_MAR_ERROR; break; } if (offset < (int) item->length && strm.avail_in == 0) { inlen = mar_read(mArchive, item, offset, inbuf, inbuf_size); if (inlen <= 0) return READ_ERROR; offset += inlen; strm.next_in = inbuf; strm.avail_in = inlen; } strm.next_out = outbuf; strm.avail_out = outbuf_size; ret = BZ2_bzDecompress(&strm); if (ret != BZ_OK && ret != BZ_STREAM_END) { ret = UNEXPECTED_BZIP_ERROR; break; } outlen = outbuf_size - strm.avail_out; if (outlen) { if (fwrite(outbuf, outlen, 1, fp) != 1) { ret = WRITE_ERROR_EXTRACT; break; } } if (ret == BZ_STREAM_END) { ret = OK; break; } } BZ2_bzDecompressEnd(&strm); return ret; }
/*---------------------------------------------------*/ int BZ_API(BZ2_bzBuffToBuffDecompress) ( char* dest, unsigned int* destLen, char* source, unsigned int sourceLen, int small, int verbosity ) { bz_stream strm; int ret; if (dest == NULL || destLen == NULL || source == NULL || (small != 0 && small != 1) || verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR; strm.bzalloc = NULL; strm.bzfree = NULL; strm.opaque = NULL; ret = BZ2_bzDecompressInit ( &strm, verbosity, small ); if (ret != BZ_OK) return ret; strm.next_in = source; strm.next_out = dest; strm.avail_in = sourceLen; strm.avail_out = *destLen; ret = BZ2_bzDecompress ( &strm ); if (ret == BZ_OK) goto output_overflow_or_eof; if (ret != BZ_STREAM_END) goto errhandler; /* normal termination */ *destLen -= strm.avail_out; BZ2_bzDecompressEnd ( &strm ); return BZ_OK; output_overflow_or_eof: if (strm.avail_out > 0) { BZ2_bzDecompressEnd ( &strm ); return BZ_UNEXPECTED_EOF; } else { BZ2_bzDecompressEnd ( &strm ); return BZ_OUTBUFF_FULL; }; errhandler: BZ2_bzDecompressEnd ( &strm ); return ret; }
size_t bzRead(int *bzerr, BZStream* stream, unsigned char* out, size_t len) { size_t toRead; size_t haveRead; size_t total; total = len; *bzerr = BZ_OK; while(total > 0) { if(!stream->ended) { memmove(stream->inBuffer, stream->bz2.next_in, stream->bz2.avail_in); stream->file->seek(stream->file, stream->offset); haveRead = stream->file->read(stream->file, stream->inBuffer + stream->bz2.avail_in, stream->bufferLen - stream->bz2.avail_in); stream->offset += haveRead; stream->bz2.avail_in += haveRead; stream->bz2.next_in = (char*) stream->inBuffer; *bzerr = BZ2_bzDecompress(&(stream->bz2)); if(*bzerr == BZ_STREAM_END) { stream->ended = TRUE; } else { if(*bzerr != BZ_OK) { return 0; } } } if(total > (stream->bufferLen - stream->bz2.avail_out)) { toRead = stream->bufferLen - stream->bz2.avail_out; } else { toRead = total; } memcpy(out, stream->outBuffer, toRead); memmove(stream->outBuffer, stream->outBuffer + toRead, stream->bufferLen - toRead); stream->bz2.next_out -= toRead; stream->bz2.avail_out += toRead; out += toRead; total -= toRead; if(total > 0 && stream->ended) { return (len - total); } } return len; }
static off_t bz_read(io_t *io, void *buffer, off_t len) { if (DATA(io)->err == ERR_EOF) return 0; /* EOF */ if (DATA(io)->err == ERR_ERROR) { errno=EIO; return -1; /* ERROR! */ } DATA(io)->strm.avail_out = len; DATA(io)->strm.next_out = buffer; while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) { while (DATA(io)->strm.avail_in <= 0) { int bytes_read = wandio_read(DATA(io)->parent, DATA(io)->inbuff, sizeof(DATA(io)->inbuff)); if (bytes_read == 0) /* EOF */ return len-DATA(io)->strm.avail_out; if (bytes_read < 0) { /* Error */ /* Errno should already be set */ DATA(io)->err = ERR_ERROR; /* Return how much data we managed to read ok */ if (DATA(io)->strm.avail_out != (uint32_t)len) { return len-DATA(io)->strm.avail_out; } /* Now return error */ return -1; } DATA(io)->strm.next_in = DATA(io)->inbuff; DATA(io)->strm.avail_in = bytes_read; } /* Decompress some data into the output buffer */ int err=BZ2_bzDecompress(&DATA(io)->strm); switch(err) { case BZ_OK: DATA(io)->err = ERR_OK; break; case BZ_STREAM_END: DATA(io)->err = ERR_EOF; break; default: errno=EIO; DATA(io)->err = ERR_ERROR; } } /* Return the number of bytes decompressed */ return len-DATA(io)->strm.avail_out; }
int FillBuffer(unsigned char* buffer, int size, bz_stream* stream) { stream->next_out = (char*)buffer; stream->avail_out = size; while (stream->avail_out > 0) { int bzerr = BZ2_bzDecompress(stream); if (bzerr != BZ_OK && bzerr != BZ_STREAM_END) { printf("bz error %d decompressing\n", bzerr); return -1; } if (stream->avail_out > 0) { printf("need %d more bytes\n", stream->avail_out); } } return 0; }
torch::Data Bz2::Decompress(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() * 2); /* next_in should point at the data to be compressed * avail_in should indicate how many bytes the library may read * BZ2_bzDecompress 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_bzDecompress 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_bzDecompressInit(&stream, 0, 0); if (status != BZ_OK) { BZ2_bzDecompressEnd(&stream); return dst; } do { status = BZ2_bzDecompress(&stream); if (status != BZ_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_bzDecompressEnd(&stream); return dst; }
Variant HHVM_FUNCTION(bzdecompress, const String& source, int small /* = 0 */) { char *dest; int source_len = source.length(); int error; uint64_t size = 0; bz_stream bzs; bzs.bzalloc = NULL; bzs.bzfree = NULL; if (BZ2_bzDecompressInit(&bzs, 0, small) != BZ_OK) { return false; } bzs.next_in = (char *) source.c_str(); bzs.avail_in = source_len; // in most cases bz2 offers at least 2:1 compression, so we use that as our // base bzs.avail_out = source_len * 2; bzs.next_out = dest = (char *) malloc(bzs.avail_out + 1); if (!dest) { return BZ_MEM_ERROR; } while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) { /* compression is better then 2:1, need to allocate more memory */ bzs.avail_out = source_len; size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32; dest = (char *) safe_realloc(dest, size + bzs.avail_out + 1); bzs.next_out = dest + size; } if (error == BZ_STREAM_END || error == BZ_OK) { size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32; dest = (char *)safe_realloc(dest, size + 1); dest[size] = '\0'; String ret = String(dest, size, AttachString); BZ2_bzDecompressEnd(&bzs); return ret; } else { free(dest); BZ2_bzDecompressEnd(&bzs); return error; } }
void do_bunzip2 (void) { int result; bz_stream strm; strm.bzalloc = NULL; strm.bzfree = NULL; strm.opaque = NULL; strm.avail_in = 0; strm.next_out = outbuf; strm.avail_out = OUTBUFSIZ; result = BZ2_bzDecompressInit (&strm, 0, SMALL_MODE); while (result == BZ_OK) { if (strm.avail_in == 0) { strm.next_in = inbuf; strm.avail_in = (*unzip_read)(strm.next_in, INBUFSIZ); if (strm.avail_in == 0) break; } result = BZ2_bzDecompress (&strm); if ((result != BZ_OK) && (result != BZ_STREAM_END)) break; if ((strm.avail_out == 0) || (result == BZ_STREAM_END)) { (*unzip_write) (outbuf, OUTBUFSIZ - strm.avail_out); strm.next_out = outbuf; strm.avail_out = OUTBUFSIZ; } } BZ2_bzDecompressEnd (&strm); if (result != BZ_STREAM_END) (*unzip_error) (NULL); }
static int64 MojoInput_bzip2_read(MojoInput *io, void *buf, uint32 bufsize) { BZIP2info *info = (BZIP2info *) io->opaque; MojoInput *origio = info->origio; int64 retval = 0; if (bufsize == 0) return 0; // quick rejection. info->stream.next_out = buf; info->stream.avail_out = bufsize; while (retval < ((int64) bufsize)) { const uint32 before = info->stream.total_out_lo32; int rc; if (info->stream.avail_in == 0) { int64 br = origio->length(origio) - origio->tell(origio); if (br > 0) { if (br > BZIP2_READBUFSIZE) br = BZIP2_READBUFSIZE; br = origio->read(origio, info->buffer, (uint32) br); if (br <= 0) return -1; info->stream.next_in = (char *) info->buffer; info->stream.avail_in = (uint32) br; } // if } // if rc = BZ2_bzDecompress(&info->stream); retval += (info->stream.total_out_lo32 - before); if (rc != BZ_OK) return -1; } // while assert(retval >= 0); info->uncompressed_position += (uint32) retval; return retval; } // MojoInput_bzip2_read
static HB_SIZE hb_bz2UncompressedSize( const char * szSrc, HB_SIZE nLen, int * piResult ) { char buffer[ 1024 ]; bz_stream stream; HB_SIZE nDest = 0; memset( &stream, 0, sizeof( stream ) ); stream.next_in = ( char * ) szSrc; stream.avail_in = ( unsigned int ) nLen; stream.bzalloc = hb_bz2Alloc; stream.bzfree = hb_bz2Free; /* stream.opaque = NULL; */ *piResult = BZ2_bzDecompressInit( &stream, 0, 0 ); if( *piResult == BZ_OK ) { do { stream.next_out = buffer; stream.avail_out = sizeof( buffer ); *piResult = BZ2_bzDecompress( &stream ); } while( *piResult == BZ_OK ); if( *piResult == BZ_STREAM_END ) { *piResult = BZ_OK; #if HB_SIZE_MAX <= UINT_MAX if( stream.total_out_hi32 != 0 ) *piResult = BZ_MEM_ERROR; else nDest = ( HB_SIZE ) stream.total_out_lo32; #else nDest = ( ( HB_SIZE ) stream.total_out_hi32 << 32 ) | stream.total_out_lo32; #endif } BZ2_bzDecompressEnd( &stream ); } return nDest; }
bool Samurai::IO::BZip2Decompressor::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 = (char*) input; d->stream->avail_out = output_len; d->stream->next_out = (char*) output; int retval = BZ2_bzDecompress(d->stream); if (retval == BZ_OK) { output_len -= d->stream->avail_out; input_len -= d->stream->avail_in; return true; } return false; }
static FT_Error ft_bzip2_file_fill_output( FT_BZip2File zip ) { bz_stream* bzstream = &zip->bzstream; FT_Error error = FT_Err_Ok; zip->cursor = zip->buffer; bzstream->next_out = (char*)zip->cursor; bzstream->avail_out = FT_BZIP2_BUFFER_SIZE; while ( bzstream->avail_out > 0 ) { int err; if ( bzstream->avail_in == 0 ) { error = ft_bzip2_file_fill_input( zip ); if ( error ) break; } err = BZ2_bzDecompress( bzstream ); if ( err == BZ_STREAM_END ) { zip->limit = (FT_Byte*)bzstream->next_out; if ( zip->limit == zip->cursor ) error = FT_THROW( Invalid_Stream_Operation ); break; } else if ( err != BZ_OK ) { zip->limit = zip->cursor; error = FT_THROW( Invalid_Stream_Operation ); break; } } return error; }
static int hb_bz2Uncompress( const char * szSrc, HB_SIZE nSrc, char * szDst, HB_SIZE * pnDst ) { 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_bzDecompressInit( &stream, 0, 0 ); if( iResult == BZ_OK ) { do { iResult = BZ2_bzDecompress( &stream ); } while( iResult == BZ_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_bzDecompressEnd( &stream ); } return iResult; }
String BZ2Decompress(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_bzDecompressInit(&z, 0, 0) != BZ_OK) return String::GetVoid(); int buf_size = minmax(s.GetLength() / 2, 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; while(BZ2_bzDecompress(&z) == BZ_OK) { if(z.avail_out == (dword)buf_size) { // no output generated - assume error BZ2_bzDecompressEnd(&z); return String::GetVoid(); } 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_bzDecompressEnd(&z); return String::GetVoid(); } } if(z.avail_out < (unsigned)buf_size) out.Cat(output, buf_size - z.avail_out); BZ2_bzDecompressEnd(&z); return out; }