entropy_common.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Common functions of New Generation Entropy library
  3. Copyright (C) 2016, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
  27. - Public forum : https://groups.google.com/forum/#!forum/lz4c
  28. *************************************************************************** */
  29. /* *************************************
  30. * Dependencies
  31. ***************************************/
  32. #include <stdlib.h>
  33. #include "mem.h"
  34. #include "fse_static.h" /* FSE_MIN_TABLELOG */
  35. #include "error_private.h"
  36. #include "fse.h" /* declaration of FSE_isError, FSE_getErrorName */
  37. #include "huf.h" /* declaration of HUF_isError, HUF_getErrorName */
  38. /*-****************************************
  39. * FSE Error Management
  40. ******************************************/
  41. unsigned FSE_isError(size_t code) { return ERR_isError(code); }
  42. const char* FSE_getErrorName(size_t code) { return ERR_getErrorName(code); }
  43. /* **************************************************************
  44. * HUF Error Management
  45. ****************************************************************/
  46. unsigned HUF_isError(size_t code) { return ERR_isError(code); }
  47. const char* HUF_getErrorName(size_t code) { return ERR_getErrorName(code); }
  48. /*-**************************************************************
  49. * FSE NCount encoding-decoding
  50. ****************************************************************/
  51. static short FSE_abs(short a) { return a<0 ? -a : a; }
  52. size_t FSE_readNCount (short* normalizedCounter, unsigned* maxSVPtr, unsigned* tableLogPtr,
  53. const void* headerBuffer, size_t hbSize)
  54. {
  55. const BYTE* const istart = (const BYTE*) headerBuffer;
  56. const BYTE* const iend = istart + hbSize;
  57. const BYTE* ip = istart;
  58. int nbBits;
  59. int remaining;
  60. int threshold;
  61. U32 bitStream;
  62. int bitCount;
  63. unsigned charnum = 0;
  64. int previous0 = 0;
  65. if (hbSize < 4) return ERROR(srcSize_wrong);
  66. bitStream = MEM_readLE32(ip);
  67. nbBits = (bitStream & 0xF) + FSE_MIN_TABLELOG; /* extract tableLog */
  68. if (nbBits > FSE_TABLELOG_ABSOLUTE_MAX) return ERROR(tableLog_tooLarge);
  69. bitStream >>= 4;
  70. bitCount = 4;
  71. *tableLogPtr = nbBits;
  72. remaining = (1<<nbBits)+1;
  73. threshold = 1<<nbBits;
  74. nbBits++;
  75. while ((remaining>1) && (charnum<=*maxSVPtr)) {
  76. if (previous0) {
  77. unsigned n0 = charnum;
  78. while ((bitStream & 0xFFFF) == 0xFFFF) {
  79. n0+=24;
  80. if (ip < iend-5) {
  81. ip+=2;
  82. bitStream = MEM_readLE32(ip) >> bitCount;
  83. } else {
  84. bitStream >>= 16;
  85. bitCount+=16;
  86. } }
  87. while ((bitStream & 3) == 3) {
  88. n0+=3;
  89. bitStream>>=2;
  90. bitCount+=2;
  91. }
  92. n0 += bitStream & 3;
  93. bitCount += 2;
  94. if (n0 > *maxSVPtr) return ERROR(maxSymbolValue_tooSmall);
  95. while (charnum < n0) normalizedCounter[charnum++] = 0;
  96. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  97. ip += bitCount>>3;
  98. bitCount &= 7;
  99. bitStream = MEM_readLE32(ip) >> bitCount;
  100. }
  101. else
  102. bitStream >>= 2;
  103. }
  104. { short const max = (short)((2*threshold-1)-remaining);
  105. short count;
  106. if ((bitStream & (threshold-1)) < (U32)max) {
  107. count = (short)(bitStream & (threshold-1));
  108. bitCount += nbBits-1;
  109. } else {
  110. count = (short)(bitStream & (2*threshold-1));
  111. if (count >= threshold) count -= max;
  112. bitCount += nbBits;
  113. }
  114. count--; /* extra accuracy */
  115. remaining -= FSE_abs(count);
  116. normalizedCounter[charnum++] = count;
  117. previous0 = !count;
  118. while (remaining < threshold) {
  119. nbBits--;
  120. threshold >>= 1;
  121. }
  122. if ((ip <= iend-7) || (ip + (bitCount>>3) <= iend-4)) {
  123. ip += bitCount>>3;
  124. bitCount &= 7;
  125. } else {
  126. bitCount -= (int)(8 * (iend - 4 - ip));
  127. ip = iend - 4;
  128. }
  129. bitStream = MEM_readLE32(ip) >> (bitCount & 31);
  130. } } /* while ((remaining>1) && (charnum<=*maxSVPtr)) */
  131. if (remaining != 1) return ERROR(GENERIC);
  132. *maxSVPtr = charnum-1;
  133. ip += (bitCount+7)>>3;
  134. if ((size_t)(ip-istart) > hbSize) return ERROR(srcSize_wrong);
  135. return ip-istart;
  136. }