zstdlib 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +5 -0
  3. data/ext/zstdlib/extconf.rb +1 -1
  4. data/ext/zstdlib/ruby/zlib-3.0/zstdlib.c +4994 -0
  5. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/bitstream.h +25 -16
  6. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/compiler.h +118 -4
  7. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/cpu.h +1 -3
  8. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.c +1 -1
  9. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/debug.h +12 -19
  10. data/ext/zstdlib/zstd-1.5.0/lib/common/entropy_common.c +362 -0
  11. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.c +2 -1
  12. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/error_private.h +3 -3
  13. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse.h +40 -12
  14. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/fse_decompress.c +139 -22
  15. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/huf.h +29 -7
  16. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/mem.h +69 -98
  17. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.c +23 -17
  18. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/pool.h +2 -2
  19. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.c +6 -5
  20. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/threading.h +0 -0
  21. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.c +20 -60
  22. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/xxhash.h +2 -2
  23. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_common.c +10 -10
  24. data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_deps.h +111 -0
  25. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/common/zstd_internal.h +105 -62
  26. data/ext/zstdlib/zstd-1.5.0/lib/common/zstd_trace.h +154 -0
  27. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/fse_compress.c +31 -24
  28. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.c +27 -29
  29. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/hist.h +2 -2
  30. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/huf_compress.c +265 -126
  31. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress.c +2843 -728
  32. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_internal.h +305 -63
  33. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.c +8 -8
  34. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_literals.h +1 -1
  35. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.c +29 -7
  36. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_sequences.h +1 -1
  37. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.c +22 -295
  38. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_compress_superblock.h +1 -1
  39. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_cwksp.h +204 -67
  40. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.c +25 -25
  41. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_double_fast.h +1 -1
  42. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.c +23 -23
  43. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_fast.h +1 -1
  44. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.c +2184 -0
  45. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_lazy.h +125 -0
  46. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.c +314 -211
  47. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_ldm.h +9 -2
  48. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstd_ldm_geartab.h +103 -0
  49. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.c +191 -46
  50. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstd_opt.h +1 -1
  51. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/compress/zstdmt_compress.c +93 -415
  52. data/ext/zstdlib/zstd-1.5.0/lib/compress/zstdmt_compress.h +110 -0
  53. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/huf_decompress.c +342 -239
  54. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.c +9 -9
  55. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_ddict.h +2 -2
  56. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress.c +369 -87
  57. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.c +191 -75
  58. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_block.h +6 -3
  59. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/decompress/zstd_decompress_internal.h +27 -11
  60. data/ext/zstdlib/zstd-1.5.0/lib/zdict.h +452 -0
  61. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/lib/zstd.h +568 -126
  62. data/ext/zstdlib/{zstd-1.4.5/lib/common → zstd-1.5.0/lib}/zstd_errors.h +2 -1
  63. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzclose.c +0 -0
  64. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzcompatibility.h +1 -1
  65. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzguts.h +0 -0
  66. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzlib.c +0 -0
  67. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzread.c +0 -0
  68. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/gzwrite.c +0 -0
  69. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.c +126 -44
  70. data/ext/zstdlib/{zstd-1.4.5 → zstd-1.5.0}/zlibWrapper/zstd_zlibwrapper.h +1 -1
  71. metadata +70 -65
  72. data/ext/zstdlib/zstd-1.4.5/lib/common/entropy_common.c +0 -216
  73. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.c +0 -1138
  74. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstd_lazy.h +0 -67
  75. data/ext/zstdlib/zstd-1.4.5/lib/compress/zstdmt_compress.h +0 -192
@@ -0,0 +1,110 @@
1
+ /*
2
+ * Copyright (c) Yann Collet, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under both the BSD-style license (found in the
6
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
+ * in the COPYING file in the root directory of this source tree).
8
+ * You may select, at your option, one of the above-listed licenses.
9
+ */
10
+
11
+ #ifndef ZSTDMT_COMPRESS_H
12
+ #define ZSTDMT_COMPRESS_H
13
+
14
+ #if defined (__cplusplus)
15
+ extern "C" {
16
+ #endif
17
+
18
+
19
+ /* Note : This is an internal API.
20
+ * These APIs used to be exposed with ZSTDLIB_API,
21
+ * because it used to be the only way to invoke MT compression.
22
+ * Now, you must use ZSTD_compress2 and ZSTD_compressStream2() instead.
23
+ *
24
+ * This API requires ZSTD_MULTITHREAD to be defined during compilation,
25
+ * otherwise ZSTDMT_createCCtx*() will fail.
26
+ */
27
+
28
+ /* === Dependencies === */
29
+ #include "../common/zstd_deps.h" /* size_t */
30
+ #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters */
31
+ #include "../zstd.h" /* ZSTD_inBuffer, ZSTD_outBuffer, ZSTDLIB_API */
32
+
33
+
34
+ /* === Constants === */
35
+ #ifndef ZSTDMT_NBWORKERS_MAX /* a different value can be selected at compile time */
36
+ # define ZSTDMT_NBWORKERS_MAX ((sizeof(void*)==4) /*32-bit*/ ? 64 : 256)
37
+ #endif
38
+ #ifndef ZSTDMT_JOBSIZE_MIN /* a different value can be selected at compile time */
39
+ # define ZSTDMT_JOBSIZE_MIN (512 KB)
40
+ #endif
41
+ #define ZSTDMT_JOBLOG_MAX (MEM_32bits() ? 29 : 30)
42
+ #define ZSTDMT_JOBSIZE_MAX (MEM_32bits() ? (512 MB) : (1024 MB))
43
+
44
+
45
+ /* ========================================================
46
+ * === Private interface, for use by ZSTD_compress.c ===
47
+ * === Not exposed in libzstd. Never invoke directly ===
48
+ * ======================================================== */
49
+
50
+ /* === Memory management === */
51
+ typedef struct ZSTDMT_CCtx_s ZSTDMT_CCtx;
52
+ /* Requires ZSTD_MULTITHREAD to be defined during compilation, otherwise it will return NULL. */
53
+ ZSTDMT_CCtx* ZSTDMT_createCCtx_advanced(unsigned nbWorkers,
54
+ ZSTD_customMem cMem,
55
+ ZSTD_threadPool *pool);
56
+ size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* mtctx);
57
+
58
+ size_t ZSTDMT_sizeof_CCtx(ZSTDMT_CCtx* mtctx);
59
+
60
+ /* === Streaming functions === */
61
+
62
+ size_t ZSTDMT_nextInputSizeHint(const ZSTDMT_CCtx* mtctx);
63
+
64
+ /*! ZSTDMT_initCStream_internal() :
65
+ * Private use only. Init streaming operation.
66
+ * expects params to be valid.
67
+ * must receive dict, or cdict, or none, but not both.
68
+ * @return : 0, or an error code */
69
+ size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs,
70
+ const void* dict, size_t dictSize, ZSTD_dictContentType_e dictContentType,
71
+ const ZSTD_CDict* cdict,
72
+ ZSTD_CCtx_params params, unsigned long long pledgedSrcSize);
73
+
74
+ /*! ZSTDMT_compressStream_generic() :
75
+ * Combines ZSTDMT_compressStream() with optional ZSTDMT_flushStream() or ZSTDMT_endStream()
76
+ * depending on flush directive.
77
+ * @return : minimum amount of data still to be flushed
78
+ * 0 if fully flushed
79
+ * or an error code
80
+ * note : needs to be init using any ZSTD_initCStream*() variant */
81
+ size_t ZSTDMT_compressStream_generic(ZSTDMT_CCtx* mtctx,
82
+ ZSTD_outBuffer* output,
83
+ ZSTD_inBuffer* input,
84
+ ZSTD_EndDirective endOp);
85
+
86
+ /*! ZSTDMT_toFlushNow()
87
+ * Tell how many bytes are ready to be flushed immediately.
88
+ * Probe the oldest active job (not yet entirely flushed) and check its output buffer.
89
+ * If return 0, it means there is no active job,
90
+ * or, it means oldest job is still active, but everything produced has been flushed so far,
91
+ * therefore flushing is limited by speed of oldest job. */
92
+ size_t ZSTDMT_toFlushNow(ZSTDMT_CCtx* mtctx);
93
+
94
+ /*! ZSTDMT_updateCParams_whileCompressing() :
95
+ * Updates only a selected set of compression parameters, to remain compatible with current frame.
96
+ * New parameters will be applied to next compression job. */
97
+ void ZSTDMT_updateCParams_whileCompressing(ZSTDMT_CCtx* mtctx, const ZSTD_CCtx_params* cctxParams);
98
+
99
+ /*! ZSTDMT_getFrameProgression():
100
+ * tells how much data has been consumed (input) and produced (output) for current frame.
101
+ * able to count progression inside worker threads.
102
+ */
103
+ ZSTD_frameProgression ZSTDMT_getFrameProgression(ZSTDMT_CCtx* mtctx);
104
+
105
+
106
+ #if defined (__cplusplus)
107
+ }
108
+ #endif
109
+
110
+ #endif /* ZSTDMT_COMPRESS_H */
@@ -1,7 +1,7 @@
1
1
  /* ******************************************************************
2
2
  * huff0 huffman decoder,
3
3
  * part of Finite State Entropy library
4
- * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
4
+ * Copyright (c) Yann Collet, Facebook, Inc.
5
5
  *
6
6
  * You can contact the author at :
7
7
  * - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -15,7 +15,7 @@
15
15
  /* **************************************************************
16
16
  * Dependencies
17
17
  ****************************************************************/
18
- #include <string.h> /* memcpy, memset */
18
+ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memset */
19
19
  #include "../common/compiler.h"
20
20
  #include "../common/bitstream.h" /* BIT_* */
21
21
  #include "../common/fse.h" /* to compress headers */
@@ -103,7 +103,7 @@ typedef struct { BYTE maxTableLog; BYTE tableType; BYTE tableLog; BYTE reserved;
103
103
  static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
104
104
  {
105
105
  DTableDesc dtd;
106
- memcpy(&dtd, table, sizeof(dtd));
106
+ ZSTD_memcpy(&dtd, table, sizeof(dtd));
107
107
  return dtd;
108
108
  }
109
109
 
@@ -115,29 +115,51 @@ static DTableDesc HUF_getDTableDesc(const HUF_DTable* table)
115
115
  /*-***************************/
116
116
  typedef struct { BYTE byte; BYTE nbBits; } HUF_DEltX1; /* single-symbol decoding */
117
117
 
118
+ /**
119
+ * Packs 4 HUF_DEltX1 structs into a U64. This is used to lay down 4 entries at
120
+ * a time.
121
+ */
122
+ static U64 HUF_DEltX1_set4(BYTE symbol, BYTE nbBits) {
123
+ U64 D4;
124
+ if (MEM_isLittleEndian()) {
125
+ D4 = symbol + (nbBits << 8);
126
+ } else {
127
+ D4 = (symbol << 8) + nbBits;
128
+ }
129
+ D4 *= 0x0001000100010001ULL;
130
+ return D4;
131
+ }
132
+
133
+ typedef struct {
134
+ U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1];
135
+ U32 rankStart[HUF_TABLELOG_ABSOLUTEMAX + 1];
136
+ U32 statsWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
137
+ BYTE symbols[HUF_SYMBOLVALUE_MAX + 1];
138
+ BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];
139
+ } HUF_ReadDTableX1_Workspace;
140
+
141
+
118
142
  size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize)
143
+ {
144
+ return HUF_readDTableX1_wksp_bmi2(DTable, src, srcSize, workSpace, wkspSize, /* bmi2 */ 0);
145
+ }
146
+
147
+ size_t HUF_readDTableX1_wksp_bmi2(HUF_DTable* DTable, const void* src, size_t srcSize, void* workSpace, size_t wkspSize, int bmi2)
119
148
  {
120
149
  U32 tableLog = 0;
121
150
  U32 nbSymbols = 0;
122
151
  size_t iSize;
123
152
  void* const dtPtr = DTable + 1;
124
153
  HUF_DEltX1* const dt = (HUF_DEltX1*)dtPtr;
154
+ HUF_ReadDTableX1_Workspace* wksp = (HUF_ReadDTableX1_Workspace*)workSpace;
125
155
 
126
- U32* rankVal;
127
- BYTE* huffWeight;
128
- size_t spaceUsed32 = 0;
129
-
130
- rankVal = (U32 *)workSpace + spaceUsed32;
131
- spaceUsed32 += HUF_TABLELOG_ABSOLUTEMAX + 1;
132
- huffWeight = (BYTE *)((U32 *)workSpace + spaceUsed32);
133
- spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
134
-
135
- if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
156
+ DEBUG_STATIC_ASSERT(HUF_DECOMPRESS_WORKSPACE_SIZE >= sizeof(*wksp));
157
+ if (sizeof(*wksp) > wkspSize) return ERROR(tableLog_tooLarge);
136
158
 
137
159
  DEBUG_STATIC_ASSERT(sizeof(DTableDesc) == sizeof(HUF_DTable));
138
- /* memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
160
+ /* ZSTD_memset(huffWeight, 0, sizeof(huffWeight)); */ /* is not necessary, even though some analyzer complain ... */
139
161
 
140
- iSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX + 1, rankVal, &nbSymbols, &tableLog, src, srcSize);
162
+ iSize = HUF_readStats_wksp(wksp->huffWeight, HUF_SYMBOLVALUE_MAX + 1, wksp->rankVal, &nbSymbols, &tableLog, src, srcSize, wksp->statsWksp, sizeof(wksp->statsWksp), bmi2);
141
163
  if (HUF_isError(iSize)) return iSize;
142
164
 
143
165
  /* Table header */
@@ -145,52 +167,117 @@ size_t HUF_readDTableX1_wksp(HUF_DTable* DTable, const void* src, size_t srcSize
145
167
  if (tableLog > (U32)(dtd.maxTableLog+1)) return ERROR(tableLog_tooLarge); /* DTable too small, Huffman tree cannot fit in */
146
168
  dtd.tableType = 0;
147
169
  dtd.tableLog = (BYTE)tableLog;
148
- memcpy(DTable, &dtd, sizeof(dtd));
170
+ ZSTD_memcpy(DTable, &dtd, sizeof(dtd));
149
171
  }
150
172
 
151
- /* Calculate starting value for each rank */
152
- { U32 n, nextRankStart = 0;
153
- for (n=1; n<tableLog+1; n++) {
154
- U32 const current = nextRankStart;
155
- nextRankStart += (rankVal[n] << (n-1));
156
- rankVal[n] = current;
157
- } }
173
+ /* Compute symbols and rankStart given rankVal:
174
+ *
175
+ * rankVal already contains the number of values of each weight.
176
+ *
177
+ * symbols contains the symbols ordered by weight. First are the rankVal[0]
178
+ * weight 0 symbols, followed by the rankVal[1] weight 1 symbols, and so on.
179
+ * symbols[0] is filled (but unused) to avoid a branch.
180
+ *
181
+ * rankStart contains the offset where each rank belongs in the DTable.
182
+ * rankStart[0] is not filled because there are no entries in the table for
183
+ * weight 0.
184
+ */
185
+ {
186
+ int n;
187
+ int nextRankStart = 0;
188
+ int const unroll = 4;
189
+ int const nLimit = (int)nbSymbols - unroll + 1;
190
+ for (n=0; n<(int)tableLog+1; n++) {
191
+ U32 const curr = nextRankStart;
192
+ nextRankStart += wksp->rankVal[n];
193
+ wksp->rankStart[n] = curr;
194
+ }
195
+ for (n=0; n < nLimit; n += unroll) {
196
+ int u;
197
+ for (u=0; u < unroll; ++u) {
198
+ size_t const w = wksp->huffWeight[n+u];
199
+ wksp->symbols[wksp->rankStart[w]++] = (BYTE)(n+u);
200
+ }
201
+ }
202
+ for (; n < (int)nbSymbols; ++n) {
203
+ size_t const w = wksp->huffWeight[n];
204
+ wksp->symbols[wksp->rankStart[w]++] = (BYTE)n;
205
+ }
206
+ }
158
207
 
159
- /* fill DTable */
160
- { U32 n;
161
- size_t const nEnd = nbSymbols;
162
- for (n=0; n<nEnd; n++) {
163
- size_t const w = huffWeight[n];
164
- size_t const length = (1 << w) >> 1;
165
- size_t const uStart = rankVal[w];
166
- size_t const uEnd = uStart + length;
167
- size_t u;
168
- HUF_DEltX1 D;
169
- D.byte = (BYTE)n;
170
- D.nbBits = (BYTE)(tableLog + 1 - w);
171
- rankVal[w] = (U32)uEnd;
172
- if (length < 4) {
173
- /* Use length in the loop bound so the compiler knows it is short. */
174
- for (u = 0; u < length; ++u)
175
- dt[uStart + u] = D;
176
- } else {
177
- /* Unroll the loop 4 times, we know it is a power of 2. */
178
- for (u = uStart; u < uEnd; u += 4) {
179
- dt[u + 0] = D;
180
- dt[u + 1] = D;
181
- dt[u + 2] = D;
182
- dt[u + 3] = D;
183
- } } } }
208
+ /* fill DTable
209
+ * We fill all entries of each weight in order.
210
+ * That way length is a constant for each iteration of the outter loop.
211
+ * We can switch based on the length to a different inner loop which is
212
+ * optimized for that particular case.
213
+ */
214
+ {
215
+ U32 w;
216
+ int symbol=wksp->rankVal[0];
217
+ int rankStart=0;
218
+ for (w=1; w<tableLog+1; ++w) {
219
+ int const symbolCount = wksp->rankVal[w];
220
+ int const length = (1 << w) >> 1;
221
+ int uStart = rankStart;
222
+ BYTE const nbBits = (BYTE)(tableLog + 1 - w);
223
+ int s;
224
+ int u;
225
+ switch (length) {
226
+ case 1:
227
+ for (s=0; s<symbolCount; ++s) {
228
+ HUF_DEltX1 D;
229
+ D.byte = wksp->symbols[symbol + s];
230
+ D.nbBits = nbBits;
231
+ dt[uStart] = D;
232
+ uStart += 1;
233
+ }
234
+ break;
235
+ case 2:
236
+ for (s=0; s<symbolCount; ++s) {
237
+ HUF_DEltX1 D;
238
+ D.byte = wksp->symbols[symbol + s];
239
+ D.nbBits = nbBits;
240
+ dt[uStart+0] = D;
241
+ dt[uStart+1] = D;
242
+ uStart += 2;
243
+ }
244
+ break;
245
+ case 4:
246
+ for (s=0; s<symbolCount; ++s) {
247
+ U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
248
+ MEM_write64(dt + uStart, D4);
249
+ uStart += 4;
250
+ }
251
+ break;
252
+ case 8:
253
+ for (s=0; s<symbolCount; ++s) {
254
+ U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
255
+ MEM_write64(dt + uStart, D4);
256
+ MEM_write64(dt + uStart + 4, D4);
257
+ uStart += 8;
258
+ }
259
+ break;
260
+ default:
261
+ for (s=0; s<symbolCount; ++s) {
262
+ U64 const D4 = HUF_DEltX1_set4(wksp->symbols[symbol + s], nbBits);
263
+ for (u=0; u < length; u += 16) {
264
+ MEM_write64(dt + uStart + u + 0, D4);
265
+ MEM_write64(dt + uStart + u + 4, D4);
266
+ MEM_write64(dt + uStart + u + 8, D4);
267
+ MEM_write64(dt + uStart + u + 12, D4);
268
+ }
269
+ assert(u == length);
270
+ uStart += length;
271
+ }
272
+ break;
273
+ }
274
+ symbol += symbolCount;
275
+ rankStart += symbolCount * length;
276
+ }
277
+ }
184
278
  return iSize;
185
279
  }
186
280
 
187
- size_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize)
188
- {
189
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
190
- return HUF_readDTableX1_wksp(DTable, src, srcSize,
191
- workSpace, sizeof(workSpace));
192
- }
193
-
194
281
  FORCE_INLINE_TEMPLATE BYTE
195
282
  HUF_decodeSymbolX1(BIT_DStream_t* Dstream, const HUF_DEltX1* dt, const U32 dtLog)
196
283
  {
@@ -389,20 +476,6 @@ size_t HUF_decompress1X1_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
389
476
  }
390
477
 
391
478
 
392
- size_t HUF_decompress1X1_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
393
- const void* cSrc, size_t cSrcSize)
394
- {
395
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
396
- return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
397
- workSpace, sizeof(workSpace));
398
- }
399
-
400
- size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
401
- {
402
- HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
403
- return HUF_decompress1X1_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
404
- }
405
-
406
479
  size_t HUF_decompress4X1_usingDTable(
407
480
  void* dst, size_t dstSize,
408
481
  const void* cSrc, size_t cSrcSize,
@@ -419,8 +492,7 @@ static size_t HUF_decompress4X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size
419
492
  {
420
493
  const BYTE* ip = (const BYTE*) cSrc;
421
494
 
422
- size_t const hSize = HUF_readDTableX1_wksp (dctx, cSrc, cSrcSize,
423
- workSpace, wkspSize);
495
+ size_t const hSize = HUF_readDTableX1_wksp_bmi2(dctx, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
424
496
  if (HUF_isError(hSize)) return hSize;
425
497
  if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
426
498
  ip += hSize; cSrcSize -= hSize;
@@ -436,18 +508,6 @@ size_t HUF_decompress4X1_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
436
508
  }
437
509
 
438
510
 
439
- size_t HUF_decompress4X1_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
440
- {
441
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
442
- return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
443
- workSpace, sizeof(workSpace));
444
- }
445
- size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
446
- {
447
- HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
448
- return HUF_decompress4X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
449
- }
450
-
451
511
  #endif /* HUF_FORCE_DECOMPRESS_X2 */
452
512
 
453
513
 
@@ -468,13 +528,15 @@ typedef rankValCol_t rankVal_t[HUF_TABLELOG_MAX];
468
528
  static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 sizeLog, const U32 consumed,
469
529
  const U32* rankValOrigin, const int minWeight,
470
530
  const sortedSymbol_t* sortedSymbols, const U32 sortedListSize,
471
- U32 nbBitsBaseline, U16 baseSeq)
531
+ U32 nbBitsBaseline, U16 baseSeq, U32* wksp, size_t wkspSize)
472
532
  {
473
533
  HUF_DEltX2 DElt;
474
- U32 rankVal[HUF_TABLELOG_MAX + 1];
534
+ U32* rankVal = wksp;
475
535
 
536
+ assert(wkspSize >= HUF_TABLELOG_MAX + 1);
537
+ (void)wkspSize;
476
538
  /* get pre-calculated rankVal */
477
- memcpy(rankVal, rankValOrigin, sizeof(rankVal));
539
+ ZSTD_memcpy(rankVal, rankValOrigin, sizeof(U32) * (HUF_TABLELOG_MAX + 1));
478
540
 
479
541
  /* fill skipped values */
480
542
  if (minWeight>1) {
@@ -509,14 +571,18 @@ static void HUF_fillDTableX2Level2(HUF_DEltX2* DTable, U32 sizeLog, const U32 co
509
571
  static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,
510
572
  const sortedSymbol_t* sortedList, const U32 sortedListSize,
511
573
  const U32* rankStart, rankVal_t rankValOrigin, const U32 maxWeight,
512
- const U32 nbBitsBaseline)
574
+ const U32 nbBitsBaseline, U32* wksp, size_t wkspSize)
513
575
  {
514
- U32 rankVal[HUF_TABLELOG_MAX + 1];
576
+ U32* rankVal = wksp;
515
577
  const int scaleLog = nbBitsBaseline - targetLog; /* note : targetLog >= srcLog, hence scaleLog <= 1 */
516
578
  const U32 minBits = nbBitsBaseline - maxWeight;
517
579
  U32 s;
518
580
 
519
- memcpy(rankVal, rankValOrigin, sizeof(rankVal));
581
+ assert(wkspSize >= HUF_TABLELOG_MAX + 1);
582
+ wksp += HUF_TABLELOG_MAX + 1;
583
+ wkspSize -= HUF_TABLELOG_MAX + 1;
584
+
585
+ ZSTD_memcpy(rankVal, rankValOrigin, sizeof(U32) * (HUF_TABLELOG_MAX + 1));
520
586
 
521
587
  /* fill DTable */
522
588
  for (s=0; s<sortedListSize; s++) {
@@ -534,7 +600,7 @@ static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,
534
600
  HUF_fillDTableX2Level2(DTable+start, targetLog-nbBits, nbBits,
535
601
  rankValOrigin[nbBits], minWeight,
536
602
  sortedList+sortedRank, sortedListSize-sortedRank,
537
- nbBitsBaseline, symbol);
603
+ nbBitsBaseline, symbol, wksp, wkspSize);
538
604
  } else {
539
605
  HUF_DEltX2 DElt;
540
606
  MEM_writeLE16(&(DElt.sequence), symbol);
@@ -548,6 +614,15 @@ static void HUF_fillDTableX2(HUF_DEltX2* DTable, const U32 targetLog,
548
614
  }
549
615
  }
550
616
 
617
+ typedef struct {
618
+ rankValCol_t rankVal[HUF_TABLELOG_MAX];
619
+ U32 rankStats[HUF_TABLELOG_MAX + 1];
620
+ U32 rankStart0[HUF_TABLELOG_MAX + 2];
621
+ sortedSymbol_t sortedSymbol[HUF_SYMBOLVALUE_MAX + 1];
622
+ BYTE weightList[HUF_SYMBOLVALUE_MAX + 1];
623
+ U32 calleeWksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
624
+ } HUF_ReadDTableX2_Workspace;
625
+
551
626
  size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
552
627
  const void* src, size_t srcSize,
553
628
  void* workSpace, size_t wkspSize)
@@ -560,48 +635,33 @@ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
560
635
  HUF_DEltX2* const dt = (HUF_DEltX2*)dtPtr;
561
636
  U32 *rankStart;
562
637
 
563
- rankValCol_t* rankVal;
564
- U32* rankStats;
565
- U32* rankStart0;
566
- sortedSymbol_t* sortedSymbol;
567
- BYTE* weightList;
568
- size_t spaceUsed32 = 0;
569
-
570
- rankVal = (rankValCol_t *)((U32 *)workSpace + spaceUsed32);
571
- spaceUsed32 += (sizeof(rankValCol_t) * HUF_TABLELOG_MAX) >> 2;
572
- rankStats = (U32 *)workSpace + spaceUsed32;
573
- spaceUsed32 += HUF_TABLELOG_MAX + 1;
574
- rankStart0 = (U32 *)workSpace + spaceUsed32;
575
- spaceUsed32 += HUF_TABLELOG_MAX + 2;
576
- sortedSymbol = (sortedSymbol_t *)workSpace + (spaceUsed32 * sizeof(U32)) / sizeof(sortedSymbol_t);
577
- spaceUsed32 += HUF_ALIGN(sizeof(sortedSymbol_t) * (HUF_SYMBOLVALUE_MAX + 1), sizeof(U32)) >> 2;
578
- weightList = (BYTE *)((U32 *)workSpace + spaceUsed32);
579
- spaceUsed32 += HUF_ALIGN(HUF_SYMBOLVALUE_MAX + 1, sizeof(U32)) >> 2;
580
-
581
- if ((spaceUsed32 << 2) > wkspSize) return ERROR(tableLog_tooLarge);
582
-
583
- rankStart = rankStart0 + 1;
584
- memset(rankStats, 0, sizeof(U32) * (2 * HUF_TABLELOG_MAX + 2 + 1));
638
+ HUF_ReadDTableX2_Workspace* const wksp = (HUF_ReadDTableX2_Workspace*)workSpace;
639
+
640
+ if (sizeof(*wksp) > wkspSize) return ERROR(GENERIC);
641
+
642
+ rankStart = wksp->rankStart0 + 1;
643
+ ZSTD_memset(wksp->rankStats, 0, sizeof(wksp->rankStats));
644
+ ZSTD_memset(wksp->rankStart0, 0, sizeof(wksp->rankStart0));
585
645
 
586
646
  DEBUG_STATIC_ASSERT(sizeof(HUF_DEltX2) == sizeof(HUF_DTable)); /* if compiler fails here, assertion is wrong */
587
647
  if (maxTableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
588
- /* memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
648
+ /* ZSTD_memset(weightList, 0, sizeof(weightList)); */ /* is not necessary, even though some analyzer complain ... */
589
649
 
590
- iSize = HUF_readStats(weightList, HUF_SYMBOLVALUE_MAX + 1, rankStats, &nbSymbols, &tableLog, src, srcSize);
650
+ iSize = HUF_readStats_wksp(wksp->weightList, HUF_SYMBOLVALUE_MAX + 1, wksp->rankStats, &nbSymbols, &tableLog, src, srcSize, wksp->calleeWksp, sizeof(wksp->calleeWksp), /* bmi2 */ 0);
591
651
  if (HUF_isError(iSize)) return iSize;
592
652
 
593
653
  /* check result */
594
654
  if (tableLog > maxTableLog) return ERROR(tableLog_tooLarge); /* DTable can't fit code depth */
595
655
 
596
656
  /* find maxWeight */
597
- for (maxW = tableLog; rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
657
+ for (maxW = tableLog; wksp->rankStats[maxW]==0; maxW--) {} /* necessarily finds a solution before 0 */
598
658
 
599
659
  /* Get start index of each weight */
600
660
  { U32 w, nextRankStart = 0;
601
661
  for (w=1; w<maxW+1; w++) {
602
- U32 current = nextRankStart;
603
- nextRankStart += rankStats[w];
604
- rankStart[w] = current;
662
+ U32 curr = nextRankStart;
663
+ nextRankStart += wksp->rankStats[w];
664
+ rankStart[w] = curr;
605
665
  }
606
666
  rankStart[0] = nextRankStart; /* put all 0w symbols at the end of sorted list*/
607
667
  sizeOfSort = nextRankStart;
@@ -610,57 +670,51 @@ size_t HUF_readDTableX2_wksp(HUF_DTable* DTable,
610
670
  /* sort symbols by weight */
611
671
  { U32 s;
612
672
  for (s=0; s<nbSymbols; s++) {
613
- U32 const w = weightList[s];
673
+ U32 const w = wksp->weightList[s];
614
674
  U32 const r = rankStart[w]++;
615
- sortedSymbol[r].symbol = (BYTE)s;
616
- sortedSymbol[r].weight = (BYTE)w;
675
+ wksp->sortedSymbol[r].symbol = (BYTE)s;
676
+ wksp->sortedSymbol[r].weight = (BYTE)w;
617
677
  }
618
678
  rankStart[0] = 0; /* forget 0w symbols; this is beginning of weight(1) */
619
679
  }
620
680
 
621
681
  /* Build rankVal */
622
- { U32* const rankVal0 = rankVal[0];
682
+ { U32* const rankVal0 = wksp->rankVal[0];
623
683
  { int const rescale = (maxTableLog-tableLog) - 1; /* tableLog <= maxTableLog */
624
684
  U32 nextRankVal = 0;
625
685
  U32 w;
626
686
  for (w=1; w<maxW+1; w++) {
627
- U32 current = nextRankVal;
628
- nextRankVal += rankStats[w] << (w+rescale);
629
- rankVal0[w] = current;
687
+ U32 curr = nextRankVal;
688
+ nextRankVal += wksp->rankStats[w] << (w+rescale);
689
+ rankVal0[w] = curr;
630
690
  } }
631
691
  { U32 const minBits = tableLog+1 - maxW;
632
692
  U32 consumed;
633
693
  for (consumed = minBits; consumed < maxTableLog - minBits + 1; consumed++) {
634
- U32* const rankValPtr = rankVal[consumed];
694
+ U32* const rankValPtr = wksp->rankVal[consumed];
635
695
  U32 w;
636
696
  for (w = 1; w < maxW+1; w++) {
637
697
  rankValPtr[w] = rankVal0[w] >> consumed;
638
698
  } } } }
639
699
 
640
700
  HUF_fillDTableX2(dt, maxTableLog,
641
- sortedSymbol, sizeOfSort,
642
- rankStart0, rankVal, maxW,
643
- tableLog+1);
701
+ wksp->sortedSymbol, sizeOfSort,
702
+ wksp->rankStart0, wksp->rankVal, maxW,
703
+ tableLog+1,
704
+ wksp->calleeWksp, sizeof(wksp->calleeWksp) / sizeof(U32));
644
705
 
645
706
  dtd.tableLog = (BYTE)maxTableLog;
646
707
  dtd.tableType = 1;
647
- memcpy(DTable, &dtd, sizeof(dtd));
708
+ ZSTD_memcpy(DTable, &dtd, sizeof(dtd));
648
709
  return iSize;
649
710
  }
650
711
 
651
- size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)
652
- {
653
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
654
- return HUF_readDTableX2_wksp(DTable, src, srcSize,
655
- workSpace, sizeof(workSpace));
656
- }
657
-
658
712
 
659
713
  FORCE_INLINE_TEMPLATE U32
660
714
  HUF_decodeSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
661
715
  {
662
716
  size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
663
- memcpy(op, dt+val, 2);
717
+ ZSTD_memcpy(op, dt+val, 2);
664
718
  BIT_skipBits(DStream, dt[val].nbBits);
665
719
  return dt[val].length;
666
720
  }
@@ -669,7 +723,7 @@ FORCE_INLINE_TEMPLATE U32
669
723
  HUF_decodeLastSymbolX2(void* op, BIT_DStream_t* DStream, const HUF_DEltX2* dt, const U32 dtLog)
670
724
  {
671
725
  size_t const val = BIT_lookBitsFast(DStream, dtLog); /* note : dtLog >= 1 */
672
- memcpy(op, dt+val, 1);
726
+ ZSTD_memcpy(op, dt+val, 1);
673
727
  if (dt[val].length==1) BIT_skipBits(DStream, dt[val].nbBits);
674
728
  else {
675
729
  if (DStream->bitsConsumed < (sizeof(DStream->bitContainer)*8)) {
@@ -890,20 +944,6 @@ size_t HUF_decompress1X2_DCtx_wksp(HUF_DTable* DCtx, void* dst, size_t dstSize,
890
944
  }
891
945
 
892
946
 
893
- size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
894
- const void* cSrc, size_t cSrcSize)
895
- {
896
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
897
- return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
898
- workSpace, sizeof(workSpace));
899
- }
900
-
901
- size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
902
- {
903
- HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
904
- return HUF_decompress1X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
905
- }
906
-
907
947
  size_t HUF_decompress4X2_usingDTable(
908
948
  void* dst, size_t dstSize,
909
949
  const void* cSrc, size_t cSrcSize,
@@ -937,20 +977,6 @@ size_t HUF_decompress4X2_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
937
977
  }
938
978
 
939
979
 
940
- size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
941
- const void* cSrc, size_t cSrcSize)
942
- {
943
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
944
- return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
945
- workSpace, sizeof(workSpace));
946
- }
947
-
948
- size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
949
- {
950
- HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
951
- return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
952
- }
953
-
954
980
  #endif /* HUF_FORCE_DECOMPRESS_X1 */
955
981
 
956
982
 
@@ -1051,67 +1077,6 @@ U32 HUF_selectDecoder (size_t dstSize, size_t cSrcSize)
1051
1077
  }
1052
1078
 
1053
1079
 
1054
- typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
1055
-
1056
- size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1057
- {
1058
- #if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)
1059
- static const decompressionAlgo decompress[2] = { HUF_decompress4X1, HUF_decompress4X2 };
1060
- #endif
1061
-
1062
- /* validation checks */
1063
- if (dstSize == 0) return ERROR(dstSize_tooSmall);
1064
- if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1065
- if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1066
- if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1067
-
1068
- { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1069
- #if defined(HUF_FORCE_DECOMPRESS_X1)
1070
- (void)algoNb;
1071
- assert(algoNb == 0);
1072
- return HUF_decompress4X1(dst, dstSize, cSrc, cSrcSize);
1073
- #elif defined(HUF_FORCE_DECOMPRESS_X2)
1074
- (void)algoNb;
1075
- assert(algoNb == 1);
1076
- return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize);
1077
- #else
1078
- return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
1079
- #endif
1080
- }
1081
- }
1082
-
1083
- size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1084
- {
1085
- /* validation checks */
1086
- if (dstSize == 0) return ERROR(dstSize_tooSmall);
1087
- if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1088
- if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1089
- if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1090
-
1091
- { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1092
- #if defined(HUF_FORCE_DECOMPRESS_X1)
1093
- (void)algoNb;
1094
- assert(algoNb == 0);
1095
- return HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
1096
- #elif defined(HUF_FORCE_DECOMPRESS_X2)
1097
- (void)algoNb;
1098
- assert(algoNb == 1);
1099
- return HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
1100
- #else
1101
- return algoNb ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
1102
- HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
1103
- #endif
1104
- }
1105
- }
1106
-
1107
- size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1108
- {
1109
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1110
- return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1111
- workSpace, sizeof(workSpace));
1112
- }
1113
-
1114
-
1115
1080
  size_t HUF_decompress4X_hufOnly_wksp(HUF_DTable* dctx, void* dst,
1116
1081
  size_t dstSize, const void* cSrc,
1117
1082
  size_t cSrcSize, void* workSpace,
@@ -1145,8 +1110,8 @@ size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
1145
1110
  /* validation checks */
1146
1111
  if (dstSize == 0) return ERROR(dstSize_tooSmall);
1147
1112
  if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1148
- if (cSrcSize == dstSize) { memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1149
- if (cSrcSize == 1) { memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1113
+ if (cSrcSize == dstSize) { ZSTD_memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1114
+ if (cSrcSize == 1) { ZSTD_memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1150
1115
 
1151
1116
  { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1152
1117
  #if defined(HUF_FORCE_DECOMPRESS_X1)
@@ -1168,14 +1133,6 @@ size_t HUF_decompress1X_DCtx_wksp(HUF_DTable* dctx, void* dst, size_t dstSize,
1168
1133
  }
1169
1134
  }
1170
1135
 
1171
- size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
1172
- const void* cSrc, size_t cSrcSize)
1173
- {
1174
- U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1175
- return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1176
- workSpace, sizeof(workSpace));
1177
- }
1178
-
1179
1136
 
1180
1137
  size_t HUF_decompress1X_usingDTable_bmi2(void* dst, size_t maxDstSize, const void* cSrc, size_t cSrcSize, const HUF_DTable* DTable, int bmi2)
1181
1138
  {
@@ -1199,7 +1156,7 @@ size_t HUF_decompress1X1_DCtx_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t dstS
1199
1156
  {
1200
1157
  const BYTE* ip = (const BYTE*) cSrc;
1201
1158
 
1202
- size_t const hSize = HUF_readDTableX1_wksp(dctx, cSrc, cSrcSize, workSpace, wkspSize);
1159
+ size_t const hSize = HUF_readDTableX1_wksp_bmi2(dctx, cSrc, cSrcSize, workSpace, wkspSize, bmi2);
1203
1160
  if (HUF_isError(hSize)) return hSize;
1204
1161
  if (hSize >= cSrcSize) return ERROR(srcSize_wrong);
1205
1162
  ip += hSize; cSrcSize -= hSize;
@@ -1246,3 +1203,149 @@ size_t HUF_decompress4X_hufOnly_wksp_bmi2(HUF_DTable* dctx, void* dst, size_t ds
1246
1203
  #endif
1247
1204
  }
1248
1205
  }
1206
+
1207
+ #ifndef ZSTD_NO_UNUSED_FUNCTIONS
1208
+ #ifndef HUF_FORCE_DECOMPRESS_X2
1209
+ size_t HUF_readDTableX1(HUF_DTable* DTable, const void* src, size_t srcSize)
1210
+ {
1211
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1212
+ return HUF_readDTableX1_wksp(DTable, src, srcSize,
1213
+ workSpace, sizeof(workSpace));
1214
+ }
1215
+
1216
+ size_t HUF_decompress1X1_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
1217
+ const void* cSrc, size_t cSrcSize)
1218
+ {
1219
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1220
+ return HUF_decompress1X1_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
1221
+ workSpace, sizeof(workSpace));
1222
+ }
1223
+
1224
+ size_t HUF_decompress1X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1225
+ {
1226
+ HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
1227
+ return HUF_decompress1X1_DCtx (DTable, dst, dstSize, cSrc, cSrcSize);
1228
+ }
1229
+ #endif
1230
+
1231
+ #ifndef HUF_FORCE_DECOMPRESS_X1
1232
+ size_t HUF_readDTableX2(HUF_DTable* DTable, const void* src, size_t srcSize)
1233
+ {
1234
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1235
+ return HUF_readDTableX2_wksp(DTable, src, srcSize,
1236
+ workSpace, sizeof(workSpace));
1237
+ }
1238
+
1239
+ size_t HUF_decompress1X2_DCtx(HUF_DTable* DCtx, void* dst, size_t dstSize,
1240
+ const void* cSrc, size_t cSrcSize)
1241
+ {
1242
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1243
+ return HUF_decompress1X2_DCtx_wksp(DCtx, dst, dstSize, cSrc, cSrcSize,
1244
+ workSpace, sizeof(workSpace));
1245
+ }
1246
+
1247
+ size_t HUF_decompress1X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1248
+ {
1249
+ HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
1250
+ return HUF_decompress1X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
1251
+ }
1252
+ #endif
1253
+
1254
+ #ifndef HUF_FORCE_DECOMPRESS_X2
1255
+ size_t HUF_decompress4X1_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1256
+ {
1257
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1258
+ return HUF_decompress4X1_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1259
+ workSpace, sizeof(workSpace));
1260
+ }
1261
+ size_t HUF_decompress4X1 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1262
+ {
1263
+ HUF_CREATE_STATIC_DTABLEX1(DTable, HUF_TABLELOG_MAX);
1264
+ return HUF_decompress4X1_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
1265
+ }
1266
+ #endif
1267
+
1268
+ #ifndef HUF_FORCE_DECOMPRESS_X1
1269
+ size_t HUF_decompress4X2_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
1270
+ const void* cSrc, size_t cSrcSize)
1271
+ {
1272
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1273
+ return HUF_decompress4X2_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1274
+ workSpace, sizeof(workSpace));
1275
+ }
1276
+
1277
+ size_t HUF_decompress4X2 (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1278
+ {
1279
+ HUF_CREATE_STATIC_DTABLEX2(DTable, HUF_TABLELOG_MAX);
1280
+ return HUF_decompress4X2_DCtx(DTable, dst, dstSize, cSrc, cSrcSize);
1281
+ }
1282
+ #endif
1283
+
1284
+ typedef size_t (*decompressionAlgo)(void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize);
1285
+
1286
+ size_t HUF_decompress (void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1287
+ {
1288
+ #if !defined(HUF_FORCE_DECOMPRESS_X1) && !defined(HUF_FORCE_DECOMPRESS_X2)
1289
+ static const decompressionAlgo decompress[2] = { HUF_decompress4X1, HUF_decompress4X2 };
1290
+ #endif
1291
+
1292
+ /* validation checks */
1293
+ if (dstSize == 0) return ERROR(dstSize_tooSmall);
1294
+ if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1295
+ if (cSrcSize == dstSize) { ZSTD_memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1296
+ if (cSrcSize == 1) { ZSTD_memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1297
+
1298
+ { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1299
+ #if defined(HUF_FORCE_DECOMPRESS_X1)
1300
+ (void)algoNb;
1301
+ assert(algoNb == 0);
1302
+ return HUF_decompress4X1(dst, dstSize, cSrc, cSrcSize);
1303
+ #elif defined(HUF_FORCE_DECOMPRESS_X2)
1304
+ (void)algoNb;
1305
+ assert(algoNb == 1);
1306
+ return HUF_decompress4X2(dst, dstSize, cSrc, cSrcSize);
1307
+ #else
1308
+ return decompress[algoNb](dst, dstSize, cSrc, cSrcSize);
1309
+ #endif
1310
+ }
1311
+ }
1312
+
1313
+ size_t HUF_decompress4X_DCtx (HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1314
+ {
1315
+ /* validation checks */
1316
+ if (dstSize == 0) return ERROR(dstSize_tooSmall);
1317
+ if (cSrcSize > dstSize) return ERROR(corruption_detected); /* invalid */
1318
+ if (cSrcSize == dstSize) { ZSTD_memcpy(dst, cSrc, dstSize); return dstSize; } /* not compressed */
1319
+ if (cSrcSize == 1) { ZSTD_memset(dst, *(const BYTE*)cSrc, dstSize); return dstSize; } /* RLE */
1320
+
1321
+ { U32 const algoNb = HUF_selectDecoder(dstSize, cSrcSize);
1322
+ #if defined(HUF_FORCE_DECOMPRESS_X1)
1323
+ (void)algoNb;
1324
+ assert(algoNb == 0);
1325
+ return HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
1326
+ #elif defined(HUF_FORCE_DECOMPRESS_X2)
1327
+ (void)algoNb;
1328
+ assert(algoNb == 1);
1329
+ return HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize);
1330
+ #else
1331
+ return algoNb ? HUF_decompress4X2_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) :
1332
+ HUF_decompress4X1_DCtx(dctx, dst, dstSize, cSrc, cSrcSize) ;
1333
+ #endif
1334
+ }
1335
+ }
1336
+
1337
+ size_t HUF_decompress4X_hufOnly(HUF_DTable* dctx, void* dst, size_t dstSize, const void* cSrc, size_t cSrcSize)
1338
+ {
1339
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1340
+ return HUF_decompress4X_hufOnly_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1341
+ workSpace, sizeof(workSpace));
1342
+ }
1343
+
1344
+ size_t HUF_decompress1X_DCtx(HUF_DTable* dctx, void* dst, size_t dstSize,
1345
+ const void* cSrc, size_t cSrcSize)
1346
+ {
1347
+ U32 workSpace[HUF_DECOMPRESS_WORKSPACE_SIZE_U32];
1348
+ return HUF_decompress1X_DCtx_wksp(dctx, dst, dstSize, cSrc, cSrcSize,
1349
+ workSpace, sizeof(workSpace));
1350
+ }
1351
+ #endif