Counter Strike : Global Offensive Source Code
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.

228 lines
7.4 KiB

  1. /*-------------------------------------------------------------*/
  2. /*--- Huffman coding low-level stuff ---*/
  3. /*--- huffman.c ---*/
  4. /*-------------------------------------------------------------*/
  5. /*--
  6. This file is a part of bzip2 and/or libbzip2, a program and
  7. library for lossless, block-sorting data compression.
  8. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. 1. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. 2. The origin of this software must not be misrepresented; you must
  15. not claim that you wrote the original software. If you use this
  16. software in a product, an acknowledgment in the product
  17. documentation would be appreciated but is not required.
  18. 3. Altered source versions must be plainly marked as such, and must
  19. not be misrepresented as being the original software.
  20. 4. The name of the author may not be used to endorse or promote
  21. products derived from this software without specific prior written
  22. permission.
  23. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  24. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  27. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. Julian Seward, Cambridge, UK.
  35. jseward@acm.org
  36. bzip2/libbzip2 version 1.0 of 21 March 2000
  37. This program is based on (at least) the work of:
  38. Mike Burrows
  39. David Wheeler
  40. Peter Fenwick
  41. Alistair Moffat
  42. Radford Neal
  43. Ian H. Witten
  44. Robert Sedgewick
  45. Jon L. Bentley
  46. For more information on these sources, see the manual.
  47. --*/
  48. #include "bzlib_private.h"
  49. /*---------------------------------------------------*/
  50. #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
  51. #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
  52. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  53. #define ADDWEIGHTS(zw1,zw2) \
  54. (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
  55. (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  56. #define UPHEAP(z) \
  57. { \
  58. Int32 zz, tmp; \
  59. zz = z; tmp = heap[zz]; \
  60. while (weight[tmp] < weight[heap[zz >> 1]]) { \
  61. heap[zz] = heap[zz >> 1]; \
  62. zz >>= 1; \
  63. } \
  64. heap[zz] = tmp; \
  65. }
  66. #define DOWNHEAP(z) \
  67. { \
  68. Int32 zz, yy, tmp; \
  69. zz = z; tmp = heap[zz]; \
  70. while (True) { \
  71. yy = zz << 1; \
  72. if (yy > nHeap) break; \
  73. if (yy < nHeap && \
  74. weight[heap[yy+1]] < weight[heap[yy]]) \
  75. yy++; \
  76. if (weight[tmp] < weight[heap[yy]]) break; \
  77. heap[zz] = heap[yy]; \
  78. zz = yy; \
  79. } \
  80. heap[zz] = tmp; \
  81. }
  82. /*---------------------------------------------------*/
  83. void BZ2_hbMakeCodeLengths ( UChar *len,
  84. Int32 *freq,
  85. Int32 alphaSize,
  86. Int32 maxLen )
  87. {
  88. /*--
  89. Nodes and heap entries run from 1. Entry 0
  90. for both the heap and nodes is a sentinel.
  91. --*/
  92. Int32 nNodes, nHeap, n1, n2, i, j, k;
  93. Bool tooLong;
  94. Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
  95. Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
  96. Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
  97. for (i = 0; i < alphaSize; i++)
  98. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  99. while (True) {
  100. nNodes = alphaSize;
  101. nHeap = 0;
  102. heap[0] = 0;
  103. weight[0] = 0;
  104. parent[0] = -2;
  105. for (i = 1; i <= alphaSize; i++) {
  106. parent[i] = -1;
  107. nHeap++;
  108. heap[nHeap] = i;
  109. UPHEAP(nHeap);
  110. }
  111. AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
  112. while (nHeap > 1) {
  113. n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  114. n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  115. nNodes++;
  116. parent[n1] = parent[n2] = nNodes;
  117. weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  118. parent[nNodes] = -1;
  119. nHeap++;
  120. heap[nHeap] = nNodes;
  121. UPHEAP(nHeap);
  122. }
  123. AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
  124. tooLong = False;
  125. for (i = 1; i <= alphaSize; i++) {
  126. j = 0;
  127. k = i;
  128. while (parent[k] >= 0) { k = parent[k]; j++; }
  129. len[i-1] = j;
  130. if (j > maxLen) tooLong = True;
  131. }
  132. if (! tooLong) break;
  133. for (i = 1; i < alphaSize; i++) {
  134. j = weight[i] >> 8;
  135. j = 1 + (j / 2);
  136. weight[i] = j << 8;
  137. }
  138. }
  139. }
  140. /*---------------------------------------------------*/
  141. void BZ2_hbAssignCodes ( Int32 *code,
  142. UChar *length,
  143. Int32 minLen,
  144. Int32 maxLen,
  145. Int32 alphaSize )
  146. {
  147. Int32 n, vec, i;
  148. vec = 0;
  149. for (n = minLen; n <= maxLen; n++) {
  150. for (i = 0; i < alphaSize; i++)
  151. if (length[i] == n) { code[i] = vec; vec++; };
  152. vec <<= 1;
  153. }
  154. }
  155. /*---------------------------------------------------*/
  156. void BZ2_hbCreateDecodeTables ( Int32 *limit,
  157. Int32 *base,
  158. Int32 *perm,
  159. UChar *length,
  160. Int32 minLen,
  161. Int32 maxLen,
  162. Int32 alphaSize )
  163. {
  164. Int32 pp, i, j, vec;
  165. pp = 0;
  166. for (i = minLen; i <= maxLen; i++)
  167. for (j = 0; j < alphaSize; j++)
  168. if (length[j] == i) { perm[pp] = j; pp++; };
  169. for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
  170. for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
  171. for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
  172. for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
  173. vec = 0;
  174. for (i = minLen; i <= maxLen; i++) {
  175. vec += (base[i+1] - base[i]);
  176. limit[i] = vec-1;
  177. vec <<= 1;
  178. }
  179. for (i = minLen + 1; i <= maxLen; i++)
  180. base[i] = ((limit[i-1] + 1) << 1) - base[i];
  181. }
  182. /*-------------------------------------------------------------*/
  183. /*--- end huffman.c ---*/
  184. /*-------------------------------------------------------------*/