benchmark silo added
[c11concurrency-benchmarks.git] / silo / third-party / lz4 / lz4.c
diff --git a/silo/third-party/lz4/lz4.c b/silo/third-party/lz4/lz4.c
new file mode 100644 (file)
index 0000000..ab35806
--- /dev/null
@@ -0,0 +1,703 @@
+/*\r
+   LZ4 - Fast LZ compression algorithm\r
+   Copyright (C) 2011-2013, Yann Collet.\r
+   BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)\r
+\r
+   Redistribution and use in source and binary forms, with or without\r
+   modification, are permitted provided that the following conditions are\r
+   met:\r
+\r
+       * Redistributions of source code must retain the above copyright\r
+   notice, this list of conditions and the following disclaimer.\r
+       * Redistributions in binary form must reproduce the above\r
+   copyright notice, this list of conditions and the following disclaimer\r
+   in the documentation and/or other materials provided with the\r
+   distribution.\r
+\r
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
+   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
+   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+   You can contact the author at :\r
+   - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html\r
+   - LZ4 source repository : http://code.google.com/p/lz4/\r
+*/\r
+\r
+/*\r
+Note : this source file requires "lz4_encoder.h"\r
+*/\r
+\r
+//**************************************\r
+// Tuning parameters\r
+//**************************************\r
+// MEMORY_USAGE :\r
+// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)\r
+// Increasing memory usage improves compression ratio\r
+// Reduced memory usage can improve speed, due to cache effect\r
+// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache\r
+#define MEMORY_USAGE 14\r
+\r
+// HEAPMODE :\r
+// Select how default compression function will allocate memory for its hash table,\r
+// in memory stack (0:default, fastest), or in memory heap (1:requires memory allocation (malloc)).\r
+// Default allocation strategy is to use stack (HEAPMODE 0)\r
+// Note : explicit functions *_stack* and *_heap* are unaffected by this setting\r
+#define HEAPMODE 0\r
+\r
+// BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE :\r
+// This will provide a small boost to performance for big endian cpu, but the resulting compressed stream will be incompatible with little-endian CPU.\r
+// You can set this option to 1 in situations where data will remain within closed environment\r
+// This option is useless on Little_Endian CPU (such as x86)\r
+//#define BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE 1\r
+\r
+\r
+\r
+//**************************************\r
+// CPU Feature Detection\r
+//**************************************\r
+// 32 or 64 bits ?\r
+#if (defined(__x86_64__) || defined(_M_X64) || defined(_WIN64) \\r
+  || defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \\r
+  || defined(__64BIT__) || defined(_LP64) || defined(__LP64__) \\r
+  || defined(__ia64) || defined(__itanium__) || defined(_M_IA64) )   // Detects 64 bits mode\r
+#  define LZ4_ARCH64 1\r
+#else\r
+#  define LZ4_ARCH64 0\r
+#endif\r
+\r
+// Little Endian or Big Endian ?\r
+// Overwrite the #define below if you know your architecture endianess\r
+#if defined (__GLIBC__)\r
+#  include <endian.h>\r
+#  if (__BYTE_ORDER == __BIG_ENDIAN)\r
+#     define LZ4_BIG_ENDIAN 1\r
+#  endif\r
+#elif (defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(__LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN))\r
+#  define LZ4_BIG_ENDIAN 1\r
+#elif defined(__sparc) || defined(__sparc__) \\r
+   || defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) \\r
+   || defined(__hpux)  || defined(__hppa) \\r
+   || defined(_MIPSEB) || defined(__s390__)\r
+#  define LZ4_BIG_ENDIAN 1\r
+#else\r
+// Little Endian assumed. PDP Endian and other very rare endian format are unsupported.\r
+#endif\r
+\r
+// Unaligned memory access is automatically enabled for "common" CPU, such as x86.\r
+// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected\r
+// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance\r
+#if defined(__ARM_FEATURE_UNALIGNED)\r
+#  define LZ4_FORCE_UNALIGNED_ACCESS 1\r
+#endif\r
+\r
+// Define this parameter if your target system or compiler does not support hardware bit count\r
+#if defined(_MSC_VER) && defined(_WIN32_WCE)            // Visual Studio for Windows CE does not support Hardware bit count\r
+#  define LZ4_FORCE_SW_BITCOUNT\r
+#endif\r
+\r
+\r
+//**************************************\r
+// Compiler Options\r
+//**************************************\r
+#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   // C99\r
+/* "restrict" is a known keyword */\r
+#else\r
+#  define restrict // Disable restrict\r
+#endif\r
+\r
+#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)\r
+\r
+#ifdef _MSC_VER    // Visual Studio\r
+#  define forceinline static __forceinline\r
+#  include <intrin.h>                 // For Visual 2005\r
+#  if LZ4_ARCH64     // 64-bits\r
+#    pragma intrinsic(_BitScanForward64) // For Visual 2005\r
+#    pragma intrinsic(_BitScanReverse64) // For Visual 2005\r
+#  else              // 32-bits\r
+#    pragma intrinsic(_BitScanForward)   // For Visual 2005\r
+#    pragma intrinsic(_BitScanReverse)   // For Visual 2005\r
+#  endif\r
+#  pragma warning(disable : 4127)        // disable: C4127: conditional expression is constant\r
+#else\r
+#  ifdef __GNUC__\r
+#    define forceinline static inline __attribute__((always_inline))\r
+#  else\r
+#    define forceinline static inline\r
+#  endif\r
+#endif\r
+\r
+#ifdef _MSC_VER\r
+#  define lz4_bswap16(x) _byteswap_ushort(x)\r
+#else\r
+#  define lz4_bswap16(x) ((unsigned short int) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))\r
+#endif\r
+\r
+#if (GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__)\r
+#  define expect(expr,value)    (__builtin_expect ((expr),(value)) )\r
+#else\r
+#  define expect(expr,value)    (expr)\r
+#endif\r
+\r
+#define likely(expr)     expect((expr) != 0, 1)\r
+#define unlikely(expr)   expect((expr) != 0, 0)\r
+\r
+\r
+//**************************************\r
+// Includes\r
+//**************************************\r
+#include <stdlib.h>   // for malloc\r
+#include <string.h>   // for memset\r
+#include "lz4.h"\r
+\r
+\r
+//**************************************\r
+// Basic Types\r
+//**************************************\r
+#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   // C99\r
+# include <stdint.h>\r
+  typedef uint8_t  BYTE;\r
+  typedef uint16_t U16;\r
+  typedef uint32_t U32;\r
+  typedef  int32_t S32;\r
+  typedef uint64_t U64;\r
+#else\r
+  typedef unsigned char       BYTE;\r
+  typedef unsigned short      U16;\r
+  typedef unsigned int        U32;\r
+  typedef   signed int        S32;\r
+  typedef unsigned long long  U64;\r
+#endif\r
+\r
+#if defined(__GNUC__)  && !defined(LZ4_FORCE_UNALIGNED_ACCESS)\r
+#  define _PACKED __attribute__ ((packed))\r
+#else\r
+#  define _PACKED\r
+#endif\r
+\r
+#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)\r
+#  ifdef __IBMC__\r
+#    pragma pack(1)\r
+#  else\r
+#    pragma pack(push, 1)\r
+#  endif\r
+#endif\r
+\r
+typedef struct _U16_S { U16 v; } _PACKED U16_S;\r
+typedef struct _U32_S { U32 v; } _PACKED U32_S;\r
+typedef struct _U64_S { U64 v; } _PACKED U64_S;\r
+\r
+#if !defined(LZ4_FORCE_UNALIGNED_ACCESS) && !defined(__GNUC__)\r
+#  pragma pack(pop)\r
+#endif\r
+\r
+#define A64(x) (((U64_S *)(x))->v)\r
+#define A32(x) (((U32_S *)(x))->v)\r
+#define A16(x) (((U16_S *)(x))->v)\r
+\r
+\r
+//**************************************\r
+// Constants\r
+//**************************************\r
+#define HASHTABLESIZE (1 << MEMORY_USAGE)\r
+\r
+#define MINMATCH 4\r
+\r
+#define COPYLENGTH 8\r
+#define LASTLITERALS 5\r
+#define MFLIMIT (COPYLENGTH+MINMATCH)\r
+#define MINLENGTH (MFLIMIT+1)\r
+\r
+#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT-1))\r
+#define SKIPSTRENGTH 6     // Increasing this value will make the compression run slower on incompressible data\r
+\r
+#define MAXD_LOG 16\r
+#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)\r
+\r
+#define ML_BITS  4\r
+#define ML_MASK  ((1U<<ML_BITS)-1)\r
+#define RUN_BITS (8-ML_BITS)\r
+#define RUN_MASK ((1U<<RUN_BITS)-1)\r
+\r
+\r
+//**************************************\r
+// Architecture-specific macros\r
+//**************************************\r
+#if LZ4_ARCH64   // 64-bit\r
+#  define STEPSIZE 8\r
+#  define UARCH U64\r
+#  define AARCH A64\r
+#  define LZ4_COPYSTEP(s,d)       A64(d) = A64(s); d+=8; s+=8;\r
+#  define LZ4_COPYPACKET(s,d)     LZ4_COPYSTEP(s,d)\r
+#  define LZ4_SECURECOPY(s,d,e)   if (d<e) LZ4_WILDCOPY(s,d,e)\r
+#  define HTYPE                   U32\r
+#  define INITBASE(base)          const BYTE* const base = ip\r
+#else      // 32-bit\r
+#  define STEPSIZE 4\r
+#  define UARCH U32\r
+#  define AARCH A32\r
+#  define LZ4_COPYSTEP(s,d)       A32(d) = A32(s); d+=4; s+=4;\r
+#  define LZ4_COPYPACKET(s,d)     LZ4_COPYSTEP(s,d); LZ4_COPYSTEP(s,d);\r
+#  define LZ4_SECURECOPY          LZ4_WILDCOPY\r
+#  define HTYPE                   const BYTE*\r
+#  define INITBASE(base)          const int base = 0\r
+#endif\r
+\r
+#if (defined(LZ4_BIG_ENDIAN) && !defined(BIG_ENDIAN_NATIVE_BUT_INCOMPATIBLE))\r
+#  define LZ4_READ_LITTLEENDIAN_16(d,s,p) { U16 v = A16(p); v = lz4_bswap16(v); d = (s) - v; }\r
+#  define LZ4_WRITE_LITTLEENDIAN_16(p,i)  { U16 v = (U16)(i); v = lz4_bswap16(v); A16(p) = v; p+=2; }\r
+#else      // Little Endian\r
+#  define LZ4_READ_LITTLEENDIAN_16(d,s,p) { d = (s) - A16(p); }\r
+#  define LZ4_WRITE_LITTLEENDIAN_16(p,v)  { A16(p) = v; p+=2; }\r
+#endif\r
+\r
+\r
+//**************************************\r
+// Macros\r
+//**************************************\r
+#define LZ4_WILDCOPY(s,d,e)     do { LZ4_COPYPACKET(s,d) } while (d<e);\r
+#define LZ4_BLINDCOPY(s,d,l)    { BYTE* e=(d)+(l); LZ4_WILDCOPY(s,d,e); d=e; }\r
+\r
+\r
+//****************************\r
+// Private functions\r
+//****************************\r
+#if LZ4_ARCH64\r
+\r
+forceinline int LZ4_NbCommonBytes (register U64 val)\r
+{\r
+#if defined(LZ4_BIG_ENDIAN)\r
+    #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    unsigned long r = 0;\r
+    _BitScanReverse64( &r, val );\r
+    return (int)(r>>3);\r
+    #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    return (__builtin_clzll(val) >> 3);\r
+    #else\r
+    int r;\r
+    if (!(val>>32)) { r=4; } else { r=0; val>>=32; }\r
+    if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; }\r
+    r += (!val);\r
+    return r;\r
+    #endif\r
+#else\r
+    #if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    unsigned long r = 0;\r
+    _BitScanForward64( &r, val );\r
+    return (int)(r>>3);\r
+    #elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    return (__builtin_ctzll(val) >> 3);\r
+    #else\r
+    static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };\r
+    return DeBruijnBytePos[((U64)((val & -val) * 0x0218A392CDABBD3F)) >> 58];\r
+    #endif\r
+#endif\r
+}\r
+\r
+#else\r
+\r
+forceinline int LZ4_NbCommonBytes (register U32 val)\r
+{\r
+#if defined(LZ4_BIG_ENDIAN)\r
+#  if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    unsigned long r = 0;\r
+    _BitScanReverse( &r, val );\r
+    return (int)(r>>3);\r
+#  elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    return (__builtin_clz(val) >> 3);\r
+#  else\r
+    int r;\r
+    if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; }\r
+    r += (!val);\r
+    return r;\r
+#  endif\r
+#else\r
+#  if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    unsigned long r;\r
+    _BitScanForward( &r, val );\r
+    return (int)(r>>3);\r
+#  elif defined(__GNUC__) && (GCC_VERSION >= 304) && !defined(LZ4_FORCE_SW_BITCOUNT)\r
+    return (__builtin_ctz(val) >> 3);\r
+#  else\r
+    static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };\r
+    return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];\r
+#  endif\r
+#endif\r
+}\r
+\r
+#endif\r
+\r
+\r
+\r
+//******************************\r
+// Compression functions\r
+//******************************\r
+\r
+/*\r
+int LZ4_compress_stack(\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest'.\r
+Destination buffer must be already allocated, and sized at a minimum of LZ4_compressBound(inputSize).\r
+return : the number of bytes written in buffer 'dest'\r
+*/\r
+#define FUNCTION_NAME LZ4_compress_stack\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress_stack_limitedOutput(\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize,\r
+                 int maxOutputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.\r
+If it cannot achieve it, compression will stop, and result of the function will be zero.\r
+return : the number of bytes written in buffer 'dest', or 0 if the compression fails\r
+*/\r
+#define FUNCTION_NAME LZ4_compress_stack_limitedOutput\r
+#define LIMITED_OUTPUT\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress64k_stack(\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest'.\r
+This function compresses better than LZ4_compress_stack(), on the condition that\r
+'inputSize' must be < to LZ4_64KLIMIT, or the function will fail.\r
+Destination buffer must be already allocated, and sized at a minimum of LZ4_compressBound(inputSize).\r
+return : the number of bytes written in buffer 'dest', or 0 if compression fails\r
+*/\r
+#define FUNCTION_NAME LZ4_compress64k_stack\r
+#define COMPRESS_64K\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress64k_stack_limitedOutput(\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize,\r
+                 int maxOutputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.\r
+This function compresses better than LZ4_compress_stack_limitedOutput(), on the condition that\r
+'inputSize' must be < to LZ4_64KLIMIT, or the function will fail.\r
+If it cannot achieve it, compression will stop, and result of the function will be zero.\r
+return : the number of bytes written in buffer 'dest', or 0 if the compression fails\r
+*/\r
+#define FUNCTION_NAME LZ4_compress64k_stack_limitedOutput\r
+#define COMPRESS_64K\r
+#define LIMITED_OUTPUT\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+void* LZ4_createHeapMemory();\r
+int LZ4_freeHeapMemory(void* ctx);\r
+\r
+Used to allocate and free hashTable memory\r
+to be used by the LZ4_compress_heap* family of functions.\r
+LZ4_createHeapMemory() returns NULL is memory allocation fails.\r
+*/\r
+void*    LZ4_create() { return malloc(HASHTABLESIZE); }\r
+unsigned LZ4_create_size() { return HASHTABLESIZE; }\r
+int      LZ4_free(void* ctx) { free(ctx); return 0; }\r
+\r
+\r
+/*\r
+int LZ4_compress_heap(\r
+                 void* ctx,\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest'.\r
+The memory used for compression must be created by LZ4_createHeapMemory() and provided by pointer 'ctx'.\r
+Destination buffer must be already allocated, and sized at a minimum of LZ4_compressBound(inputSize).\r
+return : the number of bytes written in buffer 'dest'\r
+*/\r
+#define FUNCTION_NAME LZ4_compress_heap\r
+#define USE_HEAPMEMORY\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress_heap_limitedOutput(\r
+                 void* ctx,\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize,\r
+                 int maxOutputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.\r
+If it cannot achieve it, compression will stop, and result of the function will be zero.\r
+The memory used for compression must be created by LZ4_createHeapMemory() and provided by pointer 'ctx'.\r
+return : the number of bytes written in buffer 'dest', or 0 if the compression fails\r
+*/\r
+#define FUNCTION_NAME LZ4_compress_heap_limitedOutput\r
+#define LIMITED_OUTPUT\r
+#define USE_HEAPMEMORY\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress64k_heap(\r
+                 void* ctx,\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest'.\r
+The memory used for compression must be created by LZ4_createHeapMemory() and provided by pointer 'ctx'.\r
+'inputSize' must be < to LZ4_64KLIMIT, or the function will fail.\r
+Destination buffer must be already allocated, and sized at a minimum of LZ4_compressBound(inputSize).\r
+return : the number of bytes written in buffer 'dest'\r
+*/\r
+#define FUNCTION_NAME LZ4_compress64k_heap\r
+#define COMPRESS_64K\r
+#define USE_HEAPMEMORY\r
+#include "lz4_encoder.h"\r
+\r
+\r
+/*\r
+int LZ4_compress64k_heap_limitedOutput(\r
+                 void* ctx,\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize,\r
+                 int maxOutputSize)\r
+\r
+Compress 'inputSize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.\r
+If it cannot achieve it, compression will stop, and result of the function will be zero.\r
+The memory used for compression must be created by LZ4_createHeapMemory() and provided by pointer 'ctx'.\r
+'inputSize' must be < to LZ4_64KLIMIT, or the function will fail.\r
+return : the number of bytes written in buffer 'dest', or 0 if the compression fails\r
+*/\r
+#define FUNCTION_NAME LZ4_compress64k_heap_limitedOutput\r
+#define COMPRESS_64K\r
+#define LIMITED_OUTPUT\r
+#define USE_HEAPMEMORY\r
+#include "lz4_encoder.h"\r
+\r
+\r
+int LZ4_compress(const char* source, char* dest, int inputSize)\r
+{\r
+#if HEAPMODE\r
+    void* ctx = LZ4_create();\r
+    int result;\r
+    if (ctx == NULL) return 0;    // Failed allocation => compression not done\r
+    if (inputSize < LZ4_64KLIMIT)\r
+        result = LZ4_compress64k_heap(ctx, source, dest, inputSize);\r
+    else result = LZ4_compress_heap(ctx, source, dest, inputSize);\r
+    LZ4_free(ctx);\r
+    return result;\r
+#else\r
+    if (inputSize < (int)LZ4_64KLIMIT) return LZ4_compress64k_stack(source, dest, inputSize);\r
+    return LZ4_compress_stack(source, dest, inputSize);\r
+#endif\r
+}\r
+\r
+\r
+int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize)\r
+{\r
+#if HEAPMODE\r
+    void* ctx = LZ4_create();\r
+    int result;\r
+    if (ctx == NULL) return 0;    // Failed allocation => compression not done\r
+    if (inputSize < LZ4_64KLIMIT)\r
+        result = LZ4_compress64k_heap_limitedOutput(ctx, source, dest, inputSize, maxOutputSize);\r
+    else result = LZ4_compress_heap_limitedOutput(ctx, source, dest, inputSize, maxOutputSize);\r
+    LZ4_free(ctx);\r
+    return result;\r
+#else\r
+    if (inputSize < (int)LZ4_64KLIMIT) return LZ4_compress64k_stack_limitedOutput(source, dest, inputSize, maxOutputSize);\r
+    return LZ4_compress_stack_limitedOutput(source, dest, inputSize, maxOutputSize);\r
+#endif\r
+}\r
+\r
+\r
+//****************************\r
+// Decompression functions\r
+//****************************\r
+\r
+typedef enum { noPrefix = 0, withPrefix = 1 } prefix64k_directive;\r
+typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } end_directive;\r
+typedef enum { full = 0, partial = 1 } exit_directive;\r
+\r
+\r
+// This generic decompression function cover all use cases.\r
+// It shall be instanciated several times, using different sets of directives\r
+// Note that it is essential this generic function is really inlined,\r
+// in order to remove useless branches during compilation optimisation.\r
+forceinline int LZ4_decompress_generic(\r
+                 const char* source,\r
+                 char* dest,\r
+                 int inputSize,          //\r
+                 int outputSize,         // OutputSize must be != 0; if endOnInput==endOnInputSize, this value is the max size of Output Buffer.\r
+\r
+                 int endOnInput,         // endOnOutputSize, endOnInputSize\r
+                 int prefix64k,          // noPrefix, withPrefix\r
+                 int partialDecoding,    // full, partial\r
+                 int targetOutputSize    // only used if partialDecoding==partial\r
+                 )\r
+{\r
+    // Local Variables\r
+    const BYTE* restrict ip = (const BYTE*) source;\r
+    const BYTE* ref;\r
+    const BYTE* const iend = ip + inputSize;\r
+\r
+    BYTE* op = (BYTE*) dest;\r
+    BYTE* const oend = op + outputSize;\r
+    BYTE* cpy;\r
+    BYTE* oexit = op + targetOutputSize;\r
+\r
+    size_t dec32table[] = {0, 3, 2, 3, 0, 0, 0, 0};\r
+#if LZ4_ARCH64\r
+    size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3};\r
+#endif\r
+\r
+\r
+    // Special case\r
+    if ((partialDecoding) && (oexit> oend-MFLIMIT)) oexit = oend-MFLIMIT;                        // targetOutputSize too high => decode everything\r
+    if ((endOnInput) && unlikely(outputSize==0)) return ((inputSize==1) && (*ip==0)) ? 0 : -1;   // Empty output buffer\r
+    if ((!endOnInput) && unlikely(outputSize==0)) return (*ip==0?1:-1);\r
+\r
+\r
+    // Main Loop\r
+    while (1)\r
+    {\r
+        unsigned token;\r
+        size_t length;\r
+\r
+        // get runlength\r
+        token = *ip++;\r
+        if ((length=(token>>ML_BITS)) == RUN_MASK)\r
+        {\r
+            unsigned s=255;\r
+            while (((endOnInput)?ip<iend:1) && (s==255))\r
+            {\r
+                s = *ip++;\r
+                length += s;\r
+            }\r
+        }\r
+\r
+        // copy literals\r
+        cpy = op+length;\r
+        if (((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )\r
+            || ((!endOnInput) && (cpy>oend-COPYLENGTH)))\r
+        {\r
+            if (partialDecoding)\r
+            {\r
+                if (cpy > oend) goto _output_error;                            // Error : write attempt beyond end of output buffer\r
+                if ((endOnInput) && (ip+length > iend)) goto _output_error;    // Error : read attempt beyond end of input buffer\r
+            }\r
+            else\r
+            {\r
+                if ((!endOnInput) && (cpy != oend)) goto _output_error;        // Error : block decoding must stop exactly there\r
+                if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) goto _output_error;   // Error : input must be consumed\r
+            }\r
+            memcpy(op, ip, length);\r
+            ip += length;\r
+            op += length;\r
+            break;                                       // Necessarily EOF, due to parsing restrictions\r
+        }\r
+        LZ4_WILDCOPY(ip, op, cpy); ip -= (op-cpy); op = cpy;\r
+\r
+        // get offset\r
+        LZ4_READ_LITTLEENDIAN_16(ref,cpy,ip); ip+=2;\r
+        if ((prefix64k==noPrefix) && unlikely(ref < (BYTE* const)dest)) goto _output_error;   // Error : offset outside destination buffer\r
+\r
+        // get matchlength\r
+        if ((length=(token&ML_MASK)) == ML_MASK)\r
+        {\r
+            for ( ; (!endOnInput) || (ip<iend-(LASTLITERALS+1)) ; )   // Ensure enough bytes remain for LASTLITERALS + token\r
+            {\r
+                unsigned s = *ip++;\r
+                length += s;\r
+                if (s==255) continue;\r
+                break;\r
+            }\r
+        }\r
+\r
+        // copy repeated sequence\r
+        if unlikely((op-ref)<STEPSIZE)\r
+        {\r
+#if LZ4_ARCH64\r
+            size_t dec64 = dec64table[op-ref];\r
+#else\r
+            const size_t dec64 = 0;\r
+#endif\r
+            op[0] = ref[0];\r
+            op[1] = ref[1];\r
+            op[2] = ref[2];\r
+            op[3] = ref[3];\r
+            op += 4, ref += 4; ref -= dec32table[op-ref];\r
+            A32(op) = A32(ref);\r
+            op += STEPSIZE-4; ref -= dec64;\r
+        } else { LZ4_COPYSTEP(ref,op); }\r
+        cpy = op + length - (STEPSIZE-4);\r
+\r
+        if unlikely(cpy>oend-(COPYLENGTH)-(STEPSIZE-4))\r
+        {\r
+            if (cpy > oend-LASTLITERALS) goto _output_error;    // Error : last 5 bytes must be literals\r
+            LZ4_SECURECOPY(ref, op, (oend-COPYLENGTH));\r
+            while(op<cpy) *op++=*ref++;\r
+            op=cpy;\r
+            continue;\r
+        }\r
+        LZ4_WILDCOPY(ref, op, cpy);\r
+        op=cpy;   // correction\r
+    }\r
+\r
+    // end of decoding\r
+    if (endOnInput)\r
+       return (int) (((char*)op)-dest);     // Nb of output bytes decoded\r
+    else\r
+       return (int) (((char*)ip)-source);   // Nb of input bytes read\r
+\r
+    // Overflow error detected\r
+_output_error:\r
+    return (int) (-(((char*)ip)-source))-1;\r
+}\r
+\r
+\r
+int LZ4_decompress_safe(const char* source, char* dest, int inputSize, int maxOutputSize)\r
+{\r
+    return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, noPrefix, full, 0);\r
+}\r
+\r
+int LZ4_decompress_fast(const char* source, char* dest, int outputSize)\r
+{\r
+    return LZ4_decompress_generic(source, dest, 0, outputSize, endOnOutputSize, noPrefix, full, 0);\r
+}\r
+\r
+int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int inputSize, int maxOutputSize)\r
+{\r
+    return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, withPrefix, full, 0);\r
+}\r
+\r
+int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int outputSize)\r
+{\r
+    return LZ4_decompress_generic(source, dest, 0, outputSize, endOnOutputSize, withPrefix, full, 0);\r
+}\r
+\r
+int LZ4_decompress_safe_partial(const char* source, char* dest, int inputSize, int targetOutputSize, int maxOutputSize)\r
+{\r
+    return LZ4_decompress_generic(source, dest, inputSize, maxOutputSize, endOnInputSize, noPrefix, partial, targetOutputSize);\r
+}\r
+\r