lzf.h revision 219354
1151497Sru/*
2104862Sru * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
3104862Sru *
4104862Sru * Redistribution and use in source and binary forms, with or without modifica-
5151497Sru * tion, are permitted provided that the following conditions are met:
6104862Sru *
7104862Sru *   1.  Redistributions of source code must retain the above copyright notice,
8104862Sru *       this list of conditions and the following disclaimer.
9104862Sru *
10104862Sru *   2.  Redistributions in binary form must reproduce the above copyright
11151497Sru *       notice, this list of conditions and the following disclaimer in the
12151497Sru *       documentation and/or other materials provided with the distribution.
13151497Sru *
14151497Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15151497Sru * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16151497Sru * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
17151497Sru * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18151497Sru * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19151497Sru * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20151497Sru * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21151497Sru * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22151497Sru * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23151497Sru * OF THE POSSIBILITY OF SUCH DAMAGE.
24151497Sru *
25151497Sru * Alternatively, the contents of this file may be used under the terms of
26151497Sru * the GNU General Public License ("GPL") version 2 or any later version,
27151497Sru * in which case the provisions of the GPL are applicable instead of
28151497Sru * the above. If you wish to allow the use of your version of this file
29151497Sru * only under the terms of the GPL and not to allow others to use your
30104862Sru * version of this file under the BSD license, indicate your decision
31151497Sru * by deleting the provisions above and replace them with the notice
32151497Sru * and other provisions required by the GPL. If you do not delete the
33151497Sru * provisions above, a recipient may use your version of this file under
34151497Sru * either the BSD or the GPL.
35151497Sru */
36151497Sru
37151497Sru#ifndef LZF_H
38151497Sru#define LZF_H
39151497Sru
40151497Sru/***********************************************************************
41151497Sru**
42151497Sru**	lzf -- an extremely fast/free compression/decompression-method
43151497Sru**	http://liblzf.plan9.de/
44151497Sru**
45151497Sru**	This algorithm is believed to be patent-free.
46151497Sru**
47151497Sru***********************************************************************/
48151497Sru
49151497Sru#define LZF_VERSION 0x0105 /* 1.5, API version */
50151497Sru
51151497Sru/*
52151497Sru * Compress in_len bytes stored at the memory block starting at
53151497Sru * in_data and write the result to out_data, up to a maximum length
54151497Sru * of out_len bytes.
55151497Sru *
56151497Sru * If the output buffer is not large enough or any error occurs return 0,
57151497Sru * otherwise return the number of bytes used, which might be considerably
58151497Sru * more than in_len (but less than 104% of the original size), so it
59151497Sru * makes sense to always use out_len == in_len - 1), to ensure _some_
60151497Sru * compression, and store the data uncompressed otherwise (with a flag, of
61151497Sru * course.
62151497Sru *
63151497Sru * lzf_compress might use different algorithms on different systems and
64151497Sru * even different runs, thus might result in different compressed strings
65151497Sru * depending on the phase of the moon or similar factors. However, all
66151497Sru * these strings are architecture-independent and will result in the
67151497Sru * original data when decompressed using lzf_decompress.
68151497Sru *
69151497Sru * The buffers must not be overlapping.
70151497Sru *
71151497Sru * If the option LZF_STATE_ARG is enabled, an extra argument must be
72151497Sru * supplied which is not reflected in this header file. Refer to lzfP.h
73104862Sru * and lzf_c.c.
74104862Sru *
75104862Sru */
76104862Sruunsigned int
77104862Srulzf_compress (const void *const in_data,  unsigned int in_len,
78104862Sru              void             *out_data, unsigned int out_len);
79104862Sru
80104862Sru/*
81104862Sru * Decompress data compressed with some version of the lzf_compress
82104862Sru * function and stored at location in_data and length in_len. The result
83104862Sru * will be stored at out_data up to a maximum of out_len characters.
84104862Sru *
85151497Sru * If the output buffer is not large enough to hold the decompressed
86104862Sru * data, a 0 is returned and errno is set to E2BIG. Otherwise the number
87104862Sru * of decompressed bytes (i.e. the original length of the data) is
88104862Sru * returned.
89104862Sru *
90104862Sru * If an error in the compressed data is detected, a zero is returned and
91104862Sru * errno is set to EINVAL.
92151497Sru *
93104862Sru * This function is very fast, about as fast as a copying loop.
94104862Sru */
95104862Sruunsigned int
96104862Srulzf_decompress (const void *const in_data,  unsigned int in_len,
97104862Sru                void             *out_data, unsigned int out_len);
98104862Sru
99104862Sru/*
100151497Sru * Size of hashtable is (1 << HLOG) * sizeof (char *)
101104862Sru * decompression is independent of the hash table size
102104862Sru * the difference between 15 and 14 is very small
103151497Sru * for small blocks (and 14 is usually a bit faster).
104104862Sru * For a low-memory/faster configuration, use HLOG == 13;
105104862Sru * For best compression, use 15 or 16 (or more, up to 23).
106151497Sru */
107104862Sru#ifndef HLOG
108104862Sru# define HLOG 16
109104862Sru#endif
110104862Sru
111151497Sru/*
112151497Sru * Sacrifice very little compression quality in favour of compression speed.
113104862Sru * This gives almost the same compression as the default code, and is
114104862Sru * (very roughly) 15% faster. This is the preferred mode of operation.
115104862Sru */
116151497Sru#ifndef VERY_FAST
117104862Sru# define VERY_FAST 1
118104862Sru#endif
119104862Sru
120104862Sru/*
121151497Sru * Sacrifice some more compression quality in favour of compression speed.
122104862Sru * (roughly 1-2% worse compression for large blocks and
123104862Sru * 9-10% for small, redundant, blocks and >>20% better speed in both cases)
124104862Sru * In short: when in need for speed, enable this for binary data,
125104862Sru * possibly disable this for text data.
126151497Sru */
127104862Sru#ifndef ULTRA_FAST
128104862Sru# define ULTRA_FAST 0
129104862Sru#endif
130104862Sru
131151497Sru/*
132104862Sru * Unconditionally aligning does not cost very much, so do it if unsure
133104862Sru */
134104862Sru#ifndef STRICT_ALIGN
135104862Sru# define STRICT_ALIGN !(defined(__i386) || defined (__amd64))
136151497Sru#endif
137104862Sru
138104862Sru/*
139104862Sru * You may choose to pre-set the hash table (might be faster on some
140104862Sru * modern cpus and large (>>64k) blocks, and also makes compression
141104862Sru * deterministic/repeatable when the configuration otherwise is the same).
142104862Sru */
143104862Sru#ifndef INIT_HTAB
144104862Sru# define INIT_HTAB 1
145104862Sru#endif
146104862Sru
147104862Sru/*
148104862Sru * Avoid assigning values to errno variable? for some embedding purposes
149151497Sru * (linux kernel for example), this is neccessary. NOTE: this breaks
150104862Sru * the documentation in lzf.h.
151104862Sru */
152104862Sru#ifndef AVOID_ERRNO
153104862Sru# define AVOID_ERRNO 0
154151497Sru#endif
155104862Sru
156104862Sru/*
157104862Sru * Wether to pass the LZF_STATE variable as argument, or allocate it
158104862Sru * on the stack. For small-stack environments, define this to 1.
159104862Sru * NOTE: this breaks the prototype in lzf.h.
160151497Sru */
161104862Sru#ifndef LZF_STATE_ARG
162104862Sru# define LZF_STATE_ARG 0
163104862Sru#endif
164151497Sru
165104862Sru/*
166104862Sru * Wether to add extra checks for input validity in lzf_decompress
167104862Sru * and return EINVAL if the input stream has been corrupted. This
168151497Sru * only shields against overflowing the input buffer and will not
169104862Sru * detect most corrupted streams.
170104862Sru * This check is not normally noticable on modern hardware
171104862Sru * (<1% slowdown), but might slow down older cpus considerably.
172104862Sru */
173104862Sru#ifndef CHECK_INPUT
174104862Sru# define CHECK_INPUT 1
175104862Sru#endif
176151497Sru
177151497Sru/*****************************************************************************/
178151497Sru/* nothing should be changed below */
179151497Sru
180151497Srutypedef unsigned char u8;
181151497Sru
182104862Srutypedef const u8 *LZF_STATE[1 << (HLOG)];
183151497Sru
184151497Sru#if !STRICT_ALIGN
185104862Sru/* for unaligned accesses we need a 16 bit datatype. */
186104862Sru# include <limits.h>
187104862Sru# if USHRT_MAX == 65535
188104862Sru    typedef unsigned short u16;
189104862Sru# elif UINT_MAX == 65535
190151497Sru    typedef unsigned int u16;
191151497Sru# else
192104862Sru#  undef STRICT_ALIGN
193151497Sru#  define STRICT_ALIGN 1
194104862Sru# endif
195104862Sru#endif
196104862Sru
197104862Sru#if ULTRA_FAST
198104862Sru# if defined(VERY_FAST)
199104862Sru#  undef VERY_FAST
200104862Sru# endif
201151497Sru#endif
202151497Sru
203151497Sru#if INIT_HTAB
204151497Sru# ifdef __cplusplus
205151497Sru#  include <cstring>
206151497Sru# else
207151497Sru#  include <string.h>
208151497Sru# endif
209151497Sru#endif
210151497Sru
211151497Sru#endif
212104862Sru