blob: b42acb4a3ca717329d4caa4599ef9533928ccb07 [file] [log] [blame]
inikep63ecd742016-05-13 11:27:56 +02001/*
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***************************************/
inikep63ecd742016-05-13 11:27:56 +020038#include "mem.h"
Yann Colleta91ca622016-06-05 01:33:55 +020039#include "error_private.h" /* ERR_*, ERROR */
Yann Colletd0e2cd12016-06-05 00:58:01 +020040#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
Yann Colleta91ca622016-06-05 01:33:55 +020041#include "fse.h" /* FSE_isError, FSE_getErrorName */
42#define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
43#include "huf.h" /* HUF_isError, HUF_getErrorName */
inikep63ecd742016-05-13 11:27:56 +020044
45
46
47/*-****************************************
48* FSE Error Management
49******************************************/
50unsigned FSE_isError(size_t code) { return ERR_isError(code); }
51
52const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
53
54
55/* **************************************************************
56* HUF Error Management
57****************************************************************/
58unsigned HUF_isError(size_t code) { return ERR_isError(code); }
59
60const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
61
62
63/*-**************************************************************
64* FSE NCount encoding-decoding
65****************************************************************/
66static short FSE_abs(short a) { return a<0 ? -a : a; }
67
68size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
69 const void* headerBuffer, size_t hbSize)
70{
71 const BYTE* const istart = (const BYTE*) headerBuffer;
72 const BYTE* const iend = istart + hbSize;
73 const BYTE* ip = istart;
74 int nbBits;
75 int remaining;
76 int threshold;
77 U32 bitStream;
78 int bitCount;
79 unsigned charnum = 0;
80 int previous0 = 0;
81
82 if (hbSize < 4) return ERROR(srcSize_wrong);
83 bitStream = MEM_readLE32(ip);
84 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
85 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
86 bitStream >>= 4;
87 bitCount = 4;
88 *tableLogPtr = nbBits;
89 remaining = (1<<nbBits)+1;
90 threshold = 1<<nbBits;
91 nbBits++;
92
93 while ((remaining>1) && (charnum<=*maxSVPtr)) {
94 if (previous0) {
95 unsigned n0 = charnum;
96 while ((bitStream & 0xFFFF) == 0xFFFF) {
97 n0+=24;
98 if (ip < iend-5) {
99 ip+=2;
100 bitStream = MEM_readLE32(ip) >> bitCount;
101 } else {
102 bitStream >>= 16;
103 bitCount+=16;
104 } }
105 while ((bitStream & 3) == 3) {
106 n0+=3;
107 bitStream>>=2;
108 bitCount+=2;
109 }
110 n0 += bitStream & 3;
111 bitCount += 2;
112 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
113 while (charnum < n0) normalizedCounter[charnum++] = 0;
114 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
115 ip += bitCount>>3;
116 bitCount &= 7;
117 bitStream = MEM_readLE32(ip) >> bitCount;
118 }
119 else
120 bitStream >>= 2;
121 }
122 { short const max = (short)((2*threshold-1)-remaining);
123 short count;
124
125 if ((bitStream & (threshold-1)) < (U32)max) {
126 count = (short)(bitStream & (threshold-1));
127 bitCount += nbBits-1;
128 } else {
129 count = (short)(bitStream & (2*threshold-1));
130 if (count >= threshold) count -= max;
131 bitCount += nbBits;
132 }
133
134 count--; /* extra accuracy */
135 remaining -= FSE_abs(count);
136 normalizedCounter[charnum++] = count;
137 previous0 = !count;
138 while (remaining < threshold) {
139 nbBits--;
140 threshold >>= 1;
141 }
142
143 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
144 ip += bitCount>>3;
145 bitCount &= 7;
146 } else {
147 bitCount -= (int)(8 * (iend - 4 - ip));
148 ip = iend - 4;
149 }
150 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
151 } } /* while ((remaining>1) && (charnum<=*maxSVPtr)) */
152 if (remaining != 1) return ERROR(GENERIC);
153 *maxSVPtr = charnum-1;
154
155 ip += (bitCount+7)>>3;
156 if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
157 return ip-istart;
158}
Yann Colleta91ca622016-06-05 01:33:55 +0200159
160
161/*! HUF_readStats() :
162 Read compact Huffman tree, saved by HUF_writeCTable().
163 `huffWeight` is destination buffer.
164 @return : size read from `src` , or an error Code .
165 Note : Needed by HUF_readCTable() and HUF_readDTableXn() .
166*/
167size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
168 U32* nbSymbolsPtr, U32* tableLogPtr,
169 const void* src, size_t srcSize)
170{
171 U32 weightTotal;
172 const BYTE* ip = (const BYTE*) src;
173 size_t iSize = ip[0];
174 size_t oSize;
175
176 //memset(huffWeight, 0, hwSize); /* is not necessary, even though some analyzer complain ... */
177
178 if (iSize >= 128) { /* special header */
179 if (iSize >= (242)) { /* RLE */
180 static U32 l[14] = { 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 127, 128 };
181 oSize = l[iSize-242];
182 memset(huffWeight, 1, hwSize);
183 iSize = 0;
184 }
185 else { /* Incompressible */
186 oSize = iSize - 127;
187 iSize = ((oSize+1)/2);
188 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
189 if (oSize >= hwSize) return ERROR(corruption_detected);
190 ip += 1;
191 { U32 n;
192 for (n=0; n<oSize; n+=2) {
193 huffWeight[n] = ip[n/2] >> 4;
194 huffWeight[n+1] = ip[n/2] & 15;
195 } } } }
196 else { /* header compressed with FSE (normal case) */
197 if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
198 oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */
199 if (FSE_isError(oSize)) return oSize;
200 }
201
202 /* collect weight stats */
203 memset(rankStats, 0, (HUF_TABLELOG_ABSOLUTEMAX + 1) * sizeof(U32));
204 weightTotal = 0;
205 { U32 n; for (n=0; n<oSize; n++) {
206 if (huffWeight[n] >= HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
207 rankStats[huffWeight[n]]++;
208 weightTotal += (1 << huffWeight[n]) >> 1;
209 } }
210
211 /* get last non-null symbol weight (implied, total must be 2^n) */
212 { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
213 if (tableLog > HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
214 *tableLogPtr = tableLog;
215 /* determine last weight */
216 { U32 const total = 1 << tableLog;
217 U32 const rest = total - weightTotal;
218 U32 const verif = 1 << BIT_highbit32(rest);
219 U32 const lastWeight = BIT_highbit32(rest) + 1;
220 if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
221 huffWeight[oSize] = (BYTE)lastWeight;
222 rankStats[lastWeight]++;
223 } }
224
225 /* check tree construction validity */
226 if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
227
228 /* results */
229 *nbSymbolsPtr = (U32)(oSize+1);
230 return iSize+1;
231}