blob: 9d3e4e8e36ab4f99864219003504b904dd7e709f [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;
53 int previous0 = 0;
54
Nick Terrella97e9a62018-05-23 12:16:00 -070055 if (hbSize < 4) {
Nick Terrellf2d09242018-05-23 14:58:58 -070056 /* 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 } }
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
Yann Collet38b75dd2016-07-24 15:35:59 +020080 while ((remaining>1) & (charnum<=*maxSVPtr)) {
inikep63ecd742016-05-13 11:27:56 +020081 if (previous0) {
82 unsigned n0 = charnum;
83 while ((bitStream & 0xFFFF) == 0xFFFF) {
Yann Colletcbc5e9d2016-07-24 18:02:04 +020084 n0 += 24;
inikep63ecd742016-05-13 11:27:56 +020085 if (ip < iend-5) {
Yann Colletcbc5e9d2016-07-24 18:02:04 +020086 ip += 2;
inikep63ecd742016-05-13 11:27:56 +020087 bitStream = MEM_readLE32(ip) >> bitCount;
88 } else {
89 bitStream >>= 16;
Yann Colletcbc5e9d2016-07-24 18:02:04 +020090 bitCount += 16;
inikep63ecd742016-05-13 11:27:56 +020091 } }
92 while ((bitStream & 3) == 3) {
Yann Colletcbc5e9d2016-07-24 18:02:04 +020093 n0 += 3;
94 bitStream >>= 2;
95 bitCount += 2;
inikep63ecd742016-05-13 11:27:56 +020096 }
97 n0 += bitStream & 3;
98 bitCount += 2;
99 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
100 while (charnum < n0) normalizedCounter[charnum++] = 0;
101 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
Nick Terrella97e9a62018-05-23 12:16:00 -0700102 assert((bitCount >> 3) <= 3); /* For first condition to work */
inikep63ecd742016-05-13 11:27:56 +0200103 ip += bitCount>>3;
104 bitCount &= 7;
105 bitStream = MEM_readLE32(ip) >> bitCount;
Yann Collet38b75dd2016-07-24 15:35:59 +0200106 } else {
inikep63ecd742016-05-13 11:27:56 +0200107 bitStream >>= 2;
Yann Collet38b75dd2016-07-24 15:35:59 +0200108 } }
Yann Collet45960372017-02-15 12:00:03 -0800109 { int const max = (2*threshold-1) - remaining;
110 int count;
inikep63ecd742016-05-13 11:27:56 +0200111
112 if ((bitStream & (threshold-1)) < (U32)max) {
Yann Collet45960372017-02-15 12:00:03 -0800113 count = bitStream & (threshold-1);
114 bitCount += nbBits-1;
inikep63ecd742016-05-13 11:27:56 +0200115 } else {
Yann Collet45960372017-02-15 12:00:03 -0800116 count = bitStream & (2*threshold-1);
inikep63ecd742016-05-13 11:27:56 +0200117 if (count >= threshold) count -= max;
Yann Collet45960372017-02-15 12:00:03 -0800118 bitCount += nbBits;
inikep63ecd742016-05-13 11:27:56 +0200119 }
120
121 count--; /* extra accuracy */
Yann Collet45960372017-02-15 12:00:03 -0800122 remaining -= count < 0 ? -count : count; /* -1 means +1 */
123 normalizedCounter[charnum++] = (short)count;
inikep63ecd742016-05-13 11:27:56 +0200124 previous0 = !count;
125 while (remaining < threshold) {
126 nbBits--;
127 threshold >>= 1;
128 }
129
130 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
131 ip += bitCount>>3;
132 bitCount &= 7;
133 } else {
134 bitCount -= (int)(8 * (iend - 4 - ip));
135 ip = iend - 4;
136 }
137 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
Yann Collet38b75dd2016-07-24 15:35:59 +0200138 } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
139 if (remaining != 1) return ERROR(corruption_detected);
Yann Colletcbc5e9d2016-07-24 18:02:04 +0200140 if (bitCount > 32) return ERROR(corruption_detected);
inikep63ecd742016-05-13 11:27:56 +0200141 *maxSVPtr = charnum-1;
142
143 ip += (bitCount+7)>>3;
inikep63ecd742016-05-13 11:27:56 +0200144 return ip-istart;
145}
Yann Colleta91ca622016-06-05 01:33:55 +0200146
147
148/*! HUF_readStats() :
149 Read compact Huffman tree, saved by HUF_writeCTable().
150 `huffWeight` is destination buffer.
Yann Colletb89af202016-12-01 18:24:59 -0800151 `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
Yann Colleta91ca622016-06-05 01:33:55 +0200152 @return : size read from `src` , or an error Code .
Yann Collet38b75dd2016-07-24 15:35:59 +0200153 Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
Yann Colleta91ca622016-06-05 01:33:55 +0200154*/
155size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
156 U32* nbSymbolsPtr, U32* tableLogPtr,
157 const void* src, size_t srcSize)
158{
159 U32 weightTotal;
160 const BYTE* ip = (const BYTE*) src;
Nick Terrellccfcc642016-10-17 11:28:02 -0700161 size_t iSize;
Yann Colleta91ca622016-06-05 01:33:55 +0200162 size_t oSize;
163
Nick Terrellccfcc642016-10-17 11:28:02 -0700164 if (!srcSize) return ERROR(srcSize_wrong);
165 iSize = ip[0];
Yann Collet7ed5e332016-07-24 14:26:11 +0200166 /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
Yann Colleta91ca622016-06-05 01:33:55 +0200167
Yann Collet7ed5e332016-07-24 14:26:11 +0200168 if (iSize >= 128) { /* special header */
Yann Collet38b75dd2016-07-24 15:35:59 +0200169 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 } } }
Yann Colleta91ca622016-06-05 01:33:55 +0200179 else { /* header compressed with FSE (normal case) */
Yann Colletb89af202016-12-01 18:24:59 -0800180 FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
Yann Colleta91ca622016-06-05 01:33:55 +0200181 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
Yann Colletb89af202016-12-01 18:24:59 -0800182 oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
Yann Colleta91ca622016-06-05 01:33:55 +0200183 if (FSE_isError(oSize)) return oSize;
184 }
185
186 /* collect weight stats */
Yann Colletb89af202016-12-01 18:24:59 -0800187 memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
Yann Colleta91ca622016-06-05 01:33:55 +0200188 weightTotal = 0;
189 { U32 n; for (n=0; n<oSize; n++) {
Yann Colletb89af202016-12-01 18:24:59 -0800190 if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200191 rankStats[huffWeight[n]]++;
192 weightTotal += (1 << huffWeight[n]) >> 1;
193 } }
Nick Terrelld7605292016-10-19 11:19:54 -0700194 if (weightTotal == 0) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200195
196 /* get last non-null symbol weight (implied, total must be 2^n) */
197 { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
Yann Colletb89af202016-12-01 18:24:59 -0800198 if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
Yann Colleta91ca622016-06-05 01:33:55 +0200199 *tableLogPtr = tableLog;
200 /* determine last weight */
201 { U32 const total = 1 << tableLog;
202 U32 const rest = total - weightTotal;
203 U32 const verif = 1 << BIT_highbit32(rest);
204 U32 const lastWeight = BIT_highbit32(rest) + 1;
205 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
206 huffWeight[oSize] = (BYTE)lastWeight;
207 rankStats[lastWeight]++;
208 } }
209
210 /* check tree construction validity */
211 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
212
213 /* results */
214 *nbSymbolsPtr = (U32)(oSize+1);
215 return iSize+1;
216}