File: /usr/src/linux/fs/cramfs/uncompress.c

1     /*
2      * uncompress.c
3      *
4      * (C) Copyright 1999 Linus Torvalds
5      *
6      * cramfs interfaces to the uncompression library. There's really just
7      * three entrypoints:
8      *
9      *  - cramfs_uncompress_init() - called to initialize the thing.
10      *  - cramfs_uncompress_exit() - tell me when you're done
11      *  - cramfs_uncompress_block() - uncompress a block.
12      *
13      * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
14      * only have one stream, and we'll initialize it only once even if it
15      * then is used by multiple filesystems.
16      */
17     
18     #include <linux/kernel.h>
19     
20     #include "inflate/zlib.h"
21     
22     static z_stream stream;
23     static int initialized;
24     
25     /* Returns length of decompressed data. */
26     int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
27     {
28     	int err;
29     
30     	stream.next_in = src;
31     	stream.avail_in = srclen;
32     
33     	stream.next_out = dst;
34     	stream.avail_out = dstlen;
35     
36     	err = cramfs_inflateReset(&stream);
37     	if (err != Z_OK) {
38     		printk("cramfs_inflateReset error %d\n", err);
39     		cramfs_inflateEnd(&stream);
40     		cramfs_inflateInit(&stream);
41     	}
42     
43     	err = cramfs_inflate(&stream, Z_FINISH);
44     	if (err != Z_STREAM_END)
45     		goto err;
46     	return stream.total_out;
47     
48     err:
49     	printk("Error %d while decompressing!\n", err);
50     	printk("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
51     	return 0;
52     }
53     
54     int cramfs_uncompress_init(void)
55     {
56     	if (!initialized++) {
57     		stream.next_in = NULL;
58     		stream.avail_in = 0;
59     		cramfs_inflateInit(&stream);
60     	}
61     	return 0;
62     }
63     
64     int cramfs_uncompress_exit(void)
65     {
66     	if (!--initialized)
67     		cramfs_inflateEnd(&stream);
68     	return 0;
69     }
70