blob: b4c366e61a82bba39510d1aa5c43166f204d011b [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"
40#include "fse_static.h" /* FSE_MIN_TABLELOG */
41#include "error_private.h"
42#include "fse.h" /* declaration of FSE_isError, FSE_getErrorName */
43#include "huf.h" /* declaration of HUF_isError, HUF_getErrorName */
44
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}