Example #1
0
static boolean MojoInput_bzip2_seek(MojoInput *io, uint64 offset)
{
    // This is all really expensive.
    BZIP2info *info = (BZIP2info *) io->opaque;

    /*
     * If seeking backwards, we need to redecode the file
     *  from the start and throw away the compressed bits until we hit
     *  the offset we need. If seeking forward, we still need to
     *  decode, but we don't rewind first.
     */
    if (offset < info->uncompressed_position)
    {
#if 0
        /* we do a copy so state is sane if inflateInit2() fails. */
        bz_stream str;
        initializeBZ2Stream(&str);
        if (BZ2_bzDecompressInit(&str, 0, 0) != BZ_OK)
            return false;

        if (!info->origio->seek(info->origio, 0))
            return false;  // !!! FIXME: leaking (str)?

        BZ2_bzDecompressEnd(&info->stream);
        memcpy(&info->stream, &str, sizeof (bz_stream));
#endif

        if (!info->origio->seek(info->origio, 0))
            return false;
        BZ2_bzDecompressEnd(&info->stream);
        initializeBZ2Stream(&info->stream);
        if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
            return false;
        info->uncompressed_position = 0;
    } // if

    while (info->uncompressed_position != offset)
    {
        uint8 buf[512];
        uint32 maxread;
        int64 br;

        maxread = (uint32) (offset - info->uncompressed_position);
        if (maxread > sizeof (buf))
            maxread = sizeof (buf);

        br = io->read(io, buf, maxread);
        if (br != maxread)
            return false;
    } /* while */

    return true;
} // MojoInput_bzip2_seek
Example #2
0
UnBZFilter::UnBZFilter() {
	memzero(&zs, sizeof(zs));

	if(BZ2_bzDecompressInit(&zs, 0, 0) != BZ_OK) 
		throw Exception(STRING(DECOMPRESSION_ERROR));

}
_X_HIDDEN BufFilePtr
BufFilePushBZIP2 (BufFilePtr f)
{
    xzip_buf *x;

    x = malloc (sizeof (xzip_buf));
    if (!x) return NULL;

    bzero(&(x->z), sizeof(bz_stream));
    x->f = f;

    x->zstat = BZ2_bzDecompressInit(&(x->z),
				    0,	/* verbosity: 0 silent, 4 max */
				    0);	/* 0: go faster, 1: use less memory */
    if (x->zstat != BZ_OK) {
	free(x);
	return NULL;
    }

    /* now that the history buffer is allocated, we provide the data buffer */
    x->z.next_out = (char *) x->b;
    x->z.avail_out = BUFFILESIZE;
    x->z.next_in = (char *) x->b_in;
    x->z.avail_in = 0;

    return BufFileCreate((char *)x,
			 BufBzip2FileFill,
			 NULL,
			 BufBzip2FileSkip,
			 BufBzip2FileClose);
}
Example #4
0
  static FT_Error
  ft_bzip2_file_reset( FT_BZip2File  zip )
  {
    FT_Stream  stream = zip->source;
    FT_Error   error;


    if ( !FT_STREAM_SEEK( 0 ) )
    {
      bz_stream*  bzstream = &zip->bzstream;


      BZ2_bzDecompressEnd( bzstream );

      bzstream->avail_in  = 0;
      bzstream->next_in   = (char*)zip->input;
      bzstream->avail_out = 0;
      bzstream->next_out  = (char*)zip->buffer;

      zip->limit  = zip->buffer + FT_BZIP2_BUFFER_SIZE;
      zip->cursor = zip->limit;
      zip->pos    = 0;

      BZ2_bzDecompressInit( bzstream, 0, 0 );
    }

    return error;
  }
Example #5
0
static SquashBZ2Stream*
squash_bz2_stream_new (SquashCodec* codec, SquashStreamType stream_type, SquashOptions* options) {
  int bz2_e = 0;
  SquashBZ2Stream* stream;

  assert (codec != NULL);
  assert (stream_type == SQUASH_STREAM_COMPRESS || stream_type == SQUASH_STREAM_DECOMPRESS);

  stream = squash_malloc (sizeof (SquashBZ2Stream));
  squash_bz2_stream_init (stream, codec, stream_type, options, squash_bz2_stream_destroy);

  if (stream_type == SQUASH_STREAM_COMPRESS) {
    bz2_e = BZ2_bzCompressInit (&(stream->stream),
                                squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_LEVEL),
                                0,
                                squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_WORK_FACTOR));
  } else if (stream_type == SQUASH_STREAM_DECOMPRESS) {
    bz2_e = BZ2_bzDecompressInit (&(stream->stream),
                                  0,
                                  squash_codec_get_option_int_index (codec, options, SQUASH_BZ2_OPT_SMALL));
  } else {
    squash_assert_unreachable();
  }

  if (bz2_e != BZ_OK) {
    /* We validate the params so OOM is really the only time this
       should happen, and that really shouldn't be happening here. */
    stream = squash_object_unref (stream);
  }

  return stream;
}
Example #6
0
io_t *bz_open(io_t *parent)
{
	io_t *io;
	if (!parent)
		return NULL;
	io = malloc(sizeof(io_t));
	io->source = &bz_source;
	io->data = malloc(sizeof(struct bz_t));

	DATA(io)->parent = parent;

	DATA(io)->strm.next_in = NULL;
	DATA(io)->strm.avail_in = 0;
	DATA(io)->strm.next_out = NULL;
	DATA(io)->strm.avail_out = 0;
	DATA(io)->strm.bzalloc = NULL;
	DATA(io)->strm.bzfree = NULL;
	DATA(io)->strm.opaque = NULL;
	DATA(io)->err = ERR_OK;

	BZ2_bzDecompressInit(&DATA(io)->strm, 
		0, 	/* Verbosity */
		0);	/* small */

	return io;
}
Example #7
0
static SquashBZ2Stream*
squash_bz2_stream_new (SquashCodec* codec, SquashStreamType stream_type, SquashBZ2Options* options) {
  int bz2_e = 0;
  SquashBZ2Stream* stream;

  assert (codec != NULL);
  assert (stream_type == SQUASH_STREAM_COMPRESS || stream_type == SQUASH_STREAM_DECOMPRESS);

  stream = (SquashBZ2Stream*) malloc (sizeof (SquashBZ2Stream));
  squash_bz2_stream_init (stream, codec, stream_type, options, squash_bz2_stream_free);

  if (stream_type == SQUASH_STREAM_COMPRESS) {
    bz2_e = BZ2_bzCompressInit (&(stream->stream),
                                squash_bz2_options_get_block_size_100k (options),
                                0,
                                squash_bz2_options_get_work_factor (options));
  } else if (stream_type == SQUASH_STREAM_DECOMPRESS) {
    bz2_e = BZ2_bzDecompressInit (&(stream->stream),
                                  0,
                                  squash_bz2_options_get_small (options) ? 1 : 0);
  } else {
    assert (false);
  }

  if (bz2_e != BZ_OK) {
    /* We validate the params so OOM is really the only time this
       should happen, and that really shouldn't be happening here. */
    stream = squash_object_unref (stream);
  }

  return stream;
}
Example #8
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;
}
Example #9
0
void bzip2_base::do_init
    ( bool compress, 
      #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
          bzip2::alloc_func alloc, 
          bzip2::free_func free, 
      #endif
      void* derived )
{
    bz_stream* s = static_cast<bz_stream*>(stream_);

    // Current interface for customizing memory management 
    // is non-conforming and has been disabled:
    //#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
    //    s->bzalloc = alloc;
    //    s->bzfree = free;
    //#else
        s->bzalloc = 0;
        s->bzfree = 0;
    //#endif
    s->opaque = derived;
    bzip2_error::check( 
        compress ?
            BZ2_bzCompressInit( s,
                                params_.block_size, 
                                0, 
                                params_.work_factor ) :
            BZ2_bzDecompressInit( s, 
                                  0, 
                                  params_.small )
    );
    ready_ = true;
}
Example #10
0
static bool
start(void *ud) {
    struct ctx *ctx = (struct ctx *)ud;
    int ret;

    ctx->zstr.avail_in = 0;
    ctx->zstr.next_in = NULL;
    ctx->zstr.avail_out = 0;
    ctx->zstr.next_out = NULL;

    if (ctx->compress) {
	ret = BZ2_bzCompressInit(&ctx->zstr, ctx->compression_flags, 0, 30);

    }
    else {
	ret = BZ2_bzDecompressInit(&ctx->zstr, 0, 0);
    }

    if (ret != BZ_OK) {
	zip_error_set(ctx->error, map_error(ret), 0);
	return false;
    }

    return true;
}
Example #11
0
static MojoInput *make_bzip2_input(MojoInput *origio)
{
    MojoInput *io = NULL;
    BZIP2info *info = (BZIP2info *) xmalloc(sizeof (BZIP2info));

    initializeBZ2Stream(&info->stream);
    if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
    {
        free(info);
        return NULL;
    } // if

    info->origio = origio;

    io = (MojoInput *) xmalloc(sizeof (MojoInput));
    io->ready = MojoInput_bzip2_ready;
    io->read = MojoInput_bzip2_read;
    io->seek = MojoInput_bzip2_seek;
    io->tell = MojoInput_bzip2_tell;
    io->length = MojoInput_bzip2_length;
    io->duplicate = MojoInput_bzip2_duplicate;
    io->close = MojoInput_bzip2_close;
    io->opaque = info;
    return io;
} // make_bzip2_input
Example #12
0
// Try open file stream
bool FXBZFileStream::open(const FXString& filename,FXStreamDirection save_or_load,FXuval size){
  if(FXFileStream::open(filename,save_or_load,size)){
    if(FXCALLOC(&bz,BZBlock,1)){
      int bzerror;
      bz->stream.next_in=NULL;
      bz->stream.avail_in=0;
      bz->stream.next_out=NULL;
      bz->stream.avail_out=0;
      ac=BZ_RUN;
      if(save_or_load==FXStreamLoad){
        bzerror=BZ2_bzDecompressInit(&bz->stream,VERBOSITY,0);
        if(bzerror==BZ_OK) return true;
        code=FXStreamNoRead;
        }
      else{
        bzerror=BZ2_bzCompressInit(&bz->stream,BLOCKSIZE100K,VERBOSITY,WORKFACTOR);
        if(bzerror==BZ_OK) return true;
        code=FXStreamNoWrite;
        }
      FXFREE(&bz);
      }
    FXFileStream::close();
    }
  return false;
  }
Example #13
0
/**
 * Create an decompress function.
 * options:
 */
static int larc_bzip2_decompressor(lua_State *L)
{
	bz_userdata *ud;

	ud = (bz_userdata*)lua_newuserdata(L, sizeof(bz_userdata));
	luaL_getmetatable(L, BZ2DECOMPRESS_MT);
	lua_setmetatable(L, -2);
	
	ud->z.bzalloc = NULL;
	ud->z.bzfree = NULL;
	ud->z.opaque = NULL;
	ud->z.next_in = NULL;
	ud->z.avail_in = 0;
	
	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;
	}

	lua_pushcclosure(L, decompress_call, 1);
	return 1;
}
JNIEXPORT jbyteArray JNICALL Java_cn_reactnative_modules_update_DownloadTask_bsdiffPatch
        (JNIEnv *env, jobject self, jbyteArray origin, jbyteArray patch){
    jclass newExcCls;
    jbyte* outPtr;
    struct bspatch_stream stream;
    bz_stream zip;

    jbyte* originPtr = (*env)->GetByteArrayElements(env, origin, NULL);
    size_t originLength = (*env)->GetArrayLength(env, origin);
    jbyte* patchPtr = (*env)->GetByteArrayElements(env, patch, NULL);
    size_t patchLength = (*env)->GetArrayLength(env, patch);
    jbyteArray ret = NULL;

    if (patchLength < 32) {
        newExcCls = (*env)->FindClass(env,"java/lang/Error");
        if (newExcCls != NULL) /* Unable to find the new exception class, give up. */
            (*env)->ThrowNew(env,newExcCls, "Corrupt patch");
        (*env)->ReleaseByteArrayElements(env, origin, originPtr, JNI_ABORT);
        (*env)->ReleaseByteArrayElements(env, patch, patchPtr, JNI_ABORT);
        return NULL;
    }
    int64_t newsize=offtin((uint8_t*)patchPtr + 16);
    if (memcmp(patchPtr, "ENDSLEY/BSDIFF43", 16) != 0 || newsize<0) {
        newExcCls = (*env)->FindClass(env, "java/lang/Error");
        if (newExcCls != NULL) /* Unable to find the new exception class, give up. */
            (*env)->ThrowNew(env, newExcCls, "Corrupt patch");
        (*env)->ReleaseByteArrayElements(env, origin, originPtr, JNI_ABORT);
        (*env)->ReleaseByteArrayElements(env, patch, patchPtr, JNI_ABORT);
        return NULL;
    }
    ret = (*env)->NewByteArray(env, newsize);
    if (ret == NULL) {
        return NULL; //  out of memory error thrown
    }
    outPtr = (*env)->GetByteArrayElements(env, ret, NULL);

    zip.bzalloc = NULL;
    zip.bzfree = NULL;
    zip.opaque = NULL;
    BZ2_bzDecompressInit(&zip, 0, 1);

    zip.next_in = (char*)patchPtr + 32;
    zip.avail_in = patchLength - 32;

    stream.read = bz2_read;
    stream.opaque = &zip;
    if (bspatch((const uint8_t*)originPtr, originLength, (uint8_t*)outPtr, newsize, &stream)) {
        newExcCls = (*env)->FindClass(env, "java/lang/Error");
        if (newExcCls != NULL) /* Unable to find the new exception class, give up. */
            (*env)->ThrowNew(env, newExcCls, "bspatch");
    }

    BZ2_bzDecompressEnd(&zip);

    (*env)->ReleaseByteArrayElements(env, ret, outPtr, 0);
    (*env)->ReleaseByteArrayElements(env, origin, originPtr, JNI_ABORT);
    (*env)->ReleaseByteArrayElements(env, patch, patchPtr, JNI_ABORT);
    return ret;
}
Example #15
0
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;
}
Example #16
0
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;
}
Example #17
0
Samurai::IO::BZip2Decompressor::BZip2Decompressor()
{
	d = new Bz2Private();
	
	if (BZ2_bzDecompressInit(d->stream, 0, 0) != BZ_OK)
	{
		delete d; d = 0;
	}
}
Example #18
0
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);
}
Example #19
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);
}
Example #20
0
int import_uncompress_detect(ImportCompress *c, const void *data, size_t size) {
        static const uint8_t xz_signature[] = {
                0xfd, '7', 'z', 'X', 'Z', 0x00
        };
        static const uint8_t gzip_signature[] = {
                0x1f, 0x8b
        };
        static const uint8_t bzip2_signature[] = {
                'B', 'Z', 'h'
        };

        int r;

        assert(c);

        if (c->type != IMPORT_COMPRESS_UNKNOWN)
                return 1;

        if (size < MAX3(sizeof(xz_signature),
                        sizeof(gzip_signature),
                        sizeof(bzip2_signature)))
                return 0;

        assert(data);

        if (memcmp(data, xz_signature, sizeof(xz_signature)) == 0) {
                lzma_ret xzr;

                xzr = lzma_stream_decoder(&c->xz, UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK);
                if (xzr != LZMA_OK)
                        return -EIO;

                c->type = IMPORT_COMPRESS_XZ;

        } else if (memcmp(data, gzip_signature, sizeof(gzip_signature)) == 0) {
                r = inflateInit2(&c->gzip, 15+16);
                if (r != Z_OK)
                        return -EIO;

                c->type = IMPORT_COMPRESS_GZIP;

        } else if (memcmp(data, bzip2_signature, sizeof(bzip2_signature)) == 0) {
                r = BZ2_bzDecompressInit(&c->bzip2, 0, 0);
                if (r != BZ_OK)
                        return -EIO;

                c->type = IMPORT_COMPRESS_BZIP2;
        } else
                c->type = IMPORT_COMPRESS_UNCOMPRESSED;

        c->encoding = false;

        return 1;
}
Example #21
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;
}
Example #22
0
value camlzip_bzDecompressInit(value verbosity, value small)
{
#ifdef USE_BZIP2
  int err;
  value vzs = camlzip_new_bzstream();
  if ((err = BZ2_bzDecompressInit(BZStream_val(vzs), Int_val(verbosity), Bool_val(small))) != BZ_OK)
    camlzip_bzerror("Bzlib.decompress_init", err);
  return vzs;
#else
  failwith("Bzip2 compression not supported");
#endif
}
Example #23
0
static void
init_uncompress( compress_filter_context_t *zfx, bz_stream *bzs )
{
  int rc;

  if((rc=BZ2_bzDecompressInit(bzs,0,opt.bz2_decompress_lowmem))!=BZ_OK)
    log_fatal("bz2lib problem: %d\n",rc);

  zfx->inbufsize = 2048;
  zfx->inbuf = xmalloc( zfx->inbufsize );
  bzs->avail_in = 0;
}
Example #24
0
/*---------------------------------------------------*/
BZFILE* BZ_API(BZ2_bzReadOpen) 
                   ( int*  bzerror, 
                     FILE* f, 
                     int   verbosity,
                     int   small,
                     void* unused,
                     int   nUnused )
{
   bzFile* bzf = NULL;
   int     ret;

   BZ_SETERR(BZ_OK);

   if (f == NULL || 
       (small != 0 && small != 1) ||
       (verbosity < 0 || verbosity > 4) ||
       (unused == NULL && nUnused != 0) ||
       (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
      { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };

   if (ferror(f))
      { BZ_SETERR(BZ_IO_ERROR); return NULL; };

   bzf = malloc ( sizeof(bzFile) );
   if (bzf == NULL) 
      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };

   BZ_SETERR(BZ_OK);

   bzf->initialisedOk = False;
   bzf->handle        = f;
   bzf->bufN          = 0;
   bzf->writing       = False;
   bzf->strm.bzalloc  = NULL;
   bzf->strm.bzfree   = NULL;
   bzf->strm.opaque   = NULL;
   
   while (nUnused > 0) {
      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
      unused = ((void*)( 1 + ((UChar*)(unused))  ));
      nUnused--;
   }

   ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
   if (ret != BZ_OK)
      { BZ_SETERR(ret); free(bzf); return NULL; };

   bzf->strm.avail_in = bzf->bufN;
   bzf->strm.next_in  = bzf->buf;

   bzf->initialisedOk = True;
   return bzf;   
}
 Bzip2BufferDecompressor(const char* buffer, size_t size) :
     m_buffer(buffer),
     m_buffer_size(size),
     m_bzstream() {
     m_bzstream.next_in = const_cast<char*>(buffer);
     m_bzstream.avail_in = static_cast_with_assert<unsigned int>(size);
     int result = BZ2_bzDecompressInit(&m_bzstream, 0, 0);
     if (result != BZ_OK) {
         std::string message("bzip2 error: decompression init failed: ");
         throw bzip2_error(message, result);
     }
 }
Example #26
0
/*---------------------------------------------------*/
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; 
}
Example #27
0
DskOctetFilter *dsk_bz2lib_decompressor_new   (void)
{
  DskBz2libDecompressor *rv = dsk_object_new (&dsk_bz2lib_decompressor_class);
  int zrv;
  zrv = BZ2_bzDecompressInit (&rv->bz2lib, DSK_FALSE, DSK_FALSE);
  if (zrv != BZ_OK)
    {
      dsk_warning ("BZ2_bzDecompressInit returned error");
      dsk_object_unref (rv);
      return NULL;
    }
  rv->initialized = DSK_TRUE;
  return DSK_OCTET_FILTER (rv);
}
Example #28
0
static struct bz2_mem *bz2_mem_open(const char *buffer, size_t size)
{
	struct bz2_mem *b = calloc(sizeof(struct bz2_mem), 1);
	b->stream = calloc(sizeof(bz_stream), 1);
	// next_in should point at the compressed data
	b->stream->next_in = (char *) buffer;
	// and avail_in should indicate how many bytes the library may read
	b->stream->avail_in = size;
	int bzerror = BZ2_bzDecompressInit(b->stream, 0, 0);
	if (bzerror != BZ_OK) {
		oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not build bz_stream from memory buffer: BZ2_bzDecompressInit returns %d", bzerror);
		bz2_mem_free(b);
		return NULL;
	}
	return b;
}
Example #29
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;
}
Example #30
0
BZip2StreamReader::BZip2StreamReader(void const* base, size_t length)
{
	if(!base) throw std::invalid_argument("base");
	if(length == 0) throw std::invalid_argument("length");

#ifdef _WIN64
	if(length > UINT32_MAX) throw std::invalid_argument("length");
#endif

	// Initialize the bzlib stream structure
	memset(&m_stream, 0, sizeof(bz_stream));
	m_stream.avail_in = static_cast<unsigned int>(length);
	m_stream.next_in  = reinterpret_cast<char*>(const_cast<void*>(base));

	int result = BZ2_bzDecompressInit(&m_stream, 0, 0);
	if(result != BZ_OK) throw std::exception("bzip2: decompression stream could not be initialized");
}