]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/zmod.h
c5216a8fd3e9c2aa00ea2eb7e9d4d750cfd68b44
[mirror_spl-debian.git] / include / sys / zmod.h
1 #ifndef _SPL_ZMOD_H
2 #define _SPL_ZMOD_H
3
4 #include <linux/zlib.h>
5
6 /* NOTE: z_compress_level/z_uncompress are nearly identical copies of
7 * the compress2/uncompress functions provided by the official zlib
8 * package available at http://zlib.net/. The only changes made we to
9 * slightly adapt the functioned called to match the linux kernel
10 * implementation of zlib.
11 */
12
13 /* ===========================================================================
14 * Compresses the source buffer into the destination buffer. The level
15 * parameter has the same meaning as in deflateInit. sourceLen is the byte
16 * length of the source buffer. Upon entry, destLen is the total size of the
17 * destination buffer, which must be at least 0.1% larger than sourceLen plus
18 * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
19 *
20 * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
21 * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
22 * Z_STREAM_ERROR if the level parameter is invalid.
23 */
24 static __inline__ int
25 z_compress_level(Byte *dest, uLong *destLen, const Byte *source,
26 uLong sourceLen, int level)
27 {
28 z_stream stream;
29 int err;
30
31 stream.next_in = (Byte *)source;
32 stream.avail_in = (uInt)sourceLen;
33 #ifdef MAXSEG_64K
34 /* Check for source > 64K on 16-bit machine: */
35 if ((uLong)stream.avail_in != sourceLen)
36 return Z_BUF_ERROR;
37 #endif
38 stream.next_out = dest;
39 stream.avail_out = (uInt)*destLen;
40
41 if ((uLong)stream.avail_out != *destLen)
42 return Z_BUF_ERROR;
43
44 err = zlib_deflateInit(&stream, level);
45 if (err != Z_OK)
46 return err;
47
48 err = zlib_deflate(&stream, Z_FINISH);
49 if (err != Z_STREAM_END) {
50 zlib_deflateEnd(&stream);
51 return err == Z_OK ? Z_BUF_ERROR : err;
52 }
53 *destLen = stream.total_out;
54
55 err = zlib_deflateEnd(&stream);
56 return err;
57 } /* z_compress_level() */
58
59 /* ===========================================================================
60 * Decompresses the source buffer into the destination buffer. sourceLen is
61 * the byte length of the source buffer. Upon entry, destLen is the total
62 * size of the destination buffer, which must be large enough to hold the
63 * entire uncompressed data. (The size of the uncompressed data must have
64 * been saved previously by the compressor and transmitted to the decompressor
65 * by some mechanism outside the scope of this compression library.)
66 * Upon exit, destLen is the actual size of the compressed buffer.
67 * This function can be used to decompress a whole file at once if the
68 * input file is mmap'ed.
69 *
70 * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
71 * enough memory, Z_BUF_ERROR if there was not enough room in the output
72 * buffer, or Z_DATA_ERROR if the input data was corrupted.
73 */
74 static __inline__ int
75 z_uncompress(Byte *dest, uLong *destLen, const Byte *source, uLong sourceLen)
76 {
77 z_stream stream;
78 int err;
79
80 stream.next_in = (Byte *)source;
81 stream.avail_in = (uInt)sourceLen;
82 /* Check for source > 64K on 16-bit machine: */
83 if ((uLong)stream.avail_in != sourceLen)
84 return Z_BUF_ERROR;
85
86 stream.next_out = dest;
87 stream.avail_out = (uInt)*destLen;
88
89 if ((uLong)stream.avail_out != *destLen)
90 return Z_BUF_ERROR;
91
92 err = zlib_inflateInit(&stream);
93 if (err != Z_OK)
94 return err;
95
96 err = zlib_inflate(&stream, Z_FINISH);
97 if (err != Z_STREAM_END) {
98 zlib_inflateEnd(&stream);
99
100 if (err == Z_NEED_DICT ||
101 (err == Z_BUF_ERROR && stream.avail_in == 0))
102 return Z_DATA_ERROR;
103
104 return err;
105 }
106 *destLen = stream.total_out;
107
108 err = zlib_inflateEnd(&stream);
109 return err;
110 } /* z_uncompress() */
111
112 #endif /* SPL_ZMOD_H */