inflate.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef INFLATE_H
  2. #define INFLATE_H
  3. /* inflate.h -- internal inflate state definition
  4. * Copyright (C) 1995-2004 Mark Adler
  5. * For conditions of distribution and use, see copyright notice in zlib.h
  6. */
  7. /* WARNING: this file should *not* be used by applications. It is
  8. part of the implementation of the compression library and is
  9. subject to change. Applications should only use zlib.h.
  10. */
  11. #include "inftrees.h"
  12. /* Possible inflate modes between inflate() calls */
  13. typedef enum {
  14. HEAD, /* i: waiting for magic header */
  15. FLAGS, /* i: waiting for method and flags (gzip) */
  16. TIME, /* i: waiting for modification time (gzip) */
  17. OS, /* i: waiting for extra flags and operating system (gzip) */
  18. EXLEN, /* i: waiting for extra length (gzip) */
  19. EXTRA, /* i: waiting for extra bytes (gzip) */
  20. NAME, /* i: waiting for end of file name (gzip) */
  21. COMMENT, /* i: waiting for end of comment (gzip) */
  22. HCRC, /* i: waiting for header crc (gzip) */
  23. DICTID, /* i: waiting for dictionary check value */
  24. DICT, /* waiting for inflateSetDictionary() call */
  25. TYPE, /* i: waiting for type bits, including last-flag bit */
  26. TYPEDO, /* i: same, but skip check to exit inflate on new block */
  27. STORED, /* i: waiting for stored size (length and complement) */
  28. COPY, /* i/o: waiting for input or output to copy stored block */
  29. TABLE, /* i: waiting for dynamic block table lengths */
  30. LENLENS, /* i: waiting for code length code lengths */
  31. CODELENS, /* i: waiting for length/lit and distance code lengths */
  32. LEN, /* i: waiting for length/lit code */
  33. LENEXT, /* i: waiting for length extra bits */
  34. DIST, /* i: waiting for distance code */
  35. DISTEXT, /* i: waiting for distance extra bits */
  36. MATCH, /* o: waiting for output space to copy string */
  37. LIT, /* o: waiting for output space to write literal */
  38. CHECK, /* i: waiting for 32-bit check value */
  39. LENGTH, /* i: waiting for 32-bit length (gzip) */
  40. DONE, /* finished check, done -- remain here until reset */
  41. BAD, /* got a data error -- remain here until reset */
  42. MEM, /* got an inflate() memory error -- remain here until reset */
  43. SYNC /* looking for synchronization bytes to restart inflate() */
  44. } inflate_mode;
  45. /*
  46. State transitions between above modes -
  47. (most modes can go to the BAD or MEM mode -- not shown for clarity)
  48. Process header:
  49. HEAD -> (gzip) or (zlib)
  50. (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
  51. NAME -> COMMENT -> HCRC -> TYPE
  52. (zlib) -> DICTID or TYPE
  53. DICTID -> DICT -> TYPE
  54. Read deflate blocks:
  55. TYPE -> STORED or TABLE or LEN or CHECK
  56. STORED -> COPY -> TYPE
  57. TABLE -> LENLENS -> CODELENS -> LEN
  58. Read deflate codes:
  59. LEN -> LENEXT or LIT or TYPE
  60. LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
  61. LIT -> LEN
  62. Process trailer:
  63. CHECK -> LENGTH -> DONE
  64. */
  65. /* state maintained between inflate() calls. Approximately 7K bytes. */
  66. struct inflate_state {
  67. inflate_mode mode; /* current inflate mode */
  68. int last; /* true if processing last block */
  69. int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
  70. int havedict; /* true if dictionary provided */
  71. int flags; /* gzip header method and flags (0 if zlib) */
  72. unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
  73. unsigned long check; /* protected copy of check value */
  74. unsigned long total; /* protected copy of output count */
  75. /* gz_headerp head; */ /* where to save gzip header information */
  76. /* sliding window */
  77. unsigned wbits; /* log base 2 of requested window size */
  78. unsigned wsize; /* window size or zero if not using window */
  79. unsigned whave; /* valid bytes in the window */
  80. unsigned write; /* window write index */
  81. unsigned char *window; /* allocated sliding window, if needed */
  82. /* bit accumulator */
  83. unsigned long hold; /* input bit accumulator */
  84. unsigned bits; /* number of bits in "in" */
  85. /* for string and stored block copying */
  86. unsigned length; /* literal or length of data to copy */
  87. unsigned offset; /* distance back to copy string from */
  88. /* for table and code decoding */
  89. unsigned extra; /* extra bits needed */
  90. /* fixed and dynamic code tables */
  91. code const *lencode; /* starting table for length/literal codes */
  92. code const *distcode; /* starting table for distance codes */
  93. unsigned lenbits; /* index bits for lencode */
  94. unsigned distbits; /* index bits for distcode */
  95. /* dynamic table building */
  96. unsigned ncode; /* number of code length code lengths */
  97. unsigned nlen; /* number of length code lengths */
  98. unsigned ndist; /* number of distance code lengths */
  99. unsigned have; /* number of code lengths in lens[] */
  100. code *next; /* next available space in codes[] */
  101. unsigned short lens[320]; /* temporary storage for code lengths */
  102. unsigned short work[288]; /* work area for code table building */
  103. code codes[ENOUGH]; /* space for code tables */
  104. };
  105. /* Reverse the bytes in a 32-bit value */
  106. #define REVERSE(q) \
  107. ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
  108. (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
  109. #endif