2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* uncompress.c
|
|
|
|
*
|
|
|
|
* (C) Copyright 1999 Linus Torvalds
|
|
|
|
*
|
|
|
|
* cramfs interfaces to the uncompression library. There's really just
|
|
|
|
* three entrypoints:
|
|
|
|
*
|
|
|
|
* - cramfs_uncompress_init() - called to initialize the thing.
|
|
|
|
* - cramfs_uncompress_exit() - tell me when you're done
|
|
|
|
* - cramfs_uncompress_block() - uncompress a block.
|
|
|
|
*
|
|
|
|
* NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
|
|
|
|
* only have one stream, and we'll initialize it only once even if it
|
|
|
|
* then is used by multiple filesystems.
|
|
|
|
*/
|
|
|
|
|
2014-08-08 21:22:50 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/zlib.h>
|
2013-12-10 21:54:28 +00:00
|
|
|
#include "internal.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static z_stream stream;
|
|
|
|
static int initialized;
|
|
|
|
|
|
|
|
/* Returns length of decompressed data. */
|
|
|
|
int cramfs_uncompress_block(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) {
|
2014-08-08 21:22:48 +00:00
|
|
|
pr_err("zlib_inflateReset error %d\n", err);
|
2005-04-16 22:20:36 +00:00
|
|
|
zlib_inflateEnd(&stream);
|
|
|
|
zlib_inflateInit(&stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = zlib_inflate(&stream, Z_FINISH);
|
|
|
|
if (err != Z_STREAM_END)
|
|
|
|
goto err;
|
|
|
|
return stream.total_out;
|
|
|
|
|
|
|
|
err:
|
2014-08-08 21:22:48 +00:00
|
|
|
pr_err("Error %d while decompressing!\n", err);
|
|
|
|
pr_err("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
|
2009-04-02 23:59:15 +00:00
|
|
|
return -EIO;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cramfs_uncompress_init(void)
|
|
|
|
{
|
|
|
|
if (!initialized++) {
|
|
|
|
stream.workspace = vmalloc(zlib_inflate_workspacesize());
|
2014-08-08 21:22:52 +00:00
|
|
|
if (!stream.workspace) {
|
2005-04-16 22:20:36 +00:00
|
|
|
initialized = 0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
stream.next_in = NULL;
|
|
|
|
stream.avail_in = 0;
|
|
|
|
zlib_inflateInit(&stream);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 09:01:05 +00:00
|
|
|
void cramfs_uncompress_exit(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
if (!--initialized) {
|
|
|
|
zlib_inflateEnd(&stream);
|
|
|
|
vfree(stream.workspace);
|
|
|
|
}
|
|
|
|
}
|