You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

entropy_common.c 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* ******************************************************************
  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. ****************************************************************** */
  14. /* *************************************
  15. * Dependencies
  16. ***************************************/
  17. #include "mem.h"
  18. #include "error_private.h" /* ERR_*, ERROR */
  19. #define FSE_STATIC_LINKING_ONLY /* FSE_MIN_TABLELOG */
  20. #include "fse.h"
  21. #define HUF_STATIC_LINKING_ONLY /* HUF_TABLELOG_ABSOLUTEMAX */
  22. #include "huf.h"
  23. /*=== Version ===*/
  24. unsigned FSE_versionNumber(void) { return FSE_VERSION_NUMBER; }
  25. /*=== Error Management ===*/
  26. unsigned FSE_isError(size_t code) { return ERR_isError(code); }
  27. const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
  28. unsigned HUF_isError(size_t code) { return ERR_isError(code); }
  29. const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
  30. /*-**************************************************************
  31. * FSE NCount encoding-decoding
  32. ****************************************************************/
  33. size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
  34. const void* headerBuffer, size_t hbSize)
  35. {
  36. const BYTE* const istart = (const BYTE*) headerBuffer;
  37. const BYTE* const iend = istart + hbSize;
  38. const BYTE* ip = istart;
  39. int nbBits;
  40. int remaining;
  41. int threshold;
  42. U32 bitStream;
  43. int bitCount;
  44. unsigned charnum = 0;
  45. int previous0 = 0;
  46. if (hbSize < 4) {
  47. /* This function only works when hbSize >= 4 */
  48. char buffer[4];
  49. memset(buffer, 0, sizeof(buffer));
  50. memcpy(buffer, headerBuffer, hbSize);
  51. { size_t const countSize = FSE_readNCount(normalizedCounter, maxSVPtr, tableLogPtr,
  52. buffer, sizeof(buffer));
  53. if (FSE_isError(countSize)) return countSize;
  54. if (countSize > hbSize) return ERROR(corruption_detected);
  55. return countSize;
  56. } }
  57. assert(hbSize >= 4);
  58. /* init */
  59. memset(normalizedCounter, 0, (*maxSVPtr+1) * sizeof(normalizedCounter[0])); /* all symbols not present in NCount have a frequency of 0 */
  60. bitStream = MEM_readLE32(ip);
  61. nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
  62. if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
  63. bitStream >>= 4;
  64. bitCount = 4;
  65. *tableLogPtr = nbBits;
  66. remaining = (1<<nbBits)+1;
  67. threshold = 1<<nbBits;
  68. nbBits++;
  69. while ((remaining>1) & (charnum<=*maxSVPtr)) {
  70. if (previous0) {
  71. unsigned n0 = charnum;
  72. while ((bitStream & 0xFFFF) == 0xFFFF) {
  73. n0 += 24;
  74. if (ip < iend-5) {
  75. ip += 2;
  76. bitStream = MEM_readLE32(ip) >> bitCount;
  77. } else {
  78. bitStream >>= 16;
  79. bitCount += 16;
  80. } }
  81. while ((bitStream & 3) == 3) {
  82. n0 += 3;
  83. bitStream >>= 2;
  84. bitCount += 2;
  85. }
  86. n0 += bitStream & 3;
  87. bitCount += 2;
  88. if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
  89. while (charnum < n0) normalizedCounter[charnum++] = 0;
  90. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  91. assert((bitCount >> 3) <= 3); /* For first condition to work */
  92. ip += bitCount>>3;
  93. bitCount &= 7;
  94. bitStream = MEM_readLE32(ip) >> bitCount;
  95. } else {
  96. bitStream >>= 2;
  97. } }
  98. { int const max = (2*threshold-1) - remaining;
  99. int count;
  100. if ((bitStream & (threshold-1)) < (U32)max) {
  101. count = bitStream & (threshold-1);
  102. bitCount += nbBits-1;
  103. } else {
  104. count = bitStream & (2*threshold-1);
  105. if (count >= threshold) count -= max;
  106. bitCount += nbBits;
  107. }
  108. count--; /* extra accuracy */
  109. remaining -= count < 0 ? -count : count; /* -1 means +1 */
  110. normalizedCounter[charnum++] = (short)count;
  111. previous0 = !count;
  112. while (remaining < threshold) {
  113. nbBits--;
  114. threshold >>= 1;
  115. }
  116. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  117. ip += bitCount>>3;
  118. bitCount &= 7;
  119. } else {
  120. bitCount -= (int)(8 * (iend - 4 - ip));
  121. ip = iend - 4;
  122. }
  123. bitStream = MEM_readLE32(ip) >> (bitCount & 31);
  124. } } /* while ((remaining>1) & (charnum<=*maxSVPtr)) */
  125. if (remaining != 1) return ERROR(corruption_detected);
  126. if (bitCount > 32) return ERROR(corruption_detected);
  127. *maxSVPtr = charnum-1;
  128. ip += (bitCount+7)>>3;
  129. return ip-istart;
  130. }
  131. /*! HUF_readStats() :
  132. Read compact Huffman tree, saved by HUF_writeCTable().
  133. `huffWeight` is destination buffer.
  134. `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
  135. @return : size read from `src` , or an error Code .
  136. Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
  137. */
  138. size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
  139. U32* nbSymbolsPtr, U32* tableLogPtr,
  140. const void* src, size_t srcSize)
  141. {
  142. U32 weightTotal;
  143. const BYTE* ip = (const BYTE*) src;
  144. size_t iSize;
  145. size_t oSize;
  146. if (!srcSize) return ERROR(srcSize_wrong);
  147. iSize = ip[0];
  148. /* memset(huffWeight, 0, hwSize); *//* is not necessary, even though some analyzer complain ... */
  149. if (iSize >= 128) { /* special header */
  150. oSize = iSize - 127;
  151. iSize = ((oSize+1)/2);
  152. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  153. if (oSize >= hwSize) return ERROR(corruption_detected);
  154. ip += 1;
  155. { U32 n;
  156. for (n=0; n<oSize; n+=2) {
  157. huffWeight[n] = ip[n/2] >> 4;
  158. huffWeight[n+1] = ip[n/2] & 15;
  159. } } }
  160. else { /* header compressed with FSE (normal case) */
  161. FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)]; /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
  162. if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
  163. oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6); /* max (hwSize-1) values decoded, as last one is implied */
  164. if (FSE_isError(oSize)) return oSize;
  165. }
  166. /* collect weight stats */
  167. memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
  168. weightTotal = 0;
  169. { U32 n; for (n=0; n<oSize; n++) {
  170. if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
  171. rankStats[huffWeight[n]]++;
  172. weightTotal += (1 << huffWeight[n]) >> 1;
  173. } }
  174. if (weightTotal == 0) return ERROR(corruption_detected);
  175. /* get last non-null symbol weight (implied, total must be 2^n) */
  176. { U32 const tableLog = BIT_highbit32(weightTotal) + 1;
  177. if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
  178. *tableLogPtr = tableLog;
  179. /* determine last weight */
  180. { U32 const total = 1 << tableLog;
  181. U32 const rest = total - weightTotal;
  182. U32 const verif = 1 << BIT_highbit32(rest);
  183. U32 const lastWeight = BIT_highbit32(rest) + 1;
  184. if (verif != rest) return ERROR(corruption_detected); /* last value must be a clean power of 2 */
  185. huffWeight[oSize] = (BYTE)lastWeight;
  186. rankStats[lastWeight]++;
  187. } }
  188. /* check tree construction validity */
  189. if ((rankStats[1] < 2) || (rankStats[1] & 1)) return ERROR(corruption_detected); /* by construction : at least 2 elts of rank 1, must be even */
  190. /* results */
  191. *nbSymbolsPtr = (U32)(oSize+1);
  192. return iSize+1;
  193. }