Inflater.h
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:8k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. // Form zlib 1.1.3, Copyright (C) 1995-1998 Jean-loup Gailly
  2. // Copyright (C) 1999 DXGuide.  All Rights Reserved.
  3. // File: Inflater.h
  4. #ifndef _INFLATER__H
  5. #define _INFLATER__H
  6. #if _MSC_VER >= 1000
  7. #pragma once
  8. #endif // _MSC_VER >= 1000
  9. //#define PKZIP_BUG_WORKAROUND
  10. //#define BUILDFIXED
  11. //#define SLOW
  12. #include "Zlib113.h"
  13. #include "Checksum.h"
  14. // Huffman code lookup table entry
  15. struct InflateHuft
  16. {
  17. BYTE m_byteExop; // number of extra bits or operation
  18. BYTE m_byteBits; // number of bits in this code or subcode
  19. UINT m_uBase; // literal, length base, distance base, or table offset
  20. };
  21. // simplify the use of the InflateHuft type with some defines
  22. enum InflateCodesMode
  23. {
  24. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  25. INFLATECODESMODE_START, // x: set up for LEN
  26. INFLATECODESMODE_LEN, // i: get length/literal/eob next
  27. INFLATECODESMODE_LENEXT, // i: getting length extra (have base)
  28. INFLATECODESMODE_DIST, // i: get distance next
  29. INFLATECODESMODE_DISTEXT, // i: getting distance extra
  30. INFLATECODESMODE_COPY, // o: copying bytes in window, waiting for space
  31. INFLATECODESMODE_LIT, // o: got literal, waiting for output space
  32. INFLATECODESMODE_WASH, // o: got eob, possibly still output waiting
  33. INFLATECODESMODE_END, // x: got eob and all data flushed
  34. INFLATECODESMODE_BADCODE // x: got error
  35. };
  36. // inflate codes private state
  37. struct CInflateCodesState
  38. {
  39. public:
  40. InflateCodesMode m_inflateCodesMode; // current inflate_codes mode
  41. // mode dependent information
  42. UINT m_uLen;
  43. union
  44. {
  45. struct
  46. {
  47. InflateHuft* m_pHufTree; // pointer into tree
  48. UINT m_uNeedBits; // bits needed
  49. } code; // if LEN or DIST, where in tree
  50. UINT m_uLit; // if LIT, literal
  51. struct
  52. {
  53. UINT m_uGet; // bits to get for extra
  54. UINT m_uDist; // distance back to copy from
  55. } copy; // if EXT or COPY, where and how much
  56. }; // submode
  57. // mode independent information
  58. BYTE m_LBits; // ltree bits decoded per branch
  59. BYTE m_dBits; // dtree bits decoder per branch
  60. InflateHuft* m_pLTree; // literal/length/eob tree
  61. InflateHuft* m_pDTree; // distance tree
  62. };
  63. class CInflater
  64. {
  65. protected:
  66. enum InflateMode
  67. {
  68. INFLATEMODE_METHOD, // waiting for method byte
  69. INFLATEMODE_FLAG, // waiting for flag byte
  70. INFLATEMODE_DICT4, // four dictionary check bytes to go
  71. INFLATEMODE_DICT3, // three dictionary check bytes to go
  72. INFLATEMODE_DICT2, // two dictionary check bytes to go
  73. INFLATEMODE_DICT1, // one dictionary check byte to go
  74. INFLATEMODE_DICT0, // waiting for InflateSetDictionary
  75. INFLATEMODE_BLOCKS, // decompressing blocks
  76. INFLATEMODE_CHECK4, // four check bytes to go
  77. INFLATEMODE_CHECK3, // three check bytes to go
  78. INFLATEMODE_CHECK2, // two check bytes to go
  79. INFLATEMODE_CHECK1, // one check byte to go
  80. INFLATEMODE_DONE, // finished check, done
  81. INFLATEMODE_BAD // got an error--stay here
  82. };  
  83. public:
  84. CInflater(void);
  85. ~CInflater();
  86. public:
  87. ZRetVal Inflate(FlushValues  flush);
  88. ZRetVal InflateEnd(void);
  89. // Advanced functions, The following functions are needed only in some special applications.
  90. public:
  91. ZRetVal Init(int  nWindowBits = DEF_WBITS);
  92. ZRetVal InflateSetDictionary(
  93. const BYTE*  pDictionary, UINT  uDictLength);
  94. ZRetVal InflateSync(void);
  95. ZRetVal InflateReset(void);
  96. bool InflateSyncPoint(void);
  97. // infblock
  98. protected:
  99. bool inflate_blocks_new(UINT  w); // window size
  100. void inflate_blocks_reset(void); // check value on output
  101. void inflate_set_dictionary(
  102. const BYTE*  pDictionary, // dictionary
  103. UINT  uDictLength); // dictionary length
  104. ZRetVal inflate_blocks(ZRetVal  r); // initial return code
  105. ZRetVal inflate_flush(ZRetVal  r);
  106. // inftrees
  107. protected:
  108. ZRetVal inflate_trees_bits(
  109. UINT*, // 19 code lengths
  110. UINT*, // bits tree desired/actual depth
  111. InflateHuft**, // bits tree result
  112. InflateHuft*); // space for trees
  113. ZRetVal inflate_trees_dynamic(
  114. UINT, // number of literal/length codes
  115. UINT, // number of distance codes
  116. UINT*, // that many (total) code lengths
  117. UINT*, // literal desired/actual bit depth
  118. UINT*, // distance desired/actual bit depth
  119. InflateHuft**, // literal/length tree result
  120. InflateHuft**, // distance tree result
  121. InflateHuft*); // space for trees
  122. ZRetVal inflate_trees_fixed(
  123. UINT*, // literal desired/actual bit depth
  124. UINT*, // distance desired/actual bit depth
  125. InflateHuft**, // literal/length tree result
  126. InflateHuft**); // distance tree result
  127. // inflate codes
  128. protected:
  129. CInflateCodesState* inflate_codes_new(
  130. UINT, UINT,
  131. InflateHuft*, InflateHuft*);
  132. ZRetVal inflate_codes(ZRetVal);
  133. protected:
  134. ZRetVal inflate_fast(UINT, UINT, InflateHuft*,
  135. InflateHuft*);
  136. public:
  137. const BYTE* m_pSourceBuf; // next input byte
  138. UINT m_uSourceBufLen; // number of bytes available at m_pSourceBuf
  139. BYTE* m_pDestBuf; // next output byte should be put there
  140. UINT m_uDestBufLen; // remaining free space at m_pDestBuf
  141. public:
  142. DWORD m_dwTotalInputBytes; // total nb of input bytes read so far
  143. DWORD m_dwTotalOutputBytes; // total nb of bytes output so far
  144. protected:
  145. LPCTSTR m_lpszMsg; // last error message, NULL if no error
  146. CChecksum m_checksum; // adler32 value of the uncompressed data
  147. public:
  148. DWORD GetTotalIn(void) const;
  149. DWORD GetTotalOut(void) const;
  150. DWORD GetAdler32(void) const;
  151. void SetInput(const void*  pBuf, UINT  uLen);
  152. protected:
  153. InflateMode m_curInflateMode; // current inflate mode
  154. // mode dependent information
  155. union
  156. {
  157. UINT m_method; // if INFLATEMODE_METHOD, INFLATEMODE_FLAG, method byte
  158. struct
  159. {
  160. DWORD m_dwWas; // computed check value
  161. DWORD m_dwNeed; // stream check value
  162. } check; // if CHECK, check values to compare
  163. UINT m_marker; // if INFLATEMODE_BAD, InflateSync's marker bytes count
  164. }; // submode
  165. // mode independent information
  166. bool m_bNoWrap; // flag for no wrapper
  167. UINT m_uWindowBits; // log2(window size)  (8..15, defaults to 15)
  168. // blocks ! - inflate blocks semi-private state
  169. protected:
  170. enum InflateBlockMode
  171. {
  172. INFLATEBLOCKMODE_TYPE, // get type bits (3, including end bit)
  173. INFLATEBLOCKMODE_LENS, // get lengths for stored
  174. INFLATEBLOCKMODE_STORED, // processing stored block
  175. INFLATEBLOCKMODE_TABLE, // get table lengths
  176. INFLATEBLOCKMODE_BTREE, // get bit lengths tree for a dynamic block
  177. INFLATEBLOCKMODE_DTREE, // get length, distance trees for a dynamic block
  178. INFLATEBLOCKMODE_CODES, // processing fixed or dynamic block
  179. INFLATEBLOCKMODE_DRY, // output remaining window bytes
  180. INFLATEBLOCKMODE_DONE, // finished last block, done
  181. INFLATEBLOCKMODE_BAD // got a data error--stuck here
  182. };
  183. protected:
  184. InflateBlockMode m_blockMode; // current inflate_block mode
  185. // mode dependent information
  186. union
  187. {
  188. UINT m_uLeftBytes; // if INFLATEBLOCKMODE_LENS & INFLATEBLOCKMODE_STORED(STORED), bytes left to copy
  189. struct
  190. {
  191. UINT m_uTableLen; // table lengths (14 bits)
  192. UINT m_uIndex; // index into blens (or border)
  193. UINT* m_puBitLens; // bit lengths of codes
  194. UINT m_ubb; // bit length tree depth
  195. InflateHuft* m_ptb; // bit length decoding tree
  196. } trees; // if DTREE, decoding info for trees
  197. struct
  198. {
  199. CInflateCodesState* m_pInflateCodesState;
  200. }decode; // if CODES, current state
  201. }; // sub - submode
  202. bool m_bLast; // true if this block is the last block
  203. // mode independent information
  204. UINT m_uBitK; // bits in bit buffer
  205. DWORD m_dwBitBuf; // bit buffer
  206. InflateHuft* m_pHufts; // single malloc for tree space
  207. BYTE* m_pSlidingWindow; // sliding window
  208. BYTE* m_pEnd; // one byte after sliding window
  209. BYTE* m_pRead; // window read pointer
  210. BYTE* m_pWrite; // window write pointer
  211. };
  212. #include "Inflater.inl"
  213. #endif // _INFLATER__H