View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
46   * and contributors of zlib.
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; // waiting for method byte
56      private static final int FLAG = 1; // waiting for flag byte
57      private static final int DICT4 = 2; // four dictionary check bytes to go
58      private static final int DICT3 = 3; // three dictionary check bytes to go
59      private static final int DICT2 = 4; // two dictionary check bytes to go
60      private static final int DICT1 = 5; // one dictionary check byte to go
61      private static final int DICT0 = 6; // waiting for inflateSetDictionary
62      private static final int BLOCKS = 7; // decompressing blocks
63      private static final int CHECK4 = 8; // four check bytes to go
64      private static final int CHECK3 = 9; // three check bytes to go
65      private static final int CHECK2 = 10; // two check bytes to go
66      private static final int CHECK1 = 11; // one check byte to go
67      private static final int DONE = 12; // finished check, done
68      private static final int BAD = 13; // got an error--stay here
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; // current inflate mode
84      // mode dependent information
85      private int method; // if FLAGS, method byte
86      // if CHECK, check values to compare
87      private final long[] was = new long[1]; // computed check value
88      private long need; // stream check value
89      // if BAD, inflateSync's marker bytes count
90      private int marker;
91      // mode independent information
92      private WrapperType wrapperType;
93      private int wbits; // log2(window size)  (8..15, defaults to 15)
94      private InfBlocks blocks; // current inflate_blocks state
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         //    ZFREE(z, z->state);
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         // set window size
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         // reset state
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             //System.out.println("mode: "+z.istate.mode);
172             switch (z.istate.mode) {
173             case METHOD:
174 
175                 if (z.avail_in == 0) {
176                     return r;
177                 }
178 
179                 // Switch from zlib to none if necessary.
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; // can't try inflateSync
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; // can't try inflateSync
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; // can't try inflateSync
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; // can try inflateSync
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; // can try inflateSync
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; // 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     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 }