Source code of Windows XP (NT5)
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.

220 lines
3.8 KiB

  1. /*
  2. * Table-building routines
  3. *
  4. * make_table() is based on ** Public Domain ** source "AR001.ZIP".
  5. */
  6. #include "decoder.h"
  7. /*
  8. * Make a decoding table for decoding nchar possible Huffman elements
  9. * with bit lengths given by bitlen.
  10. *
  11. * Output the main lookup table into table[] and use leftright[] for
  12. * supplementary information (for bitlengths > tablebits).
  13. *
  14. * The size of table[] is tablebits elements.
  15. */
  16. #ifndef ASM_MAKE_TABLE
  17. bool NEAR make_table(
  18. t_decoder_context *context,
  19. int nchar,
  20. const byte *bitlen,
  21. byte tablebits,
  22. short *table,
  23. short *leftright
  24. )
  25. {
  26. uint i;
  27. int ch;
  28. short * p;
  29. uint count[17], weight[17], start[18];
  30. int avail;
  31. uint nextcode;
  32. uint k;
  33. byte len;
  34. byte jutbits;
  35. for (i = 1; i <= 16; i++)
  36. count[i] = 0;
  37. /* count the number of elements of each bit length */
  38. for (i = 0; i < (uint) nchar; i++)
  39. count[bitlen[i]]++;
  40. start[1] = 0;
  41. for (i = 1; i <= 16; i++)
  42. start[i + 1] = start[i] + (count[i] << (16 - i));
  43. #ifdef BIT16
  44. if (start[17])
  45. {
  46. return false; /* bad table */
  47. }
  48. #else
  49. if (start[17] != 65536)
  50. {
  51. if (start[17] == 0)
  52. {
  53. /*
  54. * All elements are length zero
  55. */
  56. memset(table, 0, sizeof(ushort)*(1 << tablebits));
  57. return true; /* success */
  58. }
  59. else
  60. {
  61. return false; /* bad table */
  62. }
  63. }
  64. #endif
  65. jutbits = 16 - tablebits;
  66. for (i = 1; i <= tablebits; i++)
  67. {
  68. start[i] >>= jutbits;
  69. weight[i] = 1 << (tablebits - i);
  70. }
  71. while (i <= 16)
  72. {
  73. weight[i] = 1 << (16 - i);
  74. i++;
  75. }
  76. i = start[tablebits+1] >> jutbits;
  77. #ifdef BIT16
  78. if (i)
  79. #else
  80. if (i != 65536)
  81. #endif
  82. {
  83. memset(
  84. &table[i],
  85. 0,
  86. sizeof(ushort)*((1 << tablebits)-i)
  87. );
  88. }
  89. avail = nchar;
  90. for (ch = 0; ch < nchar; ch++)
  91. {
  92. if ((len = bitlen[ch]) == 0)
  93. continue;
  94. nextcode = start[len] + weight[len];
  95. if (len <= tablebits)
  96. {
  97. if (nextcode > (uint) (1 << tablebits))
  98. return false; /* bad table */
  99. for (i = start[len]; i < nextcode; i++)
  100. table[i] = (short) ch;
  101. start[len] = nextcode;
  102. }
  103. else
  104. {
  105. byte i;
  106. k = start[len];
  107. start[len] = nextcode;
  108. p = &table[k >> jutbits];
  109. i = len - tablebits;
  110. k <<= tablebits;
  111. do
  112. {
  113. if (*p == 0)
  114. {
  115. leftright[avail*2] = leftright[avail*2+1] = 0;
  116. *p = (short) -avail;
  117. avail++;
  118. }
  119. if ((signed short) k < 0) // if (k & 32768)
  120. p = &leftright[-(*p)*2+1];
  121. else
  122. p = &leftright[-(*p)*2];
  123. k <<= 1;
  124. i--;
  125. } while (i);
  126. *p = (short) ch;
  127. }
  128. }
  129. return true;
  130. }
  131. #endif
  132. /*
  133. * Specialised make table routine where it is known that there are
  134. * only 8 elements (nchar=8) and tablebits=7 (128 byte lookup table).
  135. *
  136. * Since there can be no overflow, this will be a direct lookup.
  137. *
  138. * Important difference; the lookup table returns a byte, not a ushort.
  139. */
  140. bool NEAR make_table_8bit(t_decoder_context *context, byte bitlen[], byte table[])
  141. {
  142. ushort count[17], weight[17], start[18];
  143. ushort i;
  144. ushort nextcode;
  145. byte len;
  146. byte ch;
  147. for (i = 1; i <= 16; i++)
  148. count[i] = 0;
  149. for (i = 0; i < 8; i++)
  150. count[bitlen[i]]++;
  151. start[1] = 0;
  152. for (i = 1; i <= 16; i++)
  153. start[i + 1] = start[i] + (count[i] << (16 - i));
  154. if (start[17] != 0)
  155. return false; /* bad table */
  156. for (i = 1; i <= 7; i++)
  157. {
  158. start[i] >>= 9;
  159. weight[i] = 1 << (7 - i);
  160. }
  161. while (i <= 16)
  162. {
  163. weight[i] = 1 << (16 - i);
  164. i++;
  165. }
  166. memset(table, 0, 1<<7);
  167. for (ch = 0; ch < 8; ch++)
  168. {
  169. if ((len = bitlen[ch]) == 0)
  170. continue;
  171. nextcode = start[len] + weight[len];
  172. if (nextcode > (1 << 7))
  173. return false; /* bad table */
  174. for (i = start[len]; i < nextcode; i++)
  175. table[i] = ch;
  176. start[len] = nextcode;
  177. }
  178. return true;
  179. }