extzstd 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/HISTORY.ja +5 -0
  3. data/README.md +5 -5
  4. data/contrib/zstd/CONTRIBUTING.md +42 -0
  5. data/contrib/zstd/LICENSE-examples +11 -0
  6. data/contrib/zstd/Makefile +315 -0
  7. data/contrib/zstd/NEWS +261 -0
  8. data/contrib/zstd/PATENTS +33 -0
  9. data/contrib/zstd/README.md +121 -41
  10. data/contrib/zstd/TESTING.md +44 -0
  11. data/contrib/zstd/appveyor.yml +178 -0
  12. data/contrib/zstd/circle.yml +75 -0
  13. data/contrib/zstd/lib/BUCK +186 -0
  14. data/contrib/zstd/lib/Makefile +163 -0
  15. data/contrib/zstd/lib/README.md +77 -0
  16. data/contrib/zstd/{common → lib/common}/bitstream.h +7 -4
  17. data/contrib/zstd/{common → lib/common}/entropy_common.c +19 -23
  18. data/contrib/zstd/{common → lib/common}/error_private.c +0 -0
  19. data/contrib/zstd/{common → lib/common}/error_private.h +0 -0
  20. data/contrib/zstd/{common → lib/common}/fse.h +94 -34
  21. data/contrib/zstd/{common → lib/common}/fse_decompress.c +18 -19
  22. data/contrib/zstd/{common → lib/common}/huf.h +52 -20
  23. data/contrib/zstd/{common → lib/common}/mem.h +17 -13
  24. data/contrib/zstd/lib/common/pool.c +194 -0
  25. data/contrib/zstd/lib/common/pool.h +56 -0
  26. data/contrib/zstd/lib/common/threading.c +80 -0
  27. data/contrib/zstd/lib/common/threading.h +104 -0
  28. data/contrib/zstd/{common → lib/common}/xxhash.c +3 -1
  29. data/contrib/zstd/{common → lib/common}/xxhash.h +11 -15
  30. data/contrib/zstd/{common → lib/common}/zstd_common.c +1 -11
  31. data/contrib/zstd/{common → lib/common}/zstd_errors.h +16 -2
  32. data/contrib/zstd/{common → lib/common}/zstd_internal.h +17 -1
  33. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +138 -91
  34. data/contrib/zstd/{compress → lib/compress}/huf_compress.c +218 -67
  35. data/contrib/zstd/{compress → lib/compress}/zstd_compress.c +231 -108
  36. data/contrib/zstd/{compress → lib/compress}/zstd_opt.h +44 -25
  37. data/contrib/zstd/lib/compress/zstdmt_compress.c +739 -0
  38. data/contrib/zstd/lib/compress/zstdmt_compress.h +78 -0
  39. data/contrib/zstd/{decompress → lib/decompress}/huf_decompress.c +28 -23
  40. data/contrib/zstd/{decompress → lib/decompress}/zstd_decompress.c +814 -176
  41. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +60 -39
  42. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  43. data/contrib/zstd/lib/deprecated/zbuff_compress.c +145 -0
  44. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +74 -0
  45. data/contrib/zstd/lib/dictBuilder/cover.c +1029 -0
  46. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +0 -0
  47. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +0 -0
  48. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +68 -18
  49. data/contrib/zstd/lib/dictBuilder/zdict.h +201 -0
  50. data/contrib/zstd/{legacy → lib/legacy}/zstd_legacy.h +122 -7
  51. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +34 -3
  52. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +8 -0
  53. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +45 -12
  54. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +8 -0
  55. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +45 -12
  56. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +8 -0
  57. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +56 -33
  58. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +8 -0
  59. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +45 -18
  60. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +7 -0
  61. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +43 -16
  62. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +7 -0
  63. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.c +57 -23
  64. data/contrib/zstd/{legacy → lib/legacy}/zstd_v07.h +8 -0
  65. data/contrib/zstd/lib/libzstd.pc.in +14 -0
  66. data/contrib/zstd/{zstd.h → lib/zstd.h} +206 -71
  67. data/ext/depend +2 -0
  68. data/ext/extconf.rb +4 -4
  69. data/ext/extzstd.c +1 -1
  70. data/ext/zstd_common.c +5 -5
  71. data/ext/zstd_compress.c +3 -3
  72. data/ext/zstd_decompress.c +2 -2
  73. data/ext/zstd_dictbuilder.c +2 -2
  74. data/ext/zstd_legacy_v01.c +1 -1
  75. data/ext/zstd_legacy_v02.c +1 -1
  76. data/ext/zstd_legacy_v03.c +1 -1
  77. data/ext/zstd_legacy_v04.c +1 -1
  78. data/ext/zstd_legacy_v05.c +1 -1
  79. data/ext/zstd_legacy_v06.c +1 -1
  80. data/ext/zstd_legacy_v07.c +1 -1
  81. data/gemstub.rb +9 -5
  82. data/lib/extzstd/version.rb +1 -1
  83. metadata +73 -51
  84. data/contrib/zstd/compress/zbuff_compress.c +0 -319
  85. data/contrib/zstd/decompress/zbuff_decompress.c +0 -252
  86. data/contrib/zstd/dictBuilder/zdict.h +0 -111
@@ -45,6 +45,32 @@ extern "C" {
45
45
  #include <stddef.h> /* size_t, ptrdiff_t */
46
46
 
47
47
 
48
+ /*-*****************************************
49
+ * FSE_PUBLIC_API : control library symbols visibility
50
+ ******************************************/
51
+ #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
52
+ # define FSE_PUBLIC_API __attribute__ ((visibility ("default")))
53
+ #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
54
+ # define FSE_PUBLIC_API __declspec(dllexport)
55
+ #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
56
+ # define FSE_PUBLIC_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
57
+ #else
58
+ # define FSE_PUBLIC_API
59
+ #endif
60
+
61
+ /*------ Version ------*/
62
+ #define FSE_VERSION_MAJOR 0
63
+ #define FSE_VERSION_MINOR 9
64
+ #define FSE_VERSION_RELEASE 0
65
+
66
+ #define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE
67
+ #define FSE_QUOTE(str) #str
68
+ #define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)
69
+ #define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)
70
+
71
+ #define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE)
72
+ FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */
73
+
48
74
  /*-****************************************
49
75
  * FSE simple functions
50
76
  ******************************************/
@@ -56,8 +82,8 @@ extern "C" {
56
82
  if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.
57
83
  if FSE_isError(return), compression failed (more details using FSE_getErrorName())
58
84
  */
59
- size_t FSE_compress(void* dst, size_t dstCapacity,
60
- const void* src, size_t srcSize);
85
+ FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity,
86
+ const void* src, size_t srcSize);
61
87
 
62
88
  /*! FSE_decompress():
63
89
  Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',
@@ -69,18 +95,18 @@ size_t FSE_compress(void* dst, size_t dstCapacity,
69
95
  Why ? : making this distinction requires a header.
70
96
  Header management is intentionally delegated to the user layer, which can better manage special cases.
71
97
  */
72
- size_t FSE_decompress(void* dst, size_t dstCapacity,
73
- const void* cSrc, size_t cSrcSize);
98
+ FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity,
99
+ const void* cSrc, size_t cSrcSize);
74
100
 
75
101
 
76
102
  /*-*****************************************
77
103
  * Tool functions
78
104
  ******************************************/
79
- size_t FSE_compressBound(size_t size); /* maximum compressed size */
105
+ FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */
80
106
 
81
107
  /* Error Management */
82
- unsigned FSE_isError(size_t code); /* tells if a return value is an error code */
83
- const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */
108
+ FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */
109
+ FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */
84
110
 
85
111
 
86
112
  /*-*****************************************
@@ -94,7 +120,7 @@ const char* FSE_getErrorName(size_t code); /* provides error code string (usef
94
120
  if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression.
95
121
  if FSE_isError(return), it's an error code.
96
122
  */
97
- size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
123
+ FSE_PUBLIC_API size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
98
124
 
99
125
 
100
126
  /*-*****************************************
@@ -127,50 +153,50 @@ or to save and provide normalized distribution using external method.
127
153
  @return : the count of the most frequent symbol (which is not identified).
128
154
  if return == srcSize, there is only one symbol.
129
155
  Can also return an error code, which can be tested with FSE_isError(). */
130
- size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
156
+ FSE_PUBLIC_API size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
131
157
 
132
158
  /*! FSE_optimalTableLog():
133
159
  dynamically downsize 'tableLog' when conditions are met.
134
160
  It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.
135
161
  @return : recommended tableLog (necessarily <= 'maxTableLog') */
136
- unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
162
+ FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
137
163
 
138
164
  /*! FSE_normalizeCount():
139
165
  normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
140
166
  'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
141
167
  @return : tableLog,
142
168
  or an errorCode, which can be tested using FSE_isError() */
143
- size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
169
+ FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog, const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
144
170
 
145
171
  /*! FSE_NCountWriteBound():
146
172
  Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
147
173
  Typically useful for allocation purpose. */
148
- size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
174
+ FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
149
175
 
150
176
  /*! FSE_writeNCount():
151
177
  Compactly save 'normalizedCounter' into 'buffer'.
152
178
  @return : size of the compressed table,
153
179
  or an errorCode, which can be tested using FSE_isError(). */
154
- size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
180
+ FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
155
181
 
156
182
 
157
183
  /*! Constructor and Destructor of FSE_CTable.
158
184
  Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */
159
185
  typedef unsigned FSE_CTable; /* don't allocate that. It's only meant to be more restrictive than void* */
160
- FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue);
161
- void FSE_freeCTable (FSE_CTable* ct);
186
+ FSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned tableLog, unsigned maxSymbolValue);
187
+ FSE_PUBLIC_API void FSE_freeCTable (FSE_CTable* ct);
162
188
 
163
189
  /*! FSE_buildCTable():
164
190
  Builds `ct`, which must be already allocated, using FSE_createCTable().
165
191
  @return : 0, or an errorCode, which can be tested using FSE_isError() */
166
- size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
192
+ FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
167
193
 
168
194
  /*! FSE_compress_usingCTable():
169
195
  Compress `src` using `ct` into `dst` which must be already allocated.
170
196
  @return : size of compressed data (<= `dstCapacity`),
171
197
  or 0 if compressed data could not fit into `dst`,
172
198
  or an errorCode, which can be tested using FSE_isError() */
173
- size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);
199
+ FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);
174
200
 
175
201
  /*!
176
202
  Tutorial :
@@ -223,25 +249,25 @@ If there is an error, the function will return an ErrorCode (which can be tested
223
249
  @return : size read from 'rBuffer',
224
250
  or an errorCode, which can be tested using FSE_isError().
225
251
  maxSymbolValuePtr[0] and tableLogPtr[0] will also be updated with their respective values */
226
- size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize);
252
+ FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSymbolValuePtr, unsigned* tableLogPtr, const void* rBuffer, size_t rBuffSize);
227
253
 
228
254
  /*! Constructor and Destructor of FSE_DTable.
229
255
  Note that its size depends on 'tableLog' */
230
256
  typedef unsigned FSE_DTable; /* don't allocate that. It's just a way to be more restrictive than void* */
231
- FSE_DTable* FSE_createDTable(unsigned tableLog);
232
- void FSE_freeDTable(FSE_DTable* dt);
257
+ FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog);
258
+ FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt);
233
259
 
234
260
  /*! FSE_buildDTable():
235
261
  Builds 'dt', which must be already allocated, using FSE_createDTable().
236
262
  return : 0, or an errorCode, which can be tested using FSE_isError() */
237
- size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
263
+ FSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
238
264
 
239
265
  /*! FSE_decompress_usingDTable():
240
266
  Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
241
267
  into `dst` which must be already allocated.
242
268
  @return : size of regenerated data (necessarily <= `dstCapacity`),
243
269
  or an errorCode, which can be tested using FSE_isError() */
244
- size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
270
+ FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
245
271
 
246
272
  /*!
247
273
  Tutorial :
@@ -286,7 +312,7 @@ If there is an error, the function will return an error code, which can be teste
286
312
  #define FSE_BLOCKBOUND(size) (size + (size>>7))
287
313
  #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
288
314
 
289
- /* It is possible to statically allocate FSE CTable/DTable as a table of unsigned using below macros */
315
+ /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
290
316
  #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
291
317
  #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
292
318
 
@@ -294,37 +320,72 @@ If there is an error, the function will return an error code, which can be teste
294
320
  /* *****************************************
295
321
  * FSE advanced API
296
322
  *******************************************/
323
+ /* FSE_count_wksp() :
324
+ * Same as FSE_count(), but using an externally provided scratch buffer.
325
+ * `workSpace` size must be table of >= `1024` unsigned
326
+ */
327
+ size_t FSE_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
328
+ const void* source, size_t sourceSize, unsigned* workSpace);
329
+
330
+ /** FSE_countFast() :
331
+ * same as FSE_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr
332
+ */
297
333
  size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
298
- /**< same as FSE_count(), but blindly trusts that all byte values within src are <= *maxSymbolValuePtr */
334
+
335
+ /* FSE_countFast_wksp() :
336
+ * Same as FSE_countFast(), but using an externally provided scratch buffer.
337
+ * `workSpace` must be a table of minimum `1024` unsigned
338
+ */
339
+ size_t FSE_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize, unsigned* workSpace);
340
+
341
+ /*! FSE_count_simple
342
+ * Same as FSE_countFast(), but does not use any additional memory (not even on stack).
343
+ * This function is unsafe, and will segfault if any value within `src` is `> *maxSymbolValuePtr` (presuming it's also the size of `count`).
344
+ */
345
+ size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr, const void* src, size_t srcSize);
346
+
347
+
299
348
 
300
349
  unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);
301
350
  /**< same as FSE_optimalTableLog(), which used `minus==2` */
302
351
 
352
+ /* FSE_compress_wksp() :
353
+ * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
354
+ * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
355
+ */
356
+ #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + (1<<((maxTableLog>2)?(maxTableLog-2):0)) )
357
+ size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
358
+
303
359
  size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits);
304
- /**< build a fake FSE_CTable, designed to not compress an input, where each symbol uses nbBits */
360
+ /**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */
305
361
 
306
362
  size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
307
363
  /**< build a fake FSE_CTable, designed to compress always the same symbolValue */
308
364
 
365
+ /* FSE_buildCTable_wksp() :
366
+ * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
367
+ * `wkspSize` must be >= `(1<<tableLog)`.
368
+ */
369
+ size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
370
+
309
371
  size_t FSE_buildDTable_raw (FSE_DTable* dt, unsigned nbBits);
310
- /**< build a fake FSE_DTable, designed to read an uncompressed bitstream where each symbol uses nbBits */
372
+ /**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
311
373
 
312
374
  size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
313
375
  /**< build a fake FSE_DTable, designed to always generate the same symbolValue */
314
376
 
377
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);
378
+ /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
379
+
315
380
 
316
381
  /* *****************************************
317
382
  * FSE symbol compression API
318
383
  *******************************************/
319
384
  /*!
320
385
  This API consists of small unitary functions, which highly benefit from being inlined.
321
- You will want to enable link-time-optimization to ensure these functions are properly inlined in your binary.
322
- Visual seems to do it automatically.
323
- For gcc or clang, you'll need to add -flto flag at compilation and linking stages.
324
- If none of these solutions is applicable, include "fse.c" directly.
386
+ Hence their body are included in next section.
325
387
  */
326
- typedef struct
327
- {
388
+ typedef struct {
328
389
  ptrdiff_t value;
329
390
  const void* stateTable;
330
391
  const void* symbolTT;
@@ -384,8 +445,7 @@ If there is an error, it returns an errorCode (which can be tested using FSE_isE
384
445
  /* *****************************************
385
446
  * FSE symbol decompression API
386
447
  *******************************************/
387
- typedef struct
388
- {
448
+ typedef struct {
389
449
  size_t state;
390
450
  const void* table; /* precise table may vary, depending on U16 */
391
451
  } FSE_DState_t;
@@ -59,7 +59,6 @@
59
59
  ****************************************************************/
60
60
  #include <stdlib.h> /* malloc, free, qsort */
61
61
  #include <string.h> /* memcpy, memset */
62
- #include <stdio.h> /* printf (debug) */
63
62
  #include "bitstream.h"
64
63
  #define FSE_STATIC_LINKING_ONLY
65
64
  #include "fse.h"
@@ -75,12 +74,6 @@
75
74
  #define CHECK_F(f) { size_t const e = f; if (FSE_isError(e)) return e; }
76
75
 
77
76
 
78
- /* **************************************************************
79
- * Complex types
80
- ****************************************************************/
81
- typedef U32 DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
82
-
83
-
84
77
  /* **************************************************************
85
78
  * Templates
86
79
  ****************************************************************/
@@ -300,28 +293,34 @@ size_t FSE_decompress_usingDTable(void* dst, size_t originalSize,
300
293
  }
301
294
 
302
295
 
303
- size_t FSE_decompress(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize)
296
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog)
304
297
  {
305
298
  const BYTE* const istart = (const BYTE*)cSrc;
306
299
  const BYTE* ip = istart;
307
300
  short counting[FSE_MAX_SYMBOL_VALUE+1];
308
- DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
309
301
  unsigned tableLog;
310
302
  unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
311
303
 
312
- if (cSrcSize<2) return ERROR(srcSize_wrong); /* too small input size */
313
-
314
304
  /* normal FSE decoding mode */
315
- { size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
316
- if (FSE_isError(NCountLength)) return NCountLength;
317
- if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size */
318
- ip += NCountLength;
319
- cSrcSize -= NCountLength;
320
- }
305
+ size_t const NCountLength = FSE_readNCount (counting, &maxSymbolValue, &tableLog, istart, cSrcSize);
306
+ if (FSE_isError(NCountLength)) return NCountLength;
307
+ //if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining case : NCountLength==cSrcSize */
308
+ if (tableLog > maxLog) return ERROR(tableLog_tooLarge);
309
+ ip += NCountLength;
310
+ cSrcSize -= NCountLength;
321
311
 
322
- CHECK_F( FSE_buildDTable (dt, counting, maxSymbolValue, tableLog) );
312
+ CHECK_F( FSE_buildDTable (workSpace, counting, maxSymbolValue, tableLog) );
323
313
 
324
- return FSE_decompress_usingDTable (dst, maxDstSize, ip, cSrcSize, dt); /* always return, even if it is an error code */
314
+ return FSE_decompress_usingDTable (dst, dstCapacity, ip, cSrcSize, workSpace); /* always return, even if it is an error code */
315
+ }
316
+
317
+
318
+ typedef FSE_DTable DTable_max_t[FSE_DTABLE_SIZE_U32(FSE_MAX_TABLELOG)];
319
+
320
+ size_t FSE_decompress(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize)
321
+ {
322
+ DTable_max_t dt; /* Static analyzer seems unable to understand this table will be properly initialized later */
323
+ return FSE_decompress_wksp(dst, dstCapacity, cSrc, cSrcSize, dt, FSE_MAX_TABLELOG);
325
324
  }
326
325
 
327
326
 
@@ -62,21 +62,19 @@ size_t HUF_compress(void* dst, size_t dstCapacity,
62
62
  HUF_decompress() :
63
63
  Decompress HUF data from buffer 'cSrc', of size 'cSrcSize',
64
64
  into already allocated buffer 'dst', of minimum size 'dstSize'.
65
- `dstSize` : **must** be the ***exact*** size of original (uncompressed) data.
65
+ `originalSize` : **must** be the ***exact*** size of original (uncompressed) data.
66
66
  Note : in contrast with FSE, HUF_decompress can regenerate
67
67
  RLE (cSrcSize==1) and uncompressed (cSrcSize==dstSize) data,
68
68
  because it knows size to regenerate.
69
- @return : size of regenerated data (== dstSize),
69
+ @return : size of regenerated data (== originalSize),
70
70
  or an error code, which can be tested using HUF_isError()
71
71
  */
72
- size_t HUF_decompress(void* dst, size_t dstSize,
72
+ size_t HUF_decompress(void* dst, size_t originalSize,
73
73
  const void* cSrc, size_t cSrcSize);
74
74
 
75
75
 
76
- /* ****************************************
77
- * Tool functions
78
- ******************************************/
79
- #define HUF_BLOCKSIZE_MAX (128 * 1024)
76
+ /* *** Tool functions *** */
77
+ #define HUF_BLOCKSIZE_MAX (128 * 1024) /**< maximum input size for a single block compressed with HUF_compress */
80
78
  size_t HUF_compressBound(size_t size); /**< maximum compressed size (worst case) */
81
79
 
82
80
  /* Error Management */
@@ -84,12 +82,18 @@ unsigned HUF_isError(size_t code); /**< tells if a return value is an
84
82
  const char* HUF_getErrorName(size_t code); /**< provides error code string (useful for debugging) */
85
83
 
86
84
 
87
- /* *** Advanced function *** */
85
+ /* *** Advanced function *** */
88
86
 
89
87
  /** HUF_compress2() :
90
- * Same as HUF_compress(), but offers direct control over `maxSymbolValue` and `tableLog` */
88
+ * Same as HUF_compress(), but offers direct control over `maxSymbolValue` and `tableLog` .
89
+ * `tableLog` must be `<= HUF_TABLELOG_MAX` . */
91
90
  size_t HUF_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
92
91
 
92
+ /** HUF_compress4X_wksp() :
93
+ * Same as HUF_compress2(), but uses externally allocated `workSpace`, which must be a table of >= 1024 unsigned */
94
+ size_t HUF_compress4X_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 */
95
+
96
+
93
97
 
94
98
  #ifdef HUF_STATIC_LINKING_ONLY
95
99
 
@@ -98,10 +102,11 @@ size_t HUF_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize
98
102
 
99
103
 
100
104
  /* *** Constants *** */
101
- #define HUF_TABLELOG_ABSOLUTEMAX 16 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
102
- #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
105
+ #define HUF_TABLELOG_MAX 12 /* max configured tableLog (for static allocation); can be modified up to HUF_ABSOLUTEMAX_TABLELOG */
103
106
  #define HUF_TABLELOG_DEFAULT 11 /* tableLog by default, when not specified */
104
- #define HUF_SYMBOLVALUE_MAX 255
107
+ #define HUF_SYMBOLVALUE_MAX 255
108
+
109
+ #define HUF_TABLELOG_ABSOLUTEMAX 15 /* absolute limit of HUF_MAX_TABLELOG. Beyond that value, code does not work */
105
110
  #if (HUF_TABLELOG_MAX > HUF_TABLELOG_ABSOLUTEMAX)
106
111
  # error "HUF_TABLELOG_MAX is too large !"
107
112
  #endif
@@ -125,9 +130,13 @@ size_t HUF_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize
125
130
  typedef U32 HUF_DTable;
126
131
  #define HUF_DTABLE_SIZE(maxTableLog) (1 + (1<<(maxTableLog)))
127
132
  #define HUF_CREATE_STATIC_DTABLEX2(DTable, maxTableLog) \
128
- HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1)*0x1000001) }
133
+ HUF_DTable DTable[HUF_DTABLE_SIZE((maxTableLog)-1)] = { ((U32)((maxTableLog)-1) * 0x01000001) }
129
134
  #define HUF_CREATE_STATIC_DTABLEX4(DTable, maxTableLog) \
130
- HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog)*0x1000001) }
135
+ HUF_DTable DTable[HUF_DTABLE_SIZE(maxTableLog)] = { ((U32)(maxTableLog) * 0x01000001) }
136
+
137
+ /* The workspace must have alignment at least 4 and be at least this large */
138
+ #define HUF_WORKSPACE_SIZE (6 << 10)
139
+ #define HUF_WORKSPACE_SIZE_U32 (HUF_WORKSPACE_SIZE / sizeof(U32))
131
140
 
132
141
 
133
142
  /* ****************************************
@@ -141,10 +150,6 @@ size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, con
141
150
  size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
142
151
  size_t HUF_decompress4X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
143
152
 
144
- size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
145
- size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
146
- size_t HUF_decompress1X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
147
-
148
153
 
149
154
  /* ****************************************
150
155
  * HUF detailed API
@@ -168,6 +173,23 @@ size_t HUF_buildCTable (HUF_CElt* CTable, const unsigned* count, unsigned maxSym
168
173
  size_t HUF_writeCTable (void* dst, size_t maxDstSize, const HUF_CElt* CTable, unsigned maxSymbolValue, unsigned huffLog);
169
174
  size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
170
175
 
176
+ typedef enum {
177
+ HUF_repeat_none, /**< Cannot use the previous table */
178
+ 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 */
179
+ HUF_repeat_valid /**< Can use the previous table and it is asumed to be valid */
180
+ } HUF_repeat;
181
+ /** HUF_compress4X_repeat() :
182
+ * Same as HUF_compress4X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
183
+ * If it uses hufTable it does not modify hufTable or repeat.
184
+ * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
185
+ * If preferRepeat then the old table will always be used if valid. */
186
+ size_t HUF_compress4X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
187
+
188
+ /** HUF_buildCTable_wksp() :
189
+ * Same as HUF_buildCTable(), but using externally allocated scratch buffer.
190
+ * `workSpace` must be aligned on 4-bytes boundaries, and be at least as large as a table of 1024 unsigned.
191
+ */
192
+ size_t HUF_buildCTable_wksp (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits, void* workSpace, size_t wkspSize);
171
193
 
172
194
  /*! HUF_readStats() :
173
195
  Read compact Huffman tree, saved by HUF_writeCTable().
@@ -208,16 +230,26 @@ size_t HUF_decompress4X4_usingDTable(void* dst, size_t maxDstSize, const void* c
208
230
  /* single stream variants */
209
231
 
210
232
  size_t HUF_compress1X (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
233
+ 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 */
211
234
  size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable);
235
+ /** HUF_compress1X_repeat() :
236
+ * Same as HUF_compress1X_wksp(), but considers using hufTable if *repeat != HUF_repeat_none.
237
+ * If it uses hufTable it does not modify hufTable or repeat.
238
+ * If it doesn't, it sets *repeat = HUF_repeat_none, and it sets hufTable to the table used.
239
+ * If preferRepeat then the old table will always be used if valid. */
240
+ size_t HUF_compress1X_repeat(void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize, HUF_CElt* hufTable, HUF_repeat* repeat, int preferRepeat); /**< `workSpace` must be a table of at least HUF_WORKSPACE_SIZE_U32 unsigned */
212
241
 
213
242
  size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* single-symbol decoder */
214
243
  size_t HUF_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /* double-symbol decoder */
215
244
 
216
- size_t HUF_decompress1X_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
245
+ size_t HUF_decompress1X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
246
+ size_t HUF_decompress1X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< single-symbol decoder */
247
+ size_t HUF_decompress1X4_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize); /**< double-symbols decoder */
248
+
249
+ 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 */
217
250
  size_t HUF_decompress1X2_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
218
251
  size_t HUF_decompress1X4_usingDTable(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable);
219
252
 
220
-
221
253
  #endif /* HUF_STATIC_LINKING_ONLY */
222
254
 
223
255
 
@@ -39,7 +39,7 @@ extern "C" {
39
39
  #endif
40
40
 
41
41
  /* code only tested on 32 and 64 bits systems */
42
- #define MEM_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; }
42
+ #define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; }
43
43
  MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
44
44
 
45
45
 
@@ -48,21 +48,25 @@ MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (size
48
48
  *****************************************************************/
49
49
  #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
50
50
  # include <stdint.h>
51
- typedef uint8_t BYTE;
52
- typedef uint16_t U16;
53
- typedef int16_t S16;
54
- typedef uint32_t U32;
55
- typedef int32_t S32;
56
- typedef uint64_t U64;
57
- typedef int64_t S64;
51
+ typedef uint8_t BYTE;
52
+ typedef uint16_t U16;
53
+ typedef int16_t S16;
54
+ typedef uint32_t U32;
55
+ typedef int32_t S32;
56
+ typedef uint64_t U64;
57
+ typedef int64_t S64;
58
+ typedef intptr_t iPtrDiff;
59
+ typedef uintptr_t uPtrDiff;
58
60
  #else
59
- typedef unsigned char BYTE;
61
+ typedef unsigned char BYTE;
60
62
  typedef unsigned short U16;
61
63
  typedef signed short S16;
62
64
  typedef unsigned int U32;
63
65
  typedef signed int S32;
64
66
  typedef unsigned long long U64;
65
67
  typedef signed long long S64;
68
+ typedef ptrdiff_t iPtrDiff;
69
+ typedef size_t uPtrDiff;
66
70
  #endif
67
71
 
68
72
 
@@ -74,11 +78,11 @@ MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (size
74
78
  * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
75
79
  * The below switch allow to select different access method for improved performance.
76
80
  * Method 0 (default) : use `memcpy()`. Safe and portable.
77
- * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
81
+ * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
78
82
  * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
79
83
  * Method 2 : direct access. This method is portable but violate C standard.
80
84
  * It can generate buggy code on targets depending on alignment.
81
- * In some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
85
+ * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
82
86
  * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
83
87
  * Prefer these methods in priority order (0 > 1 > 2)
84
88
  */
@@ -180,7 +184,7 @@ MEM_STATIC U32 MEM_swap32(U32 in)
180
184
  {
181
185
  #if defined(_MSC_VER) /* Visual Studio */
182
186
  return _byteswap_ulong(in);
183
- #elif defined (__GNUC__)
187
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
184
188
  return __builtin_bswap32(in);
185
189
  #else
186
190
  return ((in << 24) & 0xff000000 ) |
@@ -194,7 +198,7 @@ MEM_STATIC U64 MEM_swap64(U64 in)
194
198
  {
195
199
  #if defined(_MSC_VER) /* Visual Studio */
196
200
  return _byteswap_uint64(in);
197
- #elif defined (__GNUC__)
201
+ #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
198
202
  return __builtin_bswap64(in);
199
203
  #else
200
204
  return ((in << 56) & 0xff00000000000000ULL) |