xref: /freebsd/sbin/hastd/lzf.h (revision 8cd3d45ad9324b8698d11125105078595b1b7b21)
1*8cd3d45aSPawel Jakub Dawidek /*
2*8cd3d45aSPawel Jakub Dawidek  * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
3*8cd3d45aSPawel Jakub Dawidek  *
4*8cd3d45aSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without modifica-
5*8cd3d45aSPawel Jakub Dawidek  * tion, are permitted provided that the following conditions are met:
6*8cd3d45aSPawel Jakub Dawidek  *
7*8cd3d45aSPawel Jakub Dawidek  *   1.  Redistributions of source code must retain the above copyright notice,
8*8cd3d45aSPawel Jakub Dawidek  *       this list of conditions and the following disclaimer.
9*8cd3d45aSPawel Jakub Dawidek  *
10*8cd3d45aSPawel Jakub Dawidek  *   2.  Redistributions in binary form must reproduce the above copyright
11*8cd3d45aSPawel Jakub Dawidek  *       notice, this list of conditions and the following disclaimer in the
12*8cd3d45aSPawel Jakub Dawidek  *       documentation and/or other materials provided with the distribution.
13*8cd3d45aSPawel Jakub Dawidek  *
14*8cd3d45aSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15*8cd3d45aSPawel Jakub Dawidek  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16*8cd3d45aSPawel Jakub Dawidek  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
17*8cd3d45aSPawel Jakub Dawidek  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18*8cd3d45aSPawel Jakub Dawidek  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19*8cd3d45aSPawel Jakub Dawidek  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20*8cd3d45aSPawel Jakub Dawidek  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21*8cd3d45aSPawel Jakub Dawidek  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22*8cd3d45aSPawel Jakub Dawidek  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23*8cd3d45aSPawel Jakub Dawidek  * OF THE POSSIBILITY OF SUCH DAMAGE.
24*8cd3d45aSPawel Jakub Dawidek  *
25*8cd3d45aSPawel Jakub Dawidek  * Alternatively, the contents of this file may be used under the terms of
26*8cd3d45aSPawel Jakub Dawidek  * the GNU General Public License ("GPL") version 2 or any later version,
27*8cd3d45aSPawel Jakub Dawidek  * in which case the provisions of the GPL are applicable instead of
28*8cd3d45aSPawel Jakub Dawidek  * the above. If you wish to allow the use of your version of this file
29*8cd3d45aSPawel Jakub Dawidek  * only under the terms of the GPL and not to allow others to use your
30*8cd3d45aSPawel Jakub Dawidek  * version of this file under the BSD license, indicate your decision
31*8cd3d45aSPawel Jakub Dawidek  * by deleting the provisions above and replace them with the notice
32*8cd3d45aSPawel Jakub Dawidek  * and other provisions required by the GPL. If you do not delete the
33*8cd3d45aSPawel Jakub Dawidek  * provisions above, a recipient may use your version of this file under
34*8cd3d45aSPawel Jakub Dawidek  * either the BSD or the GPL.
35*8cd3d45aSPawel Jakub Dawidek  */
36*8cd3d45aSPawel Jakub Dawidek 
37*8cd3d45aSPawel Jakub Dawidek #ifndef LZF_H
38*8cd3d45aSPawel Jakub Dawidek #define LZF_H
39*8cd3d45aSPawel Jakub Dawidek 
40*8cd3d45aSPawel Jakub Dawidek /***********************************************************************
41*8cd3d45aSPawel Jakub Dawidek **
42*8cd3d45aSPawel Jakub Dawidek **	lzf -- an extremely fast/free compression/decompression-method
43*8cd3d45aSPawel Jakub Dawidek **	http://liblzf.plan9.de/
44*8cd3d45aSPawel Jakub Dawidek **
45*8cd3d45aSPawel Jakub Dawidek **	This algorithm is believed to be patent-free.
46*8cd3d45aSPawel Jakub Dawidek **
47*8cd3d45aSPawel Jakub Dawidek ***********************************************************************/
48*8cd3d45aSPawel Jakub Dawidek 
49*8cd3d45aSPawel Jakub Dawidek #define LZF_VERSION 0x0105 /* 1.5, API version */
50*8cd3d45aSPawel Jakub Dawidek 
51*8cd3d45aSPawel Jakub Dawidek /*
52*8cd3d45aSPawel Jakub Dawidek  * Compress in_len bytes stored at the memory block starting at
53*8cd3d45aSPawel Jakub Dawidek  * in_data and write the result to out_data, up to a maximum length
54*8cd3d45aSPawel Jakub Dawidek  * of out_len bytes.
55*8cd3d45aSPawel Jakub Dawidek  *
56*8cd3d45aSPawel Jakub Dawidek  * If the output buffer is not large enough or any error occurs return 0,
57*8cd3d45aSPawel Jakub Dawidek  * otherwise return the number of bytes used, which might be considerably
58*8cd3d45aSPawel Jakub Dawidek  * more than in_len (but less than 104% of the original size), so it
59*8cd3d45aSPawel Jakub Dawidek  * makes sense to always use out_len == in_len - 1), to ensure _some_
60*8cd3d45aSPawel Jakub Dawidek  * compression, and store the data uncompressed otherwise (with a flag, of
61*8cd3d45aSPawel Jakub Dawidek  * course.
62*8cd3d45aSPawel Jakub Dawidek  *
63*8cd3d45aSPawel Jakub Dawidek  * lzf_compress might use different algorithms on different systems and
64*8cd3d45aSPawel Jakub Dawidek  * even different runs, thus might result in different compressed strings
65*8cd3d45aSPawel Jakub Dawidek  * depending on the phase of the moon or similar factors. However, all
66*8cd3d45aSPawel Jakub Dawidek  * these strings are architecture-independent and will result in the
67*8cd3d45aSPawel Jakub Dawidek  * original data when decompressed using lzf_decompress.
68*8cd3d45aSPawel Jakub Dawidek  *
69*8cd3d45aSPawel Jakub Dawidek  * The buffers must not be overlapping.
70*8cd3d45aSPawel Jakub Dawidek  *
71*8cd3d45aSPawel Jakub Dawidek  * If the option LZF_STATE_ARG is enabled, an extra argument must be
72*8cd3d45aSPawel Jakub Dawidek  * supplied which is not reflected in this header file. Refer to lzfP.h
73*8cd3d45aSPawel Jakub Dawidek  * and lzf_c.c.
74*8cd3d45aSPawel Jakub Dawidek  *
75*8cd3d45aSPawel Jakub Dawidek  */
76*8cd3d45aSPawel Jakub Dawidek unsigned int
77*8cd3d45aSPawel Jakub Dawidek lzf_compress (const void *const in_data,  unsigned int in_len,
78*8cd3d45aSPawel Jakub Dawidek               void             *out_data, unsigned int out_len);
79*8cd3d45aSPawel Jakub Dawidek 
80*8cd3d45aSPawel Jakub Dawidek /*
81*8cd3d45aSPawel Jakub Dawidek  * Decompress data compressed with some version of the lzf_compress
82*8cd3d45aSPawel Jakub Dawidek  * function and stored at location in_data and length in_len. The result
83*8cd3d45aSPawel Jakub Dawidek  * will be stored at out_data up to a maximum of out_len characters.
84*8cd3d45aSPawel Jakub Dawidek  *
85*8cd3d45aSPawel Jakub Dawidek  * If the output buffer is not large enough to hold the decompressed
86*8cd3d45aSPawel Jakub Dawidek  * data, a 0 is returned and errno is set to E2BIG. Otherwise the number
87*8cd3d45aSPawel Jakub Dawidek  * of decompressed bytes (i.e. the original length of the data) is
88*8cd3d45aSPawel Jakub Dawidek  * returned.
89*8cd3d45aSPawel Jakub Dawidek  *
90*8cd3d45aSPawel Jakub Dawidek  * If an error in the compressed data is detected, a zero is returned and
91*8cd3d45aSPawel Jakub Dawidek  * errno is set to EINVAL.
92*8cd3d45aSPawel Jakub Dawidek  *
93*8cd3d45aSPawel Jakub Dawidek  * This function is very fast, about as fast as a copying loop.
94*8cd3d45aSPawel Jakub Dawidek  */
95*8cd3d45aSPawel Jakub Dawidek unsigned int
96*8cd3d45aSPawel Jakub Dawidek lzf_decompress (const void *const in_data,  unsigned int in_len,
97*8cd3d45aSPawel Jakub Dawidek                 void             *out_data, unsigned int out_len);
98*8cd3d45aSPawel Jakub Dawidek 
99*8cd3d45aSPawel Jakub Dawidek /*
100*8cd3d45aSPawel Jakub Dawidek  * Size of hashtable is (1 << HLOG) * sizeof (char *)
101*8cd3d45aSPawel Jakub Dawidek  * decompression is independent of the hash table size
102*8cd3d45aSPawel Jakub Dawidek  * the difference between 15 and 14 is very small
103*8cd3d45aSPawel Jakub Dawidek  * for small blocks (and 14 is usually a bit faster).
104*8cd3d45aSPawel Jakub Dawidek  * For a low-memory/faster configuration, use HLOG == 13;
105*8cd3d45aSPawel Jakub Dawidek  * For best compression, use 15 or 16 (or more, up to 23).
106*8cd3d45aSPawel Jakub Dawidek  */
107*8cd3d45aSPawel Jakub Dawidek #ifndef HLOG
108*8cd3d45aSPawel Jakub Dawidek # define HLOG 16
109*8cd3d45aSPawel Jakub Dawidek #endif
110*8cd3d45aSPawel Jakub Dawidek 
111*8cd3d45aSPawel Jakub Dawidek /*
112*8cd3d45aSPawel Jakub Dawidek  * Sacrifice very little compression quality in favour of compression speed.
113*8cd3d45aSPawel Jakub Dawidek  * This gives almost the same compression as the default code, and is
114*8cd3d45aSPawel Jakub Dawidek  * (very roughly) 15% faster. This is the preferred mode of operation.
115*8cd3d45aSPawel Jakub Dawidek  */
116*8cd3d45aSPawel Jakub Dawidek #ifndef VERY_FAST
117*8cd3d45aSPawel Jakub Dawidek # define VERY_FAST 1
118*8cd3d45aSPawel Jakub Dawidek #endif
119*8cd3d45aSPawel Jakub Dawidek 
120*8cd3d45aSPawel Jakub Dawidek /*
121*8cd3d45aSPawel Jakub Dawidek  * Sacrifice some more compression quality in favour of compression speed.
122*8cd3d45aSPawel Jakub Dawidek  * (roughly 1-2% worse compression for large blocks and
123*8cd3d45aSPawel Jakub Dawidek  * 9-10% for small, redundant, blocks and >>20% better speed in both cases)
124*8cd3d45aSPawel Jakub Dawidek  * In short: when in need for speed, enable this for binary data,
125*8cd3d45aSPawel Jakub Dawidek  * possibly disable this for text data.
126*8cd3d45aSPawel Jakub Dawidek  */
127*8cd3d45aSPawel Jakub Dawidek #ifndef ULTRA_FAST
128*8cd3d45aSPawel Jakub Dawidek # define ULTRA_FAST 0
129*8cd3d45aSPawel Jakub Dawidek #endif
130*8cd3d45aSPawel Jakub Dawidek 
131*8cd3d45aSPawel Jakub Dawidek /*
132*8cd3d45aSPawel Jakub Dawidek  * Unconditionally aligning does not cost very much, so do it if unsure
133*8cd3d45aSPawel Jakub Dawidek  */
134*8cd3d45aSPawel Jakub Dawidek #ifndef STRICT_ALIGN
135*8cd3d45aSPawel Jakub Dawidek # define STRICT_ALIGN !(defined(__i386) || defined (__amd64))
136*8cd3d45aSPawel Jakub Dawidek #endif
137*8cd3d45aSPawel Jakub Dawidek 
138*8cd3d45aSPawel Jakub Dawidek /*
139*8cd3d45aSPawel Jakub Dawidek  * You may choose to pre-set the hash table (might be faster on some
140*8cd3d45aSPawel Jakub Dawidek  * modern cpus and large (>>64k) blocks, and also makes compression
141*8cd3d45aSPawel Jakub Dawidek  * deterministic/repeatable when the configuration otherwise is the same).
142*8cd3d45aSPawel Jakub Dawidek  */
143*8cd3d45aSPawel Jakub Dawidek #ifndef INIT_HTAB
144*8cd3d45aSPawel Jakub Dawidek # define INIT_HTAB 1
145*8cd3d45aSPawel Jakub Dawidek #endif
146*8cd3d45aSPawel Jakub Dawidek 
147*8cd3d45aSPawel Jakub Dawidek /*
148*8cd3d45aSPawel Jakub Dawidek  * Avoid assigning values to errno variable? for some embedding purposes
149*8cd3d45aSPawel Jakub Dawidek  * (linux kernel for example), this is neccessary. NOTE: this breaks
150*8cd3d45aSPawel Jakub Dawidek  * the documentation in lzf.h.
151*8cd3d45aSPawel Jakub Dawidek  */
152*8cd3d45aSPawel Jakub Dawidek #ifndef AVOID_ERRNO
153*8cd3d45aSPawel Jakub Dawidek # define AVOID_ERRNO 0
154*8cd3d45aSPawel Jakub Dawidek #endif
155*8cd3d45aSPawel Jakub Dawidek 
156*8cd3d45aSPawel Jakub Dawidek /*
157*8cd3d45aSPawel Jakub Dawidek  * Wether to pass the LZF_STATE variable as argument, or allocate it
158*8cd3d45aSPawel Jakub Dawidek  * on the stack. For small-stack environments, define this to 1.
159*8cd3d45aSPawel Jakub Dawidek  * NOTE: this breaks the prototype in lzf.h.
160*8cd3d45aSPawel Jakub Dawidek  */
161*8cd3d45aSPawel Jakub Dawidek #ifndef LZF_STATE_ARG
162*8cd3d45aSPawel Jakub Dawidek # define LZF_STATE_ARG 0
163*8cd3d45aSPawel Jakub Dawidek #endif
164*8cd3d45aSPawel Jakub Dawidek 
165*8cd3d45aSPawel Jakub Dawidek /*
166*8cd3d45aSPawel Jakub Dawidek  * Wether to add extra checks for input validity in lzf_decompress
167*8cd3d45aSPawel Jakub Dawidek  * and return EINVAL if the input stream has been corrupted. This
168*8cd3d45aSPawel Jakub Dawidek  * only shields against overflowing the input buffer and will not
169*8cd3d45aSPawel Jakub Dawidek  * detect most corrupted streams.
170*8cd3d45aSPawel Jakub Dawidek  * This check is not normally noticable on modern hardware
171*8cd3d45aSPawel Jakub Dawidek  * (<1% slowdown), but might slow down older cpus considerably.
172*8cd3d45aSPawel Jakub Dawidek  */
173*8cd3d45aSPawel Jakub Dawidek #ifndef CHECK_INPUT
174*8cd3d45aSPawel Jakub Dawidek # define CHECK_INPUT 1
175*8cd3d45aSPawel Jakub Dawidek #endif
176*8cd3d45aSPawel Jakub Dawidek 
177*8cd3d45aSPawel Jakub Dawidek /*****************************************************************************/
178*8cd3d45aSPawel Jakub Dawidek /* nothing should be changed below */
179*8cd3d45aSPawel Jakub Dawidek 
180*8cd3d45aSPawel Jakub Dawidek typedef unsigned char u8;
181*8cd3d45aSPawel Jakub Dawidek 
182*8cd3d45aSPawel Jakub Dawidek typedef const u8 *LZF_STATE[1 << (HLOG)];
183*8cd3d45aSPawel Jakub Dawidek 
184*8cd3d45aSPawel Jakub Dawidek #if !STRICT_ALIGN
185*8cd3d45aSPawel Jakub Dawidek /* for unaligned accesses we need a 16 bit datatype. */
186*8cd3d45aSPawel Jakub Dawidek # include <limits.h>
187*8cd3d45aSPawel Jakub Dawidek # if USHRT_MAX == 65535
188*8cd3d45aSPawel Jakub Dawidek     typedef unsigned short u16;
189*8cd3d45aSPawel Jakub Dawidek # elif UINT_MAX == 65535
190*8cd3d45aSPawel Jakub Dawidek     typedef unsigned int u16;
191*8cd3d45aSPawel Jakub Dawidek # else
192*8cd3d45aSPawel Jakub Dawidek #  undef STRICT_ALIGN
193*8cd3d45aSPawel Jakub Dawidek #  define STRICT_ALIGN 1
194*8cd3d45aSPawel Jakub Dawidek # endif
195*8cd3d45aSPawel Jakub Dawidek #endif
196*8cd3d45aSPawel Jakub Dawidek 
197*8cd3d45aSPawel Jakub Dawidek #if ULTRA_FAST
198*8cd3d45aSPawel Jakub Dawidek # if defined(VERY_FAST)
199*8cd3d45aSPawel Jakub Dawidek #  undef VERY_FAST
200*8cd3d45aSPawel Jakub Dawidek # endif
201*8cd3d45aSPawel Jakub Dawidek #endif
202*8cd3d45aSPawel Jakub Dawidek 
203*8cd3d45aSPawel Jakub Dawidek #if INIT_HTAB
204*8cd3d45aSPawel Jakub Dawidek # ifdef __cplusplus
205*8cd3d45aSPawel Jakub Dawidek #  include <cstring>
206*8cd3d45aSPawel Jakub Dawidek # else
207*8cd3d45aSPawel Jakub Dawidek #  include <string.h>
208*8cd3d45aSPawel Jakub Dawidek # endif
209*8cd3d45aSPawel Jakub Dawidek #endif
210*8cd3d45aSPawel Jakub Dawidek 
211*8cd3d45aSPawel Jakub Dawidek #endif
212