inffast.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* inffast.c -- process literals and length/distance pairs fast
  2. * Copyright (C) 1995-1998 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include <linux/zutil.h>
  6. #include "inftrees.h"
  7. #include "infblock.h"
  8. #include "infcodes.h"
  9. #include "infutil.h"
  10. #include "inffast.h"
  11. struct inflate_codes_state;
  12. /* simplify the use of the inflate_huft type with some defines */
  13. #define exop word.what.Exop
  14. #define bits word.what.Bits
  15. /* macros for bit input with no checking and for returning unused bytes */
  16. #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  17. #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  18. /* Called with number of bytes left to write in window at least 258
  19. (the maximum string length) and number of input bytes available
  20. at least ten. The ten bytes are six bytes for the longest length/
  21. distance pair plus four bytes for overloading the bit buffer. */
  22. int zlib_inflate_fast(
  23. uInt bl,
  24. uInt bd,
  25. inflate_huft *tl,
  26. inflate_huft *td, /* need separate declaration for Borland C++ */
  27. inflate_blocks_statef *s,
  28. z_streamp z
  29. )
  30. {
  31. inflate_huft *t; /* temporary pointer */
  32. uInt e; /* extra bits or operation */
  33. uLong b; /* bit buffer */
  34. uInt k; /* bits in bit buffer */
  35. Byte *p; /* input data pointer */
  36. uInt n; /* bytes available there */
  37. Byte *q; /* output window write pointer */
  38. uInt m; /* bytes to end of window or read pointer */
  39. uInt ml; /* mask for literal/length tree */
  40. uInt md; /* mask for distance tree */
  41. uInt c; /* bytes to copy */
  42. uInt d; /* distance back to copy from */
  43. Byte *r; /* copy source pointer */
  44. /* load input, output, bit values */
  45. LOAD
  46. /* initialize masks */
  47. ml = zlib_inflate_mask[bl];
  48. md = zlib_inflate_mask[bd];
  49. /* do until not enough input or output space for fast loop */
  50. do { /* assume called with m >= 258 && n >= 10 */
  51. /* get literal/length code */
  52. GRABBITS(20) /* max bits for literal/length code */
  53. if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  54. {
  55. DUMPBITS(t->bits)
  56. *q++ = (Byte)t->base;
  57. m--;
  58. continue;
  59. }
  60. do {
  61. DUMPBITS(t->bits)
  62. if (e & 16)
  63. {
  64. /* get extra bits for length */
  65. e &= 15;
  66. c = t->base + ((uInt)b & zlib_inflate_mask[e]);
  67. DUMPBITS(e)
  68. /* decode distance base of block to copy */
  69. GRABBITS(15); /* max bits for distance code */
  70. e = (t = td + ((uInt)b & md))->exop;
  71. do {
  72. DUMPBITS(t->bits)
  73. if (e & 16)
  74. {
  75. /* get extra bits to add to distance base */
  76. e &= 15;
  77. GRABBITS(e) /* get extra bits (up to 13) */
  78. d = t->base + ((uInt)b & zlib_inflate_mask[e]);
  79. DUMPBITS(e)
  80. /* do the copy */
  81. m -= c;
  82. r = q - d;
  83. if (r < s->window) /* wrap if needed */
  84. {
  85. do {
  86. r += s->end - s->window; /* force pointer in window */
  87. } while (r < s->window); /* covers invalid distances */
  88. e = s->end - r;
  89. if (c > e)
  90. {
  91. c -= e; /* wrapped copy */
  92. do {
  93. *q++ = *r++;
  94. } while (--e);
  95. r = s->window;
  96. do {
  97. *q++ = *r++;
  98. } while (--c);
  99. }
  100. else /* normal copy */
  101. {
  102. *q++ = *r++; c--;
  103. *q++ = *r++; c--;
  104. do {
  105. *q++ = *r++;
  106. } while (--c);
  107. }
  108. }
  109. else /* normal copy */
  110. {
  111. *q++ = *r++; c--;
  112. *q++ = *r++; c--;
  113. do {
  114. *q++ = *r++;
  115. } while (--c);
  116. }
  117. break;
  118. }
  119. else if ((e & 64) == 0)
  120. {
  121. t += t->base;
  122. e = (t += ((uInt)b & zlib_inflate_mask[e]))->exop;
  123. }
  124. else
  125. {
  126. z->msg = (char*)"invalid distance code";
  127. UNGRAB
  128. UPDATE
  129. return Z_DATA_ERROR;
  130. }
  131. } while (1);
  132. break;
  133. }
  134. if ((e & 64) == 0)
  135. {
  136. t += t->base;
  137. if ((e = (t += ((uInt)b & zlib_inflate_mask[e]))->exop) == 0)
  138. {
  139. DUMPBITS(t->bits)
  140. *q++ = (Byte)t->base;
  141. m--;
  142. break;
  143. }
  144. }
  145. else if (e & 32)
  146. {
  147. UNGRAB
  148. UPDATE
  149. return Z_STREAM_END;
  150. }
  151. else
  152. {
  153. z->msg = (char*)"invalid literal/length code";
  154. UNGRAB
  155. UPDATE
  156. return Z_DATA_ERROR;
  157. }
  158. } while (1);
  159. } while (m >= 258 && n >= 10);
  160. /* not enough input or output--restore pointers and return */
  161. UNGRAB
  162. UPDATE
  163. return Z_OK;
  164. }