Fix warnings from go vet.

FIxes #1
This commit is contained in:
Andy Balholm 2019-03-26 06:05:46 -04:00
parent 45f5d9b05a
commit 36f6c9721d
5 changed files with 120 additions and 178 deletions

221
decode.go
View File

@ -783,7 +783,6 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
fallthrough fallthrough
/* Read symbols, codes & code lengths directly. */ /* Read symbols, codes & code lengths directly. */
/* Fall through. */
case stateHuffmanSimpleSize: case stateHuffmanSimpleSize:
if !safeReadBits(br, 2, &s.symbol) { /* num_symbols */ if !safeReadBits(br, 2, &s.symbol) { /* num_symbols */
s.substate_huffman = stateHuffmanSimpleSize s.substate_huffman = stateHuffmanSimpleSize
@ -792,7 +791,7 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
s.sub_loop_counter = 0 s.sub_loop_counter = 0
fallthrough fallthrough
/* Fall through. */
case stateHuffmanSimpleRead: case stateHuffmanSimpleRead:
{ {
var result int = readSimpleHuffmanSymbols(alphabet_size, max_symbol, s) var result int = readSimpleHuffmanSymbols(alphabet_size, max_symbol, s)
@ -802,29 +801,25 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
} }
fallthrough fallthrough
/* Fall through. */
case stateHuffmanSimpleBuild: case stateHuffmanSimpleBuild:
{ var table_size uint32
var table_size uint32 if s.symbol == 3 {
if s.symbol == 3 { var bits uint32
var bits uint32 if !safeReadBits(br, 1, &bits) {
if !safeReadBits(br, 1, &bits) { s.substate_huffman = stateHuffmanSimpleBuild
s.substate_huffman = stateHuffmanSimpleBuild return decoderNeedsMoreInput
return decoderNeedsMoreInput
}
s.symbol += bits
} }
table_size = buildSimpleHuffmanTable(table, huffmanTableBits, s.symbols_lists_array[:], s.symbol) s.symbol += bits
if opt_table_size != nil {
*opt_table_size = table_size
}
s.substate_huffman = stateHuffmanNone
return decoderSuccess
} }
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. */ /* Decode Huffman-coded code lengths. */
case stateHuffmanComplex: case stateHuffmanComplex:
@ -854,32 +849,28 @@ func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCod
} }
fallthrough fallthrough
/* Fall through. */
case stateHuffmanLengthSymbols: case stateHuffmanLengthSymbols:
{ var table_size uint32
var table_size uint32 var result int = readSymbolCodeLengths(max_symbol, s)
var result int = readSymbolCodeLengths(max_symbol, s) if result == decoderNeedsMoreInput {
if result == decoderNeedsMoreInput { result = safeReadSymbolCodeLengths(max_symbol, s)
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
} }
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: default:
return decoderErrorUnreachable return decoderErrorUnreachable
@ -1111,23 +1102,19 @@ func decodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a
} }
fallthrough fallthrough
/* Fall through. */
case stateContextMapTransform: case stateContextMapTransform:
{ var bits uint32
var bits uint32 if !safeReadBits(br, 1, &bits) {
if !safeReadBits(br, 1, &bits) { s.substate_context_map = stateContextMapTransform
s.substate_context_map = stateContextMapTransform return decoderNeedsMoreInput
return decoderNeedsMoreInput
}
if bits != 0 {
inverseMoveToFrontTransform(*context_map_arg, context_map_size, s)
}
s.substate_context_map = stateContextMapNone
return decoderSuccess
} }
fallthrough
if bits != 0 {
inverseMoveToFrontTransform(*context_map_arg, context_map_size, s)
}
s.substate_context_map = stateContextMapNone
return decoderSuccess
default: default:
return decoderErrorUnreachable return decoderErrorUnreachable
@ -1423,7 +1410,6 @@ func copyUncompressedBlockToOutput(available_out *uint, next_out *[]byte, total_
} }
fallthrough fallthrough
/* Fall through. */
case stateUncompressedWrite: case stateUncompressedWrite:
{ {
var result int 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. /* Calculates the smallest feasible ring buffer.
@ -1484,8 +1467,6 @@ func calculateRingBufferSize(s *Reader) {
output_size += s.meta_block_remaining_len output_size += s.meta_block_remaining_len
if min_size < output_size { if min_size < output_size {
min_size = output_size min_size = output_size
} else {
min_size = min_size
} }
if !(s.canny_ringbuffer_allocation == 0) { 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.loop_counter = 0
s.state = stateHuffmanCode0 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 s.state = stateMetablockDone
break
} }
fallthrough
case stateMetadata: case stateMetadata:
for ; s.meta_block_remaining_len > 0; s.meta_block_remaining_len-- { 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 s.state = stateHuffmanCode1
fallthrough fallthrough
/* Fall through. */
case stateHuffmanCode1: case stateHuffmanCode1:
{ {
var alphabet_size uint32 = s.num_block_types[s.loop_counter] + 2 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 fallthrough
/* Fall through. */
case stateHuffmanCode2: case stateHuffmanCode2:
{ {
var alphabet_size uint32 = numBlockLenSymbols var alphabet_size uint32 = numBlockLenSymbols
@ -2370,20 +2345,16 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
} }
fallthrough fallthrough
/* Fall through. */
case stateHuffmanCode3: case stateHuffmanCode3:
{ var tree_offset int = s.loop_counter * huffmanMaxSize26
var tree_offset int = s.loop_counter * huffmanMaxSize26 if !safeReadBlockLength(s, &s.block_length[s.loop_counter], s.block_len_trees[tree_offset:], br) {
if !safeReadBlockLength(s, &s.block_length[s.loop_counter], s.block_len_trees[tree_offset:], br) { result = decoderNeedsMoreInput
result = decoderNeedsMoreInput
break
}
s.loop_counter++
s.state = stateHuffmanCode0
break break
} }
fallthrough
s.loop_counter++
s.state = stateHuffmanCode0
case stateMetablockHeader2: case stateMetablockHeader2:
{ {
var bits uint32 var bits uint32
@ -2407,7 +2378,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
} }
fallthrough fallthrough
/* Fall through. */
case stateContextModes: case stateContextModes:
result = readContextModes(s) result = readContextModes(s)
@ -2418,7 +2388,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
s.state = stateContextMap1 s.state = stateContextMap1
fallthrough fallthrough
/* Fall through. */
case stateContextMap1: case stateContextMap1:
result = decodeContextMap(s.num_block_types[0]<<literalContextBits, &s.num_literal_htrees, &s.context_map, s) 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) detectTrivialLiteralBlockTypes(s)
s.state = stateContextMap2 s.state = stateContextMap2
fallthrough fallthrough
/* Fall through. */
case stateContextMap2: case stateContextMap2:
{ {
var num_direct_codes uint32 = s.num_direct_distance_codes - numDistanceShortCodes 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 fallthrough
/* Fall through. */
case stateTreeGroup: case stateTreeGroup:
{ var hgroup *huffmanTreeGroup = nil
var hgroup *huffmanTreeGroup = nil switch s.loop_counter {
switch s.loop_counter { case 0:
case 0: hgroup = &s.literal_hgroup
hgroup = &s.literal_hgroup case 1:
case 1: hgroup = &s.insert_copy_hgroup
hgroup = &s.insert_copy_hgroup case 2:
case 2: hgroup = &s.distance_hgroup
hgroup = &s.distance_hgroup default:
default: return saveErrorCode(s, decoderErrorUnreachable)
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
}
result = huffmanTreeGroupDecode(hgroup, s)
if result != decoderSuccess {
break 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, s.state = stateCommandBegin
/* Fall through. */ }
stateCommandInner,
/* Fall through. */ case stateCommandBegin, stateCommandInner, stateCommandPostDecodeLiterals, stateCommandPostWrapCopy:
stateCommandPostDecodeLiterals,
/* Fall through. */
stateCommandPostWrapCopy:
result = processCommands(s) result = processCommands(s)
if result == decoderNeedsMoreInput { if result == decoderNeedsMoreInput {
result = safeProcessCommands(s) result = safeProcessCommands(s)
} }
case stateCommandInnerWrite, case stateCommandInnerWrite, stateCommandPostWrite1, stateCommandPostWrite2:
/* Fall through. */
stateCommandPostWrite1,
/* Fall through. */
stateCommandPostWrite2:
result = writeRingBuffer(s, available_out, next_out, nil, false) result = writeRingBuffer(s, available_out, next_out, nil, false)
if result != decoderSuccess { if result != decoderSuccess {
@ -2588,7 +2538,6 @@ func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, ava
s.state = stateDone s.state = stateDone
fallthrough fallthrough
/* Fall through. */
case stateDone: case stateDone:
if s.ringbuffer != nil { if s.ringbuffer != nil {
result = writeRingBuffer(s, available_out, next_out, nil, true) result = writeRingBuffer(s, available_out, next_out, nil, true)

View File

@ -957,7 +957,7 @@ func writeMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes
} }
assert(*storage_ix <= 14) 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) last_bytes_bits = byte(*storage_ix)
if params.quality <= maxQualityForStaticEntropyCodes { if params.quality <= maxQualityForStaticEntropyCodes {
storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) storeMetaBlockFast(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage)

View File

@ -606,47 +606,42 @@ func buildSimpleHuffmanTable(table []huffmanCode, root_bits int, val []uint16, n
} }
table_size = 4 table_size = 4
case 3: case 3:
{ var i int
var i int var k int
var k int for i = 0; i < 3; i++ {
for i = 0; i < 3; i++ { for k = i + 1; k < 4; k++ {
for k = i + 1; k < 4; k++ { if val[k] < val[i] {
if val[k] < val[i] { var t uint16 = val[k]
var t uint16 = val[k] val[k] = val[i]
val[k] = val[i] val[i] = t
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: case 4:
{ if val[3] < val[2] {
if val[3] < val[2] { var t uint16 = val[3]
var t uint16 = val[3] val[3] = val[2]
val[3] = val[2] val[2] = t
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
} }
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 { for table_size != goal_size {

View File

@ -89,6 +89,4 @@ func (r *Reader) Read(p []byte) (n int, err error) {
} }
r.in = r.buf[:encN] r.in = r.buf[:encN]
} }
return n, nil
} }

View File

@ -543,7 +543,7 @@ func shiftTransform(word []byte, word_len int, parameter uint16) int {
if word_len < 3 { if word_len < 3 {
return word_len 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[0] = byte(0xE0 | (scalar>>12)&0x0F)
word[1] = byte(uint32(word[1]&0xC0) | (scalar>>6)&0x3F) word[1] = byte(uint32(word[1]&0xC0) | (scalar>>6)&0x3F)
word[2] = byte(uint32(word[2]&0xC0) | scalar&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 { if word_len < 4 {
return word_len 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[0] = byte(0xF0 | (scalar>>18)&0x07)
word[1] = byte(uint32(word[1]&0xC0) | (scalar>>12)&0x3F) word[1] = byte(uint32(word[1]&0xC0) | (scalar>>12)&0x3F)
word[2] = byte(uint32(word[2]&0xC0) | (scalar>>6)&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 len -= step
} }
} else if t == transformShiftFirst { } 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) shiftTransform(dst[idx-len:], int(len), param)
} else if t == transformShiftAll { } 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 var shift []byte = dst
shift = shift[idx-len:] shift = shift[idx-len:]
for len > 0 { for len > 0 {