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.

611 lines
14 KiB

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