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.

674 lines
16 KiB

  1. //
  2. // infdyna.c
  3. //
  4. // Decompress a dynamically compressed block
  5. //
  6. #include <stdio.h>
  7. #include <crtdbg.h>
  8. #include "inflate.h"
  9. #include "infmacro.h"
  10. #include "maketbl.h"
  11. #define OUTPUT_EOF() (output_curpos >= context->end_output_buffer)
  12. //
  13. // This is the slow version, which worries about the input running out or the output
  14. // running out. The trick here is to not read any more bytes than we need to; theoretically
  15. // the "end of block" code could be 1 bit, so we cannot always assume that it is ok to fill
  16. // the bit buffer with 16 bits right before a table decode.
  17. //
  18. BOOL DecodeDynamicBlock(t_decoder_context *context, BOOL *end_of_block_code_seen)
  19. {
  20. const byte * input_ptr;
  21. const byte * end_input_buffer;
  22. byte * output_curpos;
  23. byte * window;
  24. unsigned long bufpos;
  25. unsigned long bitbuf;
  26. int bitcount;
  27. int length;
  28. long dist_code;
  29. unsigned long offset;
  30. t_decoder_state old_state;
  31. BYTE fCanTryFastEncoder = TRUE;
  32. *end_of_block_code_seen = FALSE;
  33. //
  34. // Store these variables locally for speed
  35. //
  36. top:
  37. output_curpos = context->output_curpos;
  38. window = context->window;
  39. bufpos = context->bufpos;
  40. end_input_buffer = context->end_input_buffer;
  41. LOAD_BITBUF_VARS();
  42. _ASSERT(bitcount >= -16);
  43. old_state = context->state;
  44. context->state = STATE_DECODE_TOP; // reset state
  45. switch (old_state)
  46. {
  47. case STATE_DECODE_TOP:
  48. break;
  49. case STATE_HAVE_INITIAL_LENGTH:
  50. length = context->length;
  51. goto reenter_state_have_initial_length;
  52. case STATE_HAVE_FULL_LENGTH:
  53. length = context->length;
  54. goto reenter_state_have_full_length;
  55. case STATE_HAVE_DIST_CODE:
  56. length = context->length;
  57. dist_code = context->dist_code;
  58. goto reenter_state_have_dist_code;
  59. case STATE_INTERRUPTED_MATCH:
  60. length = context->length;
  61. offset = context->offset;
  62. goto reenter_state_interrupted_match;
  63. default:
  64. _ASSERT(0); // error, invalid state!
  65. }
  66. do
  67. {
  68. //
  69. // The first time we're at the top of this loop, check whether we can use the
  70. // fast encoder; we will do this if the input and output buffers are nowhere
  71. // near the end, which allows the fast encoder to be a little more relaxed
  72. // about checking for these conditions
  73. //
  74. // If we cannot enter the fast encoder when we first check, then we will not
  75. // be able to enter it again while we're in this function (the amount of
  76. // input/output available is not going to get any larger), so don't check
  77. // again.
  78. //
  79. if (fCanTryFastEncoder)
  80. {
  81. if (context->output_curpos + MAX_MATCH < context->end_output_buffer &&
  82. context->input_curpos + 12 < context->end_input_buffer)
  83. {
  84. SAVE_BITBUF_VARS();
  85. context->output_curpos = output_curpos;
  86. context->bufpos = bufpos;
  87. if (FastDecodeDynamicBlock(context, end_of_block_code_seen) == FALSE)
  88. return FALSE;
  89. if (*end_of_block_code_seen)
  90. return TRUE;
  91. goto top;
  92. }
  93. else
  94. {
  95. // don't check again
  96. fCanTryFastEncoder = FALSE;
  97. }
  98. }
  99. //
  100. // decode an element from the main tree
  101. //
  102. // we must have at least 1 bit available
  103. _ASSERT(bitcount >= -16);
  104. if (bitcount == -16)
  105. {
  106. if (input_ptr >= end_input_buffer)
  107. break;
  108. bitbuf |= ((*input_ptr++) << (bitcount+16));
  109. bitcount += 8;
  110. }
  111. retry_decode_literal:
  112. // assert that at least 1 bit is present
  113. _ASSERT(bitcount > -16);
  114. // decode an element from the literal tree
  115. length = context->literal_table[bitbuf & LITERAL_TABLE_MASK];
  116. while (length < 0)
  117. {
  118. unsigned long mask = 1 << LITERAL_TABLE_BITS;
  119. do
  120. {
  121. length = -length;
  122. if ((bitbuf & mask) == 0)
  123. length = context->literal_left[length];
  124. else
  125. length = context->literal_right[length];
  126. mask <<= 1;
  127. } while (length < 0);
  128. }
  129. //
  130. // If this code is longer than the # bits we had in the bit buffer (i.e.
  131. // we read only part of the code - but enough to know that it's too long),
  132. // read more bits and retry
  133. //
  134. if (context->literal_tree_code_length[length] > (bitcount+16))
  135. {
  136. // if we run out of bits, break
  137. if (input_ptr >= end_input_buffer)
  138. break;
  139. bitbuf |= ((*input_ptr++) << (bitcount+16));
  140. bitcount += 8;
  141. goto retry_decode_literal;
  142. }
  143. DUMPBITS(context->literal_tree_code_length[length]);
  144. _ASSERT(bitcount >= -16);
  145. //
  146. // Is it a character or a match?
  147. //
  148. if (length < 256)
  149. {
  150. // it's an unmatched symbol
  151. window[bufpos] = *output_curpos++ = (byte) length;
  152. bufpos = (bufpos + 1) & WINDOW_MASK;
  153. }
  154. else
  155. {
  156. // it's a match
  157. int extra_bits;
  158. length -= 257;
  159. // if value was 256, that was the end-of-block code
  160. if (length < 0)
  161. {
  162. *end_of_block_code_seen = TRUE;
  163. break;
  164. }
  165. //
  166. // Get match length
  167. //
  168. //
  169. // These matches are by far the most common case.
  170. //
  171. if (length < 8)
  172. {
  173. // no extra bits
  174. // match length = 3,4,5,6,7,8,9,10
  175. length += 3;
  176. }
  177. else
  178. {
  179. int extra_bits;
  180. reenter_state_have_initial_length:
  181. extra_bits = g_ExtraLengthBits[length];
  182. if (extra_bits > 0)
  183. {
  184. // make sure we have this many bits in the bit buffer
  185. if (extra_bits > bitcount + 16)
  186. {
  187. // if we run out of bits, break
  188. if (input_ptr >= end_input_buffer)
  189. {
  190. context->state = STATE_HAVE_INITIAL_LENGTH;
  191. context->length = length;
  192. break;
  193. }
  194. bitbuf |= ((*input_ptr++) << (bitcount+16));
  195. bitcount += 8;
  196. // extra_length_bits will be no more than 5, so we need to read at
  197. // most one byte of input to satisfy this request
  198. }
  199. length = g_LengthBase[length] + (bitbuf & g_BitMask[extra_bits]);
  200. DUMPBITS(extra_bits);
  201. _ASSERT(bitcount >= -16);
  202. }
  203. else
  204. {
  205. /*
  206. * we know length > 8 and extra_bits == 0, there the length must be 258
  207. */
  208. length = 258; /* g_LengthBase[length]; */
  209. }
  210. }
  211. //
  212. // Get match distance
  213. //
  214. // decode distance code
  215. reenter_state_have_full_length:
  216. // we must have at least 1 bit available
  217. if (bitcount == -16)
  218. {
  219. if (input_ptr >= end_input_buffer)
  220. {
  221. context->state = STATE_HAVE_FULL_LENGTH;
  222. context->length = length;
  223. break;
  224. }
  225. bitbuf |= ((*input_ptr++) << (bitcount+16));
  226. bitcount += 8;
  227. }
  228. retry_decode_distance:
  229. // assert that at least 1 bit is present
  230. _ASSERT(bitcount > -16);
  231. dist_code = context->distance_table[bitbuf & DISTANCE_TABLE_MASK];
  232. while (dist_code < 0)
  233. {
  234. unsigned long mask = 1 << DISTANCE_TABLE_BITS;
  235. do
  236. {
  237. dist_code = -dist_code;
  238. if ((bitbuf & mask) == 0)
  239. dist_code = context->distance_left[dist_code];
  240. else
  241. dist_code = context->distance_right[dist_code];
  242. mask <<= 1;
  243. } while (dist_code < 0);
  244. }
  245. //
  246. // If this code is longer than the # bits we had in the bit buffer (i.e.
  247. // we read only part of the code - but enough to know that it's too long),
  248. // read more bits and retry
  249. //
  250. if (context->distance_tree_code_length[dist_code] > (bitcount+16))
  251. {
  252. // if we run out of bits, break
  253. if (input_ptr >= end_input_buffer)
  254. {
  255. context->state = STATE_HAVE_FULL_LENGTH;
  256. context->length = length;
  257. break;
  258. }
  259. bitbuf |= ((*input_ptr++) << (bitcount+16));
  260. bitcount += 8;
  261. _ASSERT(bitcount >= -16);
  262. goto retry_decode_distance;
  263. }
  264. DUMPBITS(context->distance_tree_code_length[dist_code]);
  265. // To avoid a table lookup we note that for dist_code >= 2,
  266. // extra_bits = (dist_code-2) >> 1
  267. //
  268. // Old (intuitive) way of doing this:
  269. // offset = distance_base_position[dist_code] +
  270. // getBits(extra_distance_bits[dist_code]);
  271. reenter_state_have_dist_code:
  272. _ASSERT(bitcount >= -16);
  273. extra_bits = (dist_code-2) >> 1;
  274. if (extra_bits > 0)
  275. {
  276. // make sure we have this many bits in the bit buffer
  277. if (extra_bits > bitcount + 16)
  278. {
  279. // if we run out of bits, break
  280. if (input_ptr >= end_input_buffer)
  281. {
  282. context->state = STATE_HAVE_DIST_CODE;
  283. context->length = length;
  284. context->dist_code = dist_code;
  285. break;
  286. }
  287. bitbuf |= ((*input_ptr++) << (bitcount+16));
  288. bitcount += 8;
  289. // extra_length_bits can be > 8, so check again
  290. if (extra_bits > bitcount + 16)
  291. {
  292. // if we run out of bits, break
  293. if (input_ptr >= end_input_buffer)
  294. {
  295. context->state = STATE_HAVE_DIST_CODE;
  296. context->length = length;
  297. context->dist_code = dist_code;
  298. break;
  299. }
  300. bitbuf |= ((*input_ptr++) << (bitcount+16));
  301. bitcount += 8;
  302. }
  303. }
  304. offset = g_DistanceBasePosition[dist_code] + (bitbuf & g_BitMask[extra_bits]);
  305. DUMPBITS(extra_bits);
  306. _ASSERT(bitcount >= -16);
  307. }
  308. else
  309. {
  310. offset = dist_code + 1;
  311. }
  312. // copy remaining byte(s) of match
  313. reenter_state_interrupted_match:
  314. do
  315. {
  316. window[bufpos] = *output_curpos++ = window[(bufpos - offset) & WINDOW_MASK];
  317. bufpos = (bufpos + 1) & WINDOW_MASK;
  318. if (--length == 0)
  319. break;
  320. } while (output_curpos < context->end_output_buffer);
  321. if (length > 0)
  322. {
  323. context->state = STATE_INTERRUPTED_MATCH;
  324. context->length = length;
  325. context->offset = offset;
  326. break;
  327. }
  328. }
  329. // it's "<=" because we end when we received the end-of-block code,
  330. // not when we fill up the output, however, this will catch cases
  331. // of corrupted data where there is no end-of-output code
  332. } while (output_curpos < context->end_output_buffer);
  333. _ASSERT(bitcount >= -16);
  334. SAVE_BITBUF_VARS();
  335. context->output_curpos = output_curpos;
  336. context->bufpos = bufpos;
  337. return TRUE;
  338. }
  339. //
  340. // This is the fast version, which assumes that, at the top of the loop:
  341. //
  342. // 1. There are at least 12 bytes of input available at the top of the loop (so that we don't
  343. // have to check input EOF several times in the middle of the loop)
  344. //
  345. // and
  346. //
  347. // 2. There are at least MAX_MATCH bytes of output available (so that we don't have to check
  348. // for output EOF while we're copying matches)
  349. //
  350. // The state must also be STATE_DECODE_TOP on entering and exiting this function
  351. //
  352. BOOL FastDecodeDynamicBlock(t_decoder_context *context, BOOL *end_of_block_code_seen)
  353. {
  354. const byte * input_ptr;
  355. const byte * end_input_buffer;
  356. byte * output_curpos;
  357. byte * window;
  358. unsigned long bufpos;
  359. unsigned long bitbuf;
  360. int bitcount;
  361. int length;
  362. long dist_code;
  363. unsigned long offset;
  364. *end_of_block_code_seen = FALSE;
  365. //
  366. // Store these variables locally for speed
  367. //
  368. output_curpos = context->output_curpos;
  369. window = context->window;
  370. bufpos = context->bufpos;
  371. end_input_buffer = context->end_input_buffer;
  372. LOAD_BITBUF_VARS();
  373. _ASSERT(context->state == STATE_DECODE_TOP);
  374. _ASSERT(input_ptr + 12 < end_input_buffer);
  375. _ASSERT(output_curpos + MAX_MATCH < context->end_output_buffer);
  376. // make sure there are at least 16 bits in the bit buffer
  377. while (bitcount <= 0)
  378. {
  379. bitbuf |= ((*input_ptr++) << (bitcount+16));
  380. bitcount += 8;
  381. }
  382. do
  383. {
  384. //
  385. // decode an element from the main tree
  386. //
  387. // decode an element from the literal tree
  388. length = context->literal_table[bitbuf & LITERAL_TABLE_MASK];
  389. while (length < 0)
  390. {
  391. unsigned long mask = 1 << LITERAL_TABLE_BITS;
  392. do
  393. {
  394. length = -length;
  395. if ((bitbuf & mask) == 0)
  396. length = context->literal_left[length];
  397. else
  398. length = context->literal_right[length];
  399. mask <<= 1;
  400. } while (length < 0);
  401. }
  402. DUMPBITS(context->literal_tree_code_length[length]);
  403. if (bitcount <= 0)
  404. {
  405. bitbuf |= ((*input_ptr++) << (bitcount+16));
  406. bitcount += 8;
  407. if (bitcount <= 0)
  408. {
  409. bitbuf |= ((*input_ptr++) << (bitcount+16));
  410. bitcount += 8;
  411. }
  412. }
  413. //
  414. // Is it a character or a match?
  415. //
  416. if (length < 256)
  417. {
  418. // it's an unmatched symbol
  419. window[bufpos] = *output_curpos++ = (byte) length;
  420. bufpos = (bufpos + 1) & WINDOW_MASK;
  421. }
  422. else
  423. {
  424. // it's a match
  425. int extra_bits;
  426. length -= 257;
  427. // if value was 256, that was the end-of-block code
  428. if (length < 0)
  429. {
  430. *end_of_block_code_seen = TRUE;
  431. break;
  432. }
  433. //
  434. // Get match length
  435. //
  436. //
  437. // These matches are by far the most common case.
  438. //
  439. if (length < 8)
  440. {
  441. // no extra bits
  442. // match length = 3,4,5,6,7,8,9,10
  443. length += 3;
  444. }
  445. else
  446. {
  447. int extra_bits;
  448. extra_bits = g_ExtraLengthBits[length];
  449. if (extra_bits > 0)
  450. {
  451. length = g_LengthBase[length] + (bitbuf & g_BitMask[extra_bits]);
  452. DUMPBITS(extra_bits);
  453. if (bitcount <= 0)
  454. {
  455. bitbuf |= ((*input_ptr++) << (bitcount+16));
  456. bitcount += 8;
  457. if (bitcount <= 0)
  458. {
  459. bitbuf |= ((*input_ptr++) << (bitcount+16));
  460. bitcount += 8;
  461. }
  462. }
  463. }
  464. else
  465. {
  466. /*
  467. * we know length > 8 and extra_bits == 0, there the length must be 258
  468. */
  469. length = 258; /* g_LengthBase[length]; */
  470. }
  471. }
  472. //
  473. // Get match distance
  474. //
  475. // decode distance code
  476. dist_code = context->distance_table[bitbuf & DISTANCE_TABLE_MASK];
  477. while (dist_code < 0)
  478. {
  479. unsigned long mask = 1 << DISTANCE_TABLE_BITS;
  480. do
  481. {
  482. dist_code = -dist_code;
  483. if ((bitbuf & mask) == 0)
  484. dist_code = context->distance_left[dist_code];
  485. else
  486. dist_code = context->distance_right[dist_code];
  487. mask <<= 1;
  488. } while (dist_code < 0);
  489. }
  490. DUMPBITS(context->distance_tree_code_length[dist_code]);
  491. if (bitcount <= 0)
  492. {
  493. bitbuf |= ((*input_ptr++) << (bitcount+16));
  494. bitcount += 8;
  495. if (bitcount <= 0)
  496. {
  497. bitbuf |= ((*input_ptr++) << (bitcount+16));
  498. bitcount += 8;
  499. }
  500. }
  501. // To avoid a table lookup we note that for dist_code >= 2,
  502. // extra_bits = (dist_code-2) >> 1
  503. //
  504. // Old (intuitive) way of doing this:
  505. // offset = distance_base_position[dist_code] +
  506. // getBits(extra_distance_bits[dist_code]);
  507. extra_bits = (dist_code-2) >> 1;
  508. if (extra_bits > 0)
  509. {
  510. offset = g_DistanceBasePosition[dist_code] + (bitbuf & g_BitMask[extra_bits]);
  511. DUMPBITS(extra_bits);
  512. if (bitcount <= 0)
  513. {
  514. bitbuf |= ((*input_ptr++) << (bitcount+16));
  515. bitcount += 8;
  516. if (bitcount <= 0)
  517. {
  518. bitbuf |= ((*input_ptr++) << (bitcount+16));
  519. bitcount += 8;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. offset = dist_code + 1;
  526. }
  527. // copy remaining byte(s) of match
  528. do
  529. {
  530. window[bufpos] = *output_curpos++ = window[(bufpos - offset) & WINDOW_MASK];
  531. bufpos = (bufpos + 1) & WINDOW_MASK;
  532. } while (--length != 0);
  533. }
  534. } while ((input_ptr + 12 < end_input_buffer) && (output_curpos + MAX_MATCH < context->end_output_buffer));
  535. // make sure there are at least 16 bits in the bit buffer
  536. while (bitcount <= 0)
  537. {
  538. bitbuf |= ((*input_ptr++) << (bitcount+16));
  539. bitcount += 8;
  540. }
  541. SAVE_BITBUF_VARS();
  542. context->output_curpos = output_curpos;
  543. context->bufpos = bufpos;
  544. return TRUE;
  545. }