func decodeSymbol(bits uint32, table []huffmanCode, br *bitReader) uint32 {
	table = table[bits&huffmanTableMask:]
	if table[0].bits > huffmanTableBits {
		var nbits uint32 = uint32(table[0].bits) - huffmanTableBits
		dropBits(br, huffmanTableBits)
		table = table[uint32(table[0].value)+((bits>>huffmanTableBits)&bitMask(nbits)):]
	}
	dropBits(br, uint32(table[0].bits))
	return uint32(table[0].value)
}
/* Decodes the Huffman code.
This method doesn't read data from the bit reader, BUT drops the amount of
bits that correspond to the decoded symbol.
bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits.
package brotli
const (
	decoderResultError           = 0
	decoderResultSuccess         = 1
	decoderResultNeedsMoreInput  = 2
	decoderResultNeedsMoreOutput = 3
)
const (
	decoderNoError                          = 0
	decoderSuccess                          = 1
	decoderNeedsMoreInput                   = 2
	decoderNeedsMoreOutput                  = 3
	decoderErrorFormatExuberantNibble       = -1
	decoderErrorFormatReserved              = -2
	decoderErrorFormatExuberantMetaNibble   = -3
	decoderErrorFormatSimpleHuffmanAlphabet = -4
	decoderErrorFormatSimpleHuffmanSame     = -5
	decoderErrorFormatClSpace               = -6
	decoderErrorFormatHuffmanSpace          = -7
	decoderErrorFormatContextMapRepeat      = -8
	decoderErrorFormatBlockLength1          = -9
	decoderErrorFormatBlockLength2          = -10
	decoderErrorFormatTransform             = -11
	decoderErrorFormatDictionary            = -12
	decoderErrorFormatWindowBits            = -13
	decoderErrorFormatPadding1              = -14
	decoderErrorFormatPadding2              = -15
	decoderErrorFormatDistance              = -16
	decoderErrorDictionaryNotSet            = -19
	decoderErrorInvalidArguments            = -20
	decoderErrorAllocContextModes           = -21
	decoderErrorAllocTreeGroups             = -22
	decoderErrorAllocContextMap             = -25
	decoderErrorAllocRingBuffer1            = -26
	decoderErrorAllocRingBuffer2            = -27
	decoderErrorAllocBlockTypeTrees         = -30
	decoderErrorUnreachable                 = -31
)
const lastErrorCode = decoderErrorUnreachable
const (
	decoderParamDisableRingBufferReallocation = 0
	decoderParamLargeWindow                   = 1
)
const huffmanTableBits = 8
const huffmanTableMask = 0xFF
const kRingBufferWriteAheadSlack uint32 = 42
var kCodeLengthCodeOrder = [codeLengthCodes]byte{1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15}
var kCodeLengthPrefixLength = [16]byte{2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4}
var kCodeLengthPrefixValue = [16]byte{0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5}
func decoderSetParameter(state *Reader, p int, value uint32) bool {
	if state.state != stateUninited {
		return false
	}
	switch p {
	case decoderParamDisableRingBufferReallocation:
		if !(value == 0) {
			state.canny_ringbuffer_allocation = 0
		} else {
			state.canny_ringbuffer_allocation = 1
		}
		return true
	case decoderParamLargeWindow:
		state.large_window = (!(value == 0))
		return true
	default:
		return false
	}
}
func saveErrorCode(s *Reader, e int) int {
	s.error_code = int(e)
	switch e {
	case decoderSuccess:
		return decoderResultSuccess
	case decoderNeedsMoreInput:
		return decoderResultNeedsMoreInput
	case decoderNeedsMoreOutput:
		return decoderResultNeedsMoreOutput
	default:
		return decoderResultError
	}
}
func decodeWindowBits(s *Reader, br *bitReader) int {
	var n uint32
	var large_window bool = s.large_window
	s.large_window = false
	takeBits(br, 1, &n)
	if n == 0 {
		s.window_bits = 16
		return decoderSuccess
	}
	takeBits(br, 3, &n)
	if n != 0 {
		s.window_bits = 17 + n
		return decoderSuccess
	}
	takeBits(br, 3, &n)
	if n == 1 {
		if large_window {
			takeBits(br, 1, &n)
			if n == 1 {
				return decoderErrorFormatWindowBits
			}
			s.large_window = true
			return decoderSuccess
		} else {
			return decoderErrorFormatWindowBits
		}
	}
	if n != 0 {
		s.window_bits = 8 + n
		return decoderSuccess
	}
	s.window_bits = 17
	return decoderSuccess
}
func decodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int {
	var bits uint32
	switch s.substate_decode_uint8 {
	case stateDecodeUint8None:
		if !safeReadBits(br, 1, &bits) {
			return decoderNeedsMoreInput
		}
		if bits == 0 {
			*value = 0
			return decoderSuccess
		}
		fallthrough
	case stateDecodeUint8Short:
		if !safeReadBits(br, 3, &bits) {
			s.substate_decode_uint8 = stateDecodeUint8Short
			return decoderNeedsMoreInput
		}
		if bits == 0 {
			*value = 1
			s.substate_decode_uint8 = stateDecodeUint8None
			return decoderSuccess
		}
		*value = bits
		fallthrough
	case stateDecodeUint8Long:
		if !safeReadBits(br, *value, &bits) {
			s.substate_decode_uint8 = stateDecodeUint8Long
			return decoderNeedsMoreInput
		}
		*value = (1 << *value) + bits
		s.substate_decode_uint8 = stateDecodeUint8None
		return decoderSuccess
	default:
		return decoderErrorUnreachable
	}
}
func decodeMetaBlockLength(s *Reader, br *bitReader) int {
	var bits uint32
	var i int
	for {
		switch s.substate_metablock_header {
		case stateMetablockHeaderNone:
			if !safeReadBits(br, 1, &bits) {
				return decoderNeedsMoreInput
			}
			if bits != 0 {
				s.is_last_metablock = 1
			} else {
				s.is_last_metablock = 0
			}
			s.meta_block_remaining_len = 0
			s.is_uncompressed = 0
			s.is_metadata = 0
			if s.is_last_metablock == 0 {
				s.substate_metablock_header = stateMetablockHeaderNibbles
				break
			}
			s.substate_metablock_header = stateMetablockHeaderEmpty
			fallthrough
		case stateMetablockHeaderEmpty:
			if !safeReadBits(br, 1, &bits) {
				return decoderNeedsMoreInput
			}
			if bits != 0 {
				s.substate_metablock_header = stateMetablockHeaderNone
				return decoderSuccess
			}
			s.substate_metablock_header = stateMetablockHeaderNibbles
			fallthrough
		case stateMetablockHeaderNibbles:
			if !safeReadBits(br, 2, &bits) {
				return decoderNeedsMoreInput
			}
			s.size_nibbles = uint(byte(bits + 4))
			s.loop_counter = 0
			if bits == 3 {
				s.is_metadata = 1
				s.substate_metablock_header = stateMetablockHeaderReserved
				break
			}
			s.substate_metablock_header = stateMetablockHeaderSize
			fallthrough
		case stateMetablockHeaderSize:
			i = s.loop_counter
			for ; i < int(s.size_nibbles); i++ {
				if !safeReadBits(br, 4, &bits) {
					s.loop_counter = i
					return decoderNeedsMoreInput
				}
				if uint(i+1) == s.size_nibbles && s.size_nibbles > 4 && bits == 0 {
					return decoderErrorFormatExuberantNibble
				}
				s.meta_block_remaining_len |= int(bits << uint(i*4))
			}
			s.substate_metablock_header = stateMetablockHeaderUncompressed
			fallthrough
		case stateMetablockHeaderUncompressed:
			if s.is_last_metablock == 0 {
				if !safeReadBits(br, 1, &bits) {
					return decoderNeedsMoreInput
				}
				if bits != 0 {
					s.is_uncompressed = 1
				} else {
					s.is_uncompressed = 0
				}
			}
			s.meta_block_remaining_len++
			s.substate_metablock_header = stateMetablockHeaderNone
			return decoderSuccess
		case stateMetablockHeaderReserved:
			if !safeReadBits(br, 1, &bits) {
				return decoderNeedsMoreInput
			}
			if bits != 0 {
				return decoderErrorFormatReserved
			}
			s.substate_metablock_header = stateMetablockHeaderBytes
			fallthrough
		case stateMetablockHeaderBytes:
			if !safeReadBits(br, 2, &bits) {
				return decoderNeedsMoreInput
			}
			if bits == 0 {
				s.substate_metablock_header = stateMetablockHeaderNone
				return decoderSuccess
			}
			s.size_nibbles = uint(byte(bits))
			s.substate_metablock_header = stateMetablockHeaderMetadata
			fallthrough
		case stateMetablockHeaderMetadata:
			i = s.loop_counter
			for ; i < int(s.size_nibbles); i++ {
				if !safeReadBits(br, 8, &bits) {
					s.loop_counter = i
					return decoderNeedsMoreInput
				}
				if uint(i+1) == s.size_nibbles && s.size_nibbles > 1 && bits == 0 {
					return decoderErrorFormatExuberantMetaNibble
				}
				s.meta_block_remaining_len |= int(bits << uint(i*8))
			}
			s.meta_block_remaining_len++
			s.substate_metablock_header = stateMetablockHeaderNone
			return decoderSuccess
		default:
			return decoderErrorUnreachable
		}
	}
}