/*
* Copyright 2015 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2014 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf;
import io.netty.util.AsciiString;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.ThrowableUtil;
import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
final class HpackHuffmanDecoder implements ByteProcessor {
/* Scroll to the bottom! */
private static final byte HUFFMAN_COMPLETE = 1;
private static final byte HUFFMAN_EMIT_SYMBOL = 1 << 1;
private static final byte HUFFMAN_FAIL = 1 << 2;
private static final int HUFFMAN_COMPLETE_SHIFT = HUFFMAN_COMPLETE << 8;
private static final int HUFFMAN_EMIT_SYMBOL_SHIFT = HUFFMAN_EMIT_SYMBOL << 8;
private static final int HUFFMAN_FAIL_SHIFT = HUFFMAN_FAIL << 8;
/**
* A table of byte tuples (state, flags, output). They are packed together as:
*
* state<<16 + flags<<8 + output
*/
private static final int[] HUFFS = new int[] {
// Node 0 (Root Node, never emits symbols.)
(4 << 16) + (0 << 8) + 0,
(5 << 16) + (0 << 8) + 0,
(7 << 16) + (0 << 8) + 0,
(8 << 16) + (0 << 8) + 0,
(11 << 16) + (0 << 8) + 0,
(12 << 16) + (0 << 8) + 0,
(16 << 16) + (0 << 8) + 0,
(19 << 16) + (0 << 8) + 0,
(25 << 16) + (0 << 8) + 0,
(28 << 16) + (0 << 8) + 0,
(32 << 16) + (0 << 8) + 0,
(35 << 16) + (0 << 8) + 0,
(42 << 16) + (0 << 8) + 0,
(49 << 16) + (0 << 8) + 0,
(57 << 16) + (0 << 8) + 0,
(64 << 16) + (HUFFMAN_COMPLETE << 8) + 0,
// Node 1
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 48,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 49,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 50,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 97,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 99,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 101,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 105,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 111,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 115,
(0 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 116,
(13 << 16) + (0 << 8) + 0,
(14 << 16) + (0 << 8) + 0,
(17 << 16) + (0 << 8) + 0,
(18 << 16) + (0 << 8) + 0,
(20 << 16) + (0 << 8) + 0,
(21 << 16) + (0 << 8) + 0,
// Node 2
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 48,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 49,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 50,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 97,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 99,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 99,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 101,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 101,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 105,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 105,
(1 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 111,
(22 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 111,
// Node 3
(2 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(9 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(23 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(40 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 48,
(2 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(9 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(23 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(40 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 49,
(2 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(9 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(23 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(40 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 50,
(2 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(9 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(23 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(40 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 97,
// Node 4
(3 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(6 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(10 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(15 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(24 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(31 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(41 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 48,
(56 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 48,
(3 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(6 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(10 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(15 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(24 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(31 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(41 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 49,
(56 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 49,
// Node 5
(3 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(6 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(10 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(15 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(24 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(31 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(41 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 50,
(56 << 16) + ((HUFFMAN_COMPLETE | HUFFMAN_EMIT_SYMBOL) << 8) + 50,
(3 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(6 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(10 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(15 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,
(24 << 16) + (HUFFMAN_EMIT_SYMBOL << 8) + 97,