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.

351 lines
8.1 KiB

  1. /*
  2. * output.c
  3. *
  4. * General outputting routines
  5. */
  6. #include "deflate.h"
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <crtdbg.h>
  10. //
  11. // Output an element from the pre-tree
  12. //
  13. #define OUTPUT_PRETREE_ELEMENT(element) \
  14. _ASSERT(pretree_len[element] != 0); \
  15. outputBits(context, pretree_len[element], pretree_code[element]);
  16. //
  17. // Output the tree structure for a dynamic block
  18. //
  19. void outputTreeStructure(t_encoder_context *context, const BYTE *literal_tree_len, const BYTE *dist_tree_len)
  20. {
  21. int hdist, hlit, combined_tree_elements, i, pass;
  22. USHORT pretree_freq[NUM_PRETREE_ELEMENTS*2];
  23. USHORT pretree_code[NUM_PRETREE_ELEMENTS];
  24. byte pretree_len[NUM_PRETREE_ELEMENTS];
  25. //
  26. // combined literal + distance length code array for outputting the trees
  27. // in compressed form
  28. //
  29. // +3 is so we can overflow the array when performing run length encoding
  30. // (dummy values are inserted at the end so that run length encoding fails
  31. // before falling off the end of the array)
  32. //
  33. BYTE lens[MAX_LITERAL_TREE_ELEMENTS + MAX_DIST_TREE_ELEMENTS + 3];
  34. //
  35. // Calculate HDIST
  36. //
  37. for (hdist = MAX_DIST_TREE_ELEMENTS - 1; hdist >= 1; hdist--)
  38. {
  39. if (dist_tree_len[hdist] != 0)
  40. break;
  41. }
  42. hdist++;
  43. //
  44. // Calculate HLIT
  45. //
  46. for (hlit = MAX_LITERAL_TREE_ELEMENTS - 1; hlit >= 257; hlit--)
  47. {
  48. if (literal_tree_len[hlit] != 0)
  49. break;
  50. }
  51. hlit++;
  52. //
  53. // Now initialise the array to have all of the hlit and hdist codes
  54. // in it
  55. //
  56. combined_tree_elements = hdist + hlit;
  57. memcpy(lens, literal_tree_len, hlit);
  58. memcpy(&lens[hlit], dist_tree_len, hdist);
  59. //
  60. // Stick in some dummy values at the end so that we don't overflow the
  61. // array when comparing
  62. //
  63. for (i = combined_tree_elements; i < sizeof(lens); i++)
  64. lens[i] = -1;
  65. for (i = 0; i < NUM_PRETREE_ELEMENTS; i++)
  66. pretree_freq[i] = 0;
  67. //
  68. // Output the bitlengths in compressed (run length encoded) form.
  69. //
  70. // Make two passes; on the first pass count the various codes, create
  71. // the tree and output it, on the second pass output the codes using
  72. // the tree.
  73. //
  74. for (pass = 0; pass < 2; pass++)
  75. {
  76. int cur_element;
  77. // are we outputting during this pass?
  78. BOOL outputting = (pass == 1);
  79. cur_element = 0;
  80. while (cur_element < combined_tree_elements)
  81. {
  82. int curlen = lens[cur_element];
  83. int run_length;
  84. //
  85. // See how many consecutive elements have the same value
  86. //
  87. // This won't run off the end of the array; it will hit the -1's
  88. // we stored there
  89. //
  90. for (run_length = cur_element+1; lens[run_length] == curlen; run_length++)
  91. ;
  92. run_length -= cur_element;
  93. //
  94. // For non-zero codes need 4 identical in a row (original code
  95. // plus 3 repeats). We decrement the run_length by one if the
  96. // code is not zero, since we don't count the first (original)
  97. // code in this case.
  98. //
  99. // For zero codes, need 3 zeroes in a row.
  100. //
  101. if (curlen != 0)
  102. run_length--;
  103. if (run_length < 3)
  104. {
  105. if (outputting)
  106. {
  107. OUTPUT_PRETREE_ELEMENT(curlen);
  108. }
  109. else
  110. pretree_freq[curlen]++;
  111. cur_element++;
  112. }
  113. else
  114. {
  115. //
  116. // Elements with zero values are encoded specially
  117. //
  118. if (curlen == 0)
  119. {
  120. //
  121. // Do we use code 17 (3-10 repeated zeroes) or
  122. // code 18 (11-138 repeated zeroes)?
  123. //
  124. if (run_length <= 10)
  125. {
  126. // code 17
  127. if (outputting)
  128. {
  129. OUTPUT_PRETREE_ELEMENT(17);
  130. outputBits(context, 3, run_length - 3);
  131. }
  132. else
  133. {
  134. pretree_freq[17]++;
  135. }
  136. }
  137. else
  138. {
  139. // code 18
  140. if (run_length > 138)
  141. run_length = 138;
  142. if (outputting)
  143. {
  144. OUTPUT_PRETREE_ELEMENT(18);
  145. outputBits(context, 7, run_length - 11);
  146. }
  147. else
  148. {
  149. pretree_freq[18]++;
  150. }
  151. }
  152. cur_element += run_length;
  153. }
  154. else
  155. {
  156. //
  157. // Number of lengths actually encoded. This may end up
  158. // being less than run_length if we have a run length of
  159. // 7 (6 + 1 [which cannot be encoded with a code 16])
  160. //
  161. int run_length_encoded = 0;
  162. // curlen != 0
  163. // can output 3...6 repeats of a non-zero code, so split
  164. // longer runs into short ones (if possible)
  165. // remember to output the code itself first!
  166. if (outputting)
  167. {
  168. OUTPUT_PRETREE_ELEMENT(curlen);
  169. while (run_length >= 3)
  170. {
  171. int this_run = (run_length <= 6) ? run_length : 6;
  172. OUTPUT_PRETREE_ELEMENT(16);
  173. outputBits(context, 2, this_run - 3);
  174. run_length_encoded += this_run;
  175. run_length -= this_run;
  176. }
  177. }
  178. else
  179. {
  180. pretree_freq[curlen]++;
  181. while (run_length >= 3)
  182. {
  183. int this_run = (run_length <= 6) ? run_length : 6;
  184. pretree_freq[16]++;
  185. run_length_encoded += this_run;
  186. run_length -= this_run;
  187. }
  188. }
  189. // +1 for the original code itself
  190. cur_element += (run_length_encoded+1);
  191. }
  192. }
  193. }
  194. //
  195. // If this is the first pass, create the pretree from the
  196. // frequency data and output it, as well as the values of
  197. // HLIT, HDIST, HDCLEN (# pretree codes used)
  198. //
  199. if (pass == 0)
  200. {
  201. int hclen, i;
  202. makeTree(
  203. NUM_PRETREE_ELEMENTS,
  204. 7,
  205. pretree_freq,
  206. pretree_code,
  207. pretree_len
  208. );
  209. //
  210. // Calculate HCLEN
  211. //
  212. for (hclen = NUM_PRETREE_ELEMENTS-1; hclen >= 4; hclen--)
  213. {
  214. if (pretree_len[ g_CodeOrder[hclen] ] != 0)
  215. break;
  216. }
  217. hclen++;
  218. //
  219. // Dynamic block header
  220. //
  221. outputBits(context, 5, hlit - 257);
  222. outputBits(context, 5, hdist - 1);
  223. outputBits(context, 4, hclen - 4);
  224. for (i = 0; i < hclen; i++)
  225. {
  226. outputBits(context, 3, pretree_len[g_CodeOrder[i]]);
  227. }
  228. }
  229. }
  230. }
  231. //
  232. // bitwise i/o
  233. //
  234. void flushOutputBitBuffer(t_encoder_context *context)
  235. {
  236. if (context->bitcount > 0)
  237. {
  238. int prev_bitcount = context->bitcount;
  239. outputBits(context, 16 - context->bitcount, 0);
  240. // backtrack if we have to; ZIP is byte aligned, not 16-bit word aligned
  241. if (prev_bitcount <= 8)
  242. context->output_curpos--;
  243. }
  244. }
  245. //
  246. // Does not check for output overflow, so make sure to call checkOutputOverflow()
  247. // often enough!
  248. //
  249. void outputBits(t_encoder_context *context, int n, int x)
  250. {
  251. _ASSERT(context->output_curpos < context->output_endpos-1);
  252. _ASSERT(n > 0 && n <= 16);
  253. context->bitbuf |= (x << context->bitcount);
  254. context->bitcount += n;
  255. if (context->bitcount >= 16)
  256. {
  257. *context->output_curpos++ = (BYTE) context->bitbuf;
  258. *context->output_curpos++ = (BYTE) (context->bitbuf >> 8);
  259. context->bitbuf >>= 16;
  260. context->bitcount -= 16;
  261. }
  262. }
  263. // initialise the bit buffer
  264. void InitBitBuffer(t_encoder_context *context)
  265. {
  266. context->bitbuf = 0;
  267. context->bitcount = 0;
  268. }
  269. void OutputBlock(t_encoder_context *context)
  270. {
  271. _ASSERT(context->std_encoder != NULL || context->optimal_encoder != NULL);
  272. // we never call OutputBlock() with the fast encoder
  273. _ASSERT(context->fast_encoder == NULL);
  274. if (context->std_encoder != NULL)
  275. StdEncoderOutputBlock(context);
  276. else if (context->optimal_encoder != NULL)
  277. OptimalEncoderOutputBlock(context);
  278. }
  279. void FlushRecordingBuffer(t_encoder_context *context)
  280. {
  281. _ASSERT(context->std_encoder != NULL || context->optimal_encoder != NULL);
  282. _ASSERT(context->fast_encoder == NULL); // fast encoder does not record
  283. if (context->std_encoder != NULL)
  284. {
  285. *context->std_encoder->recording_bufptr++ = (BYTE) context->std_encoder->recording_bitbuf;
  286. *context->std_encoder->recording_bufptr++ = (BYTE) (context->std_encoder->recording_bitbuf >> 8);
  287. }
  288. else if (context->optimal_encoder != NULL)
  289. {
  290. *context->optimal_encoder->recording_bufptr++ = (BYTE) context->optimal_encoder->recording_bitbuf;
  291. *context->optimal_encoder->recording_bufptr++ = (BYTE) (context->optimal_encoder->recording_bitbuf >> 8);
  292. }
  293. }