1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jboss.netty.util.internal.jzlib;
50
51 import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
52
53 final class Inflate {
54
55 private static final int METHOD = 0;
56 private static final int FLAG = 1;
57 private static final int DICT4 = 2;
58 private static final int DICT3 = 3;
59 private static final int DICT2 = 4;
60 private static final int DICT1 = 5;
61 private static final int DICT0 = 6;
62 private static final int BLOCKS = 7;
63 private static final int CHECK4 = 8;
64 private static final int CHECK3 = 9;
65 private static final int CHECK2 = 10;
66 private static final int CHECK1 = 11;
67 private static final int DONE = 12;
68 private static final int BAD = 13;
69
70 private static final int GZIP_ID1 = 14;
71 private static final int GZIP_ID2 = 15;
72 private static final int GZIP_CM = 16;
73 private static final int GZIP_FLG = 17;
74 private static final int GZIP_MTIME_XFL_OS = 18;
75 private static final int GZIP_XLEN = 19;
76 private static final int GZIP_FEXTRA = 20;
77 private static final int GZIP_FNAME = 21;
78 private static final int GZIP_FCOMMENT = 22;
79 private static final int GZIP_FHCRC = 23;
80 private static final int GZIP_CRC32= 24;
81 private static final int GZIP_ISIZE = 25;
82
83 private int mode;
84
85 private int method;
86
87 private final long[] was = new long[1];
88 private long need;
89
90 private int marker;
91
92 private WrapperType wrapperType;
93 private int wbits;
94 private InfBlocks blocks;
95 private int gzipFlag;
96 private int gzipBytesToRead;
97 private int gzipXLen;
98 private int gzipUncompressedBytes;
99 private int gzipCRC32;
100 private int gzipISize;
101
102 private int inflateReset(ZStream z) {
103 if (z == null || z.istate == null) {
104 return JZlib.Z_STREAM_ERROR;
105 }
106
107 z.total_in = z.total_out = 0;
108 z.msg = null;
109 switch (wrapperType) {
110 case NONE:
111 z.istate.mode = BLOCKS;
112 break;
113 case ZLIB:
114 case ZLIB_OR_NONE:
115 z.istate.mode = METHOD;
116 break;
117 case GZIP:
118 z.istate.mode = GZIP_ID1;
119 break;
120 }
121 z.istate.blocks.reset(z, null);
122 gzipUncompressedBytes = 0;
123 return JZlib.Z_OK;
124 }
125
126 int inflateEnd(ZStream z) {
127 if (blocks != null) {
128 blocks.free(z);
129 }
130 blocks = null;
131
132 return JZlib.Z_OK;
133 }
134
135 int inflateInit(ZStream z, int w, WrapperType wrapperType) {
136 z.msg = null;
137 blocks = null;
138
139 this.wrapperType = wrapperType;
140
141 if (w < 0) {
142 throw new IllegalArgumentException("w: " + w);
143 }
144
145
146 if (w < 8 || w > 15) {
147 inflateEnd(z);
148 return JZlib.Z_STREAM_ERROR;
149 }
150 wbits = w;
151
152 z.istate.blocks = new InfBlocks(
153 z, z.istate.wrapperType == WrapperType.NONE? null : this,
154 1 << w);
155
156
157 inflateReset(z);
158 return JZlib.Z_OK;
159 }
160
161 int inflate(ZStream z, int f) {
162 int r;
163 int b;
164
165 if (z == null || z.istate == null || z.next_in == null) {
166 return JZlib.Z_STREAM_ERROR;
167 }
168 f = f == JZlib.Z_FINISH? JZlib.Z_BUF_ERROR : JZlib.Z_OK;
169 r = JZlib.Z_BUF_ERROR;
170 while (true) {
171
172 switch (z.istate.mode) {
173 case METHOD:
174
175 if (z.avail_in == 0) {
176 return r;
177 }
178
179
180 if (z.istate.wrapperType == WrapperType.ZLIB_OR_NONE) {
181 if ((z.next_in[z.next_in_index] & 0xf) != JZlib.Z_DEFLATED ||
182 (z.next_in[z.next_in_index] >> 4) + 8 > z.istate.wbits) {
183 z.istate.wrapperType = WrapperType.NONE;
184 z.istate.mode = BLOCKS;
185 break;
186 } else {
187 z.istate.wrapperType = WrapperType.ZLIB;
188 }
189 }
190
191 r = f;
192
193 z.avail_in --;
194 z.total_in ++;
195 if (((z.istate.method = z.next_in[z.next_in_index ++]) & 0xf) != JZlib.Z_DEFLATED) {
196 z.istate.mode = BAD;
197 z.msg = "unknown compression method";
198 z.istate.marker = 5;
199 break;
200 }
201 if ((z.istate.method >> 4) + 8 > z.istate.wbits) {
202 z.istate.mode = BAD;
203 z.msg = "invalid window size";
204 z.istate.marker = 5;
205 break;
206 }
207 z.istate.mode = FLAG;
208 case FLAG:
209
210 if (z.avail_in == 0) {
211 return r;
212 }
213 r = f;
214
215 z.avail_in --;
216 z.total_in ++;
217 b = z.next_in[z.next_in_index ++] & 0xff;
218
219 if (((z.istate.method << 8) + b) % 31 != 0) {
220 z.istate.mode = BAD;
221 z.msg = "incorrect header check";
222 z.istate.marker = 5;
223 break;
224 }
225
226 if ((b & JZlib.PRESET_DICT) == 0) {
227 z.istate.mode = BLOCKS;
228 break;
229 }
230 z.istate.mode = DICT4;
231 case DICT4:
232
233 if (z.avail_in == 0) {
234 return r;
235 }
236 r = f;
237
238 z.avail_in --;
239 z.total_in ++;
240 z.istate.need = (z.next_in[z.next_in_index ++] & 0xff) << 24 & 0xff000000L;
241 z.istate.mode = DICT3;
242 case DICT3:
243
244 if (z.avail_in == 0) {
245 return r;
246 }
247 r = f;
248
249 z.avail_in --;
250 z.total_in ++;
251 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 16 & 0xff0000L;
252 z.istate.mode = DICT2;
253 case DICT2:
254
255 if (z.avail_in == 0) {
256 return r;
257 }
258 r = f;
259
260 z.avail_in --;
261 z.total_in ++;
262 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 8 & 0xff00L;
263 z.istate.mode = DICT1;
264 case DICT1:
265
266 if (z.avail_in == 0) {
267 return r;
268 }
269
270 z.avail_in --;
271 z.total_in ++;
272 z.istate.need += z.next_in[z.next_in_index ++] & 0xffL;
273 z.adler = z.istate.need;
274 z.istate.mode = DICT0;
275 return JZlib.Z_NEED_DICT;
276 case DICT0:
277 z.istate.mode = BAD;
278 z.msg = "need dictionary";
279 z.istate.marker = 0;
280 return JZlib.Z_STREAM_ERROR;
281 case BLOCKS:
282 int old_next_out_index = z.next_out_index;
283 try {
284 r = z.istate.blocks.proc(z, r);
285 if (r == JZlib.Z_DATA_ERROR) {
286 z.istate.mode = BAD;
287 z.istate.marker = 0;
288 break;
289 }
290 if (r == JZlib.Z_OK) {
291 r = f;
292 }
293 if (r != JZlib.Z_STREAM_END) {
294 return r;
295 }
296 r = f;
297 z.istate.blocks.reset(z, z.istate.was);
298 } finally {
299 int decompressedBytes = z.next_out_index - old_next_out_index;
300 gzipUncompressedBytes += decompressedBytes;
301 z.crc32 = CRC32.crc32(z.crc32, z.next_out, old_next_out_index, decompressedBytes);
302 }
303
304 if (z.istate.wrapperType == WrapperType.NONE) {
305 z.istate.mode = DONE;
306 break;
307 } else if (z.istate.wrapperType == WrapperType.ZLIB) {
308 z.istate.mode = CHECK4;
309 } else if (z.istate.wrapperType == WrapperType.GZIP){
310 gzipCRC32 = 0;
311 gzipISize = 0;
312 gzipBytesToRead = 4;
313 z.istate.mode = GZIP_CRC32;
314 break;
315 } else {
316 z.istate.mode = BAD;
317 z.msg = "unexpected state";
318 z.istate.marker = 0;
319 break;
320 }
321 case CHECK4:
322 if (z.avail_in == 0) {
323 return r;
324 }
325 r = f;
326
327 z.avail_in --;
328 z.total_in ++;
329 z.istate.need = (z.next_in[z.next_in_index ++] & 0xff) << 24 & 0xff000000L;
330 z.istate.mode = CHECK3;
331 case CHECK3:
332 if (z.avail_in == 0) {
333 return r;
334 }
335 r = f;
336
337 z.avail_in --;
338 z.total_in ++;
339 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 16 & 0xff0000L;
340 z.istate.mode = CHECK2;
341 case CHECK2:
342 if (z.avail_in == 0) {
343 return r;
344 }
345 r = f;
346
347 z.avail_in --;
348 z.total_in ++;
349 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 8 & 0xff00L;
350 z.istate.mode = CHECK1;
351 case CHECK1:
352 if (z.avail_in == 0) {
353 return r;
354 }
355 r = f;
356
357 z.avail_in --;
358 z.total_in ++;
359 z.istate.need += z.next_in[z.next_in_index ++] & 0xffL;
360
361 if ((int) z.istate.was[0] != (int) z.istate.need) {
362 z.istate.mode = BAD;
363 z.msg = "incorrect data check";
364 z.istate.marker = 5;
365 break;
366 }
367
368 z.istate.mode = DONE;
369 case DONE:
370 return JZlib.Z_STREAM_END;
371 case BAD:
372 return JZlib.Z_DATA_ERROR;
373 case GZIP_ID1:
374 if (z.avail_in == 0) {
375 return r;
376 }
377 r = f;
378 z.avail_in --;
379 z.total_in ++;
380
381 if ((z.next_in[z.next_in_index ++] & 0xff) != 31) {
382 z.istate.mode = BAD;
383 z.msg = "not a gzip stream";
384 z.istate.marker = 5;
385 break;
386 }
387 z.istate.mode = GZIP_ID2;
388 case GZIP_ID2:
389 if (z.avail_in == 0) {
390 return r;
391 }
392 r = f;
393 z.avail_in --;
394 z.total_in ++;
395
396 if ((z.next_in[z.next_in_index ++] & 0xff) != 139) {
397 z.istate.mode = BAD;
398 z.msg = "not a gzip stream";
399 z.istate.marker = 5;
400 break;
401 }
402 z.istate.mode = GZIP_CM;
403 case GZIP_CM:
404 if (z.avail_in == 0) {
405 return r;
406 }
407 r = f;
408 z.avail_in --;
409 z.total_in ++;
410
411 if ((z.next_in[z.next_in_index ++] & 0xff) != JZlib.Z_DEFLATED) {
412 z.istate.mode = BAD;
413 z.msg = "unknown compression method";
414 z.istate.marker = 5;
415 break;
416 }
417 z.istate.mode = GZIP_FLG;
418 case GZIP_FLG:
419 if (z.avail_in == 0) {
420 return r;
421 }
422 r = f;
423 z.avail_in --;
424 z.total_in ++;
425 gzipFlag = z.next_in[z.next_in_index ++] & 0xff;
426
427 if ((gzipFlag & 0xE2) != 0) {
428 z.istate.mode = BAD;
429 z.msg = "unsupported flag";
430 z.istate.marker = 5;
431 break;
432 }
433 gzipBytesToRead = 6;
434 z.istate.mode = GZIP_MTIME_XFL_OS;
435 case GZIP_MTIME_XFL_OS:
436 while (gzipBytesToRead > 0) {
437 if (z.avail_in == 0) {
438 return r;
439 }
440 r = f;
441 z.avail_in --;
442 z.total_in ++;
443 z.next_in_index ++;
444 gzipBytesToRead --;
445 }
446 z.istate.mode = GZIP_XLEN;
447 gzipXLen = 0;
448 gzipBytesToRead = 2;
449 case GZIP_XLEN:
450 if ((gzipFlag & 4) != 0) {
451 while (gzipBytesToRead > 0) {
452 if (z.avail_in == 0) {
453 return r;
454 }
455 r = f;
456 z.avail_in --;
457 z.total_in ++;
458 gzipXLen |= (z.next_in[z.next_in_index ++] & 0xff) << (1 - gzipBytesToRead) * 8;
459 gzipBytesToRead --;
460 }
461 gzipBytesToRead = gzipXLen;
462 z.istate.mode = GZIP_FEXTRA;
463 } else {
464 z.istate.mode = GZIP_FNAME;
465 break;
466 }
467 case GZIP_FEXTRA:
468 while (gzipBytesToRead > 0) {
469 if (z.avail_in == 0) {
470 return r;
471 }
472 r = f;
473 z.avail_in --;
474 z.total_in ++;
475 z.next_in_index ++;
476 gzipBytesToRead --;
477 }
478 z.istate.mode = GZIP_FNAME;
479 case GZIP_FNAME:
480 if ((gzipFlag & 8) != 0) {
481 do {
482 if (z.avail_in == 0) {
483 return r;
484 }
485 r = f;
486 z.avail_in --;
487 z.total_in ++;
488 } while (z.next_in[z.next_in_index ++] != 0);
489 }
490 z.istate.mode = GZIP_FCOMMENT;
491 case GZIP_FCOMMENT:
492 if ((gzipFlag & 16) != 0) {
493 do {
494 if (z.avail_in == 0) {
495 return r;
496 }
497 r = f;
498 z.avail_in --;
499 z.total_in ++;
500 } while (z.next_in[z.next_in_index ++] != 0);
501 }
502 gzipBytesToRead = 2;
503 z.istate.mode = GZIP_FHCRC;
504 case GZIP_FHCRC:
505 if ((gzipFlag & 2) != 0) {
506 while (gzipBytesToRead > 0) {
507 if (z.avail_in == 0) {
508 return r;
509 }
510 r = f;
511 z.avail_in --;
512 z.total_in ++;
513 z.next_in_index ++;
514 gzipBytesToRead --;
515 }
516 }
517 z.istate.mode = BLOCKS;
518 break;
519 case GZIP_CRC32:
520 while (gzipBytesToRead > 0) {
521 if (z.avail_in == 0) {
522 return r;
523 }
524 r = f;
525 z.avail_in --;
526 z.total_in ++;
527 gzipBytesToRead --;
528 z.istate.gzipCRC32 |= (z.next_in[z.next_in_index ++] & 0xff) << (3 - gzipBytesToRead) * 8;
529 }
530
531 if (z.crc32 != z.istate.gzipCRC32) {
532 z.istate.mode = BAD;
533 z.msg = "incorrect CRC32 checksum";
534 z.istate.marker = 5;
535 break;
536 }
537 gzipBytesToRead = 4;
538 z.istate.mode = GZIP_ISIZE;
539 case GZIP_ISIZE:
540 while (gzipBytesToRead > 0) {
541 if (z.avail_in == 0) {
542 return r;
543 }
544 r = f;
545 z.avail_in --;
546 z.total_in ++;
547 gzipBytesToRead --;
548 z.istate.gzipISize |= (z.next_in[z.next_in_index ++] & 0xff) << (3 - gzipBytesToRead) * 8;
549 }
550
551 if (gzipUncompressedBytes != z.istate.gzipISize) {
552 z.istate.mode = BAD;
553 z.msg = "incorrect ISIZE checksum";
554 z.istate.marker = 5;
555 break;
556 }
557
558 z.istate.mode = DONE;
559 break;
560 default:
561 return JZlib.Z_STREAM_ERROR;
562 }
563 }
564 }
565
566 int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength) {
567 int index = 0;
568 int length = dictLength;
569 if (z == null || z.istate == null || z.istate.mode != DICT0) {
570 return JZlib.Z_STREAM_ERROR;
571 }
572
573 if (Adler32.adler32(1L, dictionary, 0, dictLength) != z.adler) {
574 return JZlib.Z_DATA_ERROR;
575 }
576
577 z.adler = Adler32.adler32(0, null, 0, 0);
578
579 if (length >= 1 << z.istate.wbits) {
580 length = (1 << z.istate.wbits) - 1;
581 index = dictLength - length;
582 }
583 z.istate.blocks.set_dictionary(dictionary, index, length);
584 z.istate.mode = BLOCKS;
585 return JZlib.Z_OK;
586 }
587
588 private static final byte[] mark = { (byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff };
589
590 int inflateSync(ZStream z) {
591 int n;
592 int p;
593 int m;
594 long r, w;
595
596
597 if (z == null || z.istate == null) {
598 return JZlib.Z_STREAM_ERROR;
599 }
600 if (z.istate.mode != BAD) {
601 z.istate.mode = BAD;
602 z.istate.marker = 0;
603 }
604 if ((n = z.avail_in) == 0) {
605 return JZlib.Z_BUF_ERROR;
606 }
607 p = z.next_in_index;
608 m = z.istate.marker;
609
610
611 while (n != 0 && m < 4) {
612 if (z.next_in[p] == mark[m]) {
613 m ++;
614 } else if (z.next_in[p] != 0) {
615 m = 0;
616 } else {
617 m = 4 - m;
618 }
619 p ++;
620 n --;
621 }
622
623
624 z.total_in += p - z.next_in_index;
625 z.next_in_index = p;
626 z.avail_in = n;
627 z.istate.marker = m;
628
629
630 if (m != 4) {
631 return JZlib.Z_DATA_ERROR;
632 }
633 r = z.total_in;
634 w = z.total_out;
635 inflateReset(z);
636 z.total_in = r;
637 z.total_out = w;
638 z.istate.mode = BLOCKS;
639 return JZlib.Z_OK;
640 }
641 }