Пример #1
0
int sqlzma_init(struct sqlzma_un *un, int do_lzma, unsigned int res_sz)
{
	int err;

	err = -ENOMEM;
	un->un_lzma = do_lzma;
	memset(un->un_a, 0, sizeof(un->un_a));
	un->un_a[SQUN_PROB].buf = un->un_prob;
	un->un_a[SQUN_PROB].sz = sizeof(un->un_prob);
	if (res_sz) {
		un->un_a[SQUN_RESULT].buf = kmalloc(res_sz, GFP_KERNEL);
		if (unlikely(!un->un_a[SQUN_RESULT].buf))
			return err;
		un->un_a[SQUN_RESULT].sz = res_sz;
	}

	un->un_stream.next_in = NULL;
	un->un_stream.avail_in = 0;
#ifdef __KERNEL__
	un->un_stream.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
	if (unlikely(!un->un_stream.workspace))
		return err;
#else
	un->un_stream.opaque = NULL;
	un->un_stream.zalloc = Z_NULL;
	un->un_stream.zfree = Z_NULL;
#endif
	err = zlib_inflateInit(&un->un_stream);
	if (unlikely(err == Z_MEM_ERROR))
		return -ENOMEM;
	BUG_ON(err);
	return err;
}
Пример #2
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);
}
/* 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;
}
Пример #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
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;
}
int cramfs_uncompress_init_gzip(void)
{
	if (!gzip_initialized++) {
		stream.workspace = vmalloc(zlib_inflate_workspacesize());
		if ( !stream.workspace ) {
			gzip_initialized = 0;
			return -ENOMEM;
		}
		stream.next_in = NULL;
		stream.avail_in = 0;
		zlib_inflateInit(&stream);
	}
	return 0;
}
Пример #7
0
int png_uncompress_init(void)
{
    if (!initialized++) {
        png_stream.workspace = malloc(zlib_inflate_workspacesize());
        if (!png_stream.workspace) {
            initialized = 0;
            return -ENOMEM;
        }
        png_stream.next_in = NULL;
        png_stream.avail_in = 0;
        zlib_inflateInit(&png_stream);
    }
    return 0;
}
Пример #8
0
/*
 * 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;
}
Пример #9
0
int sqlzma_init(struct sqlzma_un *un, int do_lzma, unsigned int res_sz)
{
	int err;

	err = -ENOMEM;
	un->un_lzma = do_lzma;
	memset(un->un_a, 0, sizeof(un->un_a));
	un->un_a[SQUN_PROB].buf = un->un_prob;
	un->un_a[SQUN_PROB].sz = sizeof(un->un_prob);
	if (res_sz) {
		un->un_a[SQUN_RESULT].buf = kmalloc(res_sz, GFP_KERNEL);
		if (unlikely(!un->un_a[SQUN_RESULT].buf))
			return err;
		un->un_a[SQUN_RESULT].sz = res_sz;
	}

	un->un_stream.next_in = NULL;
	un->un_stream.avail_in = 0;
//#if defined(CONFIG_MIPS_BRCM)
#if 1 // disable zlib inflate
	/* The FS is compressed with LZMA for BRCM: do not use zlib */
	un->un_stream.workspace = NULL;
	err = 0;
#else
#ifdef __KERNEL__
	un->un_stream.workspace = kmalloc(zlib_inflate_workspacesize(),
					  GFP_KERNEL);
	if (unlikely(!un->un_stream.workspace))
		return err;
#else
	un->un_stream.opaque = NULL;
	un->un_stream.zalloc = Z_NULL;
	un->un_stream.zfree = Z_NULL;
#endif
	err = zlib_inflateInit(&un->un_stream);
	if (unlikely(err == Z_MEM_ERROR))
		return -ENOMEM;
#endif
	BUG_ON(err);
	return err;
}
Пример #10
0
static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
{
	int ret = 0;
	struct z_stream_s *stream = &ctx->decomp_stream;

	stream->workspace = vzalloc(zlib_inflate_workspacesize());
	if (!stream->workspace) {
		ret = -ENOMEM;
		goto out;
	}
	if (format)
		ret = zlib_inflateInit(stream);
	else
		ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
	if (ret != Z_OK) {
		ret = -EINVAL;
		goto out_free;
	}
out:
	return ret;
out_free:
	vfree(stream->workspace);
	goto out;
}
Пример #11
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;
}
Пример #12
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;
}
Пример #13
0
static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm,
                           struct buffer_head **bh, int b, int offset, int length,
                           struct squashfs_page_actor *output)
{
    int zlib_err, zlib_init = 0, k = 0;
    z_stream *stream = strm;

    stream->avail_out = PAGE_SIZE;
    stream->next_out = squashfs_first_page(output);
    stream->avail_in = 0;

    do {
        if (stream->avail_in == 0 && k < b) {
            int avail = min(length, msblk->devblksize - offset);
            length -= avail;
            stream->next_in = bh[k]->b_data + offset;
            stream->avail_in = avail;
            offset = 0;
        }

        if (stream->avail_out == 0) {
            stream->next_out = squashfs_next_page(output);
            if (stream->next_out != NULL)
                stream->avail_out = PAGE_SIZE;
        }

        if (!zlib_init) {
            zlib_err = zlib_inflateInit(stream);
            if (zlib_err != Z_OK) {
                squashfs_finish_page(output);
                goto out;
            }
            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);

    squashfs_finish_page(output);

    if (zlib_err != Z_STREAM_END)
        goto out;

    zlib_err = zlib_inflateEnd(stream);
    if (zlib_err != Z_OK)
        goto out;

    if (k < b)
        goto out;

    return stream->total_out;

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

    return -EIO;
}
Пример #14
0
int inflate_read(LPBYTE pbySrcBuffer, LONG lSrcBuffSize, LPBYTE *ppbyDstBuffer, LONG *plDstBuffSize, BOOL bGZip)
{
#   define  CHUNK           4096

    int     nRet            = Z_DATA_ERROR;
    LONG    lReadSize       = 0;
    BYTE    pbyBufferTmp[CHUNK];
    LONG    lTotalSize      = 0;
    LPBYTE  pbyDstBuffer    = NULL;

    z_stream strm;

    strm.zalloc     =  Z_NULL;
    strm.zfree      =  Z_NULL;
    strm.opaque     =  Z_NULL;
    strm.avail_in   =  0;
    strm.next_in    =  Z_NULL;

    if (bGZip)
        nRet = zlib_inflateInit2(&strm, 47);
    else
        nRet = zlib_inflateInit(&strm);

    if (nRet != Z_OK)
        return nRet;

    strm.avail_in = lSrcBuffSize;
    strm.next_in  = pbySrcBuffer;

    do
    {
        strm.avail_out = CHUNK;
        strm.next_out  = pbyBufferTmp;

        nRet = zlib_inflate(&strm, Z_NO_FLUSH);

        switch (nRet)
        {
            case Z_NEED_DICT:
                nRet = Z_DATA_ERROR;
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                goto Exit0;
        }

        lReadSize    = CHUNK - strm.avail_out;
        lTotalSize  += lReadSize;
        pbyDstBuffer = (LPBYTE)realloc(pbyDstBuffer, lTotalSize);
        memcpy(pbyDstBuffer + lTotalSize - lReadSize, pbyBufferTmp, lReadSize);
    }
    while (strm.avail_out == 0);

Exit0:

    zlib_inflateEnd(&strm);

    if (Z_STREAM_END == nRet)
    {
        *ppbyDstBuffer = pbyDstBuffer;
        *plDstBuffSize = lTotalSize;
    }
    else
    {
        if (pbyDstBuffer)
        {
            free(pbyDstBuffer);
            pbyDstBuffer = NULL;
        }
        *ppbyDstBuffer = NULL;
        *plDstBuffSize = 0;
    }

    return (Z_STREAM_END == nRet) ? Z_OK : Z_DATA_ERROR;
}