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.

151 lines
5.0 KiB

  1. #include "common.h"
  2. #include "api_int.h"
  3. // decoding tables for dynamic blocks
  4. #define LITERAL_TABLE_BITS 9
  5. #define LITERAL_TABLE_MASK ((1 << LITERAL_TABLE_BITS)-1)
  6. #define DISTANCE_TABLE_BITS 7
  7. #define DISTANCE_TABLE_MASK ((1 << DISTANCE_TABLE_BITS)-1)
  8. #define PRETREE_TABLE_BITS 7
  9. #define PRETREE_TABLE_MASK ((1 << PRETREE_TABLE_BITS)-1)
  10. // decoding tables for static blocks
  11. #define STATIC_BLOCK_LITERAL_TABLE_BITS 9
  12. #define STATIC_BLOCK_LITERAL_TABLE_MASK ((1 << STATIC_BLOCK_LITERAL_TABLE_BITS)-1)
  13. #define STATIC_BLOCK_LITERAL_TABLE_SIZE (1 << STATIC_BLOCK_LITERAL_TABLE_BITS)
  14. #define STATIC_BLOCK_DISTANCE_TABLE_BITS 5
  15. #define STATIC_BLOCK_DISTANCE_TABLE_MASK ((1 << STATIC_BLOCK_DISTANCE_TABLE_BITS)-1)
  16. #define STATIC_BLOCK_DISTANCE_TABLE_SIZE (1 << STATIC_BLOCK_DISTANCE_TABLE_BITS)
  17. //
  18. // Various possible states
  19. //
  20. typedef enum
  21. {
  22. STATE_READING_GZIP_HEADER, // Only applies to GZIP
  23. STATE_READING_BFINAL_NEED_TO_INIT_BITBUF, // Start of block, need to init bit buffer
  24. STATE_READING_BFINAL, // About to read bfinal bit
  25. STATE_READING_BTYPE, // About to read btype bits
  26. STATE_READING_NUM_LIT_CODES, // About to read # literal codes
  27. STATE_READING_NUM_DIST_CODES, // About to read # dist codes
  28. STATE_READING_NUM_CODE_LENGTH_CODES,// About to read # code length codes
  29. STATE_READING_CODE_LENGTH_CODES, // In the middle of reading the code length codes
  30. STATE_READING_TREE_CODES_BEFORE, // In the middle of reading tree codes (loop top)
  31. STATE_READING_TREE_CODES_AFTER, // In the middle of reading tree codes (extension; code > 15)
  32. STATE_DECODE_TOP, // About to decode a literal (char/match) in a compressed block
  33. STATE_HAVE_INITIAL_LENGTH, // Decoding a match, have the literal code (base length)
  34. STATE_HAVE_FULL_LENGTH, // Ditto, now have the full match length (incl. extra length bits)
  35. STATE_HAVE_DIST_CODE, // Ditto, now have the distance code also, need extra dist bits
  36. STATE_INTERRUPTED_MATCH, // In the middle of a match, but output buffer filled up
  37. /* uncompressed blocks */
  38. STATE_UNCOMPRESSED_ALIGNING,
  39. STATE_UNCOMPRESSED_1,
  40. STATE_UNCOMPRESSED_2,
  41. STATE_UNCOMPRESSED_3,
  42. STATE_UNCOMPRESSED_4,
  43. STATE_DECODING_UNCOMPRESSED,
  44. // These three apply only to GZIP
  45. STATE_START_READING_GZIP_FOOTER, // (Initialisation for reading footer)
  46. STATE_READING_GZIP_FOOTER,
  47. STATE_VERIFYING_GZIP_FOOTER,
  48. STATE_DONE // Finished
  49. } t_decoder_state;
  50. typedef struct
  51. {
  52. byte window[WINDOW_SIZE];
  53. // output buffer
  54. byte * output_curpos; // current output pos
  55. byte * end_output_buffer; // ptr to end of output buffer
  56. byte * output_buffer; // ptr to start of output buffer
  57. // input buffer
  58. const byte * input_curpos; // current input pos
  59. const byte * end_input_buffer; // ptr to end of input buffer
  60. int num_literal_codes;
  61. int num_dist_codes;
  62. int num_code_length_codes;
  63. int temp_code_array_size;
  64. byte temp_code_list[MAX_LITERAL_TREE_ELEMENTS + MAX_DIST_TREE_ELEMENTS];
  65. // is this the last block?
  66. int bfinal;
  67. // type of current block
  68. int btype;
  69. // state information
  70. t_decoder_state state;
  71. long state_loop_counter;
  72. byte state_code;
  73. BOOL using_gzip;
  74. // gzip-specific stuff
  75. byte gzip_header_substate;
  76. byte gzip_header_flag;
  77. byte gzip_header_xlen1_byte; // first byte of XLEN
  78. unsigned int gzip_header_xlen; // xlen (0...65535)
  79. unsigned int gzip_header_loop_counter;
  80. byte gzip_footer_substate;
  81. unsigned int gzip_footer_loop_counter;
  82. unsigned long gzip_footer_crc32; // what we're supposed to end up with
  83. unsigned long gzip_footer_output_stream_size; // what we're supposed to end up with
  84. unsigned long gzip_crc32; // running counter
  85. unsigned long gzip_output_stream_size; // running counter
  86. // end of gzip-specific stuff
  87. int length;
  88. int dist_code;
  89. long offset;
  90. // bit buffer and # bits available in buffer
  91. unsigned long bitbuf;
  92. int bitcount;
  93. // position in the window
  94. long bufpos;
  95. // for decoding the uncompressed block header
  96. byte unc_buffer[4];
  97. // bit lengths of tree codes
  98. byte literal_tree_code_length[MAX_LITERAL_TREE_ELEMENTS];
  99. byte distance_tree_code_length[MAX_DIST_TREE_ELEMENTS];
  100. byte pretree_code_length[NUM_PRETREE_ELEMENTS];
  101. // tree decoding tables
  102. short distance_table[1 << DISTANCE_TABLE_BITS];
  103. short literal_table[1 << LITERAL_TABLE_BITS];
  104. short literal_left[MAX_LITERAL_TREE_ELEMENTS*2];
  105. short literal_right[MAX_LITERAL_TREE_ELEMENTS*2];
  106. short distance_left[MAX_DIST_TREE_ELEMENTS*2];
  107. short distance_right[MAX_DIST_TREE_ELEMENTS*2];
  108. short pretree_table[1 << PRETREE_TABLE_BITS];
  109. short pretree_left[NUM_PRETREE_ELEMENTS*2];
  110. short pretree_right[NUM_PRETREE_ELEMENTS*2];
  111. } t_decoder_context;
  112. #include "infproto.h"
  113. #include "infdata.h"
  114. #include "comndata.h"