1/* 2 * LZ4 - Fast LZ compression algorithm 3 * Header File 4 * Copyright (C) 2011-2013, Yann Collet. 5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 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 16 * distribution. 17 * 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. 29 * 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/ 33 * 34 * $FreeBSD$ 35 */ 36 37#include <arpa/inet.h> 38 39static int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, 40 int isize, int maxOutputSize); 41 42/* ARGSUSED */ 43static int 44lz4_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int dummy __unused) 45{ 46 const uint8_t *src = s_start; 47 uint32_t bufsiz = htonl(*(uint32_t *)src); 48 49 /* invalid compressed buffer size encoded at start */ 50 if (bufsiz + 4 > s_len) 51 return (1); 52 53 /* 54 * Returns 0 on success (decompression function returned non-negative) 55 * and non-zero on failure (decompression function returned negative). 56 */ 57 return (LZ4_uncompress_unknownOutputSize((const char *)s_start + 4, d_start, bufsiz, 58 d_len) < 0); 59} 60 61/* 62 * CPU Feature Detection 63 */ 64 65/* 32 or 64 bits ? */ 66#if (defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || \ 67 defined(__amd64) || defined(__ppc64__) || defined(_WIN64) || \ 68 defined(__LP64__) || defined(_LP64)) 69#define LZ4_ARCH64 1 70#else 71#define LZ4_ARCH64 0 72#endif 73 74/* 75 * Little Endian or Big Endian? 76 * Note: overwrite the below #define if you know your architecture endianess. 77 */ 78#if BYTE_ORDER == BIG_ENDIAN 79#define LZ4_BIG_ENDIAN 1 80#else 81 /* 82 * Little Endian assumed. PDP Endian and other very rare endian format 83 * are unsupported. 84 */ 85#endif 86 87/* 88 * Unaligned memory access is automatically enabled for "common" CPU, 89 * such as x86. For others CPU, the compiler will be more cautious, and 90 * insert extra code to ensure aligned access is respected. If you know 91 * your target CPU supports unaligned memory access, you may want to 92 * force this option manually to improve performance 93 */ 94#if defined(__ARM_FEATURE_UNALIGNED) 95#define LZ4_FORCE_UNALIGNED_ACCESS 1 96#endif 97 98/* 99 * Compiler Options 100 */ 101#if __STDC_VERSION__ >= 199901L /* C99 */ 102/* "restrict" is a known keyword */ 103#else 104/* Disable restrict */ 105#define restrict 106#endif 107 108#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) 109 110#define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) \ 111 | (((x) & 0xffu) << 8))) 112 113#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__) 114#define expect(expr, value) (__builtin_expect((expr), (value))) 115#else 116#define expect(expr, value) (expr) 117#endif 118 119#define likely(expr) expect((expr) != 0, 1) 120#define unlikely(expr) expect((expr) != 0, 0) 121 122/* Basic types */ 123#define BYTE uint8_t 124#define U16 uint16_t 125#define U32 uint32_t 126#define S32 int32_t 127#define U64 uint64_t 128 129#ifndef LZ4_FORCE_UNALIGNED_ACCESS 130#pragma pack(1) 131#endif 132 133typedef struct _U16_S { 134 U16 v; 135} U16_S; 136typedef struct _U32_S { 137 U32 v; 138} U32_S; 139typedef struct _U64_S { 140 U64 v; 141} U64_S; 142 143#ifndef LZ4_FORCE_UNALIGNED_ACCESS 144#pragma pack() 145#endif 146 147#define A64(x) (((U64_S *)(x))->v) 148#define A32(x) (((U32_S *)(x))->v) 149#define A16(x) (((U16_S *)(x))->v) 150 151/* 152 * Constants 153 */ 154#define MINMATCH 4 155 156#define COPYLENGTH 8 157#define LASTLITERALS 5 158 159#define ML_BITS 4 160#define ML_MASK ((1U<<ML_BITS)-1) 161#define RUN_BITS (8-ML_BITS) 162#define RUN_MASK ((1U<<RUN_BITS)-1) 163 164/* 165 * Architecture-specific macros 166 */ 167#if LZ4_ARCH64 168#define STEPSIZE 8 169#define UARCH U64 170#define AARCH A64 171#define LZ4_COPYSTEP(s, d) A64(d) = A64(s); d += 8; s += 8; 172#define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d) 173#define LZ4_SECURECOPY(s, d, e) if (d < e) LZ4_WILDCOPY(s, d, e) 174#define HTYPE U32 175#define INITBASE(base) const BYTE* const base = ip 176#else 177#define STEPSIZE 4 178#define UARCH U32 179#define AARCH A32 180#define LZ4_COPYSTEP(s, d) A32(d) = A32(s); d += 4; s += 4; 181#define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d); LZ4_COPYSTEP(s, d); 182#define LZ4_SECURECOPY LZ4_WILDCOPY 183#define HTYPE const BYTE* 184#define INITBASE(base) const int base = 0 185#endif 186 187#if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE)) 188#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \ 189 { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; } 190#define LZ4_WRITE_LITTLEENDIAN_16(p, i) \ 191 { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p += 2; } 192#else 193#define LZ4_READ_LITTLEENDIAN_16(d, s, p) { d = (s) - A16(p); } 194#define LZ4_WRITE_LITTLEENDIAN_16(p, v) { A16(p) = v; p += 2; } 195#endif 196 197/* Macros */ 198#define LZ4_WILDCOPY(s, d, e) do { LZ4_COPYPACKET(s, d) } while (d < e); 199 200/* Decompression functions */ 201 202static int 203LZ4_uncompress_unknownOutputSize(const char *source, 204 char *dest, int isize, int maxOutputSize) 205{ 206 /* Local Variables */ 207 const BYTE *restrict ip = (const BYTE *) source; 208 const BYTE *const iend = ip + isize; 209 const BYTE *restrict ref; 210 211 BYTE *restrict op = (BYTE *) dest; 212 BYTE *const oend = op + maxOutputSize; 213 BYTE *cpy; 214 215 size_t dec[] = { 0, 3, 2, 3, 0, 0, 0, 0 }; 216 217 /* Main Loop */ 218 while (ip < iend) { 219 BYTE token; 220 int length; 221 222 /* get runlength */ 223 token = *ip++; 224 if ((length = (token >> ML_BITS)) == RUN_MASK) { 225 int s = 255; 226 while ((ip < iend) && (s == 255)) { 227 s = *ip++; 228 length += s; 229 } 230 } 231 /* copy literals */ 232 cpy = op + length; 233 if ((cpy > oend - COPYLENGTH) || 234 (ip + length > iend - COPYLENGTH)) { 235 if (cpy > oend) 236 /* 237 * Error: request to write beyond destination 238 * buffer. 239 */ 240 goto _output_error; 241 if (ip + length > iend) 242 /* 243 * Error : request to read beyond source 244 * buffer. 245 */ 246 goto _output_error; 247 memcpy(op, ip, length); 248 op += length; 249 ip += length; 250 if (ip < iend) 251 /* Error : LZ4 format violation */ 252 goto _output_error; 253 /* Necessarily EOF, due to parsing restrictions. */ 254 break; 255 } 256 LZ4_WILDCOPY(ip, op, cpy); 257 ip -= (op - cpy); 258 op = cpy; 259 260 /* get offset */ 261 LZ4_READ_LITTLEENDIAN_16(ref, cpy, ip); 262 ip += 2; 263 if (ref < (BYTE * const) dest) 264 /* 265 * Error: offset creates reference outside of 266 * destination buffer. 267 */ 268 goto _output_error; 269 270 /* get matchlength */ 271 if ((length = (token & ML_MASK)) == ML_MASK) { 272 while (ip < iend) { 273 int s = *ip++; 274 length += s; 275 if (s == 255) 276 continue; 277 break; 278 } 279 } 280 /* copy repeated sequence */ 281 if unlikely(op - ref < STEPSIZE) { 282#if LZ4_ARCH64 283 size_t dec2table[] = { 0, 0, 0, -1, 0, 1, 2, 3 }; 284 size_t dec2 = dec2table[op - ref]; 285#else 286 const int dec2 = 0; 287#endif 288 *op++ = *ref++; 289 *op++ = *ref++; 290 *op++ = *ref++; 291 *op++ = *ref++; 292 ref -= dec[op - ref]; 293 A32(op) = A32(ref); 294 op += STEPSIZE - 4; 295 ref -= dec2; 296 } else { 297 LZ4_COPYSTEP(ref, op); 298 } 299 cpy = op + length - (STEPSIZE - 4); 300 if (cpy > oend - COPYLENGTH) { 301 if (cpy > oend) 302 /* 303 * Error: request to write outside of 304 * destination buffer. 305 */ 306 goto _output_error; 307 LZ4_SECURECOPY(ref, op, (oend - COPYLENGTH)); 308 while (op < cpy) 309 *op++ = *ref++; 310 op = cpy; 311 if (op == oend) 312 /* 313 * Check EOF (should never happen, since last 314 * 5 bytes are supposed to be literals). 315 */ 316 break; 317 continue; 318 } 319 LZ4_SECURECOPY(ref, op, cpy); 320 op = cpy; /* correction */ 321 } 322 323 /* end of decoding */ 324 return (int)(((char *)op) - dest); 325 326 /* write overflow error detected */ 327 _output_error: 328 return (int)(-(((char *)ip) - source)); 329} 330