2 * LZ4 - Fast LZ compression algorithm
4 * Copyright (C) 2011-2013, Yann Collet.
5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * You can contact the author at :
31 * - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
32 * - LZ4 source repository : http://code.google.com/p/lz4/
35 #include <sys/zfs_context.h>
37 static int real_LZ4_compress(const char *source
, char *dest
, int isize
,
39 static int real_LZ4_uncompress(const char *source
, char *dest
, int osize
);
40 static int LZ4_compressBound(int isize
);
41 static int LZ4_uncompress_unknownOutputSize(const char *source
, char *dest
,
42 int isize
, int maxOutputSize
);
43 static int LZ4_compressCtx(void *ctx
, const char *source
, char *dest
,
44 int isize
, int osize
);
45 static int LZ4_compress64kCtx(void *ctx
, const char *source
, char *dest
,
46 int isize
, int osize
);
50 lz4_compress(void *s_start
, void *d_start
, size_t s_len
, size_t d_len
, int n
)
55 ASSERT(d_len
>= sizeof (bufsiz
));
57 bufsiz
= real_LZ4_compress(s_start
, &dest
[sizeof (bufsiz
)], s_len
,
58 d_len
- sizeof (bufsiz
));
60 /* Signal an error if the compression routine returned zero. */
65 * Encode the compresed buffer size at the start. We'll need this in
66 * decompression to counter the effects of padding which might be
67 * added to the compressed buffer and which, if unhandled, would
68 * confuse the hell out of our decompression function.
70 *(uint32_t *)dest
= BE_32(bufsiz
);
72 return (bufsiz
+ sizeof (bufsiz
));
77 lz4_decompress(void *s_start
, void *d_start
, size_t s_len
, size_t d_len
, int n
)
79 const char *src
= s_start
;
80 uint32_t bufsiz
= BE_IN32(src
);
82 /* invalid compressed buffer size encoded at start */
83 if (bufsiz
+ sizeof (bufsiz
) > s_len
)
87 * Returns 0 on success (decompression function returned non-negative)
88 * and non-zero on failure (decompression function returned negative.
90 return (LZ4_uncompress_unknownOutputSize(&src
[sizeof (bufsiz
)],
91 d_start
, bufsiz
, d_len
) < 0);
95 * LZ4 API Description:
98 * real_LZ4_compress() :
99 * isize : is the input size. Max supported value is ~1.9GB
100 * return : the number of bytes written in buffer dest
101 * or 0 if the compression fails (if LZ4_COMPRESSMIN is set).
102 * note : destination buffer must be already allocated.
103 * destination buffer must be sized to handle worst cases
104 * situations (input data not compressible) worst case size
105 * evaluation is provided by function LZ4_compressBound().
107 * real_LZ4_uncompress() :
108 * osize : is the output size, therefore the original size
109 * return : the number of bytes read in the source buffer.
110 * If the source stream is malformed, the function will stop
111 * decoding and return a negative result, indicating the byte
112 * position of the faulty instruction. This function never
113 * writes beyond dest + osize, and is therefore protected
114 * against malicious data packets.
115 * note : destination buffer must be already allocated
119 * LZ4_compressBound() :
120 * Provides the maximum size that LZ4 may output in a "worst case"
121 * scenario (input data not compressible) primarily useful for memory
122 * allocation of output buffer.
124 * isize : is the input size. Max supported value is ~1.9GB
125 * return : maximum output size in a "worst case" scenario
126 * note : this function is limited by "int" range (2^31-1)
128 * LZ4_uncompress_unknownOutputSize() :
129 * isize : is the input size, therefore the compressed size
130 * maxOutputSize : is the size of the destination buffer (which must be
132 * return : the number of bytes decoded in the destination buffer
133 * (necessarily <= maxOutputSize). If the source stream is
134 * malformed, the function will stop decoding and return a
135 * negative result, indicating the byte position of the faulty
136 * instruction. This function never writes beyond dest +
137 * maxOutputSize, and is therefore protected against malicious
139 * note : Destination buffer must be already allocated.
140 * This version is slightly slower than real_LZ4_uncompress()
142 * LZ4_compressCtx() :
143 * This function explicitly handles the CTX memory structure.
145 * ILLUMOS CHANGES: the CTX memory structure must be explicitly allocated
146 * by the caller (either on the stack or using kmem_zalloc). Passing NULL
149 * LZ4_compress64kCtx() :
150 * Same as LZ4_compressCtx(), but specific to small inputs (<64KB).
151 * isize *Must* be <64KB, otherwise the output will be corrupted.
153 * ILLUMOS CHANGES: the CTX memory structure must be explicitly allocated
154 * by the caller (either on the stack or using kmem_zalloc). Passing NULL
163 * COMPRESSIONLEVEL: Increasing this value improves compression ratio
164 * Lowering this value reduces memory usage. Reduced memory usage
165 * typically improves speed, due to cache effect (ex: L1 32KB for Intel,
166 * L1 64KB for AMD). Memory usage formula : N->2^(N+2) Bytes
167 * (examples : 12 -> 16KB ; 17 -> 512KB)
169 #define COMPRESSIONLEVEL 12
172 * NOTCOMPRESSIBLE_CONFIRMATION: Decreasing this value will make the
173 * algorithm skip faster data segments considered "incompressible".
174 * This may decrease compression ratio dramatically, but will be
175 * faster on incompressible data. Increasing this value will make
176 * the algorithm search more before declaring a segment "incompressible".
177 * This could improve compression a bit, but will be slower on
178 * incompressible data. The default value (6) is recommended.
180 #define NOTCOMPRESSIBLE_CONFIRMATION 6
183 * BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE: This will provide a boost to
184 * performance for big endian cpu, but the resulting compressed stream
185 * will be incompatible with little-endian CPU. You can set this option
186 * to 1 in situations where data will stay within closed environment.
187 * This option is useless on Little_Endian CPU (such as x86).
189 /* #define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1 */
192 * CPU Feature Detection
195 /* 32 or 64 bits ? */
196 #if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || \
197 defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || \
198 defined(__LP64__) || defined(_LP64))
205 * Limits the amount of stack space that the algorithm may consume to hold
206 * the compression lookup table. The value `9' here means we'll never use
207 * more than 2k of stack (see above for a description of COMPRESSIONLEVEL).
208 * If more memory is needed, it is allocated from the heap.
213 * Little Endian or Big Endian?
214 * Note: overwrite the below #define if you know your architecture endianess.
216 #if (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || \
217 defined(_BIG_ENDIAN) || defined(_ARCH_PPC) || defined(__PPC__) || \
218 defined(__PPC) || defined(PPC) || defined(__powerpc__) || \
219 defined(__powerpc) || defined(powerpc) || \
220 ((defined(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))))
221 #define LZ4_BIG_ENDIAN 1
224 * Little Endian assumed. PDP Endian and other very rare endian format
230 * Unaligned memory access is automatically enabled for "common" CPU,
231 * such as x86. For others CPU, the compiler will be more cautious, and
232 * insert extra code to ensure aligned access is respected. If you know
233 * your target CPU supports unaligned memory access, you may want to
234 * force this option manually to improve performance
236 #if defined(__ARM_FEATURE_UNALIGNED)
237 #define LZ4_FORCE_UNALIGNED_ACCESS 1
241 #define LZ4_FORCE_SW_BITCOUNT
247 #if __STDC_VERSION__ >= 199901L /* C99 */
248 /* "restrict" is a known keyword */
250 /* Disable restrict */
254 #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
258 /* Visual is not C99, but supports some kind of inline */
259 #define inline __forceinline
261 /* For Visual 2005 */
262 #pragma intrinsic(_BitScanForward64)
263 #pragma intrinsic(_BitScanReverse64)
264 #else /* !LZ4_ARCH64 */
265 /* For Visual 2005 */
266 #pragma intrinsic(_BitScanForward)
267 #pragma intrinsic(_BitScanReverse)
268 #endif /* !LZ4_ARCH64 */
269 #endif /* _MSC_VER */
272 #define lz4_bswap16(x) _byteswap_ushort(x)
273 #else /* !_MSC_VER */
274 #define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | \
275 (((x) & 0xffu) << 8)))
276 #endif /* !_MSC_VER */
278 #if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)
279 #define expect(expr, value) (__builtin_expect((expr), (value)))
281 #define expect(expr, value) (expr)
284 #define likely(expr) expect((expr) != 0, 1)
285 #define unlikely(expr) expect((expr) != 0, 0)
288 #if defined(_MSC_VER)
289 /* Visual Studio does not support 'stdint' natively */
290 #define BYTE unsigned __int8
291 #define U16 unsigned __int16
292 #define U32 unsigned __int32
294 #define U64 unsigned __int64
295 #else /* !defined(_MSC_VER) */
301 #endif /* !defined(_MSC_VER) */
303 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
307 typedef struct _U16_S
{
310 typedef struct _U32_S
{
313 typedef struct _U64_S
{
317 #ifndef LZ4_FORCE_UNALIGNED_ACCESS
321 #define A64(x) (((U64_S *)(x))->v)
322 #define A32(x) (((U32_S *)(x))->v)
323 #define A16(x) (((U16_S *)(x))->v)
330 #define HASH_LOG COMPRESSIONLEVEL
331 #define HASHTABLESIZE (1 << HASH_LOG)
332 #define HASH_MASK (HASHTABLESIZE - 1)
334 #define SKIPSTRENGTH (NOTCOMPRESSIBLE_CONFIRMATION > 2 ? \
335 NOTCOMPRESSIBLE_CONFIRMATION : 2)
338 * Defines if memory is allocated into the stack (local variable),
339 * or into the heap (kmem_alloc()).
341 #define HEAPMODE (HASH_LOG > STACKLIMIT)
343 #define LASTLITERALS 5
344 #define MFLIMIT (COPYLENGTH + MINMATCH)
345 #define MINLENGTH (MFLIMIT + 1)
348 #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
351 #define ML_MASK ((1U<<ML_BITS)-1)
352 #define RUN_BITS (8-ML_BITS)
353 #define RUN_MASK ((1U<<RUN_BITS)-1)
357 * Architecture-specific macros
363 #define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8;
364 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
365 #define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e)
367 #define INITBASE(base) const BYTE* const base = ip
368 #else /* !LZ4_ARCH64 */
372 #define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4;
373 #define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d);
374 #define LZ4_SECURECOPY LZ4_WILDCOPY
375 #define HTYPE const BYTE *
376 #define INITBASE(base) const int base = 0
377 #endif /* !LZ4_ARCH64 */
379 #if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))
380 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
381 { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }
382 #define LZ4_WRITE_LITTLEENDIAN_16(p, i) \
383 { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p += 2; }
385 #define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - A16(p); }
386 #define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = v; p += 2; }
390 /* Local structures */
392 HTYPE hashTable
[HASHTABLESIZE
];
397 #define LZ4_HASH_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH * 8) - \
399 #define LZ4_HASH_VALUE(p) LZ4_HASH_FUNCTION(A32(p))
400 #define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e);
401 #define LZ4_BLINDCOPY(s, d, l) { BYTE* e = (d) + l; LZ4_WILDCOPY(s, d, e); \
405 /* Private functions */
409 LZ4_NbCommonBytes(register U64 val
)
411 #if defined(LZ4_BIG_ENDIAN)
412 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
414 _BitScanReverse64(&r
, val
);
415 return (int)(r
>> 3);
416 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && \
417 !defined(LZ4_FORCE_SW_BITCOUNT)
418 return (__builtin_clzll(val
) >> 3);
437 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
439 _BitScanForward64(&r
, val
);
440 return (int)(r
>> 3);
441 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && \
442 !defined(LZ4_FORCE_SW_BITCOUNT)
443 return (__builtin_ctzll(val
) >> 3);
445 static const int DeBruijnBytePos
[64] =
446 { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5,
447 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5,
448 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4,
449 4, 5, 7, 2, 6, 5, 7, 6, 7, 7
451 return DeBruijnBytePos
[((U64
) ((val
& -val
) * 0x0218A392CDABBD3F)) >>
460 LZ4_NbCommonBytes(register U32 val
)
462 #if defined(LZ4_BIG_ENDIAN)
463 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
465 _BitScanReverse(&r
, val
);
466 return (int)(r
>> 3);
467 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && \
468 !defined(LZ4_FORCE_SW_BITCOUNT)
469 return (__builtin_clz(val
) >> 3);
483 #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
485 _BitScanForward(&r
, val
);
486 return (int)(r
>> 3);
487 #elif defined(__GNUC__) && (GCC_VERSION >= 304) && \
488 !defined(LZ4_FORCE_SW_BITCOUNT)
489 return (__builtin_ctz(val
) >> 3);
491 static const int DeBruijnBytePos
[32] = {
492 0, 0, 3, 0, 3, 1, 3, 0,
493 3, 2, 2, 1, 3, 2, 0, 1,
494 3, 3, 1, 2, 2, 2, 2, 0,
495 3, 1, 2, 0, 1, 0, 1, 1
497 return DeBruijnBytePos
[((U32
) ((val
& -(S32
) val
) * 0x077CB531U
)) >>
505 /* Public functions */
508 LZ4_compressBound(int isize
)
510 return (isize
+ (isize
/ 255) + 16);
513 /* Compression functions */
517 LZ4_compressCtx(void *ctx
, const char *source
, char *dest
, int isize
,
521 struct refTables
*srt
= (struct refTables
*)ctx
;
522 HTYPE
*HashTable
= (HTYPE
*) (srt
->hashTable
);
524 HTYPE HashTable
[HASHTABLESIZE
] = { 0 };
527 const BYTE
*ip
= (BYTE
*) source
;
529 const BYTE
*anchor
= ip
;
530 const BYTE
*const iend
= ip
+ isize
;
531 const BYTE
*const oend
= (BYTE
*) dest
+ osize
;
532 const BYTE
*const mflimit
= iend
- MFLIMIT
;
533 #define matchlimit (iend - LASTLITERALS)
535 BYTE
*op
= (BYTE
*) dest
;
538 const int skipStrength
= SKIPSTRENGTH
;
543 if (isize
< MINLENGTH
)
547 HashTable
[LZ4_HASH_VALUE(ip
)] = ip
- base
;
549 forwardH
= LZ4_HASH_VALUE(ip
);
553 int findMatchAttempts
= (1U << skipStrength
) + 3;
554 const BYTE
*forwardIp
= ip
;
561 int step
= findMatchAttempts
++ >> skipStrength
;
563 forwardIp
= ip
+ step
;
565 if unlikely(forwardIp
> mflimit
) {
569 forwardH
= LZ4_HASH_VALUE(forwardIp
);
570 ref
= base
+ HashTable
[h
];
571 HashTable
[h
] = ip
- base
;
573 } while ((ref
< ip
- MAX_DISTANCE
) || (A32(ref
) != A32(ip
)));
576 while ((ip
> anchor
) && (ref
> (BYTE
*) source
) &&
577 unlikely(ip
[-1] == ref
[-1])) {
582 /* Encode Literal length */
583 length
= ip
- anchor
;
586 /* Check output limit */
587 if unlikely(op
+ length
+ (2 + 1 + LASTLITERALS
) +
588 (length
>> 8) > oend
)
591 if (length
>= (int)RUN_MASK
) {
592 *token
= (RUN_MASK
<< ML_BITS
);
593 len
= length
- RUN_MASK
;
594 for (; len
> 254; len
-= 255)
598 *token
= (length
<< ML_BITS
);
601 LZ4_BLINDCOPY(anchor
, op
, length
);
605 LZ4_WRITE_LITTLEENDIAN_16(op
, ip
- ref
);
609 ref
+= MINMATCH
; /* MinMatch verified */
611 while likely(ip
< matchlimit
- (STEPSIZE
- 1)) {
612 UARCH diff
= AARCH(ref
) ^ AARCH(ip
);
618 ip
+= LZ4_NbCommonBytes(diff
);
622 if ((ip
< (matchlimit
- 3)) && (A32(ref
) == A32(ip
))) {
627 if ((ip
< (matchlimit
- 1)) && (A16(ref
) == A16(ip
))) {
631 if ((ip
< matchlimit
) && (*ref
== *ip
))
635 /* Encode MatchLength */
637 /* Check output limit */
638 if unlikely(op
+ (1 + LASTLITERALS
) + (len
>> 8) > oend
)
640 if (len
>= (int)ML_MASK
) {
643 for (; len
> 509; len
-= 510) {
655 /* Test end of chunk */
661 HashTable
[LZ4_HASH_VALUE(ip
- 2)] = ip
- 2 - base
;
663 /* Test next position */
664 ref
= base
+ HashTable
[LZ4_HASH_VALUE(ip
)];
665 HashTable
[LZ4_HASH_VALUE(ip
)] = ip
- base
;
666 if ((ref
> ip
- (MAX_DISTANCE
+ 1)) && (A32(ref
) == A32(ip
))) {
671 /* Prepare next loop */
673 forwardH
= LZ4_HASH_VALUE(ip
);
677 /* Encode Last Literals */
679 int lastRun
= iend
- anchor
;
680 if (op
+ lastRun
+ 1 + ((lastRun
+ 255 - RUN_MASK
) / 255) >
683 if (lastRun
>= (int)RUN_MASK
) {
684 *op
++ = (RUN_MASK
<< ML_BITS
);
686 for (; lastRun
> 254; lastRun
-= 255) {
689 *op
++ = (BYTE
)lastRun
;
691 *op
++ = (lastRun
<< ML_BITS
);
692 (void) memcpy(op
, anchor
, iend
- anchor
);
697 return (int)(((char *)op
) - dest
);
702 /* Note : this function is valid only if isize < LZ4_64KLIMIT */
703 #define LZ4_64KLIMIT ((1 << 16) + (MFLIMIT - 1))
704 #define HASHLOG64K (HASH_LOG + 1)
705 #define HASH64KTABLESIZE (1U << HASHLOG64K)
706 #define LZ4_HASH64K_FUNCTION(i) (((i) * 2654435761U) >> ((MINMATCH*8) - \
708 #define LZ4_HASH64K_VALUE(p) LZ4_HASH64K_FUNCTION(A32(p))
712 LZ4_compress64kCtx(void *ctx
, const char *source
, char *dest
, int isize
,
716 struct refTables
*srt
= (struct refTables
*)ctx
;
717 U16
*HashTable
= (U16
*) (srt
->hashTable
);
719 U16 HashTable
[HASH64KTABLESIZE
] = { 0 };
722 const BYTE
*ip
= (BYTE
*) source
;
723 const BYTE
*anchor
= ip
;
724 const BYTE
*const base
= ip
;
725 const BYTE
*const iend
= ip
+ isize
;
726 const BYTE
*const oend
= (BYTE
*) dest
+ osize
;
727 const BYTE
*const mflimit
= iend
- MFLIMIT
;
728 #define matchlimit (iend - LASTLITERALS)
730 BYTE
*op
= (BYTE
*) dest
;
733 const int skipStrength
= SKIPSTRENGTH
;
737 if (isize
< MINLENGTH
)
742 forwardH
= LZ4_HASH64K_VALUE(ip
);
746 int findMatchAttempts
= (1U << skipStrength
) + 3;
747 const BYTE
*forwardIp
= ip
;
754 int step
= findMatchAttempts
++ >> skipStrength
;
756 forwardIp
= ip
+ step
;
758 if (forwardIp
> mflimit
) {
762 forwardH
= LZ4_HASH64K_VALUE(forwardIp
);
763 ref
= base
+ HashTable
[h
];
764 HashTable
[h
] = ip
- base
;
766 } while (A32(ref
) != A32(ip
));
769 while ((ip
> anchor
) && (ref
> (BYTE
*) source
) &&
770 (ip
[-1] == ref
[-1])) {
775 /* Encode Literal length */
776 length
= ip
- anchor
;
779 /* Check output limit */
780 if unlikely(op
+ length
+ (2 + 1 + LASTLITERALS
) +
781 (length
>> 8) > oend
)
784 if (length
>= (int)RUN_MASK
) {
785 *token
= (RUN_MASK
<< ML_BITS
);
786 len
= length
- RUN_MASK
;
787 for (; len
> 254; len
-= 255)
791 *token
= (length
<< ML_BITS
);
794 LZ4_BLINDCOPY(anchor
, op
, length
);
798 LZ4_WRITE_LITTLEENDIAN_16(op
, ip
- ref
);
802 ref
+= MINMATCH
; /* MinMatch verified */
804 while (ip
< matchlimit
- (STEPSIZE
- 1)) {
805 UARCH diff
= AARCH(ref
) ^ AARCH(ip
);
811 ip
+= LZ4_NbCommonBytes(diff
);
815 if ((ip
< (matchlimit
- 3)) && (A32(ref
) == A32(ip
))) {
820 if ((ip
< (matchlimit
- 1)) && (A16(ref
) == A16(ip
))) {
824 if ((ip
< matchlimit
) && (*ref
== *ip
))
828 /* Encode MatchLength */
830 /* Check output limit */
831 if unlikely(op
+ (1 + LASTLITERALS
) + (len
>> 8) > oend
)
833 if (len
>= (int)ML_MASK
) {
836 for (; len
> 509; len
-= 510) {
848 /* Test end of chunk */
854 HashTable
[LZ4_HASH64K_VALUE(ip
- 2)] = ip
- 2 - base
;
856 /* Test next position */
857 ref
= base
+ HashTable
[LZ4_HASH64K_VALUE(ip
)];
858 HashTable
[LZ4_HASH64K_VALUE(ip
)] = ip
- base
;
859 if (A32(ref
) == A32(ip
)) {
864 /* Prepare next loop */
866 forwardH
= LZ4_HASH64K_VALUE(ip
);
870 /* Encode Last Literals */
872 int lastRun
= iend
- anchor
;
873 if (op
+ lastRun
+ 1 + ((lastRun
+ 255 - RUN_MASK
) / 255) >
876 if (lastRun
>= (int)RUN_MASK
) {
877 *op
++ = (RUN_MASK
<< ML_BITS
);
879 for (; lastRun
> 254; lastRun
-= 255)
881 *op
++ = (BYTE
)lastRun
;
883 *op
++ = (lastRun
<< ML_BITS
);
884 (void) memcpy(op
, anchor
, iend
- anchor
);
889 return (int)(((char *)op
) - dest
);
893 real_LZ4_compress(const char *source
, char *dest
, int isize
, int osize
)
896 void *ctx
= kmem_zalloc(sizeof (struct refTables
), KM_NOSLEEP
);
900 * out of kernel memory, gently fall through - this will disable
901 * compression in zio_compress_data
906 if (isize
< LZ4_64KLIMIT
)
907 result
= LZ4_compress64kCtx(ctx
, source
, dest
, isize
, osize
);
909 result
= LZ4_compressCtx(ctx
, source
, dest
, isize
, osize
);
911 kmem_free(ctx
, sizeof (struct refTables
));
914 if (isize
< (int)LZ4_64KLIMIT
)
915 return (LZ4_compress64kCtx(NULL
, source
, dest
, isize
, osize
));
916 return (LZ4_compressCtx(NULL
, source
, dest
, isize
, osize
));
920 /* Decompression functions */
923 * Note: The decoding functions real_LZ4_uncompress() and
924 * LZ4_uncompress_unknownOutputSize() are safe against "buffer overflow"
925 * attack type. They will never write nor read outside of the provided
926 * output buffers. LZ4_uncompress_unknownOutputSize() also insures that
927 * it will never read outside of the input buffer. A corrupted input
928 * will produce an error result, a negative int, indicating the position
929 * of the error within input stream.
933 real_LZ4_uncompress(const char *source
, char *dest
, int osize
)
935 /* Local Variables */
936 const BYTE
*restrict ip
= (const BYTE
*) source
;
939 BYTE
*op
= (BYTE
*) dest
;
940 BYTE
*const oend
= op
+ osize
;
946 size_t dec32table
[] = {0, 3, 2, 3, 0, 0, 0, 0};
948 size_t dec64table
[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
955 if ((length
= (token
>> ML_BITS
)) == RUN_MASK
) {
957 for (; (len
= *ip
++) == 255; length
+= 255) {
963 /* CORNER-CASE: cpy might overflow. */
965 goto _output_error
; /* cpy was overflowed, bail! */
966 if unlikely(cpy
> oend
- COPYLENGTH
) {
968 /* Error: we must necessarily stand at EOF */
970 (void) memcpy(op
, ip
, length
);
974 LZ4_WILDCOPY(ip
, op
, cpy
);
979 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
981 if unlikely(ref
< (BYTE
* const) dest
)
983 * Error: offset create reference outside destination
988 /* get matchlength */
989 if ((length
= (token
& ML_MASK
)) == ML_MASK
) {
990 for (; *ip
== 255; length
+= 255) {
995 /* copy repeated sequence */
996 if unlikely(op
- ref
< STEPSIZE
) {
998 size_t dec64
= dec64table
[op
-ref
];
1000 const int dec64
= 0;
1008 ref
-= dec32table
[op
-ref
];
1013 LZ4_COPYSTEP(ref
, op
);
1015 cpy
= op
+ length
- (STEPSIZE
- 4);
1016 if (cpy
> oend
- COPYLENGTH
) {
1019 * Error: request to write beyond destination
1023 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
1029 * Check EOF (should never happen, since last
1030 * 5 bytes are supposed to be literals)
1035 LZ4_SECURECOPY(ref
, op
, cpy
);
1036 op
= cpy
; /* correction */
1039 /* end of decoding */
1040 return (int)(((char *)ip
) - source
);
1042 /* write overflow error detected */
1044 return (int)(-(((char *)ip
) - source
));
1048 LZ4_uncompress_unknownOutputSize(const char *source
, char *dest
, int isize
,
1051 /* Local Variables */
1052 const BYTE
*restrict ip
= (const BYTE
*) source
;
1053 const BYTE
*const iend
= ip
+ isize
;
1056 BYTE
*op
= (BYTE
*) dest
;
1057 BYTE
*const oend
= op
+ maxOutputSize
;
1060 size_t dec32table
[] = {0, 3, 2, 3, 0, 0, 0, 0};
1062 size_t dec64table
[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};
1072 if ((length
= (token
>> ML_BITS
)) == RUN_MASK
) {
1074 while ((ip
< iend
) && (s
== 255)) {
1081 /* CORNER-CASE: cpy might overflow. */
1083 goto _output_error
; /* cpy was overflowed, bail! */
1084 if ((cpy
> oend
- COPYLENGTH
) ||
1085 (ip
+ length
> iend
- COPYLENGTH
)) {
1087 /* Error: writes beyond output buffer */
1089 if (ip
+ length
!= iend
)
1091 * Error: LZ4 format requires to consume all
1092 * input at this stage
1095 (void) memcpy(op
, ip
, length
);
1097 /* Necessarily EOF, due to parsing restrictions */
1100 LZ4_WILDCOPY(ip
, op
, cpy
);
1105 LZ4_READ_LITTLEENDIAN_16(ref
, cpy
, ip
);
1107 if (ref
< (BYTE
* const) dest
)
1109 * Error: offset creates reference outside of
1110 * destination buffer
1114 /* get matchlength */
1115 if ((length
= (token
& ML_MASK
)) == ML_MASK
) {
1124 /* copy repeated sequence */
1125 if unlikely(op
- ref
< STEPSIZE
) {
1127 size_t dec64
= dec64table
[op
-ref
];
1129 const int dec64
= 0;
1137 ref
-= dec32table
[op
-ref
];
1142 LZ4_COPYSTEP(ref
, op
);
1144 cpy
= op
+ length
- (STEPSIZE
- 4);
1145 if (cpy
> oend
- COPYLENGTH
) {
1148 * Error: request to write outside of
1149 * destination buffer
1152 LZ4_SECURECOPY(ref
, op
, (oend
- COPYLENGTH
));
1158 * Check EOF (should never happen, since
1159 * last 5 bytes are supposed to be literals)
1164 LZ4_SECURECOPY(ref
, op
, cpy
);
1165 op
= cpy
; /* correction */
1168 /* end of decoding */
1169 return (int)(((char *)op
) - dest
);
1171 /* write overflow error detected */
1173 return (int)(-(((char *)ip
) - source
));