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                 }
307                 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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
555                     break;
556                 }
557 
558                 z.istate.mode = DONE;
559                 break;
560             default:
561                 return JZlib.Z_STREAM_ERROR;
562             }
563         }
564     }
565 
566     static 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; // number of bytes to look at
592         int p; // pointer to bytes
593         int m; // number of marker bytes found in a row
594         long r, w; // temporaries to save total_in and total_out
595 
596         // set up
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         // search
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         // restore
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         // return no joy or set up to restart on a new block
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 }