blob: 8dd5b00ead81746727216b91be18c156d2c6991d [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***************************************/
38#include <stdlib.h>
39#include "mem.h"
Yann Colletd0e2cd12016-06-05 00:58:01 +020040#define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
41#include "fse.h"
inikep63ecd742016-05-13 11:27:56 +020042#include "error_private.h"
43#include "fse.h" /* declaration of FSE_isError, FSE_getErrorName */
44#include "huf.h" /* declaration of HUF_isError, HUF_getErrorName */
45
46
47
48/*-****************************************
49* FSE Error Management
50******************************************/
51unsigned FSE_isError(size_t code) { return ERR_isError(code); }
52
53const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
54
55
56/* **************************************************************
57* HUF Error Management
58****************************************************************/
59unsigned HUF_isError(size_t code) { return ERR_isError(code); }
60
61const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
62
63
64/*-**************************************************************
65* FSE NCount encoding-decoding
66****************************************************************/
67static short FSE_abs(short a) { return a<0 ? -a : a; }
68
69size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
70 const void* headerBuffer, size_t hbSize)
71{
72 const BYTE* const istart = (const BYTE*) headerBuffer;
73 const BYTE* const iend = istart + hbSize;
74 const BYTE* ip = istart;
75 int nbBits;
76 int remaining;
77 int threshold;
78 U32 bitStream;
79 int bitCount;
80 unsigned charnum = 0;
81 int previous0 = 0;
82
83 if (hbSize < 4) return ERROR(srcSize_wrong);
84 bitStream = MEM_readLE32(ip);
85 nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
86 if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
87 bitStream >>= 4;
88 bitCount = 4;
89 *tableLogPtr = nbBits;
90 remaining = (1<<nbBits)+1;
91 threshold = 1<<nbBits;
92 nbBits++;
93
94 while ((remaining>1) && (charnum<=*maxSVPtr)) {
95 if (previous0) {
96 unsigned n0 = charnum;
97 while ((bitStream & 0xFFFF) == 0xFFFF) {
98 n0+=24;
99 if (ip < iend-5) {
100 ip+=2;
101 bitStream = MEM_readLE32(ip) >> bitCount;
102 } else {
103 bitStream >>= 16;
104 bitCount+=16;
105 } }
106 while ((bitStream & 3) == 3) {
107 n0+=3;
108 bitStream>>=2;
109 bitCount+=2;
110 }
111 n0 += bitStream & 3;
112 bitCount += 2;
113 if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
114 while (charnum < n0) normalizedCounter[charnum++] = 0;
115 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
116 ip += bitCount>>3;
117 bitCount &= 7;
118 bitStream = MEM_readLE32(ip) >> bitCount;
119 }
120 else
121 bitStream >>= 2;
122 }
123 { short const max = (short)((2*threshold-1)-remaining);
124 short count;
125
126 if ((bitStream & (threshold-1)) < (U32)max) {
127 count = (short)(bitStream & (threshold-1));
128 bitCount += nbBits-1;
129 } else {
130 count = (short)(bitStream & (2*threshold-1));
131 if (count >= threshold) count -= max;
132 bitCount += nbBits;
133 }
134
135 count--; /* extra accuracy */
136 remaining -= FSE_abs(count);
137 normalizedCounter[charnum++] = count;
138 previous0 = !count;
139 while (remaining < threshold) {
140 nbBits--;
141 threshold >>= 1;
142 }
143
144 if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
145 ip += bitCount>>3;
146 bitCount &= 7;
147 } else {
148 bitCount -= (int)(8 * (iend - 4 - ip));
149 ip = iend - 4;
150 }
151 bitStream = MEM_readLE32(ip) >> (bitCount & 31);
152 } } /* while ((remaining>1) && (charnum<=*maxSVPtr)) */
153 if (remaining != 1) return ERROR(GENERIC);
154 *maxSVPtr = charnum-1;
155
156 ip += (bitCount+7)>>3;
157 if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
158 return ip-istart;
159}