xref: /freebsd/sbin/hastd/lzf.h (revision 4b85a12f7142f8086e98f2b9b748d304b76a4b58)
18cd3d45aSPawel Jakub Dawidek /*
28cd3d45aSPawel Jakub Dawidek  * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
38cd3d45aSPawel Jakub Dawidek  *
48cd3d45aSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without modifica-
58cd3d45aSPawel Jakub Dawidek  * tion, are permitted provided that the following conditions are met:
68cd3d45aSPawel Jakub Dawidek  *
78cd3d45aSPawel Jakub Dawidek  *   1.  Redistributions of source code must retain the above copyright notice,
88cd3d45aSPawel Jakub Dawidek  *       this list of conditions and the following disclaimer.
98cd3d45aSPawel Jakub Dawidek  *
108cd3d45aSPawel Jakub Dawidek  *   2.  Redistributions in binary form must reproduce the above copyright
118cd3d45aSPawel Jakub Dawidek  *       notice, this list of conditions and the following disclaimer in the
128cd3d45aSPawel Jakub Dawidek  *       documentation and/or other materials provided with the distribution.
138cd3d45aSPawel Jakub Dawidek  *
148cd3d45aSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
158cd3d45aSPawel Jakub Dawidek  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
168cd3d45aSPawel Jakub Dawidek  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
178cd3d45aSPawel Jakub Dawidek  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
188cd3d45aSPawel Jakub Dawidek  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
198cd3d45aSPawel Jakub Dawidek  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
208cd3d45aSPawel Jakub Dawidek  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
218cd3d45aSPawel Jakub Dawidek  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
228cd3d45aSPawel Jakub Dawidek  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
238cd3d45aSPawel Jakub Dawidek  * OF THE POSSIBILITY OF SUCH DAMAGE.
248cd3d45aSPawel Jakub Dawidek  *
258cd3d45aSPawel Jakub Dawidek  * Alternatively, the contents of this file may be used under the terms of
268cd3d45aSPawel Jakub Dawidek  * the GNU General Public License ("GPL") version 2 or any later version,
278cd3d45aSPawel Jakub Dawidek  * in which case the provisions of the GPL are applicable instead of
288cd3d45aSPawel Jakub Dawidek  * the above. If you wish to allow the use of your version of this file
298cd3d45aSPawel Jakub Dawidek  * only under the terms of the GPL and not to allow others to use your
308cd3d45aSPawel Jakub Dawidek  * version of this file under the BSD license, indicate your decision
318cd3d45aSPawel Jakub Dawidek  * by deleting the provisions above and replace them with the notice
328cd3d45aSPawel Jakub Dawidek  * and other provisions required by the GPL. If you do not delete the
338cd3d45aSPawel Jakub Dawidek  * provisions above, a recipient may use your version of this file under
348cd3d45aSPawel Jakub Dawidek  * either the BSD or the GPL.
358cd3d45aSPawel Jakub Dawidek  */
368cd3d45aSPawel Jakub Dawidek 
378cd3d45aSPawel Jakub Dawidek #ifndef LZF_H
388cd3d45aSPawel Jakub Dawidek #define LZF_H
398cd3d45aSPawel Jakub Dawidek 
408cd3d45aSPawel Jakub Dawidek /***********************************************************************
418cd3d45aSPawel Jakub Dawidek **
428cd3d45aSPawel Jakub Dawidek **	lzf -- an extremely fast/free compression/decompression-method
438cd3d45aSPawel Jakub Dawidek **	http://liblzf.plan9.de/
448cd3d45aSPawel Jakub Dawidek **
458cd3d45aSPawel Jakub Dawidek **	This algorithm is believed to be patent-free.
468cd3d45aSPawel Jakub Dawidek **
478cd3d45aSPawel Jakub Dawidek ***********************************************************************/
488cd3d45aSPawel Jakub Dawidek 
498cd3d45aSPawel Jakub Dawidek #define LZF_VERSION 0x0105 /* 1.5, API version */
508cd3d45aSPawel Jakub Dawidek 
518cd3d45aSPawel Jakub Dawidek /*
528cd3d45aSPawel Jakub Dawidek  * Compress in_len bytes stored at the memory block starting at
538cd3d45aSPawel Jakub Dawidek  * in_data and write the result to out_data, up to a maximum length
548cd3d45aSPawel Jakub Dawidek  * of out_len bytes.
558cd3d45aSPawel Jakub Dawidek  *
568cd3d45aSPawel Jakub Dawidek  * If the output buffer is not large enough or any error occurs return 0,
578cd3d45aSPawel Jakub Dawidek  * otherwise return the number of bytes used, which might be considerably
588cd3d45aSPawel Jakub Dawidek  * more than in_len (but less than 104% of the original size), so it
598cd3d45aSPawel Jakub Dawidek  * makes sense to always use out_len == in_len - 1), to ensure _some_
608cd3d45aSPawel Jakub Dawidek  * compression, and store the data uncompressed otherwise (with a flag, of
618cd3d45aSPawel Jakub Dawidek  * course.
628cd3d45aSPawel Jakub Dawidek  *
638cd3d45aSPawel Jakub Dawidek  * lzf_compress might use different algorithms on different systems and
648cd3d45aSPawel Jakub Dawidek  * even different runs, thus might result in different compressed strings
658cd3d45aSPawel Jakub Dawidek  * depending on the phase of the moon or similar factors. However, all
668cd3d45aSPawel Jakub Dawidek  * these strings are architecture-independent and will result in the
678cd3d45aSPawel Jakub Dawidek  * original data when decompressed using lzf_decompress.
688cd3d45aSPawel Jakub Dawidek  *
698cd3d45aSPawel Jakub Dawidek  * The buffers must not be overlapping.
708cd3d45aSPawel Jakub Dawidek  *
718cd3d45aSPawel Jakub Dawidek  * If the option LZF_STATE_ARG is enabled, an extra argument must be
728cd3d45aSPawel Jakub Dawidek  * supplied which is not reflected in this header file. Refer to lzfP.h
738cd3d45aSPawel Jakub Dawidek  * and lzf_c.c.
748cd3d45aSPawel Jakub Dawidek  *
758cd3d45aSPawel Jakub Dawidek  */
768cd3d45aSPawel Jakub Dawidek unsigned int
778cd3d45aSPawel Jakub Dawidek lzf_compress (const void *const in_data,  unsigned int in_len,
788cd3d45aSPawel Jakub Dawidek               void             *out_data, unsigned int out_len);
798cd3d45aSPawel Jakub Dawidek 
808cd3d45aSPawel Jakub Dawidek /*
818cd3d45aSPawel Jakub Dawidek  * Decompress data compressed with some version of the lzf_compress
828cd3d45aSPawel Jakub Dawidek  * function and stored at location in_data and length in_len. The result
838cd3d45aSPawel Jakub Dawidek  * will be stored at out_data up to a maximum of out_len characters.
848cd3d45aSPawel Jakub Dawidek  *
858cd3d45aSPawel Jakub Dawidek  * If the output buffer is not large enough to hold the decompressed
868cd3d45aSPawel Jakub Dawidek  * data, a 0 is returned and errno is set to E2BIG. Otherwise the number
878cd3d45aSPawel Jakub Dawidek  * of decompressed bytes (i.e. the original length of the data) is
888cd3d45aSPawel Jakub Dawidek  * returned.
898cd3d45aSPawel Jakub Dawidek  *
908cd3d45aSPawel Jakub Dawidek  * If an error in the compressed data is detected, a zero is returned and
918cd3d45aSPawel Jakub Dawidek  * errno is set to EINVAL.
928cd3d45aSPawel Jakub Dawidek  *
938cd3d45aSPawel Jakub Dawidek  * This function is very fast, about as fast as a copying loop.
948cd3d45aSPawel Jakub Dawidek  */
958cd3d45aSPawel Jakub Dawidek unsigned int
968cd3d45aSPawel Jakub Dawidek lzf_decompress (const void *const in_data,  unsigned int in_len,
978cd3d45aSPawel Jakub Dawidek                 void             *out_data, unsigned int out_len);
988cd3d45aSPawel Jakub Dawidek 
998cd3d45aSPawel Jakub Dawidek /*
1008cd3d45aSPawel Jakub Dawidek  * Size of hashtable is (1 << HLOG) * sizeof (char *)
1018cd3d45aSPawel Jakub Dawidek  * decompression is independent of the hash table size
1028cd3d45aSPawel Jakub Dawidek  * the difference between 15 and 14 is very small
1038cd3d45aSPawel Jakub Dawidek  * for small blocks (and 14 is usually a bit faster).
1048cd3d45aSPawel Jakub Dawidek  * For a low-memory/faster configuration, use HLOG == 13;
1058cd3d45aSPawel Jakub Dawidek  * For best compression, use 15 or 16 (or more, up to 23).
1068cd3d45aSPawel Jakub Dawidek  */
1078cd3d45aSPawel Jakub Dawidek #ifndef HLOG
1088cd3d45aSPawel Jakub Dawidek # define HLOG 16
1098cd3d45aSPawel Jakub Dawidek #endif
1108cd3d45aSPawel Jakub Dawidek 
1118cd3d45aSPawel Jakub Dawidek /*
1128cd3d45aSPawel Jakub Dawidek  * Sacrifice very little compression quality in favour of compression speed.
1138cd3d45aSPawel Jakub Dawidek  * This gives almost the same compression as the default code, and is
1148cd3d45aSPawel Jakub Dawidek  * (very roughly) 15% faster. This is the preferred mode of operation.
1158cd3d45aSPawel Jakub Dawidek  */
1168cd3d45aSPawel Jakub Dawidek #ifndef VERY_FAST
1178cd3d45aSPawel Jakub Dawidek # define VERY_FAST 1
1188cd3d45aSPawel Jakub Dawidek #endif
1198cd3d45aSPawel Jakub Dawidek 
1208cd3d45aSPawel Jakub Dawidek /*
1218cd3d45aSPawel Jakub Dawidek  * Sacrifice some more compression quality in favour of compression speed.
1228cd3d45aSPawel Jakub Dawidek  * (roughly 1-2% worse compression for large blocks and
1238cd3d45aSPawel Jakub Dawidek  * 9-10% for small, redundant, blocks and >>20% better speed in both cases)
1248cd3d45aSPawel Jakub Dawidek  * In short: when in need for speed, enable this for binary data,
1258cd3d45aSPawel Jakub Dawidek  * possibly disable this for text data.
1268cd3d45aSPawel Jakub Dawidek  */
1278cd3d45aSPawel Jakub Dawidek #ifndef ULTRA_FAST
1288cd3d45aSPawel Jakub Dawidek # define ULTRA_FAST 0
1298cd3d45aSPawel Jakub Dawidek #endif
1308cd3d45aSPawel Jakub Dawidek 
1318cd3d45aSPawel Jakub Dawidek /*
1328cd3d45aSPawel Jakub Dawidek  * Unconditionally aligning does not cost very much, so do it if unsure
1338cd3d45aSPawel Jakub Dawidek  */
1348cd3d45aSPawel Jakub Dawidek #ifndef STRICT_ALIGN
1358cd3d45aSPawel Jakub Dawidek # define STRICT_ALIGN !(defined(__i386) || defined (__amd64))
1368cd3d45aSPawel Jakub Dawidek #endif
1378cd3d45aSPawel Jakub Dawidek 
1388cd3d45aSPawel Jakub Dawidek /*
1398cd3d45aSPawel Jakub Dawidek  * You may choose to pre-set the hash table (might be faster on some
1408cd3d45aSPawel Jakub Dawidek  * modern cpus and large (>>64k) blocks, and also makes compression
1418cd3d45aSPawel Jakub Dawidek  * deterministic/repeatable when the configuration otherwise is the same).
1428cd3d45aSPawel Jakub Dawidek  */
1438cd3d45aSPawel Jakub Dawidek #ifndef INIT_HTAB
1448cd3d45aSPawel Jakub Dawidek # define INIT_HTAB 1
1458cd3d45aSPawel Jakub Dawidek #endif
1468cd3d45aSPawel Jakub Dawidek 
1478cd3d45aSPawel Jakub Dawidek /*
1488cd3d45aSPawel Jakub Dawidek  * Avoid assigning values to errno variable? for some embedding purposes
149*4b85a12fSUlrich Spörlein  * (linux kernel for example), this is necessary. NOTE: this breaks
1508cd3d45aSPawel Jakub Dawidek  * the documentation in lzf.h.
1518cd3d45aSPawel Jakub Dawidek  */
1528cd3d45aSPawel Jakub Dawidek #ifndef AVOID_ERRNO
1538cd3d45aSPawel Jakub Dawidek # define AVOID_ERRNO 0
1548cd3d45aSPawel Jakub Dawidek #endif
1558cd3d45aSPawel Jakub Dawidek 
1568cd3d45aSPawel Jakub Dawidek /*
1578cd3d45aSPawel Jakub Dawidek  * Wether to pass the LZF_STATE variable as argument, or allocate it
1588cd3d45aSPawel Jakub Dawidek  * on the stack. For small-stack environments, define this to 1.
1598cd3d45aSPawel Jakub Dawidek  * NOTE: this breaks the prototype in lzf.h.
1608cd3d45aSPawel Jakub Dawidek  */
1618cd3d45aSPawel Jakub Dawidek #ifndef LZF_STATE_ARG
1628cd3d45aSPawel Jakub Dawidek # define LZF_STATE_ARG 0
1638cd3d45aSPawel Jakub Dawidek #endif
1648cd3d45aSPawel Jakub Dawidek 
1658cd3d45aSPawel Jakub Dawidek /*
1668cd3d45aSPawel Jakub Dawidek  * Wether to add extra checks for input validity in lzf_decompress
1678cd3d45aSPawel Jakub Dawidek  * and return EINVAL if the input stream has been corrupted. This
1688cd3d45aSPawel Jakub Dawidek  * only shields against overflowing the input buffer and will not
1698cd3d45aSPawel Jakub Dawidek  * detect most corrupted streams.
170*4b85a12fSUlrich Spörlein  * This check is not normally noticeable on modern hardware
1718cd3d45aSPawel Jakub Dawidek  * (<1% slowdown), but might slow down older cpus considerably.
1728cd3d45aSPawel Jakub Dawidek  */
1738cd3d45aSPawel Jakub Dawidek #ifndef CHECK_INPUT
1748cd3d45aSPawel Jakub Dawidek # define CHECK_INPUT 1
1758cd3d45aSPawel Jakub Dawidek #endif
1768cd3d45aSPawel Jakub Dawidek 
1778cd3d45aSPawel Jakub Dawidek /*****************************************************************************/
1788cd3d45aSPawel Jakub Dawidek /* nothing should be changed below */
1798cd3d45aSPawel Jakub Dawidek 
1808cd3d45aSPawel Jakub Dawidek typedef unsigned char u8;
1818cd3d45aSPawel Jakub Dawidek 
1828cd3d45aSPawel Jakub Dawidek typedef const u8 *LZF_STATE[1 << (HLOG)];
1838cd3d45aSPawel Jakub Dawidek 
1848cd3d45aSPawel Jakub Dawidek #if !STRICT_ALIGN
1858cd3d45aSPawel Jakub Dawidek /* for unaligned accesses we need a 16 bit datatype. */
1868cd3d45aSPawel Jakub Dawidek # include <limits.h>
1878cd3d45aSPawel Jakub Dawidek # if USHRT_MAX == 65535
1888cd3d45aSPawel Jakub Dawidek     typedef unsigned short u16;
1898cd3d45aSPawel Jakub Dawidek # elif UINT_MAX == 65535
1908cd3d45aSPawel Jakub Dawidek     typedef unsigned int u16;
1918cd3d45aSPawel Jakub Dawidek # else
1928cd3d45aSPawel Jakub Dawidek #  undef STRICT_ALIGN
1938cd3d45aSPawel Jakub Dawidek #  define STRICT_ALIGN 1
1948cd3d45aSPawel Jakub Dawidek # endif
1958cd3d45aSPawel Jakub Dawidek #endif
1968cd3d45aSPawel Jakub Dawidek 
1978cd3d45aSPawel Jakub Dawidek #if ULTRA_FAST
1988cd3d45aSPawel Jakub Dawidek # if defined(VERY_FAST)
1998cd3d45aSPawel Jakub Dawidek #  undef VERY_FAST
2008cd3d45aSPawel Jakub Dawidek # endif
2018cd3d45aSPawel Jakub Dawidek #endif
2028cd3d45aSPawel Jakub Dawidek 
2038cd3d45aSPawel Jakub Dawidek #if INIT_HTAB
2048cd3d45aSPawel Jakub Dawidek # ifdef __cplusplus
2058cd3d45aSPawel Jakub Dawidek #  include <cstring>
2068cd3d45aSPawel Jakub Dawidek # else
2078cd3d45aSPawel Jakub Dawidek #  include <string.h>
2088cd3d45aSPawel Jakub Dawidek # endif
2098cd3d45aSPawel Jakub Dawidek #endif
2108cd3d45aSPawel Jakub Dawidek 
2118cd3d45aSPawel Jakub Dawidek #endif
212