infutil.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* infutil.h -- types and macros common to blocks and codes
  2. * Copyright (C) 1995-1998 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 _INFUTIL_H
  10. #define _INFUTIL_H
  11. #include <linux/zconf.h>
  12. #include "inftrees.h"
  13. #include "infcodes.h"
  14. typedef enum {
  15. TYPE, /* get type bits (3, including end bit) */
  16. LENS, /* get lengths for stored */
  17. STORED, /* processing stored block */
  18. TABLE, /* get table lengths */
  19. BTREE, /* get bit lengths tree for a dynamic block */
  20. DTREE, /* get length, distance trees for a dynamic block */
  21. CODES, /* processing fixed or dynamic block */
  22. DRY, /* output remaining window bytes */
  23. B_DONE, /* finished last block, done */
  24. B_BAD} /* got a data error--stuck here */
  25. inflate_block_mode;
  26. /* inflate blocks semi-private state */
  27. struct inflate_blocks_state {
  28. /* mode */
  29. inflate_block_mode mode; /* current inflate_block mode */
  30. /* mode dependent information */
  31. union {
  32. uInt left; /* if STORED, bytes left to copy */
  33. struct {
  34. uInt table; /* table lengths (14 bits) */
  35. uInt index; /* index into blens (or border) */
  36. uInt *blens; /* bit lengths of codes */
  37. uInt bb; /* bit length tree depth */
  38. inflate_huft *tb; /* bit length decoding tree */
  39. } trees; /* if DTREE, decoding info for trees */
  40. struct {
  41. inflate_codes_statef
  42. *codes;
  43. } decode; /* if CODES, current state */
  44. } sub; /* submode */
  45. uInt last; /* true if this block is the last block */
  46. /* mode independent information */
  47. uInt bitk; /* bits in bit buffer */
  48. uLong bitb; /* bit buffer */
  49. inflate_huft *hufts; /* single malloc for tree space */
  50. Byte *window; /* sliding window */
  51. Byte *end; /* one byte after sliding window */
  52. Byte *read; /* window read pointer */
  53. Byte *write; /* window write pointer */
  54. check_func checkfn; /* check function */
  55. uLong check; /* check on output */
  56. };
  57. /* defines for inflate input/output */
  58. /* update pointers and return */
  59. #define UPDBITS {s->bitb=b;s->bitk=k;}
  60. #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
  61. #define UPDOUT {s->write=q;}
  62. #define UPDATE {UPDBITS UPDIN UPDOUT}
  63. #define LEAVE {UPDATE return zlib_inflate_flush(s,z,r);}
  64. /* get bytes and bits */
  65. #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  66. #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  67. #define NEXTBYTE (n--,*p++)
  68. #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  69. #define DUMPBITS(j) {b>>=(j);k-=(j);}
  70. /* output bytes */
  71. #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  72. #define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
  73. #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  74. #define FLUSH {UPDOUT r=zlib_inflate_flush(s,z,r); LOADOUT}
  75. #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  76. #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  77. /* load local pointers */
  78. #define LOAD {LOADIN LOADOUT}
  79. /* masks for lower bits (size given to avoid silly warnings with Visual C++) */
  80. extern uInt zlib_inflate_mask[17];
  81. /* copy as much as possible from the sliding window to the output area */
  82. extern int zlib_inflate_flush (
  83. inflate_blocks_statef *,
  84. z_streamp ,
  85. int);
  86. /* inflate private state */
  87. typedef enum {
  88. METHOD, /* waiting for method byte */
  89. FLAG, /* waiting for flag byte */
  90. DICT4, /* four dictionary check bytes to go */
  91. DICT3, /* three dictionary check bytes to go */
  92. DICT2, /* two dictionary check bytes to go */
  93. DICT1, /* one dictionary check byte to go */
  94. DICT0, /* waiting for inflateSetDictionary */
  95. BLOCKS, /* decompressing blocks */
  96. CHECK4, /* four check bytes to go */
  97. CHECK3, /* three check bytes to go */
  98. CHECK2, /* two check bytes to go */
  99. CHECK1, /* one check byte to go */
  100. I_DONE, /* finished check, done */
  101. I_BAD} /* got an error--stay here */
  102. inflate_mode;
  103. struct internal_state {
  104. /* mode */
  105. inflate_mode mode; /* current inflate mode */
  106. /* mode dependent information */
  107. union {
  108. uInt method; /* if FLAGS, method byte */
  109. struct {
  110. uLong was; /* computed check value */
  111. uLong need; /* stream check value */
  112. } check; /* if CHECK, check values to compare */
  113. uInt marker; /* if BAD, inflateSync's marker bytes count */
  114. } sub; /* submode */
  115. /* mode independent information */
  116. int nowrap; /* flag for no wrapper */
  117. uInt wbits; /* log2(window size) (8..15, defaults to 15) */
  118. inflate_blocks_statef
  119. *blocks; /* current inflate_blocks state */
  120. };
  121. /* inflate codes private state */
  122. typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  123. START, /* x: set up for LEN */
  124. LEN, /* i: get length/literal/eob next */
  125. LENEXT, /* i: getting length extra (have base) */
  126. DIST, /* i: get distance next */
  127. DISTEXT, /* i: getting distance extra */
  128. COPY, /* o: copying bytes in window, waiting for space */
  129. LIT, /* o: got literal, waiting for output space */
  130. WASH, /* o: got eob, possibly still output waiting */
  131. END, /* x: got eob and all data flushed */
  132. BADCODE} /* x: got error */
  133. inflate_codes_mode;
  134. struct inflate_codes_state {
  135. /* mode */
  136. inflate_codes_mode mode; /* current inflate_codes mode */
  137. /* mode dependent information */
  138. uInt len;
  139. union {
  140. struct {
  141. inflate_huft *tree; /* pointer into tree */
  142. uInt need; /* bits needed */
  143. } code; /* if LEN or DIST, where in tree */
  144. uInt lit; /* if LIT, literal */
  145. struct {
  146. uInt get; /* bits to get for extra */
  147. uInt dist; /* distance back to copy from */
  148. } copy; /* if EXT or COPY, where and how much */
  149. } sub; /* submode */
  150. /* mode independent information */
  151. Byte lbits; /* ltree bits decoded per branch */
  152. Byte dbits; /* dtree bits decoder per branch */
  153. inflate_huft *ltree; /* literal/length/eob tree */
  154. inflate_huft *dtree; /* distance tree */
  155. };
  156. /* memory allocation for inflation */
  157. struct inflate_workspace {
  158. inflate_codes_statef working_state;
  159. struct inflate_blocks_state working_blocks_state;
  160. struct internal_state internal_state;
  161. unsigned int tree_work_area_1[19];
  162. unsigned int tree_work_area_2[288];
  163. unsigned working_blens[258 + 0x1f + 0x1f];
  164. inflate_huft working_hufts[MANY];
  165. unsigned char working_window[1 << MAX_WBITS];
  166. };
  167. #define WS(z) ((struct inflate_workspace *)(z->workspace))
  168. #endif