Beispiel #1
0
/**
 * Inflate a string.
 * options:
 */
static int larc_bzip2_decompress(lua_State *L)
{
	size_t len;
	const char *str = luaL_checklstring(L, 1, &len);
	bz_userdata ud;

	ud.z.bzalloc = NULL;
	ud.z.bzfree = NULL;
	ud.z.opaque = NULL;
	ud.z.next_in = (char*)str;
	ud.z.avail_in = len;
	
	ud.status = BZ2_bzDecompressInit(&ud.z, 0, USE_SMALL_DECOMPRESS);
	if (ud.status != BZ_OK)
	{
		lua_pushnil(L);
		lua_pushstring(L, bz2_error(ud.status));
		lua_pushinteger(L, ud.status);
		return 3;
	}
	
	ud.result = -1;
	if (0 != lua_cpcall(L, protected_decompress_to_buffer, &ud))
	{
		BZ2_bzDecompressEnd(&ud.z);
		return lua_error(L);
	}
	BZ2_bzDecompressEnd(&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;
}
Beispiel #2
0
/* 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;
}
Beispiel #3
0
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;
}
Beispiel #4
0
value camlzip_bzDecompressEnd(value stream) {
#ifdef USE_BZIP2
  int err;
  if ((err = BZ2_bzDecompressEnd(BZStream_val(stream))) != BZ_OK)
    camlzip_bzerror("Bzlib.decompressEnd", err);
  free(BZStream_val(stream));
#else
  failwith("Bzip2 compression not supported");
#endif
  return Val_unit;
}
Beispiel #5
0
static void
gst_bz2dec_decompress_end (GstBz2dec * b)
{
  g_return_if_fail (GST_IS_BZ2DEC (b));

  if (b->ready) {
    BZ2_bzDecompressEnd (&b->stream);
    memset (&b->stream, 0, sizeof (b->stream));
    b->ready = FALSE;
  }
}
Beispiel #6
0
void MemoryDecompressor::Clear(void)
{
	if (output)
	{
		free(output);
		output=0;
	}

	if (streamInited)
		BZ2_bzDecompressEnd( &stream );
}
Beispiel #7
0
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;
}
Beispiel #8
0
static void php_bz2_decompress_dtor(php_stream_filter *thisfilter)
{
	if (thisfilter && Z_PTR(thisfilter->abstract)) {
		php_bz2_filter_data *data = Z_PTR(thisfilter->abstract);
		if (data->status == PHP_BZ2_RUNNING) {
			BZ2_bzDecompressEnd(&(data->strm));
		}
		pefree(data->inbuf, data->persistent);
		pefree(data->outbuf, data->persistent);
		pefree(data, data->persistent);
	}
}
static void i_stream_bzlib_close(struct iostream_private *stream,
				 bool close_parent)
{
	struct bzlib_istream *zstream = (struct bzlib_istream *)stream;

	if (!zstream->zs_closed) {
		(void)BZ2_bzDecompressEnd(&zstream->zs);
		zstream->zs_closed = TRUE;
	}
	if (close_parent)
		i_stream_close(zstream->istream.parent);
}
void CryptoManager::decodeBZ2(const u_int8_t* is, size_t sz, string& os) throw (CryptoException) {
	bz_stream bs = { 0 };

	if(BZ2_bzDecompressInit(&bs, 0, 0) != BZ_OK)
		throw(CryptoException(STRING(DECOMPRESSION_ERROR)));

	// We assume that the files aren't compressed more than 2:1...if they are it'll work anyway,
	// but we'll have to do multiple passes...
	size_t bufsize = 2*sz;
	AutoArray<char> buf(bufsize);
	
	bs.avail_in = sz;
	bs.avail_out = bufsize;
	bs.next_in = (char*)(const_cast<u_int8_t*>(is));
	bs.next_out = buf;

	int err;

	os.clear();
	
	while((err = BZ2_bzDecompress(&bs)) == BZ_OK) { 
		if (bs.avail_in == 0 && bs.avail_out > 0) { // error: BZ_UNEXPECTED_EOF 
			BZ2_bzDecompressEnd(&bs); 
			throw CryptoException(STRING(DECOMPRESSION_ERROR)); 
		} 
		os.append(buf, bufsize-bs.avail_out); 
		bs.avail_out = bufsize; 
		bs.next_out = buf; 
	} 

	if(err == BZ_STREAM_END)
		os.append(buf, bufsize-bs.avail_out);
	
	BZ2_bzDecompressEnd(&bs);

	if(err < 0) {
		// This was a real error
		throw CryptoException(STRING(DECOMPRESSION_ERROR));	
	}
}
Beispiel #11
0
Variant HHVM_FUNCTION(bzdecompress, const String& source, int small /* = 0 */) {
  int source_len = source.length();
  int error;
  uint64_t size = 0;
  bz_stream bzs;

  bzs.bzalloc = nullptr;
  bzs.bzfree = nullptr;

  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;
  String ret(bzs.avail_out, ReserveString);
  bzs.next_out = ret.bufferSlice().ptr;

  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;
    ret.setSize(size); // needs to be null-terminated before the reserve call
    bzs.next_out = ret.reserve(size + bzs.avail_out).ptr + size;
  }

  if (error == BZ_STREAM_END || error == BZ_OK) {
    size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
    BZ2_bzDecompressEnd(&bzs);
    ret.shrink(size);
    return ret;
  } else {
    BZ2_bzDecompressEnd(&bzs);
    return error;
  }
}
Beispiel #12
0
static int
bzf_close(struct open_file *f)
{
    struct bz_file	*bzf = (struct bz_file *)f->f_fsdata;

    f->f_fsdata = NULL;
    if (bzf) {
        BZ2_bzDecompressEnd(&(bzf->bzf_bzstream));
        close(bzf->bzf_rawfd);
        free(bzf);
    }
    return(0);
}
Beispiel #13
0
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);
}
Beispiel #14
0
/*---------------------------------------------------*/
void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
{
   bzFile* bzf = (bzFile*)b;

   BZ_SETERR(BZ_OK);
   if (bzf == NULL)
      { BZ_SETERR(BZ_OK); return; };

   if (bzf->writing)
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };

   if (bzf->initialisedOk)
      (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
   free ( bzf );
}
Beispiel #15
0
// 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;
  }
static tsf_bool_t tear_down_mode(tsf_adpt_rdr_t *reader,
				 uint8_t **spillover,
				 uint32_t *spillover_len) {
    tsf_bool_t stillok=tsf_true;
    if (reader->mode==TSF_ZIP_ZLIB) {
#ifdef HAVE_ZLIB
	inflateEnd(Cz(reader));
#endif
    } else if (reader->mode==TSF_ZIP_BZIP2) {
#ifdef HAVE_BZIP2
	BZ2_bzDecompressEnd(Cbz(reader));
#endif
    }
    if (reader->mode==TSF_ZIP_ZLIB ||
	reader->mode==TSF_ZIP_BZIP2) {
	free(reader->stream);
	if (spillover!=NULL && spillover_len!=NULL) {
	    *spillover=malloc(reader->abstract.avail_in);
	    if (*spillover==NULL) {
		tsf_set_errno("Could not allocate spillover buffer");
		stillok=tsf_false;
	    } else {
		*spillover_len=reader->abstract.avail_in;
		memcpy(*spillover,reader->abstract.next_in,*spillover_len);
	    }
	}
    } else if (reader->mode==TSF_ZIP_NONE) {
	if (spillover!=NULL && spillover_len!=NULL) {
	    *spillover=malloc(reader->puti-reader->geti);
	    if (*spillover==NULL) {
		tsf_set_errno("Could not allocate spillover buffer");
		stillok=tsf_false;
	    } else {
		*spillover_len=reader->puti-reader->geti;
		memcpy(*spillover,reader->buf+reader->geti,*spillover_len);
	    }
	}
    }
    if (reader->mode!=TSF_ZIP_UNKNOWN) {
	free(reader->buf);
    }
    reader->stream=NULL;
    reader->buf=NULL;
    reader->buf_size=0;
    reader->mode=TSF_ZIP_UNKNOWN;
    return stillok;
}
static void i_stream_bzlib_reset(struct bzlib_istream *zstream)
{
	struct istream_private *stream = &zstream->istream;

	i_stream_seek(stream->parent, stream->parent_start_offset);
	zstream->eof_offset = (uoff_t)-1;
	zstream->zs.next_in = NULL;
	zstream->zs.avail_in = 0;

	stream->parent_expected_offset = stream->parent_start_offset;
	stream->skip = stream->pos = 0;
	stream->istream.v_offset = 0;
	zstream->high_pos = 0;

	(void)BZ2_bzDecompressEnd(&zstream->zs);
	i_stream_bzlib_init(zstream);
}
Beispiel #18
0
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);
}
Beispiel #19
0
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;
}
Beispiel #20
0
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;
}
Beispiel #21
0
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;
}
Beispiel #22
0
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;
}
Beispiel #23
0
  static void
  ft_bzip2_file_done( FT_BZip2File  zip )
  {
    bz_stream*  bzstream = &zip->bzstream;


    BZ2_bzDecompressEnd( bzstream );

    /* clear the rest */
    bzstream->bzalloc   = NULL;
    bzstream->bzfree    = NULL;
    bzstream->opaque    = NULL;
    bzstream->next_in   = NULL;
    bzstream->next_out  = NULL;
    bzstream->avail_in  = 0;
    bzstream->avail_out = 0;

    zip->memory = NULL;
    zip->source = NULL;
    zip->stream = NULL;
  }
static int Decompress_BZIP2(char * pbOutBuffer, int * pcbOutBuffer, char * pbInBuffer, int cbInBuffer)
{
    bz_stream strm;
    int nResult = BZ_OK;

    // Initialize the BZIP2 decompression
    strm.bzalloc = NULL;
    strm.bzfree  = NULL;
    if(BZ2_bzDecompressInit(&strm, 0, 0) == BZ_OK)
    {
        strm.next_in   = pbInBuffer;
        strm.avail_in  = cbInBuffer;
        strm.next_out  = pbOutBuffer;
        strm.avail_out = *pcbOutBuffer;

        // Perform the decompression
        while(nResult != BZ_STREAM_END)
        {
            nResult = BZ2_bzDecompress(&strm);

            // If any error there, break the loop
            if(nResult < BZ_OK)
                break;
        }

        // Put the stream into idle state
        BZ2_bzDecompressEnd(&strm);

        // If all succeeded, set the number of output bytes
        if(nResult >= BZ_OK)
        {
            *pcbOutBuffer = strm.total_out_lo32;
            return 1;
        }
    }

    // Something failed, so set number of output bytes to zero
    *pcbOutBuffer = 0;
    return 1;
}
static tsf_bool_t tear_to_none(tsf_adpt_rdr_t *reader) {
    tsf_assert(reader->mode==TSF_ZIP_ZLIB ||
	       reader->mode==TSF_ZIP_BZIP2);
    if (reader->mode==TSF_ZIP_ZLIB) {
#ifdef HAVE_ZLIB
	inflateEnd(Cz(reader));
#endif
    } else {
#ifdef HAVE_BZIP2
	BZ2_bzDecompressEnd(Cbz(reader));
#endif
    }
    free(reader->stream);
    reader->mode=TSF_ZIP_UNKNOWN;
    if (reader->nozip_buf_size==reader->buf_size) {
	reader->geti=reader->abstract.next_in-reader->buf;
	reader->puti=reader->geti+reader->abstract.avail_in;
    } else {
	uint8_t *buf;
	reader->buf_size=tsf_max(reader->nozip_buf_size,
				 reader->abstract.avail_in);
	buf=malloc(reader->buf_size);
	if (buf==NULL) {
	    tsf_set_errno("Could not allocate buffer in tear_to_none() in "
			  "tsf_adaptive_reader.c");
	    free(reader->buf);
	    /* oh man, this leaves the reader in a flaky state */
	    return tsf_false;
	}
	memcpy(buf,
	       reader->abstract.next_in,
	       reader->abstract.avail_in);
	free(reader->buf);
	reader->geti=0;
	reader->puti=reader->abstract.avail_in;
	reader->buf=buf;
    }
    reader->mode=TSF_ZIP_NONE;
    return tsf_true;
}
/*
 * Clean up the decompressor.
 */
static int
finish(struct archive_read *a)
{
	struct private_data *state;
	int ret;

	state = (struct private_data *)a->decompressor->data;
	ret = ARCHIVE_OK;
	switch (BZ2_bzDecompressEnd(&(state->stream))) {
	case BZ_OK:
		break;
	default:
		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
		    "Failed to clean up %s compressor",
		    a->archive.compression_name);
		ret = ARCHIVE_FATAL;
	}

	free(state->uncompressed_buffer);
	free(state);

	a->decompressor->data = NULL;
	return (ret);
}
Beispiel #27
0
/* 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;
}
/*
 * Clean up the decompressor.
 */
static int
bzip2_filter_close(struct archive_read_filter *self)
{
	struct private_data *state;
	int ret = ARCHIVE_OK;

	state = (struct private_data *)self->data;

	if (state->valid) {
		switch (BZ2_bzDecompressEnd(&state->stream)) {
		case BZ_OK:
			break;
		default:
			archive_set_error(&self->archive->archive,
					  ARCHIVE_ERRNO_MISC,
					  "Failed to clean up decompressor");
			ret = ARCHIVE_FATAL;
		}
	}

	free(state->out_block);
	free(state);
	return (ret);
}
Beispiel #29
0
enum eav_error
extract_and_verify(unsigned char *ibuf, size_t ilen,
    unsigned char **obufp, size_t *olenp, size_t blocksize,
    enum eav_compression ctype,
    enum eav_digest dtype, const unsigned char *digest)
{
	int ret;
	unsigned char *obuf = NULL;
	size_t olen = 0, total_in, total_out;
	bz_stream bzs;
#ifdef MD5_SUPPORT
	size_t prev_total_in;
	MD5_CTX md5ctx;
	char i_md5sum[33];
#endif

	switch (ctype) {
	case EAV_COMP_NONE:
	case EAV_COMP_BZIP2:
		break;
	case EAV_COMP_GZIP:
	case EAV_COMP_XZ:
		return (EAV_ERR_COMP_UNSUPPORTED);
	default:
		return (EAV_ERR_COMP_UNKNOWN);
	}

	switch (dtype) {
	case EAV_DIGEST_NONE:
		break;
	case EAV_DIGEST_MD5:
#ifdef MD5_SUPPORT
		break;
#else
		return (EAV_ERR_DIGEST_UNSUPPORTED);
#endif

	default:
		return (EAV_ERR_DIGEST_UNKNOWN);
	}

	if (dtype || ctype) {
#ifdef MD5_SUPPORT
		if (dtype == EAV_DIGEST_MD5)
			MD5Init(&md5ctx);
#endif

		if (ctype) {
			/* XXX: assume bzip2 for now */
			olen = 1024 * 1024;
			if ((obuf = malloc(olen)) == NULL)
				return (EAV_ERR_MEM);

			total_in = 0;
#ifdef MD5_SUPPORT
			prev_total_in = 0;
#endif

			bzs.bzalloc = NULL;
			bzs.bzfree = NULL;
			bzs.opaque = NULL;
			bzs.next_in = (char *)ibuf;
			bzs.avail_in = MIN(ilen, 1024 * 1024);
			bzs.next_out = (char *)obuf;
			bzs.avail_out = olen;
			if (BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
				return (EAV_ERR_COMP);

			while ((ret = BZ2_bzDecompress(&bzs)) !=
			    BZ_STREAM_END) {
				if (ret != BZ_OK) {
					free(obuf);
					BZ2_bzDecompressEnd(&bzs);
					return (EAV_ERR_COMP);
				}

				total_in = ((size_t)bzs.total_in_hi32 << 32) +
				    bzs.total_in_lo32;
				total_out = ((size_t)bzs.total_out_hi32 << 32) +
				    bzs.total_out_lo32;

#ifdef MD5_SUPPORT
				if (dtype == EAV_DIGEST_MD5)
					MD5Update(&md5ctx, ibuf + prev_total_in,
					    total_in - prev_total_in);
				prev_total_in = total_in;
#endif

				if (bzs.avail_in == 0)
					bzs.avail_in =
					    MIN(ilen - total_in, 1024 * 1024);

				if (bzs.avail_out == 0) {
					olen *= 2;
					if ((obuf = reallocf(obuf, olen))
					    == NULL) {
						BZ2_bzDecompressEnd(&bzs);
						return (EAV_ERR_COMP);
					}
					bzs.next_out = (char *)obuf + total_out;
					bzs.avail_out = olen - total_out;
				}
			}
			BZ2_bzDecompressEnd(&bzs);
			total_in = ((size_t)bzs.total_in_hi32 << 32) +
			    bzs.total_in_lo32;
			total_out = ((size_t)bzs.total_out_hi32 << 32) +
			    bzs.total_out_lo32;

#ifdef MD5_SUPPORT
			/* Push the last read block in the MD5 machine */
			if (dtype == EAV_DIGEST_MD5)
				MD5Update(&md5ctx, ibuf + prev_total_in,
				    total_in - prev_total_in);
#endif

			/* Round up to blocksize and zero pad */
			olen = roundup2(total_out, blocksize);
			if (olen != total_out)
				memset(obuf + total_out, '\0',
				    olen - total_out);
			/* XXX: realloc to shorten allocation? */
		} else if (dtype) {
#ifdef MD5_SUPPORT
			if (dtype == EAV_DIGEST_MD5)
				MD5Update(&md5ctx, ibuf, ilen);
#endif
		}

		if (dtype) {
#ifdef MD5_SUPPORT
			if (dtype == EAV_DIGEST_MD5) {
				MD5End(&md5ctx, i_md5sum);
				if (strcmp(digest, i_md5sum) != 0)
					return (EAV_ERR_DIGEST);
			}
#endif
		}
	}

	if (ctype == EAV_COMP_NONE) {
		*obufp = ibuf;
		*olenp = ilen;
	} else {
		*obufp = obuf;
		*olenp = olen;
	}
	return (EAV_SUCCESS);
}
 void close() final {
     BZ2_bzDecompressEnd(&m_bzstream);
 }