make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / fs / cramfs / uncompress.c
1 /*
2  * uncompress.c
3  *
4  * Copyright (C) 1999 Linus Torvalds
5  * Copyright (C) 2000-2002 Transmeta Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License (Version 2) as
9  * published by the Free Software Foundation.
10  *
11  * cramfs interfaces to the uncompression library. There's really just
12  * three entrypoints:
13  *
14  *  - cramfs_uncompress_init() - called to initialize the thing.
15  *  - cramfs_uncompress_exit() - tell me when you're done
16  *  - cramfs_uncompress_block() - uncompress a block.
17  *
18  * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
19  * only have one stream, and we'll initialize it only once even if it
20  * then is used by multiple filesystems.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/vmalloc.h>
26 #include <linux/zlib.h>
27
28 static z_stream stream;
29 static int initialized;
30
31 /* Returns length of decompressed data. */
32 int cramfs_uncompress_block(void *dst, int dstlen, void *src, int srclen)
33 {
34         int err;
35
36         stream.next_in = src;
37         stream.avail_in = srclen;
38
39         stream.next_out = dst;
40         stream.avail_out = dstlen;
41
42         err = zlib_inflateReset(&stream);
43         if (err != Z_OK) {
44                 printk("zlib_inflateReset error %d\n", err);
45                 zlib_inflateEnd(&stream);
46                 zlib_inflateInit(&stream);
47         }
48
49         err = zlib_inflate(&stream, Z_FINISH);
50         if (err != Z_STREAM_END)
51                 goto err;
52         return stream.total_out;
53
54 err:
55         printk("Error %d while decompressing!\n", err);
56         printk("%p(%d)->%p(%d)\n", src, srclen, dst, dstlen);
57         return 0;
58 }
59
60 int cramfs_uncompress_init(void)
61 {
62         if (!initialized++) {
63                 stream.workspace = vmalloc(zlib_inflate_workspacesize());
64                 if ( !stream.workspace ) {
65                         initialized = 0;
66                         return -ENOMEM;
67                 }
68                 stream.next_in = NULL;
69                 stream.avail_in = 0;
70                 zlib_inflateInit(&stream);
71         }
72         return 0;
73 }
74
75 int cramfs_uncompress_exit(void)
76 {
77         if (!--initialized) {
78                 zlib_inflateEnd(&stream);
79                 vfree(stream.workspace);
80         }
81         return 0;
82 }