/src/tdengine/contrib/TSZ/zstd/common/entropy_common.c
Line | Count | Source |
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 | | *************************************************************************** */ |
34 | | |
35 | | /* ************************************* |
36 | | * Dependencies |
37 | | ***************************************/ |
38 | | #include "mem.h" |
39 | | #include "error_private.h" /* ERR_*, ERROR */ |
40 | | #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ |
41 | | #include "fse.h" |
42 | | #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ |
43 | | #include "huf.h" |
44 | | |
45 | | |
46 | | /*=== Version ===*/ |
47 | 0 | unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; } |
48 | | |
49 | | |
50 | | /*=== Error Management ===*/ |
51 | 0 | unsigned FSE_isError(size_t code) { return ERR_isError(code); } |
52 | 0 | const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } |
53 | | |
54 | 0 | unsigned HUF_isError(size_t code) { return ERR_isError(code); } |
55 | 0 | const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } |
56 | | |
57 | | |
58 | | /*-************************************************************** |
59 | | * FSE NCount encoding-decoding |
60 | | ****************************************************************/ |
61 | | size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr, |
62 | | const void* headerBuffer, size_t hbSize) |
63 | 0 | { |
64 | 0 | const BYTE* const istart = (const BYTE*) headerBuffer; |
65 | 0 | const BYTE* const iend = istart + hbSize; |
66 | 0 | const BYTE* ip = istart; |
67 | 0 | int nbBits; |
68 | 0 | int remaining; |
69 | 0 | int threshold; |
70 | 0 | U32 bitStream; |
71 | 0 | int bitCount; |
72 | 0 | unsigned charnum = 0; |
73 | 0 | int previous0 = 0; |
74 | |
|
75 | 0 | if (hbSize < 4) { |
76 | | /* This function only works when hbSize >= 4 */ |
77 | 0 | char buffer[4]; |
78 | 0 | memset(buffer, 0, sizeof(buffer)); |
79 | 0 | memcpy(buffer, headerBuffer, hbSize); |
80 | 0 | { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr, |
81 | 0 | buffer, sizeof(buffer)); |
82 | 0 | if (FSE_isError(countSize)) return countSize; |
83 | 0 | if (countSize > hbSize) return ERROR(corruption_detected); |
84 | 0 | return countSize; |
85 | 0 | } } |
86 | 0 | assert(hbSize >= 4); |
87 | | |
88 | | /* init */ |
89 | 0 | memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */ |
90 | 0 | bitStream = MEM_readLE32(ip); |
91 | 0 | nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */ |
92 | 0 | if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge); |
93 | 0 | bitStream >>= 4; |
94 | 0 | bitCount = 4; |
95 | 0 | *tableLogPtr = nbBits; |
96 | 0 | remaining = (1<<nbBits)+1; |
97 | 0 | threshold = 1<<nbBits; |
98 | 0 | nbBits++; |
99 | |
|
100 | 0 | while ((remaining>1) & (charnum<=*maxSVPtr)) { |
101 | 0 | if (previous0) { |
102 | 0 | unsigned n0 = charnum; |
103 | 0 | while ((bitStream & 0xFFFF) == 0xFFFF) { |
104 | 0 | n0 += 24; |
105 | 0 | if (ip < iend-5) { |
106 | 0 | ip += 2; |
107 | 0 | bitStream = MEM_readLE32(ip) >> bitCount; |
108 | 0 | } else { |
109 | 0 | bitStream >>= 16; |
110 | 0 | bitCount += 16; |
111 | 0 | } } |
112 | 0 | while ((bitStream & 3) == 3) { |
113 | 0 | n0 += 3; |
114 | 0 | bitStream >>= 2; |
115 | 0 | bitCount += 2; |
116 | 0 | } |
117 | 0 | n0 += bitStream & 3; |
118 | 0 | bitCount += 2; |
119 | 0 | if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall); |
120 | 0 | while (charnum < n0) normalizedCounter[charnum++] = 0; |
121 | 0 | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
122 | 0 | assert((bitCount >> 3) <= 3); /* For first condition to work */ |
123 | 0 | ip += bitCount>>3; |
124 | 0 | bitCount &= 7; |
125 | 0 | bitStream = MEM_readLE32(ip) >> bitCount; |
126 | 0 | } else { |
127 | 0 | bitStream >>= 2; |
128 | 0 | } } |
129 | 0 | { int const max = (2*threshold-1) - remaining; |
130 | 0 | int count; |
131 | |
|
132 | 0 | if ((bitStream & (threshold-1)) < (U32)max) { |
133 | 0 | count = bitStream & (threshold-1); |
134 | 0 | bitCount += nbBits-1; |
135 | 0 | } else { |
136 | 0 | count = bitStream & (2*threshold-1); |
137 | 0 | if (count >= threshold) count -= max; |
138 | 0 | bitCount += nbBits; |
139 | 0 | } |
140 | |
|
141 | 0 | count--; /* extra accuracy */ |
142 | 0 | remaining -= count < 0 ? -count : count; /* -1 means +1 */ |
143 | 0 | normalizedCounter[charnum++] = (short)count; |
144 | 0 | previous0 = !count; |
145 | 0 | while (remaining < threshold) { |
146 | 0 | nbBits--; |
147 | 0 | threshold >>= 1; |
148 | 0 | } |
149 | |
|
150 | 0 | if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) { |
151 | 0 | ip += bitCount>>3; |
152 | 0 | bitCount &= 7; |
153 | 0 | } else { |
154 | 0 | bitCount -= (int)(8 * (iend - 4 - ip)); |
155 | 0 | ip = iend - 4; |
156 | 0 | } |
157 | 0 | bitStream = MEM_readLE32(ip) >> (bitCount & 31); |
158 | 0 | } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */ |
159 | 0 | if (remaining != 1) return ERROR(corruption_detected); |
160 | 0 | if (bitCount > 32) return ERROR(corruption_detected); |
161 | 0 | *maxSVPtr = charnum-1; |
162 | |
|
163 | 0 | ip += (bitCount+7)>>3; |
164 | 0 | return ip-istart; |
165 | 0 | } |
166 | | |
167 | | |
168 | | /*! HUF_readStats() : |
169 | | Read compact Huffman tree, saved by HUF_writeCTable(). |
170 | | `huffWeight` is destination buffer. |
171 | | `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32. |
172 | | @return : size read from `src` , or an error Code . |
173 | | Note : Needed by HUF_readCTable() and HUF_readDTableX?() . |
174 | | */ |
175 | | size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats, |
176 | | U32* nbSymbolsPtr, U32* tableLogPtr, |
177 | | const void* src, size_t srcSize) |
178 | 0 | { |
179 | 0 | U32 weightTotal; |
180 | 0 | const BYTE* ip = (const BYTE*) src; |
181 | 0 | size_t iSize; |
182 | 0 | size_t oSize; |
183 | |
|
184 | 0 | if (!srcSize) return ERROR(srcSize_wrong); |
185 | 0 | iSize = ip[0]; |
186 | | /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ |
187 | |
|
188 | 0 | if (iSize >= 128) { /* special header */ |
189 | 0 | oSize = iSize - 127; |
190 | 0 | iSize = ((oSize+1)/2); |
191 | 0 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
192 | 0 | if (oSize >= hwSize) return ERROR(corruption_detected); |
193 | 0 | ip += 1; |
194 | 0 | { U32 n; |
195 | 0 | for (n=0; n<oSize; n+=2) { |
196 | 0 | huffWeight[n] = ip[n/2] >> 4; |
197 | 0 | huffWeight[n+1] = ip[n/2] & 15; |
198 | 0 | } } } |
199 | 0 | else { /* header compressed with FSE (normal case) */ |
200 | 0 | FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */ |
201 | 0 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
202 | 0 | oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */ |
203 | 0 | if (FSE_isError(oSize)) return oSize; |
204 | 0 | } |
205 | | |
206 | | /* collect weight stats */ |
207 | 0 | memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32)); |
208 | 0 | weightTotal = 0; |
209 | 0 | { U32 n; for (n=0; n<oSize; n++) { |
210 | 0 | if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
211 | 0 | rankStats[huffWeight[n]]++; |
212 | 0 | weightTotal += (1 << huffWeight[n]) >> 1; |
213 | 0 | } } |
214 | 0 | if (weightTotal == 0) return ERROR(corruption_detected); |
215 | | |
216 | | /* get last non-null symbol weight (implied, total must be 2^n) */ |
217 | 0 | { U32 const tableLog = BIT_highbit32(weightTotal) + 1; |
218 | 0 | if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected); |
219 | 0 | *tableLogPtr = tableLog; |
220 | | /* determine last weight */ |
221 | 0 | { U32 const total = 1 << tableLog; |
222 | 0 | U32 const rest = total - weightTotal; |
223 | 0 | U32 const verif = 1 << BIT_highbit32(rest); |
224 | 0 | U32 const lastWeight = BIT_highbit32(rest) + 1; |
225 | 0 | if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ |
226 | 0 | huffWeight[oSize] = (BYTE)lastWeight; |
227 | 0 | rankStats[lastWeight]++; |
228 | 0 | } } |
229 | | |
230 | | /* check tree construction validity */ |
231 | 0 | if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ |
232 | | |
233 | | /* results */ |
234 | 0 | *nbSymbolsPtr = (U32)(oSize+1); |
235 | 0 | return iSize+1; |
236 | 0 | } |