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.

377 lines
9.8 KiB

  1. //
  2. // General Data Compression
  3. //
  4. #ifndef _H_GDC
  5. #define _H_GDC
  6. //
  7. //
  8. // CONSTANTS
  9. //
  10. //
  11. //
  12. // Scratch buffer mutex
  13. //
  14. #define GDC_MUTEX_NAME "GDCMutex"
  15. //
  16. // Compression Types (bit flags)
  17. //
  18. #define GCT_NOCOMPRESSION 0x0000
  19. #define GCT_PKZIP 0x0001
  20. #define GCT_PERSIST_PKZIP 0x0002
  21. #define GCT_DEFAULT (GCT_PKZIP | GCT_PERSIST_PKZIP)
  22. //
  23. // Compression Options for GCT_PKZIP
  24. //
  25. #define GDCCO_MAXSPEED 0
  26. #define GDCCO_MAXCOMPRESSION 1
  27. //
  28. // Data sizes used to determine the saved dictionary space in our work
  29. // buffer.
  30. //
  31. #define GDC_DATA_SMALL 1024
  32. #define GDC_DATA_MEDIUM 2048
  33. #define GDC_DATA_MAX 4096
  34. //
  35. // Persistent Dictionaries used for compression/decompression
  36. //
  37. enum
  38. {
  39. GDC_DICT_UPDATES = 0,
  40. GDC_DICT_MISC,
  41. GDC_DICT_INPUT,
  42. GDC_DICT_COUNT
  43. };
  44. typedef struct tagGDC_DICTIONARY
  45. {
  46. UINT cbUsed; // Amount of saved data
  47. BYTE pData[GDC_DATA_MAX]; // Saved uncompressed data
  48. } GDC_DICTIONARY;
  49. typedef GDC_DICTIONARY * PGDC_DICTIONARY;
  50. //
  51. // Byte runs that can be replaced with smaller bit sequences
  52. //
  53. #define GDC_MINREP 2
  54. #define GDC_MAXREP (GDC_MINREP+(8*1)+2+4+8+16+32+64+128+256-4)
  55. // GDC_MAXREP is 516, 129*4
  56. //
  57. // Holds uncompressed data for both compression/decompression
  58. //
  59. #define GDC_UNCOMPRESSED (GDC_MAXREP + 2*GDC_DATA_MAX)
  60. //
  61. // We don't need to double-buffer compressed data--we just read it out
  62. // of the caller's source or write it into the caller's dest directly.
  63. //
  64. // NOTE: With real PKZIP, which mostly reads from/writes to files,
  65. // they don't have memory pointers containing raw data already. That's
  66. // whe original code we got used Read/Write routine callbacks. This is
  67. // no longer necessary.
  68. //
  69. //
  70. // Random, little understood PKZIP table values, codes
  71. //
  72. #define KMP_THRESHOLD 10
  73. #define GDC_LIT_SIZE (256 + GDC_MAXREP + 2)
  74. // GDC_LIT_SIZE is 774
  75. // EOF is last index of Lit array
  76. #define EOF_CODE (GDC_LIT_SIZE-1)
  77. #define ABORT_CODE (EOF_CODE+1)
  78. //
  79. // EXT_DIST_BITS is the # of bits needed to store an index into a GDC_DIST_SIZE
  80. // array. That's defined to be 64, which is 2^6, hence 6 bits. Smaller
  81. // dictionary compressions use fewer bits and hence not all of the DIST
  82. // items. The mask
  83. // is used to pull the 6-bit sized index out of a byte.
  84. //
  85. #define GDC_DIST_SIZE 64
  86. #define EXT_DIST_BITS_MIN 4
  87. #define EXT_DIST_BITS_MEDIUM 5
  88. #define EXT_DIST_BITS_MAC 6
  89. #define GDC_LEN_SIZE 16
  90. #define GDC_DECODED_SIZE 256
  91. //
  92. // The hash function has 4*256+5*256 different values, which means
  93. // we need that many entries in our hash array.
  94. //
  95. #define GDC_HASHFN(x) (4*(x)[0] + 5*(x)[1])
  96. #define GDC_HASH_SIZE (4*256 + 5*256)
  97. //
  98. // Structure: GDC_IMPLODE
  99. //
  100. // Workspace for compressing our data. We have simplified and shrunk this
  101. // structure a fair amount, by having constant code/bit tables and not
  102. // double-buffering the compressed result. PKZIP's implode calculates the
  103. // LitBits & LitCodes every time through (rather than storing 2 774 byte
  104. // arrays in data--which would be a pain to declare anyway!), and makes a
  105. // private copy of the DistBits & DistCodes.
  106. //
  107. typedef struct tagGDC_IMPLODE
  108. {
  109. //
  110. // NO SOURCE INFO--we copy source chunks and maybe dictionary into
  111. // RawData. Then at the end we copy RawData back into the dictionary
  112. // if there is one.
  113. //
  114. //
  115. // Destination info
  116. //
  117. LPBYTE pDst; // Current Dest ptr (advances as we write)
  118. UINT cbDst; // Amount of Dest left (shrinks as we write)
  119. UINT iDstBit; // Current bit pos in Current Dest ptr byte
  120. //
  121. // Compression info
  122. //
  123. UINT cbDictSize;
  124. UINT cbDictUsed;
  125. UINT ExtDistBits;
  126. UINT ExtDistMask;
  127. //
  128. // Working info
  129. //
  130. UINT Distance;
  131. UINT ibRawData;
  132. // NOTE: GDC_UNCOMPRESSED is a multiple of 4
  133. BYTE RawData[GDC_UNCOMPRESSED];
  134. // NOTE: This is DWORD aligned (GDC_MAXREP is a multiple of 4
  135. // and the extra 2 WORDS == 1 DWORD
  136. short Next[2 + GDC_MAXREP];
  137. // NOTE: GDC_UNCOMPRESED is a multiple of 4
  138. WORD SortArray[GDC_UNCOMPRESSED];
  139. // NOTE: This is DWORD aligned since GDC_HASH_SIZE is a multiple of 4
  140. WORD HashArray[GDC_HASH_SIZE];
  141. } GDC_IMPLODE, * PGDC_IMPLODE;
  142. //
  143. // GDC_EXPLODE
  144. // Workspace for uncompressing our data. We have vastly simplified and
  145. // shrunk this structure as per the comments for GDC_IMPLODE.
  146. //
  147. typedef struct tagGDC_EXPLODE
  148. {
  149. //
  150. // Source info
  151. //
  152. LPBYTE pSrc; // Current Src ptr (advances as we read)
  153. UINT cbSrc; // Amount of Src left (shrinks as we read)
  154. UINT SrcByte; // Look ahead byte in source
  155. UINT SrcBits; // Remainded src bits
  156. //
  157. // NO DEST INFO--we copy maybe dictionary into RawData at the beginning.
  158. // Then at the end we maybe copy RawData back into the dictionary.
  159. //
  160. //
  161. // Compression info
  162. //
  163. UINT ExtDistBits;
  164. UINT ExtDistMask;
  165. UINT cbDictUsed;
  166. UINT Distance;
  167. UINT iRawData; // Current index into RawData
  168. BYTE RawData[GDC_UNCOMPRESSED];
  169. } GDC_EXPLODE, *PGDC_EXPLODE;
  170. #define GDC_WORKBUF_SIZE max(sizeof(GDC_IMPLODE), sizeof(GDC_EXPLODE))
  171. //
  172. // EXTERNAL FUNCTIONS
  173. //
  174. //
  175. // API FUNCTION: GDC_Init()
  176. //
  177. // DESCRIPTION:
  178. //
  179. // Initialises the General Data Compressor.
  180. // Must be called before any other GDC functions.
  181. //
  182. // PARAMETERS:
  183. //
  184. //
  185. //
  186. void GDC_Init(void);
  187. //
  188. // FUNCTION: GDC_Compress(..)
  189. //
  190. // DESCRIPTION:
  191. //
  192. // Compresses source data into a destination buffer.
  193. //
  194. //
  195. // PARAMETERS:
  196. //
  197. // pDictionary - NULL if old PKZIP, valid ptr if persistent
  198. //
  199. // Options - specifies whether speed of compression or
  200. // size of the compressed data is the most important factor. This
  201. // basically affects the amount of previous data saved for looking
  202. // backwards. MAXSPEED means smaller dictionary. MAXCOMPRESSION
  203. // means a bigger one. The dictionary size is basically the amount
  204. // of overlap in the source data used when calculating the hash
  205. // index.
  206. //
  207. // GDCCO_MAXSPEED - compress the data as quickly as possible, at
  208. // the expense of increased compressed data size
  209. //
  210. // GDCCO_MAXCOMPRESSION - compress the data as much as possible, at the
  211. // expense of increased compression time.
  212. // With a persistent dictionary, only GDCCO_MAXCOMPRESSION is meaningful.
  213. //
  214. // pSrc - pointer to the source (uncompressed) data.
  215. //
  216. // cbSrcSize - the number of bytes of source.
  217. //
  218. // pDst - pointer to the destination, where the
  219. // compressed result will go.
  220. //
  221. // pcbDstSize - pointer to the maximum amount the destina-
  222. // tion can hold. If the compressed result ends
  223. // up being bigger than this amount, we bail
  224. // out and don't compress the source at all.
  225. // Otherwise the resulting size is written back.
  226. //
  227. // RETURNS:
  228. //
  229. // TRUE if success, FALSE if failure.
  230. //
  231. //
  232. BOOL GDC_Compress
  233. (
  234. PGDC_DICTIONARY pDictionary,
  235. UINT Options,
  236. LPBYTE pWorkBuf,
  237. LPBYTE pSrc,
  238. UINT cbSrcSize,
  239. LPBYTE pDst,
  240. UINT * pcbDstSize
  241. );
  242. //
  243. // API FUNCTION: GDC_Decompress(..)
  244. //
  245. // DESCRIPTION:
  246. //
  247. // Decompresses source data into a destination buffer.
  248. //
  249. //
  250. // PARAMETERS:
  251. //
  252. // pDictionary - NULL if old PKZIP, ptr to saved data if
  253. // persistent.
  254. //
  255. // pSrc - pointer to the source (compressed) data.
  256. //
  257. // cbSrcSize - the number of bytes of source.
  258. //
  259. // pDst - pointer to the destination, where the
  260. // uncompressed result will go.
  261. //
  262. // pcbDstSize - pointer to the maximum amount the desina-
  263. // tion can hold. If the uncompressed result
  264. // ends up being bigger than this amount, we
  265. // bail out since we can't decompress it.
  266. // Otherwise the resulting size is written back.
  267. //
  268. // RETURNS:
  269. //
  270. // TRUE if success, FALSE if failure.
  271. //
  272. BOOL GDC_Decompress
  273. (
  274. PGDC_DICTIONARY pDictionary,
  275. LPBYTE pWorkBuf,
  276. LPBYTE pSrc,
  277. UINT cbSrcSize,
  278. LPBYTE pDst,
  279. UINT * pcbDstSize
  280. );
  281. //
  282. // INTERNAL FUNCTIONS
  283. //
  284. void GDCCalcDecode(const BYTE * pBits, const BYTE * pCodes, UINT size, LPBYTE pDecode);
  285. LPBYTE GDCGetWorkBuf(void);
  286. void GDCReleaseWorkBuf(LPBYTE);
  287. UINT GDCFindRep(PGDC_IMPLODE pgdcImp, LPBYTE Start);
  288. void GDCSortBuffer(PGDC_IMPLODE pgdcImp, LPBYTE low, LPBYTE hi);
  289. BOOL GDCOutputBits(PGDC_IMPLODE pgdcImp, WORD Cnt, WORD Code);
  290. UINT GDCDecodeLit(PGDC_EXPLODE);
  291. UINT GDCDecodeDist(PGDC_EXPLODE pgdcExp, UINT Len);
  292. BOOL GDCWasteBits(PGDC_EXPLODE pgdcExp, UINT Bits);
  293. #endif // _H_GDC