huffman.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*-------------------------------------------------------------*/
  2. /*--- Huffman coding low-level stuff ---*/
  3. /*--- huffman.c ---*/
  4. /*-------------------------------------------------------------*/
  5. /* ------------------------------------------------------------------
  6. This file is part of bzip2/libbzip2, a program and library for
  7. lossless, block-sorting data compression.
  8. bzip2/libbzip2 version 1.0.8 of 13 July 2019
  9. Copyright (C) 1996-2019 Julian Seward <jseward@acm.org>
  10. Please read the WARNING, DISCLAIMER and PATENTS sections in the
  11. README file.
  12. This program is released under the terms of the license contained
  13. in the file LICENSE.
  14. ------------------------------------------------------------------ */
  15. #include "bzlib_private.h"
  16. /*---------------------------------------------------*/
  17. #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
  18. #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
  19. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  20. #define ADDWEIGHTS(zw1,zw2) \
  21. (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
  22. (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  23. #define UPHEAP(z) \
  24. { \
  25. Int32 zz, tmp; \
  26. zz = z; tmp = heap[zz]; \
  27. while (weight[tmp] < weight[heap[zz >> 1]]) { \
  28. heap[zz] = heap[zz >> 1]; \
  29. zz >>= 1; \
  30. } \
  31. heap[zz] = tmp; \
  32. }
  33. #define DOWNHEAP(z) \
  34. { \
  35. Int32 zz, yy, tmp; \
  36. zz = z; tmp = heap[zz]; \
  37. while (True) { \
  38. yy = zz << 1; \
  39. if (yy > nHeap) break; \
  40. if (yy < nHeap && \
  41. weight[heap[yy+1]] < weight[heap[yy]]) \
  42. yy++; \
  43. if (weight[tmp] < weight[heap[yy]]) break; \
  44. heap[zz] = heap[yy]; \
  45. zz = yy; \
  46. } \
  47. heap[zz] = tmp; \
  48. }
  49. /*---------------------------------------------------*/
  50. void BZ2_hbMakeCodeLengths ( UChar *len,
  51. Int32 *freq,
  52. Int32 alphaSize,
  53. Int32 maxLen )
  54. {
  55. /*--
  56. Nodes and heap entries run from 1. Entry 0
  57. for both the heap and nodes is a sentinel.
  58. --*/
  59. Int32 nNodes, nHeap, n1, n2, i, j, k;
  60. Bool tooLong;
  61. Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
  62. Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
  63. Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
  64. for (i = 0; i < alphaSize; i++)
  65. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  66. while (True) {
  67. nNodes = alphaSize;
  68. nHeap = 0;
  69. heap[0] = 0;
  70. weight[0] = 0;
  71. parent[0] = -2;
  72. for (i = 1; i <= alphaSize; i++) {
  73. parent[i] = -1;
  74. nHeap++;
  75. heap[nHeap] = i;
  76. UPHEAP(nHeap);
  77. }
  78. AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
  79. while (nHeap > 1) {
  80. n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  81. n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  82. nNodes++;
  83. parent[n1] = parent[n2] = nNodes;
  84. weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  85. parent[nNodes] = -1;
  86. nHeap++;
  87. heap[nHeap] = nNodes;
  88. UPHEAP(nHeap);
  89. }
  90. AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
  91. tooLong = False;
  92. for (i = 1; i <= alphaSize; i++) {
  93. j = 0;
  94. k = i;
  95. while (parent[k] >= 0) { k = parent[k]; j++; }
  96. len[i-1] = j;
  97. if (j > maxLen) tooLong = True;
  98. }
  99. if (! tooLong) break;
  100. /* 17 Oct 04: keep-going condition for the following loop used
  101. to be 'i < alphaSize', which missed the last element,
  102. theoretically leading to the possibility of the compressor
  103. looping. However, this count-scaling step is only needed if
  104. one of the generated Huffman code words is longer than
  105. maxLen, which up to and including version 1.0.2 was 20 bits,
  106. which is extremely unlikely. In version 1.0.3 maxLen was
  107. changed to 17 bits, which has minimal effect on compression
  108. ratio, but does mean this scaling step is used from time to
  109. time, enough to verify that it works.
  110. This means that bzip2-1.0.3 and later will only produce
  111. Huffman codes with a maximum length of 17 bits. However, in
  112. order to preserve backwards compatibility with bitstreams
  113. produced by versions pre-1.0.3, the decompressor must still
  114. handle lengths of up to 20. */
  115. for (i = 1; i <= alphaSize; i++) {
  116. j = weight[i] >> 8;
  117. j = 1 + (j / 2);
  118. weight[i] = j << 8;
  119. }
  120. }
  121. }
  122. /*---------------------------------------------------*/
  123. void BZ2_hbAssignCodes ( Int32 *code,
  124. UChar *length,
  125. Int32 minLen,
  126. Int32 maxLen,
  127. Int32 alphaSize )
  128. {
  129. Int32 n, vec, i;
  130. vec = 0;
  131. for (n = minLen; n <= maxLen; n++) {
  132. for (i = 0; i < alphaSize; i++)
  133. if (length[i] == n) { code[i] = vec; vec++; };
  134. vec <<= 1;
  135. }
  136. }
  137. /*---------------------------------------------------*/
  138. void BZ2_hbCreateDecodeTables ( Int32 *limit,
  139. Int32 *base,
  140. Int32 *perm,
  141. UChar *length,
  142. Int32 minLen,
  143. Int32 maxLen,
  144. Int32 alphaSize )
  145. {
  146. Int32 pp, i, j, vec;
  147. pp = 0;
  148. for (i = minLen; i <= maxLen; i++)
  149. for (j = 0; j < alphaSize; j++)
  150. if (length[j] == i) { perm[pp] = j; pp++; };
  151. for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
  152. for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
  153. for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
  154. for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
  155. vec = 0;
  156. for (i = minLen; i <= maxLen; i++) {
  157. vec += (base[i+1] - base[i]);
  158. limit[i] = vec-1;
  159. vec <<= 1;
  160. }
  161. for (i = minLen + 1; i <= maxLen; i++)
  162. base[i] = ((limit[i-1] + 1) << 1) - base[i];
  163. }
  164. /*-------------------------------------------------------------*/
  165. /*--- end huffman.c ---*/
  166. /*-------------------------------------------------------------*/