1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#include "error_private.h"
44#include "fse.h"
45#include "huf.h"
46#include "mem.h"
47
48
49unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
50
51
52unsigned FSE_isError(size_t code) { return ERR_isError(code); }
53
54unsigned HUF_isError(size_t code) { return ERR_isError(code); }
55
56
57
58
59size_t FSE_readNCount(short *normalizedCounter, unsigned *maxSVPtr, unsigned *tableLogPtr, const void *headerBuffer, size_t hbSize)
60{
61 const BYTE *const istart = (const BYTE *)headerBuffer;
62 const BYTE *const iend = istart + hbSize;
63 const BYTE *ip = istart;
64 int nbBits;
65 int remaining;
66 int threshold;
67 U32 bitStream;
68 int bitCount;
69 unsigned charnum = 0;
70 int previous0 = 0;
71
72 if (hbSize < 4)
73 return ERROR(srcSize_wrong);
74 bitStream = ZSTD_readLE32(ip);
75 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG;
76 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX)
77 return ERROR(tableLog_tooLarge);
78 bitStream >>= 4;
79 bitCount = 4;
80 *tableLogPtr = nbBits;
81 remaining = (1 << nbBits) + 1;
82 threshold = 1 << nbBits;
83 nbBits++;
84
85 while ((remaining > 1) & (charnum <= *maxSVPtr)) {
86 if (previous0) {
87 unsigned n0 = charnum;
88 while ((bitStream & 0xFFFF) == 0xFFFF) {
89 n0 += 24;
90 if (ip < iend - 5) {
91 ip += 2;
92 bitStream = ZSTD_readLE32(ip) >> bitCount;
93 } else {
94 bitStream >>= 16;
95 bitCount += 16;
96 }
97 }
98 while ((bitStream & 3) == 3) {
99 n0 += 3;
100 bitStream >>= 2;
101 bitCount += 2;
102 }
103 n0 += bitStream & 3;
104 bitCount += 2;
105 if (n0 > *maxSVPtr)
106 return ERROR(maxSymbolValue_tooSmall);
107 while (charnum < n0)
108 normalizedCounter[charnum++] = 0;
109 if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
110 ip += bitCount >> 3;
111 bitCount &= 7;
112 bitStream = ZSTD_readLE32(ip) >> bitCount;
113 } else {
114 bitStream >>= 2;
115 }
116 }
117 {
118 int const max = (2 * threshold - 1) - remaining;
119 int count;
120
121 if ((bitStream & (threshold - 1)) < (U32)max) {
122 count = bitStream & (threshold - 1);
123 bitCount += nbBits - 1;
124 } else {
125 count = bitStream & (2 * threshold - 1);
126 if (count >= threshold)
127 count -= max;
128 bitCount += nbBits;
129 }
130
131 count--;
132 remaining -= count < 0 ? -count : count;
133 normalizedCounter[charnum++] = (short)count;
134 previous0 = !count;
135 while (remaining < threshold) {
136 nbBits--;
137 threshold >>= 1;
138 }
139
140 if ((ip <= iend - 7) || (ip + (bitCount >> 3) <= iend - 4)) {
141 ip += bitCount >> 3;
142 bitCount &= 7;
143 } else {
144 bitCount -= (int)(8 * (iend - 4 - ip));
145 ip = iend - 4;
146 }
147 bitStream = ZSTD_readLE32(ip) >> (bitCount & 31);
148 }
149 }
150 if (remaining != 1)
151 return ERROR(corruption_detected);
152 if (bitCount > 32)
153 return ERROR(corruption_detected);
154 *maxSVPtr = charnum - 1;
155
156 ip += (bitCount + 7) >> 3;
157 return ip - istart;
158}
159
160
161
162
163
164
165
166
167size_t HUF_readStats_wksp(BYTE *huffWeight, size_t hwSize, U32 *rankStats, U32 *nbSymbolsPtr, U32 *tableLogPtr, const void *src, size_t srcSize, void *workspace, size_t workspaceSize)
168{
169 U32 weightTotal;
170 const BYTE *ip = (const BYTE *)src;
171 size_t iSize;
172 size_t oSize;
173
174 if (!srcSize)
175 return ERROR(srcSize_wrong);
176 iSize = ip[0];
177
178
179 if (iSize >= 128) {
180 oSize = iSize - 127;
181 iSize = ((oSize + 1) / 2);
182 if (iSize + 1 > srcSize)
183 return ERROR(srcSize_wrong);
184 if (oSize >= hwSize)
185 return ERROR(corruption_detected);
186 ip += 1;
187 {
188 U32 n;
189 for (n = 0; n < oSize; n += 2) {
190 huffWeight[n] = ip[n / 2] >> 4;
191 huffWeight[n + 1] = ip[n / 2] & 15;
192 }
193 }
194 } else {
195 if (iSize + 1 > srcSize)
196 return ERROR(srcSize_wrong);
197 oSize = FSE_decompress_wksp(huffWeight, hwSize - 1, ip + 1, iSize, 6, workspace, workspaceSize);
198 if (FSE_isError(oSize))
199 return oSize;
200 }
201
202
203 memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
204 weightTotal = 0;
205 {
206 U32 n;
207 for (n = 0; n < oSize; n++) {
208 if (huffWeight[n] >= HUF_TABLELOG_MAX)
209 return ERROR(corruption_detected);
210 rankStats[huffWeight[n]]++;
211 weightTotal += (1 << huffWeight[n]) >> 1;
212 }
213 }
214 if (weightTotal == 0)
215 return ERROR(corruption_detected);
216
217
218 {
219 U32 const tableLog = BIT_highbit32(weightTotal) + 1;
220 if (tableLog > HUF_TABLELOG_MAX)
221 return ERROR(corruption_detected);
222 *tableLogPtr = tableLog;
223
224 {
225 U32 const total = 1 << tableLog;
226 U32 const rest = total - weightTotal;
227 U32 const verif = 1 << BIT_highbit32(rest);
228 U32 const lastWeight = BIT_highbit32(rest) + 1;
229 if (verif != rest)
230 return ERROR(corruption_detected);
231 huffWeight[oSize] = (BYTE)lastWeight;
232 rankStats[lastWeight]++;
233 }
234 }
235
236
237 if ((rankStats[1] < 2) || (rankStats[1] & 1))
238 return ERROR(corruption_detected);
239
240
241 *nbSymbolsPtr = (U32)(oSize + 1);
242 return iSize + 1;
243}
244