View Javadoc
1   /*
2    * Copyright 2013 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  package io.netty.handler.codec.compression;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  
21  import java.util.List;
22  import java.util.zip.CRC32;
23  import java.util.zip.DataFormatException;
24  import java.util.zip.Deflater;
25  import java.util.zip.Inflater;
26  
27  /**
28   * Decompress a {@link ByteBuf} using the inflate algorithm.
29   */
30  public class JdkZlibDecoder extends ZlibDecoder {
31      private static final int FHCRC = 0x02;
32      private static final int FEXTRA = 0x04;
33      private static final int FNAME = 0x08;
34      private static final int FCOMMENT = 0x10;
35      private static final int FRESERVED = 0xE0;
36  
37      private Inflater inflater;
38      private final byte[] dictionary;
39  
40      // GZIP related
41      private final CRC32 crc;
42  
43      private enum GzipState {
44          HEADER_START,
45          HEADER_END,
46          FLG_READ,
47          XLEN_READ,
48          SKIP_FNAME,
49          SKIP_COMMENT,
50          PROCESS_FHCRC,
51          FOOTER_START,
52      }
53  
54      private GzipState gzipState = GzipState.HEADER_START;
55      private int flags = -1;
56      private int xlen = -1;
57  
58      private volatile boolean finished;
59  
60      private boolean decideZlibOrNone;
61  
62      /**
63       * Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB}).
64       */
65      public JdkZlibDecoder() {
66          this(ZlibWrapper.ZLIB, null);
67      }
68  
69      /**
70       * Creates a new instance with the specified preset dictionary. The wrapper
71       * is always {@link ZlibWrapper#ZLIB} because it is the only format that
72       * supports the preset dictionary.
73       */
74      public JdkZlibDecoder(byte[] dictionary) {
75          this(ZlibWrapper.ZLIB, dictionary);
76      }
77  
78      /**
79       * Creates a new instance with the specified wrapper.
80       * Be aware that only {@link ZlibWrapper#GZIP}, {@link ZlibWrapper#ZLIB} and {@link ZlibWrapper#NONE} are
81       * supported atm.
82       */
83      public JdkZlibDecoder(ZlibWrapper wrapper) {
84          this(wrapper, null);
85      }
86  
87      private JdkZlibDecoder(ZlibWrapper wrapper, byte[] dictionary) {
88          if (wrapper == null) {
89              throw new NullPointerException("wrapper");
90          }
91          switch (wrapper) {
92              case GZIP:
93                  inflater = new Inflater(true);
94                  crc = new CRC32();
95                  break;
96              case NONE:
97                  inflater = new Inflater(true);
98                  crc = null;
99                  break;
100             case ZLIB:
101                 inflater = new Inflater();
102                 crc = null;
103                 break;
104             case ZLIB_OR_NONE:
105                 // Postpone the decision until decode(...) is called.
106                 decideZlibOrNone = true;
107                 crc = null;
108                 break;
109             default:
110                 throw new IllegalArgumentException("Only GZIP or ZLIB is supported, but you used " + wrapper);
111         }
112         this.dictionary = dictionary;
113     }
114 
115     @Override
116     public boolean isClosed() {
117         return finished;
118     }
119 
120     @Override
121     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
122         if (finished) {
123             // Skip data received after finished.
124             in.skipBytes(in.readableBytes());
125             return;
126         }
127 
128         int readableBytes = in.readableBytes();
129         if (readableBytes == 0) {
130             return;
131         }
132 
133         if (decideZlibOrNone) {
134             // First two bytes are needed to decide if it's a ZLIB stream.
135             if (readableBytes < 2) {
136                 return;
137             }
138 
139             boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
140             inflater = new Inflater(nowrap);
141             decideZlibOrNone = false;
142         }
143 
144         if (crc != null) {
145             switch (gzipState) {
146                 case FOOTER_START:
147                     if (readGZIPFooter(in)) {
148                         finished = true;
149                     }
150                     return;
151                 default:
152                     if (gzipState != GzipState.HEADER_END) {
153                         if (!readGZIPHeader(in)) {
154                             return;
155                         }
156                     }
157             }
158             // Some bytes may have been consumed, and so we must re-set the number of readable bytes.
159             readableBytes = in.readableBytes();
160         }
161 
162         if (in.hasArray()) {
163             inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
164         } else {
165             byte[] array = new byte[readableBytes];
166             in.getBytes(in.readerIndex(), array);
167             inflater.setInput(array);
168         }
169 
170         ByteBuf decompressed = ctx.alloc().heapBuffer(inflater.getRemaining() << 1);
171         try {
172             boolean readFooter = false;
173             while (!inflater.needsInput()) {
174                 byte[] outArray = decompressed.array();
175                 int writerIndex = decompressed.writerIndex();
176                 int outIndex = decompressed.arrayOffset() + writerIndex;
177                 int outputLength = inflater.inflate(outArray, outIndex, decompressed.writableBytes());
178                 if (outputLength > 0) {
179                     decompressed.writerIndex(writerIndex + outputLength);
180                     if (crc != null) {
181                         crc.update(outArray, outIndex, outputLength);
182                     }
183                 } else {
184                     if (inflater.needsDictionary()) {
185                         if (dictionary == null) {
186                             throw new DecompressionException(
187                                     "decompression failure, unable to set dictionary as non was specified");
188                         }
189                         inflater.setDictionary(dictionary);
190                     }
191                 }
192 
193                 if (inflater.finished()) {
194                     if (crc == null) {
195                         finished = true; // Do not decode anymore.
196                     } else {
197                         readFooter = true;
198                     }
199                     break;
200                 } else {
201                     decompressed.ensureWritable(inflater.getRemaining() << 1);
202                 }
203             }
204 
205             in.skipBytes(readableBytes - inflater.getRemaining());
206 
207             if (readFooter) {
208                 gzipState = GzipState.FOOTER_START;
209                 if (readGZIPFooter(in)) {
210                     finished = true;
211                 }
212             }
213         } catch (DataFormatException e) {
214             throw new DecompressionException("decompression failure", e);
215         } finally {
216 
217             if (decompressed.isReadable()) {
218                 out.add(decompressed);
219             } else {
220                 decompressed.release();
221             }
222         }
223     }
224 
225     @Override
226     protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
227         super.handlerRemoved0(ctx);
228         if (inflater != null) {
229             inflater.end();
230         }
231     }
232 
233     private boolean readGZIPHeader(ByteBuf in) {
234         switch (gzipState) {
235             case HEADER_START:
236                 if (in.readableBytes() < 10) {
237                     return false;
238                 }
239                 // read magic numbers
240                 int magic0 = in.readByte();
241                 int magic1 = in.readByte();
242 
243                 if (magic0 != 31) {
244                     throw new DecompressionException("Input is not in the GZIP format");
245                 }
246                 crc.update(magic0);
247                 crc.update(magic1);
248 
249                 int method = in.readUnsignedByte();
250                 if (method != Deflater.DEFLATED) {
251                     throw new DecompressionException("Unsupported compression method "
252                             + method + " in the GZIP header");
253                 }
254                 crc.update(method);
255 
256                 flags = in.readUnsignedByte();
257                 crc.update(flags);
258 
259                 if ((flags & FRESERVED) != 0) {
260                     throw new DecompressionException(
261                             "Reserved flags are set in the GZIP header");
262                 }
263 
264                 // mtime (int)
265                 crc.update(in.readByte());
266                 crc.update(in.readByte());
267                 crc.update(in.readByte());
268                 crc.update(in.readByte());
269 
270                 crc.update(in.readUnsignedByte()); // extra flags
271                 crc.update(in.readUnsignedByte()); // operating system
272 
273                 gzipState = GzipState.FLG_READ;
274                 // fall through
275             case FLG_READ:
276                 if ((flags & FEXTRA) != 0) {
277                     if (in.readableBytes() < 2) {
278                         return false;
279                     }
280                     int xlen1 = in.readUnsignedByte();
281                     int xlen2 = in.readUnsignedByte();
282                     crc.update(xlen1);
283                     crc.update(xlen2);
284 
285                     xlen |= xlen1 << 8 | xlen2;
286                 }
287                 gzipState = GzipState.XLEN_READ;
288                 // fall through
289             case XLEN_READ:
290                 if (xlen != -1) {
291                     if (in.readableBytes() < xlen) {
292                         return false;
293                     }
294                     byte[] xtra = new byte[xlen];
295                     in.readBytes(xtra);
296                     crc.update(xtra);
297                 }
298                 gzipState = GzipState.SKIP_FNAME;
299                 // fall through
300             case SKIP_FNAME:
301                 if ((flags & FNAME) != 0) {
302                     if (!in.isReadable()) {
303                         return false;
304                     }
305                     do {
306                         int b = in.readUnsignedByte();
307                         crc.update(b);
308                         if (b == 0x00) {
309                             break;
310                         }
311                     } while (in.isReadable());
312                 }
313                 gzipState = GzipState.SKIP_COMMENT;
314                 // fall through
315             case SKIP_COMMENT:
316                 if ((flags & FCOMMENT) != 0) {
317                     if (!in.isReadable()) {
318                         return false;
319                     }
320                     do {
321                         int b = in.readUnsignedByte();
322                         crc.update(b);
323                         if (b == 0x00) {
324                             break;
325                         }
326                     } while (in.isReadable());
327                 }
328                 gzipState = GzipState.PROCESS_FHCRC;
329                 // fall through
330             case PROCESS_FHCRC:
331                 if ((flags & FHCRC) != 0) {
332                     if (in.readableBytes() < 4) {
333                         return false;
334                     }
335                     verifyCrc(in);
336                 }
337                 crc.reset();
338                 gzipState = GzipState.HEADER_END;
339                 // fall through
340             case HEADER_END:
341                 return true;
342             default:
343                 throw new IllegalStateException();
344         }
345     }
346 
347     private boolean readGZIPFooter(ByteBuf buf) {
348         if (buf.readableBytes() < 8) {
349             return false;
350         }
351 
352         verifyCrc(buf);
353 
354         // read ISIZE and verify
355         int dataLength = 0;
356         for (int i = 0; i < 4; ++i) {
357             dataLength |= buf.readUnsignedByte() << i * 8;
358         }
359         int readLength = inflater.getTotalOut();
360         if (dataLength != readLength) {
361             throw new DecompressionException(
362                     "Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength);
363         }
364         return true;
365     }
366 
367     private void verifyCrc(ByteBuf in) {
368         long crcValue = 0;
369         for (int i = 0; i < 4; ++i) {
370             crcValue |= (long) in.readUnsignedByte() << i * 8;
371         }
372         long readCrc = crc.getValue();
373         if (crcValue != readCrc) {
374             throw new DecompressionException(
375                     "CRC value mismatch. Expected: " + crcValue + ", Got: " + readCrc);
376         }
377     }
378 
379     /*
380      * Returns true if the cmf_flg parameter (think: first two bytes of a zlib stream)
381      * indicates that this is a zlib stream.
382      * <p>
383      * You can lookup the details in the ZLIB RFC:
384      * <a href="http://tools.ietf.org/html/rfc1950#section-2.2">RFC 1950</a>.
385      */
386     private static boolean looksLikeZlib(short cmf_flg) {
387         return (cmf_flg & 0x7800) == 0x7800 &&
388                 cmf_flg % 31 == 0;
389     }
390 }