forked from mirror/brotli
parent
45f5d9b05a
commit
36f6c9721d
221
decode.go
221
decode.go
|
@ -783,7 +783,6 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
|
|||
fallthrough
|
||||
|
||||
/* Read symbols, codes & code lengths directly. */
|
||||
/* Fall through. */
|
||||
case stateHuffmanSimpleSize:
|
||||
if !safeReadBits(br, 2, &s.symbol) { /* num_symbols */
|
||||
s.substate_huffman = stateHuffmanSimpleSize
|
||||
|
@ -792,7 +791,7 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
|
|||
|
||||
s.sub_loop_counter = 0
|
||||
fallthrough
|
||||
/* Fall through. */
|
||||
|
||||
case stateHuffmanSimpleRead:
|
||||
{
|
||||
var result int = readSimpleHuffmanSymbols(alphabet_size, max_symbol, s)
|
||||
|
@ -802,29 +801,25 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateHuffmanSimpleBuild:
|
||||
{
|
||||
var table_size uint32
|
||||
if s.symbol == 3 {
|
||||
var bits uint32
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_huffman = stateHuffmanSimpleBuild
|
||||
return decoderNeedsMoreInput
|
||||
}
|
||||
|
||||
s.symbol += bits
|
||||
var table_size uint32
|
||||
if s.symbol == 3 {
|
||||
var bits uint32
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_huffman = stateHuffmanSimpleBuild
|
||||
return decoderNeedsMoreInput
|
||||
}
|
||||
|
||||
table_size = buildSimpleHuffmanTable(table, huffmanTableBits, s.symbols_lists_array[:], s.symbol)
|
||||
if opt_table_size != nil {
|
||||
*opt_table_size = table_size
|
||||
}
|
||||
|
||||
s.substate_huffman = stateHuffmanNone
|
||||
return decoderSuccess
|
||||
s.symbol += bits
|
||||
}
|
||||
fallthrough
|
||||
|
||||
table_size = buildSimpleHuffmanTable(table, huffmanTableBits, s.symbols_lists_array[:], s.symbol)
|
||||
if opt_table_size != nil {
|
||||
*opt_table_size = table_size
|
||||
}
|
||||
|
||||
s.substate_huffman = stateHuffmanNone
|
||||
return decoderSuccess
|
||||
|
||||
/* Decode Huffman-coded code lengths. */
|
||||
case stateHuffmanComplex:
|
||||
|
@ -854,32 +849,28 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateHuffmanLengthSymbols:
|
||||
{
|
||||
var table_size uint32
|
||||
var result int = readSymbolCodeLengths(max_symbol, s)
|
||||
if result == decoderNeedsMoreInput {
|
||||
result = safeReadSymbolCodeLengths(max_symbol, s)
|
||||
}
|
||||
|
||||
if result != decoderSuccess {
|
||||
return result
|
||||
}
|
||||
|
||||
if s.space != 0 {
|
||||
return decoderErrorFormatHuffmanSpace
|
||||
}
|
||||
|
||||
table_size = buildHuffmanTable(table, huffmanTableBits, s.symbol_lists, s.code_length_histo[:])
|
||||
if opt_table_size != nil {
|
||||
*opt_table_size = table_size
|
||||
}
|
||||
|
||||
s.substate_huffman = stateHuffmanNone
|
||||
return decoderSuccess
|
||||
var table_size uint32
|
||||
var result int = readSymbolCodeLengths(max_symbol, s)
|
||||
if result == decoderNeedsMoreInput {
|
||||
result = safeReadSymbolCodeLengths(max_symbol, s)
|
||||
}
|
||||
fallthrough
|
||||
|
||||
if result != decoderSuccess {
|
||||
return result
|
||||
}
|
||||
|
||||
if s.space != 0 {
|
||||
return decoderErrorFormatHuffmanSpace
|
||||
}
|
||||
|
||||
table_size = buildHuffmanTable(table, huffmanTableBits, s.symbol_lists, s.code_length_histo[:])
|
||||
if opt_table_size != nil {
|
||||
*opt_table_size = table_size
|
||||
}
|
||||
|
||||
s.substate_huffman = stateHuffmanNone
|
||||
return decoderSuccess
|
||||
|
||||
default:
|
||||
return decoderErrorUnreachable
|
||||
|
@ -1111,23 +1102,19 @@ func decodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateContextMapTransform:
|
||||
{
|
||||
var bits uint32
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_context_map = stateContextMapTransform
|
||||
return decoderNeedsMoreInput
|
||||
}
|
||||
|
||||
if bits != 0 {
|
||||
inverseMoveToFrontTransform(*context_map_arg, context_map_size, s)
|
||||
}
|
||||
|
||||
s.substate_context_map = stateContextMapNone
|
||||
return decoderSuccess
|
||||
var bits uint32
|
||||
if !safeReadBits(br, 1, &bits) {
|
||||
s.substate_context_map = stateContextMapTransform
|
||||
return decoderNeedsMoreInput
|
||||
}
|
||||
fallthrough
|
||||
|
||||
if bits != 0 {
|
||||
inverseMoveToFrontTransform(*context_map_arg, context_map_size, s)
|
||||
}
|
||||
|
||||
s.substate_context_map = stateContextMapNone
|
||||
return decoderSuccess
|
||||
|
||||
default:
|
||||
return decoderErrorUnreachable
|
||||
|
@ -1423,7 +1410,6 @@ func copyUncompressedBlockToOutput(available_out *uint, next_out *[]byte, total_
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateUncompressedWrite:
|
||||
{
|
||||
var result int
|
||||
|
@ -1441,9 +1427,6 @@ func copyUncompressedBlockToOutput(available_out *uint, next_out *[]byte, total_
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(false) /* Unreachable */
|
||||
return 0
|
||||
}
|
||||
|
||||
/* Calculates the smallest feasible ring buffer.
|
||||
|
@ -1484,8 +1467,6 @@ func calculateRingBufferSize(s *Reader) {
|
|||
output_size += s.meta_block_remaining_len
|
||||
if min_size < output_size {
|
||||
min_size = output_size
|
||||
} else {
|
||||
min_size = min_size
|
||||
}
|
||||
|
||||
if !(s.canny_ringbuffer_allocation == 0) {
|
||||
|
@ -2296,17 +2277,12 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
|
||||
s.loop_counter = 0
|
||||
s.state = stateHuffmanCode0
|
||||
case stateUncompressed:
|
||||
{
|
||||
result = copyUncompressedBlockToOutput(available_out, next_out, nil, s)
|
||||
if result != decoderSuccess {
|
||||
break
|
||||
}
|
||||
|
||||
case stateUncompressed:
|
||||
result = copyUncompressedBlockToOutput(available_out, next_out, nil, s)
|
||||
if result == decoderSuccess {
|
||||
s.state = stateMetablockDone
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
|
||||
case stateMetadata:
|
||||
for ; s.meta_block_remaining_len > 0; s.meta_block_remaining_len-- {
|
||||
|
@ -2344,7 +2320,7 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
|
||||
s.state = stateHuffmanCode1
|
||||
fallthrough
|
||||
/* Fall through. */
|
||||
|
||||
case stateHuffmanCode1:
|
||||
{
|
||||
var alphabet_size uint32 = s.num_block_types[s.loop_counter] + 2
|
||||
|
@ -2357,7 +2333,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateHuffmanCode2:
|
||||
{
|
||||
var alphabet_size uint32 = numBlockLenSymbols
|
||||
|
@ -2370,20 +2345,16 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateHuffmanCode3:
|
||||
{
|
||||
var tree_offset int = s.loop_counter * huffmanMaxSize26
|
||||
if !safeReadBlockLength(s, &s.block_length[s.loop_counter], s.block_len_trees[tree_offset:], br) {
|
||||
result = decoderNeedsMoreInput
|
||||
break
|
||||
}
|
||||
|
||||
s.loop_counter++
|
||||
s.state = stateHuffmanCode0
|
||||
var tree_offset int = s.loop_counter * huffmanMaxSize26
|
||||
if !safeReadBlockLength(s, &s.block_length[s.loop_counter], s.block_len_trees[tree_offset:], br) {
|
||||
result = decoderNeedsMoreInput
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
|
||||
s.loop_counter++
|
||||
s.state = stateHuffmanCode0
|
||||
|
||||
case stateMetablockHeader2:
|
||||
{
|
||||
var bits uint32
|
||||
|
@ -2407,7 +2378,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateContextModes:
|
||||
result = readContextModes(s)
|
||||
|
||||
|
@ -2418,7 +2388,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
s.state = stateContextMap1
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateContextMap1:
|
||||
result = decodeContextMap(s.num_block_types[0]<<literalContextBits, &s.num_literal_htrees, &s.context_map, s)
|
||||
|
||||
|
@ -2429,7 +2398,7 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
detectTrivialLiteralBlockTypes(s)
|
||||
s.state = stateContextMap2
|
||||
fallthrough
|
||||
/* Fall through. */
|
||||
|
||||
case stateContextMap2:
|
||||
{
|
||||
var num_direct_codes uint32 = s.num_direct_distance_codes - numDistanceShortCodes
|
||||
|
@ -2469,63 +2438,44 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
}
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateTreeGroup:
|
||||
{
|
||||
var hgroup *huffmanTreeGroup = nil
|
||||
switch s.loop_counter {
|
||||
case 0:
|
||||
hgroup = &s.literal_hgroup
|
||||
case 1:
|
||||
hgroup = &s.insert_copy_hgroup
|
||||
case 2:
|
||||
hgroup = &s.distance_hgroup
|
||||
default:
|
||||
return saveErrorCode(s, decoderErrorUnreachable)
|
||||
}
|
||||
|
||||
result = huffmanTreeGroupDecode(hgroup, s)
|
||||
if result != decoderSuccess {
|
||||
break
|
||||
}
|
||||
s.loop_counter++
|
||||
if s.loop_counter >= 3 {
|
||||
prepareLiteralDecoding(s)
|
||||
s.dist_context_map_slice = s.dist_context_map
|
||||
s.htree_command = []huffmanCode(s.insert_copy_hgroup.htrees[0])
|
||||
if !ensureRingBuffer(s) {
|
||||
result = decoderErrorAllocRingBuffer2
|
||||
break
|
||||
}
|
||||
|
||||
s.state = stateCommandBegin
|
||||
}
|
||||
var hgroup *huffmanTreeGroup = nil
|
||||
switch s.loop_counter {
|
||||
case 0:
|
||||
hgroup = &s.literal_hgroup
|
||||
case 1:
|
||||
hgroup = &s.insert_copy_hgroup
|
||||
case 2:
|
||||
hgroup = &s.distance_hgroup
|
||||
default:
|
||||
return saveErrorCode(s, decoderErrorUnreachable)
|
||||
}
|
||||
|
||||
result = huffmanTreeGroupDecode(hgroup, s)
|
||||
if result != decoderSuccess {
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
s.loop_counter++
|
||||
if s.loop_counter >= 3 {
|
||||
prepareLiteralDecoding(s)
|
||||
s.dist_context_map_slice = s.dist_context_map
|
||||
s.htree_command = []huffmanCode(s.insert_copy_hgroup.htrees[0])
|
||||
if !ensureRingBuffer(s) {
|
||||
result = decoderErrorAllocRingBuffer2
|
||||
break
|
||||
}
|
||||
|
||||
case stateCommandBegin,
|
||||
/* Fall through. */
|
||||
stateCommandInner,
|
||||
s.state = stateCommandBegin
|
||||
}
|
||||
|
||||
/* Fall through. */
|
||||
stateCommandPostDecodeLiterals,
|
||||
|
||||
/* Fall through. */
|
||||
stateCommandPostWrapCopy:
|
||||
case stateCommandBegin, stateCommandInner, stateCommandPostDecodeLiterals, stateCommandPostWrapCopy:
|
||||
result = processCommands(s)
|
||||
|
||||
if result == decoderNeedsMoreInput {
|
||||
result = safeProcessCommands(s)
|
||||
}
|
||||
|
||||
case stateCommandInnerWrite,
|
||||
/* Fall through. */
|
||||
stateCommandPostWrite1,
|
||||
|
||||
/* Fall through. */
|
||||
stateCommandPostWrite2:
|
||||
case stateCommandInnerWrite, stateCommandPostWrite1, stateCommandPostWrite2:
|
||||
result = writeRingBuffer(s, available_out, next_out, nil, false)
|
||||
|
||||
if result != decoderSuccess {
|
||||
|
@ -2588,7 +2538,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
|
|||
s.state = stateDone
|
||||
fallthrough
|
||||
|
||||
/* Fall through. */
|
||||
case stateDone:
|
||||
if s.ringbuffer != nil {
|
||||
result = writeRingBuffer(s, available_out, next_out, nil, true)
|
||||
|
|
|
@ -957,7 +957,7 @@ func writeMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes
|
|||
}
|
||||
|
||||
assert(*storage_ix <= 14)
|
||||
last_bytes = uint16(storage[1]<<8 | storage[0])
|
||||
last_bytes = uint16(storage[1])<<8 | uint16(storage[0])
|
||||
last_bytes_bits = byte(*storage_ix)
|
||||
if params.quality <= maxQualityForStaticEntropyCodes {
|
||||
storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage)
|
||||
|
|
65
huffman.go
65
huffman.go
|
@ -606,47 +606,42 @@ func buildSimpleHuffmanTable(table []huffmanCode, root_bits int, val []uint16, n
|
|||
}
|
||||
|
||||
table_size = 4
|
||||
|
||||
case 3:
|
||||
{
|
||||
var i int
|
||||
var k int
|
||||
for i = 0; i < 3; i++ {
|
||||
for k = i + 1; k < 4; k++ {
|
||||
if val[k] < val[i] {
|
||||
var t uint16 = val[k]
|
||||
val[k] = val[i]
|
||||
val[i] = t
|
||||
}
|
||||
var i int
|
||||
var k int
|
||||
for i = 0; i < 3; i++ {
|
||||
for k = i + 1; k < 4; k++ {
|
||||
if val[k] < val[i] {
|
||||
var t uint16 = val[k]
|
||||
val[k] = val[i]
|
||||
val[i] = t
|
||||
}
|
||||
}
|
||||
|
||||
table[0] = constructHuffmanCode(2, val[0])
|
||||
table[2] = constructHuffmanCode(2, val[1])
|
||||
table[1] = constructHuffmanCode(2, val[2])
|
||||
table[3] = constructHuffmanCode(2, val[3])
|
||||
table_size = 4
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
|
||||
table[0] = constructHuffmanCode(2, val[0])
|
||||
table[2] = constructHuffmanCode(2, val[1])
|
||||
table[1] = constructHuffmanCode(2, val[2])
|
||||
table[3] = constructHuffmanCode(2, val[3])
|
||||
table_size = 4
|
||||
|
||||
case 4:
|
||||
{
|
||||
if val[3] < val[2] {
|
||||
var t uint16 = val[3]
|
||||
val[3] = val[2]
|
||||
val[2] = t
|
||||
}
|
||||
|
||||
table[0] = constructHuffmanCode(1, val[0])
|
||||
table[1] = constructHuffmanCode(2, val[1])
|
||||
table[2] = constructHuffmanCode(1, val[0])
|
||||
table[3] = constructHuffmanCode(3, val[2])
|
||||
table[4] = constructHuffmanCode(1, val[0])
|
||||
table[5] = constructHuffmanCode(2, val[1])
|
||||
table[6] = constructHuffmanCode(1, val[0])
|
||||
table[7] = constructHuffmanCode(3, val[3])
|
||||
table_size = 8
|
||||
break
|
||||
if val[3] < val[2] {
|
||||
var t uint16 = val[3]
|
||||
val[3] = val[2]
|
||||
val[2] = t
|
||||
}
|
||||
|
||||
table[0] = constructHuffmanCode(1, val[0])
|
||||
table[1] = constructHuffmanCode(2, val[1])
|
||||
table[2] = constructHuffmanCode(1, val[0])
|
||||
table[3] = constructHuffmanCode(3, val[2])
|
||||
table[4] = constructHuffmanCode(1, val[0])
|
||||
table[5] = constructHuffmanCode(2, val[1])
|
||||
table[6] = constructHuffmanCode(1, val[0])
|
||||
table[7] = constructHuffmanCode(3, val[3])
|
||||
table_size = 8
|
||||
}
|
||||
|
||||
for table_size != goal_size {
|
||||
|
|
|
@ -89,6 +89,4 @@ func (r *Reader) Read(p []byte) (n int, err error) {
|
|||
}
|
||||
r.in = r.buf[:encN]
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
|
|
@ -543,7 +543,7 @@ func shiftTransform(word []byte, word_len int, parameter uint16) int {
|
|||
if word_len < 3 {
|
||||
return word_len
|
||||
}
|
||||
scalar += uint32(word[2]&0x3F | (word[1]&0x3F)<<6 | (word[0]&0x0F)<<12)
|
||||
scalar += uint32(word[2])&0x3F | uint32(word[1]&0x3F)<<6 | uint32(word[0]&0x0F)<<12
|
||||
word[0] = byte(0xE0 | (scalar>>12)&0x0F)
|
||||
word[1] = byte(uint32(word[1]&0xC0) | (scalar>>6)&0x3F)
|
||||
word[2] = byte(uint32(word[2]&0xC0) | scalar&0x3F)
|
||||
|
@ -553,7 +553,7 @@ func shiftTransform(word []byte, word_len int, parameter uint16) int {
|
|||
if word_len < 4 {
|
||||
return word_len
|
||||
}
|
||||
scalar += uint32(word[3]&0x3F | (word[2]&0x3F)<<6 | (word[1]&0x3F)<<12 | (word[0]&0x07)<<18)
|
||||
scalar += uint32(word[3])&0x3F | uint32(word[2]&0x3F)<<6 | uint32(word[1]&0x3F)<<12 | uint32(word[0]&0x07)<<18
|
||||
word[0] = byte(0xF0 | (scalar>>18)&0x07)
|
||||
word[1] = byte(uint32(word[1]&0xC0) | (scalar>>12)&0x3F)
|
||||
word[2] = byte(uint32(word[2]&0xC0) | (scalar>>6)&0x3F)
|
||||
|
@ -610,10 +610,10 @@ func transformDictionaryWord(dst []byte, word []byte, len int, trans *transforms
|
|||
len -= step
|
||||
}
|
||||
} else if t == transformShiftFirst {
|
||||
var param uint16 = uint16(trans.params[transform_idx*2] + (trans.params[transform_idx*2+1] << 8))
|
||||
var param uint16 = uint16(trans.params[transform_idx*2]) + uint16(trans.params[transform_idx*2+1])<<8
|
||||
shiftTransform(dst[idx-len:], int(len), param)
|
||||
} else if t == transformShiftAll {
|
||||
var param uint16 = uint16(trans.params[transform_idx*2] + (trans.params[transform_idx*2+1] << 8))
|
||||
var param uint16 = uint16(trans.params[transform_idx*2]) + uint16(trans.params[transform_idx*2+1])<<8
|
||||
var shift []byte = dst
|
||||
shift = shift[idx-len:]
|
||||
for len > 0 {
|
||||
|
|
Loading…
Reference in New Issue