1219354Spjd/* 2219354Spjd * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de> 3219354Spjd * 4219354Spjd * Redistribution and use in source and binary forms, with or without modifica- 5219354Spjd * tion, are permitted provided that the following conditions are met: 6219354Spjd * 7219354Spjd * 1. Redistributions of source code must retain the above copyright notice, 8219354Spjd * this list of conditions and the following disclaimer. 9219354Spjd * 10219354Spjd * 2. Redistributions in binary form must reproduce the above copyright 11219354Spjd * notice, this list of conditions and the following disclaimer in the 12219354Spjd * documentation and/or other materials provided with the distribution. 13219354Spjd * 14219354Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15219354Spjd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 16219354Spjd * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17219354Spjd * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 18219354Spjd * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19219354Spjd * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20219354Spjd * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21219354Spjd * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- 22219354Spjd * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 23219354Spjd * OF THE POSSIBILITY OF SUCH DAMAGE. 24219354Spjd * 25219354Spjd * Alternatively, the contents of this file may be used under the terms of 26219354Spjd * the GNU General Public License ("GPL") version 2 or any later version, 27219354Spjd * in which case the provisions of the GPL are applicable instead of 28219354Spjd * the above. If you wish to allow the use of your version of this file 29219354Spjd * only under the terms of the GPL and not to allow others to use your 30219354Spjd * version of this file under the BSD license, indicate your decision 31219354Spjd * by deleting the provisions above and replace them with the notice 32219354Spjd * and other provisions required by the GPL. If you do not delete the 33219354Spjd * provisions above, a recipient may use your version of this file under 34219354Spjd * either the BSD or the GPL. 35219354Spjd */ 36219354Spjd 37219354Spjd#ifndef LZF_H 38219354Spjd#define LZF_H 39219354Spjd 40219354Spjd/*********************************************************************** 41219354Spjd** 42219354Spjd** lzf -- an extremely fast/free compression/decompression-method 43219354Spjd** http://liblzf.plan9.de/ 44219354Spjd** 45219354Spjd** This algorithm is believed to be patent-free. 46219354Spjd** 47219354Spjd***********************************************************************/ 48219354Spjd 49219354Spjd#define LZF_VERSION 0x0105 /* 1.5, API version */ 50219354Spjd 51219354Spjd/* 52219354Spjd * Compress in_len bytes stored at the memory block starting at 53219354Spjd * in_data and write the result to out_data, up to a maximum length 54219354Spjd * of out_len bytes. 55219354Spjd * 56219354Spjd * If the output buffer is not large enough or any error occurs return 0, 57219354Spjd * otherwise return the number of bytes used, which might be considerably 58219354Spjd * more than in_len (but less than 104% of the original size), so it 59219354Spjd * makes sense to always use out_len == in_len - 1), to ensure _some_ 60219354Spjd * compression, and store the data uncompressed otherwise (with a flag, of 61219354Spjd * course. 62219354Spjd * 63219354Spjd * lzf_compress might use different algorithms on different systems and 64219354Spjd * even different runs, thus might result in different compressed strings 65219354Spjd * depending on the phase of the moon or similar factors. However, all 66219354Spjd * these strings are architecture-independent and will result in the 67219354Spjd * original data when decompressed using lzf_decompress. 68219354Spjd * 69219354Spjd * The buffers must not be overlapping. 70219354Spjd * 71219354Spjd * If the option LZF_STATE_ARG is enabled, an extra argument must be 72219354Spjd * supplied which is not reflected in this header file. Refer to lzfP.h 73219354Spjd * and lzf_c.c. 74219354Spjd * 75219354Spjd */ 76219354Spjdunsigned int 77219354Spjdlzf_compress (const void *const in_data, unsigned int in_len, 78219354Spjd void *out_data, unsigned int out_len); 79219354Spjd 80219354Spjd/* 81219354Spjd * Decompress data compressed with some version of the lzf_compress 82219354Spjd * function and stored at location in_data and length in_len. The result 83219354Spjd * will be stored at out_data up to a maximum of out_len characters. 84219354Spjd * 85219354Spjd * If the output buffer is not large enough to hold the decompressed 86219354Spjd * data, a 0 is returned and errno is set to E2BIG. Otherwise the number 87219354Spjd * of decompressed bytes (i.e. the original length of the data) is 88219354Spjd * returned. 89219354Spjd * 90219354Spjd * If an error in the compressed data is detected, a zero is returned and 91219354Spjd * errno is set to EINVAL. 92219354Spjd * 93219354Spjd * This function is very fast, about as fast as a copying loop. 94219354Spjd */ 95219354Spjdunsigned int 96219354Spjdlzf_decompress (const void *const in_data, unsigned int in_len, 97219354Spjd void *out_data, unsigned int out_len); 98219354Spjd 99219354Spjd/* 100219354Spjd * Size of hashtable is (1 << HLOG) * sizeof (char *) 101219354Spjd * decompression is independent of the hash table size 102219354Spjd * the difference between 15 and 14 is very small 103219354Spjd * for small blocks (and 14 is usually a bit faster). 104219354Spjd * For a low-memory/faster configuration, use HLOG == 13; 105219354Spjd * For best compression, use 15 or 16 (or more, up to 23). 106219354Spjd */ 107219354Spjd#ifndef HLOG 108219354Spjd# define HLOG 16 109219354Spjd#endif 110219354Spjd 111219354Spjd/* 112219354Spjd * Sacrifice very little compression quality in favour of compression speed. 113219354Spjd * This gives almost the same compression as the default code, and is 114219354Spjd * (very roughly) 15% faster. This is the preferred mode of operation. 115219354Spjd */ 116219354Spjd#ifndef VERY_FAST 117219354Spjd# define VERY_FAST 1 118219354Spjd#endif 119219354Spjd 120219354Spjd/* 121219354Spjd * Sacrifice some more compression quality in favour of compression speed. 122219354Spjd * (roughly 1-2% worse compression for large blocks and 123219354Spjd * 9-10% for small, redundant, blocks and >>20% better speed in both cases) 124219354Spjd * In short: when in need for speed, enable this for binary data, 125219354Spjd * possibly disable this for text data. 126219354Spjd */ 127219354Spjd#ifndef ULTRA_FAST 128219354Spjd# define ULTRA_FAST 0 129219354Spjd#endif 130219354Spjd 131219354Spjd/* 132219354Spjd * Unconditionally aligning does not cost very much, so do it if unsure 133219354Spjd */ 134219354Spjd#ifndef STRICT_ALIGN 135305142Sdim# if !(defined(__i386) || defined (__amd64)) 136305142Sdim# define STRICT_ALIGN 1 137305142Sdim# else 138305142Sdim# define STRICT_ALIGN 0 139305142Sdim# endif 140219354Spjd#endif 141219354Spjd 142219354Spjd/* 143219354Spjd * You may choose to pre-set the hash table (might be faster on some 144219354Spjd * modern cpus and large (>>64k) blocks, and also makes compression 145219354Spjd * deterministic/repeatable when the configuration otherwise is the same). 146219354Spjd */ 147219354Spjd#ifndef INIT_HTAB 148219354Spjd# define INIT_HTAB 1 149219354Spjd#endif 150219354Spjd 151219354Spjd/* 152219354Spjd * Avoid assigning values to errno variable? for some embedding purposes 153229778Suqs * (linux kernel for example), this is necessary. NOTE: this breaks 154219354Spjd * the documentation in lzf.h. 155219354Spjd */ 156219354Spjd#ifndef AVOID_ERRNO 157219354Spjd# define AVOID_ERRNO 0 158219354Spjd#endif 159219354Spjd 160219354Spjd/* 161219354Spjd * Wether to pass the LZF_STATE variable as argument, or allocate it 162219354Spjd * on the stack. For small-stack environments, define this to 1. 163219354Spjd * NOTE: this breaks the prototype in lzf.h. 164219354Spjd */ 165219354Spjd#ifndef LZF_STATE_ARG 166219354Spjd# define LZF_STATE_ARG 0 167219354Spjd#endif 168219354Spjd 169219354Spjd/* 170219354Spjd * Wether to add extra checks for input validity in lzf_decompress 171219354Spjd * and return EINVAL if the input stream has been corrupted. This 172219354Spjd * only shields against overflowing the input buffer and will not 173219354Spjd * detect most corrupted streams. 174229778Suqs * This check is not normally noticeable on modern hardware 175219354Spjd * (<1% slowdown), but might slow down older cpus considerably. 176219354Spjd */ 177219354Spjd#ifndef CHECK_INPUT 178219354Spjd# define CHECK_INPUT 1 179219354Spjd#endif 180219354Spjd 181219354Spjd/*****************************************************************************/ 182219354Spjd/* nothing should be changed below */ 183219354Spjd 184219354Spjdtypedef unsigned char u8; 185219354Spjd 186219354Spjdtypedef const u8 *LZF_STATE[1 << (HLOG)]; 187219354Spjd 188219354Spjd#if !STRICT_ALIGN 189219354Spjd/* for unaligned accesses we need a 16 bit datatype. */ 190219354Spjd# include <limits.h> 191219354Spjd# if USHRT_MAX == 65535 192219354Spjd typedef unsigned short u16; 193219354Spjd# elif UINT_MAX == 65535 194219354Spjd typedef unsigned int u16; 195219354Spjd# else 196219354Spjd# undef STRICT_ALIGN 197219354Spjd# define STRICT_ALIGN 1 198219354Spjd# endif 199219354Spjd#endif 200219354Spjd 201219354Spjd#if ULTRA_FAST 202219354Spjd# if defined(VERY_FAST) 203219354Spjd# undef VERY_FAST 204219354Spjd# endif 205219354Spjd#endif 206219354Spjd 207219354Spjd#if INIT_HTAB 208219354Spjd# ifdef __cplusplus 209219354Spjd# include <cstring> 210219354Spjd# else 211219354Spjd# include <string.h> 212219354Spjd# endif 213219354Spjd#endif 214219354Spjd 215219354Spjd#endif 216