]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/zmod.h
Update rwlocks to track owner to ensure correct semantics
[mirror_spl.git] / include / sys / zmod.h
CommitLineData
715f6251 1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
3b3ba48f 27#ifndef _SPL_ZMOD_H
28#define _SPL_ZMOD_H
29
30#include <linux/zlib.h>
31
32/* NOTE: z_compress_level/z_uncompress are nearly identical copies of
33 * the compress2/uncompress functions provided by the official zlib
34 * package available at http://zlib.net/. The only changes made we to
35 * slightly adapt the functioned called to match the linux kernel
36 * implementation of zlib.
37 */
38
39/* ===========================================================================
40 * Compresses the source buffer into the destination buffer. The level
41 * parameter has the same meaning as in deflateInit. sourceLen is the byte
42 * length of the source buffer. Upon entry, destLen is the total size of the
43 * destination buffer, which must be at least 0.1% larger than sourceLen plus
44 * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
45 *
46 * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
47 * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
48 * Z_STREAM_ERROR if the level parameter is invalid.
49 */
50static __inline__ int
4327ac3f
RC
51z_compress_level(void *dest, size_t *destLen, const void *source,
52 size_t sourceLen, int level)
3b3ba48f 53{
54 z_stream stream;
55 int err;
56
57 stream.next_in = (Byte *)source;
58 stream.avail_in = (uInt)sourceLen;
59#ifdef MAXSEG_64K
60 /* Check for source > 64K on 16-bit machine: */
4327ac3f 61 if ((size_t)stream.avail_in != sourceLen)
3b3ba48f 62 return Z_BUF_ERROR;
63#endif
64 stream.next_out = dest;
65 stream.avail_out = (uInt)*destLen;
66
4327ac3f 67 if ((size_t)stream.avail_out != *destLen)
3b3ba48f 68 return Z_BUF_ERROR;
69
70 err = zlib_deflateInit(&stream, level);
71 if (err != Z_OK)
72 return err;
73
74 err = zlib_deflate(&stream, Z_FINISH);
75 if (err != Z_STREAM_END) {
76 zlib_deflateEnd(&stream);
77 return err == Z_OK ? Z_BUF_ERROR : err;
78 }
79 *destLen = stream.total_out;
80
81 err = zlib_deflateEnd(&stream);
82 return err;
83} /* z_compress_level() */
84
85/* ===========================================================================
86 * Decompresses the source buffer into the destination buffer. sourceLen is
87 * the byte length of the source buffer. Upon entry, destLen is the total
88 * size of the destination buffer, which must be large enough to hold the
89 * entire uncompressed data. (The size of the uncompressed data must have
90 * been saved previously by the compressor and transmitted to the decompressor
91 * by some mechanism outside the scope of this compression library.)
92 * Upon exit, destLen is the actual size of the compressed buffer.
93 * This function can be used to decompress a whole file at once if the
94 * input file is mmap'ed.
95 *
96 * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
97 * enough memory, Z_BUF_ERROR if there was not enough room in the output
98 * buffer, or Z_DATA_ERROR if the input data was corrupted.
99 */
100static __inline__ int
4327ac3f 101z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
3b3ba48f 102{
103 z_stream stream;
104 int err;
105
106 stream.next_in = (Byte *)source;
107 stream.avail_in = (uInt)sourceLen;
108 /* Check for source > 64K on 16-bit machine: */
4327ac3f 109 if ((size_t)stream.avail_in != sourceLen)
3b3ba48f 110 return Z_BUF_ERROR;
111
112 stream.next_out = dest;
113 stream.avail_out = (uInt)*destLen;
114
4327ac3f 115 if ((size_t)stream.avail_out != *destLen)
3b3ba48f 116 return Z_BUF_ERROR;
117
118 err = zlib_inflateInit(&stream);
119 if (err != Z_OK)
120 return err;
121
122 err = zlib_inflate(&stream, Z_FINISH);
123 if (err != Z_STREAM_END) {
124 zlib_inflateEnd(&stream);
125
126 if (err == Z_NEED_DICT ||
127 (err == Z_BUF_ERROR && stream.avail_in == 0))
128 return Z_DATA_ERROR;
129
130 return err;
131 }
132 *destLen = stream.total_out;
133
134 err = zlib_inflateEnd(&stream);
135 return err;
136} /* z_uncompress() */
137
138#endif /* SPL_ZMOD_H */