extzstd 0.0.3.CONCEPT → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -1,78 +1,35 @@
1
1
  /* ******************************************************************
2
- FSE : Finite State Entropy decoder
3
- Copyright (C) 2013-2015, Yann Collet.
4
-
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
- - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
32
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
2
+ * FSE : Finite State Entropy decoder
3
+ * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ *
5
+ * You can contact the author at :
6
+ * - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
7
+ * - Public forum : https://groups.google.com/forum/#!forum/lz4c
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
33
13
  ****************************************************************** */
34
14
 
35
15
 
36
- /* **************************************************************
37
- * Compiler specifics
38
- ****************************************************************/
39
- #ifdef _MSC_VER /* Visual Studio */
40
- # define FORCE_INLINE static __forceinline
41
- # include <intrin.h> /* For Visual 2005 */
42
- # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
43
- # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
44
- #else
45
- # ifdef __GNUC__
46
- # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
47
- # define FORCE_INLINE static inline __attribute__((always_inline))
48
- # else
49
- # define FORCE_INLINE static inline
50
- # endif
51
- #endif
52
-
53
-
54
16
  /* **************************************************************
55
17
  * Includes
56
18
  ****************************************************************/
57
19
  #include <stdlib.h> /* malloc, free, qsort */
58
20
  #include <string.h> /* memcpy, memset */
59
- #include <stdio.h> /* printf (debug) */
60
21
  #include "bitstream.h"
22
+ #include "compiler.h"
61
23
  #define FSE_STATIC_LINKING_ONLY
62
24
  #include "fse.h"
25
+ #include "error_private.h"
63
26
 
64
27
 
65
28
  /* **************************************************************
66
29
  * Error Management
67
30
  ****************************************************************/
68
31
  #define FSE_isError ERR_isError
69
- #define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
70
-
71
-
72
- /* **************************************************************
73
- * Complex types
74
- ****************************************************************/
75
- typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
32
+ #define FSE_STATIC_ASSERT(c) DEBUG_STATIC_ASSERT(c) /* use only *after* variable declarations */
76
33
 
77
34
 
78
35
  /* **************************************************************
@@ -152,7 +109,6 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
152
109
  position = (position + step) & tableMask;
153
110
  while (position > highThreshold) position = (position + step) & tableMask; /* lowprob area */
154
111
  } }
155
-
156
112
  if (position!=0) return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */
157
113
  }
158
114
 
@@ -160,8 +116,8 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
160
116
  { U32 u;
161
117
  for (u=0; u<tableSize; u++) {
162
118
  FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol);
163
- U16 nextState = symbolNext[symbol]++;
164
- tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32 ((U32)nextState) );
119
+ U32 const nextState = symbolNext[symbol]++;
120
+ tableDecode[u].nbBits = (BYTE) (tableLog - BIT_highbit32(nextState) );
165
121
  tableDecode[u].newState = (U16) ( (nextState << tableDecode[u].nbBits) - tableSize);
166
122
  } }
167
123
 
@@ -169,7 +125,6 @@ size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned
169
125
  }
170
126
 
171
127
 
172
-
173
128
  #ifndef FSE_COMMONDEFS_ONLY
174
129
 
175
130
  /*-*******************************************************
@@ -219,7 +174,7 @@ size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits)
219
174
  return 0;
220
175
  }
221
176
 
222
- FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
177
+ FORCE_INLINE_TEMPLATE size_t FSE_decompress_usingDTable_generic(
223
178
  void* dst, size_t maxDstSize,
224
179
  const void* cSrc, size_t cSrcSize,
225
180
  const FSE_DTable* dt, const unsigned fast)
@@ -234,8 +189,7 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
234
189
  FSE_DState_t state2;
235
190
 
236
191
  /* Init */
237
- { size_t const errorCode = BIT_initDStream(&bitD, cSrc, cSrcSize); /* replaced last arg by maxCompressed Size */
238
- if (FSE_isError(errorCode)) return errorCode; }
192
+ CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize));
239
193
 
240
194
  FSE_initDState(&state1, &bitD, dt);
241
195
  FSE_initDState(&state2, &bitD, dt);
@@ -243,7 +197,7 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
243
197
  #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD)
244
198
 
245
199
  /* 4 symbols per loop */
246
- for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) && (op<olimit) ; op+=4) {
200
+ for ( ; (BIT_reloadDStream(&bitD)==BIT_DStream_unfinished) & (op<olimit) ; op+=4) {
247
201
  op[0] = FSE_GETSYMBOL(&state1);
248
202
 
249
203
  if (FSE_MAX_TABLELOG*2+7 > sizeof(bitD.bitContainer)*8) /* This test must be static */
@@ -266,18 +220,14 @@ FORCE_INLINE size_t FSE_decompress_usingDTable_generic(
266
220
  /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */
267
221
  while (1) {
268
222
  if (op>(omax-2)) return ERROR(dstSize_tooSmall);
269
-
270
223
  *op++ = FSE_GETSYMBOL(&state1);
271
-
272
224
  if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
273
225
  *op++ = FSE_GETSYMBOL(&state2);
274
226
  break;
275
227
  }
276
228
 
277
229
  if (op>(omax-2)) return ERROR(dstSize_tooSmall);
278
-
279
230
  *op++ = FSE_GETSYMBOL(&state2);
280
-
281
231
  if (BIT_reloadDStream(&bitD)==BIT_DStream_overflow) {
282
232
  *op++ = FSE_GETSYMBOL(&state1);
283
233
  break;
@@ -301,29 +251,34 @@ size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
301
251
  }
302
252
 
303
253
 
304
- size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
254
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)
305
255
  {
306
256
  const BYTE* const istart = (const BYTE*)cSrc;
307
257
  const BYTE* ip = istart;
308
258
  short counting[FSE_MAX_SYMBOL_VALUE+1];
309
- DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
310
259
  unsigned tableLog;
311
260
  unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
312
261
 
313
- if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
314
-
315
262
  /* normal FSE decoding mode */
316
- { size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
317
- if (FSE_isError(NCountLength)) return NCountLength;
318
- if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
319
- ip += NCountLength;
320
- cSrcSize -= NCountLength;
321
- }
263
+ size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
264
+ if (FSE_isError(NCountLength)) return NCountLength;
265
+ /* if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); */ /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */
266
+ if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
267
+ ip += NCountLength;
268
+ cSrcSize -= NCountLength;
322
269
 
323
- { size_t const errorCode = FSE_buildDTable (dt, counting, maxSymbolValue, tableLog);
324
- if (FSE_isError(errorCode)) return errorCode; }
270
+ CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );
325
271
 
326
- return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); /* always return, even if it is an error code */
272
+ return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */
273
+ }
274
+
275
+
276
+ typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
277
+
278
+ size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)
279
+ {
280
+ DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
281
+ return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);
327
282
  }
328
283
 
329
284
 
@@ -0,0 +1,340 @@
1
+ /* ******************************************************************
2
+ * huff0 huffman codec,
3
+ * part of Finite State Entropy library
4
+ * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
5
+ *
6
+ * You can contact the author at :
7
+ * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
+ *
9
+ * This source code is licensed under both the BSD-style license (found in the
10
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11
+ * in the COPYING file in the root directory of this source tree).
12
+ * You may select, at your option, one of the above-listed licenses.
13
+ ****************************************************************** */
14
+
15
+ #if defined (__cplusplus)
16
+ extern "C" {
17
+ #endif
18
+
19
+ #ifndef HUF_H_298734234
20
+ #define HUF_H_298734234
21
+
22
+ /* *** Dependencies *** */
23
+ #include <stddef.h> /* size_t */
24
+
25
+
26
+ /* *** library symbols visibility *** */
27
+ /* Note : when linking with -fvisibility=hidden on gcc, or by default on Visual,
28
+ * HUF symbols remain "private" (internal symbols for library only).
29
+ * Set macro FSE_DLL_EXPORT to 1 if you want HUF symbols visible on DLL interface */
30
+ #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
31
+ # define HUF_PUBLIC_API __attribute__ ((visibility ("default")))
32
+ #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
33
+ # define HUF_PUBLIC_API __declspec(dllexport)
34
+ #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
35
+ # define HUF_PUBLIC_API __declspec(dllimport) /* not required, just to generate faster code (saves a function pointer load from IAT and an indirect jump) */
36
+ #else
37
+ # define HUF_PUBLIC_API
38
+ #endif
39
+
40
+
41
+ /* ========================== */
42
+ /* *** simple functions *** */
43
+ /* ========================== */
44
+
45
+ /** HUF_compress() :
46
+ * Compress content from buffer 'src', of size 'srcSize', into buffer 'dst'.
47
+ * 'dst' buffer must be already allocated.
48
+ * Compression runs faster if `dstCapacity` >= HUF_compressBound(srcSize).
49
+ * `srcSize` must be <= `HUF_BLOCKSIZE_MAX` == 128 KB.
50
+ * @return : size of compressed data (<= `dstCapacity`).
51
+ * Special values : if return == 0, srcData is not compressible => Nothing is stored within dst !!!
52
+ * if HUF_isError(return), compression failed (more details using HUF_getErrorName())
53
+ */
54
+ HUF_PUBLIC_API size_t HUF_compress(void* dst, size_t dstCapacity,
55
+ const void* src, size_t srcSize);
56
+
57
+ /** HUF_decompress() :
58
+ * Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
59
+ * into already allocated buffer 'dst', of minimum size 'dstSize'.
60
+ * `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
61
+ * Note : in contrast with FSE, HUF_decompress can regenerate
62
+ * RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
63
+ * because it knows size to regenerate (originalSize).
64
+ * @return : size of regenerated data (== originalSize),
65
+ * or an error code, which can be tested using HUF_isError()
66
+ */
67
+ HUF_PUBLIC_API size_t HUF_decompress(void* dst, size_t originalSize,
68
+ const void* cSrc, size_t cSrcSize);
69
+
70
+
71
+ /* *** Tool functions *** */
72
+ #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
73
+ HUF_PUBLIC_API size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
74
+
75
+ /* Error Management */
76
+ HUF_PUBLIC_API unsigned HUF_isError(size_t code); /**< tells if a return value is an error code */
77
+ HUF_PUBLIC_API const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
78
+
79
+
80
+ /* *** Advanced function *** */
81
+
82
+ /** HUF_compress2() :
83
+ * Same as HUF_compress(), but offers control over `maxSymbolValue` and `tableLog`.
84
+ * `maxSymbolValue` must be <= HUF_SYMBOLVALUE_MAX .
85
+ * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
86
+ HUF_PUBLIC_API size_t HUF_compress2 (void* dst, size_t dstCapacity,
87
+ const void* src, size_t srcSize,
88
+ unsigned maxSymbolValue, unsigned tableLog);
89
+
90
+ /** HUF_compress4X_wksp() :
91
+ * Same as HUF_compress2(), but uses externally allocated `workSpace`.
92
+ * `workspace` must have minimum alignment of 4, and be at least as large as HUF_WORKSPACE_SIZE */
93
+ #define HUF_WORKSPACE_SIZE ((6 << 10) + 256)
94
+ #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
95
+ HUF_PUBLIC_API size_t HUF_compress4X_wksp (void* dst, size_t dstCapacity,
96
+ const void* src, size_t srcSize,
97
+ unsigned maxSymbolValue, unsigned tableLog,
98
+ void* workSpace, size_t wkspSize);
99
+
100
+ #endif /* HUF_H_298734234 */
101
+
102
+ /* ******************************************************************
103
+ * WARNING !!
104
+ * The following section contains advanced and experimental definitions
105
+ * which shall never be used in the context of a dynamic library,
106
+ * because they are not guaranteed to remain stable in the future.
107
+ * Only consider them in association with static linking.
108
+ * *****************************************************************/
109
+ #if defined(HUF_STATIC_LINKING_ONLY) && !defined(HUF_H_HUF_STATIC_LINKING_ONLY)
110
+ #define HUF_H_HUF_STATIC_LINKING_ONLY
111
+
112
+ /* *** Dependencies *** */
113
+ #include "mem.h" /* U32 */
114
+
115
+
116
+ /* *** Constants *** */
117
+ #define HUF_TABLELOG_MAX 12 /* max runtime value of tableLog (due to static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
118
+ #define HUF_TABLELOG_DEFAULT 11 /* default tableLog value when none specified */
119
+ #define HUF_SYMBOLVALUE_MAX 255
120
+
121
+ #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
122
+ #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
123
+ # error "HUF_TABLELOG_MAX is too large !"
124
+ #endif
125
+
126
+
127
+ /* ****************************************
128
+ * Static allocation
129
+ ******************************************/
130
+ /* HUF buffer bounds */
131
+ #define HUF_CTABLEBOUND 129
132
+ #define HUF_BLOCKBOUND(size) (size + (size>>8) + 8) /* only true when incompressible is pre-filtered with fast heuristic */
133
+ #define HUF_COMPRESSBOUND(size) (HUF_CTABLEBOUND + HUF_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
134
+
135
+ /* static allocation of HUF's Compression Table */
136
+ #define HUF_CTABLE_SIZE_U32(maxSymbolValue) ((maxSymbolValue)+1) /* Use tables of U32, for proper alignment */
137
+ #define HUF_CTABLE_SIZE(maxSymbolValue) (HUF_CTABLE_SIZE_U32(maxSymbolValue) * sizeof(U32))
138
+ #define HUF_CREATE_STATIC_CTABLE(name, maxSymbolValue) \
139
+ U32 name##hb[HUF_CTABLE_SIZE_U32(maxSymbolValue)]; \
140
+ void* name##hv = &(name##hb); \
141
+ HUF_CElt* name = (HUF_CElt*)(name##hv) /* no final ; */
142
+
143
+ /* static allocation of HUF's DTable */
144
+ typedef U32 HUF_DTable;
145
+ #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
146
+ #define HUF_CREATE_STATIC_DTABLEX1(DTable, maxTableLog) \
147
+ HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
148
+ #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
149
+ HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
150
+
151
+
152
+ /* ****************************************
153
+ * Advanced decompression functions
154
+ ******************************************/
155
+ size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
156
+ #ifndef HUF_FORCE_DECOMPRESS_X1
157
+ size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
158
+ #endif
159
+
160
+ size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< decodes RLE and uncompressed */
161
+ size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< considers RLE and uncompressed as errors */
162
+ size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< considers RLE and uncompressed as errors */
163
+ size_t HUF_decompress4X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
164
+ size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
165
+ #ifndef HUF_FORCE_DECOMPRESS_X1
166
+ size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
167
+ size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
168
+ #endif
169
+
170
+
171
+ /* ****************************************
172
+ * HUF detailed API
173
+ * ****************************************/
174
+
175
+ /*! HUF_compress() does the following:
176
+ * 1. count symbol occurrence from source[] into table count[] using FSE_count() (exposed within "fse.h")
177
+ * 2. (optional) refine tableLog using HUF_optimalTableLog()
178
+ * 3. build Huffman table from count using HUF_buildCTable()
179
+ * 4. save Huffman table to memory buffer using HUF_writeCTable()
180
+ * 5. encode the data stream using HUF_compress4X_usingCTable()
181
+ *
182
+ * The following API allows targeting specific sub-functions for advanced tasks.
183
+ * For example, it's possible to compress several blocks using the same 'CTable',
184
+ * or to save and regenerate 'CTable' using external methods.
185
+ */
186
+ unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
187
+ typedef struct HUF_CElt_s HUF_CElt; /* incomplete type */
188
+ size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue, unsigned maxNbBits); /* @return : maxNbBits; CTable and count can overlap. In which case, CTable will overwrite count content */
189
+ size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
190
+ size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
191
+ size_t HUF_estimateCompressedSize(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
192
+ int HUF_validateCTable(const HUF_CElt* CTable, const unsigned* count, unsigned maxSymbolValue);
193
+
194
+ typedef enum {
195
+ HUF_repeat_none, /**< Cannot use the previous table */
196
+ HUF_repeat_check, /**< Can use the previous table but it must be checked. Note : The previous table must have been constructed by HUF_compress{1, 4}X_repeat */
197
+ HUF_repeat_valid /**< Can use the previous table and it is assumed to be valid */
198
+ } HUF_repeat;
199
+ /** HUF_compress4X_repeat() :
200
+ * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
201
+ * If it uses hufTable it does not modify hufTable or repeat.
202
+ * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
203
+ * If preferRepeat then the old table will always be used if valid. */
204
+ size_t HUF_compress4X_repeat(void* dst, size_t dstSize,
205
+ const void* src, size_t srcSize,
206
+ unsigned maxSymbolValue, unsigned tableLog,
207
+ void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
208
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
209
+
210
+ /** HUF_buildCTable_wksp() :
211
+ * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
212
+ * `workSpace` must be aligned on 4-bytes boundaries, and its size must be >= HUF_CTABLE_WORKSPACE_SIZE.
213
+ */
214
+ #define HUF_CTABLE_WORKSPACE_SIZE_U32 (2*HUF_SYMBOLVALUE_MAX +1 +1)
215
+ #define HUF_CTABLE_WORKSPACE_SIZE (HUF_CTABLE_WORKSPACE_SIZE_U32 * sizeof(unsigned))
216
+ size_t HUF_buildCTable_wksp (HUF_CElt* tree,
217
+ const unsigned* count, U32 maxSymbolValue, U32 maxNbBits,
218
+ void* workSpace, size_t wkspSize);
219
+
220
+ /*! HUF_readStats() :
221
+ * Read compact Huffman tree, saved by HUF_writeCTable().
222
+ * `huffWeight` is destination buffer.
223
+ * @return : size read from `src` , or an error Code .
224
+ * Note : Needed by HUF_readCTable() and HUF_readDTableXn() . */
225
+ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize,
226
+ U32* rankStats, U32* nbSymbolsPtr, U32* tableLogPtr,
227
+ const void* src, size_t srcSize);
228
+
229
+ /** HUF_readCTable() :
230
+ * Loading a CTable saved with HUF_writeCTable() */
231
+ size_t HUF_readCTable (HUF_CElt* CTable, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned *hasZeroWeights);
232
+
233
+ /** HUF_getNbBits() :
234
+ * Read nbBits from CTable symbolTable, for symbol `symbolValue` presumed <= HUF_SYMBOLVALUE_MAX
235
+ * Note 1 : is not inlined, as HUF_CElt definition is private
236
+ * Note 2 : const void* used, so that it can provide a statically allocated table as argument (which uses type U32) */
237
+ U32 HUF_getNbBits(const void* symbolTable, U32 symbolValue);
238
+
239
+ /*
240
+ * HUF_decompress() does the following:
241
+ * 1. select the decompression algorithm (X1, X2) based on pre-computed heuristics
242
+ * 2. build Huffman table from save, using HUF_readDTableX?()
243
+ * 3. decode 1 or 4 segments in parallel using HUF_decompress?X?_usingDTable()
244
+ */
245
+
246
+ /** HUF_selectDecoder() :
247
+ * Tells which decoder is likely to decode faster,
248
+ * based on a set of pre-computed metrics.
249
+ * @return : 0==HUF_decompress4X1, 1==HUF_decompress4X2 .
250
+ * Assumption : 0 < dstSize <= 128 KB */
251
+ U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize);
252
+
253
+ /**
254
+ * The minimum workspace size for the `workSpace` used in
255
+ * HUF_readDTableX1_wksp() and HUF_readDTableX2_wksp().
256
+ *
257
+ * The space used depends on HUF_TABLELOG_MAX, ranging from ~1500 bytes when
258
+ * HUF_TABLE_LOG_MAX=12 to ~1850 bytes when HUF_TABLE_LOG_MAX=15.
259
+ * Buffer overflow errors may potentially occur if code modifications result in
260
+ * a required workspace size greater than that specified in the following
261
+ * macro.
262
+ */
263
+ #define HUF_DECOMPRESS_WORKSPACE_SIZE (2 << 10)
264
+ #define HUF_DECOMPRESS_WORKSPACE_SIZE_U32 (HUF_DECOMPRESS_WORKSPACE_SIZE / sizeof(U32))
265
+
266
+ #ifndef HUF_FORCE_DECOMPRESS_X2
267
+ size_t HUF_readDTableX1 (HUF_DTable* DTable, const void* src, size_t srcSize);
268
+ size_t HUF_readDTableX1_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
269
+ #endif
270
+ #ifndef HUF_FORCE_DECOMPRESS_X1
271
+ size_t HUF_readDTableX2 (HUF_DTable* DTable, const void* src, size_t srcSize);
272
+ size_t HUF_readDTableX2_wksp (HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize);
273
+ #endif
274
+
275
+ size_t HUF_decompress4X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
276
+ #ifndef HUF_FORCE_DECOMPRESS_X2
277
+ size_t HUF_decompress4X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
278
+ #endif
279
+ #ifndef HUF_FORCE_DECOMPRESS_X1
280
+ size_t HUF_decompress4X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
281
+ #endif
282
+
283
+
284
+ /* ====================== */
285
+ /* single stream variants */
286
+ /* ====================== */
287
+
288
+ size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
289
+ size_t HUF_compress1X_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
290
+ size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
291
+ /** HUF_compress1X_repeat() :
292
+ * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
293
+ * If it uses hufTable it does not modify hufTable or repeat.
294
+ * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
295
+ * If preferRepeat then the old table will always be used if valid. */
296
+ size_t HUF_compress1X_repeat(void* dst, size_t dstSize,
297
+ const void* src, size_t srcSize,
298
+ unsigned maxSymbolValue, unsigned tableLog,
299
+ void* workSpace, size_t wkspSize, /**< `workSpace` must be aligned on 4-bytes boundaries, `wkspSize` must be >= HUF_WORKSPACE_SIZE */
300
+ HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat, int bmi2);
301
+
302
+ size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
303
+ #ifndef HUF_FORCE_DECOMPRESS_X1
304
+ size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
305
+ #endif
306
+
307
+ size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
308
+ size_t HUF_decompress1X_DCtx_wksp (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize);
309
+ #ifndef HUF_FORCE_DECOMPRESS_X2
310
+ size_t HUF_decompress1X1_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
311
+ size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< single-symbol decoder */
312
+ #endif
313
+ #ifndef HUF_FORCE_DECOMPRESS_X1
314
+ size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
315
+ size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize); /**< double-symbols decoder */
316
+ #endif
317
+
318
+ size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable); /**< automatic selection of sing or double symbol decoder, based on DTable */
319
+ #ifndef HUF_FORCE_DECOMPRESS_X2
320
+ size_t HUF_decompress1X1_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
321
+ #endif
322
+ #ifndef HUF_FORCE_DECOMPRESS_X1
323
+ size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
324
+ #endif
325
+
326
+ /* BMI2 variants.
327
+ * If the CPU has BMI2 support, pass bmi2=1, otherwise pass bmi2=0.
328
+ */
329
+ size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
330
+ #ifndef HUF_FORCE_DECOMPRESS_X2
331
+ size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
332
+ #endif
333
+ size_t HUF_decompress4X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2);
334
+ size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize, void* workSpace, size_t wkspSize, int bmi2);
335
+
336
+ #endif /* HUF_STATIC_LINKING_ONLY */
337
+
338
+ #if defined (__cplusplus)
339
+ }
340
+ #endif