/* Returns length of decompressed data. */
int cramfs_uncompress_block_gzip(void *dst, int dstlen, void *src, int srclen)
{
	int err;

	stream.next_in = src;
	stream.avail_in = srclen;

	stream.next_out = dst;
	stream.avail_out = dstlen;

	err = zlib_inflateReset(&stream);
	if (err != Z_OK) {
		printk("zlib_inflateReset error %d\n", err);
		zlib_inflateEnd(&stream);
		zlib_inflateInit(&stream);
	}

	err = zlib_inflate(&stream, Z_FINISH);
	if (err != Z_STREAM_END)
		goto err;
	return stream.total_out;

err:
	printk("Error %d while decompressing!\n", err);
	printk("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
	return 0;
}
예제 #2
0
파일: platform.c 프로젝트: infidel/linux
/* Derived from logfs_uncompress */
static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
{
    int err, ret;

    ret = -EIO;
    err = zlib_inflateInit2(&stream, WINDOW_BITS);
    if (err != Z_OK)
        goto error;

    stream.next_in = in;
    stream.avail_in = inlen;
    stream.total_in = 0;
    stream.next_out = out;
    stream.avail_out = outlen;
    stream.total_out = 0;

    err = zlib_inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END)
        goto error;

    err = zlib_inflateEnd(&stream);
    if (err != Z_OK)
        goto error;

    ret = stream.total_out;
error:
    return ret;
}
예제 #3
0
파일: fzp.c 프로젝트: vanElden/burp
// There is no zlib gztruncate. Inflate it, truncate it, recompress it.
static int gztruncate(const char *path, off_t length, int compression)
{
	int ret=1;
	char tmp[16];
	char *dest=NULL;
	char *dest2=NULL;
	snprintf(tmp, sizeof(tmp), ".%d", getpid());
	if(!(dest=prepend(path, tmp))
	  || !(dest2=prepend(dest, "-2"))
	  || zlib_inflate(NULL, path, dest, NULL))
		goto end;
	if(truncate(dest, length))
	{
		logp("truncate of %s failed in %s\n", dest, __func__);
		goto end;
	}
	if(compress_file(dest, dest2, compression))
		goto end;
	unlink(dest);
	ret=do_rename(dest2, path);
end:
	if(dest) unlink(dest);
	if(dest2) unlink(dest2);
	free_w(&dest);
	free_w(&dest2);
	return ret;
}
예제 #4
0
int ctf_uncompress (char *dest, int *destLen, char *source, int sourceLen)
{
	z_stream stream;
	int err;

	memset(&stream, 0, sizeof stream);

	stream.next_in = source;
	stream.avail_in = sourceLen;
	/* Check for source > 64K on 16-bit machine: */
	if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;

	stream.next_out = dest;
	stream.avail_out = (uInt)*destLen;
	if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

	err = zlib_inflateInit(&stream);
	if (err != Z_OK) return err;

	err = zlib_inflate(&stream, Z_FINISH);
	last_err = stream.msg;
	if (err != Z_STREAM_END) {
		zlib_inflateEnd(&stream);
		if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
	        	return Z_DATA_ERROR;
		return err;
	}
	*destLen = stream.total_out;

	err = zlib_inflateEnd(&stream);
	return err;
}
예제 #5
0
void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
		      __u32 srclen, __u32 destlen)
{
	z_stream strm;
	int ret;

	down(&inflate_sem);
	strm.workspace = inflate_workspace;

	if (Z_OK != zlib_inflateInit(&strm)) {
		printk(KERN_WARNING "inflateInit failed\n");
		up(&inflate_sem);
		return;
	}
	strm.next_in = data_in;
	strm.avail_in = srclen;
	strm.total_in = 0;
	
	strm.next_out = cpage_out;
	strm.avail_out = destlen;
	strm.total_out = 0;

	while((ret = zlib_inflate(&strm, Z_FINISH)) == Z_OK)
		;
	if (ret != Z_STREAM_END) {
		printk(KERN_NOTICE "inflate returned %d\n", ret);
	}
	zlib_inflateEnd(&strm);
	up(&inflate_sem);
}
예제 #6
0
int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen)
{
	int err, ret;

	ret = -EIO;
	mutex_lock(&compr_mutex);
	err = zlib_inflateInit(&stream);
	if (err != Z_OK)
		goto error;

	stream.next_in = in;
	stream.avail_in = inlen;
	stream.total_in = 0;
	stream.next_out = out;
	stream.avail_out = outlen;
	stream.total_out = 0;

	err = zlib_inflate(&stream, Z_FINISH);
	if (err != Z_STREAM_END)
		goto error;

	err = zlib_inflateEnd(&stream);
	if (err != Z_OK)
		goto error;

	ret = 0;
error:
	mutex_unlock(&compr_mutex);
	return ret;
}
예제 #7
0
static int zlib_decompress_final(struct crypto_pcomp *tfm,
				 struct comp_request *req)
{
	int ret;
	struct zlib_ctx *dctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
	struct z_stream_s *stream = &dctx->decomp_stream;

	pr_debug("avail_in %u, avail_out %u\n", req->avail_in, req->avail_out);
	stream->next_in = req->next_in;
	stream->avail_in = req->avail_in;
	stream->next_out = req->next_out;
	stream->avail_out = req->avail_out;

	if (dctx->decomp_windowBits < 0) {
		ret = zlib_inflate(stream, Z_SYNC_FLUSH);
		/*
		 * Work around a bug in zlib, which sometimes wants to taste an
		 * extra byte when being used in the (undocumented) raw deflate
		 * mode. (From USAGI).
		 */
		if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
			const void *saved_next_in = stream->next_in;
			u8 zerostuff = 0;

			stream->next_in = &zerostuff;
			stream->avail_in = 1;
			ret = zlib_inflate(stream, Z_FINISH);
			stream->next_in = saved_next_in;
			stream->avail_in = 0;
		}
	} else
		ret = zlib_inflate(stream, Z_FINISH);
	if (ret != Z_STREAM_END) {
		pr_debug("zlib_inflate failed %d\n", ret);
		return -EINVAL;
	}

	ret = req->avail_out - stream->avail_out;
	pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
		 stream->avail_in, stream->avail_out,
		 req->avail_in - stream->avail_in, ret);
	req->next_in = stream->next_in;
	req->avail_in = stream->avail_in;
	req->next_out = stream->next_out;
	req->avail_out = stream->avail_out;
	return ret;
}
예제 #8
0
static void
gzip1_decompress(coa_t coa, __u8 * src_first, size_t src_len,
                 __u8 * dst_first, size_t *dst_len)
{
    int ret = 0;
    struct z_stream_s stream;

    assert("edward-843", coa != NULL);
    assert("edward-876", src_len != 0);

    stream.workspace = coa;
    ret = zlib_inflateInit2(&stream, -GZIP1_DEF_WINBITS);
    if (ret != Z_OK) {
        warning("edward-774", "zlib_inflateInit2 returned %d\n", ret);
        return;
    }
    ret = zlib_inflateReset(&stream);
    if (ret != Z_OK) {
        warning("edward-775", "zlib_inflateReset returned %d\n", ret);
        return;
    }

    stream.next_in = src_first;
    stream.avail_in = src_len;
    stream.next_out = dst_first;
    stream.avail_out = *dst_len;

    ret = zlib_inflate(&stream, Z_SYNC_FLUSH);
    /*
     * Work around a bug in zlib, which sometimes wants to taste an extra
     * byte when being used in the (undocumented) raw deflate mode.
     * (From USAGI).
     */
    if (ret == Z_OK && !stream.avail_in && stream.avail_out) {
        u8 zerostuff = 0;
        stream.next_in = &zerostuff;
        stream.avail_in = 1;
        ret = zlib_inflate(&stream, Z_FINISH);
    }
    if (ret != Z_STREAM_END) {
        warning("edward-776", "zlib_inflate returned %d\n", ret);
        return;
    }
    *dst_len = stream.total_out;
    return;
}
예제 #9
0
파일: geutils.c 프로젝트: drewet/libge
ge_Buffer* geDecompress(void* in, int insz, int mode){
	ge_Buffer* out = (ge_Buffer*)geMalloc(sizeof(ge_Buffer));
	if(mode == GE_COMPRESSION_ZLIB){
		ge_Buffer b_in = { in, insz, 0 };
		zlib_inflate(out, &b_in);
	}
	return out;
}
예제 #10
0
파일: restore_server.c 프로젝트: fenio/burp
static int inflate_or_link_oldfile(const char *oldpath, const char *infpath)
{
	int ret=0;
	struct stat statp;

	if(lstat(oldpath, &statp))
	{
		logp("could not lstat %s\n", oldpath);
		return -1;
	}

	if(dpth_is_compressed(oldpath))
	{
		FILE *source=NULL;
		FILE *dest=NULL;

		//logp("inflating...\n");

		if(!(dest=open_file(infpath, "wb")))
		{
			close_fp(&dest);
			return -1;
		}

		if(!statp.st_size)
		{
			// Empty file - cannot inflate.
			// just close the destination and we have duplicated a
			// zero length file.
			logp("asked to inflate zero length file: %s\n", oldpath);
			close_fp(&dest);
			return 0;
		}

		if(!(source=open_file(oldpath, "rb")))
		{
			close_fp(&dest);
			return -1;
		}

		if((ret=zlib_inflate(source, dest))!=Z_OK)
			logp("zlib_inflate returned: %d\n", ret);

		close_fp(&source);
		close_fp(&dest);
	}
	else
	{
		// Not compressed - just hard link it.
		if(link(oldpath, infpath))
		{
			logp("hardlink %s to %s failed: %s\n",
				infpath, oldpath, strerror(errno));
			ret=-1;
		}
	}
	return ret;
}
예제 #11
0
static int zlib_decompress_final(struct crypto_pcomp *tfm,
				 struct comp_request *req)
{
	int ret;
	struct zlib_ctx *dctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
	struct z_stream_s *stream = &dctx->decomp_stream;

	pr_debug("avail_in %u, avail_out %u\n", req->avail_in, req->avail_out);
	stream->next_in = req->next_in;
	stream->avail_in = req->avail_in;
	stream->next_out = req->next_out;
	stream->avail_out = req->avail_out;

	if (dctx->decomp_windowBits < 0) {
		ret = zlib_inflate(stream, Z_SYNC_FLUSH);
		if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
			const void *saved_next_in = stream->next_in;
			u8 zerostuff = 0;

			stream->next_in = &zerostuff;
			stream->avail_in = 1;
			ret = zlib_inflate(stream, Z_FINISH);
			stream->next_in = saved_next_in;
			stream->avail_in = 0;
		}
	} else
		ret = zlib_inflate(stream, Z_FINISH);
	if (ret != Z_STREAM_END) {
		pr_debug("zlib_inflate failed %d\n", ret);
		return -EINVAL;
	}

	ret = req->avail_out - stream->avail_out;
	pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
		 stream->avail_in, stream->avail_out,
		 req->avail_in - stream->avail_in, ret);
	req->next_in = stream->next_in;
	req->avail_in = stream->avail_in;
	req->next_out = stream->next_out;
	req->avail_out = stream->avail_out;
	return ret;
}
예제 #12
0
int sqlzma_un(struct sqlzma_un *un, struct sized_buf *src,
	      struct sized_buf *dst)
{
	int err, by_lzma = 1;
	if (un->un_lzma && is_lzma(*src->buf)) {
		un->un_cmbuf = src->buf;
		un->un_cmlen = src->sz;
		un->un_resbuf = dst->buf;
		un->un_reslen = dst->sz;

		/* this library is thread-safe */
		err = LzmaUncompress(un);
		goto out;
	}

	by_lzma = 0;

//#if defined(CONFIG_MIPS_BRCM)
#if 1//disable zlib inflate
    /* The FS is compressed with LZMA for BRCM: do not use zlib */
    printk("%s: ZLIB decompression is not supported\n", __func__);
    err = -EINVAL;
#else
	err = zlib_inflateReset(&un->un_stream);
	if (unlikely(err != Z_OK))
		goto out;
	un->un_stream.next_in = src->buf;
	un->un_stream.avail_in = src->sz;
	un->un_stream.next_out = dst->buf;
	un->un_stream.avail_out = dst->sz;
	err = zlib_inflate(&un->un_stream, Z_FINISH);
	if (err == Z_STREAM_END)
		err = 0;
#endif 
 out:
	if (unlikely(err)) {
#ifdef __KERNEL__
		WARN_ON_ONCE(1);
#else
		char a[64] = "ZLIB ";
		if (by_lzma) {
			strcpy(a, "LZMA ");
#ifdef _REENTRANT
			strerror_r(err, a + 5, sizeof(a) - 5);
#else
			strncat(a, strerror(err), sizeof(a) - 5);
#endif
		} else
			strncat(a, zError(err), sizeof(a) - 5);
		fprintf(stderr, "%s: %.*s\n", __func__, sizeof(a), a);
#endif
	}
	return err;
}
예제 #13
0
static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src,
                              unsigned int slen, u8 *dst, unsigned int *dlen)
{

    int ret = 0;
    struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
    struct z_stream_s *stream = &dctx->decomp_stream;

    ret = zlib_inflateReset(stream);
    if (ret != Z_OK) {
        ret = -EINVAL;
        goto out;
    }

    stream->next_in = (u8 *)src;
    stream->avail_in = slen;
    stream->next_out = (u8 *)dst;
    stream->avail_out = *dlen;

    ret = zlib_inflate(stream, Z_SYNC_FLUSH);
    /*
     * Work around a bug in zlib, which sometimes wants to taste an extra
     * byte when being used in the (undocumented) raw deflate mode.
     * (From USAGI).
     */
    if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
        u8 zerostuff = 0;
        stream->next_in = &zerostuff;
        stream->avail_in = 1;
        ret = zlib_inflate(stream, Z_FINISH);
    }
    if (ret != Z_STREAM_END) {
        ret = -EINVAL;
        goto out;
    }
    ret = 0;
    *dlen = stream->total_out;
out:
    return ret;
}
예제 #14
0
static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
{
	z_stream s;
	int r, i, flags;

	/* skip header */
	i = 10;
	flags = src[3];
	if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
		printf("bad gzipped data\n\r");
		exit();
	}
	if ((flags & EXTRA_FIELD) != 0)
		i = 12 + src[10] + (src[11] << 8);
	if ((flags & ORIG_NAME) != 0)
		while (src[i++] != 0)
			;
	if ((flags & COMMENT) != 0)
		while (src[i++] != 0)
			;
	if ((flags & HEAD_CRC) != 0)
		i += 2;
	if (i >= *lenp) {
		printf("gunzip: ran out of data in header\n\r");
		exit();
	}

	if (zlib_inflate_workspacesize() > sizeof(scratch)) {
		printf("gunzip needs more mem\n");
		exit();
	}
	memset(&s, 0, sizeof(s));
	s.workspace = scratch;
	r = zlib_inflateInit2(&s, -MAX_WBITS);
	if (r != Z_OK) {
		printf("inflateInit2 returned %d\n\r", r);
		exit();
	}
	s.next_in = src + i;
	s.avail_in = *lenp - i;
	s.next_out = dst;
	s.avail_out = dstlen;
	r = zlib_inflate(&s, Z_FULL_FLUSH);
	if (r != Z_OK && r != Z_STREAM_END) {
		printf("inflate returned %d msg: %s\n\r", r, s.msg);
		exit();
	}
	*lenp = s.next_out - (unsigned char *) dst;
	zlib_inflateEnd(&s);
}
예제 #15
0
int sqlzma_un(struct sqlzma_un *un, struct sized_buf *src,
	      struct sized_buf *dst)
{
	int err, by_lzma = 0;
	if (un->un_lzma && is_lzma(*src->buf)) {
		by_lzma = 1;
		un->un_cmbuf = src->buf;
		un->un_cmlen = src->sz;
		un->un_resbuf = dst->buf;
		un->un_reslen = dst->sz;

		/* this library is thread-safe */
		err = LzmaUncompress(un);
		goto out;
	}

	err = zlib_inflateReset(&un->un_stream);
	if (unlikely(err != Z_OK))
		goto out;
	un->un_stream.next_in = src->buf;
	un->un_stream.avail_in = src->sz;
	un->un_stream.next_out = dst->buf;
	un->un_stream.avail_out = dst->sz;
	err = zlib_inflate(&un->un_stream, Z_FINISH);
	if (err == Z_STREAM_END)
		err = 0;

 out:
	if (err) {
#ifdef __KERNEL__
		WARN_ON_ONCE(1);
#else
		char a[64] = "ZLIB ";
		if (by_lzma) {
			strcpy(a, "LZMA ");
#ifdef _REENTRANT
			strerror_r(err, a + 5, sizeof(a) - 5);
#else
			strncat(a, strerror(err), sizeof(a) - 5);
#endif
		} else
			strncat(a, zError(err), sizeof(a) - 5);
		fprintf(stderr, "%s: %.*s\n", __func__, sizeof(a), a);
#endif
	}
	return err;
}
예제 #16
0
void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
{
	z_stream s;
	int r, i, flags;

	/* skip header */
	i = 10;
	flags = src[3];
	if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
		puts("bad gzipped data\n");
		exit();
	}
	if ((flags & EXTRA_FIELD) != 0)
		i = 12 + src[10] + (src[11] << 8);
	if ((flags & ORIG_NAME) != 0)
		while (src[i++] != 0)
			;
	if ((flags & COMMENT) != 0)
		while (src[i++] != 0)
			;
	if ((flags & HEAD_CRC) != 0)
		i += 2;
	if (i >= *lenp) {
		puts("gunzip: ran out of data in header\n");
		exit();
	}

	/* Initialize ourself. */
	s.workspace = zalloc(zlib_inflate_workspacesize());
	r = zlib_inflateInit2(&s, -MAX_WBITS);
	if (r != Z_OK) {
		puts("zlib_inflateInit2 returned "); puthex(r); puts("\n");
		exit();
	}
	s.next_in = src + i;
	s.avail_in = *lenp - i;
	s.next_out = dst;
	s.avail_out = dstlen;
	r = zlib_inflate(&s, Z_FINISH);
	if (r != Z_OK && r != Z_STREAM_END) {
		puts("inflate returned "); puthex(r); puts("\n");
		exit();
	}
	*lenp = s.next_out - (unsigned char *) dst;
	zlib_inflateEnd(&s);
}
예제 #17
0
void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp)
{
	z_stream s;
	int r, i, flags;

        
        i = 10;
        flags = src[3];
        if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
                
                exit();
        }
        if ((flags & EXTRA_FIELD) != 0)
                i = 12 + src[10] + (src[11] << 8);
        if ((flags & ORIG_NAME) != 0)
                while (src[i++] != 0)
                        ;
        if ((flags & COMMENT) != 0)
                while (src[i++] != 0)
                        ;
        if ((flags & HEAD_CRC) != 0)
                i += 2;
        if (i >= *lenp) {
                
                exit();
        }

	s.workspace = zalloc(zlib_inflate_workspacesize());
        r = zlib_inflateInit2(&s, -MAX_WBITS);
        if (r != Z_OK) {
                
                exit();
        }
        s.next_in = src + i;
        s.avail_in = *lenp - i;
        s.next_out = dst;
        s.avail_out = dstlen;
        r = zlib_inflate(&s, Z_FINISH);
        if (r != Z_OK && r != Z_STREAM_END) {
                
                exit();
        }
        *lenp = s.next_out - (unsigned char *) dst;
        zlib_inflateEnd(&s);
}
/* _VMKLNX_CODECHECK_: zlib_inflate_blob */
int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
                      const void *buf, unsigned int len)
{
        const u8 *zbuf = buf;
        struct z_stream_s *strm;
        int rc;
        
#if defined(__VMKLNX__)
	VMK_ASSERT(vmk_PreemptionIsEnabled() == VMK_FALSE);
#endif
        rc = -ENOMEM;
        strm = kmalloc(sizeof(*strm), GFP_KERNEL);
        if (strm == NULL)
                goto gunzip_nomem1;
        strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
        if (strm->workspace == NULL)
                goto gunzip_nomem2;
        
        /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)
         * expected to be stripped from input
         */
        strm->next_in = zbuf;
        strm->avail_in = len;
        strm->next_out = gunzip_buf;
        strm->avail_out = sz;
        
        rc = zlib_inflateInit2(strm, -MAX_WBITS);
        if (rc == Z_OK) {
                rc = zlib_inflate(strm, Z_FINISH);
                /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
                if (rc == Z_STREAM_END)
                        rc = sz - strm->avail_out;
                else
                        rc = -EINVAL;
                zlib_inflateEnd(strm);
        } else
                rc = -EINVAL;
        
        kfree(strm->workspace);
 gunzip_nomem2:
        kfree(strm);
 gunzip_nomem1:
        return rc; /* returns Z_OK (0) if successful */
}
예제 #19
0
파일: spl-zlib.c 프로젝트: dunecn/spl
/*
 * Decompresses the source buffer into the destination buffer.  sourceLen is
 * the byte length of the source buffer. Upon entry, destLen is the total
 * size of the destination buffer, which must be large enough to hold the
 * entire uncompressed data. (The size of the uncompressed data must have
 * been saved previously by the compressor and transmitted to the decompressor
 * by some mechanism outside the scope of this compression library.)
 * Upon exit, destLen is the actual size of the compressed buffer.
 * This function can be used to decompress a whole file at once if the
 * input file is mmap'ed.
 *
 * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 * enough memory, Z_BUF_ERROR if there was not enough room in the output
 * buffer, or Z_DATA_ERROR if the input data was corrupted.
 */
int
z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
{
	z_stream stream;
	int err;

	stream.next_in = (Byte *)source;
	stream.avail_in = (uInt)sourceLen;
	stream.next_out = dest;
	stream.avail_out = (uInt)*destLen;

	if ((size_t)stream.avail_out != *destLen)
		return Z_BUF_ERROR;

	stream.workspace = zlib_workspace_alloc(KM_SLEEP);
	if (!stream.workspace)
		return Z_MEM_ERROR;

	err = zlib_inflateInit(&stream);
	if (err != Z_OK) {
		zlib_workspace_free(stream.workspace);
		return err;
	}

	err = zlib_inflate(&stream, Z_FINISH);
	if (err != Z_STREAM_END) {
		zlib_inflateEnd(&stream);
		zlib_workspace_free(stream.workspace);

		if (err == Z_NEED_DICT ||
		   (err == Z_BUF_ERROR && stream.avail_in == 0))
			return Z_DATA_ERROR;

		return err;
	}
	*destLen = stream.total_out;

	err = zlib_inflateEnd(&stream);
	zlib_workspace_free(stream.workspace);

	return err;
}
예제 #20
0
/* Utility function: initialize zlib, unpack binary blob, clean up zlib,
 * return len or negative error code.
 */
int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
		      const void *buf, unsigned int len)
{
	const u8 *zbuf = buf;
	struct z_stream_s *strm;
	int rc;

	rc = -ENOMEM;
	strm = MALLOC(sizeof(*strm));
	if (strm == NULL)
		goto gunzip_nomem1;
	strm->workspace = MALLOC(zlib_inflate_workspacesize());
	if (strm->workspace == NULL)
		goto gunzip_nomem2;

	/* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)
	 * expected to be stripped from input
	 */
	strm->next_in = zbuf;
	strm->avail_in = len;
	strm->next_out = gunzip_buf;
	strm->avail_out = sz;

	rc = zlib_inflateInit2(strm, -MAX_WBITS);
	if (rc == Z_OK) {
		rc = zlib_inflate(strm, Z_FINISH);
		/* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
		if (rc == Z_STREAM_END)
			rc = sz - strm->avail_out;
		else
			rc = -EINVAL;
		zlib_inflateEnd(strm);
	} else
		rc = -EINVAL;

	FREE(strm->workspace);
gunzip_nomem2:
	FREE(strm);
gunzip_nomem1:
	return rc; /* returns Z_OK (0) if successful */
}
예제 #21
0
파일: 7ciso.c 프로젝트: cielavenir/7razf
static int _decompress(FILE *in, FILE *out){
	CISO_H header;
	fread(&header,1,sizeof(header),in);
	if(memcmp(header.magic,"CISO",4)||(header.header_size&&header.header_size!=sizeof(header))){fprintf(stderr,"not CISO\n");return 1;}
	int total_block=align2p(header.block_size,header.total_bytes)/header.block_size;
	fread(buf,4,total_block+1,in);
	int i=0;
	int counter=sizeof(header)+4*(total_block+1);

	//void* coder=NULL;
	//lzmaCreateCoder(&coder,0x040108,0,0);
	for(;i<total_block;i++){
		u32 index=read32(buf+4*i);
		u32 plain=index&0x80000000;
		index&=0x7fffffff;
		u32 pos=index<<header.align;
		if(pos>counter)fread(buf+BUFLEN-(pos-counter),1,pos-counter,in); //discard

		u32 size=((read32(buf+4*(i+1))&0x7fffffff)<<header.align) - index;
		if(plain){
			fread(__decompbuf,1,size,in);counter+=size;
			fwrite(__decompbuf,1,size,out);
		}else{
			fread(__compbuf,1,size,in);counter+=size;
			size_t decompsize=header.block_size;

			int r=zlib_inflate(__decompbuf,&decompsize,__compbuf,size);
			if(r){
				fprintf(stderr,"inflate %d\n",r);
				return 1;
			}

			fwrite(__decompbuf,1,decompsize,out);
		}
		if((i+1)%256==0)fprintf(stderr,"%d / %d\r",i+1,total_block);
	}
	fprintf(stderr,"%d / %d done.\n",i,total_block);
	//lzmaDestroyCoder(&coder);
	return 0;
}
예제 #22
0
static int zlib_decompress_update(struct crypto_pcomp *tfm,
				  struct comp_request *req)
{
	int ret;
	struct zlib_ctx *dctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
	struct z_stream_s *stream = &dctx->decomp_stream;

	pr_debug("avail_in %u, avail_out %u\n", req->avail_in, req->avail_out);
	stream->next_in = req->next_in;
	stream->avail_in = req->avail_in;
	stream->next_out = req->next_out;
	stream->avail_out = req->avail_out;

	ret = zlib_inflate(stream, Z_SYNC_FLUSH);
	switch (ret) {
	case Z_OK:
	case Z_STREAM_END:
		break;

	case Z_BUF_ERROR:
		pr_debug("zlib_inflate could not make progress\n");
		return -EAGAIN;

	default:
		pr_debug("zlib_inflate failed %d\n", ret);
		return -EINVAL;
	}

	ret = req->avail_out - stream->avail_out;
	pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
		 stream->avail_in, stream->avail_out,
		 req->avail_in - stream->avail_in, ret);
	req->next_in = stream->next_in;
	req->avail_in = stream->avail_in;
	req->next_out = stream->next_out;
	req->avail_out = stream->avail_out;
	return ret;
}
int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
{
	int len;

	if (state->s.workspace) {
		/* gunzipping */
		int r;

		state->s.next_out = dst;
		state->s.avail_out = dstlen;
		r = zlib_inflate(&state->s, Z_FULL_FLUSH);
		if (r != Z_OK && r != Z_STREAM_END)
			fatal("inflate returned %d msg: %s\n\r", r, state->s.msg);
		len = state->s.next_out - (unsigned char *)dst;
	} else {
		/* uncompressed image */
		len = min(state->s.avail_in, (unsigned)dstlen);
		memcpy(dst, state->s.next_in, len);
		state->s.next_in += len;
		state->s.avail_in -= len;
	}
	return len;
}
예제 #24
0
int main(int argc, char **argv)
{
	FILE *fin, *fout;
	int result;

	if (argc != 3)
	{
		usage(argv[0]);
		exit(1);
	}

	fin = fopen(argv[1], "r");
	if (fin == NULL)
	{
		fprintf(stderr, "couldn't open input file '%s'\n", argv[1]);
		exit(1);
	}

	fout = fopen(argv[2], "w");
	if (fin == NULL)
	{
		fprintf(stderr, "couldn't open output file '%s'\n", argv[2]);
		exit(1);
	}

	result = zlib_inflate(fin, fout);
	if (result != Z_OK)
	{
		fprintf(stderr, "zlib error %s\n", zlib_error2str(result));
		exit(1);
	}

	fclose(fin);
	fclose(fout);

	exit(0);
}
예제 #25
0
파일: restore.c 프로젝트: pkdevbox/burp
static int inflate_or_link_oldfile(struct asfd *asfd, const char *oldpath,
	const char *infpath, struct conf **cconfs, int compression)
{
	int ret=0;
	struct stat statp;

	if(lstat(oldpath, &statp))
	{
		logp("could not lstat %s\n", oldpath);
		return -1;
	}

	if(dpth_protocol1_is_compressed(compression, oldpath))
	{
		//logp("inflating...\n");

		if(!statp.st_size)
		{
			// Empty file - cannot inflate.
			logp("asked to inflate zero length file: %s\n",
				oldpath);
			return create_zero_length_file(infpath);
		}

		if((ret=zlib_inflate(asfd, oldpath, infpath, get_cntr(cconfs))))
			logp("zlib_inflate returned: %d\n", ret);
	}
	else
	{
		// Not compressed - just hard link it.
		if(do_link(oldpath, infpath, &statp, cconfs,
			1 /* allow overwrite of infpath */))
				return -1;
	}
	return ret;
}
예제 #26
0
static guchar *fb_gunzip(const guchar *gzip_data, ssize_t *len_ptr)
{
	gsize gzip_data_len	= *len_ptr;
	z_stream zstr;
	int gzip_err = 0;
	guchar *output_data;
	gulong gzip_len = G_MAXUINT16;

	g_return_val_if_fail(zlib_inflate != NULL, NULL);

	output_data = g_new0(guchar, gzip_len);

	zstr.next_in = gzip_data;
	zstr.avail_in = gzip_data_len;
	zstr.zalloc = Z_NULL;
	zstr.zfree = Z_NULL;
	zstr.opaque = Z_NULL;
	int flags = gzip_data[3];
	int offset = 4;
	/* if (flags & 0x04) offset += *tmp[] */
	zstr.next_in += offset;
	zstr.avail_in -= offset;
	zlib_inflateInit2_(&zstr, -MAX_WBITS, ZLIB_VERSION, sizeof(z_stream));
	zstr.next_out = output_data;
	zstr.avail_out = gzip_len;
	gzip_err = zlib_inflate(&zstr, Z_FINISH);
	zlib_inflateEnd(&zstr);

	purple_debug_info("facebook", "gzip len: %ld, len: %ld\n", gzip_len,
			gzip_data_len);
	purple_debug_info("facebook", "gzip flags: %d\n", flags);
	purple_debug_info("facebook", "gzip error: %d\n", gzip_err);

	*len_ptr = gzip_len;
	return output_data;
}
예제 #27
0
static int inflate_oldfile(const char *opath, const char *infpath,
	struct stat *statp, struct conf *conf)
{
	int ret=0;

	if(!statp->st_size)
	{
		FILE *dest;
		// Empty file - cannot inflate.
		// just close the destination and we have duplicated a
		// zero length file.
		if(!(dest=open_file(infpath, "wb"))) goto end;
		logp("asked to inflate zero length file: %s\n", opath);
		if(close_fp(&dest))
			logp("error closing %s in %s\n", infpath, __func__);
	}
	else if(zlib_inflate(NULL, opath, infpath, conf))
	{
		logp("zlib_inflate returned error\n");
		ret=-1;
	}
end:
	return ret;
}
예제 #28
0
/*
 * Read data of @inode from @block_start to @block_end and uncompress
 * to one zisofs block. Store the data in the @pages array with @pcount
 * entries. Start storing at offset @poffset of the first page.
 */
static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
				      loff_t block_end, int pcount,
				      struct page **pages, unsigned poffset,
				      int *errp)
{
	unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1];
	unsigned int bufsize = ISOFS_BUFFER_SIZE(inode);
	unsigned int bufshift = ISOFS_BUFFER_BITS(inode);
	unsigned int bufmask = bufsize - 1;
	int i, block_size = block_end - block_start;
	z_stream stream = { .total_out = 0,
			    .avail_in = 0,
			    .avail_out = 0, };
	int zerr;
	int needblocks = (block_size + (block_start & bufmask) + bufmask)
				>> bufshift;
	int haveblocks;
	blkcnt_t blocknum;
	struct buffer_head *bhs[needblocks + 1];
	int curbh, curpage;

	if (block_size > deflateBound(1UL << zisofs_block_shift)) {
		*errp = -EIO;
		return 0;
	}
	/* Empty block? */
	if (block_size == 0) {
		for ( i = 0 ; i < pcount ; i++ ) {
			if (!pages[i])
				continue;
			memset(page_address(pages[i]), 0, PAGE_SIZE);
			flush_dcache_page(pages[i]);
			SetPageUptodate(pages[i]);
		}
		return ((loff_t)pcount) << PAGE_SHIFT;
	}

	/* Because zlib is not thread-safe, do all the I/O at the top. */
	blocknum = block_start >> bufshift;
	memset(bhs, 0, (needblocks + 1) * sizeof(struct buffer_head *));
	haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks);
	ll_rw_block(REQ_OP_READ, 0, haveblocks, bhs);

	curbh = 0;
	curpage = 0;
	/*
	 * First block is special since it may be fractional.  We also wait for
	 * it before grabbing the zlib mutex; odds are that the subsequent
	 * blocks are going to come in in short order so we don't hold the zlib
	 * mutex longer than necessary.
	 */

	if (!bhs[0])
		goto b_eio;

	wait_on_buffer(bhs[0]);
	if (!buffer_uptodate(bhs[0])) {
		*errp = -EIO;
		goto b_eio;
	}

	stream.workspace = zisofs_zlib_workspace;
	mutex_lock(&zisofs_zlib_lock);
		
	zerr = zlib_inflateInit(&stream);
	if (zerr != Z_OK) {
		if (zerr == Z_MEM_ERROR)
			*errp = -ENOMEM;
		else
			*errp = -EIO;
		printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n",
			       zerr);
		goto z_eio;
	}

	while (curpage < pcount && curbh < haveblocks &&
	       zerr != Z_STREAM_END) {
		if (!stream.avail_out) {
			if (pages[curpage]) {
				stream.next_out = page_address(pages[curpage])
						+ poffset;
				stream.avail_out = PAGE_SIZE - poffset;
				poffset = 0;
			} else {
				stream.next_out = (void *)&zisofs_sink_page;
				stream.avail_out = PAGE_SIZE;
			}
		}
		if (!stream.avail_in) {
			wait_on_buffer(bhs[curbh]);
			if (!buffer_uptodate(bhs[curbh])) {
				*errp = -EIO;
				break;
			}
			stream.next_in  = bhs[curbh]->b_data +
						(block_start & bufmask);
			stream.avail_in = min_t(unsigned, bufsize -
						(block_start & bufmask),
						block_size);
			block_size -= stream.avail_in;
			block_start = 0;
		}

		while (stream.avail_out && stream.avail_in) {
			zerr = zlib_inflate(&stream, Z_SYNC_FLUSH);
			if (zerr == Z_BUF_ERROR && stream.avail_in == 0)
				break;
			if (zerr == Z_STREAM_END)
				break;
			if (zerr != Z_OK) {
				/* EOF, error, or trying to read beyond end of input */
				if (zerr == Z_MEM_ERROR)
					*errp = -ENOMEM;
				else {
					printk(KERN_DEBUG
					       "zisofs: zisofs_inflate returned"
					       " %d, inode = %lu,"
					       " page idx = %d, bh idx = %d,"
					       " avail_in = %ld,"
					       " avail_out = %ld\n",
					       zerr, inode->i_ino, curpage,
					       curbh, stream.avail_in,
					       stream.avail_out);
					*errp = -EIO;
				}
				goto inflate_out;
			}
		}

		if (!stream.avail_out) {
			/* This page completed */
			if (pages[curpage]) {
				flush_dcache_page(pages[curpage]);
				SetPageUptodate(pages[curpage]);
			}
			curpage++;
		}
		if (!stream.avail_in)
			curbh++;
	}
inflate_out:
	zlib_inflateEnd(&stream);

z_eio:
	mutex_unlock(&zisofs_zlib_lock);

b_eio:
	for (i = 0; i < haveblocks; i++)
		brelse(bhs[i]);
	return stream.total_out;
}
예제 #29
0
static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer,
	struct buffer_head **bh, int b, int offset, int length, int srclength,
	int pages)
{
	int zlib_err = 0, zlib_init = 0;
	int avail, bytes, k = 0, page = 0;
	z_stream *stream = msblk->stream;

	mutex_lock(&msblk->read_data_mutex);

	stream->avail_out = 0;
	stream->avail_in = 0;

	bytes = length;
	do {
		if (stream->avail_in == 0 && k < b) {
			avail = min(bytes, msblk->devblksize - offset);
			bytes -= avail;
			wait_on_buffer(bh[k]);
			if (!buffer_uptodate(bh[k]))
				goto release_mutex;

			if (avail == 0) {
				offset = 0;
				put_bh(bh[k++]);
				continue;
			}

			stream->next_in = bh[k]->b_data + offset;
			stream->avail_in = avail;
			offset = 0;
		}

		if (stream->avail_out == 0 && page < pages) {
			stream->next_out = buffer[page++];
			stream->avail_out = PAGE_CACHE_SIZE;
		}

		if (!zlib_init) {
			zlib_err = zlib_inflateInit(stream);
			if (zlib_err != Z_OK) {
				ERROR("zlib_inflateInit returned unexpected "
					"result 0x%x, srclength %d\n",
					zlib_err, srclength);
				goto release_mutex;
			}
			zlib_init = 1;
		}

		zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH);

		if (stream->avail_in == 0 && k < b)
			put_bh(bh[k++]);
	} while (zlib_err == Z_OK);

	if (zlib_err != Z_STREAM_END) {
		ERROR("zlib_inflate error, data probably corrupt\n");
		goto release_mutex;
	}

	zlib_err = zlib_inflateEnd(stream);
	if (zlib_err != Z_OK) {
		ERROR("zlib_inflate error, data probably corrupt\n");
		goto release_mutex;
	}

	length = stream->total_out;
	mutex_unlock(&msblk->read_data_mutex);
	return length;

release_mutex:
	mutex_unlock(&msblk->read_data_mutex);

	for (; k < b; k++)
		put_bh(bh[k]);

	return -EIO;
}
예제 #30
0
void
QcOsmPbfReader::read_blob()
{
  // size of the following blob
  int32_t data_size = m_blob_header.datasize();
  qDebug().nospace() << "  datasize = " << data_size;

  // ensure the blob is smaller then MAX_BLOB_SIZE
  if (data_size > OSMPBF::max_uncompressed_blob_size)
    qCritical() << "blob-size is bigger then allowed (" << data_size << " > " << OSMPBF::max_uncompressed_blob_size << ")";

  // read the blob from the file
  if (m_data_stream.readRawData(m_buffer, data_size) == -1)
    qCritical() << "unable to read blob from file";

  // parse the blob from the read-buffer
  if (!m_blob.ParseFromArray(m_buffer, data_size))
    qCritical() << "unable to parse blob";

  // tell about the blob-header
  qDebug().nospace() << "Blob (" << data_size << " bytes)";

  // set when we find at least one data stream
  bool found_data = false;

  // if the blob has uncompressed data
  if (m_blob.has_raw()) {
    // we have at least one datastream
    found_data = true;

    // size of the blob-data
    m_buffer_size = m_blob.raw().size();

    // check that raw_size is set correctly
    if (m_buffer_size != m_blob.raw_size())
      qWarning() << "  reports wrong raw_size: " << m_blob.raw_size() << " bytes";

    // tell about the blob-data
    qDebug().nospace() << "  contains uncompressed data: " << m_buffer_size << " bytes";

    // copy the uncompressed data over to the unpack_buffer
    memcpy(m_unpack_buffer, m_buffer, m_buffer_size);
  }

  // if the blob has zlib-compressed data
  if (m_blob.has_zlib_data()) {
    // issue a warning if there is more than one data steam, a blob may only contain one data stream
    if (found_data)
      qWarning() << "  contains several data streams";

    // we have at least one datastream
    found_data = true;

    // unpacked size
    zlib_inflate();
  }

  // if the blob has lzma-compressed data
  if (m_blob.has_lzma_data()) {
    // issue a warning if there is more than one data steam, a blob may only contain one data stream
    if (found_data)
      qWarning() << "  contains several data streams";

    // we have at least one datastream
    found_data = true;

    // unpacked size
    lzma_inflate();
  }

  // check we have at least one data-stream
  if (!found_data)
    qCritical() << "  does not contain any known data stream";

  // switch between different blob-types
  auto blob_type = m_blob_header.type();
  if (blob_type == "OSMHeader")
    // The Blob contains a serialized HeaderBlock message.
    // Every fileblock must have one of these blocks before the first 'OSMData' block.
    read_osm_header();
  else if (blob_type == "OSMData")
    // The Blob contains a serialized PrimitiveBlock message.
    read_osm_data();
  else
    // unknown blob type
    qWarning() << "  unknown blob type: " << m_blob_header.type().c_str();
}