OSDN Git Service

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