inflate.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* inflate.h -- internal inflate state definition
  2. * Copyright (C) 1995-2019 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /* WARNING: this file should *not* be used by applications. It is
  6. part of the implementation of the compression library and is
  7. subject to change. Applications should only use zlib.h.
  8. */
  9. #ifndef INFLATE_H_
  10. #define INFLATE_H_
  11. #include "crc32.h"
  12. #ifdef S390_DFLTCC_INFLATE
  13. # include "arch/s390/dfltcc_common.h"
  14. # define HAVE_ARCH_INFLATE_STATE
  15. #endif
  16. /* define NO_GZIP when compiling if you want to disable gzip header and trailer decoding by inflate().
  17. NO_GZIP would be used to avoid linking in the crc code when it is not needed.
  18. For shared libraries, gzip decoding should be left enabled. */
  19. #ifndef NO_GZIP
  20. # define GUNZIP
  21. #endif
  22. /* Possible inflate modes between inflate() calls */
  23. typedef enum {
  24. HEAD = 16180, /* i: waiting for magic header */
  25. FLAGS, /* i: waiting for method and flags (gzip) */
  26. TIME, /* i: waiting for modification time (gzip) */
  27. OS, /* i: waiting for extra flags and operating system (gzip) */
  28. EXLEN, /* i: waiting for extra length (gzip) */
  29. EXTRA, /* i: waiting for extra bytes (gzip) */
  30. NAME, /* i: waiting for end of file name (gzip) */
  31. COMMENT, /* i: waiting for end of comment (gzip) */
  32. HCRC, /* i: waiting for header crc (gzip) */
  33. DICTID, /* i: waiting for dictionary check value */
  34. DICT, /* waiting for inflateSetDictionary() call */
  35. TYPE, /* i: waiting for type bits, including last-flag bit */
  36. TYPEDO, /* i: same, but skip check to exit inflate on new block */
  37. STORED, /* i: waiting for stored size (length and complement) */
  38. COPY_, /* i/o: same as COPY below, but only first time in */
  39. COPY, /* i/o: waiting for input or output to copy stored block */
  40. TABLE, /* i: waiting for dynamic block table lengths */
  41. LENLENS, /* i: waiting for code length code lengths */
  42. CODELENS, /* i: waiting for length/lit and distance code lengths */
  43. LEN_, /* i: same as LEN below, but only first time in */
  44. LEN, /* i: waiting for length/lit/eob code */
  45. LENEXT, /* i: waiting for length extra bits */
  46. DIST, /* i: waiting for distance code */
  47. DISTEXT, /* i: waiting for distance extra bits */
  48. MATCH, /* o: waiting for output space to copy string */
  49. LIT, /* o: waiting for output space to write literal */
  50. CHECK, /* i: waiting for 32-bit check value */
  51. LENGTH, /* i: waiting for 32-bit length (gzip) */
  52. DONE, /* finished check, done -- remain here until reset */
  53. BAD, /* got a data error -- remain here until reset */
  54. SYNC /* looking for synchronization bytes to restart inflate() */
  55. } inflate_mode;
  56. /*
  57. State transitions between above modes -
  58. (most modes can go to BAD on error -- not shown for clarity)
  59. Process header:
  60. HEAD -> (gzip) or (zlib) or (raw)
  61. (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
  62. HCRC -> TYPE
  63. (zlib) -> DICTID or TYPE
  64. DICTID -> DICT -> TYPE
  65. (raw) -> TYPEDO
  66. Read deflate blocks:
  67. TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
  68. STORED -> COPY_ -> COPY -> TYPE
  69. TABLE -> LENLENS -> CODELENS -> LEN_
  70. LEN_ -> LEN
  71. Read deflate codes in fixed or dynamic block:
  72. LEN -> LENEXT or LIT or TYPE
  73. LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
  74. LIT -> LEN
  75. Process trailer:
  76. CHECK -> LENGTH -> DONE
  77. */
  78. typedef struct inflate_state inflate_state;
  79. /* Struct for memory allocation handling */
  80. typedef struct inflate_allocs_s {
  81. char *buf_start;
  82. free_func zfree;
  83. inflate_state *state;
  84. unsigned char *window;
  85. } inflate_allocs;
  86. /* State maintained between inflate() calls -- approximately 7K bytes, not
  87. including the allocated sliding window, which is up to 32K bytes. */
  88. struct ALIGNED_(64) inflate_state {
  89. PREFIX3(stream) *strm; /* pointer back to this zlib stream */
  90. inflate_mode mode; /* current inflate mode */
  91. int last; /* true if processing last block */
  92. int wrap; /* bit 0 true for zlib, bit 1 true for gzip,
  93. bit 2 true to validate check value */
  94. int havedict; /* true if dictionary provided */
  95. int flags; /* gzip header method and flags, 0 if zlib, or
  96. -1 if raw or no header yet */
  97. unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
  98. unsigned long check; /* protected copy of check value */
  99. unsigned long total; /* protected copy of output count */
  100. PREFIX(gz_headerp) head; /* where to save gzip header information */
  101. /* sliding window */
  102. unsigned wbits; /* log base 2 of requested window size */
  103. uint32_t wsize; /* window size or zero if not using window */
  104. uint32_t whave; /* valid bytes in the window */
  105. uint32_t wnext; /* window write index */
  106. unsigned char *window; /* allocated sliding window, if needed */
  107. struct crc32_fold_s ALIGNED_(16) crc_fold;
  108. /* bit accumulator */
  109. uint32_t hold; /* input bit accumulator */
  110. unsigned bits; /* number of bits in "in" */
  111. /* for string and stored block copying */
  112. uint32_t length; /* literal or length of data to copy */
  113. unsigned offset; /* distance back to copy string from */
  114. /* for table and code decoding */
  115. unsigned extra; /* extra bits needed */
  116. /* fixed and dynamic code tables */
  117. code const *lencode; /* starting table for length/literal codes */
  118. code const *distcode; /* starting table for distance codes */
  119. unsigned lenbits; /* index bits for lencode */
  120. unsigned distbits; /* index bits for distcode */
  121. /* dynamic table building */
  122. unsigned ncode; /* number of code length code lengths */
  123. unsigned nlen; /* number of length code lengths */
  124. unsigned ndist; /* number of distance code lengths */
  125. uint32_t have; /* number of code lengths in lens[] */
  126. code *next; /* next available space in codes[] */
  127. uint16_t lens[320]; /* temporary storage for code lengths */
  128. uint16_t work[288]; /* work area for code table building */
  129. code codes[ENOUGH]; /* space for code tables */
  130. int sane; /* if false, allow invalid distance too far */
  131. int back; /* bits back of last unprocessed length/lit */
  132. unsigned was; /* initial length of match */
  133. uint32_t chunksize; /* size of memory copying chunk */
  134. inflate_allocs *alloc_bufs; /* struct for handling memory allocations */
  135. #ifdef HAVE_ARCH_INFLATE_STATE
  136. arch_inflate_state arch; /* architecture-specific extensions */
  137. #endif
  138. };
  139. void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state);
  140. Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm);
  141. Z_INTERNAL void free_inflate(PREFIX3(stream) *strm);
  142. #endif /* INFLATE_H_ */