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.

257 lines
8.4 KiB

  1. /*
  2. * init.c
  3. *
  4. * Initialise encoder
  5. */
  6. #include "encoder.h"
  7. #define MEM_WINDOW_ALLOC_SIZE \
  8. (context->enc_window_size+(MAX_MATCH+EXTRA_SIZE)+context->enc_encoder_second_partition_size)
  9. /*
  10. * Initialise encoder
  11. */
  12. void init_compression_memory(t_encoder_context *context)
  13. {
  14. /* set all root pointers to NULL */
  15. #ifdef MULTIPLE_SEARCH_TREES
  16. memset(
  17. context->enc_tree_root,
  18. 0,
  19. NUM_SEARCH_TREES * sizeof(context->enc_tree_root[0])
  20. );
  21. #else
  22. context->enc_single_tree_root = 0;
  23. #endif
  24. context->enc_MemWindow = context->enc_RealMemWindow - context->enc_window_size;
  25. context->enc_Left = context->enc_RealLeft - context->enc_window_size;
  26. context->enc_Right = context->enc_RealRight - context->enc_window_size;
  27. context->enc_BufPos = context->enc_window_size;
  28. /*
  29. * Set initial state of repeated match offsets
  30. */
  31. context->enc_last_matchpos_offset[0] = 1;
  32. context->enc_last_matchpos_offset[1] = 1;
  33. context->enc_last_matchpos_offset[2] = 1;
  34. /*
  35. * repeated offset states the last time we output a block
  36. * see block.c/encdata.c
  37. */
  38. context->enc_repeated_offset_at_literal_zero[0] = 1;
  39. context->enc_repeated_offset_at_literal_zero[1] = 1;
  40. context->enc_repeated_offset_at_literal_zero[2] = 1;
  41. /* this is the first compressed block in the file */
  42. context->enc_first_block = true;
  43. /* we don't have any cumulative stats yet */
  44. context->enc_need_to_recalc_stats = true;
  45. /* bufpos @ last time we output a block */
  46. context->enc_bufpos_last_output_block = context->enc_BufPos;
  47. /* initialise bit buffer */
  48. context->enc_bitcount = 32;
  49. context->enc_bitbuf = 0;
  50. context->enc_output_overflow = false;
  51. /*
  52. * The last lengths are zeroed in both the encoder and decoder,
  53. * since our tree representation is delta format.
  54. */
  55. memset(context->enc_main_tree_prev_len, 0, MAIN_TREE_ELEMENTS);
  56. memset(context->enc_secondary_tree_prev_len, 0, NUM_SECONDARY_LENGTHS);
  57. /*
  58. * Set the default last tree lengths for cost estimation
  59. */
  60. memset(context->enc_main_tree_len, 8, NUM_CHARS);
  61. memset(&context->enc_main_tree_len[NUM_CHARS], 9, MAIN_TREE_ELEMENTS-NUM_CHARS);
  62. memset(context->enc_secondary_tree_len, 6, NUM_SECONDARY_LENGTHS);
  63. memset(context->enc_aligned_tree_len, 3, ALIGNED_NUM_ELEMENTS);
  64. prevent_far_matches(context); /* prevent far match 2's from being taken */
  65. context->enc_bufpos_at_last_block = context->enc_BufPos;
  66. context->enc_earliest_window_data_remaining = context->enc_BufPos;
  67. context->enc_input_running_total = 0;
  68. context->enc_first_time_this_group = true;
  69. /* Clear the literal types array */
  70. memset(context->enc_ItemType, 0, MAX_LITERAL_ITEMS/8);
  71. /* No literals or distances encoded yet */
  72. context->enc_literals = 0;
  73. context->enc_distances = 0;
  74. /* No block splits yet */
  75. context->enc_num_block_splits = 0;
  76. context->enc_repeated_offset_at_literal_zero[0] = 1;
  77. context->enc_repeated_offset_at_literal_zero[1] = 1;
  78. context->enc_repeated_offset_at_literal_zero[2] = 1;
  79. /* reset instruction pointer (for translation) to zero */
  80. reset_translation(context);
  81. context->enc_num_cfdata_frames = 0;
  82. }
  83. /*
  84. * Allocate memory for the compressor
  85. *
  86. * Returns true if successful, false otherwise
  87. */
  88. bool comp_alloc_compress_memory(t_encoder_context *context)
  89. {
  90. ulong pos_start;
  91. #ifdef MULTIPLE_SEARCH_TREES
  92. context->enc_tree_root = NULL;
  93. #endif
  94. context->enc_RealLeft = NULL;
  95. context->enc_RealRight = NULL;
  96. context->enc_MemWindow = NULL;
  97. context->enc_decision_node = NULL;
  98. context->enc_LitData = NULL;
  99. #ifdef EXTRALONGMATCHES
  100. context->enc_ExtraLength = NULL;
  101. #endif
  102. context->enc_DistData = NULL;
  103. context->enc_ItemType = NULL;
  104. context->enc_output_buffer_start = NULL;
  105. /* ALSO NULLIFY BUFFERS! */
  106. /*
  107. * Determine the number of position slots in the main tree
  108. */
  109. context->enc_num_position_slots = 4;
  110. pos_start = 4;
  111. while (1)
  112. {
  113. pos_start += 1 << enc_extra_bits[context->enc_num_position_slots];
  114. context->enc_num_position_slots++;
  115. if (pos_start >= context->enc_window_size)
  116. break;
  117. }
  118. #ifdef MULTIPLE_SEARCH_TREES
  119. context->enc_tree_root = (ulong *) context->enc_malloc(
  120. context->enc_mallochandle,
  121. sizeof(context->enc_tree_root[0]) * NUM_SEARCH_TREES
  122. );
  123. if (context->enc_tree_root == NULL)
  124. {
  125. return false;
  126. }
  127. #endif
  128. context->enc_RealLeft = (ulong *) context->enc_malloc(
  129. context->enc_mallochandle,
  130. sizeof(ulong) * MEM_WINDOW_ALLOC_SIZE
  131. );
  132. if (context->enc_RealLeft == NULL)
  133. {
  134. return false;
  135. }
  136. context->enc_RealRight = (ulong *) context->enc_malloc(
  137. context->enc_mallochandle,
  138. sizeof(ulong) * MEM_WINDOW_ALLOC_SIZE
  139. );
  140. if (context->enc_RealRight == NULL)
  141. {
  142. return false;
  143. }
  144. context->enc_RealMemWindow = (byte *) context->enc_malloc(
  145. context->enc_mallochandle,
  146. MEM_WINDOW_ALLOC_SIZE
  147. );
  148. if (context->enc_RealMemWindow == NULL)
  149. {
  150. return false;
  151. }
  152. context->enc_MemWindow = context->enc_RealMemWindow;
  153. context->enc_LitData = (byte *) context->enc_malloc(
  154. context->enc_mallochandle,
  155. MAX_LITERAL_ITEMS * sizeof(*context->enc_LitData)
  156. );
  157. if (context->enc_LitData == NULL)
  158. {
  159. return false;
  160. }
  161. #ifdef EXTRALONGMATCHES
  162. context->enc_ExtraLength = (ushort *) context->enc_malloc(
  163. context->enc_mallochandle,
  164. MAX_LITERAL_ITEMS * sizeof(*context->enc_ExtraLength)
  165. );
  166. if (context->enc_ExtraLength == NULL)
  167. {
  168. return false;
  169. }
  170. #endif
  171. context->enc_DistData = (ulong *) context->enc_malloc(
  172. context->enc_mallochandle,
  173. MAX_DIST_ITEMS * sizeof(*context->enc_DistData)
  174. );
  175. if (context->enc_DistData == NULL)
  176. {
  177. return false;
  178. }
  179. context->enc_ItemType = (byte *) context->enc_malloc(
  180. context->enc_mallochandle,
  181. MAX_LITERAL_ITEMS/8
  182. );
  183. if (context->enc_ItemType == NULL)
  184. {
  185. return false;
  186. }
  187. create_slot_lookup_table(context);
  188. create_ones_table(context);
  189. if (init_compressed_output_buffer(context) == false)
  190. {
  191. return false;
  192. }
  193. context->enc_decision_node = context->enc_malloc(
  194. context->enc_mallochandle,
  195. sizeof(decision_node) * (LOOK+MAX_MATCH+16)
  196. );
  197. if (context->enc_decision_node == NULL)
  198. {
  199. return false;
  200. }
  201. /* success */
  202. return true;
  203. }