Source code of Windows XP (NT5)
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.

302 lines
9.8 KiB

  1. /* DEC/CMS REPLACEMENT HISTORY, Element CompPirv.H */
  2. /* *1 14-NOV-1996 10:26:00 ANIGBOGU "[113914]Private declarations for all compression/decompression functions" */
  3. /* DEC/CMS REPLACEMENT HISTORY, Element CompPirv.H */
  4. /* PUBLIC FILE
  5. ******************************************************************************
  6. **
  7. ** (c) Copyright Schlumberger Technology Corp., unpublished work, created 1996.
  8. **
  9. ** This computer program includes Confidential, Proprietary Information and is
  10. ** a Trade Secret of Schlumberger Technology Corp. All use, disclosure, and/or
  11. ** reproduction is prohibited unless authorized in writing by Schlumberger.
  12. ** All Rights Reserved.
  13. **
  14. ******************************************************************************
  15. **
  16. ** compress/CompPirv.h
  17. **
  18. ** PURPOSE
  19. **
  20. ** Common declarations for all compression/decompression modules
  21. **
  22. ** SPECIAL REQUIREMENTS & NOTES
  23. **
  24. ** AUTHOR
  25. **
  26. ** J. C. Anigbogu
  27. ** Austin Systems Center
  28. ** Nov 1996
  29. **
  30. ******************************************************************************
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "CompPub.h"
  35. #include "slbCrc32.h"
  36. #if defined(VMS)
  37. #define OS_CODE 0x02
  38. #endif
  39. #if defined(Unix)
  40. #define OS_CODE 0x03
  41. #endif
  42. /* Common defaults */
  43. #ifndef OS_CODE
  44. #define OS_CODE 0x0b /* assume WindowsNT or Windows95*/
  45. #endif
  46. #include <string.h>
  47. #define memzero(String, Size) memset ((void *)(String), 0, (Size))
  48. #define STORED 0
  49. /* methods 1 to 7 reserved */
  50. #define DEFLATED 8
  51. #define MAX_METHODS 9
  52. /*
  53. * For compression, input is done in window[]. For decompression, output
  54. * is done in window.
  55. */
  56. #ifndef INBUFSIZ
  57. #define INBUFSIZ 0x8000 /* input buffer size */
  58. #endif
  59. #ifndef OUTBUFSIZ
  60. #define OUTBUFSIZ 16384 /* output buffer size */
  61. #endif
  62. #ifndef DISTBUFSIZE
  63. #define DISTBUFSIZE 0x8000 /* buffer for distances, see Trees.c */
  64. #endif
  65. #ifndef WSIZE
  66. #define WSIZE 0x8000 /* window size--must be a power of two, and */
  67. #endif /* at least 32K for zip's deflate method */
  68. #ifndef DWSIZE
  69. #define DWSIZE 0x10000 /* 2L * WSIZE */
  70. #endif
  71. #ifndef BITS
  72. #define BITS 16
  73. #endif
  74. typedef struct CompData
  75. {
  76. unsigned char *Data;
  77. int Size;
  78. struct CompData *next;
  79. } CompData_t;
  80. typedef struct CompParam
  81. {
  82. unsigned long InputSize; /* valid bytes in inbuf */
  83. unsigned long Index; /* index of next byte to be processed in inbuf */
  84. unsigned long OutBytes; /* bytes in output buffer */
  85. unsigned long GlobalSize; /* bytes in inbut g_inbuf */
  86. unsigned long BytesIn; /* number of input bytes */
  87. unsigned long BytesOut; /* number of output bytes */
  88. unsigned long HeaderBytes; /* number of bytes in gzip header */
  89. unsigned long WindowSize; /* window size, 2*WSIZE */
  90. Crc32 *pCRC; /* Cyclic Redundancy Check */
  91. unsigned long BitBuffer; /* Global bit buffer */
  92. unsigned int BitsInBitBuffer; /* bits in global bit buffer */
  93. unsigned short DistBuffer[DISTBUFSIZE]; /* buffer for distances, see trees.c */
  94. unsigned short HashLink[1L<<BITS]; /* hash link (see deflate.c) */
  95. unsigned char InputBuffer[INBUFSIZ]; /* input buffer */
  96. unsigned char *Input; /* pointer to input buffer */
  97. unsigned char Output[OUTBUFSIZ]; /* output buffer */
  98. unsigned char Window[DWSIZE]; /* Sliding window */
  99. unsigned char *GlobalInput; /* global input buffer */
  100. CompData_t *CompressedOutput; /* compressed output list */
  101. CompData_t *PtrOutput; /* pointer to compressed output list */
  102. } CompParam_t;
  103. typedef struct DeflateParam
  104. {
  105. long BlockStart;
  106. unsigned int PrevLength;
  107. unsigned int StringStart; /* start of string to insert */
  108. unsigned int MatchStart; /* start of matching string */
  109. unsigned int MaxChainLength;
  110. unsigned int MaxLazyMatch;
  111. unsigned int GoodMatch;
  112. int NiceMatch; /* Stop searching when current match exceeds this */
  113. } DeflateParam_t;
  114. /* long BlockStart:
  115. * window position at the beginning of the current output block. Gets
  116. * negative when the window is moved backwards.
  117. */
  118. /* PrevLength:
  119. * Length of the best match at previous step. Matches not greater than this
  120. * are discarded. This is used in the lazy match evaluation.
  121. */
  122. /* MaxChainLength:
  123. * To speed up deflation, hash chains are never searched beyond this length.
  124. * A higher limit improves compression ratio but degrades the speed.
  125. */
  126. /* MaxLazyMatch:
  127. * Attempt to find a better match only when the current match is strictly
  128. * smaller than this value. This mechanism is used only for compression
  129. * levels >= 4.
  130. */
  131. /* GoodMatch: */
  132. /* Use a faster search when the previous match is longer than this */
  133. /* Values for MaxLazyMatch, GoodMatch and MaxChainLength, depending on
  134. * the desired pack level (0..9). The values given below have been tuned to
  135. * exclude worst case performance for pathological data. Better values may be
  136. * found for specific data.
  137. */
  138. /* ===========================================================================
  139. * Data used by the "bit string" routines (Bits.c) and zip (Zip.c)
  140. */
  141. typedef struct LocalBits
  142. {
  143. unsigned int BitBuffer;
  144. unsigned int ValidBits;
  145. } LocalBits_t;
  146. /* BitBuffer: Output buffer. bits are inserted starting at the bottom
  147. * (least significant bits).
  148. */
  149. /* ValidBits: Number of valid bits in BitBuffer. All bits above the last valid bit
  150. * are always zero.
  151. */
  152. typedef struct LocalDef
  153. {
  154. unsigned int HashIndex; /* hash index of string to be inserted */
  155. int EndOfInput; /* flag set at end of input buffer */
  156. unsigned int Lookahead; /* number of valid bytes ahead in window */
  157. int CompLevel; /* compression level (1..9) */
  158. } LocalDef_t;
  159. /* for compatibility with old zip sources (to be cleaned) */
  160. #define GZIP_MAGIC "\037\213" /* Magic header for gzip format, 1F 8B */
  161. #define MIN_MATCH 3
  162. #define MAX_MATCH 258
  163. /* The minimum and maximum match lengths */
  164. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  165. /* Minimum amount of lookahead, except at the end of the input file.
  166. * See Deflate.c for comments about the MIN_MATCH+1.
  167. */
  168. #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
  169. /* In order to simplify the code, particularly on 16 bit machines, match
  170. * distances are limited to MAX_DIST instead of WSIZE.
  171. */
  172. #define STORED_BLOCK 0
  173. #define STATIC_TREES 1
  174. #define DYN_TREES 2
  175. /* The three kinds of block type */
  176. #define GetByte(c) \
  177. (c->Index < c->InputSize ? c->Input[c->Index++] : FillInputBuffer(0,c))
  178. #define TryByte(c) \
  179. (c->Index < c->InputSize ? c->Input[c->Index++] : FillInputBuffer(1,c))
  180. /* PutByte is used for the compressed output
  181. */
  182. #define PutByte(c,o) {o->Output[o->OutBytes++]=(unsigned char)(c); \
  183. if (o->OutBytes==OUTBUFSIZ) \
  184. FlushOutputBuffer(o);}
  185. /* Output a 16 bit value, lsb first */
  186. #define PutShort(w,c) \
  187. { if (c->OutBytes < OUTBUFSIZ-2) { \
  188. c->Output[c->OutBytes++] = (unsigned char) ((w) & 0xff); \
  189. c->Output[c->OutBytes++] = (unsigned char) ((unsigned short)(w) >> 8); \
  190. } else { \
  191. PutByte((unsigned char)((w) & 0xff),c); \
  192. PutByte((unsigned char)((unsigned short)(w) >> 8),c); \
  193. } \
  194. }
  195. /* Output a 32 bit value to the bit stream, LSB first */
  196. #define PutLong(n,c) { \
  197. PutShort(((n) & 0xffff),c); \
  198. PutShort(((unsigned long)(n)) >> 16,c); \
  199. }
  200. /* Macros for getting two-byte and four-byte header values */
  201. #define SH(p) ((unsigned short)(unsigned char)((p)[0]) | ((unsigned short)(unsigned char)((p)[1]) << 8))
  202. #define LG(p) ((unsigned long)(SH(p)) | ((unsigned long)(SH((p)+2)) << 16))
  203. /* Diagnostic functions */
  204. #ifdef DEBUG
  205. # define Assert(cond,msg) {if(!(cond)) error(msg);}
  206. #else
  207. # define Assert(cond,msg)
  208. #endif
  209. #ifndef MAX
  210. #define MAX(a,b) (((a)>(b))?(a):(b))
  211. #endif
  212. #ifndef MIN
  213. #define MIN(a,b) (((a)<(b))?(a):(b))
  214. #endif
  215. /* in Zip.c: */
  216. extern CompressStatus_t Zip(int Level, CompParam_t *Comp);
  217. extern int FillBuffer(unsigned char *Buffer, unsigned int Size, CompParam_t *Comp);
  218. int ReadBuffer(char *Buffer, unsigned int Size, CompParam_t *Comp);
  219. /* in Unzip.c */
  220. extern CompressStatus_t Unzip(int Method, CompParam_t *Comp);
  221. /* in Deflate.c */
  222. CompressStatus_t InitLongestMatch(int PackLevel, unsigned short
  223. *Flags, DeflateParam_t *Defl,
  224. LocalDef_t *Deflt, CompParam_t *Comp);
  225. unsigned long Deflate(int Level, LocalBits_t *Bits, DeflateParam_t *Defl,
  226. LocalDef_t *Deflt, CompParam_t *Comp);
  227. /* in Trees.c */
  228. void InitMatchBuffer(void);
  229. int TallyFrequencies(int Dist, int LengthC, int Level,
  230. DeflateParam_t *Defl, CompParam_t *Comp);
  231. unsigned long FlushBlock(char *Buffer, unsigned long stored_len, int Eof,
  232. LocalBits_t *Bits, CompParam_t *Comp);
  233. /* in Bits.c */
  234. void InitializeBits(LocalBits_t *Bits);
  235. void SendBits(int Value, int Length, LocalBits_t *Bits,
  236. CompParam_t *Comp);
  237. unsigned int ReverseBits(unsigned int Value, int Length);
  238. void WindupBits(LocalBits_t *Bits, CompParam_t *Comp);
  239. void CopyBlock(char *Input, unsigned int Length, int Header,
  240. LocalBits_t *Bits, CompParam_t *Comp);
  241. /* in Util.c: */
  242. extern unsigned long UpdateCRC(CompParam_t *Comp, unsigned char *Input,
  243. unsigned int Size);
  244. extern void ClearBuffers(CompParam_t *Comp);
  245. extern int FillInputBuffer(int EOF_OK, CompParam_t *Comp);
  246. extern CompressStatus_t FlushOutputBuffer(CompParam_t *Comp);
  247. extern CompressStatus_t FlushWindow(CompParam_t *Comp);
  248. extern CompressStatus_t WriteBuffer(CompParam_t *Comp, void *Buffer,
  249. unsigned int Count);
  250. /* in Inflate.c */
  251. extern CompressStatus_t Inflate(CompParam_t *Comp);