Home | History | Annotate | Line # | Download | only in zfs
      1  1.1  chs /*
      2  1.1  chs  * LZ4 - Fast LZ compression algorithm
      3  1.1  chs  * Header File
      4  1.1  chs  * Copyright (C) 2011-2013, Yann Collet.
      5  1.1  chs  * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
      6  1.1  chs  *
      7  1.1  chs  * Redistribution and use in source and binary forms, with or without
      8  1.1  chs  * modification, are permitted provided that the following conditions are
      9  1.1  chs  * met:
     10  1.1  chs  *
     11  1.1  chs  *     * Redistributions of source code must retain the above copyright
     12  1.1  chs  * notice, this list of conditions and the following disclaimer.
     13  1.1  chs  *     * Redistributions in binary form must reproduce the above
     14  1.1  chs  * copyright notice, this list of conditions and the following disclaimer
     15  1.1  chs  * in the documentation and/or other materials provided with the
     16  1.1  chs  * distribution.
     17  1.1  chs  *
     18  1.1  chs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  1.1  chs  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  1.1  chs  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  1.1  chs  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  1.1  chs  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  1.1  chs  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  1.1  chs  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  chs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  chs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  chs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  1.1  chs  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  chs  *
     30  1.1  chs  * You can contact the author at :
     31  1.1  chs  * - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
     32  1.1  chs  * - LZ4 source repository : http://code.google.com/p/lz4/
     33  1.1  chs  */
     34  1.1  chs 
     35  1.1  chs #include <sys/zfs_context.h>
     36  1.1  chs 
     37  1.1  chs static int real_LZ4_compress(const char *source, char *dest, int isize,
     38  1.1  chs     int osize);
     39  1.1  chs static int LZ4_compressBound(int isize);
     40  1.1  chs static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest,
     41  1.1  chs     int isize, int maxOutputSize);
     42  1.1  chs static int LZ4_compressCtx(void *ctx, const char *source, char *dest,
     43  1.1  chs     int isize, int osize);
     44  1.1  chs static int LZ4_compress64kCtx(void *ctx, const char *source, char *dest,
     45  1.1  chs     int isize, int osize);
     46  1.1  chs 
     47  1.2  chs #ifdef __FreeBSD__
     48  1.1  chs static kmem_cache_t *lz4_ctx_cache;
     49  1.2  chs #endif
     50  1.1  chs 
     51  1.1  chs /*ARGSUSED*/
     52  1.1  chs size_t
     53  1.1  chs lz4_compress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
     54  1.1  chs {
     55  1.1  chs 	uint32_t bufsiz;
     56  1.1  chs 	char *dest = d_start;
     57  1.1  chs 
     58  1.1  chs 	ASSERT(d_len >= sizeof (bufsiz));
     59  1.1  chs 
     60  1.1  chs 	bufsiz = real_LZ4_compress(s_start, &dest[sizeof (bufsiz)], s_len,
     61  1.1  chs 	    d_len - sizeof (bufsiz));
     62  1.1  chs 
     63  1.1  chs 	/* Signal an error if the compression routine returned zero. */
     64  1.1  chs 	if (bufsiz == 0)
     65  1.1  chs 		return (s_len);
     66  1.1  chs 
     67  1.1  chs 	/*
     68  1.1  chs 	 * Encode the compresed buffer size at the start. We'll need this in
     69  1.1  chs 	 * decompression to counter the effects of padding which might be
     70  1.1  chs 	 * added to the compressed buffer and which, if unhandled, would
     71  1.1  chs 	 * confuse the hell out of our decompression function.
     72  1.1  chs 	 */
     73  1.1  chs 	*(uint32_t *)dest = BE_32(bufsiz);
     74  1.1  chs 
     75  1.1  chs 	return (bufsiz + sizeof (bufsiz));
     76  1.1  chs }
     77  1.1  chs 
     78  1.1  chs /*ARGSUSED*/
     79  1.1  chs int
     80  1.1  chs lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
     81  1.1  chs {
     82  1.1  chs 	const char *src = s_start;
     83  1.1  chs 	uint32_t bufsiz = BE_IN32(src);
     84  1.1  chs 
     85  1.1  chs 	/* invalid compressed buffer size encoded at start */
     86  1.1  chs 	if (bufsiz + sizeof (bufsiz) > s_len)
     87  1.1  chs 		return (1);
     88  1.1  chs 
     89  1.1  chs 	/*
     90  1.1  chs 	 * Returns 0 on success (decompression function returned non-negative)
     91  1.1  chs 	 * and non-zero on failure (decompression function returned negative.
     92  1.1  chs 	 */
     93  1.1  chs 	return (LZ4_uncompress_unknownOutputSize(&src[sizeof (bufsiz)],
     94  1.1  chs 	    d_start, bufsiz, d_len) < 0);
     95  1.1  chs }
     96  1.1  chs 
     97  1.1  chs /*
     98  1.1  chs  * LZ4 API Description:
     99  1.1  chs  *
    100  1.1  chs  * Simple Functions:
    101  1.1  chs  * real_LZ4_compress() :
    102  1.1  chs  * 	isize  : is the input size. Max supported value is ~1.9GB
    103  1.1  chs  * 	return : the number of bytes written in buffer dest
    104  1.1  chs  *		 or 0 if the compression fails (if LZ4_COMPRESSMIN is set).
    105  1.1  chs  * 	note : destination buffer must be already allocated.
    106  1.1  chs  * 		destination buffer must be sized to handle worst cases
    107  1.1  chs  * 		situations (input data not compressible) worst case size
    108  1.1  chs  * 		evaluation is provided by function LZ4_compressBound().
    109  1.1  chs  *
    110  1.1  chs  * Advanced Functions
    111  1.1  chs  *
    112  1.1  chs  * LZ4_compressBound() :
    113  1.1  chs  * 	Provides the maximum size that LZ4 may output in a "worst case"
    114  1.1  chs  * 	scenario (input data not compressible) primarily useful for memory
    115  1.1  chs  * 	allocation of output buffer.
    116  1.1  chs  *
    117  1.1  chs  * 	isize  : is the input size. Max supported value is ~1.9GB
    118  1.1  chs  * 	return : maximum output size in a "worst case" scenario
    119  1.1  chs  * 	note : this function is limited by "int" range (2^31-1)
    120  1.1  chs  *
    121  1.1  chs  * LZ4_uncompress_unknownOutputSize() :
    122  1.1  chs  * 	isize  : is the input size, therefore the compressed size
    123  1.1  chs  * 	maxOutputSize : is the size of the destination buffer (which must be
    124  1.1  chs  * 		already allocated)
    125  1.1  chs  * 	return : the number of bytes decoded in the destination buffer
    126  1.1  chs  * 		(necessarily <= maxOutputSize). If the source stream is
    127  1.1  chs  * 		malformed, the function will stop decoding and return a
    128  1.1  chs  * 		negative result, indicating the byte position of the faulty
    129  1.1  chs  * 		instruction. This function never writes beyond dest +
    130  1.1  chs  * 		maxOutputSize, and is therefore protected against malicious
    131  1.1  chs  * 		data packets.
    132  1.1  chs  * 	note   : Destination buffer must be already allocated.
    133  1.1  chs  *
    134  1.1  chs  * LZ4_compressCtx() :
    135  1.1  chs  * 	This function explicitly handles the CTX memory structure.
    136  1.1  chs  *
    137  1.1  chs  * 	ILLUMOS CHANGES: the CTX memory structure must be explicitly allocated
    138  1.1  chs  * 	by the caller (either on the stack or using kmem_zalloc). Passing NULL
    139  1.1  chs  * 	isn't valid.
    140  1.1  chs  *
    141  1.1  chs  * LZ4_compress64kCtx() :
    142  1.1  chs  * 	Same as LZ4_compressCtx(), but specific to small inputs (<64KB).
    143  1.1  chs  * 	isize *Must* be <64KB, otherwise the output will be corrupted.
    144  1.1  chs  *
    145  1.1  chs  * 	ILLUMOS CHANGES: the CTX memory structure must be explicitly allocated
    146  1.1  chs  * 	by the caller (either on the stack or using kmem_zalloc). Passing NULL
    147  1.1  chs  * 	isn't valid.
    148  1.1  chs  */
    149  1.1  chs 
    150  1.1  chs /*
    151  1.1  chs  * Tuning parameters
    152  1.1  chs  */
    153  1.1  chs 
    154  1.1  chs /*
    155  1.1  chs  * COMPRESSIONLEVEL: Increasing this value improves compression ratio
    156  1.1  chs  *	 Lowering this value reduces memory usage. Reduced memory usage
    157  1.1  chs  *	typically improves speed, due to cache effect (ex: L1 32KB for Intel,
    158  1.1  chs  *	L1 64KB for AMD). Memory usage formula : N->2^(N+2) Bytes
    159  1.1  chs  *	(examples : 12 -> 16KB ; 17 -> 512KB)
    160  1.1  chs  */
    161  1.1  chs #define	COMPRESSIONLEVEL 12
    162  1.1  chs 
    163  1.1  chs /*
    164  1.1  chs  * NOTCOMPRESSIBLE_CONFIRMATION: Decreasing this value will make the
    165  1.1  chs  *	algorithm skip faster data segments considered "incompressible".
    166  1.1  chs  *	This may decrease compression ratio dramatically, but will be
    167  1.1  chs  *	faster on incompressible data. Increasing this value will make
    168  1.1  chs  *	the algorithm search more before declaring a segment "incompressible".
    169  1.1  chs  *	This could improve compression a bit, but will be slower on
    170  1.1  chs  *	incompressible data. The default value (6) is recommended.
    171  1.1  chs  */
    172  1.1  chs #define	NOTCOMPRESSIBLE_CONFIRMATION 6
    173  1.1  chs 
    174  1.1  chs /*
    175  1.1  chs  * BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE: This will provide a boost to
    176  1.1  chs  * performance for big endian cpu, but the resulting compressed stream
    177  1.1  chs  * will be incompatible with little-endian CPU. You can set this option
    178  1.1  chs  * to 1 in situations where data will stay within closed environment.
    179  1.1  chs  * This option is useless on Little_Endian CPU (such as x86).
    180  1.1  chs  */
    181  1.1  chs /* #define	BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1 */
    182  1.1  chs 
    183  1.1  chs /*
    184  1.1  chs  * CPU Feature Detection
    185  1.1  chs  */
    186  1.1  chs 
    187  1.1  chs /* 32 or 64 bits ? */
    188  1.1  chs #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || \
    189  1.1  chs     defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || \
    190  1.1  chs     defined(__LP64__) || defined(_LP64))
    191  1.1  chs #define	LZ4_ARCH64 1
    192  1.1  chs #else
    193  1.1  chs #define	LZ4_ARCH64 0
    194  1.1  chs #endif
    195  1.1  chs 
    196  1.1  chs /*
    197  1.1  chs  * Limits the amount of stack space that the algorithm may consume to hold
    198  1.1  chs  * the compression lookup table. The value `9' here means we'll never use
    199  1.1  chs  * more than 2k of stack (see above for a description of COMPRESSIONLEVEL).
    200  1.1  chs  * If more memory is needed, it is allocated from the heap.
    201  1.1  chs  */
    202  1.1  chs /* FreeBSD: Use heap for all platforms for now */
    203  1.1  chs #define	STACKLIMIT 0
    204  1.1  chs 
    205  1.1  chs /*
    206  1.1  chs  * Little Endian or Big Endian?
    207  1.1  chs  * Note: overwrite the below #define if you know your architecture endianess.
    208  1.1  chs  */
    209  1.1  chs #if BYTE_ORDER == BIG_ENDIAN
    210  1.1  chs #define	LZ4_BIG_ENDIAN 1
    211  1.1  chs #else
    212  1.1  chs /*
    213  1.1  chs  * Little Endian assumed. PDP Endian and other very rare endian format
    214  1.1  chs  * are unsupported.
    215  1.1  chs  */
    216  1.1  chs #endif
    217  1.1  chs 
    218  1.1  chs /*
    219  1.1  chs  * Unaligned memory access is automatically enabled for "common" CPU,
    220  1.1  chs  * such as x86. For others CPU, the compiler will be more cautious, and
    221  1.1  chs  * insert extra code to ensure aligned access is respected. If you know
    222  1.1  chs  * your target CPU supports unaligned memory access, you may want to
    223  1.1  chs  * force this option manually to improve performance
    224  1.1  chs  */
    225  1.1  chs #if defined(__ARM_FEATURE_UNALIGNED)
    226  1.1  chs #define	LZ4_FORCE_UNALIGNED_ACCESS 1
    227  1.1  chs #endif
    228  1.1  chs 
    229  1.1  chs /*
    230  1.1  chs  * FreeBSD: can't use GCC's __builtin_ctz when using sparc64 because
    231  1.1  chs  * gcc currently rely on libcompiler_rt.
    232  1.1  chs  *
    233  1.1  chs  * TODO: revisit this when situation changes.
    234  1.1  chs  */
    235  1.1  chs #if defined(__sparc64__)
    236  1.1  chs #define	LZ4_FORCE_SW_BITCOUNT
    237  1.1  chs #endif
    238  1.1  chs 
    239  1.1  chs /*
    240  1.1  chs  * Compiler Options
    241  1.1  chs  */
    242  1.1  chs #if __STDC_VERSION__ >= 199901L	/* C99 */
    243  1.1  chs /* "restrict" is a known keyword */
    244  1.1  chs #else
    245  1.1  chs /* Disable restrict */
    246  1.1  chs #define	restrict
    247  1.1  chs #endif
    248  1.1  chs 
    249  1.1  chs #define	lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | \
    250  1.1  chs 	(((x) & 0xffu) << 8)))
    251  1.1  chs 
    252  1.1  chs #define	expect(expr, value)    (__builtin_expect((expr), (value)))
    253  1.1  chs 
    254  1.1  chs #if defined(likely)
    255  1.1  chs #undef likely
    256  1.1  chs #endif
    257  1.1  chs #if defined(unlikely)
    258  1.1  chs #undef unlikely
    259  1.1  chs #endif
    260  1.1  chs 
    261  1.1  chs #define	likely(expr)	expect((expr) != 0, 1)
    262  1.1  chs #define	unlikely(expr)	expect((expr) != 0, 0)
    263  1.1  chs 
    264  1.1  chs /* Basic types */
    265  1.1  chs #define	BYTE	uint8_t
    266  1.1  chs #define	U16	uint16_t
    267  1.1  chs #define	U32	uint32_t
    268  1.1  chs #define	S32	int32_t
    269  1.1  chs #define	U64	uint64_t
    270  1.1  chs 
    271  1.1  chs #ifndef LZ4_FORCE_UNALIGNED_ACCESS
    272  1.1  chs #pragma pack(1)
    273  1.1  chs #endif
    274  1.1  chs 
    275  1.1  chs typedef struct _U16_S {
    276  1.1  chs 	U16 v;
    277  1.1  chs } U16_S;
    278  1.1  chs typedef struct _U32_S {
    279  1.1  chs 	U32 v;
    280  1.1  chs } U32_S;
    281  1.1  chs typedef struct _U64_S {
    282  1.1  chs 	U64 v;
    283  1.1  chs } U64_S;
    284  1.1  chs 
    285  1.1  chs #ifndef LZ4_FORCE_UNALIGNED_ACCESS
    286  1.1  chs #pragma pack()
    287  1.1  chs #endif
    288  1.1  chs 
    289  1.1  chs #define	A64(x) (((U64_S *)(x))->v)
    290  1.1  chs #define	A32(x) (((U32_S *)(x))->v)
    291  1.1  chs #define	A16(x) (((U16_S *)(x))->v)
    292  1.1  chs 
    293  1.1  chs /*
    294  1.1  chs  * Constants
    295  1.1  chs  */
    296  1.1  chs #define	MINMATCH 4
    297  1.1  chs 
    298  1.1  chs #define	HASH_LOG COMPRESSIONLEVEL
    299  1.1  chs #define	HASHTABLESIZE (1 << HASH_LOG)
    300  1.1  chs #define	HASH_MASK (HASHTABLESIZE - 1)
    301  1.1  chs 
    302  1.1  chs #define	SKIPSTRENGTH (NOTCOMPRESSIBLE_CONFIRMATION > 2 ? \
    303  1.1  chs 	NOTCOMPRESSIBLE_CONFIRMATION : 2)
    304  1.1  chs 
    305  1.1  chs /*
    306  1.1  chs  * Defines if memory is allocated into the stack (local variable),
    307  1.1  chs  * or into the heap (kmem_alloc()).
    308  1.1  chs  */
    309  1.1  chs #define	HEAPMODE (HASH_LOG > STACKLIMIT)
    310  1.1  chs #define	COPYLENGTH 8
    311  1.1  chs #define	LASTLITERALS 5
    312  1.1  chs #define	MFLIMIT (COPYLENGTH + MINMATCH)
    313  1.1  chs #define	MINLENGTH (MFLIMIT + 1)
    314  1.1  chs 
    315  1.1  chs #define	MAXD_LOG 16
    316  1.1  chs #define	MAX_DISTANCE ((1 << MAXD_LOG) - 1)
    317  1.1  chs 
    318  1.1  chs #define	ML_BITS 4
    319  1.1  chs #define	ML_MASK ((1U<<ML_BITS)-1)
    320  1.1  chs #define	RUN_BITS (8-ML_BITS)
    321  1.1  chs #define	RUN_MASK ((1U<<RUN_BITS)-1)
    322  1.1  chs 
    323  1.1  chs 
    324  1.1  chs /*
    325  1.1  chs  * Architecture-specific macros
    326  1.1  chs  */
    327  1.1  chs #if LZ4_ARCH64
    328  1.1  chs #define	STEPSIZE 8
    329  1.1  chs #define	UARCH U64
    330  1.1  chs #define	AARCH A64
    331  1.1  chs #define	LZ4_COPYSTEP(s, d)	A64(d) = A64(s); d += 8; s += 8;
    332  1.1  chs #define	LZ4_COPYPACKET(s, d)	LZ4_COPYSTEP(s, d)
    333  1.1  chs #define	LZ4_SECURECOPY(s, d, e)	if (d < e) LZ4_WILDCOPY(s, d, e)
    334  1.1  chs #define	HTYPE U32
    335  1.1  chs #define	INITBASE(base)		const BYTE* const base = ip
    336  1.1  chs #else /* !LZ4_ARCH64 */
    337  1.1  chs #define	STEPSIZE 4
    338  1.1  chs #define	UARCH U32
    339  1.1  chs #define	AARCH A32
    340  1.1  chs #define	LZ4_COPYSTEP(s, d)	A32(d) = A32(s); d += 4; s += 4;
    341  1.1  chs #define	LZ4_COPYPACKET(s, d)	LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
    342  1.1  chs #define	LZ4_SECURECOPY		LZ4_WILDCOPY
    343  1.1  chs #define	HTYPE const BYTE *
    344  1.1  chs #define	INITBASE(base)		const int base = 0
    345  1.1  chs #endif /* !LZ4_ARCH64 */
    346  1.1  chs 
    347  1.1  chs #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
    348  1.1  chs #define	LZ4_READ_LITTLEENDIAN_16(d, s, p) \
    349  1.1  chs 	{ U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
    350  1.1  chs #define	LZ4_WRITE_LITTLEENDIAN_16(p, i) \
    351  1.1  chs 	{ U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p += 2; }
    352  1.1  chs #else
    353  1.1  chs #define	LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - A16(p); }
    354  1.1  chs #define	LZ4_WRITE_LITTLEENDIAN_16(p, v)  { A16(p) = v; p += 2; }
    355  1.1  chs #endif
    356  1.1  chs 
    357  1.1  chs 
    358  1.1  chs /* Local structures */
    359  1.1  chs struct refTables {
    360  1.1  chs 	HTYPE hashTable[HASHTABLESIZE];
    361  1.1  chs };
    362  1.1  chs 
    363  1.1  chs 
    364  1.1  chs /* Macros */
    365  1.1  chs #define	LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH * 8) - \
    366  1.1  chs 	HASH_LOG))
    367  1.1  chs #define	LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
    368  1.1  chs #define	LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
    369  1.1  chs #define	LZ4_BLINDCOPY(s, d, l) { BYTE* e = (d) + l; LZ4_WILDCOPY(s, d, e); \
    370  1.1  chs 	d = e; }
    371  1.1  chs 
    372  1.1  chs 
    373  1.1  chs /* Private functions */
    374  1.1  chs #if LZ4_ARCH64
    375  1.1  chs 
    376  1.1  chs static inline int
    377  1.1  chs LZ4_NbCommonBytes(register U64 val)
    378  1.1  chs {
    379  1.1  chs #if defined(LZ4_BIG_ENDIAN)
    380  1.1  chs #if !defined(LZ4_FORCE_SW_BITCOUNT)
    381  1.1  chs 	return (__builtin_clzll(val) >> 3);
    382  1.1  chs #else
    383  1.1  chs 	int r;
    384  1.1  chs 	if (!(val >> 32)) {
    385  1.1  chs 		r = 4;
    386  1.1  chs 	} else {
    387  1.1  chs 		r = 0;
    388  1.1  chs 		val >>= 32;
    389  1.1  chs 	}
    390  1.1  chs 	if (!(val >> 16)) {
    391  1.1  chs 		r += 2;
    392  1.1  chs 		val >>= 8;
    393  1.1  chs 	} else {
    394  1.1  chs 		val >>= 24;
    395  1.1  chs 	}
    396  1.1  chs 	r += (!val);
    397  1.1  chs 	return (r);
    398  1.1  chs #endif
    399  1.1  chs #else
    400  1.1  chs #if !defined(LZ4_FORCE_SW_BITCOUNT)
    401  1.1  chs 	return (__builtin_ctzll(val) >> 3);
    402  1.1  chs #else
    403  1.1  chs 	static const int DeBruijnBytePos[64] =
    404  1.1  chs 	    { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5,
    405  1.1  chs 		3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5,
    406  1.1  chs 		5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4,
    407  1.1  chs 		4, 5, 7, 2, 6, 5, 7, 6, 7, 7
    408  1.1  chs 	};
    409  1.1  chs 	return DeBruijnBytePos[((U64) ((val & -val) * 0x0218A392CDABBD3F)) >>
    410  1.1  chs 	    58];
    411  1.1  chs #endif
    412  1.1  chs #endif
    413  1.1  chs }
    414  1.1  chs 
    415  1.1  chs #else
    416  1.1  chs 
    417  1.1  chs static inline int
    418  1.1  chs LZ4_NbCommonBytes(register U32 val)
    419  1.1  chs {
    420  1.1  chs #if defined(LZ4_BIG_ENDIAN)
    421  1.1  chs #if !defined(LZ4_FORCE_SW_BITCOUNT)
    422  1.1  chs 	return (__builtin_clz(val) >> 3);
    423  1.1  chs #else
    424  1.1  chs 	int r;
    425  1.1  chs 	if (!(val >> 16)) {
    426  1.1  chs 		r = 2;
    427  1.1  chs 		val >>= 8;
    428  1.1  chs 	} else {
    429  1.1  chs 		r = 0;
    430  1.1  chs 		val >>= 24;
    431  1.1  chs 	}
    432  1.1  chs 	r += (!val);
    433  1.1  chs 	return (r);
    434  1.1  chs #endif
    435  1.1  chs #else
    436  1.1  chs #if !defined(LZ4_FORCE_SW_BITCOUNT)
    437  1.1  chs 	return (__builtin_ctz(val) >> 3);
    438  1.1  chs #else
    439  1.1  chs 	static const int DeBruijnBytePos[32] = {
    440  1.1  chs 		0, 0, 3, 0, 3, 1, 3, 0,
    441  1.1  chs 		3, 2, 2, 1, 3, 2, 0, 1,
    442  1.1  chs 		3, 3, 1, 2, 2, 2, 2, 0,
    443  1.1  chs 		3, 1, 2, 0, 1, 0, 1, 1
    444  1.1  chs 	};
    445  1.1  chs 	return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >>
    446  1.1  chs 	    27];
    447  1.1  chs #endif
    448  1.1  chs #endif
    449  1.1  chs }
    450  1.1  chs 
    451  1.1  chs #endif
    452  1.1  chs 
    453  1.1  chs /* Public functions */
    454  1.1  chs 
    455  1.1  chs static int
    456  1.1  chs LZ4_compressBound(int isize)
    457  1.1  chs {
    458  1.1  chs 	return (isize + (isize / 255) + 16);
    459  1.1  chs }
    460  1.1  chs 
    461  1.1  chs /* Compression functions */
    462  1.1  chs 
    463  1.1  chs /*ARGSUSED*/
    464  1.1  chs static int
    465  1.1  chs LZ4_compressCtx(void *ctx, const char *source, char *dest, int isize,
    466  1.1  chs     int osize)
    467  1.1  chs {
    468  1.1  chs #if HEAPMODE
    469  1.1  chs 	struct refTables *srt = (struct refTables *)ctx;
    470  1.1  chs 	HTYPE *HashTable = (HTYPE *) (srt->hashTable);
    471  1.1  chs #else
    472  1.1  chs 	HTYPE HashTable[HASHTABLESIZE] = { 0 };
    473  1.1  chs #endif
    474  1.1  chs 
    475  1.1  chs 	const BYTE *ip = (BYTE *) source;
    476  1.1  chs 	INITBASE(base);
    477  1.1  chs 	const BYTE *anchor = ip;
    478  1.1  chs 	const BYTE *const iend = ip + isize;
    479  1.1  chs 	const BYTE *const oend = (BYTE *) dest + osize;
    480  1.1  chs 	const BYTE *const mflimit = iend - MFLIMIT;
    481  1.1  chs #define	matchlimit (iend - LASTLITERALS)
    482  1.1  chs 
    483  1.1  chs 	BYTE *op = (BYTE *) dest;
    484  1.1  chs 
    485  1.1  chs 	int len, length;
    486  1.1  chs 	const int skipStrength = SKIPSTRENGTH;
    487  1.1  chs 	U32 forwardH;
    488  1.1  chs 
    489  1.1  chs 
    490  1.1  chs 	/* Init */
    491  1.1  chs 	if (isize < MINLENGTH)
    492  1.1  chs 		goto _last_literals;
    493  1.1  chs 
    494  1.1  chs 	/* First Byte */
    495  1.1  chs 	HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
    496  1.1  chs 	ip++;
    497  1.1  chs 	forwardH = LZ4_HASH_VALUE(ip);
    498  1.1  chs 
    499  1.1  chs 	/* Main Loop */
    500  1.1  chs 	for (;;) {
    501  1.1  chs 		int findMatchAttempts = (1U << skipStrength) + 3;
    502  1.1  chs 		const BYTE *forwardIp = ip;
    503  1.1  chs 		const BYTE *ref;
    504  1.1  chs 		BYTE *token;
    505  1.1  chs 
    506  1.1  chs 		/* Find a match */
    507  1.1  chs 		do {
    508  1.1  chs 			U32 h = forwardH;
    509  1.1  chs 			int step = findMatchAttempts++ >> skipStrength;
    510  1.1  chs 			ip = forwardIp;
    511  1.1  chs 			forwardIp = ip + step;
    512  1.1  chs 
    513  1.1  chs 			if unlikely(forwardIp > mflimit) {
    514  1.1  chs 				goto _last_literals;
    515  1.1  chs 			}
    516  1.1  chs 
    517  1.1  chs 			forwardH = LZ4_HASH_VALUE(forwardIp);
    518  1.1  chs 			ref = base + HashTable[h];
    519  1.1  chs 			HashTable[h] = ip - base;
    520  1.1  chs 
    521  1.1  chs 		} while ((ref < ip - MAX_DISTANCE) || (A32(ref) != A32(ip)));
    522  1.1  chs 
    523  1.1  chs 		/* Catch up */
    524  1.1  chs 		while ((ip > anchor) && (ref > (BYTE *) source) &&
    525  1.1  chs 		    unlikely(ip[-1] == ref[-1])) {
    526  1.1  chs 			ip--;
    527  1.1  chs 			ref--;
    528  1.1  chs 		}
    529  1.1  chs 
    530  1.1  chs 		/* Encode Literal length */
    531  1.1  chs 		length = ip - anchor;
    532  1.1  chs 		token = op++;
    533  1.1  chs 
    534  1.1  chs 		/* Check output limit */
    535  1.1  chs 		if unlikely(op + length + (2 + 1 + LASTLITERALS) +
    536  1.1  chs 		    (length >> 8) > oend)
    537  1.1  chs 			return (0);
    538  1.1  chs 
    539  1.1  chs 		if (length >= (int)RUN_MASK) {
    540  1.1  chs 			*token = (RUN_MASK << ML_BITS);
    541  1.1  chs 			len = length - RUN_MASK;
    542  1.1  chs 			for (; len > 254; len -= 255)
    543  1.1  chs 				*op++ = 255;
    544  1.1  chs 			*op++ = (BYTE)len;
    545  1.1  chs 		} else
    546  1.1  chs 			*token = (length << ML_BITS);
    547  1.1  chs 
    548  1.1  chs 		/* Copy Literals */
    549  1.1  chs 		LZ4_BLINDCOPY(anchor, op, length);
    550  1.1  chs 
    551  1.1  chs 		_next_match:
    552  1.1  chs 		/* Encode Offset */
    553  1.1  chs 		LZ4_WRITE_LITTLEENDIAN_16(op, ip - ref);
    554  1.1  chs 
    555  1.1  chs 		/* Start Counting */
    556  1.1  chs 		ip += MINMATCH;
    557  1.1  chs 		ref += MINMATCH;	/* MinMatch verified */
    558  1.1  chs 		anchor = ip;
    559  1.1  chs 		while likely(ip < matchlimit - (STEPSIZE - 1)) {
    560  1.1  chs 			UARCH diff = AARCH(ref) ^ AARCH(ip);
    561  1.1  chs 			if (!diff) {
    562  1.1  chs 				ip += STEPSIZE;
    563  1.1  chs 				ref += STEPSIZE;
    564  1.1  chs 				continue;
    565  1.1  chs 			}
    566  1.1  chs 			ip += LZ4_NbCommonBytes(diff);
    567  1.1  chs 			goto _endCount;
    568  1.1  chs 		}
    569  1.1  chs #if LZ4_ARCH64
    570  1.1  chs 		if ((ip < (matchlimit - 3)) && (A32(ref) == A32(ip))) {
    571  1.1  chs 			ip += 4;
    572  1.1  chs 			ref += 4;
    573  1.1  chs 		}
    574  1.1  chs #endif
    575  1.1  chs 		if ((ip < (matchlimit - 1)) && (A16(ref) == A16(ip))) {
    576  1.1  chs 			ip += 2;
    577  1.1  chs 			ref += 2;
    578  1.1  chs 		}
    579  1.1  chs 		if ((ip < matchlimit) && (*ref == *ip))
    580  1.1  chs 			ip++;
    581  1.1  chs 		_endCount:
    582  1.1  chs 
    583  1.1  chs 		/* Encode MatchLength */
    584  1.1  chs 		len = (ip - anchor);
    585  1.1  chs 		/* Check output limit */
    586  1.1  chs 		if unlikely(op + (1 + LASTLITERALS) + (len >> 8) > oend)
    587  1.1  chs 			return (0);
    588  1.1  chs 		if (len >= (int)ML_MASK) {
    589  1.1  chs 			*token += ML_MASK;
    590  1.1  chs 			len -= ML_MASK;
    591  1.1  chs 			for (; len > 509; len -= 510) {
    592  1.1  chs 				*op++ = 255;
    593  1.1  chs 				*op++ = 255;
    594  1.1  chs 			}
    595  1.1  chs 			if (len > 254) {
    596  1.1  chs 				len -= 255;
    597  1.1  chs 				*op++ = 255;
    598  1.1  chs 			}
    599  1.1  chs 			*op++ = (BYTE)len;
    600  1.1  chs 		} else
    601  1.1  chs 			*token += len;
    602  1.1  chs 
    603  1.1  chs 		/* Test end of chunk */
    604  1.1  chs 		if (ip > mflimit) {
    605  1.1  chs 			anchor = ip;
    606  1.1  chs 			break;
    607  1.1  chs 		}
    608  1.1  chs 		/* Fill table */
    609  1.1  chs 		HashTable[LZ4_HASH_VALUE(ip - 2)] = ip - 2 - base;
    610  1.1  chs 
    611  1.1  chs 		/* Test next position */
    612  1.1  chs 		ref = base + HashTable[LZ4_HASH_VALUE(ip)];
    613  1.1  chs 		HashTable[LZ4_HASH_VALUE(ip)] = ip - base;
    614  1.1  chs 		if ((ref > ip - (MAX_DISTANCE + 1)) && (A32(ref) == A32(ip))) {
    615  1.1  chs 			token = op++;
    616  1.1  chs 			*token = 0;
    617  1.1  chs 			goto _next_match;
    618  1.1  chs 		}
    619  1.1  chs 		/* Prepare next loop */
    620  1.1  chs 		anchor = ip++;
    621  1.1  chs 		forwardH = LZ4_HASH_VALUE(ip);
    622  1.1  chs 	}
    623  1.1  chs 
    624  1.1  chs 	_last_literals:
    625  1.1  chs 	/* Encode Last Literals */
    626  1.1  chs 	{
    627  1.1  chs 		int lastRun = iend - anchor;
    628  1.1  chs 		if (op + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) >
    629  1.1  chs 		    oend)
    630  1.1  chs 			return (0);
    631  1.1  chs 		if (lastRun >= (int)RUN_MASK) {
    632  1.1  chs 			*op++ = (RUN_MASK << ML_BITS);
    633  1.1  chs 			lastRun -= RUN_MASK;
    634  1.1  chs 			for (; lastRun > 254; lastRun -= 255) {
    635  1.1  chs 				*op++ = 255;
    636  1.1  chs 			}
    637  1.1  chs 			*op++ = (BYTE)lastRun;
    638  1.1  chs 		} else
    639  1.1  chs 			*op++ = (lastRun << ML_BITS);
    640  1.1  chs 		(void) memcpy(op, anchor, iend - anchor);
    641  1.1  chs 		op += iend - anchor;
    642  1.1  chs 	}
    643  1.1  chs 
    644  1.1  chs 	/* End */
    645  1.1  chs 	return (int)(((char *)op) - dest);
    646  1.1  chs }
    647  1.1  chs 
    648  1.1  chs 
    649  1.1  chs 
    650  1.1  chs /* Note : this function is valid only if isize < LZ4_64KLIMIT */
    651  1.1  chs #define	LZ4_64KLIMIT ((1 << 16) + (MFLIMIT - 1))
    652  1.1  chs #define	HASHLOG64K (HASH_LOG + 1)
    653  1.1  chs #define	HASH64KTABLESIZE (1U << HASHLOG64K)
    654  1.1  chs #define	LZ4_HASH64K_FUNCTION(i)	(((i) * 2654435761U) >> ((MINMATCH*8) - \
    655  1.1  chs 	HASHLOG64K))
    656  1.1  chs #define	LZ4_HASH64K_VALUE(p)	LZ4_HASH64K_FUNCTION(A32(p))
    657  1.1  chs 
    658  1.1  chs /*ARGSUSED*/
    659  1.1  chs static int
    660  1.1  chs LZ4_compress64kCtx(void *ctx, const char *source, char *dest, int isize,
    661  1.1  chs     int osize)
    662  1.1  chs {
    663  1.1  chs #if HEAPMODE
    664  1.1  chs 	struct refTables *srt = (struct refTables *)ctx;
    665  1.1  chs 	U16 *HashTable = (U16 *) (srt->hashTable);
    666  1.1  chs #else
    667  1.1  chs 	U16 HashTable[HASH64KTABLESIZE] = { 0 };
    668  1.1  chs #endif
    669  1.1  chs 
    670  1.1  chs 	const BYTE *ip = (BYTE *) source;
    671  1.1  chs 	const BYTE *anchor = ip;
    672  1.1  chs 	const BYTE *const base = ip;
    673  1.1  chs 	const BYTE *const iend = ip + isize;
    674  1.1  chs 	const BYTE *const oend = (BYTE *) dest + osize;
    675  1.1  chs 	const BYTE *const mflimit = iend - MFLIMIT;
    676  1.1  chs #define	matchlimit (iend - LASTLITERALS)
    677  1.1  chs 
    678  1.1  chs 	BYTE *op = (BYTE *) dest;
    679  1.1  chs 
    680  1.1  chs 	int len, length;
    681  1.1  chs 	const int skipStrength = SKIPSTRENGTH;
    682  1.1  chs 	U32 forwardH;
    683  1.1  chs 
    684  1.1  chs 	/* Init */
    685  1.1  chs 	if (isize < MINLENGTH)
    686  1.1  chs 		goto _last_literals;
    687  1.1  chs 
    688  1.1  chs 	/* First Byte */
    689  1.1  chs 	ip++;
    690  1.1  chs 	forwardH = LZ4_HASH64K_VALUE(ip);
    691  1.1  chs 
    692  1.1  chs 	/* Main Loop */
    693  1.1  chs 	for (;;) {
    694  1.1  chs 		int findMatchAttempts = (1U << skipStrength) + 3;
    695  1.1  chs 		const BYTE *forwardIp = ip;
    696  1.1  chs 		const BYTE *ref;
    697  1.1  chs 		BYTE *token;
    698  1.1  chs 
    699  1.1  chs 		/* Find a match */
    700  1.1  chs 		do {
    701  1.1  chs 			U32 h = forwardH;
    702  1.1  chs 			int step = findMatchAttempts++ >> skipStrength;
    703  1.1  chs 			ip = forwardIp;
    704  1.1  chs 			forwardIp = ip + step;
    705  1.1  chs 
    706  1.1  chs 			if (forwardIp > mflimit) {
    707  1.1  chs 				goto _last_literals;
    708  1.1  chs 			}
    709  1.1  chs 
    710  1.1  chs 			forwardH = LZ4_HASH64K_VALUE(forwardIp);
    711  1.1  chs 			ref = base + HashTable[h];
    712  1.1  chs 			HashTable[h] = ip - base;
    713  1.1  chs 
    714  1.1  chs 		} while (A32(ref) != A32(ip));
    715  1.1  chs 
    716  1.1  chs 		/* Catch up */
    717  1.1  chs 		while ((ip > anchor) && (ref > (BYTE *) source) &&
    718  1.1  chs 		    (ip[-1] == ref[-1])) {
    719  1.1  chs 			ip--;
    720  1.1  chs 			ref--;
    721  1.1  chs 		}
    722  1.1  chs 
    723  1.1  chs 		/* Encode Literal length */
    724  1.1  chs 		length = ip - anchor;
    725  1.1  chs 		token = op++;
    726  1.1  chs 
    727  1.1  chs 		/* Check output limit */
    728  1.1  chs 		if unlikely(op + length + (2 + 1 + LASTLITERALS) +
    729  1.1  chs 		    (length >> 8) > oend)
    730  1.1  chs 			return (0);
    731  1.1  chs 
    732  1.1  chs 		if (length >= (int)RUN_MASK) {
    733  1.1  chs 			*token = (RUN_MASK << ML_BITS);
    734  1.1  chs 			len = length - RUN_MASK;
    735  1.1  chs 			for (; len > 254; len -= 255)
    736  1.1  chs 				*op++ = 255;
    737  1.1  chs 			*op++ = (BYTE)len;
    738  1.1  chs 		} else
    739  1.1  chs 			*token = (length << ML_BITS);
    740  1.1  chs 
    741  1.1  chs 		/* Copy Literals */
    742  1.1  chs 		LZ4_BLINDCOPY(anchor, op, length);
    743  1.1  chs 
    744  1.1  chs 		_next_match:
    745  1.1  chs 		/* Encode Offset */
    746  1.1  chs 		LZ4_WRITE_LITTLEENDIAN_16(op, ip - ref);
    747  1.1  chs 
    748  1.1  chs 		/* Start Counting */
    749  1.1  chs 		ip += MINMATCH;
    750  1.1  chs 		ref += MINMATCH;	/* MinMatch verified */
    751  1.1  chs 		anchor = ip;
    752  1.1  chs 		while (ip < matchlimit - (STEPSIZE - 1)) {
    753  1.1  chs 			UARCH diff = AARCH(ref) ^ AARCH(ip);
    754  1.1  chs 			if (!diff) {
    755  1.1  chs 				ip += STEPSIZE;
    756  1.1  chs 				ref += STEPSIZE;
    757  1.1  chs 				continue;
    758  1.1  chs 			}
    759  1.1  chs 			ip += LZ4_NbCommonBytes(diff);
    760  1.1  chs 			goto _endCount;
    761  1.1  chs 		}
    762  1.1  chs #if LZ4_ARCH64
    763  1.1  chs 		if ((ip < (matchlimit - 3)) && (A32(ref) == A32(ip))) {
    764  1.1  chs 			ip += 4;
    765  1.1  chs 			ref += 4;
    766  1.1  chs 		}
    767  1.1  chs #endif
    768  1.1  chs 		if ((ip < (matchlimit - 1)) && (A16(ref) == A16(ip))) {
    769  1.1  chs 			ip += 2;
    770  1.1  chs 			ref += 2;
    771  1.1  chs 		}
    772  1.1  chs 		if ((ip < matchlimit) && (*ref == *ip))
    773  1.1  chs 			ip++;
    774  1.1  chs 		_endCount:
    775  1.1  chs 
    776  1.1  chs 		/* Encode MatchLength */
    777  1.1  chs 		len = (ip - anchor);
    778  1.1  chs 		/* Check output limit */
    779  1.1  chs 		if unlikely(op + (1 + LASTLITERALS) + (len >> 8) > oend)
    780  1.1  chs 			return (0);
    781  1.1  chs 		if (len >= (int)ML_MASK) {
    782  1.1  chs 			*token += ML_MASK;
    783  1.1  chs 			len -= ML_MASK;
    784  1.1  chs 			for (; len > 509; len -= 510) {
    785  1.1  chs 				*op++ = 255;
    786  1.1  chs 				*op++ = 255;
    787  1.1  chs 			}
    788  1.1  chs 			if (len > 254) {
    789  1.1  chs 				len -= 255;
    790  1.1  chs 				*op++ = 255;
    791  1.1  chs 			}
    792  1.1  chs 			*op++ = (BYTE)len;
    793  1.1  chs 		} else
    794  1.1  chs 			*token += len;
    795  1.1  chs 
    796  1.1  chs 		/* Test end of chunk */
    797  1.1  chs 		if (ip > mflimit) {
    798  1.1  chs 			anchor = ip;
    799  1.1  chs 			break;
    800  1.1  chs 		}
    801  1.1  chs 		/* Fill table */
    802  1.1  chs 		HashTable[LZ4_HASH64K_VALUE(ip - 2)] = ip - 2 - base;
    803  1.1  chs 
    804  1.1  chs 		/* Test next position */
    805  1.1  chs 		ref = base + HashTable[LZ4_HASH64K_VALUE(ip)];
    806  1.1  chs 		HashTable[LZ4_HASH64K_VALUE(ip)] = ip - base;
    807  1.1  chs 		if (A32(ref) == A32(ip)) {
    808  1.1  chs 			token = op++;
    809  1.1  chs 			*token = 0;
    810  1.1  chs 			goto _next_match;
    811  1.1  chs 		}
    812  1.1  chs 		/* Prepare next loop */
    813  1.1  chs 		anchor = ip++;
    814  1.1  chs 		forwardH = LZ4_HASH64K_VALUE(ip);
    815  1.1  chs 	}
    816  1.1  chs 
    817  1.1  chs 	_last_literals:
    818  1.1  chs 	/* Encode Last Literals */
    819  1.1  chs 	{
    820  1.1  chs 		int lastRun = iend - anchor;
    821  1.1  chs 		if (op + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) >
    822  1.1  chs 		    oend)
    823  1.1  chs 			return (0);
    824  1.1  chs 		if (lastRun >= (int)RUN_MASK) {
    825  1.1  chs 			*op++ = (RUN_MASK << ML_BITS);
    826  1.1  chs 			lastRun -= RUN_MASK;
    827  1.1  chs 			for (; lastRun > 254; lastRun -= 255)
    828  1.1  chs 				*op++ = 255;
    829  1.1  chs 			*op++ = (BYTE)lastRun;
    830  1.1  chs 		} else
    831  1.1  chs 			*op++ = (lastRun << ML_BITS);
    832  1.1  chs 		(void) memcpy(op, anchor, iend - anchor);
    833  1.1  chs 		op += iend - anchor;
    834  1.1  chs 	}
    835  1.1  chs 
    836  1.1  chs 	/* End */
    837  1.1  chs 	return (int)(((char *)op) - dest);
    838  1.1  chs }
    839  1.1  chs 
    840  1.1  chs static int
    841  1.1  chs real_LZ4_compress(const char *source, char *dest, int isize, int osize)
    842  1.1  chs {
    843  1.1  chs #if HEAPMODE
    844  1.2  chs #ifdef __FreeBSD__
    845  1.1  chs 	void *ctx = kmem_cache_alloc(lz4_ctx_cache, KM_NOSLEEP);
    846  1.2  chs #else
    847  1.2  chs 	void *ctx = kmem_zalloc(sizeof (struct refTables), KM_NOSLEEP);
    848  1.2  chs #endif
    849  1.1  chs 	int result;
    850  1.1  chs 
    851  1.1  chs 	/*
    852  1.1  chs 	 * out of kernel memory, gently fall through - this will disable
    853  1.1  chs 	 * compression in zio_compress_data
    854  1.1  chs 	 */
    855  1.1  chs 	if (ctx == NULL)
    856  1.1  chs 		return (0);
    857  1.1  chs 
    858  1.1  chs 	bzero(ctx, sizeof(struct refTables));
    859  1.1  chs 	if (isize < LZ4_64KLIMIT)
    860  1.1  chs 		result = LZ4_compress64kCtx(ctx, source, dest, isize, osize);
    861  1.1  chs 	else
    862  1.1  chs 		result = LZ4_compressCtx(ctx, source, dest, isize, osize);
    863  1.1  chs 
    864  1.2  chs #ifdef __FreeBSD__
    865  1.1  chs 	kmem_cache_free(lz4_ctx_cache, ctx);
    866  1.2  chs #else
    867  1.2  chs 	kmem_free(ctx, sizeof (struct refTables));
    868  1.2  chs #endif
    869  1.1  chs 	return (result);
    870  1.1  chs #else
    871  1.1  chs 	if (isize < (int)LZ4_64KLIMIT)
    872  1.1  chs 		return (LZ4_compress64kCtx(NULL, source, dest, isize, osize));
    873  1.1  chs 	return (LZ4_compressCtx(NULL, source, dest, isize, osize));
    874  1.1  chs #endif
    875  1.1  chs }
    876  1.1  chs 
    877  1.1  chs /* Decompression functions */
    878  1.1  chs 
    879  1.1  chs /*
    880  1.1  chs  * Note: The decoding function LZ4_uncompress_unknownOutputSize() is safe
    881  1.1  chs  *	against "buffer overflow" attack type. They will never write nor
    882  1.1  chs  *	read outside of the provided output buffers.
    883  1.1  chs  *	LZ4_uncompress_unknownOutputSize() also insures that it will never
    884  1.1  chs  *	read outside of the input buffer.  A corrupted input will produce
    885  1.1  chs  *	an error result, a negative int, indicating the position of the
    886  1.1  chs  *	error within input stream.
    887  1.1  chs  */
    888  1.1  chs 
    889  1.1  chs static int
    890  1.1  chs LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize,
    891  1.1  chs     int maxOutputSize)
    892  1.1  chs {
    893  1.1  chs 	/* Local Variables */
    894  1.1  chs 	const BYTE *restrict ip = (const BYTE *) source;
    895  1.1  chs 	const BYTE *const iend = ip + isize;
    896  1.1  chs 	const BYTE *ref;
    897  1.1  chs 
    898  1.1  chs 	BYTE *op = (BYTE *) dest;
    899  1.1  chs 	BYTE *const oend = op + maxOutputSize;
    900  1.1  chs 	BYTE *cpy;
    901  1.1  chs 
    902  1.1  chs 	size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};
    903  1.1  chs #if LZ4_ARCH64
    904  1.1  chs 	size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
    905  1.1  chs #endif
    906  1.1  chs 
    907  1.1  chs 	/* Main Loop */
    908  1.1  chs 	while (ip < iend) {
    909  1.1  chs 		unsigned token;
    910  1.1  chs 		size_t length;
    911  1.1  chs 
    912  1.1  chs 		/* get runlength */
    913  1.1  chs 		token = *ip++;
    914  1.1  chs 		if ((length = (token >> ML_BITS)) == RUN_MASK) {
    915  1.1  chs 			int s = 255;
    916  1.1  chs 			while ((ip < iend) && (s == 255)) {
    917  1.1  chs 				s = *ip++;
    918  1.1  chs 				length += s;
    919  1.1  chs 			}
    920  1.1  chs 		}
    921  1.1  chs 		/* copy literals */
    922  1.1  chs 		cpy = op + length;
    923  1.1  chs 		/* CORNER-CASE: cpy might overflow. */
    924  1.1  chs 		if (cpy < op)
    925  1.1  chs 			goto _output_error;	/* cpy was overflowed, bail! */
    926  1.1  chs 		if ((cpy > oend - COPYLENGTH) ||
    927  1.1  chs 		    (ip + length > iend - COPYLENGTH)) {
    928  1.1  chs 			if (cpy > oend)
    929  1.1  chs 				/* Error: writes beyond output buffer */
    930  1.1  chs 				goto _output_error;
    931  1.1  chs 			if (ip + length != iend)
    932  1.1  chs 				/*
    933  1.1  chs 				 * Error: LZ4 format requires to consume all
    934  1.1  chs 				 * input at this stage
    935  1.1  chs 				 */
    936  1.1  chs 				goto _output_error;
    937  1.1  chs 			(void) memcpy(op, ip, length);
    938  1.1  chs 			op += length;
    939  1.1  chs 			/* Necessarily EOF, due to parsing restrictions */
    940  1.1  chs 			break;
    941  1.1  chs 		}
    942  1.1  chs 		LZ4_WILDCOPY(ip, op, cpy);
    943  1.1  chs 		ip -= (op - cpy);
    944  1.1  chs 		op = cpy;
    945  1.1  chs 
    946  1.1  chs 		/* get offset */
    947  1.1  chs 		LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip);
    948  1.1  chs 		ip += 2;
    949  1.1  chs 		if (ref < (BYTE * const) dest)
    950  1.1  chs 			/*
    951  1.1  chs 			 * Error: offset creates reference outside of
    952  1.1  chs 			 * destination buffer
    953  1.1  chs 			 */
    954  1.1  chs 			goto _output_error;
    955  1.1  chs 
    956  1.1  chs 		/* get matchlength */
    957  1.1  chs 		if ((length = (token & ML_MASK)) == ML_MASK) {
    958  1.1  chs 			while (ip < iend) {
    959  1.1  chs 				int s = *ip++;
    960  1.1  chs 				length += s;
    961  1.1  chs 				if (s == 255)
    962  1.1  chs 					continue;
    963  1.1  chs 				break;
    964  1.1  chs 			}
    965  1.1  chs 		}
    966  1.1  chs 		/* copy repeated sequence */
    967  1.1  chs 		if unlikely(op - ref < STEPSIZE) {
    968  1.1  chs #if LZ4_ARCH64
    969  1.1  chs 			size_t dec64 = dec64table[op-ref];
    970  1.1  chs #else
    971  1.1  chs 			const int dec64 = 0;
    972  1.1  chs #endif
    973  1.1  chs 			op[0] = ref[0];
    974  1.1  chs 			op[1] = ref[1];
    975  1.1  chs 			op[2] = ref[2];
    976  1.1  chs 			op[3] = ref[3];
    977  1.1  chs 			op += 4;
    978  1.1  chs 			ref += 4;
    979  1.1  chs 			ref -= dec32table[op-ref];
    980  1.1  chs 			A32(op) = A32(ref);
    981  1.1  chs 			op += STEPSIZE - 4;
    982  1.1  chs 			ref -= dec64;
    983  1.1  chs 		} else {
    984  1.1  chs 			LZ4_COPYSTEP(ref, op);
    985  1.1  chs 		}
    986  1.1  chs 		cpy = op + length - (STEPSIZE - 4);
    987  1.1  chs 		if (cpy > oend - COPYLENGTH) {
    988  1.1  chs 			if (cpy > oend)
    989  1.1  chs 				/*
    990  1.1  chs 				 * Error: request to write outside of
    991  1.1  chs 				 * destination buffer
    992  1.1  chs 				 */
    993  1.1  chs 				goto _output_error;
    994  1.1  chs 			LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH));
    995  1.1  chs 			while (op < cpy)
    996  1.1  chs 				*op++ = *ref++;
    997  1.1  chs 			op = cpy;
    998  1.1  chs 			if (op == oend)
    999  1.1  chs 				/*
   1000  1.1  chs 				 * Check EOF (should never happen, since
   1001  1.1  chs 				 * last 5 bytes are supposed to be literals)
   1002  1.1  chs 				 */
   1003  1.1  chs 				goto _output_error;
   1004  1.1  chs 			continue;
   1005  1.1  chs 		}
   1006  1.1  chs 		LZ4_SECURECOPY(ref, op, cpy);
   1007  1.1  chs 		op = cpy;	/* correction */
   1008  1.1  chs 	}
   1009  1.1  chs 
   1010  1.1  chs 	/* end of decoding */
   1011  1.1  chs 	return (int)(((char *)op) - dest);
   1012  1.1  chs 
   1013  1.1  chs 	/* write overflow error detected */
   1014  1.1  chs 	_output_error:
   1015  1.1  chs 	return (int)(-(((char *)ip) - source));
   1016  1.1  chs }
   1017  1.1  chs 
   1018  1.2  chs #ifdef __FreeBSD__
   1019  1.2  chs 
   1020  1.1  chs extern void
   1021  1.1  chs lz4_init(void)
   1022  1.1  chs {
   1023  1.1  chs 
   1024  1.1  chs #if HEAPMODE
   1025  1.1  chs 	lz4_ctx_cache = kmem_cache_create("lz4_ctx", sizeof(struct refTables),
   1026  1.1  chs 	    0, NULL, NULL, NULL, NULL, NULL, 0);
   1027  1.1  chs #endif
   1028  1.1  chs }
   1029  1.1  chs 
   1030  1.1  chs extern void
   1031  1.1  chs lz4_fini(void)
   1032  1.1  chs {
   1033  1.1  chs 
   1034  1.1  chs #if HEAPMODE
   1035  1.1  chs 	kmem_cache_destroy(lz4_ctx_cache);
   1036  1.1  chs #endif
   1037  1.1  chs }
   1038  1.2  chs 
   1039  1.2  chs #endif /* __FreeBSD__ */
   1040