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.

251 lines
8.0 KiB

  1. /*
  2. * dectree.c
  3. *
  4. * Decoding the encoded tree structures
  5. *
  6. * To save much code size, the fillbuf()/getbits() calls have
  7. * been made into functions, rather than being inlined macros.
  8. * The macros actually take up a lot of space. There is no
  9. * performance loss from doing so here.
  10. */
  11. #include "decoder.h"
  12. /* number of elements in pre-tree */
  13. #define NUM_DECODE_SMALL 20
  14. /* lookup table size */
  15. #define DS_TABLE_BITS 8
  16. /* macro to decode a pre-tree element */
  17. #define DECODE_SMALL(item) \
  18. { \
  19. item = small_table[context->dec_bitbuf >> (32-DS_TABLE_BITS) ]; \
  20. if (item < 0) \
  21. { \
  22. mask = (1L << (32-1-DS_TABLE_BITS)); \
  23. do \
  24. { \
  25. item = -item; \
  26. if (context->dec_bitbuf & mask) \
  27. item = leftright_s[2*item+1]; \
  28. else \
  29. item = leftright_s[2*item]; \
  30. mask >>= 1; \
  31. } while (item < 0); \
  32. } \
  33. fillbuf(context, small_bitlen[item]); \
  34. }
  35. /*
  36. * Reads a compressed tree structure
  37. */
  38. static bool NEAR ReadRepTree(
  39. t_decoder_context *context,
  40. int num_elements,
  41. byte *lastlen,
  42. byte *len
  43. )
  44. {
  45. ulong mask;
  46. int i;
  47. int consecutive;
  48. byte small_bitlen[24];
  49. short small_table[1 << DS_TABLE_BITS];
  50. short leftright_s [2*(2 * 24 - 1)];
  51. short Temp;
  52. /* Declare this inline to help compilers see the optimisation */
  53. static const byte Modulo17Lookup[] =
  54. {
  55. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  56. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  57. };
  58. /* read pre-tree */
  59. for (i = 0; i < NUM_DECODE_SMALL; i++)
  60. {
  61. small_bitlen[i] = (byte) getbits(context, 4);
  62. }
  63. /* exceeded input buffer? */
  64. if (context->dec_error_condition)
  65. return false;
  66. /* make a table for this pre-tree */
  67. make_table(
  68. context,
  69. NUM_DECODE_SMALL,
  70. small_bitlen,
  71. DS_TABLE_BITS,
  72. small_table,
  73. leftright_s
  74. );
  75. for (i = 0; i < num_elements; i++)
  76. {
  77. DECODE_SMALL(Temp);
  78. /* exceeded input buffer? */
  79. if (context->dec_error_condition)
  80. return false;
  81. /* Repeat "TREE_ENC_REP_MIN...TREE_ENC_REP_MIN+(1<<TREE_ENC_REPZ_FIRST_EXTRA_BITS)-1" zeroes */
  82. if (Temp == 17)
  83. {
  84. /* code 17 means "a small number of repeated zeroes" */
  85. consecutive = (byte) getbits(context, TREE_ENC_REPZ_FIRST_EXTRA_BITS);
  86. consecutive += TREE_ENC_REP_MIN;
  87. /* boundary check */
  88. if (i + consecutive >= num_elements)
  89. consecutive = num_elements-i;
  90. while (consecutive-- > 0)
  91. len[i++] = 0;
  92. i--;
  93. }
  94. else if (Temp == 18)
  95. {
  96. /* code 18 means "a large number of repeated zeroes" */
  97. /* Repeat "TREE_ENC_REP_MIN+(1<<TREE_ENC_REPZ_FIRST_EXTRA_BITS)-1...<ditto>+(1<<TREE_ENC_REPZ_SECOND_EXTRA_BITS)-1" zeroes */
  98. consecutive = (byte) getbits(context, TREE_ENC_REPZ_SECOND_EXTRA_BITS);
  99. consecutive += (TREE_ENC_REP_MIN+TREE_ENC_REP_ZERO_FIRST);
  100. /* boundary check */
  101. if (i + consecutive >= num_elements)
  102. consecutive = num_elements-i;
  103. while (consecutive-- > 0)
  104. len[i++] = 0;
  105. i--;
  106. }
  107. else if (Temp == 19)
  108. {
  109. byte value;
  110. /* code 19 means "a small number of repeated somethings" */
  111. /* Repeat "TREE_ENC_REP_MIN...TREE_ENC_REP_MIN+(1<<TREE_ENC_REP_SAME_EXTRA_BITS)-1" elements */
  112. consecutive = (byte) getbits(context, TREE_ENC_REP_SAME_EXTRA_BITS);
  113. consecutive += TREE_ENC_REP_MIN;
  114. /* boundary check */
  115. if (i + consecutive >= num_elements)
  116. consecutive = num_elements-i;
  117. /* get the element number to repeat */
  118. DECODE_SMALL(Temp);
  119. value = Modulo17Lookup[(lastlen[i] - Temp)+17];
  120. while (consecutive-- > 0)
  121. len[i++] = value;
  122. i--;
  123. }
  124. else
  125. {
  126. len[i] = Modulo17Lookup[(lastlen[i] - Temp)+17];
  127. }
  128. }
  129. /* exceeded input buffer? */
  130. if (context->dec_error_condition)
  131. return false;
  132. else
  133. return true;
  134. }
  135. bool NEAR read_main_and_secondary_trees(t_decoder_context *context)
  136. {
  137. /* read first 256 elements (characters) of the main tree */
  138. if (false == ReadRepTree(
  139. context,
  140. 256,
  141. context->dec_main_tree_prev_len,
  142. context->dec_main_tree_len))
  143. {
  144. return false;
  145. }
  146. /*
  147. * read remaining elements (primary match lengths * positions)
  148. * of the main tree
  149. */
  150. if (false == ReadRepTree(
  151. context,
  152. context->dec_num_position_slots*NUM_LENGTHS,
  153. &context->dec_main_tree_prev_len[256],
  154. &context->dec_main_tree_len[256]))
  155. {
  156. return false;
  157. }
  158. /* create lookup table for the main tree */
  159. if (false == make_table(
  160. context,
  161. MAIN_TREE_ELEMENTS,
  162. context->dec_main_tree_len,
  163. MAIN_TREE_TABLE_BITS,
  164. context->dec_main_tree_table,
  165. context->dec_main_tree_left_right))
  166. {
  167. return false;
  168. }
  169. /* read secondary length tree */
  170. if (false == ReadRepTree(
  171. context,
  172. NUM_SECONDARY_LENGTHS,
  173. context->dec_secondary_length_tree_prev_len,
  174. context->dec_secondary_length_tree_len))
  175. {
  176. return false;
  177. }
  178. /* create lookup table for the secondary length tree */
  179. if (false == make_table(
  180. context,
  181. NUM_SECONDARY_LENGTHS,
  182. context->dec_secondary_length_tree_len,
  183. SECONDARY_LEN_TREE_TABLE_BITS,
  184. context->dec_secondary_length_tree_table,
  185. context->dec_secondary_length_tree_left_right))
  186. {
  187. return false;
  188. }
  189. return true;
  190. }
  191. /* read 8 element aligned offset tree */
  192. bool NEAR read_aligned_offset_tree(t_decoder_context *context)
  193. {
  194. int i;
  195. /* read bit lengths of the 8 codes */
  196. for (i = 0; i < 8; i++)
  197. {
  198. context->dec_aligned_len[i] = (byte) getbits(context, 3);
  199. }
  200. if (context->dec_error_condition)
  201. return false;
  202. /*
  203. * Make table with no left/right, and byte Table[] instead of
  204. * short Table[]
  205. */
  206. if (false == make_table_8bit(
  207. context,
  208. context->dec_aligned_len,
  209. (byte *) context->dec_aligned_table))
  210. {
  211. return false;
  212. }
  213. return true;
  214. }