extzstd 0.0.3.CONCEPT → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
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,36 +1,16 @@
1
- /*
2
- Common functions of New Generation Entropy library
3
- Copyright (C) 2016, 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+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
32
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
33
- *************************************************************************** */
1
+ /* ******************************************************************
2
+ * Common functions of New Generation Entropy library
3
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4
+ *
5
+ * You can contact the author at :
6
+ * - FSE+HUF 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.
13
+ ****************************************************************** */
34
14
 
35
15
  /* *************************************
36
16
  * Dependencies
@@ -38,33 +18,26 @@
38
18
  #include "mem.h"
39
19
  #include "error_private.h" /* ERR_*, ERROR */
40
20
  #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
41
- #include "fse.h" /* FSE_isError, FSE_getErrorName */
21
+ #include "fse.h"
42
22
  #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
43
- #include "huf.h" /* HUF_isError, HUF_getErrorName */
23
+ #include "huf.h"
44
24
 
45
25
 
26
+ /*=== Version ===*/
27
+ unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
46
28
 
47
- /*-****************************************
48
- * FSE Error Management
49
- ******************************************/
50
- unsigned FSE_isError(size_t code) { return ERR_isError(code); }
51
29
 
30
+ /*=== Error Management ===*/
31
+ unsigned FSE_isError(size_t code) { return ERR_isError(code); }
52
32
  const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
53
33
 
54
-
55
- /* **************************************************************
56
- * HUF Error Management
57
- ****************************************************************/
58
34
  unsigned HUF_isError(size_t code) { return ERR_isError(code); }
59
-
60
35
  const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
61
36
 
62
37
 
63
38
  /*-**************************************************************
64
39
  * FSE NCount encoding-decoding
65
40
  ****************************************************************/
66
- static short FSE_abs(short a) { return a<0 ? -a : a; }
67
-
68
41
  size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
69
42
  const void* headerBuffer, size_t hbSize)
70
43
  {
@@ -79,7 +52,21 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
79
52
  unsigned charnum = 0;
80
53
  int previous0 = 0;
81
54
 
82
- if (hbSize < 4) return ERROR(srcSize_wrong);
55
+ if (hbSize < 4) {
56
+ /* This function only works when hbSize >= 4 */
57
+ char buffer[4];
58
+ memset(buffer, 0, sizeof(buffer));
59
+ memcpy(buffer, headerBuffer, hbSize);
60
+ { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
61
+ buffer, sizeof(buffer));
62
+ if (FSE_isError(countSize)) return countSize;
63
+ if (countSize > hbSize) return ERROR(corruption_detected);
64
+ return countSize;
65
+ } }
66
+ assert(hbSize >= 4);
67
+
68
+ /* init */
69
+ memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
83
70
  bitStream = MEM_readLE32(ip);
84
71
  nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
85
72
  if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
@@ -90,50 +77,50 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
90
77
  threshold = 1<<nbBits;
91
78
  nbBits++;
92
79
 
93
- while ((remaining>1) && (charnum<=*maxSVPtr)) {
80
+ while ((remaining>1) & (charnum<=*maxSVPtr)) {
94
81
  if (previous0) {
95
82
  unsigned n0 = charnum;
96
83
  while ((bitStream & 0xFFFF) == 0xFFFF) {
97
- n0+=24;
84
+ n0 += 24;
98
85
  if (ip < iend-5) {
99
- ip+=2;
86
+ ip += 2;
100
87
  bitStream = MEM_readLE32(ip) >> bitCount;
101
88
  } else {
102
89
  bitStream >>= 16;
103
- bitCount+=16;
90
+ bitCount += 16;
104
91
  } }
105
92
  while ((bitStream & 3) == 3) {
106
- n0+=3;
107
- bitStream>>=2;
108
- bitCount+=2;
93
+ n0 += 3;
94
+ bitStream >>= 2;
95
+ bitCount += 2;
109
96
  }
110
97
  n0 += bitStream & 3;
111
98
  bitCount += 2;
112
99
  if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
113
100
  while (charnum < n0) normalizedCounter[charnum++] = 0;
114
101
  if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
102
+ assert((bitCount >> 3) <= 3); /* For first condition to work */
115
103
  ip += bitCount>>3;
116
104
  bitCount &= 7;
117
105
  bitStream = MEM_readLE32(ip) >> bitCount;
118
- }
119
- else
106
+ } else {
120
107
  bitStream >>= 2;
121
- }
122
- { short const max = (short)((2*threshold-1)-remaining);
123
- short count;
108
+ } }
109
+ { int const max = (2*threshold-1) - remaining;
110
+ int count;
124
111
 
125
112
  if ((bitStream & (threshold-1)) < (U32)max) {
126
- count = (short)(bitStream & (threshold-1));
127
- bitCount += nbBits-1;
113
+ count = bitStream & (threshold-1);
114
+ bitCount += nbBits-1;
128
115
  } else {
129
- count = (short)(bitStream & (2*threshold-1));
116
+ count = bitStream & (2*threshold-1);
130
117
  if (count >= threshold) count -= max;
131
- bitCount += nbBits;
118
+ bitCount += nbBits;
132
119
  }
133
120
 
134
121
  count--; /* extra accuracy */
135
- remaining -= FSE_abs(count);
136
- normalizedCounter[charnum++] = count;
122
+ remaining -= count < 0 ? -count : count; /* -1 means +1 */
123
+ normalizedCounter[charnum++] = (short)count;
137
124
  previous0 = !count;
138
125
  while (remaining < threshold) {
139
126
  nbBits--;
@@ -148,12 +135,12 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
148
135
  ip = iend - 4;
149
136
  }
150
137
  bitStream = MEM_readLE32(ip) >> (bitCount & 31);
151
- } } /* while ((remaining>1) && (charnum<=*maxSVPtr)) */
152
- if (remaining != 1) return ERROR(GENERIC);
138
+ } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
139
+ if (remaining != 1) return ERROR(corruption_detected);
140
+ if (bitCount > 32) return ERROR(corruption_detected);
153
141
  *maxSVPtr = charnum-1;
154
142
 
155
143
  ip += (bitCount+7)>>3;
156
- if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
157
144
  return ip-istart;
158
145
  }
159
146
 
@@ -161,8 +148,9 @@ size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* t
161
148
  /*! HUF_readStats() :
162
149
  Read compact Huffman tree, saved by HUF_writeCTable().
163
150
  `huffWeight` is destination buffer.
151
+ `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
164
152
  @return : size read from `src` , or an error Code .
165
- Note : Needed by HUF_readCTable() and HUF_readDTableXn() .
153
+ Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
166
154
  */
167
155
  size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
168
156
  U32* nbSymbolsPtr, U32* tableLogPtr,
@@ -170,47 +158,44 @@ size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
170
158
  {
171
159
  U32 weightTotal;
172
160
  const BYTE* ip = (const BYTE*) src;
173
- size_t iSize = ip[0];
161
+ size_t iSize;
174
162
  size_t oSize;
175
163
 
176
- //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
177
-
178
- if (iSize >= 128) { /* special header */
179
- if (iSize >= (242)) { /* RLE */
180
- static U32 l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
181
- oSize = l[iSize-242];
182
- memset(huffWeight, 1, hwSize);
183
- iSize = 0;
184
- }
185
- else { /* Incompressible */
186
- oSize = iSize - 127;
187
- iSize = ((oSize+1)/2);
188
- if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
189
- if (oSize >= hwSize) return ERROR(corruption_detected);
190
- ip += 1;
191
- { U32 n;
192
- for (n=0; n<oSize; n+=2) {
193
- huffWeight[n] = ip[n/2] >> 4;
194
- huffWeight[n+1] = ip[n/2] & 15;
195
- } } } }
164
+ if (!srcSize) return ERROR(srcSize_wrong);
165
+ iSize = ip[0];
166
+ /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
167
+
168
+ if (iSize >= 128) { /* special header */
169
+ oSize = iSize - 127;
170
+ iSize = ((oSize+1)/2);
171
+ if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
172
+ if (oSize >= hwSize) return ERROR(corruption_detected);
173
+ ip += 1;
174
+ { U32 n;
175
+ for (n=0; n<oSize; n+=2) {
176
+ huffWeight[n] = ip[n/2] >> 4;
177
+ huffWeight[n+1] = ip[n/2] & 15;
178
+ } } }
196
179
  else { /* header compressed with FSE (normal case) */
180
+ FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
197
181
  if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
198
- oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
182
+ oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
199
183
  if (FSE_isError(oSize)) return oSize;
200
184
  }
201
185
 
202
186
  /* collect weight stats */
203
- memset(rankStats, 0, (HUF_TABLELOG_ABSOLUTEMAX + 1) * sizeof(U32));
187
+ memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
204
188
  weightTotal = 0;
205
189
  { U32 n; for (n=0; n<oSize; n++) {
206
- if (huffWeight[n] >= HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
190
+ if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
207
191
  rankStats[huffWeight[n]]++;
208
192
  weightTotal += (1 << huffWeight[n]) >> 1;
209
193
  } }
194
+ if (weightTotal == 0) return ERROR(corruption_detected);
210
195
 
211
196
  /* get last non-null symbol weight (implied, total must be 2^n) */
212
197
  { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
213
- if (tableLog > HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
198
+ if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
214
199
  *tableLogPtr = tableLog;
215
200
  /* determine last weight */
216
201
  { U32 const total = 1 << tableLog;
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, 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
+ /* The purpose of this file is to have a single list of error strings embedded in binary */
12
+
13
+ #include "error_private.h"
14
+
15
+ const char* ERR_getErrorString(ERR_enum code)
16
+ {
17
+ #ifdef ZSTD_STRIP_ERROR_STRINGS
18
+ (void)code;
19
+ return "Error strings stripped";
20
+ #else
21
+ static const char* const notErrorCode = "Unspecified error code";
22
+ switch( code )
23
+ {
24
+ case PREFIX(no_error): return "No error detected";
25
+ case PREFIX(GENERIC): return "Error (generic)";
26
+ case PREFIX(prefix_unknown): return "Unknown frame descriptor";
27
+ case PREFIX(version_unsupported): return "Version not supported";
28
+ case PREFIX(frameParameter_unsupported): return "Unsupported frame parameter";
29
+ case PREFIX(frameParameter_windowTooLarge): return "Frame requires too much memory for decoding";
30
+ case PREFIX(corruption_detected): return "Corrupted block detected";
31
+ case PREFIX(checksum_wrong): return "Restored data doesn't match checksum";
32
+ case PREFIX(parameter_unsupported): return "Unsupported parameter";
33
+ case PREFIX(parameter_outOfBound): return "Parameter is out of bound";
34
+ case PREFIX(init_missing): return "Context should be init first";
35
+ case PREFIX(memory_allocation): return "Allocation error : not enough memory";
36
+ case PREFIX(workSpace_tooSmall): return "workSpace buffer is not large enough";
37
+ case PREFIX(stage_wrong): return "Operation not authorized at current processing stage";
38
+ case PREFIX(tableLog_tooLarge): return "tableLog requires too much memory : unsupported";
39
+ case PREFIX(maxSymbolValue_tooLarge): return "Unsupported max Symbol Value : too large";
40
+ case PREFIX(maxSymbolValue_tooSmall): return "Specified maxSymbolValue is too small";
41
+ case PREFIX(dictionary_corrupted): return "Dictionary is corrupted";
42
+ case PREFIX(dictionary_wrong): return "Dictionary mismatch";
43
+ case PREFIX(dictionaryCreation_failed): return "Cannot create Dictionary from provided samples";
44
+ case PREFIX(dstSize_tooSmall): return "Destination buffer is too small";
45
+ case PREFIX(srcSize_wrong): return "Src size is incorrect";
46
+ case PREFIX(dstBuffer_null): return "Operation on NULL destination buffer";
47
+ /* following error codes are not stable and may be removed or changed in a future version */
48
+ case PREFIX(frameIndex_tooLarge): return "Frame index is too large";
49
+ case PREFIX(seekableIO): return "An I/O error occurred when reading/seeking";
50
+ case PREFIX(dstBuffer_wrong): return "Destination buffer is wrong";
51
+ case PREFIX(maxCode):
52
+ default: return notErrorCode;
53
+ }
54
+ #endif
55
+ }
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright (c) 2016-2020, 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
+ /* Note : this module is expected to remain private, do not expose it */
12
+
13
+ #ifndef ERROR_H_MODULE
14
+ #define ERROR_H_MODULE
15
+
16
+ #if defined (__cplusplus)
17
+ extern "C" {
18
+ #endif
19
+
20
+
21
+ /* ****************************************
22
+ * Dependencies
23
+ ******************************************/
24
+ #include <stddef.h> /* size_t */
25
+ #include "zstd_errors.h" /* enum list */
26
+
27
+
28
+ /* ****************************************
29
+ * Compiler-specific
30
+ ******************************************/
31
+ #if defined(__GNUC__)
32
+ # define ERR_STATIC static __attribute__((unused))
33
+ #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
34
+ # define ERR_STATIC static inline
35
+ #elif defined(_MSC_VER)
36
+ # define ERR_STATIC static __inline
37
+ #else
38
+ # define ERR_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
39
+ #endif
40
+
41
+
42
+ /*-****************************************
43
+ * Customization (error_public.h)
44
+ ******************************************/
45
+ typedef ZSTD_ErrorCode ERR_enum;
46
+ #define PREFIX(name) ZSTD_error_##name
47
+
48
+
49
+ /*-****************************************
50
+ * Error codes handling
51
+ ******************************************/
52
+ #undef ERROR /* already defined on Visual Studio */
53
+ #define ERROR(name) ZSTD_ERROR(name)
54
+ #define ZSTD_ERROR(name) ((size_t)-PREFIX(name))
55
+
56
+ ERR_STATIC unsigned ERR_isError(size_t code) { return (code > ERROR(maxCode)); }
57
+
58
+ ERR_STATIC ERR_enum ERR_getErrorCode(size_t code) { if (!ERR_isError(code)) return (ERR_enum)0; return (ERR_enum) (0-code); }
59
+
60
+ /* check and forward error code */
61
+ #define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
62
+ #define CHECK_F(f) { CHECK_V_F(_var_err__, f); }
63
+
64
+
65
+ /*-****************************************
66
+ * Error Strings
67
+ ******************************************/
68
+
69
+ const char* ERR_getErrorString(ERR_enum code); /* error_private.c */
70
+
71
+ ERR_STATIC const char* ERR_getErrorName(size_t code)
72
+ {
73
+ return ERR_getErrorString(ERR_getErrorCode(code));
74
+ }
75
+
76
+ #if defined (__cplusplus)
77
+ }
78
+ #endif
79
+
80
+ #endif /* ERROR_H_MODULE */
@@ -1,43 +1,24 @@
1
1
  /* ******************************************************************
2
- FSE : Finite State Entropy codec
3
- Public Prototypes declaration
4
- Copyright (C) 2013-2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
-
12
- * Redistributions of source code must retain the above copyright
13
- notice, this list of conditions and the following disclaimer.
14
- * Redistributions in binary form must reproduce the above
15
- copyright notice, this list of conditions and the following disclaimer
16
- in the documentation and/or other materials provided with the
17
- distribution.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- You can contact the author at :
32
- - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
2
+ * FSE : Finite State Entropy codec
3
+ * Public Prototypes declaration
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.
33
13
  ****************************************************************** */
34
- #ifndef FSE_H
35
- #define FSE_H
36
14
 
37
15
  #if defined (__cplusplus)
38
16
  extern "C" {
39
17
  #endif
40
18
 
19
+ #ifndef FSE_H
20
+ #define FSE_H
21
+
41
22
 
42
23
  /*-*****************************************
43
24
  * Dependencies
@@ -45,6 +26,33 @@ extern "C" {
45
26
  #include <stddef.h> /* size_t, ptrdiff_t */
46
27
 
47
28
 
29
+ /*-*****************************************
30
+ * FSE_PUBLIC_API : control library symbols visibility
31
+ ******************************************/
32
+ #if defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) && defined(__GNUC__) && (__GNUC__ >= 4)
33
+ # define FSE_PUBLIC_API __attribute__ ((visibility ("default")))
34
+ #elif defined(FSE_DLL_EXPORT) && (FSE_DLL_EXPORT==1) /* Visual expected */
35
+ # define FSE_PUBLIC_API __declspec(dllexport)
36
+ #elif defined(FSE_DLL_IMPORT) && (FSE_DLL_IMPORT==1)
37
+ # 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.*/
38
+ #else
39
+ # define FSE_PUBLIC_API
40
+ #endif
41
+
42
+ /*------ Version ------*/
43
+ #define FSE_VERSION_MAJOR 0
44
+ #define FSE_VERSION_MINOR 9
45
+ #define FSE_VERSION_RELEASE 0
46
+
47
+ #define FSE_LIB_VERSION FSE_VERSION_MAJOR.FSE_VERSION_MINOR.FSE_VERSION_RELEASE
48
+ #define FSE_QUOTE(str) #str
49
+ #define FSE_EXPAND_AND_QUOTE(str) FSE_QUOTE(str)
50
+ #define FSE_VERSION_STRING FSE_EXPAND_AND_QUOTE(FSE_LIB_VERSION)
51
+
52
+ #define FSE_VERSION_NUMBER (FSE_VERSION_MAJOR *100*100 + FSE_VERSION_MINOR *100 + FSE_VERSION_RELEASE)
53
+ FSE_PUBLIC_API unsigned FSE_versionNumber(void); /**< library version number; to be used when checking dll version */
54
+
55
+
48
56
  /*-****************************************
49
57
  * FSE simple functions
50
58
  ******************************************/
@@ -56,8 +64,8 @@ extern "C" {
56
64
  if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression instead.
57
65
  if FSE_isError(return), compression failed (more details using FSE_getErrorName())
58
66
  */
59
- size_t FSE_compress(void* dst, size_t dstCapacity,
60
- const void* src, size_t srcSize);
67
+ FSE_PUBLIC_API size_t FSE_compress(void* dst, size_t dstCapacity,
68
+ const void* src, size_t srcSize);
61
69
 
62
70
  /*! FSE_decompress():
63
71
  Decompress FSE data from buffer 'cSrc', of size 'cSrcSize',
@@ -69,18 +77,18 @@ size_t FSE_compress(void* dst, size_t dstCapacity,
69
77
  Why ? : making this distinction requires a header.
70
78
  Header management is intentionally delegated to the user layer, which can better manage special cases.
71
79
  */
72
- size_t FSE_decompress(void* dst, size_t dstCapacity,
73
- const void* cSrc, size_t cSrcSize);
80
+ FSE_PUBLIC_API size_t FSE_decompress(void* dst, size_t dstCapacity,
81
+ const void* cSrc, size_t cSrcSize);
74
82
 
75
83
 
76
84
  /*-*****************************************
77
85
  * Tool functions
78
86
  ******************************************/
79
- size_t FSE_compressBound(size_t size); /* maximum compressed size */
87
+ FSE_PUBLIC_API size_t FSE_compressBound(size_t size); /* maximum compressed size */
80
88
 
81
89
  /* 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) */
90
+ FSE_PUBLIC_API unsigned FSE_isError(size_t code); /* tells if a return value is an error code */
91
+ FSE_PUBLIC_API const char* FSE_getErrorName(size_t code); /* provides error code string (useful for debugging) */
84
92
 
85
93
 
86
94
  /*-*****************************************
@@ -94,7 +102,7 @@ const char* FSE_getErrorName(size_t code); /* provides error code string (usef
94
102
  if return == 1, srcData is a single byte symbol * srcSize times. Use RLE compression.
95
103
  if FSE_isError(return), it's an error code.
96
104
  */
97
- size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
105
+ FSE_PUBLIC_API size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog);
98
106
 
99
107
 
100
108
  /*-*****************************************
@@ -102,7 +110,7 @@ size_t FSE_compress2 (void* dst, size_t dstSize, const void* src, size_t srcSize
102
110
  ******************************************/
103
111
  /*!
104
112
  FSE_compress() does the following:
105
- 1. count symbol occurrence from source[] into table count[]
113
+ 1. count symbol occurrence from source[] into table count[] (see hist.h)
106
114
  2. normalize counters so that sum(count[]) == Power_of_2 (2^tableLog)
107
115
  3. save normalized counters to memory buffer using writeNCount()
108
116
  4. build encoding table 'CTable' from normalized counters
@@ -120,57 +128,50 @@ or to save and provide normalized distribution using external method.
120
128
 
121
129
  /* *** COMPRESSION *** */
122
130
 
123
- /*! FSE_count():
124
- Provides the precise count of each byte within a table 'count'.
125
- 'count' is a table of unsigned int, of minimum size (*maxSymbolValuePtr+1).
126
- *maxSymbolValuePtr will be updated if detected smaller than initial value.
127
- @return : the count of the most frequent symbol (which is not identified).
128
- if return == srcSize, there is only one symbol.
129
- 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);
131
-
132
131
  /*! FSE_optimalTableLog():
133
132
  dynamically downsize 'tableLog' when conditions are met.
134
133
  It saves CPU time, by using smaller tables, while preserving or even improving compression ratio.
135
134
  @return : recommended tableLog (necessarily <= 'maxTableLog') */
136
- unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
135
+ FSE_PUBLIC_API unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue);
137
136
 
138
137
  /*! FSE_normalizeCount():
139
138
  normalize counts so that sum(count[]) == Power_of_2 (2^tableLog)
140
139
  'normalizedCounter' is a table of short, of minimum size (maxSymbolValue+1).
141
140
  @return : tableLog,
142
141
  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);
142
+ FSE_PUBLIC_API size_t FSE_normalizeCount(short* normalizedCounter, unsigned tableLog,
143
+ const unsigned* count, size_t srcSize, unsigned maxSymbolValue);
144
144
 
145
145
  /*! FSE_NCountWriteBound():
146
146
  Provides the maximum possible size of an FSE normalized table, given 'maxSymbolValue' and 'tableLog'.
147
147
  Typically useful for allocation purpose. */
148
- size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
148
+ FSE_PUBLIC_API size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog);
149
149
 
150
150
  /*! FSE_writeNCount():
151
151
  Compactly save 'normalizedCounter' into 'buffer'.
152
152
  @return : size of the compressed table,
153
153
  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);
155
-
154
+ FSE_PUBLIC_API size_t FSE_writeNCount (void* buffer, size_t bufferSize,
155
+ const short* normalizedCounter,
156
+ unsigned maxSymbolValue, unsigned tableLog);
156
157
 
157
158
  /*! Constructor and Destructor of FSE_CTable.
158
159
  Note that FSE_CTable size depends on 'tableLog' and 'maxSymbolValue' */
159
160
  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);
161
+ FSE_PUBLIC_API FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog);
162
+ FSE_PUBLIC_API void FSE_freeCTable (FSE_CTable* ct);
162
163
 
163
164
  /*! FSE_buildCTable():
164
165
  Builds `ct`, which must be already allocated, using FSE_createCTable().
165
166
  @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);
167
+ FSE_PUBLIC_API size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
167
168
 
168
169
  /*! FSE_compress_usingCTable():
169
170
  Compress `src` using `ct` into `dst` which must be already allocated.
170
171
  @return : size of compressed data (<= `dstCapacity`),
171
172
  or 0 if compressed data could not fit into `dst`,
172
173
  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);
174
+ FSE_PUBLIC_API size_t FSE_compress_usingCTable (void* dst, size_t dstCapacity, const void* src, size_t srcSize, const FSE_CTable* ct);
174
175
 
175
176
  /*!
176
177
  Tutorial :
@@ -223,25 +224,27 @@ If there is an error, the function will return an ErrorCode (which can be tested
223
224
  @return : size read from 'rBuffer',
224
225
  or an errorCode, which can be tested using FSE_isError().
225
226
  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);
227
+ FSE_PUBLIC_API size_t FSE_readNCount (short* normalizedCounter,
228
+ unsigned* maxSymbolValuePtr, unsigned* tableLogPtr,
229
+ const void* rBuffer, size_t rBuffSize);
227
230
 
228
231
  /*! Constructor and Destructor of FSE_DTable.
229
232
  Note that its size depends on 'tableLog' */
230
233
  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);
234
+ FSE_PUBLIC_API FSE_DTable* FSE_createDTable(unsigned tableLog);
235
+ FSE_PUBLIC_API void FSE_freeDTable(FSE_DTable* dt);
233
236
 
234
237
  /*! FSE_buildDTable():
235
238
  Builds 'dt', which must be already allocated, using FSE_createDTable().
236
239
  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);
240
+ FSE_PUBLIC_API size_t FSE_buildDTable (FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog);
238
241
 
239
242
  /*! FSE_decompress_usingDTable():
240
243
  Decompress compressed source `cSrc` of size `cSrcSize` using `dt`
241
244
  into `dst` which must be already allocated.
242
245
  @return : size of regenerated data (necessarily <= `dstCapacity`),
243
246
  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);
247
+ FSE_PUBLIC_API size_t FSE_decompress_usingDTable(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, const FSE_DTable* dt);
245
248
 
246
249
  /*!
247
250
  Tutorial :
@@ -271,8 +274,10 @@ FSE_decompress_usingDTable() result will tell how many bytes were regenerated (<
271
274
  If there is an error, the function will return an error code, which can be tested using FSE_isError(). (ex: dst buffer too small)
272
275
  */
273
276
 
277
+ #endif /* FSE_H */
274
278
 
275
- #ifdef FSE_STATIC_LINKING_ONLY
279
+ #if defined(FSE_STATIC_LINKING_ONLY) && !defined(FSE_H_FSE_STATIC_LINKING_ONLY)
280
+ #define FSE_H_FSE_STATIC_LINKING_ONLY
276
281
 
277
282
  /* *** Dependency *** */
278
283
  #include "bitstream.h"
@@ -283,48 +288,67 @@ If there is an error, the function will return an error code, which can be teste
283
288
  *******************************************/
284
289
  /* FSE buffer bounds */
285
290
  #define FSE_NCOUNTBOUND 512
286
- #define FSE_BLOCKBOUND(size) (size + (size>>7))
291
+ #define FSE_BLOCKBOUND(size) (size + (size>>7) + 4 /* fse states */ + sizeof(size_t) /* bitContainer */)
287
292
  #define FSE_COMPRESSBOUND(size) (FSE_NCOUNTBOUND + FSE_BLOCKBOUND(size)) /* Macro version, useful for static allocation */
288
293
 
289
- /* It is possible to statically allocate FSE CTable/DTable as a table of unsigned using below macros */
294
+ /* It is possible to statically allocate FSE CTable/DTable as a table of FSE_CTable/FSE_DTable using below macros */
290
295
  #define FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) (1 + (1<<(maxTableLog-1)) + ((maxSymbolValue+1)*2))
291
296
  #define FSE_DTABLE_SIZE_U32(maxTableLog) (1 + (1<<maxTableLog))
292
297
 
298
+ /* or use the size to malloc() space directly. Pay attention to alignment restrictions though */
299
+ #define FSE_CTABLE_SIZE(maxTableLog, maxSymbolValue) (FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) * sizeof(FSE_CTable))
300
+ #define FSE_DTABLE_SIZE(maxTableLog) (FSE_DTABLE_SIZE_U32(maxTableLog) * sizeof(FSE_DTable))
301
+
293
302
 
294
303
  /* *****************************************
295
- * FSE advanced API
296
- *******************************************/
297
- 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 */
304
+ * FSE advanced API
305
+ ***************************************** */
299
306
 
300
307
  unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus);
301
308
  /**< same as FSE_optimalTableLog(), which used `minus==2` */
302
309
 
310
+ /* FSE_compress_wksp() :
311
+ * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
312
+ * FSE_WKSP_SIZE_U32() provides the minimum size required for `workSpace` as a table of FSE_CTable.
313
+ */
314
+ #define FSE_WKSP_SIZE_U32(maxTableLog, maxSymbolValue) ( FSE_CTABLE_SIZE_U32(maxTableLog, maxSymbolValue) + ((maxTableLog > 12) ? (1 << (maxTableLog - 2)) : 1024) )
315
+ 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);
316
+
303
317
  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 */
318
+ /**< build a fake FSE_CTable, designed for a flat distribution, where each symbol uses nbBits */
305
319
 
306
320
  size_t FSE_buildCTable_rle (FSE_CTable* ct, unsigned char symbolValue);
307
321
  /**< build a fake FSE_CTable, designed to compress always the same symbolValue */
308
322
 
323
+ /* FSE_buildCTable_wksp() :
324
+ * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
325
+ * `wkspSize` must be >= `(1<<tableLog)`.
326
+ */
327
+ size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize);
328
+
309
329
  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 */
330
+ /**< build a fake FSE_DTable, designed to read a flat distribution where each symbol uses nbBits */
311
331
 
312
332
  size_t FSE_buildDTable_rle (FSE_DTable* dt, unsigned char symbolValue);
313
333
  /**< build a fake FSE_DTable, designed to always generate the same symbolValue */
314
334
 
335
+ size_t FSE_decompress_wksp(void* dst, size_t dstCapacity, const void* cSrc, size_t cSrcSize, FSE_DTable* workSpace, unsigned maxLog);
336
+ /**< same as FSE_decompress(), using an externally allocated `workSpace` produced with `FSE_DTABLE_SIZE_U32(maxLog)` */
337
+
338
+ typedef enum {
339
+ FSE_repeat_none, /**< Cannot use the previous table */
340
+ FSE_repeat_check, /**< Can use the previous table but it must be checked */
341
+ FSE_repeat_valid /**< Can use the previous table and it is assumed to be valid */
342
+ } FSE_repeat;
315
343
 
316
344
  /* *****************************************
317
345
  * FSE symbol compression API
318
346
  *******************************************/
319
347
  /*!
320
348
  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.
349
+ Hence their body are included in next section.
325
350
  */
326
- typedef struct
327
- {
351
+ typedef struct {
328
352
  ptrdiff_t value;
329
353
  const void* stateTable;
330
354
  const void* symbolTT;
@@ -384,8 +408,7 @@ If there is an error, it returns an errorCode (which can be tested using FSE_isE
384
408
  /* *****************************************
385
409
  * FSE symbol decompression API
386
410
  *******************************************/
387
- typedef struct
388
- {
411
+ typedef struct {
389
412
  size_t state;
390
413
  const void* table; /* precise table may vary, depending on U16 */
391
414
  } FSE_DState_t;
@@ -469,7 +492,7 @@ MEM_STATIC void FSE_initCState(FSE_CState_t* statePtr, const FSE_CTable* ct)
469
492
  const U32 tableLog = MEM_read16(ptr);
470
493
  statePtr->value = (ptrdiff_t)1<<tableLog;
471
494
  statePtr->stateTable = u16ptr+2;
472
- statePtr->symbolTT = ((const U32*)ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1));
495
+ statePtr->symbolTT = ct + 1 + (tableLog ? (1<<(tableLog-1)) : 1);
473
496
  statePtr->stateLog = tableLog;
474
497
  }
475
498
 
@@ -488,11 +511,11 @@ MEM_STATIC void FSE_initCState2(FSE_CState_t* statePtr, const FSE_CTable* ct, U3
488
511
  }
489
512
  }
490
513
 
491
- MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, U32 symbol)
514
+ MEM_STATIC void FSE_encodeSymbol(BIT_CStream_t* bitC, FSE_CState_t* statePtr, unsigned symbol)
492
515
  {
493
- const FSE_symbolCompressionTransform symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
516
+ FSE_symbolCompressionTransform const symbolTT = ((const FSE_symbolCompressionTransform*)(statePtr->symbolTT))[symbol];
494
517
  const U16* const stateTable = (const U16*)(statePtr->stateTable);
495
- U32 nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
518
+ U32 const nbBitsOut = (U32)((statePtr->value + symbolTT.deltaNbBits) >> 16);
496
519
  BIT_addBits(bitC, statePtr->value, nbBitsOut);
497
520
  statePtr->value = stateTable[ (statePtr->value >> nbBitsOut) + symbolTT.deltaFindState];
498
521
  }
@@ -503,7 +526,41 @@ MEM_STATIC void FSE_flushCState(BIT_CStream_t* bitC, const FSE_CState_t* statePt
503
526
  BIT_flushBits(bitC);
504
527
  }
505
528
 
506
- /*<===== Decompression =====>*/
529
+
530
+ /* FSE_getMaxNbBits() :
531
+ * Approximate maximum cost of a symbol, in bits.
532
+ * Fractional get rounded up (i.e : a symbol with a normalized frequency of 3 gives the same result as a frequency of 2)
533
+ * note 1 : assume symbolValue is valid (<= maxSymbolValue)
534
+ * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */
535
+ MEM_STATIC U32 FSE_getMaxNbBits(const void* symbolTTPtr, U32 symbolValue)
536
+ {
537
+ const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;
538
+ return (symbolTT[symbolValue].deltaNbBits + ((1<<16)-1)) >> 16;
539
+ }
540
+
541
+ /* FSE_bitCost() :
542
+ * Approximate symbol cost, as fractional value, using fixed-point format (accuracyLog fractional bits)
543
+ * note 1 : assume symbolValue is valid (<= maxSymbolValue)
544
+ * note 2 : if freq[symbolValue]==0, @return a fake cost of tableLog+1 bits */
545
+ MEM_STATIC U32 FSE_bitCost(const void* symbolTTPtr, U32 tableLog, U32 symbolValue, U32 accuracyLog)
546
+ {
547
+ const FSE_symbolCompressionTransform* symbolTT = (const FSE_symbolCompressionTransform*) symbolTTPtr;
548
+ U32 const minNbBits = symbolTT[symbolValue].deltaNbBits >> 16;
549
+ U32 const threshold = (minNbBits+1) << 16;
550
+ assert(tableLog < 16);
551
+ assert(accuracyLog < 31-tableLog); /* ensure enough room for renormalization double shift */
552
+ { U32 const tableSize = 1 << tableLog;
553
+ U32 const deltaFromThreshold = threshold - (symbolTT[symbolValue].deltaNbBits + tableSize);
554
+ U32 const normalizedDeltaFromThreshold = (deltaFromThreshold << accuracyLog) >> tableLog; /* linear interpolation (very approximate) */
555
+ U32 const bitMultiplier = 1 << accuracyLog;
556
+ assert(symbolTT[symbolValue].deltaNbBits + tableSize <= threshold);
557
+ assert(normalizedDeltaFromThreshold <= bitMultiplier);
558
+ return (minNbBits+1)*bitMultiplier - normalizedDeltaFromThreshold;
559
+ }
560
+ }
561
+
562
+
563
+ /* ====== Decompression ====== */
507
564
 
508
565
  typedef struct {
509
566
  U16 tableLog;
@@ -581,14 +638,19 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
581
638
  * Increasing memory usage improves compression ratio
582
639
  * Reduced memory usage can improve speed, due to cache effect
583
640
  * Recommended max value is 14, for 16KB, which nicely fits into Intel x86 L1 cache */
584
- #define FSE_MAX_MEMORY_USAGE 14
585
- #define FSE_DEFAULT_MEMORY_USAGE 13
641
+ #ifndef FSE_MAX_MEMORY_USAGE
642
+ # define FSE_MAX_MEMORY_USAGE 14
643
+ #endif
644
+ #ifndef FSE_DEFAULT_MEMORY_USAGE
645
+ # define FSE_DEFAULT_MEMORY_USAGE 13
646
+ #endif
586
647
 
587
648
  /*!FSE_MAX_SYMBOL_VALUE :
588
649
  * Maximum symbol value authorized.
589
650
  * Required for proper stack allocation */
590
- #define FSE_MAX_SYMBOL_VALUE 255
591
-
651
+ #ifndef FSE_MAX_SYMBOL_VALUE
652
+ # define FSE_MAX_SYMBOL_VALUE 255
653
+ #endif
592
654
 
593
655
  /* **************************************************************
594
656
  * template functions type & suffix
@@ -624,5 +686,3 @@ MEM_STATIC unsigned FSE_endOfDState(const FSE_DState_t* DStatePtr)
624
686
  #if defined (__cplusplus)
625
687
  }
626
688
  #endif
627
-
628
- #endif /* FSE_H */