lz4defs.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #ifndef __LZ4DEFS_H__
  2. #define __LZ4DEFS_H__
  3. /*
  4. * lz4defs.h -- common and architecture specific defines for the kernel usage
  5. * LZ4 - Fast LZ compression algorithm
  6. * Copyright (C) 2011-2016, Yann Collet.
  7. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  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. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. * You can contact the author at :
  29. * - LZ4 homepage : http://www.lz4.org
  30. * - LZ4 source repository : https://github.com/lz4/lz4
  31. *
  32. * Changed for kernel usage by:
  33. * Sven Schmidt <[email protected]>
  34. */
  35. #include <asm/unaligned.h>
  36. #include <linux/bitops.h>
  37. #include <linux/string.h> /* memset, memcpy */
  38. #define FORCE_INLINE __always_inline
  39. /*-************************************
  40. * Basic Types
  41. **************************************/
  42. #include <linux/types.h>
  43. typedef uint8_t BYTE;
  44. typedef uint16_t U16;
  45. typedef uint32_t U32;
  46. typedef int32_t S32;
  47. typedef uint64_t U64;
  48. typedef uintptr_t uptrval;
  49. /*-************************************
  50. * Architecture specifics
  51. **************************************/
  52. #if defined(CONFIG_64BIT)
  53. #define LZ4_ARCH64 1
  54. #else
  55. #define LZ4_ARCH64 0
  56. #endif
  57. #if defined(__LITTLE_ENDIAN)
  58. #define LZ4_LITTLE_ENDIAN 1
  59. #else
  60. #define LZ4_LITTLE_ENDIAN 0
  61. #endif
  62. /*-************************************
  63. * Constants
  64. **************************************/
  65. #define MINMATCH 4
  66. #define WILDCOPYLENGTH 8
  67. #define LASTLITERALS 5
  68. #define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
  69. /*
  70. * ensure it's possible to write 2 x wildcopyLength
  71. * without overflowing output buffer
  72. */
  73. #define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH)
  74. /* Increase this value ==> compression run slower on incompressible data */
  75. #define LZ4_SKIPTRIGGER 6
  76. #define HASH_UNIT sizeof(size_t)
  77. #define KB (1 << 10)
  78. #define MB (1 << 20)
  79. #define GB (1U << 30)
  80. #define MAXD_LOG 16
  81. #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
  82. #define STEPSIZE sizeof(size_t)
  83. #define ML_BITS 4
  84. #define ML_MASK ((1U << ML_BITS) - 1)
  85. #define RUN_BITS (8 - ML_BITS)
  86. #define RUN_MASK ((1U << RUN_BITS) - 1)
  87. /*-************************************
  88. * Reading and writing into memory
  89. **************************************/
  90. static FORCE_INLINE U16 LZ4_read16(const void *ptr)
  91. {
  92. return get_unaligned((const U16 *)ptr);
  93. }
  94. static FORCE_INLINE U32 LZ4_read32(const void *ptr)
  95. {
  96. return get_unaligned((const U32 *)ptr);
  97. }
  98. static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
  99. {
  100. return get_unaligned((const size_t *)ptr);
  101. }
  102. static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
  103. {
  104. put_unaligned(value, (U16 *)memPtr);
  105. }
  106. static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
  107. {
  108. put_unaligned(value, (U32 *)memPtr);
  109. }
  110. static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
  111. {
  112. return get_unaligned_le16(memPtr);
  113. }
  114. static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
  115. {
  116. return put_unaligned_le16(value, memPtr);
  117. }
  118. /*
  119. * LZ4 relies on memcpy with a constant size being inlined. In freestanding
  120. * environments, the compiler can't assume the implementation of memcpy() is
  121. * standard compliant, so apply its specialized memcpy() inlining logic. When
  122. * possible, use __builtin_memcpy() to tell the compiler to analyze memcpy()
  123. * as-if it were standard compliant, so it can inline it in freestanding
  124. * environments. This is needed when decompressing the Linux Kernel, for example.
  125. */
  126. #define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
  127. #define LZ4_memmove(dst, src, size) __builtin_memmove(dst, src, size)
  128. static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
  129. {
  130. #if LZ4_ARCH64
  131. U64 a = get_unaligned((const U64 *)src);
  132. put_unaligned(a, (U64 *)dst);
  133. #else
  134. U32 a = get_unaligned((const U32 *)src);
  135. U32 b = get_unaligned((const U32 *)src + 1);
  136. put_unaligned(a, (U32 *)dst);
  137. put_unaligned(b, (U32 *)dst + 1);
  138. #endif
  139. }
  140. /*
  141. * customized variant of memcpy,
  142. * which can overwrite up to 7 bytes beyond dstEnd
  143. */
  144. static FORCE_INLINE void LZ4_wildCopy(void *dstPtr,
  145. const void *srcPtr, void *dstEnd)
  146. {
  147. BYTE *d = (BYTE *)dstPtr;
  148. const BYTE *s = (const BYTE *)srcPtr;
  149. BYTE *const e = (BYTE *)dstEnd;
  150. do {
  151. LZ4_copy8(d, s);
  152. d += 8;
  153. s += 8;
  154. } while (d < e);
  155. }
  156. static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
  157. {
  158. #if LZ4_LITTLE_ENDIAN
  159. return __ffs(val) >> 3;
  160. #else
  161. return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
  162. #endif
  163. }
  164. static FORCE_INLINE unsigned int LZ4_count(
  165. const BYTE *pIn,
  166. const BYTE *pMatch,
  167. const BYTE *pInLimit)
  168. {
  169. const BYTE *const pStart = pIn;
  170. while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
  171. size_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
  172. if (!diff) {
  173. pIn += STEPSIZE;
  174. pMatch += STEPSIZE;
  175. continue;
  176. }
  177. pIn += LZ4_NbCommonBytes(diff);
  178. return (unsigned int)(pIn - pStart);
  179. }
  180. #if LZ4_ARCH64
  181. if ((pIn < (pInLimit - 3))
  182. && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
  183. pIn += 4;
  184. pMatch += 4;
  185. }
  186. #endif
  187. if ((pIn < (pInLimit - 1))
  188. && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
  189. pIn += 2;
  190. pMatch += 2;
  191. }
  192. if ((pIn < pInLimit) && (*pMatch == *pIn))
  193. pIn++;
  194. return (unsigned int)(pIn - pStart);
  195. }
  196. typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive;
  197. typedef enum { byPtr, byU32, byU16 } tableType_t;
  198. typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
  199. typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
  200. typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
  201. typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
  202. #define LZ4_STATIC_ASSERT(c) BUILD_BUG_ON(!(c))
  203. #endif