|
| 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 | +/** |
| 29 | + * Decompress a {@link ByteBuf} using the inflate algorithm. |
| 30 | + */ |
| 31 | +public class JdkZlibDecoder extends ZlibDecoder { |
| 32 | + private static final int FHCRC = 0x02; |
| 33 | + private static final int FEXTRA = 0x04; |
| 34 | + private static final int FNAME = 0x08; |
| 35 | + private static final int FCOMMENT = 0x10; |
| 36 | + private static final int FRESERVED = 0xE0; |
| 37 | + |
| 38 | + private final Inflater inflater; |
| 39 | + private final byte[] dictionary; |
| 40 | + |
| 41 | + // GZIP related |
| 42 | + private final CRC32 crc; |
| 43 | + |
| 44 | + private enum GzipState { |
| 45 | + HEADER_START, |
| 46 | + HEADER_END, |
| 47 | + FLG_READ, |
| 48 | + XLEN_READ, |
| 49 | + SKIP_FNAME, |
| 50 | + SKIP_COMMENT, |
| 51 | + PROCESS_FHCRC, |
| 52 | + FOOTER_START, |
| 53 | + } |
| 54 | + |
| 55 | + private GzipState gzipState = GzipState.HEADER_START; |
| 56 | + private int flags = -1; |
| 57 | + private int xlen = -1; |
| 58 | + |
| 59 | + private volatile boolean finished; |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB}). |
| 63 | + */ |
| 64 | + public JdkZlibDecoder() { |
| 65 | + this(ZlibWrapper.ZLIB, null); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new instance with the specified preset dictionary. The wrapper |
| 70 | + * is always {@link ZlibWrapper#ZLIB} because it is the only format that |
| 71 | + * supports the preset dictionary. |
| 72 | + */ |
| 73 | + public JdkZlibDecoder(byte[] dictionary) { |
| 74 | + this(ZlibWrapper.ZLIB, dictionary); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a new instance with the specified wrapper. |
| 79 | + * Be aware that only {@link ZlibWrapper#GZIP}, {@link ZlibWrapper#ZLIB} and {@link ZlibWrapper#NONE} are |
| 80 | + * supported atm. |
| 81 | + */ |
| 82 | + public JdkZlibDecoder(ZlibWrapper wrapper) { |
| 83 | + this(wrapper, null); |
| 84 | + } |
| 85 | + |
| 86 | + private JdkZlibDecoder(ZlibWrapper wrapper, byte[] dictionary) { |
| 87 | + if (wrapper == null) { |
| 88 | + throw new NullPointerException("wrapper"); |
| 89 | + } |
| 90 | + switch (wrapper) { |
| 91 | + case GZIP: |
| 92 | + inflater = new Inflater(true); |
| 93 | + crc = new CRC32(); |
| 94 | + break; |
| 95 | + case NONE: |
| 96 | + inflater = new Inflater(true); |
| 97 | + crc = null; |
| 98 | + break; |
| 99 | + case ZLIB: |
| 100 | + inflater = new Inflater(); |
| 101 | + crc = null; |
| 102 | + break; |
| 103 | + default: |
| 104 | + throw new IllegalArgumentException("Only GZIP or ZLIB is supported, but you used " + wrapper); |
| 105 | + } |
| 106 | + this.dictionary = dictionary; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean isClosed() { |
| 111 | + return finished; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { |
| 116 | + if (!in.isReadable() && finished) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (crc != null) { |
| 121 | + switch (gzipState) { |
| 122 | + case FOOTER_START: |
| 123 | + if (readGZIPFooter(in)) { |
| 124 | + finished = true; |
| 125 | + } |
| 126 | + return; |
| 127 | + default: |
| 128 | + if (gzipState != GzipState.HEADER_END) { |
| 129 | + if (!readGZIPHeader(in)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + int readableBytes = in.readableBytes(); |
| 137 | + if (in.hasArray()) { |
| 138 | + inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), in.readableBytes()); |
| 139 | + } else { |
| 140 | + byte[] array = new byte[in.readableBytes()]; |
| 141 | + in.getBytes(in.readerIndex(), array); |
| 142 | + inflater.setInput(array); |
| 143 | + } |
| 144 | + |
| 145 | + int maxOutputLength = inflater.getRemaining() << 1; |
| 146 | + ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength); |
| 147 | + try { |
| 148 | + boolean readFooter = false; |
| 149 | + while (!inflater.needsInput()) { |
| 150 | + byte[] outArray = decompressed.array(); |
| 151 | + int outIndex = decompressed.arrayOffset() + decompressed.writerIndex(); |
| 152 | + int length = outArray.length - outIndex; |
| 153 | + |
| 154 | + int outputLength = inflater.inflate(outArray, outIndex, length); |
| 155 | + |
| 156 | + if (outputLength > 0) { |
| 157 | + decompressed.writerIndex(decompressed.writerIndex() + outputLength); |
| 158 | + if (crc != null) { |
| 159 | + crc.update(outArray, outIndex, length); |
| 160 | + } |
| 161 | + } else { |
| 162 | + if (inflater.needsDictionary()) { |
| 163 | + if (dictionary == null) { |
| 164 | + throw new DecompressionException( |
| 165 | + "decompression failure, unable to set dictionary as non was specified"); |
| 166 | + } |
| 167 | + inflater.setDictionary(dictionary); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (inflater.finished()) { |
| 172 | + if (crc == null) { |
| 173 | + finished = true; // Do not decode anymore. |
| 174 | + } else { |
| 175 | + readFooter = true; |
| 176 | + } |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + in.skipBytes(readableBytes - inflater.getRemaining()); |
| 182 | + |
| 183 | + if (readFooter) { |
| 184 | + gzipState = GzipState.FOOTER_START; |
| 185 | + if (readGZIPFooter(in)) { |
| 186 | + finished = true; |
| 187 | + } |
| 188 | + } |
| 189 | + } catch (DataFormatException e) { |
| 190 | + throw new DecompressionException("decompression failure", e); |
| 191 | + } finally { |
| 192 | + |
| 193 | + if (decompressed.isReadable()) { |
| 194 | + out.add(decompressed); |
| 195 | + } else { |
| 196 | + decompressed.release(); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception { |
| 203 | + super.handlerRemoved0(ctx); |
| 204 | + inflater.end(); |
| 205 | + } |
| 206 | + |
| 207 | + private boolean readGZIPHeader(ByteBuf in) { |
| 208 | + switch (gzipState) { |
| 209 | + case HEADER_START: |
| 210 | + if (in.readableBytes() < 10) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + // read magic numbers |
| 214 | + int magic0 = in.readByte(); |
| 215 | + int magic1 = in.readByte(); |
| 216 | + |
| 217 | + if (magic0 != 31) { |
| 218 | + throw new CompressionException("Input is not in the GZIP format"); |
| 219 | + } |
| 220 | + crc.update(magic0); |
| 221 | + crc.update(magic1); |
| 222 | + |
| 223 | + int method = in.readUnsignedByte(); |
| 224 | + if (method != Deflater.DEFLATED) { |
| 225 | + throw new CompressionException("Unsupported compression method " |
| 226 | + + method + " in the GZIP header"); |
| 227 | + } |
| 228 | + crc.update(method); |
| 229 | + |
| 230 | + flags = in.readUnsignedByte(); |
| 231 | + crc.update(flags); |
| 232 | + |
| 233 | + if ((flags & FRESERVED) != 0) { |
| 234 | + throw new CompressionException( |
| 235 | + "Reserved flags are set in the GZIP header"); |
| 236 | + } |
| 237 | + |
| 238 | + // mtime (int) |
| 239 | + crc.update(in.readByte()); |
| 240 | + crc.update(in.readByte()); |
| 241 | + crc.update(in.readByte()); |
| 242 | + crc.update(in.readByte()); |
| 243 | + |
| 244 | + crc.update(in.readUnsignedByte()); // extra flags |
| 245 | + crc.update(in.readUnsignedByte()); // operating system |
| 246 | + |
| 247 | + gzipState = GzipState.FLG_READ; |
| 248 | + case FLG_READ: |
| 249 | + if ((flags & FEXTRA) != 0) { |
| 250 | + if (in.readableBytes() < 2) { |
| 251 | + return false; |
| 252 | + } |
| 253 | + int xlen1 = in.readUnsignedByte(); |
| 254 | + int xlen2 = in.readUnsignedByte(); |
| 255 | + crc.update(xlen1); |
| 256 | + crc.update(xlen2); |
| 257 | + |
| 258 | + xlen |= xlen1 << 8 | xlen2; |
| 259 | + } |
| 260 | + gzipState = GzipState.XLEN_READ; |
| 261 | + case XLEN_READ: |
| 262 | + if (xlen != -1) { |
| 263 | + if (in.readableBytes() < xlen) { |
| 264 | + return false; |
| 265 | + } |
| 266 | + byte[] xtra = new byte[xlen]; |
| 267 | + in.readBytes(xtra); |
| 268 | + crc.update(xtra); |
| 269 | + } |
| 270 | + gzipState = GzipState.SKIP_FNAME; |
| 271 | + case SKIP_FNAME: |
| 272 | + if ((flags & FNAME) != 0) { |
| 273 | + if (!in.isReadable()) { |
| 274 | + return false; |
| 275 | + } |
| 276 | + do { |
| 277 | + int b = in.readUnsignedByte(); |
| 278 | + crc.update(b); |
| 279 | + if (b == 0x00) { |
| 280 | + break; |
| 281 | + } |
| 282 | + } while (in.isReadable()); |
| 283 | + } |
| 284 | + gzipState = GzipState.SKIP_COMMENT; |
| 285 | + case SKIP_COMMENT: |
| 286 | + if ((flags & FCOMMENT) != 0) { |
| 287 | + if (!in.isReadable()) { |
| 288 | + return false; |
| 289 | + } |
| 290 | + do { |
| 291 | + int b = in.readUnsignedByte(); |
| 292 | + crc.update(b); |
| 293 | + if (b == 0x00) { |
| 294 | + break; |
| 295 | + } |
| 296 | + } while (in.isReadable()); |
| 297 | + } |
| 298 | + gzipState = GzipState.PROCESS_FHCRC; |
| 299 | + case PROCESS_FHCRC: |
| 300 | + if ((flags & FHCRC) != 0) { |
| 301 | + if (!in.isReadable()) { |
| 302 | + return false; |
| 303 | + } |
| 304 | + int headerCrc = in.readShort(); |
| 305 | + int readCrc = (int) crc.getValue() & 0xffff; |
| 306 | + if (headerCrc != readCrc) { |
| 307 | + throw new CompressionException( |
| 308 | + "Header CRC value missmatch. Expected: " + headerCrc + ", Got: " + readCrc); |
| 309 | + } |
| 310 | + } |
| 311 | + crc.reset(); |
| 312 | + gzipState = GzipState.HEADER_END; |
| 313 | + case HEADER_END: |
| 314 | + return true; |
| 315 | + default: |
| 316 | + throw new IllegalStateException(); |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + private boolean readGZIPFooter(ByteBuf buf) { |
| 321 | + if (buf.readableBytes() < 8) { |
| 322 | + return false; |
| 323 | + } |
| 324 | + int dataCrc = buf.readInt(); |
| 325 | + int readCrc = (int) crc.getValue() & 0xffff; |
| 326 | + if (dataCrc != readCrc) { |
| 327 | + throw new CompressionException( |
| 328 | + "Data CRC value missmatch. Expected: " + dataCrc + ", Got: " + readCrc); |
| 329 | + } |
| 330 | + |
| 331 | + int dataLength = buf.readInt(); |
| 332 | + int readLength = inflater.getTotalOut(); |
| 333 | + if (dataLength != readLength) { |
| 334 | + throw new CompressionException( |
| 335 | + "Number of bytes missmatch. Expected: " + dataLength + ", Got: " + readLength); |
| 336 | + } |
| 337 | + return true; |
| 338 | + } |
| 339 | +} |
0 commit comments