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.

277 lines
6.9 KiB

  1. //
  2. // inflate.c
  3. //
  4. // Decompressor
  5. //
  6. #include <crtdbg.h>
  7. #include <stdio.h>
  8. #include "inflate.h"
  9. #include "infmacro.h"
  10. #include "infgzip.h"
  11. #include "maketbl.h"
  12. //
  13. // Local function prototypes
  14. //
  15. static BOOL decodeBlock(t_decoder_context *context);
  16. static BOOL makeTables(t_decoder_context *context);
  17. HRESULT WINAPI Decompress(
  18. PVOID void_context,
  19. CONST BYTE * input,
  20. LONG input_size,
  21. BYTE * output,
  22. LONG output_size,
  23. PLONG input_used,
  24. PLONG output_used
  25. )
  26. {
  27. t_decoder_context *context = (t_decoder_context *) void_context;
  28. context->input_curpos = input;
  29. context->end_input_buffer = input + input_size;
  30. context->output_curpos = output;
  31. context->end_output_buffer = output + output_size;
  32. context->output_buffer = output;
  33. //
  34. // Keep decoding blocks until the output fills up, we read all the input, or we enter
  35. // the "done" state
  36. //
  37. // Note that INPUT_EOF() is not a sufficient check for determining that all the input
  38. // has been used; there could be an additional block stored entirely in the bit buffer.
  39. // For this reason, if we're in the READING_BFINAL state (start of new block) after
  40. // calling decodeBlock(), don't quit the loop unless there is truly no input left in
  41. // the bit buffer.
  42. //
  43. while ( (context->output_curpos < context->end_output_buffer) &&
  44. (!INPUT_EOF()) &&
  45. (context->state != STATE_DONE && context->state != STATE_VERIFYING_GZIP_FOOTER)
  46. )
  47. {
  48. retry:
  49. if (decodeBlock(context) == FALSE)
  50. {
  51. *input_used = 0;
  52. *output_used = 0;
  53. return E_FAIL;
  54. }
  55. // No more input bytes, but am starting a new block and there's at least one bit
  56. // in the bit buffer
  57. if (context->state == STATE_READING_BFINAL && INPUT_EOF() && context->bitcount > -16)
  58. goto retry;
  59. }
  60. *input_used = (long) (context->input_curpos - input);
  61. *output_used = (long) (context->output_curpos - output);
  62. if (context->using_gzip)
  63. {
  64. // Calculate the crc32 of everything we just decompressed, and then, if our state
  65. // is STATE_DONE, verify the crc
  66. if (*output_used > 0)
  67. {
  68. context->gzip_crc32 = GzipCRC32(context->gzip_crc32, output, *output_used);
  69. context->gzip_output_stream_size += (*output_used);
  70. }
  71. if (context->state == STATE_VERIFYING_GZIP_FOOTER)
  72. {
  73. context->state = STATE_DONE;
  74. // Now do our crc/input size check
  75. if (context->gzip_crc32 != context->gzip_footer_crc32 ||
  76. context->gzip_output_stream_size != context->gzip_footer_output_stream_size)
  77. {
  78. *input_used = 0;
  79. *output_used = 0;
  80. return E_FAIL;
  81. }
  82. }
  83. }
  84. if (*input_used == 0 && *output_used == 0)
  85. {
  86. if (context->state == STATE_DONE)
  87. return S_FALSE; // End of compressed data
  88. else
  89. return E_FAIL; // Avoid infinite loops
  90. }
  91. else
  92. {
  93. return S_OK;
  94. }
  95. }
  96. //
  97. // Returns TRUE for success, FALSE for an error of some kind (invalid data)
  98. //
  99. static BOOL decodeBlock(t_decoder_context *context)
  100. {
  101. BOOL eob, result;
  102. if (context->state == STATE_DONE || context->state == STATE_VERIFYING_GZIP_FOOTER)
  103. return TRUE;
  104. if (context->using_gzip)
  105. {
  106. if (context->state == STATE_READING_GZIP_HEADER)
  107. {
  108. if (ReadGzipHeader(context) == FALSE)
  109. return FALSE;
  110. // If we're still reading the GZIP header it means we ran out of input
  111. if (context->state == STATE_READING_GZIP_HEADER)
  112. return TRUE;
  113. }
  114. if (context->state == STATE_START_READING_GZIP_FOOTER || context->state == STATE_READING_GZIP_FOOTER)
  115. {
  116. if (ReadGzipFooter(context) == FALSE)
  117. return FALSE;
  118. // Whether we ran out of input or not, return
  119. return TRUE;
  120. }
  121. }
  122. //
  123. // Do we need to fill our bit buffer?
  124. //
  125. // This will happen the very first time we call Decompress(), as well as after decoding
  126. // an uncompressed block
  127. //
  128. if (context->state == STATE_READING_BFINAL_NEED_TO_INIT_BITBUF)
  129. {
  130. //
  131. // If we didn't have enough bits to init, return
  132. //
  133. if (initBitBuffer(context) == FALSE)
  134. return TRUE;
  135. }
  136. //
  137. // Need to read bfinal bit
  138. //
  139. if (context->state == STATE_READING_BFINAL)
  140. {
  141. // Need 1 bit
  142. if (ensureBitsContext(context, 1) == FALSE)
  143. return TRUE;
  144. context->bfinal = getBits(context, 1);
  145. context->state = STATE_READING_BTYPE;
  146. }
  147. if (context->state == STATE_READING_BTYPE)
  148. {
  149. // Need 2 bits
  150. if (ensureBitsContext(context, 2) == FALSE)
  151. return TRUE;
  152. context->btype = getBits(context, 2);
  153. if (context->btype == BLOCKTYPE_DYNAMIC)
  154. {
  155. context->state = STATE_READING_NUM_LIT_CODES;
  156. }
  157. else if (context->btype == BLOCKTYPE_FIXED)
  158. {
  159. context->state = STATE_DECODE_TOP;
  160. }
  161. else if (context->btype == BLOCKTYPE_UNCOMPRESSED)
  162. {
  163. context->state = STATE_UNCOMPRESSED_ALIGNING;
  164. }
  165. else
  166. {
  167. // unsupported compression mode
  168. return FALSE;
  169. }
  170. }
  171. if (context->btype == BLOCKTYPE_DYNAMIC)
  172. {
  173. if (context->state < STATE_DECODE_TOP)
  174. {
  175. if (readDynamicBlockHeader(context) == FALSE)
  176. return FALSE;
  177. if (context->state == STATE_DECODE_TOP)
  178. {
  179. if (makeTables(context) == FALSE)
  180. return FALSE; // bad tables
  181. }
  182. else
  183. {
  184. return TRUE; // not enough input
  185. }
  186. }
  187. result = DecodeDynamicBlock(context, &eob);
  188. if (eob)
  189. context->state = STATE_READING_BFINAL;
  190. }
  191. else if (context->btype == BLOCKTYPE_FIXED)
  192. {
  193. result = DecodeStaticBlock(context, &eob);
  194. if (eob)
  195. context->state = STATE_READING_BFINAL;
  196. }
  197. else if (context->btype == BLOCKTYPE_UNCOMPRESSED)
  198. {
  199. result = decodeUncompressedBlock(context, &eob);
  200. }
  201. else
  202. {
  203. //
  204. // Invalid block type
  205. //
  206. return FALSE;
  207. }
  208. //
  209. // If we reached the end of the block and the block we were decoding had
  210. // bfinal=1 (final block)
  211. //
  212. if (eob && context->bfinal)
  213. {
  214. if (context->using_gzip)
  215. context->state = STATE_START_READING_GZIP_FOOTER;
  216. else
  217. context->state = STATE_DONE;
  218. }
  219. return result;
  220. }
  221. //
  222. // Will throw an exception if a corrupt table is detected
  223. //
  224. static BOOL makeTables(t_decoder_context *context)
  225. {
  226. if (makeTable(
  227. MAX_LITERAL_TREE_ELEMENTS,
  228. LITERAL_TABLE_BITS,
  229. context->literal_tree_code_length,
  230. context->literal_table,
  231. context->literal_left,
  232. context->literal_right) == FALSE)
  233. return FALSE;
  234. return makeTable(
  235. MAX_DIST_TREE_ELEMENTS,
  236. DISTANCE_TABLE_BITS,
  237. context->distance_tree_code_length,
  238. context->distance_table,
  239. context->distance_left,
  240. context->distance_right
  241. );
  242. }