View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  /*
17  Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
18  
19  Redistribution and use in source and binary forms, with or without
20  modification, are permitted provided that the following conditions are met:
21  
22    1. Redistributions of source code must retain the above copyright notice,
23       this list of conditions and the following disclaimer.
24  
25    2. Redistributions in binary form must reproduce the above copyright
26       notice, this list of conditions and the following disclaimer in
27       the documentation and/or other materials provided with the distribution.
28  
29    3. The names of the authors may not be used to endorse or promote products
30       derived from this software without specific prior written permission.
31  
32  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
33  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
35  INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
36  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
38  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  /*
44   * This program is based on zlib-1.1.3, so all credit should go authors
45   * Jean-loup Gailly([email protected]) and Mark Adler([email protected])
46   * and contributors of zlib.
47   */
48  package org.jboss.netty.util.internal.jzlib;
49  
50  import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
51  
52  final class Inflate {
53  
54      private static final int METHOD = 0; // waiting for method byte
55      private static final int FLAG = 1; // waiting for flag byte
56      private static final int DICT4 = 2; // four dictionary check bytes to go
57      private static final int DICT3 = 3; // three dictionary check bytes to go
58      private static final int DICT2 = 4; // two dictionary check bytes to go
59      private static final int DICT1 = 5; // one dictionary check byte to go
60      private static final int DICT0 = 6; // waiting for inflateSetDictionary
61      private static final int BLOCKS = 7; // decompressing blocks
62      private static final int CHECK4 = 8; // four check bytes to go
63      private static final int CHECK3 = 9; // three check bytes to go
64      private static final int CHECK2 = 10; // two check bytes to go
65      private static final int CHECK1 = 11; // one check byte to go
66      private static final int DONE = 12; // finished check, done
67      private static final int BAD = 13; // got an error--stay here
68  
69      private static final int GZIP_ID1 = 14;
70      private static final int GZIP_ID2 = 15;
71      private static final int GZIP_CM = 16;
72      private static final int GZIP_FLG = 17;
73      private static final int GZIP_MTIME_XFL_OS = 18;
74      private static final int GZIP_XLEN = 19;
75      private static final int GZIP_FEXTRA = 20;
76      private static final int GZIP_FNAME = 21;
77      private static final int GZIP_FCOMMENT = 22;
78      private static final int GZIP_FHCRC = 23;
79      private static final int GZIP_CRC32 = 24;
80      private static final int GZIP_ISIZE = 25;
81  
82      private int mode; // current inflate mode
83      // mode dependent information
84      private int method; // if FLAGS, method byte
85      // if CHECK, check values to compare
86      private final long[] was = new long[1]; // computed check value
87      private long need; // stream check value
88      // if BAD, inflateSync's marker bytes count
89      private int marker;
90      // mode independent information
91      private WrapperType wrapperType;
92      private int wbits; // log2(window size)  (8..15, defaults to 15)
93      private InfBlocks blocks; // current inflate_blocks state
94      private int gzipFlag;
95      private int gzipBytesToRead;
96      private int gzipXLen;
97      private int gzipUncompressedBytes;
98      private int gzipCRC32;
99      private int gzipISize;
100 
101     private int inflateReset(ZStream z) {
102         if (z == null || z.istate == null) {
103             return JZlib.Z_STREAM_ERROR;
104         }
105 
106         z.total_in = z.total_out = 0;
107         z.msg = null;
108         switch (wrapperType) {
109         case NONE:
110             z.istate.mode = BLOCKS;
111             break;
112         case ZLIB:
113         case ZLIB_OR_NONE:
114             z.istate.mode = METHOD;
115             break;
116         case GZIP:
117             z.istate.mode = GZIP_ID1;
118             break;
119         }
120         z.istate.blocks.reset(z, null);
121         gzipUncompressedBytes = 0;
122         return JZlib.Z_OK;
123     }
124 
125     int inflateEnd(ZStream z) {
126         if (blocks != null) {
127             blocks.free(z);
128         }
129         blocks = null;
130         //    ZFREE(z, z->state);
131         return JZlib.Z_OK;
132     }
133 
134     int inflateInit(ZStream z, int w, WrapperType wrapperType) {
135         z.msg = null;
136         blocks = null;
137 
138         this.wrapperType = wrapperType;
139 
140         if (w < 0) {
141             throw new IllegalArgumentException("w: " + w);
142         }
143 
144         // set window size
145         if (w < 8 || w > 15) {
146             inflateEnd(z);
147             return JZlib.Z_STREAM_ERROR;
148         }
149         wbits = w;
150 
151         z.istate.blocks = new InfBlocks(
152                 z, z.istate.wrapperType == WrapperType.NONE? null : this,
153                 1 << w);
154 
155         // reset state
156         inflateReset(z);
157         return JZlib.Z_OK;
158     }
159 
160     int inflate(ZStream z, int f) {
161         int r;
162         int b;
163 
164         if (z == null || z.istate == null || z.next_in == null) {
165             return JZlib.Z_STREAM_ERROR;
166         }
167         f = f == JZlib.Z_FINISH? JZlib.Z_BUF_ERROR : JZlib.Z_OK;
168         r = JZlib.Z_BUF_ERROR;
169         while (true) {
170             //System.out.println("mode: "+z.istate.mode);
171             switch (z.istate.mode) {
172             case METHOD:
173 
174                 if (z.avail_in == 0) {
175                     return r;
176                 }
177 
178                 // Switch from zlib to none if necessary.
179                 if (z.istate.wrapperType == WrapperType.ZLIB_OR_NONE) {
180                     if ((z.next_in[z.next_in_index] & 0xf) != JZlib.Z_DEFLATED ||
181                         (z.next_in[z.next_in_index] >> 4) + 8 > z.istate.wbits) {
182                         z.istate.wrapperType = WrapperType.NONE;
183                         z.istate.mode = BLOCKS;
184                         break;
185                     } else {
186                         z.istate.wrapperType = WrapperType.ZLIB;
187                     }
188                 }
189 
190                 r = f;
191 
192                 z.avail_in --;
193                 z.total_in ++;
194                 if (((z.istate.method = z.next_in[z.next_in_index ++]) & 0xf) != JZlib.Z_DEFLATED) {
195                     z.istate.mode = BAD;
196                     z.msg = "unknown compression method";
197                     z.istate.marker = 5; // can't try inflateSync
198                     break;
199                 }
200                 if ((z.istate.method >> 4) + 8 > z.istate.wbits) {
201                     z.istate.mode = BAD;
202                     z.msg = "invalid window size";
203                     z.istate.marker = 5; // can't try inflateSync
204                     break;
205                 }
206                 z.istate.mode = FLAG;
207             case FLAG:
208 
209                 if (z.avail_in == 0) {
210                     return r;
211                 }
212                 r = f;
213 
214                 z.avail_in --;
215                 z.total_in ++;
216                 b = z.next_in[z.next_in_index ++] & 0xff;
217 
218                 if (((z.istate.method << 8) + b) % 31 != 0) {
219                     z.istate.mode = BAD;
220                     z.msg = "incorrect header check";
221                     z.istate.marker = 5; // can't try inflateSync
222                     break;
223                 }
224 
225                 if ((b & JZlib.PRESET_DICT) == 0) {
226                     z.istate.mode = BLOCKS;
227                     break;
228                 }
229                 z.istate.mode = DICT4;
230             case DICT4:
231 
232                 if (z.avail_in == 0) {
233                     return r;
234                 }
235                 r = f;
236 
237                 z.avail_in --;
238                 z.total_in ++;
239                 z.istate.need = (z.next_in[z.next_in_index ++] & 0xff) << 24 & 0xff000000L;
240                 z.istate.mode = DICT3;
241             case DICT3:
242 
243                 if (z.avail_in == 0) {
244                     return r;
245                 }
246                 r = f;
247 
248                 z.avail_in --;
249                 z.total_in ++;
250                 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 16 & 0xff0000L;
251                 z.istate.mode = DICT2;
252             case DICT2:
253 
254                 if (z.avail_in == 0) {
255                     return r;
256                 }
257                 r = f;
258 
259                 z.avail_in --;
260                 z.total_in ++;
261                 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 8 & 0xff00L;
262                 z.istate.mode = DICT1;
263             case DICT1:
264 
265                 if (z.avail_in == 0) {
266                     return r;
267                 }
268 
269                 z.avail_in --;
270                 z.total_in ++;
271                 z.istate.need += z.next_in[z.next_in_index ++] & 0xffL;
272                 z.adler = z.istate.need;
273                 z.istate.mode = DICT0;
274                 return JZlib.Z_NEED_DICT;
275             case DICT0:
276                 z.istate.mode = BAD;
277                 z.msg = "need dictionary";
278                 z.istate.marker = 0; // can try inflateSync
279                 return JZlib.Z_STREAM_ERROR;
280             case BLOCKS:
281                 int old_next_out_index = z.next_out_index;
282                 try {
283                     r = z.istate.blocks.proc(z, r);
284                     if (r == JZlib.Z_DATA_ERROR) {
285                         z.istate.mode = BAD;
286                         z.istate.marker = 0; // can try inflateSync
287                         break;
288                     }
289                     if (r == JZlib.Z_OK) {
290                         r = f;
291                     }
292                     if (r != JZlib.Z_STREAM_END) {
293                         return r;
294                     }
295                     r = f;
296                     z.istate.blocks.reset(z, z.istate.was);
297                 } finally {
298                     int decompressedBytes = z.next_out_index - old_next_out_index;
299                     gzipUncompressedBytes += decompressedBytes;
300                     z.crc32 = CRC32.crc32(z.crc32, z.next_out, old_next_out_index, decompressedBytes);
301                 }
302 
303                 if (z.istate.wrapperType == WrapperType.NONE) {
304                     z.istate.mode = DONE;
305                     break;
306                 } else if (z.istate.wrapperType == WrapperType.ZLIB) {
307                     z.istate.mode = CHECK4;
308                 } else if (z.istate.wrapperType == WrapperType.GZIP) {
309                     gzipCRC32 = 0;
310                     gzipISize = 0;
311                     gzipBytesToRead = 4;
312                     z.istate.mode = GZIP_CRC32;
313                     break;
314                 } else {
315                     z.istate.mode = BAD;
316                     z.msg = "unexpected state";
317                     z.istate.marker = 0;
318                     break;
319                 }
320             case CHECK4:
321                 if (z.avail_in == 0) {
322                     return r;
323                 }
324                 r = f;
325 
326                 z.avail_in --;
327                 z.total_in ++;
328                 z.istate.need = (z.next_in[z.next_in_index ++] & 0xff) << 24 & 0xff000000L;
329                 z.istate.mode = CHECK3;
330             case CHECK3:
331                 if (z.avail_in == 0) {
332                     return r;
333                 }
334                 r = f;
335 
336                 z.avail_in --;
337                 z.total_in ++;
338                 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 16 & 0xff0000L;
339                 z.istate.mode = CHECK2;
340             case CHECK2:
341                 if (z.avail_in == 0) {
342                     return r;
343                 }
344                 r = f;
345 
346                 z.avail_in --;
347                 z.total_in ++;
348                 z.istate.need += (z.next_in[z.next_in_index ++] & 0xff) << 8 & 0xff00L;
349                 z.istate.mode = CHECK1;
350             case CHECK1:
351                 if (z.avail_in == 0) {
352                     return r;
353                 }
354                 r = f;
355 
356                 z.avail_in --;
357                 z.total_in ++;
358                 z.istate.need += z.next_in[z.next_in_index ++] & 0xffL;
359 
360                 if ((int) z.istate.was[0] != (int) z.istate.need) {
361                     z.istate.mode = BAD;
362                     z.msg = "incorrect data check";
363                     z.istate.marker = 5; // can't try inflateSync
364                     break;
365                 }
366 
367                 z.istate.mode = DONE;
368             case DONE:
369                 return JZlib.Z_STREAM_END;
370             case BAD:
371                 return JZlib.Z_DATA_ERROR;
372             case GZIP_ID1:
373                 if (z.avail_in == 0) {
374                     return r;
375                 }
376                 r = f;
377                 z.avail_in --;
378                 z.total_in ++;
379 
380                 if ((z.next_in[z.next_in_index ++] & 0xff) != 31) {
381                     z.istate.mode = BAD;
382                     z.msg = "not a gzip stream";
383                     z.istate.marker = 5; // can't try inflateSync
384                     break;
385                 }
386                 z.istate.mode = GZIP_ID2;
387             case GZIP_ID2:
388                 if (z.avail_in == 0) {
389                     return r;
390                 }
391                 r = f;
392                 z.avail_in --;
393                 z.total_in ++;
394 
395                 if ((z.next_in[z.next_in_index ++] & 0xff) != 139) {
396                     z.istate.mode = BAD;
397                     z.msg = "not a gzip stream";
398                     z.istate.marker = 5; // can't try inflateSync
399                     break;
400                 }
401                 z.istate.mode = GZIP_CM;
402             case GZIP_CM:
403                 if (z.avail_in == 0) {
404                     return r;
405                 }
406                 r = f;
407                 z.avail_in --;
408                 z.total_in ++;
409 
410                 if ((z.next_in[z.next_in_index ++] & 0xff) != JZlib.Z_DEFLATED) {
411                     z.istate.mode = BAD;
412                     z.msg = "unknown compression method";
413                     z.istate.marker = 5; // can't try inflateSync
414                     break;
415                 }
416                 z.istate.mode = GZIP_FLG;
417             case GZIP_FLG:
418                 if (z.avail_in == 0) {
419                     return r;
420                 }
421                 r = f;
422                 z.avail_in --;
423                 z.total_in ++;
424                 gzipFlag = z.next_in[z.next_in_index ++] & 0xff;
425 
426                 if ((gzipFlag & 0xE2) != 0) {
427                     z.istate.mode = BAD;
428                     z.msg = "unsupported flag";
429                     z.istate.marker = 5; // can't try inflateSync
430                     break;
431                 }
432                 gzipBytesToRead = 6;
433                 z.istate.mode = GZIP_MTIME_XFL_OS;
434             case GZIP_MTIME_XFL_OS:
435                 while (gzipBytesToRead > 0) {
436                     if (z.avail_in == 0) {
437                         return r;
438                     }
439                     r = f;
440                     z.avail_in --;
441                     z.total_in ++;
442                     z.next_in_index ++;
443                     gzipBytesToRead --;
444                 }
445                 z.istate.mode = GZIP_XLEN;
446                 gzipXLen = 0;
447                 gzipBytesToRead = 2;
448             case GZIP_XLEN:
449                 if ((gzipFlag & 4) != 0) {
450                     while (gzipBytesToRead > 0) {
451                         if (z.avail_in == 0) {
452                             return r;
453                         }
454                         r = f;
455                         z.avail_in --;
456                         z.total_in ++;
457                         gzipXLen |= (z.next_in[z.next_in_index ++] & 0xff) << (1 - gzipBytesToRead) * 8;
458                         gzipBytesToRead --;
459                     }
460                     gzipBytesToRead = gzipXLen;
461                     z.istate.mode = GZIP_FEXTRA;
462                 } else {
463                     z.istate.mode = GZIP_FNAME;
464                     break;
465                 }
466             case GZIP_FEXTRA:
467                 while (gzipBytesToRead > 0) {
468                     if (z.avail_in == 0) {
469                         return r;
470                     }
471                     r = f;
472                     z.avail_in --;
473                     z.total_in ++;
474                     z.next_in_index ++;
475                     gzipBytesToRead --;
476                 }
477                 z.istate.mode = GZIP_FNAME;
478             case GZIP_FNAME:
479                 if ((gzipFlag & 8) != 0) {
480                     do {
481                         if (z.avail_in == 0) {
482                             return r;
483                         }
484                         r = f;
485                         z.avail_in --;
486                         z.total_in ++;
487                     } while (z.next_in[z.next_in_index ++] != 0);
488                 }
489                 z.istate.mode = GZIP_FCOMMENT;
490             case GZIP_FCOMMENT:
491                 if ((gzipFlag & 16) != 0) {
492                     do {
493                         if (z.avail_in == 0) {
494                             return r;
495                         }
496                         r = f;
497                         z.avail_in --;
498                         z.total_in ++;
499                     } while (z.next_in[z.next_in_index ++] != 0);
500                 }
501                 gzipBytesToRead = 2;
502                 z.istate.mode = GZIP_FHCRC;
503             case GZIP_FHCRC:
504                 if ((gzipFlag & 2) != 0) {
505                     while (gzipBytesToRead > 0) {
506                         if (z.avail_in == 0) {
507                             return r;
508                         }
509                         r = f;
510                         z.avail_in --;
511                         z.total_in ++;
512                         z.next_in_index ++;
513                         gzipBytesToRead --;
514                     }
515                 }
516                 z.istate.mode = BLOCKS;
517                 break;
518             case GZIP_CRC32:
519                 while (gzipBytesToRead > 0) {
520                     if (z.avail_in == 0) {
521                         return r;
522                     }
523                     r = f;
524                     z.avail_in --;
525                     z.total_in ++;
526                     gzipBytesToRead --;
527                     z.istate.gzipCRC32 |= (z.next_in[z.next_in_index ++] & 0xff) << (3 - gzipBytesToRead) * 8;
528                 }
529 
530                 if (z.crc32 != z.istate.gzipCRC32) {
531                     z.istate.mode = BAD;
532                     z.msg = "incorrect CRC32 checksum";
533                     z.istate.marker = 5; // can't try inflateSync
534                     break;
535                 }
536                 gzipBytesToRead = 4;
537                 z.istate.mode = GZIP_ISIZE;
538             case GZIP_ISIZE:
539                 while (gzipBytesToRead > 0) {
540                     if (z.avail_in == 0) {
541                         return r;
542                     }
543                     r = f;
544                     z.avail_in --;
545                     z.total_in ++;
546                     gzipBytesToRead --;
547                     z.istate.gzipISize |= (z.next_in[z.next_in_index ++] & 0xff) << (3 - gzipBytesToRead) * 8;
548                 }
549 
550                 if (gzipUncompressedBytes != z.istate.gzipISize) {
551                     z.istate.mode = BAD;
552                     z.msg = "incorrect ISIZE checksum";
553                     z.istate.marker = 5; // can't try inflateSync
554                     break;
555                 }
556 
557                 z.istate.mode = DONE;
558                 break;
559             default:
560                 return JZlib.Z_STREAM_ERROR;
561             }
562         }
563     }
564 
565     static int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength) {
566         int index = 0;
567         int length = dictLength;
568         if (z == null || z.istate == null || z.istate.mode != DICT0) {
569             return JZlib.Z_STREAM_ERROR;
570         }
571 
572         if (Adler32.adler32(1L, dictionary, 0, dictLength) != z.adler) {
573             return JZlib.Z_DATA_ERROR;
574         }
575 
576         z.adler = Adler32.adler32(0, null, 0, 0);
577 
578         if (length >= 1 << z.istate.wbits) {
579             length = (1 << z.istate.wbits) - 1;
580             index = dictLength - length;
581         }
582         z.istate.blocks.set_dictionary(dictionary, index, length);
583         z.istate.mode = BLOCKS;
584         return JZlib.Z_OK;
585     }
586 
587     private static final byte[] mark = { (byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff };
588 
589     int inflateSync(ZStream z) {
590         int n; // number of bytes to look at
591         int p; // pointer to bytes
592         int m; // number of marker bytes found in a row
593         long r, w; // temporaries to save total_in and total_out
594 
595         // set up
596         if (z == null || z.istate == null) {
597             return JZlib.Z_STREAM_ERROR;
598         }
599         if (z.istate.mode != BAD) {
600             z.istate.mode = BAD;
601             z.istate.marker = 0;
602         }
603         if ((n = z.avail_in) == 0) {
604             return JZlib.Z_BUF_ERROR;
605         }
606         p = z.next_in_index;
607         m = z.istate.marker;
608 
609         // search
610         while (n != 0 && m < 4) {
611             if (z.next_in[p] == mark[m]) {
612                 m ++;
613             } else if (z.next_in[p] != 0) {
614                 m = 0;
615             } else {
616                 m = 4 - m;
617             }
618             p ++;
619             n --;
620         }
621 
622         // restore
623         z.total_in += p - z.next_in_index;
624         z.next_in_index = p;
625         z.avail_in = n;
626         z.istate.marker = m;
627 
628         // return no joy or set up to restart on a new block
629         if (m != 4) {
630             return JZlib.Z_DATA_ERROR;
631         }
632         r = z.total_in;
633         w = z.total_out;
634         inflateReset(z);
635         z.total_in = r;
636         z.total_out = w;
637         z.istate.mode = BLOCKS;
638         return JZlib.Z_OK;
639     }
640 }