unzip.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Changes by Gunnar Ritter, Freiburg i. Br., Germany, May 2003.
  3. *
  4. * Derived from unzip 5.40.
  5. *
  6. * Sccsid @(#)unzip.h 1.5 (gritter) 7/16/04
  7. */
  8. #include <inttypes.h>
  9. #define Trace(a)
  10. #define MAX_BITS 13
  11. #define HSIZE (1 << MAX_BITS)
  12. #define WSIZE 65536L /* at least 64K for enhanced deflate */
  13. #define redirSlide G.area.Slide
  14. #define NEXTBYTE (--G.incnt >= 0 ? (int)(*G.inptr++) : readbyte(&G))
  15. #define READBITS(nbits, zdest) { \
  16. if (nbits > G.bits_left) { \
  17. int temp; \
  18. G.zipeof = 1; \
  19. while (G.bits_left <= 8 * (int)(sizeof G.bitbuf - 1) && \
  20. (temp = NEXTBYTE) != EOF) { \
  21. G.bitbuf |= (uint32_t)temp << G.bits_left; \
  22. G.bits_left += 8; \
  23. G.zipeof = 0; \
  24. } \
  25. } \
  26. zdest = (int16_t)((uint16_t)G.bitbuf & mask_bits[nbits]); \
  27. G.bitbuf >>= nbits; \
  28. G.bits_left -= nbits; \
  29. }
  30. #undef FALSE
  31. #undef TRUE
  32. enum {
  33. FALSE = 0,
  34. TRUE = 1
  35. };
  36. union work {
  37. struct {
  38. int16_t Parent[HSIZE];
  39. uint8_t value[HSIZE];
  40. uint8_t Stack[HSIZE];
  41. } shrink;
  42. uint8_t Slide[WSIZE];
  43. };
  44. #define OUTBUFSIZ 4096
  45. struct globals {
  46. union work area;
  47. uint8_t inbuf[4096];
  48. long long zsize;
  49. long long uzsize;
  50. uint8_t *inptr;
  51. const char *tgt;
  52. struct huft *fixed_tl;
  53. struct huft *fixed_td;
  54. struct huft *fixed_tl64;
  55. struct huft *fixed_td64;
  56. struct huft *fixed_tl32;
  57. struct huft *fixed_td32;
  58. const uint16_t *cplens;
  59. const uint8_t *cplext;
  60. const uint8_t *cpdext;
  61. long csize;
  62. long ucsize;
  63. uint8_t outbuf[OUTBUFSIZ];
  64. uint8_t *outptr;
  65. uint32_t *crc;
  66. uint32_t bitbuf;
  67. uint32_t wp;
  68. uint32_t bb;
  69. uint32_t bk;
  70. unsigned outcnt;
  71. int tfd;
  72. int doswap;
  73. int incnt;
  74. int bits_left;
  75. int zipeof;
  76. int fixed_bl;
  77. int fixed_bd;
  78. int fixed_bl64;
  79. int fixed_bd64;
  80. int fixed_bl32;
  81. int fixed_bd32;
  82. int status;
  83. };
  84. /* Huffman code lookup table entry--this entry is four bytes for machines
  85. that have 16-bit pointers (e.g. PC's in the small or medium model).
  86. Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
  87. means that v is a literal, 16 < e < 32 means that v is a pointer to
  88. the next table, which codes e - 16 bits, and lastly e == 99 indicates
  89. an unused code. If a code with e == 99 is looked up, this implies an
  90. error in the data. */
  91. struct huft {
  92. uint8_t e; /* number of extra bits or operation */
  93. uint8_t b; /* number of bits in this code or subcode */
  94. union {
  95. uint16_t n; /* literal, length base, or distance base */
  96. struct huft *t; /* pointer to next level of table */
  97. } v;
  98. };
  99. extern const uint16_t mask_bits[];
  100. extern void flush(struct globals *, const void *, size_t);
  101. extern int readbyte(struct globals *);
  102. extern int huft_build(const unsigned *b, unsigned n, unsigned s,
  103. const uint16_t *d, const uint8_t *e,
  104. struct huft **t, int *m,
  105. int bits, int nob, int eob);
  106. extern void huft_free(struct huft *);