inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 1 | /* |
| 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 | ***************************************/ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 38 | #include "mem.h" |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 39 | #include "error_private.h" /* ERR_*, ERROR */ |
Yann Collet | d0e2cd1 | 2016-06-05 00:58:01 +0200 | [diff] [blame] | 40 | #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 41 | #include "fse.h" /* FSE_isError, FSE_getErrorName */ |
| 42 | #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */ |
| 43 | #include "huf.h" /* HUF_isError, HUF_getErrorName */ |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 44 | |
| 45 | |
| 46 | |
| 47 | /*-**************************************** |
| 48 | * FSE Error Management |
| 49 | ******************************************/ |
| 50 | unsigned FSE_isError(size_t code) { return ERR_isError(code); } |
| 51 | |
| 52 | const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 53 | |
| 54 | |
| 55 | /* ************************************************************** |
| 56 | * HUF Error Management |
| 57 | ****************************************************************/ |
| 58 | unsigned HUF_isError(size_t code) { return ERR_isError(code); } |
| 59 | |
| 60 | const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); } |
| 61 | |
| 62 | |
| 63 | /*-************************************************************** |
| 64 | * FSE NCount encoding-decoding |
| 65 | ****************************************************************/ |
Yann Collet | d5c5a77 | 2016-07-19 15:06:55 +0200 | [diff] [blame] | 66 | static short FSE_abs(short a) { return (short)(a<0 ? -a : a); } |
inikep | 63ecd74 | 2016-05-13 11:27:56 +0200 | [diff] [blame] | 67 | |
| 68 | size_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 Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 159 | |
| 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 | */ |
| 167 | size_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 | |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame^] | 176 | /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 177 | |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame^] | 178 | if (iSize >= 128) { /* special header */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 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; |
Yann Collet | 7ed5e33 | 2016-07-24 14:26:11 +0200 | [diff] [blame^] | 184 | } else { /* Incompressible */ |
Yann Collet | a91ca62 | 2016-06-05 01:33:55 +0200 | [diff] [blame] | 185 | oSize = iSize - 127; |
| 186 | iSize = ((oSize+1)/2); |
| 187 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
| 188 | if (oSize >= hwSize) return ERROR(corruption_detected); |
| 189 | ip += 1; |
| 190 | { U32 n; |
| 191 | for (n=0; n<oSize; n+=2) { |
| 192 | huffWeight[n] = ip[n/2] >> 4; |
| 193 | huffWeight[n+1] = ip[n/2] & 15; |
| 194 | } } } } |
| 195 | else { /* header compressed with FSE (normal case) */ |
| 196 | if (iSize+1 > srcSize) return ERROR(srcSize_wrong); |
| 197 | oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize); /* max (hwSize-1) values decoded, as last one is implied */ |
| 198 | if (FSE_isError(oSize)) return oSize; |
| 199 | } |
| 200 | |
| 201 | /* collect weight stats */ |
| 202 | memset(rankStats, 0, (HUF_TABLELOG_ABSOLUTEMAX + 1) * sizeof(U32)); |
| 203 | weightTotal = 0; |
| 204 | { U32 n; for (n=0; n<oSize; n++) { |
| 205 | if (huffWeight[n] >= HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected); |
| 206 | rankStats[huffWeight[n]]++; |
| 207 | weightTotal += (1 << huffWeight[n]) >> 1; |
| 208 | } } |
| 209 | |
| 210 | /* get last non-null symbol weight (implied, total must be 2^n) */ |
| 211 | { U32 const tableLog = BIT_highbit32(weightTotal) + 1; |
| 212 | if (tableLog > HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected); |
| 213 | *tableLogPtr = tableLog; |
| 214 | /* determine last weight */ |
| 215 | { U32 const total = 1 << tableLog; |
| 216 | U32 const rest = total - weightTotal; |
| 217 | U32 const verif = 1 << BIT_highbit32(rest); |
| 218 | U32 const lastWeight = BIT_highbit32(rest) + 1; |
| 219 | if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */ |
| 220 | huffWeight[oSize] = (BYTE)lastWeight; |
| 221 | rankStats[lastWeight]++; |
| 222 | } } |
| 223 | |
| 224 | /* check tree construction validity */ |
| 225 | if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */ |
| 226 | |
| 227 | /* results */ |
| 228 | *nbSymbolsPtr = (U32)(oSize+1); |
| 229 | return iSize+1; |
| 230 | } |