OSDN Git Service

Update version number to 1.2.1.0
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / libsvn_diff / infblock.c
1 /* infblock.c -- interpret and process block types to last block\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 "infblock.h"\r
8 #include "inftrees.h"\r
9 #include "infcodes.h"\r
10 #include "infutil.h"\r
11 \r
12 struct inflate_codes_state {int dummy;}; /* for buggy compilers */\r
13 \r
14 /* simplify the use of the inflate_huft type with some defines */\r
15 #define exop word.what.Exop\r
16 #define bits word.what.Bits\r
17 \r
18 /* Table for deflate from PKZIP's appnote.txt. */\r
19 local const uInt border[] = { /* Order of the bit length code lengths */\r
20         16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};\r
21 \r
22 /*\r
23    Notes beyond the 1.93a appnote.txt:\r
24 \r
25    1. Distance pointers never point before the beginning of the output\r
26       stream.\r
27    2. Distance pointers can point back across blocks, up to 32k away.\r
28    3. There is an implied maximum of 7 bits for the bit length table and\r
29       15 bits for the actual data.\r
30    4. If only one code exists, then it is encoded using one bit.  (Zero\r
31       would be more efficient, but perhaps a little confusing.)  If two\r
32       codes exist, they are coded using one bit each (0 and 1).\r
33    5. There is no way of sending zero distance codes--a dummy must be\r
34       sent if there are none.  (History: a pre 2.0 version of PKZIP would\r
35       store blocks with no distance codes, but this was discovered to be\r
36       too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow\r
37       zero distance codes, which is sent as one code of zero bits in\r
38       length.\r
39    6. There are up to 286 literal/length codes.  Code 256 represents the\r
40       end-of-block.  Note however that the static length tree defines\r
41       288 codes just to fill out the Huffman codes.  Codes 286 and 287\r
42       cannot be used though, since there is no length base or extra bits\r
43       defined for them.  Similarily, there are up to 30 distance codes.\r
44       However, static trees define 32 codes (all 5 bits) to fill out the\r
45       Huffman codes, but the last two had better not show up in the data.\r
46    7. Unzip can check dynamic Huffman blocks for complete code sets.\r
47       The exception is that a single code would not be complete (see #4).\r
48    8. The five bits following the block type is really the number of\r
49       literal codes sent minus 257.\r
50    9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits\r
51       (1+6+6).  Therefore, to output three times the length, you output\r
52       three codes (1+1+1), whereas to output four times the same length,\r
53       you only need two codes (1+3).  Hmm.\r
54   10. In the tree reconstruction algorithm, Code = Code + Increment\r
55       only if BitLength(i) is not zero.  (Pretty obvious.)\r
56   11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)\r
57   12. Note: length code 284 can represent 227-258, but length code 285\r
58       really is 258.  The last length deserves its own, short code\r
59       since it gets used a lot in very redundant files.  The length\r
60       258 is special since 258 - 3 (the min match length) is 255.\r
61   13. The literal/length and distance code bit lengths are read as a\r
62       single stream of lengths.  It is possible (and advantageous) for\r
63       a repeat code (16, 17, or 18) to go across the boundary between\r
64       the two sets of lengths.\r
65  */\r
66 \r
67 \r
68 void inflate_blocks_reset(s, z, c)\r
69 inflate_blocks_statef *s;\r
70 z_streamp z;\r
71 uLongf *c;\r
72 {\r
73   if (c != Z_NULL)\r
74     *c = s->check;\r
75   if (s->mode == BTREE || s->mode == DTREE)\r
76     ZFREE(z, s->sub.trees.blens);\r
77   if (s->mode == CODES)\r
78     inflate_codes_free(s->sub.decode.codes, z);\r
79   s->mode = TYPE;\r
80   s->bitk = 0;\r
81   s->bitb = 0;\r
82   s->read = s->write = s->window;\r
83   if (s->checkfn != Z_NULL)\r
84     z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);\r
85   Tracev((stderr, "inflate:   blocks reset\n"));\r
86 }\r
87 \r
88 \r
89 inflate_blocks_statef *inflate_blocks_new(z, c, w)\r
90 z_streamp z;\r
91 check_func c;\r
92 uInt w;\r
93 {\r
94   inflate_blocks_statef *s;\r
95 \r
96   if ((s = (inflate_blocks_statef *)ZALLOC\r
97        (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)\r
98     return s;\r
99   if ((s->hufts =\r
100        (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)\r
101   {\r
102     ZFREE(z, s);\r
103     return Z_NULL;\r
104   }\r
105   if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)\r
106   {\r
107     ZFREE(z, s->hufts);\r
108     ZFREE(z, s);\r
109     return Z_NULL;\r
110   }\r
111   s->end = s->window + w;\r
112   s->checkfn = c;\r
113   s->mode = TYPE;\r
114   Tracev((stderr, "inflate:   blocks allocated\n"));\r
115   inflate_blocks_reset(s, z, Z_NULL);\r
116   return s;\r
117 }\r
118 \r
119 \r
120 int inflate_blocks(s, z, r)\r
121 inflate_blocks_statef *s;\r
122 z_streamp z;\r
123 int r;\r
124 {\r
125   uInt t;               /* temporary storage */\r
126   uLong b;              /* bit buffer */\r
127   uInt k;               /* bits in bit buffer */\r
128   Bytef *p;             /* input data pointer */\r
129   uInt n;               /* bytes available there */\r
130   Bytef *q;             /* output window write pointer */\r
131   uInt m;               /* bytes to end of window or read pointer */\r
132 \r
133   /* copy input/output information to locals (UPDATE macro restores) */\r
134   LOAD\r
135 \r
136   /* process input based on current state */\r
137   while (1) switch (s->mode)\r
138   {\r
139     case TYPE:\r
140       NEEDBITS(3)\r
141       t = (uInt)b & 7;\r
142       s->last = t & 1;\r
143       switch (t >> 1)\r
144       {\r
145         case 0:                         /* stored */\r
146           Tracev((stderr, "inflate:     stored block%s\n",\r
147                  s->last ? " (last)" : ""));\r
148           DUMPBITS(3)\r
149           t = k & 7;                    /* go to byte boundary */\r
150           DUMPBITS(t)\r
151           s->mode = LENS;               /* get length of stored block */\r
152           break;\r
153         case 1:                         /* fixed */\r
154           Tracev((stderr, "inflate:     fixed codes block%s\n",\r
155                  s->last ? " (last)" : ""));\r
156           {\r
157             uInt bl, bd;\r
158             inflate_huft *tl, *td;\r
159 \r
160             inflate_trees_fixed(&bl, &bd, &tl, &td, z);\r
161             s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);\r
162             if (s->sub.decode.codes == Z_NULL)\r
163             {\r
164               r = Z_MEM_ERROR;\r
165               LEAVE\r
166             }\r
167           }\r
168           DUMPBITS(3)\r
169           s->mode = CODES;\r
170           break;\r
171         case 2:                         /* dynamic */\r
172           Tracev((stderr, "inflate:     dynamic codes block%s\n",\r
173                  s->last ? " (last)" : ""));\r
174           DUMPBITS(3)\r
175           s->mode = TABLE;\r
176           break;\r
177         case 3:                         /* illegal */\r
178           DUMPBITS(3)\r
179           s->mode = BAD;\r
180           z->msg = (char*)"invalid block type";\r
181           r = Z_DATA_ERROR;\r
182           LEAVE\r
183       }\r
184       break;\r
185     case LENS:\r
186       NEEDBITS(32)\r
187       if ((((~b) >> 16) & 0xffff) != (b & 0xffff))\r
188       {\r
189         s->mode = BAD;\r
190         z->msg = (char*)"invalid stored block lengths";\r
191         r = Z_DATA_ERROR;\r
192         LEAVE\r
193       }\r
194       s->sub.left = (uInt)b & 0xffff;\r
195       b = k = 0;                      /* dump bits */\r
196       Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));\r
197       s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);\r
198       break;\r
199     case STORED:\r
200       if (n == 0)\r
201         LEAVE\r
202       NEEDOUT\r
203       t = s->sub.left;\r
204       if (t > n) t = n;\r
205       if (t > m) t = m;\r
206       zmemcpy(q, p, t);\r
207       p += t;  n -= t;\r
208       q += t;  m -= t;\r
209       if ((s->sub.left -= t) != 0)\r
210         break;\r
211       Tracev((stderr, "inflate:       stored end, %lu total out\n",\r
212               z->total_out + (q >= s->read ? q - s->read :\r
213               (s->end - s->read) + (q - s->window))));\r
214       s->mode = s->last ? DRY : TYPE;\r
215       break;\r
216     case TABLE:\r
217       NEEDBITS(14)\r
218       s->sub.trees.table = t = (uInt)b & 0x3fff;\r
219 #ifndef PKZIP_BUG_WORKAROUND\r
220       if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)\r
221       {\r
222         s->mode = BAD;\r
223         z->msg = (char*)"too many length or distance symbols";\r
224         r = Z_DATA_ERROR;\r
225         LEAVE\r
226       }\r
227 #endif\r
228       t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);\r
229       if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)\r
230       {\r
231         r = Z_MEM_ERROR;\r
232         LEAVE\r
233       }\r
234       DUMPBITS(14)\r
235       s->sub.trees.index = 0;\r
236       Tracev((stderr, "inflate:       table sizes ok\n"));\r
237       s->mode = BTREE;\r
238     case BTREE:\r
239       while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))\r
240       {\r
241         NEEDBITS(3)\r
242         s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;\r
243         DUMPBITS(3)\r
244       }\r
245       while (s->sub.trees.index < 19)\r
246         s->sub.trees.blens[border[s->sub.trees.index++]] = 0;\r
247       s->sub.trees.bb = 7;\r
248       t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,\r
249                              &s->sub.trees.tb, s->hufts, z);\r
250       if (t != Z_OK)\r
251       {\r
252         r = t;\r
253         if (r == Z_DATA_ERROR)\r
254         {\r
255           ZFREE(z, s->sub.trees.blens);\r
256           s->mode = BAD;\r
257         }\r
258         LEAVE\r
259       }\r
260       s->sub.trees.index = 0;\r
261       Tracev((stderr, "inflate:       bits tree ok\n"));\r
262       s->mode = DTREE;\r
263     case DTREE:\r
264       while (t = s->sub.trees.table,\r
265              s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))\r
266       {\r
267         inflate_huft *h;\r
268         uInt i, j, c;\r
269 \r
270         t = s->sub.trees.bb;\r
271         NEEDBITS(t)\r
272         h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);\r
273         t = h->bits;\r
274         c = h->base;\r
275         if (c < 16)\r
276         {\r
277           DUMPBITS(t)\r
278           s->sub.trees.blens[s->sub.trees.index++] = c;\r
279         }\r
280         else /* c == 16..18 */\r
281         {\r
282           i = c == 18 ? 7 : c - 14;\r
283           j = c == 18 ? 11 : 3;\r
284           NEEDBITS(t + i)\r
285           DUMPBITS(t)\r
286           j += (uInt)b & inflate_mask[i];\r
287           DUMPBITS(i)\r
288           i = s->sub.trees.index;\r
289           t = s->sub.trees.table;\r
290           if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||\r
291               (c == 16 && i < 1))\r
292           {\r
293             ZFREE(z, s->sub.trees.blens);\r
294             s->mode = BAD;\r
295             z->msg = (char*)"invalid bit length repeat";\r
296             r = Z_DATA_ERROR;\r
297             LEAVE\r
298           }\r
299           c = c == 16 ? s->sub.trees.blens[i - 1] : 0;\r
300           do {\r
301             s->sub.trees.blens[i++] = c;\r
302           } while (--j);\r
303           s->sub.trees.index = i;\r
304         }\r
305       }\r
306       s->sub.trees.tb = Z_NULL;\r
307       {\r
308         uInt bl, bd;\r
309         inflate_huft *tl, *td;\r
310         inflate_codes_statef *c;\r
311 \r
312         bl = 9;         /* must be <= 9 for lookahead assumptions */\r
313         bd = 6;         /* must be <= 9 for lookahead assumptions */\r
314         t = s->sub.trees.table;\r
315         t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),\r
316                                   s->sub.trees.blens, &bl, &bd, &tl, &td,\r
317                                   s->hufts, z);\r
318         if (t != Z_OK)\r
319         {\r
320           if (t == (uInt)Z_DATA_ERROR)\r
321           {\r
322             ZFREE(z, s->sub.trees.blens);\r
323             s->mode = BAD;\r
324           }\r
325           r = t;\r
326           LEAVE\r
327         }\r
328         Tracev((stderr, "inflate:       trees ok\n"));\r
329         if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)\r
330         {\r
331           r = Z_MEM_ERROR;\r
332           LEAVE\r
333         }\r
334         s->sub.decode.codes = c;\r
335       }\r
336       ZFREE(z, s->sub.trees.blens);\r
337       s->mode = CODES;\r
338     case CODES:\r
339       UPDATE\r
340       if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)\r
341         return inflate_flush(s, z, r);\r
342       r = Z_OK;\r
343       inflate_codes_free(s->sub.decode.codes, z);\r
344       LOAD\r
345       Tracev((stderr, "inflate:       codes end, %lu total out\n",\r
346               z->total_out + (q >= s->read ? q - s->read :\r
347               (s->end - s->read) + (q - s->window))));\r
348       if (!s->last)\r
349       {\r
350         s->mode = TYPE;\r
351         break;\r
352       }\r
353       s->mode = DRY;\r
354     case DRY:\r
355       FLUSH\r
356       if (s->read != s->write)\r
357         LEAVE\r
358       s->mode = DONE;\r
359     case DONE:\r
360       r = Z_STREAM_END;\r
361       LEAVE\r
362     case BAD:\r
363       r = Z_DATA_ERROR;\r
364       LEAVE\r
365     default:\r
366       r = Z_STREAM_ERROR;\r
367       LEAVE\r
368   }\r
369 }\r
370 \r
371 \r
372 int inflate_blocks_free(s, z)\r
373 inflate_blocks_statef *s;\r
374 z_streamp z;\r
375 {\r
376   inflate_blocks_reset(s, z, Z_NULL);\r
377   ZFREE(z, s->window);\r
378   ZFREE(z, s->hufts);\r
379   ZFREE(z, s);\r
380   Tracev((stderr, "inflate:   blocks freed\n"));\r
381   return Z_OK;\r
382 }\r
383 \r
384 \r
385 void inflate_set_dictionary(s, d, n)\r
386 inflate_blocks_statef *s;\r
387 const Bytef *d;\r
388 uInt  n;\r
389 {\r
390   zmemcpy(s->window, d, n);\r
391   s->read = s->write = s->window + n;\r
392 }\r
393 \r
394 \r
395 /* Returns true if inflate is currently at the end of a block generated\r
396  * by Z_SYNC_FLUSH or Z_FULL_FLUSH. \r
397  * IN assertion: s != Z_NULL\r
398  */\r
399 int inflate_blocks_sync_point(s)\r
400 inflate_blocks_statef *s;\r
401 {\r
402   return s->mode == LENS;\r
403 }\r