Splint annotations.
[tools/librpm-tizen.git] / zlib / inffast.c
1 /* inffast.c -- fast decoding
2  * Copyright (C) 1995-2004 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5
6 #include "zutil.h"
7 #include "inftrees.h"
8 #include "inflate.h"
9 #include "inffast.h"
10
11 #ifndef ASMINF
12
13 /* Allow machine dependent optimization for post-increment or pre-increment.
14    Based on testing to date,
15    Pre-increment preferred for:
16    - PowerPC G3 (Adler)
17    - MIPS R5000 (Randers-Pehrson)
18    Post-increment preferred for:
19    - none
20    No measurable difference:
21    - Pentium III (Anderson)
22    - M68060 (Nikl)
23  */
24 #ifdef POSTINC
25 #  define OFF 0
26 #  define PUP(a) *(a)++
27 #else
28 #  define OFF 1
29 #  define PUP(a) *++(a)
30 #endif
31
32 /*
33    Decode literal, length, and distance codes and write out the resulting
34    literal and match bytes until either not enough input or output is
35    available, an end-of-block is encountered, or a data error is encountered.
36    When large enough input and output buffers are supplied to inflate(), for
37    example, a 16K input buffer and a 64K output buffer, more than 95% of the
38    inflate execution time is spent in this routine.
39
40    Entry assumptions:
41
42         state->mode == LEN
43         strm->avail_in >= 6
44         strm->avail_out >= 258
45         start >= strm->avail_out
46         state->bits < 8
47
48    On return, state->mode is one of:
49
50         LEN -- ran out of enough output space or enough available input
51         TYPE -- reached end of block code, inflate() to interpret next block
52         BAD -- error in block data
53
54    Notes:
55
56     - The maximum input bits used by a length/distance pair is 15 bits for the
57       length code, 5 bits for the length extra, 15 bits for the distance code,
58       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
59       Therefore if strm->avail_in >= 6, then there is enough input to avoid
60       checking for available input while decoding.
61
62     - The maximum bytes that a single length/distance pair can output is 258
63       bytes, which is the maximum length that can be coded.  inflate_fast()
64       requires strm->avail_out >= 258 for each loop to avoid checking for
65       output space.
66  */
67 void inflate_fast(z_streamp strm, unsigned start)
68 {
69     struct inflate_state FAR *state;
70     unsigned char FAR *in;      /* local strm->next_in */
71     unsigned char FAR *last;    /* while in < last, enough input available */
72     unsigned char FAR *out;     /* local strm->next_out */
73     unsigned char FAR *beg;     /* inflate()'s initial strm->next_out */
74     unsigned char FAR *end;     /* while out < end, enough space available */
75     unsigned wsize;             /* window size or zero if not using window */
76     unsigned whave;             /* valid bytes in the window */
77     unsigned write;             /* window write index */
78     unsigned char FAR *window;  /* allocated sliding window, if wsize != 0 */
79     unsigned long hold;         /* local strm->hold */
80     unsigned bits;              /* local strm->bits */
81     code const FAR *lcode;      /* local strm->lencode */
82     code const FAR *dcode;      /* local strm->distcode */
83     unsigned lmask;             /* mask for first level of length codes */
84     unsigned dmask;             /* mask for first level of distance codes */
85     code this;                  /* retrieved table entry */
86     unsigned op;                /* code bits, operation, extra bits, or */
87                                 /*  window position, window bytes to copy */
88     unsigned len;               /* match length, unused bytes */
89     unsigned dist;              /* match distance */
90     unsigned char FAR *from;    /* where to copy match from */
91
92     /* copy state to local variables */
93     state = (struct inflate_state FAR *)strm->state;
94     in = strm->next_in - OFF;
95     last = in + (strm->avail_in - 5);
96     out = strm->next_out - OFF;
97     beg = out - (start - strm->avail_out);
98     end = out + (strm->avail_out - 257);
99     wsize = state->wsize;
100     whave = state->whave;
101     write = state->write;
102     window = state->window;
103     hold = state->hold;
104     bits = state->bits;
105     lcode = state->lencode;
106     dcode = state->distcode;
107     lmask = (1U << state->lenbits) - 1;
108     dmask = (1U << state->distbits) - 1;
109
110     /* decode literals and length/distances until end-of-block or not enough
111        input data or output space */
112     do {
113         if (bits < 15) {
114             hold += (unsigned long)(PUP(in)) << bits;
115             bits += 8;
116             hold += (unsigned long)(PUP(in)) << bits;
117             bits += 8;
118         }
119         this = lcode[hold & lmask];
120       dolen:
121         op = (unsigned)(this.bits);
122         hold >>= op;
123         bits -= op;
124         op = (unsigned)(this.op);
125         if (op == 0) {                          /* literal */
126             Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
127                     "inflate:         literal '%c'\n" :
128                     "inflate:         literal 0x%02x\n", this.val));
129             PUP(out) = (unsigned char)(this.val);
130         }
131         else if (op & 16) {                     /* length base */
132             len = (unsigned)(this.val);
133             op &= 15;                           /* number of extra bits */
134             if (op) {
135                 if (bits < op) {
136                     hold += (unsigned long)(PUP(in)) << bits;
137                     bits += 8;
138                 }
139                 len += (unsigned)hold & ((1U << op) - 1);
140                 hold >>= op;
141                 bits -= op;
142             }
143             Tracevv((stderr, "inflate:         length %u\n", len));
144             if (bits < 15) {
145                 hold += (unsigned long)(PUP(in)) << bits;
146                 bits += 8;
147                 hold += (unsigned long)(PUP(in)) << bits;
148                 bits += 8;
149             }
150             this = dcode[hold & dmask];
151           dodist:
152             op = (unsigned)(this.bits);
153             hold >>= op;
154             bits -= op;
155             op = (unsigned)(this.op);
156             if (op & 16) {                      /* distance base */
157                 dist = (unsigned)(this.val);
158                 op &= 15;                       /* number of extra bits */
159                 if (bits < op) {
160                     hold += (unsigned long)(PUP(in)) << bits;
161                     bits += 8;
162                     if (bits < op) {
163                         hold += (unsigned long)(PUP(in)) << bits;
164                         bits += 8;
165                     }
166                 }
167                 dist += (unsigned)hold & ((1U << op) - 1);
168                 hold >>= op;
169                 bits -= op;
170                 Tracevv((stderr, "inflate:         distance %u\n", dist));
171                 op = (unsigned)(out - beg);     /* max distance in output */
172                 if (dist > op) {                /* see if copy from window */
173                     op = dist - op;             /* distance back in window */
174                     if (op > whave) {
175                         strm->msg = (char *)"invalid distance too far back";
176                         state->mode = BAD;
177                         break;
178                     }
179                     from = window - OFF;
180                     if (write == 0) {           /* very common case */
181                         from += wsize - op;
182                         if (op < len) {         /* some from window */
183                             len -= op;
184                             do {
185                                 PUP(out) = PUP(from);
186                             } while (--op);
187                             from = out - dist;  /* rest from output */
188                         }
189                     }
190                     else if (write < op) {      /* wrap around window */
191                         from += wsize + write - op;
192                         op -= write;
193                         if (op < len) {         /* some from end of window */
194                             len -= op;
195                             do {
196                                 PUP(out) = PUP(from);
197                             } while (--op);
198                             from = window - OFF;
199                             if (write < len) {  /* some from start of window */
200                                 op = write;
201                                 len -= op;
202                                 do {
203                                     PUP(out) = PUP(from);
204                                 } while (--op);
205                                 from = out - dist;      /* rest from output */
206                             }
207                         }
208                     }
209                     else {                      /* contiguous in window */
210                         from += write - op;
211                         if (op < len) {         /* some from window */
212                             len -= op;
213                             do {
214                                 PUP(out) = PUP(from);
215                             } while (--op);
216                             from = out - dist;  /* rest from output */
217                         }
218                     }
219                     while (len > 2) {
220                         PUP(out) = PUP(from);
221                         PUP(out) = PUP(from);
222                         PUP(out) = PUP(from);
223                         len -= 3;
224                     }
225                     if (len) {
226                         PUP(out) = PUP(from);
227                         if (len > 1)
228                             PUP(out) = PUP(from);
229                     }
230                 }
231                 else {
232                     from = out - dist;          /* copy direct from output */
233                     do {                        /* minimum length is three */
234                         PUP(out) = PUP(from);
235                         PUP(out) = PUP(from);
236                         PUP(out) = PUP(from);
237                         len -= 3;
238                     } while (len > 2);
239                     if (len) {
240                         PUP(out) = PUP(from);
241                         if (len > 1)
242                             PUP(out) = PUP(from);
243                     }
244                 }
245             }
246             else if ((op & 64) == 0) {          /* 2nd level distance code */
247                 this = dcode[this.val + (hold & ((1U << op) - 1))];
248                 goto dodist;
249             }
250             else {
251                 strm->msg = (char *)"invalid distance code";
252                 state->mode = BAD;
253                 break;
254             }
255         }
256         else if ((op & 64) == 0) {              /* 2nd level length code */
257             this = lcode[this.val + (hold & ((1U << op) - 1))];
258             goto dolen;
259         }
260         else if (op & 32) {                     /* end-of-block */
261             Tracevv((stderr, "inflate:         end of block\n"));
262             state->mode = TYPE;
263             break;
264         }
265         else {
266             strm->msg = (char *)"invalid literal/length code";
267             state->mode = BAD;
268             break;
269         }
270     } while (in < last && out < end);
271
272     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
273     len = bits >> 3;
274     in -= len;
275     bits -= len << 3;
276     hold &= (1U << bits) - 1;
277
278     /* update state and return */
279     strm->next_in = in + OFF;
280     strm->next_out = out + OFF;
281     strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
282     strm->avail_out = (unsigned)(out < end ?
283                                  257 + (end - out) : 257 - (out - end));
284     state->hold = hold;
285     state->bits = bits;
286     return;
287 }
288
289 /*
290    inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
291    - Using bit fields for code structure
292    - Different op definition to avoid & for extra bits (do & for table bits)
293    - Three separate decoding do-loops for direct, window, and write == 0
294    - Special case for distance > 1 copies to do overlapped load and store copy
295    - Explicit branch predictions (based on measured branch probabilities)
296    - Deferring match copy and interspersed it with decoding subsequent codes
297    - Swapping literal/length else
298    - Swapping window/direct else
299    - Larger unrolled copy loops (three is about right)
300    - Moving len -= 3 statement into middle of loop
301  */
302
303 #endif /* !ASMINF */