Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6a7d6fa8ac068a3c07185b4ae87e4706a1c0e332 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.client;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import java.util.zip.ZipException;

import org.eclipse.jetty.util.BufferUtil;

/**
 * {@link ContentDecoder} for the "gzip" encoding.
 */
public class GZIPContentDecoder implements ContentDecoder
{
    private final Inflater inflater = new Inflater(true);
    private final byte[] bytes;
    private byte[] output;
    private State state;
    private int size;
    private int value;
    private byte flags;

    public GZIPContentDecoder()
    {
        this(2048);
    }

    public GZIPContentDecoder(int bufferSize)
    {
        this.bytes = new byte[bufferSize];
        reset();
    }

    /**
     * {@inheritDoc}
     * <p>If the decoding did not produce any output, for example because it consumed gzip header
     * or trailer bytes, it returns a buffer with zero capacity.</p>
     * <p>This method never returns null.</p>
     * <p>The given {@code buffer}'s position will be modified to reflect the bytes consumed during
     * the decoding.</p>
     * <p>The decoding may be finished without consuming the buffer completely if the buffer contains
     * gzip bytes plus other bytes (either plain or gzipped).</p>
     */
    @Override
    public ByteBuffer decode(ByteBuffer buffer)
    {
        try
        {
            while (buffer.hasRemaining())
            {
                byte currByte = buffer.get();
                switch (state)
                {
                    case INITIAL:
                    {
                        buffer.position(buffer.position() - 1);
                        state = State.ID;
                        break;
                    }
                    case ID:
                    {
                        value += (currByte & 0xFF) << 8 * size;
                        ++size;
                        if (size == 2)
                        {
                            if (value != 0x8B1F)
                                throw new ZipException("Invalid gzip bytes");
                            state = State.CM;
                        }
                        break;
                    }
                    case CM:
                    {
                        if ((currByte & 0xFF) != 0x08)
                            throw new ZipException("Invalid gzip compression method");
                        state = State.FLG;
                        break;
                    }
                    case FLG:
                    {
                        flags = currByte;
                        state = State.MTIME;
                        size = 0;
                        value = 0;
                        break;
                    }
                    case MTIME:
                    {
                        // Skip the 4 MTIME bytes
                        ++size;
                        if (size == 4)
                            state = State.XFL;
                        break;
                    }
                    case XFL:
                    {
                        // Skip XFL
                        state = State.OS;
                        break;
                    }
                    case OS:
                    {
                        // Skip OS
                        state = State.FLAGS;
                        break;
                    }
                    case FLAGS:
                    {
                        buffer.position(buffer.position() - 1);
                        if ((flags & 0x04) == 0x04)
                        {
                            state = State.EXTRA_LENGTH;
                            size = 0;
                            value = 0;
                        }
                        else if ((flags & 0x08) == 0x08)
                            state = State.NAME;
                        else if ((flags & 0x10) == 0x10)
                            state = State.COMMENT;
                        else if ((flags & 0x2) == 0x2)
                        {
                            state = State.HCRC;
                            size = 0;
                            value = 0;
                        }
                        else
                            state = State.DATA;
                        break;
                    }
                    case EXTRA_LENGTH:
                    {
                        value += (currByte & 0xFF) << 8 * size;
                        ++size;
                        if (size == 2)
                            state = State.EXTRA;
                        break;
                    }
                    case EXTRA:
                    {
                        // Skip EXTRA bytes
                        --value;
                        if (value == 0)
                        {
                            // Clear the EXTRA flag and loop on the flags
                            flags &= ~0x04;
                            state = State.FLAGS;
                        }
                        break;
                    }
                    case NAME:
                    {
                        // Skip NAME bytes
                        if (currByte == 0)
                        {
                            // Clear the NAME flag and loop on the flags
                            flags &= ~0x08;
                            state = State.FLAGS;
                        }
                        break;
                    }
                    case COMMENT:
                    {
                        // Skip COMMENT bytes
                        if (currByte == 0)
                        {
                            // Clear the COMMENT flag and loop on the flags
                            flags &= ~0x10;
                            state = State.FLAGS;
                        }
                        break;
                    }
                    case HCRC:
                    {
                        // Skip HCRC
                        ++size;
                        if (size == 2)
                        {
                            // Clear the HCRC flag and loop on the flags
                            flags &= ~0x02;
                            state = State.FLAGS;
                        }
                        break;
                    }
                    case DATA:
                    {
                        buffer.position(buffer.position() - 1);
                        while (true)
                        {
                            int decoded = inflate(bytes);
                            if (decoded == 0)
                            {
                                if (inflater.needsInput())
                                {
                                    if (buffer.hasRemaining())
                                    {
                                        byte[] input = new byte[buffer.remaining()];
                                        buffer.get(input);
                                        inflater.setInput(input);
                                    }
                                    else
                                    {
                                        if (output != null)
                                        {
                                            ByteBuffer result = ByteBuffer.wrap(output);
                                            output = null;
                                            return result;
                                        }
                                        break;
                                    }
                                }
                                else if (inflater.finished())
                                {
                                    int remaining = inflater.getRemaining();
                                    buffer.position(buffer.limit() - remaining);
                                    state = State.CRC;
                                    size = 0;
                                    value = 0;
                                    break;
                                }
                                else
                                {
                                    throw new ZipException("Invalid inflater state");
                                }
                            }
                            else
                            {
                                if (output == null)
                                {
                                    // Save the inflated bytes and loop to see if we have finished
                                    output = Arrays.copyOf(bytes, decoded);
                                }
                                else
                                {
                                    // Accumulate inflated bytes and loop to see if we have finished
                                    byte[] newOutput = Arrays.copyOf(output, output.length + decoded);
                                    System.arraycopy(bytes, 0, newOutput, output.length, decoded);
                                    output = newOutput;
                                }
                            }
                        }
                        break;
                    }
                    case CRC:
                    {
                        value += (currByte & 0xFF) << 8 * size;
                        ++size;
                        if (size == 4)
                        {
                            // From RFC 1952, compliant decoders need not to verify the CRC
                            state = State.ISIZE;
                            size = 0;
                            value = 0;
                        }
                        break;
                    }
                    case ISIZE:
                    {
                        value += (currByte & 0xFF) << 8 * size;
                        ++size;
                        if (size == 4)
                        {
                            if (value != inflater.getBytesWritten())
                                throw new ZipException("Invalid input size");

                            ByteBuffer result = output == null ? BufferUtil.EMPTY_BUFFER : ByteBuffer.wrap(output);
                            reset();
                            return result;
                        }
                        break;
                    }
                    default:
                        throw new ZipException();
                }
            }
            return BufferUtil.EMPTY_BUFFER;
        }
        catch (ZipException x)
        {
            throw new RuntimeException(x);
        }
    }

    private int inflate(byte[] bytes) throws ZipException
    {
        try
        {
            return inflater.inflate(bytes);
        }
        catch (DataFormatException x)
        {
            throw new ZipException(x.getMessage());
        }
    }

    private void reset()
    {
        inflater.reset();
        Arrays.fill(bytes, (byte)0);
        output = null;
        state = State.INITIAL;
        size = 0;
        value = 0;
        flags = 0;
    }

    protected boolean isFinished()
    {
        return state == State.INITIAL;
    }

    /**
     * Specialized {@link ContentDecoder.Factory} for the "gzip" encoding.
     */
    public static class Factory extends ContentDecoder.Factory
    {
        private final int bufferSize;

        public Factory()
        {
            this(2048);
        }

        public Factory(int bufferSize)
        {
            super("gzip");
            this.bufferSize = bufferSize;
        }

        @Override
        public ContentDecoder newContentDecoder()
        {
            return new GZIPContentDecoder(bufferSize);
        }
    }

    private enum State
    {
        INITIAL, ID, CM, FLG, MTIME, XFL, OS, FLAGS, EXTRA_LENGTH, EXTRA, NAME, COMMENT, HCRC, DATA, CRC, ISIZE
    }
}

Back to the top