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.

221 lines
7.0 KiB

  1. /*
  2. * decblk.c
  3. *
  4. * main decoder module
  5. */
  6. #include "decoder.h"
  7. #include <memory.h>
  8. #pragma intrinsic(memcpy)
  9. /*
  10. * Decode a block type
  11. */
  12. static int decode_block(
  13. t_decoder_context *context,
  14. lzx_block_type block_type,
  15. long bufpos,
  16. long amount_to_decode
  17. )
  18. {
  19. int result;
  20. if (block_type == BLOCKTYPE_ALIGNED)
  21. result = decode_aligned_offset_block(context, bufpos, (int) amount_to_decode);
  22. else if (block_type == BLOCKTYPE_VERBATIM)
  23. result = decode_verbatim_block(context, bufpos, (int) amount_to_decode);
  24. else if (block_type == BLOCKTYPE_UNCOMPRESSED)
  25. result = decode_uncompressed_block(context, bufpos, (int) amount_to_decode);
  26. else /* no other block types exist */
  27. result = -1;
  28. return result;
  29. }
  30. /*
  31. * Main decode entrypoint
  32. */
  33. long NEAR decode_data(t_decoder_context *context, long bytes_to_decode)
  34. {
  35. ulong amount_can_decode;
  36. long total_decoded;
  37. total_decoded = 0;
  38. while (bytes_to_decode > 0)
  39. {
  40. if (context->dec_decoder_state == DEC_STATE_START_NEW_BLOCK)
  41. {
  42. ulong temp1;
  43. ulong temp2;
  44. ulong temp3;
  45. bool do_translation;
  46. /*
  47. * If this is the first time this group, then get the
  48. * file size for translation.
  49. */
  50. if (context->dec_first_time_this_group)
  51. {
  52. context->dec_first_time_this_group = false;
  53. do_translation = (bool) getbits(context, 1);
  54. if (do_translation)
  55. {
  56. ulong high, low;
  57. high = getbits(context, 16);
  58. low = getbits(context, 16);
  59. context->dec_current_file_size = (high<<16)|low;
  60. }
  61. else
  62. {
  63. context->dec_current_file_size = 0;
  64. }
  65. }
  66. /*
  67. * If the last block we decoded was uncompressed, then
  68. * we need to skip the pad byte (if it exists), and
  69. * initialise the decoder's bit buffer
  70. */
  71. if (context->dec_block_type == BLOCKTYPE_UNCOMPRESSED)
  72. {
  73. /*
  74. * If block size was odd, a pad byte is required
  75. */
  76. if (context->dec_original_block_size & 1)
  77. {
  78. if (context->dec_input_curpos < context->dec_end_input_pos)
  79. context->dec_input_curpos++;
  80. }
  81. /* so that initialise_decoder_bitbuf() will succeed */
  82. context->dec_block_type = BLOCKTYPE_INVALID;
  83. initialise_decoder_bitbuf(context);
  84. }
  85. /* get the block type */
  86. context->dec_block_type = (lzx_block_type) getbits(context, 3);
  87. /* get size of block (in uncompressed bytes) to decode */
  88. temp1 = getbits(context, 8);
  89. temp2 = getbits(context, 8);
  90. temp3 = getbits(context, 8);
  91. /*
  92. * How large is the block we're going to decode?
  93. * It can be from 0...16777215 bytes (16MB)
  94. */
  95. context->dec_block_size =
  96. context->dec_original_block_size = (temp1<<16) + (temp2<<8) + (temp3);
  97. /* if block is an aligned type, read the aligned offset tree */
  98. if (context->dec_block_type == BLOCKTYPE_ALIGNED)
  99. read_aligned_offset_tree(context);
  100. /* read trees */
  101. if (context->dec_block_type == BLOCKTYPE_VERBATIM ||
  102. context->dec_block_type == BLOCKTYPE_ALIGNED)
  103. {
  104. /* backup old trees */
  105. memcpy(
  106. context->dec_main_tree_prev_len,
  107. context->dec_main_tree_len,
  108. MAIN_TREE_ELEMENTS
  109. );
  110. memcpy(
  111. context->dec_secondary_length_tree_prev_len,
  112. context->dec_secondary_length_tree_len,
  113. NUM_SECONDARY_LENGTHS
  114. );
  115. read_main_and_secondary_trees(context);
  116. }
  117. else if (context->dec_block_type == BLOCKTYPE_UNCOMPRESSED)
  118. {
  119. if (handle_beginning_of_uncompressed_block(context) == false)
  120. return -1;
  121. }
  122. else
  123. {
  124. /* no other block types are supported at this time */
  125. return -1;
  126. }
  127. context->dec_decoder_state = DEC_STATE_DECODING_DATA;
  128. }
  129. /*
  130. * Keep decoding until the whole block has been decoded
  131. */
  132. while ((context->dec_block_size > 0) && (bytes_to_decode > 0))
  133. {
  134. int decode_residue;
  135. amount_can_decode = min(context->dec_block_size, bytes_to_decode);
  136. /* shouldn't happen */
  137. if (amount_can_decode == 0)
  138. return -1;
  139. decode_residue = decode_block(
  140. context,
  141. context->dec_block_type,
  142. context->dec_bufpos,
  143. amount_can_decode
  144. );
  145. /*
  146. * We should have decoded exactly the amount we wanted,
  147. * since the encoder makes sure that no matches span 32K
  148. * boundaries.
  149. *
  150. * If the data was corrupted, it's possible that we decoded
  151. * up to MAX_MATCH bytes more than we wanted to.
  152. */
  153. if (decode_residue != 0)
  154. {
  155. /* error, we didn't decode what we wanted! */
  156. return -1;
  157. }
  158. context->dec_block_size -= amount_can_decode;
  159. bytes_to_decode -= amount_can_decode;
  160. total_decoded += amount_can_decode;
  161. }
  162. if (context->dec_block_size == 0)
  163. {
  164. context->dec_decoder_state = DEC_STATE_START_NEW_BLOCK;
  165. }
  166. if (bytes_to_decode == 0)
  167. {
  168. initialise_decoder_bitbuf(context);
  169. }
  170. }
  171. #ifdef BIT16
  172. copy_data_to_output(
  173. context,
  174. total_decoded,
  175. context->dec_output_buffer
  176. );
  177. #else
  178. copy_data_to_output(
  179. context,
  180. total_decoded,
  181. context->dec_bufpos ?
  182. &context->dec_mem_window[context->dec_bufpos - total_decoded] :
  183. &context->dec_mem_window[context->dec_window_size - total_decoded]
  184. );
  185. #endif
  186. return total_decoded;
  187. }