/src/barebox/lib/zstd/fse_decompress.c
Line | Count | Source |
1 | | /* |
2 | | * FSE : Finite State Entropy decoder |
3 | | * Copyright (C) 2013-2015, Yann Collet. |
4 | | * |
5 | | * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions are |
9 | | * met: |
10 | | * |
11 | | * * Redistributions of source code must retain the above copyright |
12 | | * notice, this list of conditions and the following disclaimer. |
13 | | * * Redistributions in binary form must reproduce the above |
14 | | * copyright notice, this list of conditions and the following disclaimer |
15 | | * in the documentation and/or other materials provided with the |
16 | | * distribution. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | * |
30 | | * This program is free software; you can redistribute it and/or modify it under |
31 | | * the terms of the GNU General Public License version 2 as published by the |
32 | | * Free Software Foundation. This program is dual-licensed; you may select |
33 | | * either version 2 of the GNU General Public License ("GPL") or BSD license |
34 | | * ("BSD"). |
35 | | * |
36 | | * You can contact the author at : |
37 | | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
38 | | */ |
39 | | |
40 | | /* ************************************************************** |
41 | | * Compiler specifics |
42 | | ****************************************************************/ |
43 | | #define FORCE_INLINE static __always_inline |
44 | | |
45 | | /* ************************************************************** |
46 | | * Includes |
47 | | ****************************************************************/ |
48 | | #include "bitstream.h" |
49 | | #include "fse.h" |
50 | | #include <linux/compiler.h> |
51 | | #include <linux/kernel.h> |
52 | | #include <linux/string.h> /* memcpy, memset */ |
53 | | |
54 | | /* ************************************************************** |
55 | | * Error Management |
56 | | ****************************************************************/ |
57 | 13.6k | #define FSE_isError ERR_isError |
58 | | #define FSE_STATIC_ASSERT(c) \ |
59 | 4.63k | { \ |
60 | 4.63k | enum { FSE_static_assert = 1 / (int)(!!(c)) }; \ |
61 | 4.63k | } /* use only *after* variable declarations */ |
62 | | |
63 | | /* check and forward error code */ |
64 | | #define CHECK_F(f) \ |
65 | 9.06k | { \ |
66 | 9.06k | size_t const e = f; \ |
67 | 9.06k | if (FSE_isError(e)) \ |
68 | 9.06k | return e; \ |
69 | 9.06k | } |
70 | | |
71 | | /* ************************************************************** |
72 | | * Templates |
73 | | ****************************************************************/ |
74 | | /* |
75 | | designed to be included |
76 | | for type-specific functions (template emulation in C) |
77 | | Objective is to write these functions only once, for improved maintenance |
78 | | */ |
79 | | |
80 | | /* safety checks */ |
81 | | #ifndef FSE_FUNCTION_EXTENSION |
82 | | #error "FSE_FUNCTION_EXTENSION must be defined" |
83 | | #endif |
84 | | #ifndef FSE_FUNCTION_TYPE |
85 | | #error "FSE_FUNCTION_TYPE must be defined" |
86 | | #endif |
87 | | |
88 | | /* Function names */ |
89 | | #define FSE_CAT(X, Y) X##Y |
90 | | #define FSE_FUNCTION_NAME(X, Y) FSE_CAT(X, Y) |
91 | | #define FSE_TYPE_NAME(X, Y) FSE_CAT(X, Y) |
92 | | |
93 | | /* Function templates */ |
94 | | |
95 | | size_t FSE_buildDTable_wksp(FSE_DTable *dt, const short *normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void *workspace, size_t workspaceSize) |
96 | 28.0k | { |
97 | 28.0k | void *const tdPtr = dt + 1; /* because *dt is unsigned, 32-bits aligned on 32-bits */ |
98 | 28.0k | FSE_DECODE_TYPE *const tableDecode = (FSE_DECODE_TYPE *)(tdPtr); |
99 | 28.0k | U16 *symbolNext = (U16 *)workspace; |
100 | | |
101 | 28.0k | U32 const maxSV1 = maxSymbolValue + 1; |
102 | 28.0k | U32 const tableSize = 1 << tableLog; |
103 | 28.0k | U32 highThreshold = tableSize - 1; |
104 | | |
105 | | /* Sanity Checks */ |
106 | 28.0k | if (workspaceSize < sizeof(U16) * (FSE_MAX_SYMBOL_VALUE + 1)) |
107 | 0 | return ERROR(tableLog_tooLarge); |
108 | 28.0k | if (maxSymbolValue > FSE_MAX_SYMBOL_VALUE) |
109 | 0 | return ERROR(maxSymbolValue_tooLarge); |
110 | 28.0k | if (tableLog > FSE_MAX_TABLELOG) |
111 | 0 | return ERROR(tableLog_tooLarge); |
112 | | |
113 | | /* Init, lay down lowprob symbols */ |
114 | 28.0k | { |
115 | 28.0k | FSE_DTableHeader DTableH; |
116 | 28.0k | DTableH.tableLog = (U16)tableLog; |
117 | 28.0k | DTableH.fastMode = 1; |
118 | 28.0k | { |
119 | 28.0k | S16 const largeLimit = (S16)(1 << (tableLog - 1)); |
120 | 28.0k | U32 s; |
121 | 411k | for (s = 0; s < maxSV1; s++) { |
122 | 383k | if (normalizedCounter[s] == -1) { |
123 | 200k | tableDecode[highThreshold--].symbol = (FSE_FUNCTION_TYPE)s; |
124 | 200k | symbolNext[s] = 1; |
125 | 200k | } else { |
126 | 183k | if (normalizedCounter[s] >= largeLimit) |
127 | 10.1k | DTableH.fastMode = 0; |
128 | 183k | symbolNext[s] = normalizedCounter[s]; |
129 | 183k | } |
130 | 383k | } |
131 | 28.0k | } |
132 | 28.0k | memcpy(dt, &DTableH, sizeof(DTableH)); |
133 | 28.0k | } |
134 | | |
135 | | /* Spread symbols */ |
136 | 28.0k | { |
137 | 28.0k | U32 const tableMask = tableSize - 1; |
138 | 28.0k | U32 const step = FSE_TABLESTEP(tableSize); |
139 | 28.0k | U32 s, position = 0; |
140 | 411k | for (s = 0; s < maxSV1; s++) { |
141 | 383k | int i; |
142 | 1.88M | for (i = 0; i < normalizedCounter[s]; i++) { |
143 | 1.49M | tableDecode[position].symbol = (FSE_FUNCTION_TYPE)s; |
144 | 1.49M | position = (position + step) & tableMask; |
145 | 1.70M | while (position > highThreshold) |
146 | 200k | position = (position + step) & tableMask; /* lowprob area */ |
147 | 1.49M | } |
148 | 383k | } |
149 | 28.0k | if (position != 0) |
150 | 0 | return ERROR(GENERIC); /* position must reach all cells once, otherwise normalizedCounter is incorrect */ |
151 | 28.0k | } |
152 | | |
153 | | /* Build Decoding table */ |
154 | 28.0k | { |
155 | 28.0k | U32 u; |
156 | 1.72M | for (u = 0; u < tableSize; u++) { |
157 | 1.70M | FSE_FUNCTION_TYPE const symbol = (FSE_FUNCTION_TYPE)(tableDecode[u].symbol); |
158 | 1.70M | U16 nextState = symbolNext[symbol]++; |
159 | 1.70M | tableDecode[u].nbBits = (BYTE)(tableLog - BIT_highbit32((U32)nextState)); |
160 | 1.70M | tableDecode[u].newState = (U16)((nextState << tableDecode[u].nbBits) - tableSize); |
161 | 1.70M | } |
162 | 28.0k | } |
163 | | |
164 | 28.0k | return 0; |
165 | 28.0k | } |
166 | | |
167 | | /*-******************************************************* |
168 | | * Decompression (Byte symbols) |
169 | | *********************************************************/ |
170 | | size_t FSE_buildDTable_rle(FSE_DTable *dt, BYTE symbolValue) |
171 | 15.8k | { |
172 | 15.8k | void *ptr = dt; |
173 | 15.8k | FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr; |
174 | 15.8k | void *dPtr = dt + 1; |
175 | 15.8k | FSE_decode_t *const cell = (FSE_decode_t *)dPtr; |
176 | | |
177 | 15.8k | DTableH->tableLog = 0; |
178 | 15.8k | DTableH->fastMode = 0; |
179 | | |
180 | 15.8k | cell->newState = 0; |
181 | 15.8k | cell->symbol = symbolValue; |
182 | 15.8k | cell->nbBits = 0; |
183 | | |
184 | 15.8k | return 0; |
185 | 15.8k | } |
186 | | |
187 | | size_t FSE_buildDTable_raw(FSE_DTable *dt, unsigned nbBits) |
188 | 0 | { |
189 | 0 | void *ptr = dt; |
190 | 0 | FSE_DTableHeader *const DTableH = (FSE_DTableHeader *)ptr; |
191 | 0 | void *dPtr = dt + 1; |
192 | 0 | FSE_decode_t *const dinfo = (FSE_decode_t *)dPtr; |
193 | 0 | const unsigned tableSize = 1 << nbBits; |
194 | 0 | const unsigned tableMask = tableSize - 1; |
195 | 0 | const unsigned maxSV1 = tableMask + 1; |
196 | 0 | unsigned s; |
197 | | |
198 | | /* Sanity checks */ |
199 | 0 | if (nbBits < 1) |
200 | 0 | return ERROR(GENERIC); /* min size */ |
201 | | |
202 | | /* Build Decoding Table */ |
203 | 0 | DTableH->tableLog = (U16)nbBits; |
204 | 0 | DTableH->fastMode = 1; |
205 | 0 | for (s = 0; s < maxSV1; s++) { |
206 | 0 | dinfo[s].newState = 0; |
207 | 0 | dinfo[s].symbol = (BYTE)s; |
208 | 0 | dinfo[s].nbBits = (BYTE)nbBits; |
209 | 0 | } |
210 | |
|
211 | 0 | return 0; |
212 | 0 | } |
213 | | |
214 | | FORCE_INLINE size_t FSE_decompress_usingDTable_generic(void *dst, size_t maxDstSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt, |
215 | | const unsigned fast) |
216 | 4.53k | { |
217 | 4.53k | BYTE *const ostart = (BYTE *)dst; |
218 | 4.53k | BYTE *op = ostart; |
219 | 4.53k | BYTE *const omax = op + maxDstSize; |
220 | 4.53k | BYTE *const olimit = omax - 3; |
221 | | |
222 | 4.53k | BIT_DStream_t bitD; |
223 | 4.53k | FSE_DState_t state1; |
224 | 4.53k | FSE_DState_t state2; |
225 | | |
226 | | /* Init */ |
227 | 4.53k | CHECK_F(BIT_initDStream(&bitD, cSrc, cSrcSize)); |
228 | | |
229 | 4.22k | FSE_initDState(&state1, &bitD, dt); |
230 | 4.22k | FSE_initDState(&state2, &bitD, dt); |
231 | | |
232 | 447k | #define FSE_GETSYMBOL(statePtr) fast ? FSE_decodeSymbolFast(statePtr, &bitD) : FSE_decodeSymbol(statePtr, &bitD) |
233 | | |
234 | | /* 4 symbols per loop */ |
235 | 59.0k | for (; (BIT_reloadDStream(&bitD) == BIT_DStream_unfinished) & (op < olimit); op += 4) { |
236 | 54.8k | op[0] = FSE_GETSYMBOL(&state1); |
237 | | |
238 | 54.8k | if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */ |
239 | 0 | BIT_reloadDStream(&bitD); |
240 | | |
241 | 54.8k | op[1] = FSE_GETSYMBOL(&state2); |
242 | | |
243 | 54.8k | if (FSE_MAX_TABLELOG * 4 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */ |
244 | 0 | { |
245 | 0 | if (BIT_reloadDStream(&bitD) > BIT_DStream_unfinished) { |
246 | 0 | op += 2; |
247 | 0 | break; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | 54.8k | op[2] = FSE_GETSYMBOL(&state1); |
252 | | |
253 | 54.8k | if (FSE_MAX_TABLELOG * 2 + 7 > sizeof(bitD.bitContainer) * 8) /* This test must be static */ |
254 | 0 | BIT_reloadDStream(&bitD); |
255 | | |
256 | 54.8k | op[3] = FSE_GETSYMBOL(&state2); |
257 | 54.8k | } |
258 | | |
259 | | /* tail */ |
260 | | /* note : BIT_reloadDStream(&bitD) >= FSE_DStream_partiallyFilled; Ends at exactly BIT_DStream_completed */ |
261 | 113k | while (1) { |
262 | 113k | if (op > (omax - 2)) |
263 | 74 | return ERROR(dstSize_tooSmall); |
264 | 113k | *op++ = FSE_GETSYMBOL(&state1); |
265 | 113k | if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) { |
266 | 2.36k | *op++ = FSE_GETSYMBOL(&state2); |
267 | 2.36k | break; |
268 | 2.36k | } |
269 | | |
270 | 110k | if (op > (omax - 2)) |
271 | 0 | return ERROR(dstSize_tooSmall); |
272 | 110k | *op++ = FSE_GETSYMBOL(&state2); |
273 | 110k | if (BIT_reloadDStream(&bitD) == BIT_DStream_overflow) { |
274 | 1.79k | *op++ = FSE_GETSYMBOL(&state1); |
275 | 1.79k | break; |
276 | 1.79k | } |
277 | 110k | } |
278 | | |
279 | 4.15k | return op - ostart; |
280 | 4.22k | } |
281 | | |
282 | | size_t FSE_decompress_usingDTable(void *dst, size_t originalSize, const void *cSrc, size_t cSrcSize, const FSE_DTable *dt) |
283 | 4.53k | { |
284 | 4.53k | const void *ptr = dt; |
285 | 4.53k | const FSE_DTableHeader *DTableH = (const FSE_DTableHeader *)ptr; |
286 | 4.53k | const U32 fastMode = DTableH->fastMode; |
287 | | |
288 | | /* select fast mode (static) */ |
289 | 4.53k | if (fastMode) |
290 | 1.87k | return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 1); |
291 | 2.65k | return FSE_decompress_usingDTable_generic(dst, originalSize, cSrc, cSrcSize, dt, 0); |
292 | 4.53k | } |
293 | | |
294 | | size_t FSE_decompress_wksp(void *dst, size_t dstCapacity, const void *cSrc, size_t cSrcSize, unsigned maxLog, void *workspace, size_t workspaceSize) |
295 | 4.63k | { |
296 | 4.63k | const BYTE *const istart = (const BYTE *)cSrc; |
297 | 4.63k | const BYTE *ip = istart; |
298 | 4.63k | unsigned tableLog; |
299 | 4.63k | unsigned maxSymbolValue = FSE_MAX_SYMBOL_VALUE; |
300 | 4.63k | size_t NCountLength; |
301 | | |
302 | 4.63k | FSE_DTable *dt; |
303 | 4.63k | short *counting; |
304 | 4.63k | size_t spaceUsed32 = 0; |
305 | | |
306 | 4.63k | FSE_STATIC_ASSERT(sizeof(FSE_DTable) == sizeof(U32)); |
307 | | |
308 | 4.63k | dt = (FSE_DTable *)((U32 *)workspace + spaceUsed32); |
309 | 4.63k | spaceUsed32 += FSE_DTABLE_SIZE_U32(maxLog); |
310 | 4.63k | counting = (short *)((U32 *)workspace + spaceUsed32); |
311 | 4.63k | spaceUsed32 += ALIGN(sizeof(short) * (FSE_MAX_SYMBOL_VALUE + 1), sizeof(U32)) >> 2; |
312 | | |
313 | 4.63k | if ((spaceUsed32 << 2) > workspaceSize) |
314 | 0 | return ERROR(tableLog_tooLarge); |
315 | 4.63k | workspace = (U32 *)workspace + spaceUsed32; |
316 | 4.63k | workspaceSize -= (spaceUsed32 << 2); |
317 | | |
318 | | /* normal FSE decoding mode */ |
319 | 4.63k | NCountLength = FSE_readNCount(counting, &maxSymbolValue, &tableLog, istart, cSrcSize); |
320 | 4.63k | if (FSE_isError(NCountLength)) |
321 | 87 | return NCountLength; |
322 | | // if (NCountLength >= cSrcSize) return ERROR(srcSize_wrong); /* too small input size; supposed to be already checked in NCountLength, only remaining |
323 | | // case : NCountLength==cSrcSize */ |
324 | 4.54k | if (tableLog > maxLog) |
325 | 16 | return ERROR(tableLog_tooLarge); |
326 | 4.53k | ip += NCountLength; |
327 | 4.53k | cSrcSize -= NCountLength; |
328 | | |
329 | 4.53k | CHECK_F(FSE_buildDTable_wksp(dt, counting, maxSymbolValue, tableLog, workspace, workspaceSize)); |
330 | | |
331 | 4.53k | return FSE_decompress_usingDTable(dst, dstCapacity, ip, cSrcSize, dt); /* always return, even if it is an error code */ |
332 | 4.53k | } |