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 ByteBufChecksum 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 = ByteBufChecksum.wrapChecksum(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, in.readerIndex(), 4);
266                 in.skipBytes(4);
267 
268                 crc.update(in.readUnsignedByte()); // extra flags
269                 crc.update(in.readUnsignedByte()); // operating system
270 
271                 gzipState = GzipState.FLG_READ;
272                 // fall through
273             case FLG_READ:
274                 if ((flags & FEXTRA) != 0) {
275                     if (in.readableBytes() < 2) {
276                         return false;
277                     }
278                     int xlen1 = in.readUnsignedByte();
279                     int xlen2 = in.readUnsignedByte();
280                     crc.update(xlen1);
281                     crc.update(xlen2);
282 
283                     xlen |= xlen1 << 8 | xlen2;
284                 }
285                 gzipState = GzipState.XLEN_READ;
286                 // fall through
287             case XLEN_READ:
288                 if (xlen != -1) {
289                     if (in.readableBytes() < xlen) {
290                         return false;
291                     }
292                     crc.update(in, in.readerIndex(), xlen);
293                     in.skipBytes(xlen);
294                 }
295                 gzipState = GzipState.SKIP_FNAME;
296                 // fall through
297             case SKIP_FNAME:
298                 if ((flags & FNAME) != 0) {
299                     if (!in.isReadable()) {
300                         return false;
301                     }
302                     do {
303                         int b = in.readUnsignedByte();
304                         crc.update(b);
305                         if (b == 0x00) {
306                             break;
307                         }
308                     } while (in.isReadable());
309                 }
310                 gzipState = GzipState.SKIP_COMMENT;
311                 // fall through
312             case SKIP_COMMENT:
313                 if ((flags & FCOMMENT) != 0) {
314                     if (!in.isReadable()) {
315                         return false;
316                     }
317                     do {
318                         int b = in.readUnsignedByte();
319                         crc.update(b);
320                         if (b == 0x00) {
321                             break;
322                         }
323                     } while (in.isReadable());
324                 }
325                 gzipState = GzipState.PROCESS_FHCRC;
326                 // fall through
327             case PROCESS_FHCRC:
328                 if ((flags & FHCRC) != 0) {
329                     if (in.readableBytes() < 4) {
330                         return false;
331                     }
332                     verifyCrc(in);
333                 }
334                 crc.reset();
335                 gzipState = GzipState.HEADER_END;
336                 // fall through
337             case HEADER_END:
338                 return true;
339             default:
340                 throw new IllegalStateException();
341         }
342     }
343 
344     private boolean readGZIPFooter(ByteBuf buf) {
345         if (buf.readableBytes() < 8) {
346             return false;
347         }
348 
349         verifyCrc(buf);
350 
351         // read ISIZE and verify
352         int dataLength = 0;
353         for (int i = 0; i < 4; ++i) {
354             dataLength |= buf.readUnsignedByte() << i * 8;
355         }
356         int readLength = inflater.getTotalOut();
357         if (dataLength != readLength) {
358             throw new DecompressionException(
359                     "Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength);
360         }
361         return true;
362     }
363 
364     private void verifyCrc(ByteBuf in) {
365         long crcValue = 0;
366         for (int i = 0; i < 4; ++i) {
367             crcValue |= (long) in.readUnsignedByte() << i * 8;
368         }
369         long readCrc = crc.getValue();
370         if (crcValue != readCrc) {
371             throw new DecompressionException(
372                     "CRC value mismatch. Expected: " + crcValue + ", Got: " + readCrc);
373         }
374     }
375 
376     /*
377      * Returns true if the cmf_flg parameter (think: first two bytes of a zlib stream)
378      * indicates that this is a zlib stream.
379      * <p>
380      * You can lookup the details in the ZLIB RFC:
381      * <a href="http://tools.ietf.org/html/rfc1950#section-2.2">RFC 1950</a>.
382      */
383     private static boolean looksLikeZlib(short cmf_flg) {
384         return (cmf_flg & 0x7800) == 0x7800 &&
385                 cmf_flg % 31 == 0;
386     }
387 }