blob: 3969d2b3007c5b5497a742b2a8f6a4b13c0cd7b7 [file] [log] [blame]
Nick Terrellac58c8d2020-03-26 15:19:05 -07001/* ******************************************************************
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****************************************************************** */
inikep63ecd742016-05-13 11:27:56 +020014
15/* *************************************
16* Dependencies
17***************************************/
inikep63ecd742016-05-13 11:27:56 +020018#include "mem.h"
Yann Colleta91ca622016-06-05 01:33:55 +020019#include "error_private.h" /* ERR_*, ERROR */
Yann Colletd0e2cd12016-06-05 00:58:01 +020020#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
Yann Collet38b75dd2016-07-24 15:35:59 +020021#include "fse.h"
Yann Colleta91ca622016-06-05 01:33:55 +020022#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
Yann Collet38b75dd2016-07-24 15:35:59 +020023#include "huf.h"
inikep63ecd742016-05-13 11:27:56 +020024
25
Yann Collet1f2c95c2017-03-05 21:07:20 -080026/*=== Version ===*/
Yann Collet45960372017-02-15 12:00:03 -080027unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
28
29
Yann Collet1f2c95c2017-03-05 21:07:20 -080030/*=== Error Management ===*/
inikep63ecd742016-05-13 11:27:56 +020031unsigned FSE_isError(size_t code) { return ERR_isError(code); }
inikep63ecd742016-05-13 11:27:56 +020032const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
33
inikep63ecd742016-05-13 11:27:56 +020034unsigned HUF_isError(size_t code) { return ERR_isError(code); }
inikep63ecd742016-05-13 11:27:56 +020035const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
36
37
38/*-**************************************************************
39* FSE NCount encoding-decoding
40****************************************************************/
inikep63ecd742016-05-13 11:27:56 +020041size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
42 const void* headerBuffer, size_t hbSize)
43{
44 const BYTE* const istart = (const BYTE*) headerBuffer;
45 const BYTE* const iend = istart + hbSize;
46 const BYTE* ip = istart;
47 int nbBits;
48 int remaining;
49 int threshold;
50 U32 bitStream;
51 int bitCount;
52 unsigned charnum = 0;
Nick Terrell6004c112020-08-14 15:28:59 -070053 unsigned const maxSV1 = *maxSVPtr + 1;
inikep63ecd742016-05-13 11:27:56 +020054 int previous0 = 0;
55
Nick Terrella97e9a62018-05-23 12:16:00 -070056 if (hbSize < 4) {
Nick Terrellf2d09242018-05-23 14:58:58 -070057 /* This function only works when hbSize >= 4 */
Yann Collet21c273d2020-07-16 20:25:15 -070058 char buffer[4] = {0};
Nick Terrellf2d09242018-05-23 14:58:58 -070059 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 } }
Nick Terrellc92dd112018-05-23 14:47:20 -070066 assert(hbSize >= 4);
Nick Terrella97e9a62018-05-23 12:16:00 -070067
Yann Colletff773bf2018-06-26 17:24:41 -070068 /* init */
69 memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
inikep63ecd742016-05-13 11:27:56 +020070 bitStream = MEM_readLE32(ip);
71 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
72 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
73 bitStream >>= 4;
74 bitCount = 4;
75 *tableLogPtr = nbBits;
76 remaining = (1<<nbBits)+1;
77 threshold = 1<<nbBits;
78 nbBits++;
79
Nick Terrell6004c112020-08-14 15:28:59 -070080 for (;;) {
inikep63ecd742016-05-13 11:27:56 +020081 if (previous0) {
Nick Terrell6004c112020-08-14 15:28:59 -070082 // TODO: Generalize to FSE_countTrailingZeros() or something
83 int repeats = __builtin_ctz(~bitStream) >> 1;
84 while (repeats >= 12) {
85 charnum += 3 * 12;
86 if (ip < iend-6) {
87 ip += 3;
inikep63ecd742016-05-13 11:27:56 +020088 bitStream = MEM_readLE32(ip) >> bitCount;
89 } else {
Nick Terrell6004c112020-08-14 15:28:59 -070090 bitStream >>= 24;
91 bitCount += 24;
92 }
93 repeats = __builtin_ctz(~bitStream) >> 1;
inikep63ecd742016-05-13 11:27:56 +020094 }
Nick Terrell6004c112020-08-14 15:28:59 -070095 charnum += 3 * repeats;
96 bitStream >>= 2 * repeats;
97 bitCount += 2 * repeats;
98
Nick Terrell6004c112020-08-14 15:28:59 -070099 charnum += bitStream & 3;
inikep63ecd742016-05-13 11:27:56 +0200100 bitCount += 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700101
102 /* This is an error, but break and return an error
103 * at the end, because returning out of a loop makes
104 * it harder for the compiler to optimize.
105 */
106 if (charnum >= maxSV1) break;
107
108 /* We don't need to set the normalized count to 0
109 * because we already memset the whole buffer to 0.
110 */
111
inikep63ecd742016-05-13 11:27:56 +0200112 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
Nick Terrella97e9a62018-05-23 12:16:00 -0700113 assert((bitCount >> 3) <= 3); /* For first condition to work */
inikep63ecd742016-05-13 11:27:56 +0200114 ip += bitCount>>3;
115 bitCount &= 7;
116 bitStream = MEM_readLE32(ip) >> bitCount;
Yann Collet38b75dd2016-07-24 15:35:59 +0200117 } else {
inikep63ecd742016-05-13 11:27:56 +0200118 bitStream >>= 2;
Nick Terrell6004c112020-08-14 15:28:59 -0700119 }
120 }
121 {
122 int const max = (2*threshold-1) - remaining;
Yann Collet45960372017-02-15 12:00:03 -0800123 int count;
inikep63ecd742016-05-13 11:27:56 +0200124
125 if ((bitStream & (threshold-1)) < (U32)max) {
Yann Collet45960372017-02-15 12:00:03 -0800126 count = bitStream & (threshold-1);
127 bitCount += nbBits-1;
inikep63ecd742016-05-13 11:27:56 +0200128 } else {
Yann Collet45960372017-02-15 12:00:03 -0800129 count = bitStream & (2*threshold-1);
inikep63ecd742016-05-13 11:27:56 +0200130 if (count >= threshold) count -= max;
Yann Collet45960372017-02-15 12:00:03 -0800131 bitCount += nbBits;
inikep63ecd742016-05-13 11:27:56 +0200132 }
133
134 count--; /* extra accuracy */
Nick Terrell6004c112020-08-14 15:28:59 -0700135 /* When it matters (small blocks), this is a
136 * predictable branch, because we don't use -1.
137 */
138 if (count >= 0) {
139 remaining -= count;
140 } else {
141 assert(count == -1);
142 remaining += count;
143 }
Yann Collet45960372017-02-15 12:00:03 -0800144 normalizedCounter[charnum++] = (short)count;
inikep63ecd742016-05-13 11:27:56 +0200145 previous0 = !count;
inikep63ecd742016-05-13 11:27:56 +0200146
Nick Terrell6004c112020-08-14 15:28:59 -0700147 assert(threshold > 1);
148 if (remaining < threshold) {
149 /* This branch can be folded into the
150 * threshold update condition because we
151 * know that threshold > 1.
152 */
153 if (remaining <= 1) break;
154 nbBits = BIT_highbit32(remaining) + 1;
155 threshold = 1 << (nbBits - 1);
156 }
157 if (charnum >= maxSV1) break;
158
159 if (LIKELY((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4))) {
inikep63ecd742016-05-13 11:27:56 +0200160 ip += bitCount>>3;
161 bitCount &= 7;
162 } else {
163 bitCount -= (int)(8 * (iend - 4 - ip));
164 ip = iend - 4;
165 }
166 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
Nick Terrell6004c112020-08-14 15:28:59 -0700167 } }
Yann Collet38b75dd2016-07-24 15:35:59 +0200168 if (remaining != 1) return ERROR(corruption_detected);
Nick Terrell6004c112020-08-14 15:28:59 -0700169 /* Only possible when there are too many zeros. */
170 if (charnum > maxSV1) return ERROR(maxSymbolValue_tooSmall);
Yann Colletcbc5e9d2016-07-24 18:02:04 +0200171 if (bitCount > 32) return ERROR(corruption_detected);
inikep63ecd742016-05-13 11:27:56 +0200172 *maxSVPtr = charnum-1;
173
174 ip += (bitCount+7)>>3;
inikep63ecd742016-05-13 11:27:56 +0200175 return ip-istart;
176}
Yann Colleta91ca622016-06-05 01:33:55 +0200177
Yann Colleta91ca622016-06-05 01:33:55 +0200178/*! HUF_readStats() :
179 Read compact Huffman tree, saved by HUF_writeCTable().
180 `huffWeight` is destination buffer.
Yann Colletb89af202016-12-01 18:24:59 -0800181 `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
Yann Colleta91ca622016-06-05 01:33:55 +0200182 @return : size read from `src` , or an error Code .
Yann Collet38b75dd2016-07-24 15:35:59 +0200183 Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
Yann Colleta91ca622016-06-05 01:33:55 +0200184*/
185size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
186 U32* nbSymbolsPtr, U32* tableLogPtr,
187 const void* src, size_t srcSize)
188{
Nick Terrellba1fd172020-08-16 22:22:33 -0700189 U32 wksp[HUF_READ_STATS_WORKSPACE_SIZE_U32];
190 return HUF_readStats_wksp(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, wksp, sizeof(wksp));
191}
192
193size_t HUF_readStats_wksp(BYTE* huffWeight, size_t hwSize, U32* rankStats,
194 U32* nbSymbolsPtr, U32* tableLogPtr,
195 const void* src, size_t srcSize,
196 void* workSpace, size_t wkspSize)
197{
Yann Colleta91ca622016-06-05 01:33:55 +0200198 U32 weightTotal;
199 const BYTE* ip = (const BYTE*) src;
Nick Terrellccfcc642016-10-17 11:28:02 -0700200 size_t iSize;
Yann Colleta91ca622016-06-05 01:33:55 +0200201 size_t oSize;
202
Nick Terrellccfcc642016-10-17 11:28:02 -0700203 if (!srcSize) return ERROR(srcSize_wrong);
204 iSize = ip[0];
Yann Collet7ed5e332016-07-24 14:26:11 +0200205 /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
Yann Colleta91ca622016-06-05 01:33:55 +0200206
Yann Collet7ed5e332016-07-24 14:26:11 +0200207 if (iSize >= 128) { /* special header */
Yann Collet38b75dd2016-07-24 15:35:59 +0200208 oSize = iSize - 127;
209 iSize = ((oSize+1)/2);
210 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
211 if (oSize >= hwSize) return ERROR(corruption_detected);
212 ip += 1;
213 { U32 n;
214 for (n=0; n<oSize; n+=2) {
215 huffWeight[n] = ip[n/2] >> 4;
216 huffWeight[n+1] = ip[n/2] & 15;
217 } } }
Yann Colleta91ca622016-06-05 01:33:55 +0200218 else { /* header compressed with FSE (normal case) */
219 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
Nick Terrellba1fd172020-08-16 22:22:33 -0700220 oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, 6, workSpace, wkspSize); /* max (hwSize-1) values decoded, as last one is implied */
Yann Colleta91ca622016-06-05 01:33:55 +0200221 if (FSE_isError(oSize)) return oSize;
222 }
223
224 /* collect weight stats */
Yann Colletb89af202016-12-01 18:24:59 -0800225 memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
Yann Colleta91ca622016-06-05 01:33:55 +0200226 weightTotal = 0;
227 { U32 n; for (n=0; n<oSize; n++) {
Yann Colletb89af202016-12-01 18:24:59 -0800228 if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200229 rankStats[huffWeight[n]]++;
230 weightTotal += (1 << huffWeight[n]) >> 1;
231 } }
Nick Terrelld7605292016-10-19 11:19:54 -0700232 if (weightTotal == 0) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200233
234 /* get last non-null symbol weight (implied, total must be 2^n) */
235 { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
Yann Colletb89af202016-12-01 18:24:59 -0800236 if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200237 *tableLogPtr = tableLog;
238 /* determine last weight */
239 { U32 const total = 1 << tableLog;
240 U32 const rest = total - weightTotal;
241 U32 const verif = 1 << BIT_highbit32(rest);
242 U32 const lastWeight = BIT_highbit32(rest) + 1;
243 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
244 huffWeight[oSize] = (BYTE)lastWeight;
245 rankStats[lastWeight]++;
246 } }
247
248 /* check tree construction validity */
249 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
250
251 /* results */
252 *nbSymbolsPtr = (U32)(oSize+1);
253 return iSize+1;
254}