Leaked source code of windows server 2003
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.

224 lines
5.6 KiB

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