diff --git a/backward_references.go b/backward_references.go index 784e6f9..3fd185c 100644 --- a/backward_references.go +++ b/backward_references.go @@ -42,8 +42,8 @@ func computeDistanceCode(distance uint, max_distance uint, dist_cache []int) uin return distance + numDistanceShortCodes - 1 } -func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { - var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) +func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher hasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { + var max_backward_limit uint = maxBackwardLimit(params.lgwin) var orig_commands []command = commands var insert_length uint = *last_insert_len var pos_end uint = position + num_bytes @@ -58,7 +58,7 @@ func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, var gap uint = 0 /* Set maximum distance, see section 9.1. of the spec. */ - var kMinScore uint = BROTLI_SCORE_BASE + 100 + var kMinScore uint = scoreBase + 100 /* For speed up heuristics for random data. */ @@ -68,7 +68,7 @@ func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, for position+hasher.HashTypeLength() < pos_end { var max_length uint = pos_end - position var max_distance uint = brotli_min_size_t(position, max_backward_limit) - var sr HasherSearchResult + var sr hasherSearchResult sr.len = 0 sr.len_code_delta = 0 sr.distance = 0 @@ -80,7 +80,7 @@ func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, max_length-- for ; ; max_length-- { var cost_diff_lazy uint = 175 - var sr2 HasherSearchResult + var sr2 hasherSearchResult if params.quality < MIN_QUALITY_FOR_EXTENSIVE_REFERENCE_SEARCH { sr2.len = brotli_min_size_t(sr.len-1, max_length) } else { diff --git a/backward_references_hq.go b/backward_references_hq.go index 9833fce..1389e5d 100644 --- a/backward_references_hq.go +++ b/backward_references_hq.go @@ -80,7 +80,7 @@ type zopfliCostModel struct { num_bytes_ uint } -func initZopfliCostModel(self *zopfliCostModel, dist *BrotliDistanceParams, num_bytes uint) { +func initZopfliCostModel(self *zopfliCostModel, dist *distanceParams, num_bytes uint) { var distance_histogram_size uint32 = dist.alphabet_size if distance_histogram_size > maxEffectiveDistanceAlphabetSize { distance_histogram_size = maxEffectiveDistanceAlphabetSize @@ -107,7 +107,7 @@ func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, co sum += uint(histogram[i]) } - log2sum = float32(FastLog2(sum)) + log2sum = float32(fastLog2(sum)) missing_symbol_sum = sum if !literal_histogram { for i = 0; i < histogram_size; i++ { @@ -117,7 +117,7 @@ func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, co } } - missing_symbol_cost = float32(FastLog2(missing_symbol_sum)) + 2 + missing_symbol_cost = float32(fastLog2(missing_symbol_sum)) + 2 for i = 0; i < histogram_size; i++ { if histogram[i] == 0 { cost[i] = missing_symbol_cost @@ -125,7 +125,7 @@ func setCost(histogram []uint32, histogram_size uint, literal_histogram bool, co } /* Shannon bits for this symbol. */ - cost[i] = log2sum - float32(FastLog2(uint(histogram[i]))) + cost[i] = log2sum - float32(fastLog2(uint(histogram[i]))) /* Cannot be coded with less than 1 bit */ if cost[i] < 1 { @@ -197,7 +197,7 @@ func zopfliCostModelSetFromLiteralCosts(self *zopfliCostModel, position uint, ri var cost_cmd []float32 = self.cost_cmd_[:] var num_bytes uint = self.num_bytes_ var i uint - BrotliEstimateBitCostsForLiterals(position, num_bytes, ringbuffer_mask, ringbuffer, literal_costs[1:]) + estimateBitCostsForLiterals(position, num_bytes, ringbuffer_mask, ringbuffer, literal_costs[1:]) literal_costs[0] = 0.0 for i = 0; i < num_bytes; i++ { literal_carry += literal_costs[i+1] @@ -206,14 +206,14 @@ func zopfliCostModelSetFromLiteralCosts(self *zopfliCostModel, position uint, ri } for i = 0; i < numCommandSymbols; i++ { - cost_cmd[i] = float32(FastLog2(uint(11 + uint32(i)))) + cost_cmd[i] = float32(fastLog2(uint(11 + uint32(i)))) } for i = 0; uint32(i) < self.distance_histogram_size; i++ { - cost_dist[i] = float32(FastLog2(uint(20 + uint32(i)))) + cost_dist[i] = float32(fastLog2(uint(20 + uint32(i)))) } - self.min_cost_cmd_ = float32(FastLog2(11)) + self.min_cost_cmd_ = float32(fastLog2(11)) } func zopfliCostModelGetCommandCost(self *zopfliCostModel, cmdcode uint16) float32 { @@ -382,7 +382,7 @@ func evaluateNode(block_start uint, pos uint, max_backward_limit uint, gap uint, } /* Returns longest copy length. */ -func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, max_backward_limit uint, starting_dist_cache []int, num_matches uint, matches []BackwardMatch, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) uint { +func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, max_backward_limit uint, starting_dist_cache []int, num_matches uint, matches []backwardMatch, model *zopfliCostModel, queue *startPosQueue, nodes []zopfliNode) uint { var cur_ix uint = block_start + pos var cur_ix_masked uint = cur_ix & ringbuffer_mask var max_distance uint = brotli_min_size_t(cur_ix, max_backward_limit) @@ -439,7 +439,7 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, continue } - len = FindMatchLengthWithLimit(ringbuffer[prev_ix:], ringbuffer[cur_ix_masked:], max_len) + len = findMatchLengthWithLimit(ringbuffer[prev_ix:], ringbuffer[cur_ix_masked:], max_len) } else { continue } @@ -476,7 +476,7 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, /* Loop through all possible copy lengths at this position. */ var len uint = min_len for j = 0; j < num_matches; j++ { - var match BackwardMatch = matches[j] + var match backwardMatch = matches[j] var dist uint = uint(match.distance) var is_dictionary_match bool = (dist > max_distance+gap) var dist_code uint = dist + numDistanceShortCodes - 1 @@ -495,7 +495,7 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, /* Try all copy lengths up until the maximum copy length corresponding to this distance. If the distance refers to the static dictionary, or the maximum length is long enough, try only one maximum length. */ - max_match_len = BackwardMatchLength(&match) + max_match_len = backwardMatchLength(&match) if len < max_match_len && (is_dictionary_match || max_match_len > max_zopfli_len) { len = max_match_len @@ -504,7 +504,7 @@ func updateNodes(num_bytes uint, block_start uint, pos uint, ringbuffer []byte, for ; len <= max_match_len; len++ { var len_code uint if is_dictionary_match { - len_code = BackwardMatchLengthCode(&match) + len_code = backwardMatchLengthCode(&match) } else { len_code = len } @@ -541,8 +541,8 @@ func computeShortestPathFromNodes(num_bytes uint, nodes []zopfliNode) uint { } /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */ -func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *BrotliEncoderParams, commands []command, num_literals *uint) { - var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) +func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, dist_cache []int, last_insert_len *uint, params *encoderParams, commands []command, num_literals *uint) { + var max_backward_limit uint = maxBackwardLimit(params.lgwin) var pos uint = 0 var offset uint32 = nodes[0].u.next var i uint @@ -580,8 +580,8 @@ func zopfliCreateCommands(num_bytes uint, block_start uint, nodes []zopfliNode, *last_insert_len += num_bytes - pos } -func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, gap uint, dist_cache []int, model *zopfliCostModel, num_matches []uint32, matches []BackwardMatch, nodes []zopfliNode) uint { - var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) +func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, gap uint, dist_cache []int, model *zopfliCostModel, num_matches []uint32, matches []backwardMatch, nodes []zopfliNode) uint { + var max_backward_limit uint = maxBackwardLimit(params.lgwin) var max_zopfli_len uint = MaxZopfliLen(params) var queue startPosQueue var cur_match_pos uint = 0 @@ -595,8 +595,8 @@ func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_ skip = 0 } cur_match_pos += uint(num_matches[i]) - if num_matches[i] == 1 && BackwardMatchLength(&matches[cur_match_pos-1]) > max_zopfli_len { - skip = brotli_max_size_t(BackwardMatchLength(&matches[cur_match_pos-1]), skip) + if num_matches[i] == 1 && backwardMatchLength(&matches[cur_match_pos-1]) > max_zopfli_len { + skip = brotli_max_size_t(backwardMatchLength(&matches[cur_match_pos-1]), skip) } if skip > 1 { @@ -617,12 +617,12 @@ func zopfliIterate(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_ } /* REQUIRES: nodes != NULL and len(nodes) >= num_bytes + 1 */ -func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, dist_cache []int, hasher *H10, nodes []zopfliNode) uint { - var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) +func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, dist_cache []int, hasher *h10, nodes []zopfliNode) uint { + var max_backward_limit uint = maxBackwardLimit(params.lgwin) var max_zopfli_len uint = MaxZopfliLen(params) var model zopfliCostModel var queue startPosQueue - var matches [2 * (MAX_NUM_MATCHES_H10 + 64)]BackwardMatch + var matches [2 * (maxNumMatchesH10 + 64)]backwardMatch var store_end uint if num_bytes >= hasher.StoreLookahead() { store_end = position + num_bytes - hasher.StoreLookahead() + 1 @@ -642,8 +642,8 @@ func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, var max_distance uint = brotli_min_size_t(pos, max_backward_limit) var skip uint var num_matches uint - num_matches = FindAllMatchesH10(hasher, ¶ms.dictionary, ringbuffer, ringbuffer_mask, pos, num_bytes-i, max_distance, gap, params, matches[lz_matches_offset:]) - if num_matches > 0 && BackwardMatchLength(&matches[num_matches-1]) > max_zopfli_len { + num_matches = findAllMatchesH10(hasher, ¶ms.dictionary, ringbuffer, ringbuffer_mask, pos, num_bytes-i, max_distance, gap, params, matches[lz_matches_offset:]) + if num_matches > 0 && backwardMatchLength(&matches[num_matches-1]) > max_zopfli_len { matches[0] = matches[num_matches-1] num_matches = 1 } @@ -652,8 +652,8 @@ func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, if skip < BROTLI_LONG_COPY_QUICK_STEP { skip = 0 } - if num_matches == 1 && BackwardMatchLength(&matches[0]) > max_zopfli_len { - skip = brotli_max_size_t(BackwardMatchLength(&matches[0]), skip) + if num_matches == 1 && backwardMatchLength(&matches[0]) > max_zopfli_len { + skip = brotli_max_size_t(backwardMatchLength(&matches[0]), skip) } if skip > 1 { @@ -676,7 +676,7 @@ func zopfliComputeShortestPath(num_bytes uint, position uint, ringbuffer []byte, return computeShortestPathFromNodes(num_bytes, nodes) } -func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher *H10, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { +func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher *h10, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { var nodes []zopfliNode nodes = make([]zopfliNode, (num_bytes + 1)) initZopfliNodes(nodes, num_bytes+1) @@ -685,8 +685,8 @@ func createZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer [] nodes = nil } -func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *BrotliEncoderParams, hasher HasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { - var max_backward_limit uint = BROTLI_MAX_BACKWARD_LIMIT(params.lgwin) +func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher hasherHandle, dist_cache []int, last_insert_len *uint, commands []command, num_commands *uint, num_literals *uint) { + var max_backward_limit uint = maxBackwardLimit(params.lgwin) var num_matches []uint32 = make([]uint32, num_bytes) var matches_size uint = 4 * num_bytes var store_end uint @@ -703,10 +703,10 @@ func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer var orig_num_commands uint var model zopfliCostModel var nodes []zopfliNode - var matches []BackwardMatch = make([]BackwardMatch, matches_size) + var matches []backwardMatch = make([]backwardMatch, matches_size) var gap uint = 0 var shadow_matches uint = 0 - var new_array []BackwardMatch + var new_array []backwardMatch for i = 0; i+hasher.HashTypeLength()-1 < num_bytes; i++ { var pos uint = position + i var max_distance uint = brotli_min_size_t(pos, max_backward_limit) @@ -716,17 +716,17 @@ func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer var j uint /* Ensure that we have enough free slots. */ - if matches_size < cur_match_pos+MAX_NUM_MATCHES_H10+shadow_matches { + if matches_size < cur_match_pos+maxNumMatchesH10+shadow_matches { var new_size uint = matches_size if new_size == 0 { - new_size = cur_match_pos + MAX_NUM_MATCHES_H10 + shadow_matches + new_size = cur_match_pos + maxNumMatchesH10 + shadow_matches } - for new_size < cur_match_pos+MAX_NUM_MATCHES_H10+shadow_matches { + for new_size < cur_match_pos+maxNumMatchesH10+shadow_matches { new_size *= 2 } - new_array = make([]BackwardMatch, new_size) + new_array = make([]backwardMatch, new_size) if matches_size != 0 { copy(new_array, matches[:matches_size]) } @@ -735,15 +735,15 @@ func createHqZopfliBackwardReferences(num_bytes uint, position uint, ringbuffer matches_size = new_size } - num_found_matches = FindAllMatchesH10(hasher, ¶ms.dictionary, ringbuffer, ringbuffer_mask, pos, max_length, max_distance, gap, params, matches[cur_match_pos+shadow_matches:]) + num_found_matches = findAllMatchesH10(hasher.(*h10), ¶ms.dictionary, ringbuffer, ringbuffer_mask, pos, max_length, max_distance, gap, params, matches[cur_match_pos+shadow_matches:]) cur_match_end = cur_match_pos + num_found_matches for j = cur_match_pos; j+1 < cur_match_end; j++ { - assert(BackwardMatchLength(&matches[j]) <= BackwardMatchLength(&matches[j+1])) + assert(backwardMatchLength(&matches[j]) <= backwardMatchLength(&matches[j+1])) } num_matches[i] = uint32(num_found_matches) if num_found_matches > 0 { - var match_len uint = BackwardMatchLength(&matches[cur_match_end-1]) + var match_len uint = backwardMatchLength(&matches[cur_match_end-1]) if match_len > MAX_ZOPFLI_LEN_QUALITY_11 { var skip uint = match_len - 1 matches[cur_match_pos] = matches[cur_match_end-1] diff --git a/bit_cost.go b/bit_cost.go index 0a82585..c6eaad7 100644 --- a/bit_cost.go +++ b/bit_cost.go @@ -23,11 +23,11 @@ func shannonEntropy(population []uint32, size uint, total *uint) float64 { p = uint(population[0]) population = population[1:] sum += p - retval -= float64(p) * FastLog2(p) + retval -= float64(p) * fastLog2(p) } if sum != 0 { - retval += float64(sum) * FastLog2(sum) + retval += float64(sum) * fastLog2(sum) } *total = sum return retval @@ -49,8 +49,8 @@ const kTwoSymbolHistogramCost float64 = 20 const kThreeSymbolHistogramCost float64 = 28 const kFourSymbolHistogramCost float64 = 37 -func populationCostLiteral(histogram *HistogramLiteral) float64 { - var data_size uint = HistogramDataSizeLiteral() +func populationCostLiteral(histogram *histogramLiteral) float64 { + var data_size uint = histogramDataSizeLiteral() var count int = 0 var s [5]uint var bits float64 = 0.0 @@ -116,10 +116,10 @@ func populationCostLiteral(histogram *HistogramLiteral) float64 { build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ - var log2total float64 = FastLog2(histogram.total_count_) + var log2total float64 = fastLog2(histogram.total_count_) for i = 0; i < data_size; { if histogram.data_[i] > 0 { - var log2p float64 = log2total - FastLog2(uint(histogram.data_[i])) + var log2p float64 = log2total - fastLog2(uint(histogram.data_[i])) /* Compute -log2(P(symbol)) = -log2(count(symbol)/total_count) = = log2(total_count) - log2(count(symbol)) */ @@ -180,8 +180,8 @@ func populationCostLiteral(histogram *HistogramLiteral) float64 { return bits } -func populationCostCommand(histogram *HistogramCommand) float64 { - var data_size uint = HistogramDataSizeCommand() +func populationCostCommand(histogram *histogramCommand) float64 { + var data_size uint = histogramDataSizeCommand() var count int = 0 var s [5]uint var bits float64 = 0.0 @@ -247,10 +247,10 @@ func populationCostCommand(histogram *HistogramCommand) float64 { build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ - var log2total float64 = FastLog2(histogram.total_count_) + var log2total float64 = fastLog2(histogram.total_count_) for i = 0; i < data_size; { if histogram.data_[i] > 0 { - var log2p float64 = log2total - FastLog2(uint(histogram.data_[i])) + var log2p float64 = log2total - fastLog2(uint(histogram.data_[i])) /* Compute -log2(P(symbol)) = -log2(count(symbol)/total_count) = = log2(total_count) - log2(count(symbol)) */ @@ -311,8 +311,8 @@ func populationCostCommand(histogram *HistogramCommand) float64 { return bits } -func populationCostDistance(histogram *HistogramDistance) float64 { - var data_size uint = HistogramDataSizeDistance() +func populationCostDistance(histogram *histogramDistance) float64 { + var data_size uint = histogramDataSizeDistance() var count int = 0 var s [5]uint var bits float64 = 0.0 @@ -378,10 +378,10 @@ func populationCostDistance(histogram *HistogramDistance) float64 { build a simplified histogram of the code length codes where we use the zero repeat code 17, but we don't use the non-zero repeat code 16. */ - var log2total float64 = FastLog2(histogram.total_count_) + var log2total float64 = fastLog2(histogram.total_count_) for i = 0; i < data_size; { if histogram.data_[i] > 0 { - var log2p float64 = log2total - FastLog2(uint(histogram.data_[i])) + var log2p float64 = log2total - fastLog2(uint(histogram.data_[i])) /* Compute -log2(P(symbol)) = -log2(count(symbol)/total_count) = = log2(total_count) - log2(count(symbol)) */ diff --git a/block_splitter.go b/block_splitter.go index 8fb306c..ab37ff4 100644 --- a/block_splitter.go +++ b/block_splitter.go @@ -102,7 +102,7 @@ func bitCost(count uint) float64 { if count == 0 { return -2.0 } else { - return FastLog2(count) + return fastLog2(count) } } @@ -124,7 +124,7 @@ func destroyBlockSplit(self *blockSplit) { self.lengths = nil } -func splitBlock(cmds []command, num_commands uint, data []byte, pos uint, mask uint, params *BrotliEncoderParams, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit) { +func splitBlock(cmds []command, num_commands uint, data []byte, pos uint, mask uint, params *encoderParams, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit) { { var literals_count uint = countLiterals(cmds, num_commands) var literals []byte = make([]byte, literals_count) diff --git a/block_splitter_command.go b/block_splitter_command.go index 6aa6844..cc73438 100644 --- a/block_splitter_command.go +++ b/block_splitter_command.go @@ -6,11 +6,11 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func initialEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { +func initialEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramCommand) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint - ClearHistogramsCommand(histograms, num_histograms) + clearHistogramsCommand(histograms, num_histograms) for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { @@ -21,11 +21,11 @@ func initialEntropyCodesCommand(data []uint16, length uint, stride uint, num_his pos = length - stride - 1 } - HistogramAddVectorCommand(&histograms[i], data[pos:], stride) + histogramAddVectorCommand(&histograms[i], data[pos:], stride) } } -func randomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramCommand) { +func randomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, sample *histogramCommand) { var pos uint = 0 if stride >= length { stride = length @@ -33,27 +33,27 @@ func randomSampleCommand(seed *uint32, data []uint16, length uint, stride uint, pos = uint(myRand(seed) % uint32(length-stride+1)) } - HistogramAddVectorCommand(sample, data[pos:], stride) + histogramAddVectorCommand(sample, data[pos:], stride) } -func refineEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramCommand) { +func refineEntropyCodesCommand(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramCommand) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms for iter = 0; iter < iters; iter++ { - var sample HistogramCommand - HistogramClearCommand(&sample) + var sample histogramCommand + histogramClearCommand(&sample) randomSampleCommand(&seed, data, length, stride, &sample) - HistogramAddHistogramCommand(&histograms[iter%num_histograms], &sample) + histogramAddHistogramCommand(&histograms[iter%num_histograms], &sample) } } /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func findBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramCommand, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { - var data_size uint = HistogramDataSizeCommand() +func findBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []histogramCommand, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { + var data_size uint = histogramDataSizeCommand() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 var i uint @@ -71,7 +71,7 @@ func findBlocksCommand(data []uint16, length uint, block_switch_bitcost float64, insert_cost[i] = 0 } for i = 0; i < num_histograms; i++ { - insert_cost[i] = FastLog2(uint(uint32(histograms[i].total_count_))) + insert_cost[i] = fastLog2(uint(uint32(histograms[i].total_count_))) } for i = data_size; i != 0; { @@ -176,11 +176,11 @@ func remapBlockIdsCommand(block_ids []byte, length uint, new_id []uint16, num_hi return uint(next_id) } -func buildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramCommand) { +func buildBlockHistogramsCommand(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []histogramCommand) { var i uint - ClearHistogramsCommand(histograms, num_histograms) + clearHistogramsCommand(histograms, num_histograms) for i = 0; i < length; i++ { - HistogramAddCommand(&histograms[block_ids[i]], uint(data[i])) + histogramAddCommand(&histograms[block_ids[i]], uint(data[i])) } } @@ -192,12 +192,12 @@ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters - var all_histograms []HistogramCommand = make([]HistogramCommand, all_histograms_capacity) + var all_histograms []histogramCommand = make([]histogramCommand, all_histograms_capacity) var cluster_size_size uint = 0 var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramCommand = make([]HistogramCommand, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var histograms []histogramCommand = make([]histogramCommand, brotli_min_size_t(num_blocks, histogramsPerBatch)) var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 var pairs []histogramPair = make([]histogramPair, pairs_capacity) @@ -233,9 +233,9 @@ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids var j uint for j = 0; j < num_to_combine; j++ { var k uint - HistogramClearCommand(&histograms[j]) + histogramClearCommand(&histograms[j]) for k = 0; uint32(k) < block_lengths[i+j]; k++ { - HistogramAddCommand(&histograms[j], uint(data[pos])) + histogramAddCommand(&histograms[j], uint(data[pos])) pos++ } @@ -253,11 +253,11 @@ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids } else { _new_size = all_histograms_capacity } - var new_array []HistogramCommand + var new_array []histogramCommand for _new_size < (all_histograms_size + num_new_clusters) { _new_size *= 2 } - new_array = make([]HistogramCommand, _new_size) + new_array = make([]histogramCommand, _new_size) if all_histograms_capacity != 0 { copy(new_array, all_histograms[:all_histograms_capacity]) } @@ -309,13 +309,13 @@ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids { var next_index uint32 = 0 for i = 0; i < num_blocks; i++ { - var histo HistogramCommand + var histo histogramCommand var j uint var best_out uint32 var best_bits float64 - HistogramClearCommand(&histo) + histogramClearCommand(&histo) for j = 0; uint32(j) < block_lengths[i]; j++ { - HistogramAddCommand(&histo, uint(data[pos])) + histogramAddCommand(&histo, uint(data[pos])) pos++ } @@ -370,10 +370,10 @@ func clusterBlocksCommand(data []uint16, length uint, num_blocks uint, block_ids histogram_symbols = nil } -func splitByteVectorCommand(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { - var data_size uint = HistogramDataSizeCommand() +func splitByteVectorCommand(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *encoderParams, split *blockSplit) { + var data_size uint = histogramDataSizeCommand() var num_histograms uint = length/literals_per_histogram + 1 - var histograms []HistogramCommand + var histograms []histogramCommand if num_histograms > max_histograms { num_histograms = max_histograms } @@ -391,7 +391,7 @@ func splitByteVectorCommand(data []uint16, length uint, literals_per_histogram u return } - histograms = make([]HistogramCommand, num_histograms) + histograms = make([]histogramCommand, num_histograms) /* Find good entropy codes. */ initialEntropyCodesCommand(data, length, sampling_stride_length, num_histograms, histograms) diff --git a/block_splitter_distance.go b/block_splitter_distance.go index dbcd27c..92c7adf 100644 --- a/block_splitter_distance.go +++ b/block_splitter_distance.go @@ -6,11 +6,11 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func initialEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { +func initialEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramDistance) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint - ClearHistogramsDistance(histograms, num_histograms) + clearHistogramsDistance(histograms, num_histograms) for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { @@ -21,11 +21,11 @@ func initialEntropyCodesDistance(data []uint16, length uint, stride uint, num_hi pos = length - stride - 1 } - HistogramAddVectorDistance(&histograms[i], data[pos:], stride) + histogramAddVectorDistance(&histograms[i], data[pos:], stride) } } -func randomSampleDistance(seed *uint32, data []uint16, length uint, stride uint, sample *HistogramDistance) { +func randomSampleDistance(seed *uint32, data []uint16, length uint, stride uint, sample *histogramDistance) { var pos uint = 0 if stride >= length { stride = length @@ -33,27 +33,27 @@ func randomSampleDistance(seed *uint32, data []uint16, length uint, stride uint, pos = uint(myRand(seed) % uint32(length-stride+1)) } - HistogramAddVectorDistance(sample, data[pos:], stride) + histogramAddVectorDistance(sample, data[pos:], stride) } -func refineEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []HistogramDistance) { +func refineEntropyCodesDistance(data []uint16, length uint, stride uint, num_histograms uint, histograms []histogramDistance) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms for iter = 0; iter < iters; iter++ { - var sample HistogramDistance - HistogramClearDistance(&sample) + var sample histogramDistance + histogramClearDistance(&sample) randomSampleDistance(&seed, data, length, stride, &sample) - HistogramAddHistogramDistance(&histograms[iter%num_histograms], &sample) + histogramAddHistogramDistance(&histograms[iter%num_histograms], &sample) } } /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func findBlocksDistance(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramDistance, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { - var data_size uint = HistogramDataSizeDistance() +func findBlocksDistance(data []uint16, length uint, block_switch_bitcost float64, num_histograms uint, histograms []histogramDistance, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { + var data_size uint = histogramDataSizeDistance() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 var i uint @@ -71,7 +71,7 @@ func findBlocksDistance(data []uint16, length uint, block_switch_bitcost float64 insert_cost[i] = 0 } for i = 0; i < num_histograms; i++ { - insert_cost[i] = FastLog2(uint(uint32(histograms[i].total_count_))) + insert_cost[i] = fastLog2(uint(uint32(histograms[i].total_count_))) } for i = data_size; i != 0; { @@ -176,11 +176,11 @@ func remapBlockIdsDistance(block_ids []byte, length uint, new_id []uint16, num_h return uint(next_id) } -func buildBlockHistogramsDistance(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []HistogramDistance) { +func buildBlockHistogramsDistance(data []uint16, length uint, block_ids []byte, num_histograms uint, histograms []histogramDistance) { var i uint - ClearHistogramsDistance(histograms, num_histograms) + clearHistogramsDistance(histograms, num_histograms) for i = 0; i < length; i++ { - HistogramAddDistance(&histograms[block_ids[i]], uint(data[i])) + histogramAddDistance(&histograms[block_ids[i]], uint(data[i])) } } @@ -192,12 +192,12 @@ func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters - var all_histograms []HistogramDistance = make([]HistogramDistance, all_histograms_capacity) + var all_histograms []histogramDistance = make([]histogramDistance, all_histograms_capacity) var cluster_size_size uint = 0 var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramDistance = make([]HistogramDistance, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var histograms []histogramDistance = make([]histogramDistance, brotli_min_size_t(num_blocks, histogramsPerBatch)) var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 var pairs []histogramPair = make([]histogramPair, pairs_capacity) @@ -233,9 +233,9 @@ func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id var j uint for j = 0; j < num_to_combine; j++ { var k uint - HistogramClearDistance(&histograms[j]) + histogramClearDistance(&histograms[j]) for k = 0; uint32(k) < block_lengths[i+j]; k++ { - HistogramAddDistance(&histograms[j], uint(data[pos])) + histogramAddDistance(&histograms[j], uint(data[pos])) pos++ } @@ -253,11 +253,11 @@ func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id } else { _new_size = all_histograms_capacity } - var new_array []HistogramDistance + var new_array []histogramDistance for _new_size < (all_histograms_size + num_new_clusters) { _new_size *= 2 } - new_array = make([]HistogramDistance, _new_size) + new_array = make([]histogramDistance, _new_size) if all_histograms_capacity != 0 { copy(new_array, all_histograms[:all_histograms_capacity]) } @@ -309,13 +309,13 @@ func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id { var next_index uint32 = 0 for i = 0; i < num_blocks; i++ { - var histo HistogramDistance + var histo histogramDistance var j uint var best_out uint32 var best_bits float64 - HistogramClearDistance(&histo) + histogramClearDistance(&histo) for j = 0; uint32(j) < block_lengths[i]; j++ { - HistogramAddDistance(&histo, uint(data[pos])) + histogramAddDistance(&histo, uint(data[pos])) pos++ } @@ -370,10 +370,10 @@ func clusterBlocksDistance(data []uint16, length uint, num_blocks uint, block_id histogram_symbols = nil } -func splitByteVectorDistance(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { - var data_size uint = HistogramDataSizeDistance() +func splitByteVectorDistance(data []uint16, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *encoderParams, split *blockSplit) { + var data_size uint = histogramDataSizeDistance() var num_histograms uint = length/literals_per_histogram + 1 - var histograms []HistogramDistance + var histograms []histogramDistance if num_histograms > max_histograms { num_histograms = max_histograms } @@ -391,7 +391,7 @@ func splitByteVectorDistance(data []uint16, length uint, literals_per_histogram return } - histograms = make([]HistogramDistance, num_histograms) + histograms = make([]histogramDistance, num_histograms) /* Find good entropy codes. */ initialEntropyCodesDistance(data, length, sampling_stride_length, num_histograms, histograms) diff --git a/block_splitter_literal.go b/block_splitter_literal.go index bda9ad6..3a10c90 100644 --- a/block_splitter_literal.go +++ b/block_splitter_literal.go @@ -6,11 +6,11 @@ package brotli Distributed under MIT license. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -func initialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { +func initialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []histogramLiteral) { var seed uint32 = 7 var block_length uint = length / num_histograms var i uint - ClearHistogramsLiteral(histograms, num_histograms) + clearHistogramsLiteral(histograms, num_histograms) for i = 0; i < num_histograms; i++ { var pos uint = length * i / num_histograms if i != 0 { @@ -21,11 +21,11 @@ func initialEntropyCodesLiteral(data []byte, length uint, stride uint, num_histo pos = length - stride - 1 } - HistogramAddVectorLiteral(&histograms[i], data[pos:], stride) + histogramAddVectorLiteral(&histograms[i], data[pos:], stride) } } -func randomSampleLiteral(seed *uint32, data []byte, length uint, stride uint, sample *HistogramLiteral) { +func randomSampleLiteral(seed *uint32, data []byte, length uint, stride uint, sample *histogramLiteral) { var pos uint = 0 if stride >= length { stride = length @@ -33,27 +33,27 @@ func randomSampleLiteral(seed *uint32, data []byte, length uint, stride uint, sa pos = uint(myRand(seed) % uint32(length-stride+1)) } - HistogramAddVectorLiteral(sample, data[pos:], stride) + histogramAddVectorLiteral(sample, data[pos:], stride) } -func refineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []HistogramLiteral) { +func refineEntropyCodesLiteral(data []byte, length uint, stride uint, num_histograms uint, histograms []histogramLiteral) { var iters uint = kIterMulForRefining*length/stride + kMinItersForRefining var seed uint32 = 7 var iter uint iters = ((iters + num_histograms - 1) / num_histograms) * num_histograms for iter = 0; iter < iters; iter++ { - var sample HistogramLiteral - HistogramClearLiteral(&sample) + var sample histogramLiteral + histogramClearLiteral(&sample) randomSampleLiteral(&seed, data, length, stride, &sample) - HistogramAddHistogramLiteral(&histograms[iter%num_histograms], &sample) + histogramAddHistogramLiteral(&histograms[iter%num_histograms], &sample) } } /* Assigns a block id from the range [0, num_histograms) to each data element in data[0..length) and fills in block_id[0..length) with the assigned values. Returns the number of blocks, i.e. one plus the number of block switches. */ -func findBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, num_histograms uint, histograms []HistogramLiteral, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { - var data_size uint = HistogramDataSizeLiteral() +func findBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, num_histograms uint, histograms []histogramLiteral, insert_cost []float64, cost []float64, switch_signal []byte, block_id []byte) uint { + var data_size uint = histogramDataSizeLiteral() var bitmaplen uint = (num_histograms + 7) >> 3 var num_blocks uint = 1 var i uint @@ -71,7 +71,7 @@ func findBlocksLiteral(data []byte, length uint, block_switch_bitcost float64, n insert_cost[i] = 0 } for i = 0; i < num_histograms; i++ { - insert_cost[i] = FastLog2(uint(uint32(histograms[i].total_count_))) + insert_cost[i] = fastLog2(uint(uint32(histograms[i].total_count_))) } for i = data_size; i != 0; { @@ -176,11 +176,11 @@ func remapBlockIdsLiteral(block_ids []byte, length uint, new_id []uint16, num_hi return uint(next_id) } -func buildBlockHistogramsLiteral(data []byte, length uint, block_ids []byte, num_histograms uint, histograms []HistogramLiteral) { +func buildBlockHistogramsLiteral(data []byte, length uint, block_ids []byte, num_histograms uint, histograms []histogramLiteral) { var i uint - ClearHistogramsLiteral(histograms, num_histograms) + clearHistogramsLiteral(histograms, num_histograms) for i = 0; i < length; i++ { - HistogramAddLiteral(&histograms[block_ids[i]], uint(data[i])) + histogramAddLiteral(&histograms[block_ids[i]], uint(data[i])) } } @@ -192,12 +192,12 @@ func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ var expected_num_clusters uint = clustersPerBatch * (num_blocks + histogramsPerBatch - 1) / histogramsPerBatch var all_histograms_size uint = 0 var all_histograms_capacity uint = expected_num_clusters - var all_histograms []HistogramLiteral = make([]HistogramLiteral, all_histograms_capacity) + var all_histograms []histogramLiteral = make([]histogramLiteral, all_histograms_capacity) var cluster_size_size uint = 0 var cluster_size_capacity uint = expected_num_clusters var cluster_size []uint32 = make([]uint32, cluster_size_capacity) var num_clusters uint = 0 - var histograms []HistogramLiteral = make([]HistogramLiteral, brotli_min_size_t(num_blocks, histogramsPerBatch)) + var histograms []histogramLiteral = make([]histogramLiteral, brotli_min_size_t(num_blocks, histogramsPerBatch)) var max_num_pairs uint = histogramsPerBatch * histogramsPerBatch / 2 var pairs_capacity uint = max_num_pairs + 1 var pairs []histogramPair = make([]histogramPair, pairs_capacity) @@ -233,9 +233,9 @@ func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ var j uint for j = 0; j < num_to_combine; j++ { var k uint - HistogramClearLiteral(&histograms[j]) + histogramClearLiteral(&histograms[j]) for k = 0; uint32(k) < block_lengths[i+j]; k++ { - HistogramAddLiteral(&histograms[j], uint(data[pos])) + histogramAddLiteral(&histograms[j], uint(data[pos])) pos++ } @@ -253,11 +253,11 @@ func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ } else { _new_size = all_histograms_capacity } - var new_array []HistogramLiteral + var new_array []histogramLiteral for _new_size < (all_histograms_size + num_new_clusters) { _new_size *= 2 } - new_array = make([]HistogramLiteral, _new_size) + new_array = make([]histogramLiteral, _new_size) if all_histograms_capacity != 0 { copy(new_array, all_histograms[:all_histograms_capacity]) } @@ -309,13 +309,13 @@ func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ { var next_index uint32 = 0 for i = 0; i < num_blocks; i++ { - var histo HistogramLiteral + var histo histogramLiteral var j uint var best_out uint32 var best_bits float64 - HistogramClearLiteral(&histo) + histogramClearLiteral(&histo) for j = 0; uint32(j) < block_lengths[i]; j++ { - HistogramAddLiteral(&histo, uint(data[pos])) + histogramAddLiteral(&histo, uint(data[pos])) pos++ } @@ -370,10 +370,10 @@ func clusterBlocksLiteral(data []byte, length uint, num_blocks uint, block_ids [ histogram_symbols = nil } -func splitByteVectorLiteral(data []byte, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *BrotliEncoderParams, split *blockSplit) { - var data_size uint = HistogramDataSizeLiteral() +func splitByteVectorLiteral(data []byte, length uint, literals_per_histogram uint, max_histograms uint, sampling_stride_length uint, block_switch_cost float64, params *encoderParams, split *blockSplit) { + var data_size uint = histogramDataSizeLiteral() var num_histograms uint = length/literals_per_histogram + 1 - var histograms []HistogramLiteral + var histograms []histogramLiteral if num_histograms > max_histograms { num_histograms = max_histograms } @@ -391,7 +391,7 @@ func splitByteVectorLiteral(data []byte, length uint, literals_per_histogram uin return } - histograms = make([]HistogramLiteral, num_histograms) + histograms = make([]histogramLiteral, num_histograms) /* Find good entropy codes. */ initialEntropyCodesLiteral(data, length, sampling_stride_length, num_histograms, histograms) diff --git a/brotli_bit_stream.go b/brotli_bit_stream.go index d4f1f6b..6e296a3 100644 --- a/brotli_bit_stream.go +++ b/brotli_bit_stream.go @@ -99,7 +99,7 @@ func encodeMlen(length uint, bits *uint64, numbits *uint, nibblesbits *uint64) { if length == 1 { lg = 1 } else { - lg = uint(Log2FloorNonZero(uint(uint32(length-1)))) + 1 + lg = uint(log2FloorNonZero(uint(uint32(length-1)))) + 1 } var tmp uint if lg < 16 { @@ -142,7 +142,7 @@ func storeVarLenUint8(n uint, storage_ix *uint, storage []byte) { if n == 0 { BrotliWriteBits(1, 0, storage_ix, storage) } else { - var nbits uint = uint(Log2FloorNonZero(n)) + var nbits uint = uint(log2FloorNonZero(n)) BrotliWriteBits(1, 1, storage_ix, storage) BrotliWriteBits(3, uint64(nbits), storage_ix, storage) BrotliWriteBits(nbits, uint64(n)-(uint64(uint(1))<= count_limit { - InitHuffmanTree(&tree[node:][0], histogram[l], -1, int16(l)) + initHuffmanTree(&tree[node:][0], histogram[l], -1, int16(l)) } else { - InitHuffmanTree(&tree[node:][0], count_limit, -1, int16(l)) + initHuffmanTree(&tree[node:][0], count_limit, -1, int16(l)) } node++ @@ -464,12 +464,12 @@ func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_ { var n int = node /* Points to the next leaf node. */ /* Points to the next non-leaf node. */ - var sentinel HuffmanTree + var sentinel huffmanTree var i int = 0 var j int = n + 1 var k int - SortHuffmanTreeItems(tree, uint(n), HuffmanTreeComparator(sortHuffmanTree1)) + sortHuffmanTreeItems(tree, uint(n), huffmanTreeComparator(sortHuffmanTree1)) /* The nodes are: [0, n): the sorted leaf nodes that we start with. @@ -478,7 +478,7 @@ func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_ (n+1). These are naturally in ascending order. [2n]: we add a sentinel at the end as well. There will be (2n+1) elements at the end. */ - InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1) + initHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1) tree[node] = sentinel node++ @@ -515,7 +515,7 @@ func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_ node++ } - if BrotliSetDepth(2*n-1, tree, depth, 14) { + if setDepth(2*n-1, tree, depth, 14) { /* We need to pack the Huffman tree in 14 bits. If this was not successful, add fake entities to the lowest values and retry. */ break @@ -526,7 +526,7 @@ func buildAndStoreHuffmanTreeFast(histogram []uint32, histogram_total uint, max_ tree = nil } - BrotliConvertBitDepthsToSymbols(depth, length, bits) + convertBitDepthsToSymbols(depth, length, bits) if count <= 4 { var i uint @@ -683,7 +683,7 @@ func runLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length } if max_reps > 0 { - max_prefix = Log2FloorNonZero(uint(max_reps)) + max_prefix = log2FloorNonZero(uint(max_reps)) } else { max_prefix = 0 } @@ -706,7 +706,7 @@ func runLengthCodeZeros(in_size uint, v []uint32, out_size *uint, max_run_length i += uint(reps) for reps != 0 { if reps < 2< 1 { var repeat_code uint = context_bits - 1 @@ -898,7 +898,7 @@ func cleanupBlockEncoder(self *blockEncoder) { /* Creates entropy codes of block lengths and block types and stores them to the bit stream. */ -func buildAndStoreBlockSwitchEntropyCodes(self *blockEncoder, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreBlockSwitchEntropyCodes(self *blockEncoder, tree []huffmanTree, storage_ix *uint, storage []byte) { buildAndStoreBlockSplitCode(self.block_types_, self.block_lengths_, self.num_blocks_, self.num_block_types_, tree, &self.block_split_code_, storage_ix, storage) } @@ -944,7 +944,7 @@ func storeSymbolWithContext(self *blockEncoder, symbol uint, context uint, conte } } -func buildAndStoreEntropyCodesLiteral(self *blockEncoder, histograms []HistogramLiteral, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesLiteral(self *blockEncoder, histograms []histogramLiteral, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -957,7 +957,7 @@ func buildAndStoreEntropyCodesLiteral(self *blockEncoder, histograms []Histogram } } -func buildAndStoreEntropyCodesCommand(self *blockEncoder, histograms []HistogramCommand, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesCommand(self *blockEncoder, histograms []histogramCommand, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -970,7 +970,7 @@ func buildAndStoreEntropyCodesCommand(self *blockEncoder, histograms []Histogram } } -func buildAndStoreEntropyCodesDistance(self *blockEncoder, histograms []HistogramDistance, histograms_size uint, alphabet_size uint, tree []HuffmanTree, storage_ix *uint, storage []byte) { +func buildAndStoreEntropyCodesDistance(self *blockEncoder, histograms []histogramDistance, histograms_size uint, alphabet_size uint, tree []huffmanTree, storage_ix *uint, storage []byte) { var table_size uint = histograms_size * self.histogram_length_ self.depths_ = make([]byte, table_size) self.bits_ = make([]uint16, table_size) @@ -988,24 +988,24 @@ func jumpToByteBoundary(storage_ix *uint, storage []byte) { storage[*storage_ix>>3] = 0 } -func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_byte byte, prev_byte2 byte, is_last bool, params *BrotliEncoderParams, literal_context_mode int, commands []command, n_commands uint, mb *MetaBlockSplit, storage_ix *uint, storage []byte) { +func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_byte byte, prev_byte2 byte, is_last bool, params *encoderParams, literal_context_mode int, commands []command, n_commands uint, mb *metaBlockSplit, storage_ix *uint, storage []byte) { var pos uint = start_pos var i uint var num_distance_symbols uint32 = params.dist.alphabet_size var num_effective_distance_symbols uint32 = num_distance_symbols - var tree []HuffmanTree - var literal_context_lut ContextLut = BROTLI_CONTEXT_LUT(literal_context_mode) + var tree []huffmanTree + var literal_context_lut contextLUT = getContextLUT(literal_context_mode) var literal_enc blockEncoder var command_enc blockEncoder var distance_enc blockEncoder - var dist *BrotliDistanceParams = ¶ms.dist - if params.large_window && num_effective_distance_symbols > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS { - num_effective_distance_symbols = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS + var dist *distanceParams = ¶ms.dist + if params.large_window && num_effective_distance_symbols > numHistogramDistanceSymbols { + num_effective_distance_symbols = numHistogramDistanceSymbols } storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) - tree = make([]HuffmanTree, maxHuffmanTreeSize) + tree = make([]huffmanTree, maxHuffmanTreeSize) initBlockEncoder(&literal_enc, numLiteralSymbols, mb.literal_split.num_types, mb.literal_split.types, mb.literal_split.lengths, mb.literal_split.num_blocks) initBlockEncoder(&command_enc, numCommandSymbols, mb.command_split.num_types, mb.command_split.types, mb.command_split.lengths, mb.command_split.num_blocks) initBlockEncoder(&distance_enc, uint(num_effective_distance_symbols), mb.distance_split.num_types, mb.distance_split.types, mb.distance_split.lengths, mb.distance_split.num_blocks) @@ -1021,13 +1021,13 @@ func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_b } if mb.literal_context_map_size == 0 { - storeTrivialContextMap(mb.literal_histograms_size, BROTLI_LITERAL_CONTEXT_BITS, tree, storage_ix, storage) + storeTrivialContextMap(mb.literal_histograms_size, literalContextBits, tree, storage_ix, storage) } else { encodeContextMap(mb.literal_context_map, mb.literal_context_map_size, mb.literal_histograms_size, tree, storage_ix, storage) } if mb.distance_context_map_size == 0 { - storeTrivialContextMap(mb.distance_histograms_size, BROTLI_DISTANCE_CONTEXT_BITS, tree, storage_ix, storage) + storeTrivialContextMap(mb.distance_histograms_size, distanceContextBits, tree, storage_ix, storage) } else { encodeContextMap(mb.distance_context_map, mb.distance_context_map_size, mb.distance_histograms_size, tree, storage_ix, storage) } @@ -1051,9 +1051,9 @@ func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_b } else { var j uint for j = uint(cmd.insert_len_); j != 0; j-- { - var context uint = uint(BROTLI_CONTEXT(prev_byte, prev_byte2, literal_context_lut)) + var context uint = uint(getContext(prev_byte, prev_byte2, literal_context_lut)) var literal byte = input[pos&mask] - storeSymbolWithContext(&literal_enc, uint(literal), context, mb.literal_context_map, storage_ix, storage, BROTLI_LITERAL_CONTEXT_BITS) + storeSymbolWithContext(&literal_enc, uint(literal), context, mb.literal_context_map, storage_ix, storage, literalContextBits) prev_byte2 = prev_byte prev_byte = literal pos++ @@ -1072,7 +1072,7 @@ func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_b storeSymbol(&distance_enc, dist_code, storage_ix, storage) } else { var context uint = uint(commandDistanceContext(&cmd)) - storeSymbolWithContext(&distance_enc, dist_code, context, mb.distance_context_map, storage_ix, storage, BROTLI_DISTANCE_CONTEXT_BITS) + storeSymbolWithContext(&distance_enc, dist_code, context, mb.distance_context_map, storage_ix, storage, distanceContextBits) } BrotliWriteBits(uint(distnumextra), distextra, storage_ix, storage) @@ -1088,21 +1088,21 @@ func storeMetaBlock(input []byte, start_pos uint, length uint, mask uint, prev_b } } -func buildHistograms(input []byte, start_pos uint, mask uint, commands []command, n_commands uint, lit_histo *HistogramLiteral, cmd_histo *HistogramCommand, dist_histo *HistogramDistance) { +func buildHistograms(input []byte, start_pos uint, mask uint, commands []command, n_commands uint, lit_histo *histogramLiteral, cmd_histo *histogramCommand, dist_histo *histogramDistance) { var pos uint = start_pos var i uint for i = 0; i < n_commands; i++ { var cmd command = commands[i] var j uint - HistogramAddCommand(cmd_histo, uint(cmd.cmd_prefix_)) + histogramAddCommand(cmd_histo, uint(cmd.cmd_prefix_)) for j = uint(cmd.insert_len_); j != 0; j-- { - HistogramAddLiteral(lit_histo, uint(input[pos&mask])) + histogramAddLiteral(lit_histo, uint(input[pos&mask])) pos++ } pos += uint(commandCopyLen(&cmd)) if commandCopyLen(&cmd) != 0 && cmd.cmd_prefix_ >= 128 { - HistogramAddDistance(dist_histo, uint(cmd.dist_prefix_)&0x3FF) + histogramAddDistance(dist_histo, uint(cmd.dist_prefix_)&0x3FF) } } } @@ -1133,30 +1133,30 @@ func storeDataWithHuffmanCodes(input []byte, start_pos uint, mask uint, commands } } -func storeMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { - var lit_histo HistogramLiteral - var cmd_histo HistogramCommand - var dist_histo HistogramDistance +func storeMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *encoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { + var lit_histo histogramLiteral + var cmd_histo histogramCommand + var dist_histo histogramDistance var lit_depth [numLiteralSymbols]byte var lit_bits [numLiteralSymbols]uint16 var cmd_depth [numCommandSymbols]byte var cmd_bits [numCommandSymbols]uint16 var dist_depth [maxSimpleDistanceAlphabetSize]byte var dist_bits [maxSimpleDistanceAlphabetSize]uint16 - var tree []HuffmanTree + var tree []huffmanTree var num_distance_symbols uint32 = params.dist.alphabet_size storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) - HistogramClearLiteral(&lit_histo) - HistogramClearCommand(&cmd_histo) - HistogramClearDistance(&dist_histo) + histogramClearLiteral(&lit_histo) + histogramClearCommand(&cmd_histo) + histogramClearDistance(&dist_histo) buildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) BrotliWriteBits(13, 0, storage_ix, storage) - tree = make([]HuffmanTree, maxHuffmanTreeSize) + tree = make([]huffmanTree, maxHuffmanTreeSize) buildAndStoreHuffmanTree(lit_histo.data_[:], numLiteralSymbols, numLiteralSymbols, tree, lit_depth[:], lit_bits[:], storage_ix, storage) buildAndStoreHuffmanTree(cmd_histo.data_[:], numCommandSymbols, numCommandSymbols, tree, cmd_depth[:], cmd_bits[:], storage_ix, storage) buildAndStoreHuffmanTree(dist_histo.data_[:], maxSimpleDistanceAlphabetSize, uint(num_distance_symbols), tree, dist_depth[:], dist_bits[:], storage_ix, storage) @@ -1167,9 +1167,9 @@ func storeMetaBlockTrivial(input []byte, start_pos uint, length uint, mask uint, } } -func storeMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *BrotliEncoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { +func storeMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is_last bool, params *encoderParams, commands []command, n_commands uint, storage_ix *uint, storage []byte) { var num_distance_symbols uint32 = params.dist.alphabet_size - var distance_alphabet_bits uint32 = Log2FloorNonZero(uint(num_distance_symbols-1)) + 1 + var distance_alphabet_bits uint32 = log2FloorNonZero(uint(num_distance_symbols-1)) + 1 storeCompressedMetaBlockHeader(is_last, length, storage_ix, storage) @@ -1197,22 +1197,22 @@ func storeMetaBlockFast(input []byte, start_pos uint, length uint, mask uint, is buildAndStoreHuffmanTreeFast(histogram[:], num_literals, /* max_bits = */ 8, lit_depth[:], lit_bits[:], storage_ix, storage) - StoreStaticCommandHuffmanTree(storage_ix, storage) - StoreStaticDistanceHuffmanTree(storage_ix, storage) + storeStaticCommandHuffmanTree(storage_ix, storage) + storeStaticDistanceHuffmanTree(storage_ix, storage) storeDataWithHuffmanCodes(input, start_pos, mask, commands, n_commands, lit_depth[:], lit_bits[:], kStaticCommandCodeDepth[:], kStaticCommandCodeBits[:], kStaticDistanceCodeDepth[:], kStaticDistanceCodeBits[:], storage_ix, storage) } else { - var lit_histo HistogramLiteral - var cmd_histo HistogramCommand - var dist_histo HistogramDistance + var lit_histo histogramLiteral + var cmd_histo histogramCommand + var dist_histo histogramDistance var lit_depth [numLiteralSymbols]byte var lit_bits [numLiteralSymbols]uint16 var cmd_depth [numCommandSymbols]byte var cmd_bits [numCommandSymbols]uint16 var dist_depth [maxSimpleDistanceAlphabetSize]byte var dist_bits [maxSimpleDistanceAlphabetSize]uint16 - HistogramClearLiteral(&lit_histo) - HistogramClearCommand(&cmd_histo) - HistogramClearDistance(&dist_histo) + histogramClearLiteral(&lit_histo) + histogramClearCommand(&cmd_histo) + histogramClearDistance(&dist_histo) buildHistograms(input, start_pos, mask, commands, n_commands, &lit_histo, &cmd_histo, &dist_histo) buildAndStoreHuffmanTreeFast(lit_histo.data_[:], lit_histo.total_count_, /* max_bits = */ 8, lit_depth[:], lit_bits[:], storage_ix, storage) diff --git a/cluster.go b/cluster.go index 6900853..1cd56d6 100644 --- a/cluster.go +++ b/cluster.go @@ -39,5 +39,5 @@ func histogramPairIsLess(p1 *histogramPair, p2 *histogramPair) bool { /* Returns entropy reduction of the context map when we combine two clusters. */ func clusterCostDiff(size_a uint, size_b uint) float64 { var size_c uint = size_a + size_b - return float64(size_a)*FastLog2(size_a) + float64(size_b)*FastLog2(size_b) - float64(size_c)*FastLog2(size_c) + return float64(size_a)*fastLog2(size_a) + float64(size_b)*fastLog2(size_b) - float64(size_c)*fastLog2(size_c) } diff --git a/cluster_command.go b/cluster_command.go index 72607a7..c1bee18 100644 --- a/cluster_command.go +++ b/cluster_command.go @@ -9,7 +9,7 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func compareAndPushToQueueCommand(out []HistogramCommand, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { +func compareAndPushToQueueCommand(out []histogramCommand, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false var p histogramPair p.idx2 = 0 @@ -45,9 +45,9 @@ func compareAndPushToQueueCommand(out []HistogramCommand, cluster_size []uint32, } else { threshold = brotli_max_double(0.0, pairs[0].cost_diff) } - var combo HistogramCommand = out[idx1] + var combo histogramCommand = out[idx1] var cost_combo float64 - HistogramAddHistogramCommand(&combo, &out[idx2]) + histogramAddHistogramCommand(&combo, &out[idx2]) cost_combo = populationCostCommand(&combo) if cost_combo < threshold-p.cost_diff { p.cost_combo = cost_combo @@ -72,7 +72,7 @@ func compareAndPushToQueueCommand(out []HistogramCommand, cluster_size []uint32, } } -func histogramCombineCommand(out []HistogramCommand, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineCommand(out []histogramCommand, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -102,7 +102,7 @@ func histogramCombineCommand(out []HistogramCommand, cluster_size []uint32, symb best_idx1 = pairs[0].idx1 best_idx2 = pairs[0].idx2 - HistogramAddHistogramCommand(&out[best_idx1], &out[best_idx2]) + histogramAddHistogramCommand(&out[best_idx1], &out[best_idx2]) out[best_idx1].bit_cost_ = pairs[0].cost_combo cluster_size[best_idx1] += cluster_size[best_idx2] for i = 0; i < symbols_size; i++ { @@ -154,12 +154,12 @@ func histogramCombineCommand(out []HistogramCommand, cluster_size []uint32, symb } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func histogramBitCostDistanceCommand(histogram *HistogramCommand, candidate *HistogramCommand) float64 { +func histogramBitCostDistanceCommand(histogram *histogramCommand, candidate *histogramCommand) float64 { if histogram.total_count_ == 0 { return 0.0 } else { - var tmp HistogramCommand = *histogram - HistogramAddHistogramCommand(&tmp, candidate) + var tmp histogramCommand = *histogram + histogramAddHistogramCommand(&tmp, candidate) return populationCostCommand(&tmp) - candidate.bit_cost_ } } @@ -168,7 +168,7 @@ func histogramBitCostDistanceCommand(histogram *HistogramCommand, candidate *His When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func histogramRemapCommand(in []HistogramCommand, in_size uint, clusters []uint32, num_clusters uint, out []HistogramCommand, symbols []uint32) { +func histogramRemapCommand(in []histogramCommand, in_size uint, clusters []uint32, num_clusters uint, out []histogramCommand, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -192,11 +192,11 @@ func histogramRemapCommand(in []HistogramCommand, in_size uint, clusters []uint3 /* Recompute each out based on raw and symbols. */ for i = 0; i < num_clusters; i++ { - HistogramClearCommand(&out[clusters[i]]) + histogramClearCommand(&out[clusters[i]]) } for i = 0; i < in_size; i++ { - HistogramAddHistogramCommand(&out[symbols[i]], &in[i]) + histogramAddHistogramCommand(&out[symbols[i]], &in[i]) } } @@ -213,10 +213,10 @@ func histogramRemapCommand(in []HistogramCommand, in_size uint, clusters []uint3 var histogramReindexCommand_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func histogramReindexCommand(out []HistogramCommand, symbols []uint32, length uint) uint { +func histogramReindexCommand(out []histogramCommand, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 - var tmp []HistogramCommand + var tmp []histogramCommand var i uint for i = 0; i < length; i++ { new_index[i] = histogramReindexCommand_kInvalidIndex @@ -232,7 +232,7 @@ func histogramReindexCommand(out []HistogramCommand, symbols []uint32, length ui /* TODO: by using idea of "cycle-sort" we can avoid allocation of tmp and reduce the number of copying by the factor of 2. */ - tmp = make([]HistogramCommand, next_index) + tmp = make([]histogramCommand, next_index) next_index = 0 for i = 0; i < length; i++ { @@ -253,7 +253,7 @@ func histogramReindexCommand(out []HistogramCommand, symbols []uint32, length ui return uint(next_index) } -func clusterHistogramsCommand(in []HistogramCommand, in_size uint, max_histograms uint, out []HistogramCommand, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsCommand(in []histogramCommand, in_size uint, max_histograms uint, out []histogramCommand, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 diff --git a/cluster_distance.go b/cluster_distance.go index 355e64a..0aa74a2 100644 --- a/cluster_distance.go +++ b/cluster_distance.go @@ -9,7 +9,7 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func compareAndPushToQueueDistance(out []HistogramDistance, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { +func compareAndPushToQueueDistance(out []histogramDistance, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false var p histogramPair p.idx2 = 0 @@ -45,9 +45,9 @@ func compareAndPushToQueueDistance(out []HistogramDistance, cluster_size []uint3 } else { threshold = brotli_max_double(0.0, pairs[0].cost_diff) } - var combo HistogramDistance = out[idx1] + var combo histogramDistance = out[idx1] var cost_combo float64 - HistogramAddHistogramDistance(&combo, &out[idx2]) + histogramAddHistogramDistance(&combo, &out[idx2]) cost_combo = populationCostDistance(&combo) if cost_combo < threshold-p.cost_diff { p.cost_combo = cost_combo @@ -72,7 +72,7 @@ func compareAndPushToQueueDistance(out []HistogramDistance, cluster_size []uint3 } } -func histogramCombineDistance(out []HistogramDistance, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineDistance(out []histogramDistance, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -102,7 +102,7 @@ func histogramCombineDistance(out []HistogramDistance, cluster_size []uint32, sy best_idx1 = pairs[0].idx1 best_idx2 = pairs[0].idx2 - HistogramAddHistogramDistance(&out[best_idx1], &out[best_idx2]) + histogramAddHistogramDistance(&out[best_idx1], &out[best_idx2]) out[best_idx1].bit_cost_ = pairs[0].cost_combo cluster_size[best_idx1] += cluster_size[best_idx2] for i = 0; i < symbols_size; i++ { @@ -154,12 +154,12 @@ func histogramCombineDistance(out []HistogramDistance, cluster_size []uint32, sy } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func histogramBitCostDistanceDistance(histogram *HistogramDistance, candidate *HistogramDistance) float64 { +func histogramBitCostDistanceDistance(histogram *histogramDistance, candidate *histogramDistance) float64 { if histogram.total_count_ == 0 { return 0.0 } else { - var tmp HistogramDistance = *histogram - HistogramAddHistogramDistance(&tmp, candidate) + var tmp histogramDistance = *histogram + histogramAddHistogramDistance(&tmp, candidate) return populationCostDistance(&tmp) - candidate.bit_cost_ } } @@ -168,7 +168,7 @@ func histogramBitCostDistanceDistance(histogram *HistogramDistance, candidate *H When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func histogramRemapDistance(in []HistogramDistance, in_size uint, clusters []uint32, num_clusters uint, out []HistogramDistance, symbols []uint32) { +func histogramRemapDistance(in []histogramDistance, in_size uint, clusters []uint32, num_clusters uint, out []histogramDistance, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -192,11 +192,11 @@ func histogramRemapDistance(in []HistogramDistance, in_size uint, clusters []uin /* Recompute each out based on raw and symbols. */ for i = 0; i < num_clusters; i++ { - HistogramClearDistance(&out[clusters[i]]) + histogramClearDistance(&out[clusters[i]]) } for i = 0; i < in_size; i++ { - HistogramAddHistogramDistance(&out[symbols[i]], &in[i]) + histogramAddHistogramDistance(&out[symbols[i]], &in[i]) } } @@ -213,10 +213,10 @@ func histogramRemapDistance(in []HistogramDistance, in_size uint, clusters []uin var histogramReindexDistance_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func histogramReindexDistance(out []HistogramDistance, symbols []uint32, length uint) uint { +func histogramReindexDistance(out []histogramDistance, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 - var tmp []HistogramDistance + var tmp []histogramDistance var i uint for i = 0; i < length; i++ { new_index[i] = histogramReindexDistance_kInvalidIndex @@ -232,7 +232,7 @@ func histogramReindexDistance(out []HistogramDistance, symbols []uint32, length /* TODO: by using idea of "cycle-sort" we can avoid allocation of tmp and reduce the number of copying by the factor of 2. */ - tmp = make([]HistogramDistance, next_index) + tmp = make([]histogramDistance, next_index) next_index = 0 for i = 0; i < length; i++ { @@ -253,7 +253,7 @@ func histogramReindexDistance(out []HistogramDistance, symbols []uint32, length return uint(next_index) } -func clusterHistogramsDistance(in []HistogramDistance, in_size uint, max_histograms uint, out []HistogramDistance, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsDistance(in []histogramDistance, in_size uint, max_histograms uint, out []histogramDistance, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 diff --git a/cluster_literal.go b/cluster_literal.go index 2d4d108..eeb1de9 100644 --- a/cluster_literal.go +++ b/cluster_literal.go @@ -9,7 +9,7 @@ package brotli /* Computes the bit cost reduction by combining out[idx1] and out[idx2] and if it is below a threshold, stores the pair (idx1, idx2) in the *pairs queue. */ -func compareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { +func compareAndPushToQueueLiteral(out []histogramLiteral, cluster_size []uint32, idx1 uint32, idx2 uint32, max_num_pairs uint, pairs []histogramPair, num_pairs *uint) { var is_good_pair bool = false var p histogramPair p.idx2 = 0 @@ -45,9 +45,9 @@ func compareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []uint32, } else { threshold = brotli_max_double(0.0, pairs[0].cost_diff) } - var combo HistogramLiteral = out[idx1] + var combo histogramLiteral = out[idx1] var cost_combo float64 - HistogramAddHistogramLiteral(&combo, &out[idx2]) + histogramAddHistogramLiteral(&combo, &out[idx2]) cost_combo = populationCostLiteral(&combo) if cost_combo < threshold-p.cost_diff { p.cost_combo = cost_combo @@ -72,7 +72,7 @@ func compareAndPushToQueueLiteral(out []HistogramLiteral, cluster_size []uint32, } } -func histogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { +func histogramCombineLiteral(out []histogramLiteral, cluster_size []uint32, symbols []uint32, clusters []uint32, pairs []histogramPair, num_clusters uint, symbols_size uint, max_clusters uint, max_num_pairs uint) uint { var cost_diff_threshold float64 = 0.0 var min_cluster_size uint = 1 var num_pairs uint = 0 @@ -102,7 +102,7 @@ func histogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32, symb best_idx1 = pairs[0].idx1 best_idx2 = pairs[0].idx2 - HistogramAddHistogramLiteral(&out[best_idx1], &out[best_idx2]) + histogramAddHistogramLiteral(&out[best_idx1], &out[best_idx2]) out[best_idx1].bit_cost_ = pairs[0].cost_combo cluster_size[best_idx1] += cluster_size[best_idx2] for i = 0; i < symbols_size; i++ { @@ -154,12 +154,12 @@ func histogramCombineLiteral(out []HistogramLiteral, cluster_size []uint32, symb } /* What is the bit cost of moving histogram from cur_symbol to candidate. */ -func histogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidate *HistogramLiteral) float64 { +func histogramBitCostDistanceLiteral(histogram *histogramLiteral, candidate *histogramLiteral) float64 { if histogram.total_count_ == 0 { return 0.0 } else { - var tmp HistogramLiteral = *histogram - HistogramAddHistogramLiteral(&tmp, candidate) + var tmp histogramLiteral = *histogram + histogramAddHistogramLiteral(&tmp, candidate) return populationCostLiteral(&tmp) - candidate.bit_cost_ } } @@ -168,7 +168,7 @@ func histogramBitCostDistanceLiteral(histogram *HistogramLiteral, candidate *His When called, clusters[0..num_clusters) contains the unique values from symbols[0..in_size), but this property is not preserved in this function. Note: we assume that out[]->bit_cost_ is already up-to-date. */ -func histogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters []uint32, num_clusters uint, out []HistogramLiteral, symbols []uint32) { +func histogramRemapLiteral(in []histogramLiteral, in_size uint, clusters []uint32, num_clusters uint, out []histogramLiteral, symbols []uint32) { var i uint for i = 0; i < in_size; i++ { var best_out uint32 @@ -192,11 +192,11 @@ func histogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters []uint3 /* Recompute each out based on raw and symbols. */ for i = 0; i < num_clusters; i++ { - HistogramClearLiteral(&out[clusters[i]]) + histogramClearLiteral(&out[clusters[i]]) } for i = 0; i < in_size; i++ { - HistogramAddHistogramLiteral(&out[symbols[i]], &in[i]) + histogramAddHistogramLiteral(&out[symbols[i]], &in[i]) } } @@ -213,10 +213,10 @@ func histogramRemapLiteral(in []HistogramLiteral, in_size uint, clusters []uint3 var histogramReindexLiteral_kInvalidIndex uint32 = BROTLI_UINT32_MAX -func histogramReindexLiteral(out []HistogramLiteral, symbols []uint32, length uint) uint { +func histogramReindexLiteral(out []histogramLiteral, symbols []uint32, length uint) uint { var new_index []uint32 = make([]uint32, length) var next_index uint32 - var tmp []HistogramLiteral + var tmp []histogramLiteral var i uint for i = 0; i < length; i++ { new_index[i] = histogramReindexLiteral_kInvalidIndex @@ -232,7 +232,7 @@ func histogramReindexLiteral(out []HistogramLiteral, symbols []uint32, length ui /* TODO: by using idea of "cycle-sort" we can avoid allocation of tmp and reduce the number of copying by the factor of 2. */ - tmp = make([]HistogramLiteral, next_index) + tmp = make([]histogramLiteral, next_index) next_index = 0 for i = 0; i < length; i++ { @@ -253,7 +253,7 @@ func histogramReindexLiteral(out []HistogramLiteral, symbols []uint32, length ui return uint(next_index) } -func clusterHistogramsLiteral(in []HistogramLiteral, in_size uint, max_histograms uint, out []HistogramLiteral, out_size *uint, histogram_symbols []uint32) { +func clusterHistogramsLiteral(in []histogramLiteral, in_size uint, max_histograms uint, out []histogramLiteral, out_size *uint, histogram_symbols []uint32) { var cluster_size []uint32 = make([]uint32, in_size) var clusters []uint32 = make([]uint32, in_size) var num_clusters uint = 0 diff --git a/command.go b/command.go index 04c8da6..05a5ab0 100644 --- a/command.go +++ b/command.go @@ -112,10 +112,10 @@ func getInsertLengthCode(insertlen uint) uint16 { if insertlen < 6 { return uint16(insertlen) } else if insertlen < 130 { - var nbits uint32 = Log2FloorNonZero(insertlen-2) - 1 + var nbits uint32 = log2FloorNonZero(insertlen-2) - 1 return uint16((nbits << 1) + uint32((insertlen-2)>>nbits) + 2) } else if insertlen < 2114 { - return uint16(Log2FloorNonZero(insertlen-66) + 10) + return uint16(log2FloorNonZero(insertlen-66) + 10) } else if insertlen < 6210 { return 21 } else if insertlen < 22594 { @@ -129,10 +129,10 @@ func getCopyLengthCode(copylen uint) uint16 { if copylen < 10 { return uint16(copylen - 2) } else if copylen < 134 { - var nbits uint32 = Log2FloorNonZero(copylen-6) - 1 + var nbits uint32 = log2FloorNonZero(copylen-6) - 1 return uint16((nbits << 1) + uint32((copylen-6)>>nbits) + 4) } else if copylen < 2118 { - return uint16(Log2FloorNonZero(copylen-70) + 12) + return uint16(log2FloorNonZero(copylen-70) + 12) } else { return 23 } @@ -194,7 +194,7 @@ type command struct { } /* distance_code is e.g. 0 for same-as-last short code, or 16 for offset 1. */ -func initCommand(self *command, dist *BrotliDistanceParams, insertlen uint, copylen uint, copylen_code_delta int, distance_code uint) { +func initCommand(self *command, dist *distanceParams, insertlen uint, copylen uint, copylen_code_delta int, distance_code uint) { /* Don't rely on signed int representation, use honest casts. */ var delta uint32 = uint32(byte(int8(copylen_code_delta))) self.insert_len_ = uint32(insertlen) @@ -216,7 +216,7 @@ func initInsertCommand(self *command, insertlen uint) { getLengthCode(insertlen, 4, false, &self.cmd_prefix_) } -func commandRestoreDistanceCode(self *command, dist *BrotliDistanceParams) uint32 { +func commandRestoreDistanceCode(self *command, dist *distanceParams) uint32 { if uint32(self.dist_prefix_&0x3FF) < numDistanceShortCodes+dist.num_direct_distance_codes { return uint32(self.dist_prefix_) & 0x3FF } else { diff --git a/compress_fragment.go b/compress_fragment.go index 02ca19a..deb8d1a 100644 --- a/compress_fragment.go +++ b/compress_fragment.go @@ -135,14 +135,14 @@ func buildAndStoreLiteralPrefixCode(input []byte, input_size uint, depths []byte /* Builds a command and distance prefix code (each 64 symbols) into "depth" and "bits" based on "histogram" and stores it into the bit stream. */ func buildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { - var tree [129]HuffmanTree + var tree [129]huffmanTree var cmd_depth = [numCommandSymbols]byte{0} /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */ var cmd_bits [64]uint16 - BrotliCreateHuffmanTree(histogram, 64, 15, tree[:], depth) - BrotliCreateHuffmanTree(histogram[64:], 64, 14, tree[:], depth[64:]) + createHuffmanTree(histogram, 64, 15, tree[:], depth) + createHuffmanTree(histogram[64:], 64, 14, tree[:], depth[64:]) /* We have to jump through a few hoops here in order to compute the command bits because the symbols are in a different order than in @@ -156,14 +156,14 @@ func buildAndStoreCommandPrefixCode1(histogram []uint32, depth []byte, bits []ui copy(cmd_depth[40:][:], depth[48:][:8]) copy(cmd_depth[48:][:], depth[32:][:8]) copy(cmd_depth[56:][:], depth[56:][:8]) - BrotliConvertBitDepthsToSymbols(cmd_depth[:], 64, cmd_bits[:]) + convertBitDepthsToSymbols(cmd_depth[:], 64, cmd_bits[:]) copy(bits, cmd_bits[:24]) copy(bits[24:], cmd_bits[32:][:8]) copy(bits[32:], cmd_bits[48:][:8]) copy(bits[40:], cmd_bits[24:][:8]) copy(bits[48:], cmd_bits[40:][:8]) copy(bits[56:], cmd_bits[56:][:8]) - BrotliConvertBitDepthsToSymbols(depth[64:], 64, bits[64:]) + convertBitDepthsToSymbols(depth[64:], 64, bits[64:]) { /* Create the bit length array for the full command alphabet. */ var i uint @@ -195,7 +195,7 @@ func emitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, histo[code]++ } else if insertlen < 130 { var tail uint = insertlen - 2 - var nbits uint32 = Log2FloorNonZero(tail) - 1 + var nbits uint32 = log2FloorNonZero(tail) - 1 var prefix uint = tail >> nbits var inscode uint = uint((nbits << 1) + uint32(prefix) + 42) BrotliWriteBits(uint(depth[inscode]), uint64(bits[inscode]), storage_ix, storage) @@ -203,7 +203,7 @@ func emitInsertLen1(insertlen uint, depth []byte, bits []uint16, histo []uint32, histo[inscode]++ } else if insertlen < 2114 { var tail uint = insertlen - 66 - var nbits uint32 = Log2FloorNonZero(tail) + var nbits uint32 = log2FloorNonZero(tail) var code uint = uint(nbits + 50) BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) BrotliWriteBits(uint(nbits), uint64(tail)-(uint64(uint(1))<> nbits var code uint = uint((nbits << 1) + uint32(prefix) + 20) BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) @@ -241,7 +241,7 @@ func emitCopyLen1(copylen uint, depth []byte, bits []uint16, histo []uint32, sto histo[code]++ } else if copylen < 2118 { var tail uint = copylen - 70 - var nbits uint32 = Log2FloorNonZero(tail) + var nbits uint32 = log2FloorNonZero(tail) var code uint = uint(nbits + 28) BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) BrotliWriteBits(uint(nbits), uint64(tail)-(uint64(uint(1))<> nbits var code uint = uint((nbits << 1) + uint32(prefix) + 4) BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) @@ -275,7 +275,7 @@ func emitCopyLenLastDistance1(copylen uint, depth []byte, bits []uint16, histo [ histo[64]++ } else if copylen < 2120 { var tail uint = copylen - 72 - var nbits uint32 = Log2FloorNonZero(tail) + var nbits uint32 = log2FloorNonZero(tail) var code uint = uint(nbits + 28) BrotliWriteBits(uint(depth[code]), uint64(bits[code]), storage_ix, storage) BrotliWriteBits(uint(nbits), uint64(tail)-(uint64(uint(1))<> nbits) & 1 var offset uint = (2 + prefix) << nbits var distcode uint = uint(2*(nbits-1) + uint32(prefix) + 80) @@ -363,9 +363,9 @@ func shouldMergeBlock(data []byte, len uint, depths []byte) bool { } { var total uint = (len + shouldMergeBlock_kSampleRate - 1) / shouldMergeBlock_kSampleRate - var r float64 = (FastLog2(total)+0.5)*float64(total) + 200 + var r float64 = (fastLog2(total)+0.5)*float64(total) + 200 for i = 0; i < 256; i++ { - r -= float64(histo[i]) * (float64(depths[i]) + FastLog2(histo[i])) + r -= float64(histo[i]) * (float64(depths[i]) + fastLog2(histo[i])) } return r >= 0.0 @@ -531,7 +531,7 @@ func compressFragmentFastImpl(in []byte, input_size uint, is_last bool, table [] var next_emit int = 0 var base_ip int = 0 var input int = 0 - var kInputMarginBytes uint = BROTLI_WINDOW_GAP + var kInputMarginBytes uint = windowGap var kMinMatchLen uint = 5 var metablock_start int = input var block_size uint = brotli_min_size_t(input_size, compressFragmentFastImpl_kFirstBlockSize) @@ -647,7 +647,7 @@ emit_commands: /* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */ - if ip-candidate > maxDistance { + if ip-candidate > maxDistance_compress_fragment { goto trawl } @@ -658,7 +658,7 @@ emit_commands: { var base int = ip /* > 0 */ - var matched uint = 5 + FindMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5) + var matched uint = 5 + findMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5) var distance int = int(base - candidate) /* We have a 5-byte match at ip, and we need to emit bytes in [next_emit, ip). */ @@ -716,8 +716,8 @@ emit_commands: /* We have a 5-byte match at ip, and no need to emit any literal bytes prior to ip. */ - var matched uint = 5 + FindMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5) - if ip-candidate > maxDistance { + var matched uint = 5 + findMatchLengthWithLimit(in[candidate+5:], in[ip+5:], uint(ip_end-ip)-5) + if ip-candidate > maxDistance_compress_fragment { break } ip += int(matched) @@ -823,7 +823,7 @@ next_block: func compressFragmentFast(input []byte, input_size uint, is_last bool, table []int, table_size uint, cmd_depth []byte, cmd_bits []uint16, cmd_code_numbits *uint, cmd_code []byte, storage_ix *uint, storage []byte) { var initial_storage_ix uint = *storage_ix - var table_bits uint = uint(Log2FloorNonZero(table_size)) + var table_bits uint = uint(log2FloorNonZero(table_size)) if input_size == 0 { assert(is_last) diff --git a/compress_fragment_common.go b/compress_fragment_common.go index 1cdd7b0..1d74fe1 100644 --- a/compress_fragment_common.go +++ b/compress_fragment_common.go @@ -13,7 +13,7 @@ package brotli REQUIRES: "table_size" is a power of two OUTPUT: maximal copy distance <= |input_size| OUTPUT: maximal copy distance <= BROTLI_MAX_BACKWARD_LIMIT(18) */ -const maxDistance = 262128 +const maxDistance_compress_fragment = 262128 /* kHashMul32_a multiplier has these properties: * The multiplier must be odd. Otherwise we may lose the highest bit. diff --git a/compress_fragment_two_pass.go b/compress_fragment_two_pass.go index dabf92a..cad8973 100644 --- a/compress_fragment_two_pass.go +++ b/compress_fragment_two_pass.go @@ -53,13 +53,13 @@ func isMatch1(p1 []byte, p2 []byte, length uint) bool { /* Builds a command and distance prefix code (each 64 symbols) into "depth" and "bits" based on "histogram" and stores it into the bit stream. */ func buildAndStoreCommandPrefixCode(histogram []uint32, depth []byte, bits []uint16, storage_ix *uint, storage []byte) { - var tree [129]HuffmanTree + var tree [129]huffmanTree var cmd_depth = [numCommandSymbols]byte{0} /* Tree size for building a tree over 64 symbols is 2 * 64 + 1. */ var cmd_bits [64]uint16 - BrotliCreateHuffmanTree(histogram, 64, 15, tree[:], depth) - BrotliCreateHuffmanTree(histogram[64:], 64, 14, tree[:], depth[64:]) + createHuffmanTree(histogram, 64, 15, tree[:], depth) + createHuffmanTree(histogram[64:], 64, 14, tree[:], depth[64:]) /* We have to jump through a few hoops here in order to compute the command bits because the symbols are in a different order than in @@ -73,14 +73,14 @@ func buildAndStoreCommandPrefixCode(histogram []uint32, depth []byte, bits []uin copy(cmd_depth[40:][:], depth[8:][:8]) copy(cmd_depth[48:][:], depth[56:][:8]) copy(cmd_depth[56:][:], depth[16:][:8]) - BrotliConvertBitDepthsToSymbols(cmd_depth[:], 64, cmd_bits[:]) + convertBitDepthsToSymbols(cmd_depth[:], 64, cmd_bits[:]) copy(bits, cmd_bits[24:][:8]) copy(bits[8:], cmd_bits[40:][:8]) copy(bits[16:], cmd_bits[56:][:8]) copy(bits[24:], cmd_bits[:24]) copy(bits[48:], cmd_bits[32:][:8]) copy(bits[56:], cmd_bits[48:][:8]) - BrotliConvertBitDepthsToSymbols(depth[64:], 64, bits[64:]) + convertBitDepthsToSymbols(depth[64:], 64, bits[64:]) { /* Create the bit length array for the full command alphabet. */ var i uint @@ -109,14 +109,14 @@ func emitInsertLen(insertlen uint32, commands *[]uint32) { (*commands)[0] = insertlen } else if insertlen < 130 { var tail uint32 = insertlen - 2 - var nbits uint32 = Log2FloorNonZero(uint(tail)) - 1 + var nbits uint32 = log2FloorNonZero(uint(tail)) - 1 var prefix uint32 = tail >> nbits var inscode uint32 = (nbits << 1) + prefix + 2 var extra uint32 = tail - (prefix << nbits) (*commands)[0] = inscode | extra<<8 } else if insertlen < 2114 { var tail uint32 = insertlen - 66 - var nbits uint32 = Log2FloorNonZero(uint(tail)) + var nbits uint32 = log2FloorNonZero(uint(tail)) var code uint32 = nbits + 10 var extra uint32 = tail - (1 << nbits) (*commands)[0] = code | extra<<8 @@ -139,14 +139,14 @@ func emitCopyLen(copylen uint, commands *[]uint32) { (*commands)[0] = uint32(copylen + 38) } else if copylen < 134 { var tail uint = copylen - 6 - var nbits uint = uint(Log2FloorNonZero(tail) - 1) + var nbits uint = uint(log2FloorNonZero(tail) - 1) var prefix uint = tail >> nbits var code uint = (nbits << 1) + prefix + 44 var extra uint = tail - (prefix << nbits) (*commands)[0] = uint32(code | extra<<8) } else if copylen < 2118 { var tail uint = copylen - 70 - var nbits uint = uint(Log2FloorNonZero(tail)) + var nbits uint = uint(log2FloorNonZero(tail)) var code uint = nbits + 52 var extra uint = tail - (uint(1) << nbits) (*commands)[0] = uint32(code | extra<<8) @@ -164,7 +164,7 @@ func emitCopyLenLastDistance(copylen uint, commands *[]uint32) { *commands = (*commands)[1:] } else if copylen < 72 { var tail uint = copylen - 8 - var nbits uint = uint(Log2FloorNonZero(tail) - 1) + var nbits uint = uint(log2FloorNonZero(tail) - 1) var prefix uint = tail >> nbits var code uint = (nbits << 1) + prefix + 28 var extra uint = tail - (prefix << nbits) @@ -180,7 +180,7 @@ func emitCopyLenLastDistance(copylen uint, commands *[]uint32) { *commands = (*commands)[1:] } else if copylen < 2120 { var tail uint = copylen - 72 - var nbits uint = uint(Log2FloorNonZero(tail)) + var nbits uint = uint(log2FloorNonZero(tail)) var code uint = nbits + 52 var extra uint = tail - (uint(1) << nbits) (*commands)[0] = uint32(code | extra<<8) @@ -198,7 +198,7 @@ func emitCopyLenLastDistance(copylen uint, commands *[]uint32) { func emitDistance(distance uint32, commands *[]uint32) { var d uint32 = distance + 3 - var nbits uint32 = Log2FloorNonZero(uint(d)) - 1 + var nbits uint32 = log2FloorNonZero(uint(d)) - 1 var prefix uint32 = (d >> nbits) & 1 var offset uint32 = (2 + prefix) << nbits var distcode uint32 = 2*(nbits-1) + prefix + 80 @@ -236,7 +236,7 @@ func createCommands(input []byte, block_size uint, input_size uint, base_ip_ptr var last_distance int = -1 /* "ip" is the input pointer. */ - var kInputMarginBytes uint = BROTLI_WINDOW_GAP + var kInputMarginBytes uint = windowGap /* "next_emit" is a pointer to the first byte that is not covered by a previous copy. Bytes between "next_emit" and the start of the next copy or @@ -307,7 +307,7 @@ func createCommands(input []byte, block_size uint, input_size uint, base_ip_ptr /* Check copy distance. If candidate is not feasible, continue search. Checking is done outside of hot loop to reduce overhead. */ - if ip-candidate > maxDistance { + if ip-candidate > maxDistance_compress_fragment { goto trawl } @@ -318,7 +318,7 @@ func createCommands(input []byte, block_size uint, input_size uint, base_ip_ptr { var base int = ip /* > 0 */ - var matched uint = min_match + FindMatchLengthWithLimit(input[uint(candidate)+min_match:], input[uint(ip)+min_match:], uint(ip_end-ip)-min_match) + var matched uint = min_match + findMatchLengthWithLimit(input[uint(candidate)+min_match:], input[uint(ip)+min_match:], uint(ip_end-ip)-min_match) var distance int = int(base - candidate) /* We have a 6-byte match at ip, and we need to emit bytes in [next_emit, ip). */ @@ -380,12 +380,12 @@ func createCommands(input []byte, block_size uint, input_size uint, base_ip_ptr } } - for ip-candidate <= maxDistance && isMatch1(input[ip:], input[candidate:], min_match) { + for ip-candidate <= maxDistance_compress_fragment && isMatch1(input[ip:], input[candidate:], min_match) { var base int = ip /* We have a 6-byte match at ip, and no need to emit any literal bytes prior to ip. */ - var matched uint = min_match + FindMatchLengthWithLimit(input[uint(candidate)+min_match:], input[uint(ip)+min_match:], uint(ip_end-ip)-min_match) + var matched uint = min_match + findMatchLengthWithLimit(input[uint(candidate)+min_match:], input[uint(ip)+min_match:], uint(ip_end-ip)-min_match) ip += int(matched) last_distance = int(base - candidate) /* > 0 */ emitCopyLen(matched, commands) @@ -723,7 +723,7 @@ func compressFragmentTwoPassImpl(input []byte, input_size uint, is_last bool, co func compressFragmentTwoPass(input []byte, input_size uint, is_last bool, command_buf []uint32, literal_buf []byte, table []int, table_size uint, storage_ix *uint, storage []byte) { var initial_storage_ix uint = *storage_ix - var table_bits uint = uint(Log2FloorNonZero(table_size)) + var table_bits uint = uint(log2FloorNonZero(table_size)) var min_match uint if table_bits <= 15 { min_match = 4 diff --git a/constants.go b/constants.go index 876cd73..a880dff 100644 --- a/constants.go +++ b/constants.go @@ -56,22 +56,22 @@ func distanceAlphabetSize(NPOSTFIX uint, NDIRECT uint, MAXNBITS uint) uint { /* numDistanceSymbols == 1128 */ const numDistanceSymbols = 1128 -const BROTLI_MAX_DISTANCE = 0x3FFFFFC +const maxDistance = 0x3FFFFFC -const BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC +const maxAllowedDistance = 0x7FFFFFFC /* 7.1. Context modes and context ID lookup for literals */ /* "context IDs for literals are in the range of 0..63" */ -const BROTLI_LITERAL_CONTEXT_BITS = 6 +const literalContextBits = 6 /* 7.2. Context ID for distances */ -const BROTLI_DISTANCE_CONTEXT_BITS = 2 +const distanceContextBits = 2 /* 9.1. Format of the Stream Header */ /* Number of slack bytes for window size. Don't confuse with BROTLI_NUM_DISTANCE_SHORT_CODES. */ -const BROTLI_WINDOW_GAP = 16 +const windowGap = 16 -func BROTLI_MAX_BACKWARD_LIMIT(W uint) uint { - return (uint(1) << W) - BROTLI_WINDOW_GAP +func maxBackwardLimit(W uint) uint { + return (uint(1) << W) - windowGap } diff --git a/context.go b/context.go index 5958f7a..f4a1317 100644 --- a/context.go +++ b/context.go @@ -179,17 +179,17 @@ package brotli /* Lookup table to map the previous two bytes to a context id. There are four different context modeling modes defined here: - CONTEXT_LSB6: context id is the least significant 6 bits of the last byte, - CONTEXT_MSB6: context id is the most significant 6 bits of the last byte, - CONTEXT_UTF8: second-order context model tuned for UTF8-encoded text, - CONTEXT_SIGNED: second-order context model tuned for signed integers. + contextLSB6: context id is the least significant 6 bits of the last byte, + contextMSB6: context id is the most significant 6 bits of the last byte, + contextUTF8: second-order context model tuned for UTF8-encoded text, + contextSigned: second-order context model tuned for signed integers. If |p1| and |p2| are the previous two bytes, and |mode| is current context mode, we calculate the context as: context = ContextLut(mode)[p1] | ContextLut(mode)[p2 + 256]. -For CONTEXT_UTF8 mode, if the previous two bytes are ASCII characters +For contextUTF8 mode, if the previous two bytes are ASCII characters (i.e. < 128), this will be equivalent to context = 4 * context1(p1) + context2(p2), @@ -257,10 +257,10 @@ context ids and the type of the next byte is summarized in the table below: |-------------|-------------------|---------------------|------------------| */ const ( - CONTEXT_LSB6 = 0 - CONTEXT_MSB6 = 1 - CONTEXT_UTF8 = 2 - CONTEXT_SIGNED = 3 + contextLSB6 = 0 + contextMSB6 = 1 + contextUTF8 = 2 + contextSigned = 3 ) /* Common context lookup table for all context modes. */ @@ -2340,12 +2340,12 @@ var kContextLookup = [2048]byte{ 7, } -type ContextLut []byte +type contextLUT []byte -func BROTLI_CONTEXT_LUT(mode int) ContextLut { +func getContextLUT(mode int) contextLUT { return kContextLookup[mode<<9:] } -func BROTLI_CONTEXT(p1 byte, p2 byte, lut ContextLut) byte { +func getContext(p1 byte, p2 byte, lut contextLUT) byte { return lut[p1] | lut[256+int(p2)] } diff --git a/decode.go b/decode.go index ebad1ea..65198ca 100644 --- a/decode.go +++ b/decode.go @@ -21,10 +21,10 @@ package brotli * ::BrotliDecoderDecompressStream functions. */ const ( - BROTLI_DECODER_RESULT_ERROR = 0 - BROTLI_DECODER_RESULT_SUCCESS = 1 - BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT = 2 - BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT = 3 + decoderResultError = 0 + decoderResultSuccess = 1 + decoderResultNeedsMoreInput = 2 + decoderResultNeedsMoreOutput = 3 ) /** @@ -52,55 +52,55 @@ const ( * See ::BrotliDecoderGetErrorCode and ::BROTLI_LAST_ERROR_CODE. */ const ( - BROTLI_DECODER_NO_ERROR = 0 - BROTLI_DECODER_SUCCESS = 1 - BROTLI_DECODER_NEEDS_MORE_INPUT = 2 - BROTLI_DECODER_NEEDS_MORE_OUTPUT = 3 - BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE = -1 - BROTLI_DECODER_ERROR_FORMAT_RESERVED = -2 - BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE = -3 - BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET = -4 - BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME = -5 - BROTLI_DECODER_ERROR_FORMAT_CL_SPACE = -6 - BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE = -7 - BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT = -8 - BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1 = -9 - BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2 = -10 - BROTLI_DECODER_ERROR_FORMAT_TRANSFORM = -11 - BROTLI_DECODER_ERROR_FORMAT_DICTIONARY = -12 - BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS = -13 - BROTLI_DECODER_ERROR_FORMAT_PADDING_1 = -14 - BROTLI_DECODER_ERROR_FORMAT_PADDING_2 = -15 - BROTLI_DECODER_ERROR_FORMAT_DISTANCE = -16 - BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET = -19 - BROTLI_DECODER_ERROR_INVALID_ARGUMENTS = -20 - BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES = -21 - BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS = -22 - BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP = -25 - BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1 = -26 - BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2 = -27 - BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES = -30 - BROTLI_DECODER_ERROR_UNREACHABLE = -31 + 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 ) /** * The value of the last error code, negative integer. * - * All other error code values are in the range from ::BROTLI_LAST_ERROR_CODE + * All other error code values are in the range from ::lastErrorCode * to @c -1. There are also 4 other possible non-error codes @c 0 .. @c 3 in * ::BrotliDecoderErrorCode enumeration. */ -const BROTLI_LAST_ERROR_CODE = BROTLI_DECODER_ERROR_UNREACHABLE +const lastErrorCode = decoderErrorUnreachable /** Options to be used with ::BrotliDecoderSetParameter. */ const ( - BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION = 0 - BROTLI_DECODER_PARAM_LARGE_WINDOW = 1 + decoderParamDisableRingBufferReallocation = 0 + decoderParamLargeWindow = 1 ) -const HUFFMAN_TABLE_BITS = 8 +const huffmanTableBits = 8 -const HUFFMAN_TABLE_MASK = 0xFF +const huffmanTableMask = 0xFF /* We need the slack region for the following reasons: - doing up to two 16-byte copies for fast backward copying @@ -114,12 +114,12 @@ var kCodeLengthPrefixLength = [16]byte{2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, var kCodeLengthPrefixValue = [16]byte{0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5} -func BrotliDecoderSetParameter(state *Reader, p int, value uint32) bool { +func decoderSetParameter(state *Reader, p int, value uint32) bool { if state.state != BROTLI_STATE_UNINITED { return false } switch p { - case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: + case decoderParamDisableRingBufferReallocation: if !(value == 0) { state.canny_ringbuffer_allocation = 0 } else { @@ -127,7 +127,7 @@ func BrotliDecoderSetParameter(state *Reader, p int, value uint32) bool { } return true - case BROTLI_DECODER_PARAM_LARGE_WINDOW: + case decoderParamLargeWindow: state.large_window = (!(value == 0)) return true @@ -137,39 +137,39 @@ func BrotliDecoderSetParameter(state *Reader, p int, value uint32) bool { } /* Saves error code and converts it to BrotliDecoderResult. */ -func SaveErrorCode(s *Reader, e int) int { +func saveErrorCode(s *Reader, e int) int { s.error_code = int(e) switch e { - case BROTLI_DECODER_SUCCESS: - return BROTLI_DECODER_RESULT_SUCCESS + case decoderSuccess: + return decoderResultSuccess - case BROTLI_DECODER_NEEDS_MORE_INPUT: - return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + case decoderNeedsMoreInput: + return decoderResultNeedsMoreInput - case BROTLI_DECODER_NEEDS_MORE_OUTPUT: - return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT + case decoderNeedsMoreOutput: + return decoderResultNeedsMoreOutput default: - return BROTLI_DECODER_RESULT_ERROR + return decoderResultError } } /* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli". Precondition: bit-reader accumulator has at least 8 bits. */ -func DecodeWindowBits(s *Reader, br *bitReader) int { +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 BROTLI_DECODER_SUCCESS + return decoderSuccess } takeBits(br, 3, &n) if n != 0 { s.window_bits = 17 + n - return BROTLI_DECODER_SUCCESS + return decoderSuccess } takeBits(br, 3, &n) @@ -177,37 +177,37 @@ func DecodeWindowBits(s *Reader, br *bitReader) int { if large_window { takeBits(br, 1, &n) if n == 1 { - return BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS + return decoderErrorFormatWindowBits } s.large_window = true - return BROTLI_DECODER_SUCCESS + return decoderSuccess } else { - return BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS + return decoderErrorFormatWindowBits } } if n != 0 { s.window_bits = 8 + n - return BROTLI_DECODER_SUCCESS + return decoderSuccess } s.window_bits = 17 - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ -func DecodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int { +func decodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int { var bits uint32 switch s.substate_decode_uint8 { case BROTLI_STATE_DECODE_UINT8_NONE: if !safeReadBits(br, 1, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits == 0 { *value = 0 - return BROTLI_DECODER_SUCCESS + return decoderSuccess } fallthrough @@ -215,13 +215,13 @@ func DecodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int { case BROTLI_STATE_DECODE_UINT8_SHORT: if !safeReadBits(br, 3, &bits) { s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits == 0 { *value = 1 s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Use output value as a temporary storage. It MUST be persisted. */ @@ -232,27 +232,27 @@ func DecodeVarLenUint8(s *Reader, br *bitReader, value *uint32) int { case BROTLI_STATE_DECODE_UINT8_LONG: if !safeReadBits(br, *value, &bits) { s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } *value = (1 << *value) + bits s.substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess default: - return BROTLI_DECODER_ERROR_UNREACHABLE + return decoderErrorUnreachable } } /* Decodes a metablock length and flags by reading 2 - 31 bits. */ -func DecodeMetaBlockLength(s *Reader, br *bitReader) int { +func decodeMetaBlockLength(s *Reader, br *bitReader) int { var bits uint32 var i int for { switch s.substate_metablock_header { case BROTLI_STATE_METABLOCK_HEADER_NONE: if !safeReadBits(br, 1, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits != 0 { @@ -274,12 +274,12 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_EMPTY: if !safeReadBits(br, 1, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits != 0 { s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES @@ -288,7 +288,7 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: if !safeReadBits(br, 2, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } s.size_nibbles = uint(byte(bits + 4)) @@ -309,11 +309,11 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { for ; i < int(s.size_nibbles); i++ { if !safeReadBits(br, 4, &bits) { s.loop_counter = i - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if uint(i+1) == s.size_nibbles && s.size_nibbles > 4 && bits == 0 { - return BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE + return decoderErrorFormatExuberantNibble } s.meta_block_remaining_len |= int(bits << uint(i*4)) @@ -326,7 +326,7 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: if s.is_last_metablock == 0 { if !safeReadBits(br, 1, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits != 0 { @@ -338,15 +338,15 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { s.meta_block_remaining_len++ s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess case BROTLI_STATE_METABLOCK_HEADER_RESERVED: if !safeReadBits(br, 1, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits != 0 { - return BROTLI_DECODER_ERROR_FORMAT_RESERVED + return decoderErrorFormatReserved } s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES @@ -355,12 +355,12 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER_BYTES: if !safeReadBits(br, 2, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits == 0 { s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } s.size_nibbles = uint(byte(bits)) @@ -374,11 +374,11 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { for ; i < int(s.size_nibbles); i++ { if !safeReadBits(br, 8, &bits) { s.loop_counter = i - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if uint(i+1) == s.size_nibbles && s.size_nibbles > 1 && bits == 0 { - return BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE + return decoderErrorFormatExuberantMetaNibble } s.meta_block_remaining_len |= int(bits << uint(i*8)) @@ -386,10 +386,10 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { s.meta_block_remaining_len++ s.substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess default: - return BROTLI_DECODER_ERROR_UNREACHABLE + return decoderErrorUnreachable } } } @@ -398,12 +398,12 @@ func DecodeMetaBlockLength(s *Reader, br *bitReader) int { 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. */ -func DecodeSymbol(bits uint32, table []HuffmanCode, br *bitReader) uint32 { - table = table[bits&HUFFMAN_TABLE_MASK:] - if table[0].bits > HUFFMAN_TABLE_BITS { - var nbits uint32 = uint32(table[0].bits) - HUFFMAN_TABLE_BITS - dropBits(br, HUFFMAN_TABLE_BITS) - table = table[uint32(table[0].value)+((bits>>HUFFMAN_TABLE_BITS)&bitMask(nbits)):] +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)) @@ -412,13 +412,13 @@ func DecodeSymbol(bits uint32, table []HuffmanCode, br *bitReader) uint32 { /* Reads and decodes the next Huffman code from bit-stream. This method peeks 16 bits of input and drops 0 - 15 of them. */ -func ReadSymbol(table []HuffmanCode, br *bitReader) uint32 { - return DecodeSymbol(get16BitsUnmasked(br), table, br) +func readSymbol(table []huffmanCode, br *bitReader) uint32 { + return decodeSymbol(get16BitsUnmasked(br), table, br) } /* Same as DecodeSymbol, but it is known that there is less than 15 bits of input are currently available. */ -func SafeDecodeSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool { +func safeDecodeSymbol(table []huffmanCode, br *bitReader, result *uint32) bool { var val uint32 var available_bits uint32 = getAvailableBits(br) if available_bits == 0 { @@ -431,8 +431,8 @@ func SafeDecodeSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool { } val = uint32(getBitsUnmasked(br)) - table = table[val&HUFFMAN_TABLE_MASK:] - if table[0].bits <= HUFFMAN_TABLE_BITS { + table = table[val&huffmanTableMask:] + if table[0].bits <= huffmanTableBits { if uint32(table[0].bits) <= available_bits { dropBits(br, uint32(table[0].bits)) *result = uint32(table[0].value) @@ -442,67 +442,67 @@ func SafeDecodeSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool { } } - if available_bits <= HUFFMAN_TABLE_BITS { + if available_bits <= huffmanTableBits { return false /* Not enough bits to move to the second level. */ } /* Speculatively drop HUFFMAN_TABLE_BITS. */ - val = (val & bitMask(uint32(table[0].bits))) >> HUFFMAN_TABLE_BITS + val = (val & bitMask(uint32(table[0].bits))) >> huffmanTableBits - available_bits -= HUFFMAN_TABLE_BITS + available_bits -= huffmanTableBits table = table[uint32(table[0].value)+val:] if available_bits < uint32(table[0].bits) { return false /* Not enough bits for the second level. */ } - dropBits(br, HUFFMAN_TABLE_BITS+uint32(table[0].bits)) + dropBits(br, huffmanTableBits+uint32(table[0].bits)) *result = uint32(table[0].value) return true } -func SafeReadSymbol(table []HuffmanCode, br *bitReader, result *uint32) bool { +func safeReadSymbol(table []huffmanCode, br *bitReader, result *uint32) bool { var val uint32 if safeGetBits(br, 15, &val) { - *result = DecodeSymbol(val, table, br) + *result = decodeSymbol(val, table, br) return true } - return SafeDecodeSymbol(table, br, result) + return safeDecodeSymbol(table, br, result) } /* Makes a look-up in first level Huffman table. Peeks 8 bits. */ -func PreloadSymbol(safe int, table []HuffmanCode, br *bitReader, bits *uint32, value *uint32) { +func preloadSymbol(safe int, table []huffmanCode, br *bitReader, bits *uint32, value *uint32) { if safe != 0 { return } - table = table[BrotliGetBits(br, HUFFMAN_TABLE_BITS):] + table = table[BrotliGetBits(br, huffmanTableBits):] *bits = uint32(table[0].bits) *value = uint32(table[0].value) } /* Decodes the next Huffman code using data prepared by PreloadSymbol. Reads 0 - 15 bits. Also peeks 8 following bits. */ -func ReadPreloadedSymbol(table []HuffmanCode, br *bitReader, bits *uint32, value *uint32) uint32 { +func readPreloadedSymbol(table []huffmanCode, br *bitReader, bits *uint32, value *uint32) uint32 { var result uint32 = *value - var ext []HuffmanCode - if *bits > HUFFMAN_TABLE_BITS { + var ext []huffmanCode + if *bits > huffmanTableBits { var val uint32 = get16BitsUnmasked(br) - ext = table[val&HUFFMAN_TABLE_MASK:][*value:] - var mask uint32 = bitMask((*bits - HUFFMAN_TABLE_BITS)) - dropBits(br, HUFFMAN_TABLE_BITS) - ext = ext[(val>>HUFFMAN_TABLE_BITS)&mask:] + ext = table[val&huffmanTableMask:][*value:] + var mask uint32 = bitMask((*bits - huffmanTableBits)) + dropBits(br, huffmanTableBits) + ext = ext[(val>>huffmanTableBits)&mask:] dropBits(br, uint32(ext[0].bits)) result = uint32(ext[0].value) } else { dropBits(br, *bits) } - PreloadSymbol(0, table, br, bits, value) + preloadSymbol(0, table, br, bits, value) return result } -func Log2Floor(x uint32) uint32 { +func log2Floor(x uint32) uint32 { var result uint32 = 0 for x != 0 { x >>= 1 @@ -515,9 +515,9 @@ func Log2Floor(x uint32) uint32 { /* Reads (s->symbol + 1) symbols. Totally 1..4 symbols are read, 1..11 bits each. The list of symbols MUST NOT contain duplicates. */ -func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader) int { +func readSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader) int { var br *bitReader = &s.br - var max_bits uint32 = Log2Floor(alphabet_size - 1) + var max_bits uint32 = log2Floor(alphabet_size - 1) var i uint32 = s.sub_loop_counter /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */ @@ -527,11 +527,11 @@ func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader if !safeReadBits(br, max_bits, &v) { s.sub_loop_counter = i s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if v >= max_symbol { - return BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET + return decoderErrorFormatSimpleHuffmanAlphabet } s.symbols_lists_array[i] = uint16(v) @@ -542,12 +542,12 @@ func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader var k uint32 = i + 1 for ; k <= num_symbols; k++ { if s.symbols_lists_array[i] == s.symbols_lists_array[k] { - return BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME + return decoderErrorFormatSimpleHuffmanSame } } } - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Process single decoded symbol code length: @@ -556,7 +556,7 @@ func ReadSimpleHuffmanSymbols(alphabet_size uint32, max_symbol uint32, s *Reader C) extend corresponding index-chain D) reduce the Huffman space E) update the histogram */ -func ProcessSingleCodeLength(code_len uint32, symbol *uint32, repeat *uint32, space *uint32, prev_code_len *uint32, symbol_lists SymbolList, code_length_histo []uint16, next_symbol []int) { +func processSingleCodeLength(code_len uint32, symbol *uint32, repeat *uint32, space *uint32, prev_code_len *uint32, symbol_lists SymbolList, code_length_histo []uint16, next_symbol []int) { *repeat = 0 if code_len != 0 { /* code_len == 1..15 */ SymbolListPut(symbol_lists, next_symbol[code_len], uint16(*symbol)) @@ -579,7 +579,7 @@ func ProcessSingleCodeLength(code_len uint32, symbol *uint32, repeat *uint32, sp PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH */ -func ProcessRepeatedCodeLength(code_len uint32, repeat_delta uint32, alphabet_size uint32, symbol *uint32, repeat *uint32, space *uint32, prev_code_len *uint32, repeat_code_len *uint32, symbol_lists SymbolList, code_length_histo []uint16, next_symbol []int) { +func processRepeatedCodeLength(code_len uint32, repeat_delta uint32, alphabet_size uint32, symbol *uint32, repeat *uint32, space *uint32, prev_code_len *uint32, repeat_code_len *uint32, symbol_lists SymbolList, code_length_histo []uint16, next_symbol []int) { var old_repeat uint32 /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ var extra_bits uint32 = 3 var new_len uint32 = 0 @@ -628,7 +628,7 @@ func ProcessRepeatedCodeLength(code_len uint32, repeat_delta uint32, alphabet_si } /* Reads and decodes symbol codelengths. */ -func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { +func readSymbolCodeLengths(alphabet_size uint32, s *Reader) int { var br *bitReader = &s.br var symbol uint32 = s.symbol var repeat uint32 = s.repeat @@ -639,9 +639,9 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { var code_length_histo []uint16 = s.code_length_histo[:] var next_symbol []int = s.next_symbol[:] if !warmupBitReader(br) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } - var p []HuffmanCode + var p []huffmanCode for symbol < alphabet_size && space > 0 { p = s.table[:] var code_len uint32 @@ -651,15 +651,15 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { s.prev_code_len = prev_code_len s.repeat_code_len = repeat_code_len s.space = space - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } fillBitWindow16(br) - p = p[getBitsUnmasked(br)&uint64(bitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH)):] + p = p[getBitsUnmasked(br)&uint64(bitMask(huffmanMaxCodeLengthCodeLength)):] dropBits(br, uint32(p[0].bits)) /* Use 1..5 bits. */ code_len = uint32(p[0].value) /* code_len == 0..17 */ if code_len < repeatPreviousCodeLength { - ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol) /* code_len == 16..17, extra_bits == 2..3 */ + processSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol) /* code_len == 16..17, extra_bits == 2..3 */ } else { var extra_bits uint32 if code_len == repeatPreviousCodeLength { @@ -669,25 +669,25 @@ func ReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { } var repeat_delta uint32 = uint32(getBitsUnmasked(br)) & bitMask(extra_bits) dropBits(br, extra_bits) - ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, symbol_lists, code_length_histo, next_symbol) + processRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, symbol_lists, code_length_histo, next_symbol) } } s.space = space - return BROTLI_DECODER_SUCCESS + return decoderSuccess } -func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { +func safeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { var br *bitReader = &s.br var get_byte bool = false - var p []HuffmanCode + var p []huffmanCode for s.symbol < alphabet_size && s.space > 0 { p = s.table[:] var code_len uint32 var available_bits uint32 var bits uint32 = 0 if get_byte && !pullByte(br) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } get_byte = false available_bits = getAvailableBits(br) @@ -695,7 +695,7 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { bits = uint32(getBitsUnmasked(br)) } - p = p[bits&bitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH):] + p = p[bits&bitMask(huffmanMaxCodeLengthCodeLength):] if uint32(p[0].bits) > available_bits { get_byte = true continue @@ -704,7 +704,7 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { code_len = uint32(p[0].value) /* code_len == 0..17 */ if code_len < repeatPreviousCodeLength { dropBits(br, uint32(p[0].bits)) - ProcessSingleCodeLength(code_len, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) /* code_len == 16..17, extra_bits == 2..3 */ + processSingleCodeLength(code_len, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) /* code_len == 16..17, extra_bits == 2..3 */ } else { var extra_bits uint32 = code_len - 14 var repeat_delta uint32 = (bits >> p[0].bits) & bitMask(extra_bits) @@ -714,16 +714,16 @@ func SafeReadSymbolCodeLengths(alphabet_size uint32, s *Reader) int { } dropBits(br, uint32(p[0].bits)+extra_bits) - ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, &s.repeat_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) + processRepeatedCodeLength(code_len, repeat_delta, alphabet_size, &s.symbol, &s.repeat, &s.space, &s.prev_code_len, &s.repeat_code_len, s.symbol_lists, s.code_length_histo[:], s.next_symbol[:]) } } - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Reads and decodes 15..18 codes using static prefix code. Each code is 2..4 bits long. In total 30..72 bits are used. */ -func ReadCodeLengthCodeLengths(s *Reader) int { +func readCodeLengthCodeLengths(s *Reader) int { var br *bitReader = &s.br var num_codes uint32 = s.repeat var space uint32 = s.space @@ -745,7 +745,7 @@ func ReadCodeLengthCodeLengths(s *Reader) int { s.repeat = num_codes s.space = space s.substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } } @@ -764,10 +764,10 @@ func ReadCodeLengthCodeLengths(s *Reader) int { } if num_codes != 1 && space != 0 { - return BROTLI_DECODER_ERROR_FORMAT_CL_SPACE + return decoderErrorFormatClSpace } - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Decodes the Huffman tables. @@ -781,7 +781,7 @@ func ReadCodeLengthCodeLengths(s *Reader) int { encoded with predefined entropy code. 32 - 74 bits are used. B.2) Decoded table is used to decode code lengths of symbols in resulting Huffman table. In worst case 3520 bits are read. */ -func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCode, opt_table_size *uint32, s *Reader) int { +func readHuffmanCode(alphabet_size uint32, max_symbol uint32, table []huffmanCode, opt_table_size *uint32, s *Reader) int { var br *bitReader = &s.br /* Unnecessary masking, but might be good for safety. */ @@ -792,7 +792,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod switch s.substate_huffman { case BROTLI_STATE_HUFFMAN_NONE: if !safeReadBits(br, 2, &s.sub_loop_counter) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } /* The value is used as follows: @@ -802,7 +802,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod s.space = 32 s.repeat = 0 /* num_codes */ var i int - for i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH; i++ { + for i = 0; i <= huffmanMaxCodeLengthCodeLength; i++ { s.code_length_histo[i] = 0 } @@ -820,7 +820,7 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: if !safeReadBits(br, 2, &s.symbol) { /* num_symbols */ s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } s.sub_loop_counter = 0 @@ -828,8 +828,8 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod /* Fall through. */ case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { - var result int = ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s) - if result != BROTLI_DECODER_SUCCESS { + var result int = readSimpleHuffmanSymbols(alphabet_size, max_symbol, s) + if result != decoderSuccess { return result } } @@ -843,19 +843,19 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod var bits uint32 if !safeReadBits(br, 1, &bits) { s.substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } s.symbol += bits } - table_size = BrotliBuildSimpleHuffmanTable(table, HUFFMAN_TABLE_BITS, s.symbols_lists_array[:], s.symbol) + table_size = buildSimpleHuffmanTable(table, huffmanTableBits, s.symbols_lists_array[:], s.symbol) if opt_table_size != nil { *opt_table_size = table_size } s.substate_huffman = BROTLI_STATE_HUFFMAN_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } fallthrough @@ -863,18 +863,18 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod case BROTLI_STATE_HUFFMAN_COMPLEX: { var i uint32 - var result int = ReadCodeLengthCodeLengths(s) - if result != BROTLI_DECODER_SUCCESS { + var result int = readCodeLengthCodeLengths(s) + if result != decoderSuccess { return result } - BrotliBuildCodeLengthsHuffmanTable(s.table[:], s.code_length_code_lengths[:], s.code_length_histo[:]) + buildCodeLengthsHuffmanTable(s.table[:], s.code_length_code_lengths[:], s.code_length_histo[:]) for i = 0; i < 16; i++ { s.code_length_histo[i] = 0 } - for i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; i++ { - s.next_symbol[i] = int(i) - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1) + for i = 0; i <= huffmanMaxCodeLength; i++ { + s.next_symbol[i] = int(i) - (huffmanMaxCodeLength + 1) SymbolListPut(s.symbol_lists, s.next_symbol[i], 0xFFFF) } @@ -891,50 +891,50 @@ func ReadHuffmanCode(alphabet_size uint32, max_symbol uint32, table []HuffmanCod case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { var table_size uint32 - var result int = ReadSymbolCodeLengths(max_symbol, s) - if result == BROTLI_DECODER_NEEDS_MORE_INPUT { - result = SafeReadSymbolCodeLengths(max_symbol, s) + var result int = readSymbolCodeLengths(max_symbol, s) + if result == decoderNeedsMoreInput { + result = safeReadSymbolCodeLengths(max_symbol, s) } - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { return result } if s.space != 0 { - return BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE + return decoderErrorFormatHuffmanSpace } - table_size = BrotliBuildHuffmanTable(table, HUFFMAN_TABLE_BITS, s.symbol_lists, s.code_length_histo[:]) + 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 = BROTLI_STATE_HUFFMAN_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } fallthrough default: - return BROTLI_DECODER_ERROR_UNREACHABLE + return decoderErrorUnreachable } } } /* Decodes a block length by reading 3..39 bits. */ -func ReadBlockLength(table []HuffmanCode, br *bitReader) uint32 { +func readBlockLength(table []huffmanCode, br *bitReader) uint32 { var code uint32 var nbits uint32 - code = ReadSymbol(table, br) + code = readSymbol(table, br) nbits = uint32(kBlockLengthPrefixCode1[code].nbits) /* nbits == 2..24 */ return uint32(kBlockLengthPrefixCode1[code].offset) + readBits(br, nbits) } /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then reading can't be continued with ReadBlockLength. */ -func SafeReadBlockLength(s *Reader, result *uint32, table []HuffmanCode, br *bitReader) bool { +func safeReadBlockLength(s *Reader, result *uint32, table []huffmanCode, br *bitReader) bool { var index uint32 if s.substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE { - if !SafeReadSymbol(table, br, &index) { + if !safeReadSymbol(table, br, &index) { return false } } else { @@ -969,7 +969,7 @@ func SafeReadBlockLength(s *Reader, result *uint32, table []HuffmanCode, br *bit Most of input values are 0 and 1. To reduce number of branches, we replace inner for loop with do-while. */ -func InverseMoveToFrontTransform(v []byte, v_len uint32, state *Reader) { +func inverseMoveToFrontTransform(v []byte, v_len uint32, state *Reader) { var mtf [256]byte var i int for i = 1; i < 256; i++ { @@ -993,7 +993,7 @@ func InverseMoveToFrontTransform(v []byte, v_len uint32, state *Reader) { } /* Decodes a series of Huffman table using ReadHuffmanCode function. */ -func HuffmanTreeGroupDecode(group *HuffmanTreeGroup, s *Reader) int { +func huffmanTreeGroupDecode(group *huffmanTreeGroup, s *Reader) int { if s.substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP { s.next = group.codes s.htree_index = 0 @@ -1002,8 +1002,8 @@ func HuffmanTreeGroupDecode(group *HuffmanTreeGroup, s *Reader) int { for s.htree_index < int(group.num_htrees) { var table_size uint32 - var result int = ReadHuffmanCode(uint32(group.alphabet_size), uint32(group.max_symbol), s.next, &table_size, s) - if result != BROTLI_DECODER_SUCCESS { + var result int = readHuffmanCode(uint32(group.alphabet_size), uint32(group.max_symbol), s.next, &table_size, s) + if result != decoderSuccess { return result } group.htrees[s.htree_index] = s.next @@ -1012,7 +1012,7 @@ func HuffmanTreeGroupDecode(group *HuffmanTreeGroup, s *Reader) int { } s.substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } /* Decodes a context map. @@ -1023,14 +1023,14 @@ func HuffmanTreeGroupDecode(group *HuffmanTreeGroup, s *Reader) int { This table will be used for reading context map items. 3) Read context map items; "0" values could be run-length encoded. 4) Optionally, apply InverseMoveToFront transform to the resulting map. */ -func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_arg *[]byte, s *Reader) int { +func decodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_arg *[]byte, s *Reader) int { var br *bitReader = &s.br - var result int = BROTLI_DECODER_SUCCESS + var result int = decoderSuccess switch int(s.substate_context_map) { case BROTLI_STATE_CONTEXT_MAP_NONE: - result = DecodeVarLenUint8(s, br, num_htrees) - if result != BROTLI_DECODER_SUCCESS { + result = decodeVarLenUint8(s, br, num_htrees) + if result != decoderSuccess { return result } @@ -1038,14 +1038,14 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a s.context_index = 0 *context_map_arg = make([]byte, uint(context_map_size)) if *context_map_arg == nil { - return BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP + return decoderErrorAllocContextMap } if *num_htrees <= 1 { for i := 0; i < int(context_map_size); i++ { (*context_map_arg)[i] = 0 } - return BROTLI_DECODER_SUCCESS + return decoderSuccess } s.substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX @@ -1058,7 +1058,7 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe to peek 4 bits ahead. */ if !safeGetBits(br, 5, &bits) { - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits&1 != 0 { /* Use RLE for zeros. */ @@ -1077,8 +1077,8 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: { var alphabet_size uint32 = *num_htrees + s.max_run_length_prefix - result = ReadHuffmanCode(alphabet_size, alphabet_size, s.context_map_table[:], nil, s) - if result != BROTLI_DECODER_SUCCESS { + result = readHuffmanCode(alphabet_size, alphabet_size, s.context_map_table[:], nil, s) + if result != decoderSuccess { return result } s.code = 0xFFFF @@ -1096,10 +1096,10 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a var skip_preamble bool = (code != 0xFFFF) for context_index < context_map_size || skip_preamble { if !skip_preamble { - if !SafeReadSymbol(s.context_map_table[:], br, &code) { + if !safeReadSymbol(s.context_map_table[:], br, &code) { s.code = 0xFFFF s.context_index = context_index - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if code == 0 { @@ -1123,12 +1123,12 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a if !safeReadBits(br, code, &reps) { s.code = code s.context_index = context_index - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } reps += 1 << code if context_index+reps > context_map_size { - return BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT + return decoderErrorFormatContextMapRepeat } for { @@ -1150,31 +1150,31 @@ func DecodeContextMap(context_map_size uint32, num_htrees *uint32, context_map_a var bits uint32 if !safeReadBits(br, 1, &bits) { s.substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM - return BROTLI_DECODER_NEEDS_MORE_INPUT + return decoderNeedsMoreInput } if bits != 0 { - InverseMoveToFrontTransform(*context_map_arg, context_map_size, s) + inverseMoveToFrontTransform(*context_map_arg, context_map_size, s) } s.substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE - return BROTLI_DECODER_SUCCESS + return decoderSuccess } fallthrough default: - return BROTLI_DECODER_ERROR_UNREACHABLE + return decoderErrorUnreachable } } /* Decodes a command or literal and updates block type ring-buffer. Reads 3..54 bits. */ -func DecodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool { +func decodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool { var max_block_type uint32 = s.num_block_types[tree_type] - var type_tree []HuffmanCode - type_tree = s.block_type_trees[tree_type*BROTLI_HUFFMAN_MAX_SIZE_258:] - var len_tree []HuffmanCode - len_tree = s.block_len_trees[tree_type*BROTLI_HUFFMAN_MAX_SIZE_26:] + var type_tree []huffmanCode + type_tree = s.block_type_trees[tree_type*huffmanMaxSize258:] + var len_tree []huffmanCode + len_tree = s.block_len_trees[tree_type*huffmanMaxSize26:] var br *bitReader = &s.br var ringbuffer []uint32 = s.block_type_rb[tree_type*2:] var block_type uint32 @@ -1184,15 +1184,15 @@ func DecodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool { /* Read 0..15 + 3..39 bits. */ if safe == 0 { - block_type = ReadSymbol(type_tree, br) - s.block_length[tree_type] = ReadBlockLength(len_tree, br) + block_type = readSymbol(type_tree, br) + s.block_length[tree_type] = readBlockLength(len_tree, br) } else { var memento bitReaderState bitReaderSaveState(br, &memento) - if !SafeReadSymbol(type_tree, br, &block_type) { + if !safeReadSymbol(type_tree, br, &block_type) { return false } - if !SafeReadBlockLength(s, &s.block_length[tree_type], len_tree, br) { + if !safeReadBlockLength(s, &s.block_length[tree_type], len_tree, br) { s.substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE bitReaderRestoreState(br, &memento) return false @@ -1216,17 +1216,17 @@ func DecodeBlockTypeAndLength(safe int, s *Reader, tree_type int) bool { return true } -func DetectTrivialLiteralBlockTypes(s *Reader) { +func detectTrivialLiteralBlockTypes(s *Reader) { var i uint for i = 0; i < 8; i++ { s.trivial_literal_contexts[i] = 0 } for i = 0; uint32(i) < s.num_block_types[0]; i++ { - var offset uint = i << BROTLI_LITERAL_CONTEXT_BITS + var offset uint = i << literalContextBits var error uint = 0 var sample uint = uint(s.context_map[offset]) var j uint - for j = 0; j < 1<>5]) s.trivial_literal_context = int((trivial >> (block_type & 31)) & 1) - s.literal_htree = []HuffmanCode(s.literal_hgroup.htrees[s.context_map_slice[0]]) + s.literal_htree = []huffmanCode(s.literal_hgroup.htrees[s.context_map_slice[0]]) context_mode = s.context_modes[block_type] & 3 - s.context_lookup = BROTLI_CONTEXT_LUT(int(context_mode)) + s.context_lookup = getContextLUT(int(context_mode)) } /* Decodes the block type and updates the state for literal context. Reads 3..54 bits. */ -func DecodeLiteralBlockSwitchInternal(safe int, s *Reader) bool { - if !DecodeBlockTypeAndLength(safe, s, 0) { +func decodeLiteralBlockSwitchInternal(safe int, s *Reader) bool { + if !decodeBlockTypeAndLength(safe, s, 0) { return false } - PrepareLiteralDecoding(s) + prepareLiteralDecoding(s) return true } -func DecodeLiteralBlockSwitch(s *Reader) { - DecodeLiteralBlockSwitchInternal(0, s) +func decodeLiteralBlockSwitch(s *Reader) { + decodeLiteralBlockSwitchInternal(0, s) } -func SafeDecodeLiteralBlockSwitch(s *Reader) bool { - return DecodeLiteralBlockSwitchInternal(1, s) +func safeDecodeLiteralBlockSwitch(s *Reader) bool { + return decodeLiteralBlockSwitchInternal(1, s) } /* Block switch for insert/copy length. Reads 3..54 bits. */ -func DecodeCommandBlockSwitchInternal(safe int, s *Reader) bool { - if !DecodeBlockTypeAndLength(safe, s, 1) { +func decodeCommandBlockSwitchInternal(safe int, s *Reader) bool { + if !decodeBlockTypeAndLength(safe, s, 1) { return false } - s.htree_command = []HuffmanCode(s.insert_copy_hgroup.htrees[s.block_type_rb[3]]) + s.htree_command = []huffmanCode(s.insert_copy_hgroup.htrees[s.block_type_rb[3]]) return true } -func DecodeCommandBlockSwitch(s *Reader) { - DecodeCommandBlockSwitchInternal(0, s) +func decodeCommandBlockSwitch(s *Reader) { + decodeCommandBlockSwitchInternal(0, s) } -func SafeDecodeCommandBlockSwitch(s *Reader) bool { - return DecodeCommandBlockSwitchInternal(1, s) +func safeDecodeCommandBlockSwitch(s *Reader) bool { + return decodeCommandBlockSwitchInternal(1, s) } /* Block switch for distance codes. Reads 3..54 bits. */ -func DecodeDistanceBlockSwitchInternal(safe int, s *Reader) bool { - if !DecodeBlockTypeAndLength(safe, s, 2) { +func decodeDistanceBlockSwitchInternal(safe int, s *Reader) bool { + if !decodeBlockTypeAndLength(safe, s, 2) { return false } - s.dist_context_map_slice = s.dist_context_map[s.block_type_rb[5]< s.ringbuffer_size { pos = uint(s.ringbuffer_size) @@ -1325,17 +1325,17 @@ func UnwrittenBytes(s *Reader, wrap bool) uint { /* Dumps output. Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push and either ring-buffer is as big as window size, or |force| is true. */ -func WriteRingBuffer(s *Reader, available_out *uint, next_out *[]byte, total_out *uint, force bool) int { +func writeRingBuffer(s *Reader, available_out *uint, next_out *[]byte, total_out *uint, force bool) int { var start []byte start = s.ringbuffer[s.partial_pos_out&uint(s.ringbuffer_mask):] - var to_write uint = UnwrittenBytes(s, true) + var to_write uint = unwrittenBytes(s, true) var num_written uint = *available_out if num_written > to_write { num_written = to_write } if s.meta_block_remaining_len < 0 { - return BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1 + return decoderErrorFormatBlockLength1 } if next_out != nil && *next_out == nil { @@ -1355,9 +1355,9 @@ func WriteRingBuffer(s *Reader, available_out *uint, next_out *[]byte, total_out if num_written < to_write { if s.ringbuffer_size == 1<distance_code < 0. */ -func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool { +func readDistanceInternal(safe int, s *Reader, br *bitReader) bool { var distval int var memento bitReaderState - var distance_tree []HuffmanCode = []HuffmanCode(s.distance_hgroup.htrees[s.dist_htree_index]) + var distance_tree []huffmanCode = []huffmanCode(s.distance_hgroup.htrees[s.dist_htree_index]) if safe == 0 { - s.distance_code = int(ReadSymbol(distance_tree, br)) + s.distance_code = int(readSymbol(distance_tree, br)) } else { var code uint32 bitReaderSaveState(br, &memento) - if !SafeReadSymbol(distance_tree, br, &code) { + if !safeReadSymbol(distance_tree, br, &code) { return false } @@ -1616,7 +1616,7 @@ func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool { s.distance_context = 0 if s.distance_code&^0xF == 0 { - TakeDistanceFromRingBuffer(s) + takeDistanceFromRingBuffer(s) s.block_length[2]-- return true } @@ -1637,7 +1637,7 @@ func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool { distval >>= s.distance_postfix_bits nbits = (uint32(distval) >> 1) + 1 if safe != 0 { - if !SafeReadBits(br, nbits, &bits) { + if !safeReadBitsMaybeZero(br, nbits, &bits) { s.distance_code = -1 /* Restore precondition. */ bitReaderRestoreState(br, &memento) return false @@ -1656,25 +1656,25 @@ func ReadDistanceInternal(safe int, s *Reader, br *bitReader) bool { return true } -func ReadDistance(s *Reader, br *bitReader) { - ReadDistanceInternal(0, s, br) +func readDistance(s *Reader, br *bitReader) { + readDistanceInternal(0, s, br) } -func SafeReadDistance(s *Reader, br *bitReader) bool { - return ReadDistanceInternal(1, s, br) +func safeReadDistance(s *Reader, br *bitReader) bool { + return readDistanceInternal(1, s, br) } -func ReadCommandInternal(safe int, s *Reader, br *bitReader, insert_length *int) bool { +func readCommandInternal(safe int, s *Reader, br *bitReader, insert_length *int) bool { var cmd_code uint32 var insert_len_extra uint32 = 0 var copy_length uint32 var v CmdLutElement var memento bitReaderState if safe == 0 { - cmd_code = ReadSymbol(s.htree_command, br) + cmd_code = readSymbol(s.htree_command, br) } else { bitReaderSaveState(br, &memento) - if !SafeReadSymbol(s.htree_command, br, &cmd_code) { + if !safeReadSymbol(s.htree_command, br, &cmd_code) { return false } } @@ -1691,7 +1691,7 @@ func ReadCommandInternal(safe int, s *Reader, br *bitReader, insert_length *int) copy_length = readBits(br, uint32(v.copy_len_extra_bits)) } else { - if !SafeReadBits(br, uint32(v.insert_len_extra_bits), &insert_len_extra) || !SafeReadBits(br, uint32(v.copy_len_extra_bits), ©_length) { + if !safeReadBitsMaybeZero(br, uint32(v.insert_len_extra_bits), &insert_len_extra) || !safeReadBitsMaybeZero(br, uint32(v.copy_len_extra_bits), ©_length) { bitReaderRestoreState(br, &memento) return false } @@ -1703,15 +1703,15 @@ func ReadCommandInternal(safe int, s *Reader, br *bitReader, insert_length *int) return true } -func ReadCommand(s *Reader, br *bitReader, insert_length *int) { - ReadCommandInternal(0, s, br, insert_length) +func readCommand(s *Reader, br *bitReader, insert_length *int) { + readCommandInternal(0, s, br, insert_length) } -func SafeReadCommand(s *Reader, br *bitReader, insert_length *int) bool { - return ReadCommandInternal(1, s, br, insert_length) +func safeReadCommand(s *Reader, br *bitReader, insert_length *int) bool { + return readCommandInternal(1, s, br, insert_length) } -func CheckInputAmount(safe int, br *bitReader, num uint) bool { +func checkInputAmountMaybeSafe(safe int, br *bitReader, num uint) bool { if safe != 0 { return true } @@ -1719,15 +1719,15 @@ func CheckInputAmount(safe int, br *bitReader, num uint) bool { return checkInputAmount(br, num) } -func ProcessCommandsInternal(safe int, s *Reader) int { +func processCommandsInternal(safe int, s *Reader) int { var pos int = s.pos var i int = s.loop_counter - var result int = BROTLI_DECODER_SUCCESS + var result int = decoderSuccess var br *bitReader = &s.br - var hc []HuffmanCode + var hc []huffmanCode - if !CheckInputAmount(safe, br, 28) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !checkInputAmountMaybeSafe(safe, br, 28) { + result = decoderNeedsMoreInput goto saveStateAndReturn } @@ -1745,7 +1745,7 @@ func ProcessCommandsInternal(safe int, s *Reader) int { } else if s.state == BROTLI_STATE_COMMAND_POST_WRAP_COPY { goto CommandPostWrapCopy } else { - return BROTLI_DECODER_ERROR_UNREACHABLE + return decoderErrorUnreachable } CommandBegin: @@ -1753,20 +1753,20 @@ CommandBegin: s.state = BROTLI_STATE_COMMAND_BEGIN } - if !CheckInputAmount(safe, br, 28) { /* 156 bits + 7 bytes */ + if !checkInputAmountMaybeSafe(safe, br, 28) { /* 156 bits + 7 bytes */ s.state = BROTLI_STATE_COMMAND_BEGIN - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput goto saveStateAndReturn } if s.block_length[1] == 0 { if safe != 0 { - if !SafeDecodeCommandBlockSwitch(s) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeDecodeCommandBlockSwitch(s) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - DecodeCommandBlockSwitch(s) + decodeCommandBlockSwitch(s) } goto CommandBegin @@ -1774,12 +1774,12 @@ CommandBegin: /* Read the insert/copy length in the command. */ if safe != 0 { - if !SafeReadCommand(s, br, &i) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeReadCommand(s, br, &i) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - ReadCommand(s, br, &i) + readCommand(s, br, &i) } if i == 0 { @@ -1797,36 +1797,36 @@ CommandInner: if s.trivial_literal_context != 0 { var bits uint32 var value uint32 - PreloadSymbol(safe, s.literal_htree, br, &bits, &value) + preloadSymbol(safe, s.literal_htree, br, &bits, &value) for { - if !CheckInputAmount(safe, br, 28) { /* 162 bits + 7 bytes */ + if !checkInputAmountMaybeSafe(safe, br, 28) { /* 162 bits + 7 bytes */ s.state = BROTLI_STATE_COMMAND_INNER - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput goto saveStateAndReturn } if s.block_length[0] == 0 { if safe != 0 { - if !SafeDecodeLiteralBlockSwitch(s) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeDecodeLiteralBlockSwitch(s) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - DecodeLiteralBlockSwitch(s) + decodeLiteralBlockSwitch(s) } - PreloadSymbol(safe, s.literal_htree, br, &bits, &value) + preloadSymbol(safe, s.literal_htree, br, &bits, &value) if s.trivial_literal_context == 0 { goto CommandInner } } if safe == 0 { - s.ringbuffer[pos] = byte(ReadPreloadedSymbol(s.literal_htree, br, &bits, &value)) + s.ringbuffer[pos] = byte(readPreloadedSymbol(s.literal_htree, br, &bits, &value)) } else { var literal uint32 - if !SafeReadSymbol(s.literal_htree, br, &literal) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeReadSymbol(s.literal_htree, br, &literal) { + result = decoderNeedsMoreInput goto saveStateAndReturn } @@ -1850,20 +1850,20 @@ CommandInner: var p2 byte = s.ringbuffer[(pos-2)&s.ringbuffer_mask] for { var context byte - if !CheckInputAmount(safe, br, 28) { /* 162 bits + 7 bytes */ + if !checkInputAmountMaybeSafe(safe, br, 28) { /* 162 bits + 7 bytes */ s.state = BROTLI_STATE_COMMAND_INNER - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput goto saveStateAndReturn } if s.block_length[0] == 0 { if safe != 0 { - if !SafeDecodeLiteralBlockSwitch(s) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeDecodeLiteralBlockSwitch(s) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - DecodeLiteralBlockSwitch(s) + decodeLiteralBlockSwitch(s) } if s.trivial_literal_context != 0 { @@ -1871,15 +1871,15 @@ CommandInner: } } - context = BROTLI_CONTEXT(p1, p2, s.context_lookup) - hc = []HuffmanCode(s.literal_hgroup.htrees[s.context_map_slice[context]]) + context = getContext(p1, p2, s.context_lookup) + hc = []huffmanCode(s.literal_hgroup.htrees[s.context_map_slice[context]]) p2 = p1 if safe == 0 { - p1 = byte(ReadSymbol(hc, br)) + p1 = byte(readSymbol(hc, br)) } else { var literal uint32 - if !SafeReadSymbol(hc, br, &literal) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeReadSymbol(hc, br, &literal) { + result = decoderNeedsMoreInput goto saveStateAndReturn } @@ -1925,22 +1925,22 @@ CommandPostDecodeLiterals: /* Read distance code in the command, unless it was implicitly zero. */ if s.block_length[2] == 0 { if safe != 0 { - if !SafeDecodeDistanceBlockSwitch(s) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeDecodeDistanceBlockSwitch(s) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - DecodeDistanceBlockSwitch(s) + decodeDistanceBlockSwitch(s) } } if safe != 0 { - if !SafeReadDistance(s, br) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + if !safeReadDistance(s, br) { + result = decoderNeedsMoreInput goto saveStateAndReturn } } else { - ReadDistance(s, br) + readDistance(s, br) } } @@ -1960,13 +1960,13 @@ CommandPostDecodeLiterals: /* The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC. With this choice, no signed overflow can occur after decoding a special distance code (e.g., after adding 3 to the last distance). */ - if s.distance_code > BROTLI_MAX_ALLOWED_DISTANCE { - return BROTLI_DECODER_ERROR_FORMAT_DISTANCE + if s.distance_code > maxAllowedDistance { + return decoderErrorFormatDistance } - if i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH && i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH { + if i >= minDictionaryWordLength && i <= maxDictionaryWordLength { var address int = s.distance_code - s.max_distance - 1 - var words *BrotliDictionary = s.dictionary + var words *dictionary = s.dictionary var transforms *BrotliTransforms = s.transforms var offset int = int(s.dictionary.offsets_by_length[i]) var shift uint32 = uint32(s.dictionary.size_bits_by_length[i]) @@ -1979,7 +1979,7 @@ CommandPostDecodeLiterals: offset += word_idx * i if words.data == nil { - return BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET + return decoderErrorDictionaryNotSet } if transform_idx < int(transforms.num_transforms) { @@ -1999,10 +1999,10 @@ CommandPostDecodeLiterals: goto saveStateAndReturn } } else { - return BROTLI_DECODER_ERROR_FORMAT_TRANSFORM + return decoderErrorFormatTransform } } else { - return BROTLI_DECODER_ERROR_FORMAT_DICTIONARY + return decoderErrorFormatDictionary } } else { var src_start int = (pos - s.distance_code) & s.ringbuffer_mask @@ -2087,28 +2087,28 @@ saveStateAndReturn: return result } -func ProcessCommands(s *Reader) int { - return ProcessCommandsInternal(0, s) +func processCommands(s *Reader) int { + return processCommandsInternal(0, s) } -func SafeProcessCommands(s *Reader) int { - return ProcessCommandsInternal(1, s) +func safeProcessCommands(s *Reader) int { + return processCommandsInternal(1, s) } /* Returns the maximum number of distance symbols which can only represent distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */ -var BrotliMaxDistanceSymbol_bound = [maxNpostfix + 1]uint32{0, 4, 12, 28} -var BrotliMaxDistanceSymbol_diff = [maxNpostfix + 1]uint32{73, 126, 228, 424} +var maxDistanceSymbol_bound = [maxNpostfix + 1]uint32{0, 4, 12, 28} +var maxDistanceSymbol_diff = [maxNpostfix + 1]uint32{73, 126, 228, 424} -func BrotliMaxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 { +func maxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 { var postfix uint32 = 1 << npostfix - if ndirect < BrotliMaxDistanceSymbol_bound[npostfix] { - return ndirect + BrotliMaxDistanceSymbol_diff[npostfix] + postfix - } else if ndirect > BrotliMaxDistanceSymbol_bound[npostfix]+postfix { - return ndirect + BrotliMaxDistanceSymbol_diff[npostfix] + if ndirect < maxDistanceSymbol_bound[npostfix] { + return ndirect + maxDistanceSymbol_diff[npostfix] + postfix + } else if ndirect > maxDistanceSymbol_bound[npostfix]+postfix { + return ndirect + maxDistanceSymbol_diff[npostfix] } else { - return BrotliMaxDistanceSymbol_bound[npostfix] + BrotliMaxDistanceSymbol_diff[npostfix] + postfix + return maxDistanceSymbol_bound[npostfix] + maxDistanceSymbol_diff[npostfix] + postfix } } @@ -2123,17 +2123,17 @@ func BrotliMaxDistanceSymbol(ndirect uint32, npostfix uint32) uint32 { buffer ahead of time - when result is "success" decoder MUST return all unused data back to input buffer; this is possible because the invariant is held on enter */ -func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, available_out *uint, next_out *[]byte) int { - var result int = BROTLI_DECODER_SUCCESS +func decoderDecompressStream(s *Reader, available_in *uint, next_in *[]byte, available_out *uint, next_out *[]byte) int { + var result int = decoderSuccess var br *bitReader = &s.br /* Do not try to process further in a case of unrecoverable error. */ if int(s.error_code) < 0 { - return BROTLI_DECODER_RESULT_ERROR + return decoderResultError } if *available_out != 0 && (next_out == nil || *next_out == nil) { - return SaveErrorCode(s, BROTLI_DECODER_ERROR_INVALID_ARGUMENTS) + return saveErrorCode(s, decoderErrorInvalidArguments) } if *available_out == 0 { @@ -2147,7 +2147,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* At least one byte of input is required. More than one byte of input may be required to complete the transaction -> reading more data must be done in a loop -> do it in a main loop. */ - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput br.input = s.buffer.u8[:] br.byte_pos = 0 @@ -2155,11 +2155,11 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* State machine */ for { - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { /* Error, needs more input/output. */ - if result == BROTLI_DECODER_NEEDS_MORE_INPUT { + if result == decoderNeedsMoreInput { if s.ringbuffer != nil { /* Pro-actively push output. */ - var intermediate_result int = WriteRingBuffer(s, available_out, next_out, nil, true) + var intermediate_result int = writeRingBuffer(s, available_out, next_out, nil, true) /* WriteRingBuffer checks s->meta_block_remaining_len validity. */ if int(intermediate_result) < 0 { @@ -2176,7 +2176,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt s.buffer_length = 0 /* Switch to input stream and restart. */ - result = BROTLI_DECODER_SUCCESS + result = decoderSuccess br.input_len = *available_in br.input = *next_in @@ -2185,7 +2185,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt } else if *available_in != 0 { /* Not enough data in buffer, but can take one more byte from input stream. */ - result = BROTLI_DECODER_SUCCESS + result = decoderSuccess s.buffer.u8[s.buffer_length] = (*next_in)[0] s.buffer_length++ @@ -2240,13 +2240,13 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Prepare to the first read. */ case BROTLI_STATE_UNINITED: if !warmupBitReader(br) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput break } /* Decode window size. */ - result = DecodeWindowBits(s, br) /* Reads 1..8 bits. */ - if result != BROTLI_DECODER_SUCCESS { + result = decodeWindowBits(s, br) /* Reads 1..8 bits. */ + if result != decoderSuccess { break } @@ -2259,12 +2259,12 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt case BROTLI_STATE_LARGE_WINDOW_BITS: if !safeReadBits(br, 6, &s.window_bits) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput break } if s.window_bits < largeMinWbits || s.window_bits > largeMaxWbits { - result = BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS + result = decoderErrorFormatWindowBits break } @@ -2274,17 +2274,17 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Maximum distance, see section 9.1. of the spec. */ /* Fall through. */ case BROTLI_STATE_INITIALIZE: - s.max_backward_distance = (1 << s.window_bits) - BROTLI_WINDOW_GAP + s.max_backward_distance = (1 << s.window_bits) - windowGap /* Allocate memory for both block_type_trees and block_len_trees. */ - s.block_type_trees = make([]HuffmanCode, (3 * (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26))) + s.block_type_trees = make([]huffmanCode, (3 * (huffmanMaxSize258 + huffmanMaxSize26))) if s.block_type_trees == nil { - result = BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES + result = decoderErrorAllocBlockTypeTrees break } - s.block_len_trees = s.block_type_trees[3*BROTLI_HUFFMAN_MAX_SIZE_258:] + s.block_len_trees = s.block_type_trees[3*huffmanMaxSize258:] s.state = BROTLI_STATE_METABLOCK_BEGIN fallthrough @@ -2298,15 +2298,15 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_METABLOCK_HEADER: - result = DecodeMetaBlockLength(s, br) + result = decodeMetaBlockLength(s, br) /* Reads 2 - 31 bits. */ - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { break } if s.is_metadata != 0 || s.is_uncompressed != 0 { if !bitReaderJumpToByteBoundary(br) { - result = BROTLI_DECODER_ERROR_FORMAT_PADDING_1 + result = decoderErrorFormatPadding1 break } } @@ -2321,7 +2321,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt break } - BrotliCalculateRingBufferSize(s) + calculateRingBufferSize(s) if s.is_uncompressed != 0 { s.state = BROTLI_STATE_UNCOMPRESSED break @@ -2331,8 +2331,8 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt s.state = BROTLI_STATE_HUFFMAN_CODE_0 case BROTLI_STATE_UNCOMPRESSED: { - result = CopyUncompressedBlockToOutput(available_out, next_out, nil, s) - if result != BROTLI_DECODER_SUCCESS { + result = copyUncompressedBlockToOutput(available_out, next_out, nil, s) + if result != decoderSuccess { break } @@ -2347,12 +2347,12 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Read one byte and ignore it. */ if !safeReadBits(br, 8, &bits) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput break } } - if result == BROTLI_DECODER_SUCCESS { + if result == decoderSuccess { s.state = BROTLI_STATE_METABLOCK_DONE } @@ -2363,9 +2363,9 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt } /* Reads 1..11 bits. */ - result = DecodeVarLenUint8(s, br, &s.num_block_types[s.loop_counter]) + result = decodeVarLenUint8(s, br, &s.num_block_types[s.loop_counter]) - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { break } @@ -2381,9 +2381,9 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt case BROTLI_STATE_HUFFMAN_CODE_1: { var alphabet_size uint32 = s.num_block_types[s.loop_counter] + 2 - var tree_offset int = s.loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258 - result = ReadHuffmanCode(alphabet_size, alphabet_size, s.block_type_trees[tree_offset:], nil, s) - if result != BROTLI_DECODER_SUCCESS { + var tree_offset int = s.loop_counter * huffmanMaxSize258 + result = readHuffmanCode(alphabet_size, alphabet_size, s.block_type_trees[tree_offset:], nil, s) + if result != decoderSuccess { break } s.state = BROTLI_STATE_HUFFMAN_CODE_2 @@ -2394,9 +2394,9 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt case BROTLI_STATE_HUFFMAN_CODE_2: { var alphabet_size uint32 = numBlockLenSymbols - var tree_offset int = s.loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26 - result = ReadHuffmanCode(alphabet_size, alphabet_size, s.block_len_trees[tree_offset:], nil, s) - if result != BROTLI_DECODER_SUCCESS { + var tree_offset int = s.loop_counter * huffmanMaxSize26 + result = readHuffmanCode(alphabet_size, alphabet_size, s.block_len_trees[tree_offset:], nil, s) + if result != decoderSuccess { break } s.state = BROTLI_STATE_HUFFMAN_CODE_3 @@ -2406,9 +2406,9 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_HUFFMAN_CODE_3: { - var tree_offset int = s.loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26 - if !SafeReadBlockLength(s, &s.block_length[s.loop_counter], s.block_len_trees[tree_offset:], br) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + 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 } @@ -2421,7 +2421,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt { var bits uint32 if !safeReadBits(br, 6, &bits) { - result = BROTLI_DECODER_NEEDS_MORE_INPUT + result = decoderNeedsMoreInput break } @@ -2431,7 +2431,7 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt s.distance_postfix_mask = int(bitMask(s.distance_postfix_bits)) s.context_modes = make([]byte, uint(s.num_block_types[0])) if s.context_modes == nil { - result = BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES + result = decoderErrorAllocContextModes break } @@ -2442,9 +2442,9 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_CONTEXT_MODES: - result = ReadContextModes(s) + result = readContextModes(s) - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { break } @@ -2453,13 +2453,13 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ case BROTLI_STATE_CONTEXT_MAP_1: - result = DecodeContextMap(s.num_block_types[0]<= 3 { - PrepareLiteralDecoding(s) + prepareLiteralDecoding(s) s.dist_context_map_slice = s.dist_context_map - s.htree_command = []HuffmanCode(s.insert_copy_hgroup.htrees[0]) - if !BrotliEnsureRingBuffer(s) { - result = BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2 + s.htree_command = []huffmanCode(s.insert_copy_hgroup.htrees[0]) + if !ensureRingBuffer(s) { + result = decoderErrorAllocRingBuffer2 break } @@ -2547,10 +2547,10 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ BROTLI_STATE_COMMAND_POST_WRAP_COPY: - result = ProcessCommands(s) + result = processCommands(s) - if result == BROTLI_DECODER_NEEDS_MORE_INPUT { - result = SafeProcessCommands(s) + if result == decoderNeedsMoreInput { + result = safeProcessCommands(s) } case BROTLI_STATE_COMMAND_INNER_WRITE, @@ -2559,13 +2559,13 @@ func BrotliDecoderDecompressStream(s *Reader, available_in *uint, next_in *[]byt /* Fall through. */ BROTLI_STATE_COMMAND_POST_WRITE_2: - result = WriteRingBuffer(s, available_out, next_out, nil, false) + result = writeRingBuffer(s, available_out, next_out, nil, false) - if result != BROTLI_DECODER_SUCCESS { + if result != decoderSuccess { break } - WrapRingBuffer(s) + wrapRingBuffer(s) if s.ringbuffer_size == 1<= uint64(block_size) { return 0 } @@ -144,7 +144,7 @@ func RemainingInputBlockSize(s *Writer) uint { /* Wraps 64-bit input position to 32-bit ring-buffer position preserving "not-a-first-lap" feature. */ -func WrapPosition(position uint64) uint32 { +func wrapPosition(position uint64) uint32 { var result uint32 = uint32(position) var gb uint64 = position >> 30 if gb > 2 { @@ -155,7 +155,7 @@ func WrapPosition(position uint64) uint32 { return result } -func GetBrotliStorage(s *Writer, size uint) []byte { +func getBrotliStorage(s *Writer, size uint) []byte { if s.storage_size_ < size { s.storage_ = nil s.storage_ = make([]byte, size) @@ -165,7 +165,7 @@ func GetBrotliStorage(s *Writer, size uint) []byte { return s.storage_ } -func HashTableSize(max_table_size uint, input_size uint) uint { +func hashTableSize(max_table_size uint, input_size uint) uint { var htsize uint = 256 for htsize < max_table_size && htsize < input_size { htsize <<= 1 @@ -174,9 +174,9 @@ func HashTableSize(max_table_size uint, input_size uint) uint { return htsize } -func GetHashTable(s *Writer, quality int, input_size uint, table_size *uint) []int { +func getHashTable(s *Writer, quality int, input_size uint, table_size *uint) []int { var max_table_size uint = MaxHashTableSize(quality) - var htsize uint = HashTableSize(max_table_size, input_size) + var htsize uint = hashTableSize(max_table_size, input_size) /* Use smaller hash table when input.size() is smaller, since we fill the table, incurring O(hash table size) overhead for compression, and if the input is short, we won't need that @@ -210,7 +210,7 @@ func GetHashTable(s *Writer, quality int, input_size uint, table_size *uint) []i return table } -func EncodeWindowBits(lgwin int, large_window bool, last_bytes *uint16, last_bytes_bits *byte) { +func encodeWindowBits(lgwin int, large_window bool, last_bytes *uint16, last_bytes_bits *byte) { if large_window { *last_bytes = uint16((lgwin&0x3F)<<8 | 0x11) *last_bytes_bits = 14 @@ -233,7 +233,7 @@ func EncodeWindowBits(lgwin int, large_window bool, last_bytes *uint16, last_byt /* Initializes the command and distance prefix codes for the first block. */ -var InitCommandPrefixCodes_kDefaultCommandDepths = [128]byte{ +var initCommandPrefixCodes_kDefaultCommandDepths = [128]byte{ 0, 4, 4, @@ -359,7 +359,7 @@ var InitCommandPrefixCodes_kDefaultCommandDepths = [128]byte{ 12, 12, } -var InitCommandPrefixCodes_kDefaultCommandBits = [128]uint16{ +var initCommandPrefixCodes_kDefaultCommandBits = [128]uint16{ 0, 0, 8, @@ -485,7 +485,7 @@ var InitCommandPrefixCodes_kDefaultCommandBits = [128]uint16{ 2047, 4095, } -var InitCommandPrefixCodes_kDefaultCommandCode = []byte{ +var initCommandPrefixCodes_kDefaultCommandCode = []byte{ 0xff, 0x77, 0xd5, @@ -544,17 +544,17 @@ var InitCommandPrefixCodes_kDefaultCommandCode = []byte{ 0x04, 0x00, } -var InitCommandPrefixCodes_kDefaultCommandCodeNumBits uint = 448 +var initCommandPrefixCodes_kDefaultCommandCodeNumBits uint = 448 -func InitCommandPrefixCodes(cmd_depths []byte, cmd_bits []uint16, cmd_code []byte, cmd_code_numbits *uint) { - copy(cmd_depths, InitCommandPrefixCodes_kDefaultCommandDepths[:]) - copy(cmd_bits, InitCommandPrefixCodes_kDefaultCommandBits[:]) +func initCommandPrefixCodes(cmd_depths []byte, cmd_bits []uint16, cmd_code []byte, cmd_code_numbits *uint) { + copy(cmd_depths, initCommandPrefixCodes_kDefaultCommandDepths[:]) + copy(cmd_bits, initCommandPrefixCodes_kDefaultCommandBits[:]) /* Initialize the pre-compressed form of the command and distance prefix codes. */ - copy(cmd_code, InitCommandPrefixCodes_kDefaultCommandCode) + copy(cmd_code, initCommandPrefixCodes_kDefaultCommandCode) - *cmd_code_numbits = InitCommandPrefixCodes_kDefaultCommandCodeNumBits + *cmd_code_numbits = initCommandPrefixCodes_kDefaultCommandCodeNumBits } /* Decide about the context map based on the ability of the prediction @@ -565,7 +565,7 @@ func InitCommandPrefixCodes(cmd_depths []byte, cmd_bits []uint16, cmd_code []byt BitsEntropy will assume that symbol to be stored alone using Huffman coding. */ -var ChooseContextMap_kStaticContextMapContinuation = [64]uint32{ +var kStaticContextMapContinuation = [64]uint32{ 1, 1, 2, @@ -631,7 +631,7 @@ var ChooseContextMap_kStaticContextMapContinuation = [64]uint32{ 0, 0, } -var ChooseContextMap_kStaticContextMapSimpleUTF8 = [64]uint32{ +var kStaticContextMapSimpleUTF8 = [64]uint32{ 0, 0, 1, @@ -698,7 +698,7 @@ var ChooseContextMap_kStaticContextMapSimpleUTF8 = [64]uint32{ 0, } -func ChooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts *uint, literal_context_map *[]uint32) { +func chooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts *uint, literal_context_map *[]uint32) { var monogram_histo = [3]uint32{0} var two_prefix_histo = [6]uint32{0} var total uint @@ -735,10 +735,10 @@ func ChooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts * *num_literal_contexts = 1 } else if entropy[2]-entropy[3] < 0.02 { *num_literal_contexts = 2 - *literal_context_map = ChooseContextMap_kStaticContextMapSimpleUTF8[:] + *literal_context_map = kStaticContextMapSimpleUTF8[:] } else { *num_literal_contexts = 3 - *literal_context_map = ChooseContextMap_kStaticContextMapContinuation[:] + *literal_context_map = kStaticContextMapContinuation[:] } } @@ -746,7 +746,7 @@ func ChooseContextMap(quality int, bigram_histo []uint32, num_literal_contexts * context values, based on the entropy reduction of histograms over the first 5 bits of literals. */ -var ShouldUseComplexStaticContextMap_kStaticContextMapComplexUTF8 = [64]uint32{ +var kStaticContextMapComplexUTF8 = [64]uint32{ 11, 11, 12, @@ -813,7 +813,7 @@ var ShouldUseComplexStaticContextMap_kStaticContextMapComplexUTF8 = [64]uint32{ 6, } -func ShouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) bool { +func shouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) bool { /* Try the more complex static context map only for long data. */ if size_hint < 1<<20 { return false @@ -825,7 +825,7 @@ func ShouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, var entropy [3]float64 var dummy uint var i uint - var utf8_lut ContextLut = BROTLI_CONTEXT_LUT(CONTEXT_UTF8) + var utf8_lut contextLUT = getContextLUT(contextUTF8) /* To make entropy calculations faster and to fit on the stack, we collect histograms over the 5 most significant bits of literals. One histogram without context and 13 additional histograms for each context value. */ @@ -839,7 +839,7 @@ func ShouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, strides at every 4kB intervals. */ for pos = start_pos + 2; pos < stride_end_pos; pos++ { var literal byte = input[pos&mask] - var context byte = byte(ShouldUseComplexStaticContextMap_kStaticContextMapComplexUTF8[BROTLI_CONTEXT(prev1, prev2, utf8_lut)]) + var context byte = byte(kStaticContextMapComplexUTF8[getContext(prev1, prev2, utf8_lut)]) total++ combined_histo[literal>>3]++ context_histo[context][literal>>3]++ @@ -869,16 +869,16 @@ func ShouldUseComplexStaticContextMap(input []byte, start_pos uint, length uint, return false } else { *num_literal_contexts = 13 - *literal_context_map = ShouldUseComplexStaticContextMap_kStaticContextMapComplexUTF8[:] + *literal_context_map = kStaticContextMapComplexUTF8[:] return true } } } -func DecideOverLiteralContextModeling(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) { +func decideOverLiteralContextModeling(input []byte, start_pos uint, length uint, mask uint, quality int, size_hint uint, num_literal_contexts *uint, literal_context_map *[]uint32) { if quality < MIN_QUALITY_FOR_CONTEXT_MODELING || length < 64 { return - } else if ShouldUseComplexStaticContextMap(input, start_pos, length, mask, quality, size_hint, num_literal_contexts, literal_context_map) { + } else if shouldUseComplexStaticContextMap(input, start_pos, length, mask, quality, size_hint, num_literal_contexts, literal_context_map) { } else /* Context map was already set, nothing else to do. */ { var end_pos uint = start_pos + length @@ -899,11 +899,11 @@ func DecideOverLiteralContextModeling(input []byte, start_pos uint, length uint, } } - ChooseContextMap(quality, bigram_prefix_histo[0:], num_literal_contexts, literal_context_map) + chooseContextMap(quality, bigram_prefix_histo[0:], num_literal_contexts, literal_context_map) } } -func ShouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes uint, num_literals uint, num_commands uint) bool { +func shouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes uint, num_literals uint, num_commands uint) bool { /* TODO: find more precise minimal block overhead. */ if bytes <= 2 { return false @@ -932,22 +932,22 @@ func ShouldCompress_encode(data []byte, mask uint, last_flush_pos uint64, bytes } /* Chooses the literal context mode for a metablock */ -func ChooseContextMode(params *BrotliEncoderParams, data []byte, pos uint, mask uint, length uint) int { +func chooseContextMode(params *encoderParams, data []byte, pos uint, mask uint, length uint) int { /* We only do the computation for the option of something else than CONTEXT_UTF8 for the highest qualities */ if params.quality >= MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING && !BrotliIsMostlyUTF8(data, pos, mask, length, kMinUTF8Ratio) { - return CONTEXT_SIGNED + return contextSigned } - return CONTEXT_UTF8 + return contextUTF8 } -func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, num_commands uint, commands []command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) { - var wrapped_last_flush_pos uint32 = WrapPosition(last_flush_pos) +func writeMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes uint, is_last bool, literal_context_mode int, params *encoderParams, prev_byte byte, prev_byte2 byte, num_literals uint, num_commands uint, commands []command, saved_dist_cache []int, dist_cache []int, storage_ix *uint, storage []byte) { + var wrapped_last_flush_pos uint32 = wrapPosition(last_flush_pos) var last_bytes uint16 var last_bytes_bits byte - var literal_context_lut ContextLut = BROTLI_CONTEXT_LUT(literal_context_mode) - var block_params BrotliEncoderParams = *params + var literal_context_lut contextLUT = getContextLUT(literal_context_mode) + var block_params encoderParams = *params if bytes == 0 { /* Write the ISLAST and ISEMPTY bits. */ @@ -957,7 +957,7 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes return } - if !ShouldCompress_encode(data, mask, last_flush_pos, bytes, num_literals, num_commands) { + if !shouldCompress_encode(data, mask, last_flush_pos, bytes, num_literals, num_commands) { /* Restore the distance cache, as its last update by CreateBackwardReferences is now unused. */ copy(dist_cache, saved_dist_cache[:4]) @@ -974,18 +974,18 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes } else if params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT { storeMetaBlockTrivial(data, uint(wrapped_last_flush_pos), bytes, mask, is_last, params, commands, num_commands, storage_ix, storage) } else { - var mb MetaBlockSplit - InitMetaBlockSplit(&mb) + var mb metaBlockSplit + initMetaBlockSplit(&mb) if params.quality < MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING { var num_literal_contexts uint = 1 var literal_context_map []uint32 = nil if !params.disable_literal_context_modeling { - DecideOverLiteralContextModeling(data, uint(wrapped_last_flush_pos), bytes, mask, params.quality, params.size_hint, &num_literal_contexts, &literal_context_map) + decideOverLiteralContextModeling(data, uint(wrapped_last_flush_pos), bytes, mask, params.quality, params.size_hint, &num_literal_contexts, &literal_context_map) } - BrotliBuildMetaBlockGreedy(data, uint(wrapped_last_flush_pos), mask, prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, literal_context_map, commands, num_commands, &mb) + buildMetaBlockGreedy(data, uint(wrapped_last_flush_pos), mask, prev_byte, prev_byte2, literal_context_lut, num_literal_contexts, literal_context_map, commands, num_commands, &mb) } else { - BrotliBuildMetaBlock(data, uint(wrapped_last_flush_pos), mask, &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, &mb) + buildMetaBlock(data, uint(wrapped_last_flush_pos), mask, &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, &mb) } if params.quality >= MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS { @@ -993,15 +993,15 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes histograms. It might be less than distance alphabet size for "Large Window Brotli" (32-bit). */ var num_effective_dist_codes uint32 = block_params.dist.alphabet_size - if num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS { - num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS + if num_effective_dist_codes > numHistogramDistanceSymbols { + num_effective_dist_codes = numHistogramDistanceSymbols } - BrotliOptimizeHistograms(num_effective_dist_codes, &mb) + optimizeHistograms(num_effective_dist_codes, &mb) } storeMetaBlock(data, uint(wrapped_last_flush_pos), bytes, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, storage_ix, storage) - DestroyMetaBlockSplit(&mb) + destroyMetaBlockSplit(&mb) } if bytes+4 < *storage_ix>>3 { @@ -1015,13 +1015,13 @@ func WriteMetaBlockInternal(data []byte, mask uint, last_flush_pos uint64, bytes } } -func ChooseDistanceParams(params *BrotliEncoderParams) { +func chooseDistanceParams(params *encoderParams) { var distance_postfix_bits uint32 = 0 var num_direct_distance_codes uint32 = 0 if params.quality >= MIN_QUALITY_FOR_NONZERO_DISTANCE_PARAMS { var ndirect_msb uint32 - if params.mode == BROTLI_MODE_FONT { + if params.mode == modeFont { distance_postfix_bits = 1 num_direct_distance_codes = 12 } else { @@ -1036,10 +1036,10 @@ func ChooseDistanceParams(params *BrotliEncoderParams) { } } - BrotliInitDistanceParams(params, distance_postfix_bits, num_direct_distance_codes) + initDistanceParams(params, distance_postfix_bits, num_direct_distance_codes) } -func EnsureInitialized(s *Writer) bool { +func ensureInitialized(s *Writer) bool { if s.is_initialized_ { return true } @@ -1050,7 +1050,7 @@ func EnsureInitialized(s *Writer) bool { SanitizeParams(&s.params) s.params.lgblock = ComputeLgBlock(&s.params) - ChooseDistanceParams(&s.params) + chooseDistanceParams(&s.params) RingBufferSetup(&s.params, &s.ringbuffer_) @@ -1061,34 +1061,34 @@ func EnsureInitialized(s *Writer) bool { lgwin = brotli_max_int(lgwin, 18) } - EncodeWindowBits(lgwin, s.params.large_window, &s.last_bytes_, &s.last_bytes_bits_) + encodeWindowBits(lgwin, s.params.large_window, &s.last_bytes_, &s.last_bytes_bits_) } if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY { - InitCommandPrefixCodes(s.cmd_depths_[:], s.cmd_bits_[:], s.cmd_code_[:], &s.cmd_code_numbits_) + initCommandPrefixCodes(s.cmd_depths_[:], s.cmd_bits_[:], s.cmd_code_[:], &s.cmd_code_numbits_) } s.is_initialized_ = true return true } -func BrotliEncoderInitParams(params *BrotliEncoderParams) { - params.mode = BROTLI_DEFAULT_MODE +func encoderInitParams(params *encoderParams) { + params.mode = defaultMode params.large_window = false - params.quality = BROTLI_DEFAULT_QUALITY - params.lgwin = BROTLI_DEFAULT_WINDOW + params.quality = defaultQuality + params.lgwin = defaultWindow params.lgblock = 0 params.size_hint = 0 params.disable_literal_context_modeling = false - BrotliInitEncoderDictionary(¶ms.dictionary) + initEncoderDictionary(¶ms.dictionary) params.dist.distance_postfix_bits = 0 params.dist.num_direct_distance_codes = 0 params.dist.alphabet_size = uint32(distanceAlphabetSize(0, 0, maxDistanceBits)) - params.dist.max_distance = BROTLI_MAX_DISTANCE + params.dist.max_distance = maxDistance } -func BrotliEncoderInitState(s *Writer) { - BrotliEncoderInitParams(&s.params) +func encoderInitState(s *Writer) { + encoderInitParams(&s.params) s.input_pos_ = 0 s.num_commands_ = 0 s.num_literals_ = 0 @@ -1108,7 +1108,7 @@ func BrotliEncoderInitState(s *Writer) { s.next_out_ = nil s.available_out_ = 0 s.total_out_ = 0 - s.stream_state_ = BROTLI_STREAM_PROCESSING + s.stream_state_ = streamProcessing s.is_last_block_emitted_ = false s.is_initialized_ = false @@ -1129,25 +1129,6 @@ func BrotliEncoderInitState(s *Writer) { copy(s.saved_dist_cache_[:], s.dist_cache_[:]) } -func BrotliEncoderCleanupState(s *Writer) { - s.storage_ = nil - s.commands_ = nil - RingBufferFree(&s.ringbuffer_) - DestroyHasher(&s.hasher_) - s.large_table_ = nil - s.command_buf_ = nil - s.literal_buf_ = nil -} - -/* Deinitializes and frees BrotliEncoderState instance. */ -func BrotliEncoderDestroyInstance(state *Writer) { - if state == nil { - return - } else { - BrotliEncoderCleanupState(state) - } -} - /* Copies the given input data to the internal ring buffer of the compressor. No processing of the data occurs at this time and this function can be @@ -1155,7 +1136,7 @@ func BrotliEncoderDestroyInstance(state *Writer) { accumulated input. At most input_block_size() bytes of input data can be copied to the ring buffer, otherwise the next WriteBrotliData() will fail. */ -func CopyInputToRingBuffer(s *Writer, input_size uint, input_buffer []byte) { +func copyInputToRingBuffer(s *Writer, input_size uint, input_buffer []byte) { var ringbuffer_ *RingBuffer = &s.ringbuffer_ RingBufferWrite(input_buffer, input_size, ringbuffer_) s.input_pos_ += uint64(input_size) @@ -1210,18 +1191,18 @@ func CopyInputToRingBuffer(s *Writer, input_size uint, input_buffer []byte) { /* Marks all input as processed. Returns true if position wrapping occurs. */ -func UpdateLastProcessedPos(s *Writer) bool { - var wrapped_last_processed_pos uint32 = WrapPosition(s.last_processed_pos_) - var wrapped_input_pos uint32 = WrapPosition(s.input_pos_) +func updateLastProcessedPos(s *Writer) bool { + var wrapped_last_processed_pos uint32 = wrapPosition(s.last_processed_pos_) + var wrapped_input_pos uint32 = wrapPosition(s.input_pos_) s.last_processed_pos_ = s.input_pos_ return wrapped_input_pos < wrapped_last_processed_pos } -func ExtendLastCommand(s *Writer, bytes *uint32, wrapped_last_processed_pos *uint32) { +func extendLastCommand(s *Writer, bytes *uint32, wrapped_last_processed_pos *uint32) { var last_command *command = &s.commands_[s.num_commands_-1] var data []byte = s.ringbuffer_.buffer_ var mask uint32 = s.ringbuffer_.mask_ - var max_backward_distance uint64 = ((uint64(1)) << s.params.lgwin) - BROTLI_WINDOW_GAP + var max_backward_distance uint64 = ((uint64(1)) << s.params.lgwin) - windowGap var last_copy_len uint64 = uint64(last_command.copy_len_) & 0x1FFFFFF var last_processed_pos uint64 = s.last_processed_pos_ - last_copy_len var max_distance uint64 @@ -1258,10 +1239,10 @@ func ExtendLastCommand(s *Writer, bytes *uint32, wrapped_last_processed_pos *uin Returns false if the size of the input data is larger than input_block_size(). */ -func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, output *[]byte) bool { - var delta uint64 = UnprocessedInputSize(s) +func encodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, output *[]byte) bool { + var delta uint64 = unprocessedInputSize(s) var bytes uint32 = uint32(delta) - var wrapped_last_processed_pos uint32 = WrapPosition(s.last_processed_pos_) + var wrapped_last_processed_pos uint32 = wrapPosition(s.last_processed_pos_) var data []byte var mask uint32 var literal_context_mode int @@ -1277,7 +1258,7 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu s.is_last_block_emitted_ = true } - if delta > uint64(InputBlockSize(s)) { + if delta > uint64(inputBlockSize(s)) { return false } @@ -1300,10 +1281,10 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu return true } - storage = GetBrotliStorage(s, uint(2*bytes+503)) + storage = getBrotliStorage(s, uint(2*bytes+503)) storage[0] = byte(s.last_bytes_) storage[1] = byte(s.last_bytes_ >> 8) - table = GetHashTable(s, s.params.quality, uint(bytes), &table_size) + table = getHashTable(s, s.params.quality, uint(bytes), &table_size) if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY { compressFragmentFast(data[wrapped_last_processed_pos&mask:], uint(bytes), is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) } else { @@ -1312,7 +1293,7 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu s.last_bytes_ = uint16(storage[storage_ix>>3]) s.last_bytes_bits_ = byte(storage_ix & 7) - UpdateLastProcessedPos(s) + updateLastProcessedPos(s) *output = storage[0:] *out_size = storage_ix >> 3 return true @@ -1338,17 +1319,17 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu } } - InitOrStitchToPreviousBlock(&s.hasher_, data, uint(mask), &s.params, uint(wrapped_last_processed_pos), uint(bytes), is_last) + initOrStitchToPreviousBlock(&s.hasher_, data, uint(mask), &s.params, uint(wrapped_last_processed_pos), uint(bytes), is_last) - literal_context_mode = ChooseContextMode(&s.params, data, uint(WrapPosition(s.last_flush_pos_)), uint(mask), uint(s.input_pos_-s.last_flush_pos_)) + literal_context_mode = chooseContextMode(&s.params, data, uint(wrapPosition(s.last_flush_pos_)), uint(mask), uint(s.input_pos_-s.last_flush_pos_)) if s.num_commands_ != 0 && s.last_insert_len_ == 0 { - ExtendLastCommand(s, &bytes, &wrapped_last_processed_pos) + extendLastCommand(s, &bytes, &wrapped_last_processed_pos) } if s.params.quality == ZOPFLIFICATION_QUALITY { assert(s.params.hasher.type_ == 10) - createZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_.(*H10), s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_) + createZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_.(*h10), s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_) } else if s.params.quality == HQ_ZOPFLIFICATION_QUALITY { assert(s.params.hasher.type_ == 10) createHqZopfliBackwardReferences(uint(bytes), uint(wrapped_last_processed_pos), data, uint(mask), &s.params, s.hasher_, s.dist_cache_[:], &s.last_insert_len_, s.commands_[s.num_commands_:], &s.num_commands_, &s.num_literals_) @@ -1360,7 +1341,7 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu var max_literals uint = max_length / 8 var max_commands uint = max_length / 8 var processed_bytes uint = uint(s.input_pos_ - s.last_flush_pos_) - var next_input_fits_metablock bool = (processed_bytes+InputBlockSize(s) <= max_length) + var next_input_fits_metablock bool = (processed_bytes+inputBlockSize(s) <= max_length) var should_flush bool = (s.params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT && s.num_literals_+s.num_commands_ >= MAX_NUM_DELAYED_SYMBOLS) /* If maximal possible additional block doesn't fit metablock, flush now. */ /* TODO: Postpone decision until next block arrives? */ @@ -1369,8 +1350,8 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu amount of commands / literals produced. */ if !is_last && !force_flush && !should_flush && next_input_fits_metablock && s.num_literals_ < max_literals && s.num_commands_ < max_commands { /* Merge with next input block. Everything will happen later. */ - if UpdateLastProcessedPos(s) { - HasherReset(s.hasher_) + if updateLastProcessedPos(s) { + hasherReset(s.hasher_) } *out_size = 0 @@ -1399,16 +1380,16 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu assert(s.input_pos_-s.last_flush_pos_ <= 1<<24) { var metablock_size uint32 = uint32(s.input_pos_ - s.last_flush_pos_) - var storage []byte = GetBrotliStorage(s, uint(2*metablock_size+503)) + var storage []byte = getBrotliStorage(s, uint(2*metablock_size+503)) var storage_ix uint = uint(s.last_bytes_bits_) storage[0] = byte(s.last_bytes_) storage[1] = byte(s.last_bytes_ >> 8) - WriteMetaBlockInternal(data, uint(mask), s.last_flush_pos_, uint(metablock_size), is_last, literal_context_mode, &s.params, s.prev_byte_, s.prev_byte2_, s.num_literals_, s.num_commands_, s.commands_, s.saved_dist_cache_[:], s.dist_cache_[:], &storage_ix, storage) + writeMetaBlockInternal(data, uint(mask), s.last_flush_pos_, uint(metablock_size), is_last, literal_context_mode, &s.params, s.prev_byte_, s.prev_byte2_, s.num_literals_, s.num_commands_, s.commands_, s.saved_dist_cache_[:], s.dist_cache_[:], &storage_ix, storage) s.last_bytes_ = uint16(storage[storage_ix>>3]) s.last_bytes_bits_ = byte(storage_ix & 7) s.last_flush_pos_ = s.input_pos_ - if UpdateLastProcessedPos(s) { - HasherReset(s.hasher_) + if updateLastProcessedPos(s) { + hasherReset(s.hasher_) } if s.last_flush_pos_ > 0 { @@ -1436,7 +1417,7 @@ func EncodeData(s *Writer, is_last bool, force_flush bool, out_size *uint, outpu Returns number of produced bytes. REQUIRED: |header| should be 8-byte aligned and at least 16 bytes long. REQUIRED: |block_size| <= (1 << 24). */ -func WriteMetadataHeader(s *Writer, block_size uint, header []byte) uint { +func writeMetadataHeader(s *Writer, block_size uint, header []byte) uint { var storage_ix uint storage_ix = uint(s.last_bytes_bits_) header[0] = byte(s.last_bytes_) @@ -1454,7 +1435,7 @@ func WriteMetadataHeader(s *Writer, block_size uint, header []byte) uint { if block_size == 1 { nbits = 0 } else { - nbits = Log2FloorNonZero(uint(uint32(block_size)-1)) + 1 + nbits = log2FloorNonZero(uint(uint32(block_size)-1)) + 1 } var nbytes uint32 = (nbits + 7) / 8 BrotliWriteBits(2, uint64(nbytes), &storage_ix, header) @@ -1464,261 +1445,7 @@ func WriteMetadataHeader(s *Writer, block_size uint, header []byte) uint { return (storage_ix + 7) >> 3 } -func BrotliCompressBufferQuality10(lgwin int, input_size uint, input_buffer []byte, encoded_size *uint, encoded_buffer []byte) bool { - var mask uint = BROTLI_SIZE_MAX >> 1 - var dist_cache = [4]int{4, 11, 15, 16} - var saved_dist_cache = [4]int{4, 11, 15, 16} - var ok bool = true - var max_out_size uint = *encoded_size - var total_out_size uint = 0 - var last_bytes uint16 - var last_bytes_bits byte - var hasher HasherHandle = nil - var hasher_eff_size uint = brotli_min_size_t(input_size, BROTLI_MAX_BACKWARD_LIMIT(uint(lgwin))+BROTLI_WINDOW_GAP) - var params BrotliEncoderParams - var lgmetablock int = brotli_min_int(24, lgwin+1) - var max_block_size uint - var max_metablock_size uint = uint(1) << uint(lgmetablock) - var max_literals_per_metablock uint = max_metablock_size / 8 - var max_commands_per_metablock uint = max_metablock_size / 8 - var metablock_start uint = 0 - var prev_byte byte = 0 - var prev_byte2 byte = 0 - - BrotliEncoderInitParams(¶ms) - params.quality = 10 - params.lgwin = uint(lgwin) - if lgwin > BROTLI_MAX_WINDOW_BITS { - params.large_window = true - } - - SanitizeParams(¶ms) - params.lgblock = ComputeLgBlock(¶ms) - ChooseDistanceParams(¶ms) - max_block_size = uint(1) << uint(params.lgblock) - - assert(input_size <= mask+1) - EncodeWindowBits(lgwin, params.large_window, &last_bytes, &last_bytes_bits) - InitOrStitchToPreviousBlock(&hasher, input_buffer, mask, ¶ms, 0, hasher_eff_size, true) - - for ok && metablock_start < input_size { - var metablock_end uint = brotli_min_size_t(input_size, metablock_start+max_metablock_size) - var expected_num_commands uint = (metablock_end-metablock_start)/12 + 16 - var commands []command = nil - var num_commands uint = 0 - var last_insert_len uint = 0 - var num_literals uint = 0 - var metablock_size uint = 0 - var cmd_alloc_size uint = 0 - var is_last bool - var storage []byte - var storage_ix uint - var literal_context_mode int = ChooseContextMode(¶ms, input_buffer, metablock_start, mask, metablock_end-metablock_start) - var block_start uint - for block_start = metablock_start; block_start < metablock_end; { - var block_size uint = brotli_min_size_t(metablock_end-block_start, max_block_size) - var nodes []zopfliNode = make([]zopfliNode, (block_size + 1)) - var path_size uint - var new_cmd_alloc_size uint - initZopfliNodes(nodes, block_size+1) - hasher.StitchToPreviousBlock(block_size, block_start, input_buffer, mask) - path_size = zopfliComputeShortestPath(block_size, block_start, input_buffer, mask, ¶ms, dist_cache[:], hasher.(*H10), nodes) - - /* We allocate a command buffer in the first iteration of this loop that - will be likely big enough for the whole metablock, so that for most - inputs we will not have to reallocate in later iterations. We do the - allocation here and not before the loop, because if the input is small, - this will be allocated after the Zopfli cost model is freed, so this - will not increase peak memory usage. - TODO: If the first allocation is too small, increase command - buffer size exponentially. */ - new_cmd_alloc_size = brotli_max_size_t(expected_num_commands, num_commands+path_size+1) - - if cmd_alloc_size != new_cmd_alloc_size { - var new_commands []command = make([]command, new_cmd_alloc_size) - cmd_alloc_size = new_cmd_alloc_size - if commands != nil { - copy(new_commands, commands[:num_commands]) - commands = nil - } - - commands = new_commands - } - - zopfliCreateCommands(block_size, block_start, nodes[0:], dist_cache[:], &last_insert_len, ¶ms, commands[num_commands:], &num_literals) - num_commands += path_size - block_start += block_size - metablock_size += block_size - nodes = nil - if num_literals > max_literals_per_metablock || num_commands > max_commands_per_metablock { - break - } - } - - if last_insert_len > 0 { - initInsertCommand(&commands[num_commands], last_insert_len) - num_commands++ - num_literals += last_insert_len - } - - is_last = (metablock_start+metablock_size == input_size) - storage = nil - storage_ix = uint(last_bytes_bits) - - if metablock_size == 0 { - /* Write the ISLAST and ISEMPTY bits. */ - storage = make([]byte, 16) - - storage[0] = byte(last_bytes) - storage[1] = byte(last_bytes >> 8) - BrotliWriteBits(2, 3, &storage_ix, storage) - storage_ix = (storage_ix + 7) &^ 7 - } else if !ShouldCompress_encode(input_buffer, mask, uint64(metablock_start), metablock_size, num_literals, num_commands) { - /* Restore the distance cache, as its last update by - CreateBackwardReferences is now unused. */ - copy(dist_cache[:], saved_dist_cache[:4]) - - storage = make([]byte, (metablock_size + 16)) - storage[0] = byte(last_bytes) - storage[1] = byte(last_bytes >> 8) - storeUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) - } else { - var mb MetaBlockSplit - var block_params BrotliEncoderParams = params - InitMetaBlockSplit(&mb) - BrotliBuildMetaBlock(input_buffer, metablock_start, mask, &block_params, prev_byte, prev_byte2, commands, num_commands, literal_context_mode, &mb) - { - /* The number of distance symbols effectively used for distance - histograms. It might be less than distance alphabet size - for "Large Window Brotli" (32-bit). */ - var num_effective_dist_codes uint32 = block_params.dist.alphabet_size - if num_effective_dist_codes > BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS { - num_effective_dist_codes = BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS - } - - BrotliOptimizeHistograms(num_effective_dist_codes, &mb) - } - - storage = make([]byte, (2*metablock_size + 503)) - storage[0] = byte(last_bytes) - storage[1] = byte(last_bytes >> 8) - storeMetaBlock(input_buffer, metablock_start, metablock_size, mask, prev_byte, prev_byte2, is_last, &block_params, literal_context_mode, commands, num_commands, &mb, &storage_ix, storage) - if metablock_size+4 < storage_ix>>3 { - /* Restore the distance cache and last byte. */ - copy(dist_cache[:], saved_dist_cache[:4]) - - storage[0] = byte(last_bytes) - storage[1] = byte(last_bytes >> 8) - storage_ix = uint(last_bytes_bits) - storeUncompressedMetaBlock(is_last, input_buffer, metablock_start, mask, metablock_size, &storage_ix, storage) - } - - DestroyMetaBlockSplit(&mb) - } - - last_bytes = uint16(storage[storage_ix>>3]) - last_bytes_bits = byte(storage_ix & 7) - metablock_start += metablock_size - if metablock_start < input_size { - prev_byte = input_buffer[metablock_start-1] - prev_byte2 = input_buffer[metablock_start-2] - } - - /* Save the state of the distance cache in case we need to restore it for - emitting an uncompressed block. */ - copy(saved_dist_cache[:], dist_cache[:4]) - { - var out_size uint = storage_ix >> 3 - total_out_size += out_size - if total_out_size <= max_out_size { - copy(encoded_buffer, storage[:out_size]) - encoded_buffer = encoded_buffer[out_size:] - } else { - ok = false - } - } - - storage = nil - commands = nil - } - - *encoded_size = total_out_size - DestroyHasher(&hasher) - return ok -} - -func BrotliEncoderMaxCompressedSize(input_size uint) uint { - var num_large_blocks uint = input_size >> 14 - var overhead uint = 2 + (4 * num_large_blocks) + 3 + 1 - /* [window bits / empty metadata] + N * [uncompressed] + [last empty] */ - - var result uint = input_size + overhead - if input_size == 0 { - return 2 - } - if result < input_size { - return 0 - } else { - return result - } -} - -/* Wraps data to uncompressed brotli stream with minimal window size. - |output| should point at region with at least BrotliEncoderMaxCompressedSize - addressable bytes. - Returns the length of stream. */ -func MakeUncompressedStream(input []byte, input_size uint, output []byte) uint { - var size uint = input_size - var result uint = 0 - var offset uint = 0 - if input_size == 0 { - output[0] = 6 - return 1 - } - - output[result] = 0x21 - result++ /* window bits = 10, is_last = false */ - output[result] = 0x03 - result++ /* empty metadata, padding */ - for size > 0 { - var nibbles uint32 = 0 - var chunk_size uint32 - var bits uint32 - if size > 1<<24 { - chunk_size = 1 << 24 - } else { - chunk_size = uint32(size) - } - if chunk_size > 1<<16 { - if chunk_size > 1<<20 { - nibbles = 2 - } else { - nibbles = 1 - } - } - bits = nibbles<<1 | (chunk_size-1)<<3 | 1<<(19+4*nibbles) - output[result] = byte(bits) - result++ - output[result] = byte(bits >> 8) - result++ - output[result] = byte(bits >> 16) - result++ - if nibbles == 2 { - output[result] = byte(bits >> 24) - result++ - } - copy(output[result:], input[offset:][:chunk_size]) - result += uint(chunk_size) - offset += uint(chunk_size) - size -= uint(chunk_size) - } - - output[result] = 3 - result++ - return result -} - -func InjectBytePaddingBlock(s *Writer) { +func injectBytePaddingBlock(s *Writer) { var seal uint32 = uint32(s.last_bytes_) var seal_bits uint = uint(s.last_bytes_bits_) var destination []byte @@ -1749,14 +1476,14 @@ func InjectBytePaddingBlock(s *Writer) { s.available_out_ += (seal_bits + 7) >> 3 } -func CheckFlushComplete(s *Writer) { - if s.stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED && s.available_out_ == 0 { - s.stream_state_ = BROTLI_STREAM_PROCESSING +func checkFlushComplete(s *Writer) { + if s.stream_state_ == streamFlushRequested && s.available_out_ == 0 { + s.stream_state_ = streamProcessing s.next_out_ = nil } } -func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next_in *[]byte) bool { +func encoderCompressStreamFast(s *Writer, op int, available_in *uint, next_in *[]byte) bool { var block_size_limit uint = uint(1) << s.params.lgwin var buf_size uint = brotli_min_size_t(kCompressFragmentTwoPassBlockSize, brotli_min_size_t(*available_in, block_size_limit)) var tmp_command_buf []uint32 = nil @@ -1785,18 +1512,18 @@ func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next } for { - if s.stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED && s.last_bytes_bits_ != 0 { - InjectBytePaddingBlock(s) + if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 { + injectBytePaddingBlock(s) continue } /* Compress block only when internal output buffer is empty, stream is not finished, there is no pending flush request, and there is either additional input or pending operation. */ - if s.available_out_ == 0 && s.stream_state_ == BROTLI_STREAM_PROCESSING && (*available_in != 0 || op != int(BROTLI_OPERATION_PROCESS)) { + if s.available_out_ == 0 && s.stream_state_ == streamProcessing && (*available_in != 0 || op != int(operationProcess)) { var block_size uint = brotli_min_size_t(block_size_limit, *available_in) - var is_last bool = (*available_in == block_size) && (op == int(BROTLI_OPERATION_FINISH)) - var force_flush bool = (*available_in == block_size) && (op == int(BROTLI_OPERATION_FLUSH)) + var is_last bool = (*available_in == block_size) && (op == int(operationFinish)) + var force_flush bool = (*available_in == block_size) && (op == int(operationFlush)) var max_out_size uint = 2*block_size + 503 var storage []byte = nil var storage_ix uint = uint(s.last_bytes_bits_) @@ -1804,15 +1531,15 @@ func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next var table []int if force_flush && block_size == 0 { - s.stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED + s.stream_state_ = streamFlushRequested continue } - storage = GetBrotliStorage(s, max_out_size) + storage = getBrotliStorage(s, max_out_size) storage[0] = byte(s.last_bytes_) storage[1] = byte(s.last_bytes_ >> 8) - table = GetHashTable(s, s.params.quality, block_size, &table_size) + table = getHashTable(s, s.params.quality, block_size, &table_size) if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY { compressFragmentFast(*next_in, block_size, is_last, table, table_size, s.cmd_depths_[:], s.cmd_bits_[:], &s.cmd_code_numbits_, s.cmd_code_[:], &storage_ix, storage) @@ -1830,10 +1557,10 @@ func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next s.last_bytes_bits_ = byte(storage_ix & 7) if force_flush { - s.stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED + s.stream_state_ = streamFlushRequested } if is_last { - s.stream_state_ = BROTLI_STREAM_FINISHED + s.stream_state_ = streamFinished } continue } @@ -1843,28 +1570,28 @@ func BrotliEncoderCompressStreamFast(s *Writer, op int, available_in *uint, next tmp_command_buf = nil tmp_literal_buf = nil - CheckFlushComplete(s) + checkFlushComplete(s) return true } -func ProcessMetadata(s *Writer, available_in *uint, next_in *[]byte) bool { +func processMetadata(s *Writer, available_in *uint, next_in *[]byte) bool { if *available_in > 1<<24 { return false } /* Switch to metadata block workflow, if required. */ - if s.stream_state_ == BROTLI_STREAM_PROCESSING { + if s.stream_state_ == streamProcessing { s.remaining_metadata_bytes_ = uint32(*available_in) - s.stream_state_ = BROTLI_STREAM_METADATA_HEAD + s.stream_state_ = streamMetadataHead } - if s.stream_state_ != BROTLI_STREAM_METADATA_HEAD && s.stream_state_ != BROTLI_STREAM_METADATA_BODY { + if s.stream_state_ != streamMetadataHead && s.stream_state_ != streamMetadataBody { return false } for { - if s.stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED && s.last_bytes_bits_ != 0 { - InjectBytePaddingBlock(s) + if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 { + injectBytePaddingBlock(s) continue } @@ -1873,24 +1600,24 @@ func ProcessMetadata(s *Writer, available_in *uint, next_in *[]byte) bool { } if s.input_pos_ != s.last_flush_pos_ { - var result bool = EncodeData(s, false, true, &s.available_out_, &s.next_out_) + var result bool = encodeData(s, false, true, &s.available_out_, &s.next_out_) if !result { return false } continue } - if s.stream_state_ == BROTLI_STREAM_METADATA_HEAD { + if s.stream_state_ == streamMetadataHead { s.next_out_ = s.tiny_buf_.u8[:] - s.available_out_ = WriteMetadataHeader(s, uint(s.remaining_metadata_bytes_), s.next_out_) - s.stream_state_ = BROTLI_STREAM_METADATA_BODY + s.available_out_ = writeMetadataHeader(s, uint(s.remaining_metadata_bytes_), s.next_out_) + s.stream_state_ = streamMetadataBody continue } else { /* Exit workflow only when there is no more input and no more output. Otherwise client may continue producing empty metadata blocks. */ if s.remaining_metadata_bytes_ == 0 { s.remaining_metadata_bytes_ = BROTLI_UINT32_MAX - s.stream_state_ = BROTLI_STREAM_PROCESSING + s.stream_state_ = streamProcessing break } @@ -1910,9 +1637,9 @@ func ProcessMetadata(s *Writer, available_in *uint, next_in *[]byte) bool { return true } -func UpdateSizeHint(s *Writer, available_in uint) { +func updateSizeHint(s *Writer, available_in uint) { if s.params.size_hint == 0 { - var delta uint64 = UnprocessedInputSize(s) + var delta uint64 = unprocessedInputSize(s) var tail uint64 = uint64(available_in) var limit uint32 = 1 << 30 var total uint32 @@ -1926,8 +1653,8 @@ func UpdateSizeHint(s *Writer, available_in uint) { } } -func BrotliEncoderCompressStream(s *Writer, op int, available_in *uint, next_in *[]byte) bool { - if !EnsureInitialized(s) { +func encoderCompressStream(s *Writer, op int, available_in *uint, next_in *[]byte) bool { + if !ensureInitialized(s) { return false } @@ -1936,61 +1663,61 @@ func BrotliEncoderCompressStream(s *Writer, op int, available_in *uint, next_in if uint32(*available_in) != s.remaining_metadata_bytes_ { return false } - if op != int(BROTLI_OPERATION_EMIT_METADATA) { + if op != int(operationEmitMetadata) { return false } } - if op == int(BROTLI_OPERATION_EMIT_METADATA) { - UpdateSizeHint(s, 0) /* First data metablock might be emitted here. */ - return ProcessMetadata(s, available_in, next_in) + if op == int(operationEmitMetadata) { + updateSizeHint(s, 0) /* First data metablock might be emitted here. */ + return processMetadata(s, available_in, next_in) } - if s.stream_state_ == BROTLI_STREAM_METADATA_HEAD || s.stream_state_ == BROTLI_STREAM_METADATA_BODY { + if s.stream_state_ == streamMetadataHead || s.stream_state_ == streamMetadataBody { return false } - if s.stream_state_ != BROTLI_STREAM_PROCESSING && *available_in != 0 { + if s.stream_state_ != streamProcessing && *available_in != 0 { return false } if s.params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY || s.params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY { - return BrotliEncoderCompressStreamFast(s, op, available_in, next_in) + return encoderCompressStreamFast(s, op, available_in, next_in) } for { - var remaining_block_size uint = RemainingInputBlockSize(s) + var remaining_block_size uint = remainingInputBlockSize(s) if remaining_block_size != 0 && *available_in != 0 { var copy_input_size uint = brotli_min_size_t(remaining_block_size, *available_in) - CopyInputToRingBuffer(s, copy_input_size, *next_in) + copyInputToRingBuffer(s, copy_input_size, *next_in) *next_in = (*next_in)[copy_input_size:] *available_in -= copy_input_size continue } - if s.stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED && s.last_bytes_bits_ != 0 { - InjectBytePaddingBlock(s) + if s.stream_state_ == streamFlushRequested && s.last_bytes_bits_ != 0 { + injectBytePaddingBlock(s) continue } /* Compress data only when internal output buffer is empty, stream is not finished and there is no pending flush request. */ - if s.available_out_ == 0 && s.stream_state_ == BROTLI_STREAM_PROCESSING { - if remaining_block_size == 0 || op != int(BROTLI_OPERATION_PROCESS) { - var is_last bool = ((*available_in == 0) && op == int(BROTLI_OPERATION_FINISH)) - var force_flush bool = ((*available_in == 0) && op == int(BROTLI_OPERATION_FLUSH)) + if s.available_out_ == 0 && s.stream_state_ == streamProcessing { + if remaining_block_size == 0 || op != int(operationProcess) { + var is_last bool = ((*available_in == 0) && op == int(operationFinish)) + var force_flush bool = ((*available_in == 0) && op == int(operationFlush)) var result bool - UpdateSizeHint(s, *available_in) - result = EncodeData(s, is_last, force_flush, &s.available_out_, &s.next_out_) + updateSizeHint(s, *available_in) + result = encodeData(s, is_last, force_flush, &s.available_out_, &s.next_out_) if !result { return false } if force_flush { - s.stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED + s.stream_state_ = streamFlushRequested } if is_last { - s.stream_state_ = BROTLI_STREAM_FINISHED + s.stream_state_ = streamFinished } continue } @@ -1999,30 +1726,22 @@ func BrotliEncoderCompressStream(s *Writer, op int, available_in *uint, next_in break } - CheckFlushComplete(s) + checkFlushComplete(s) return true } -func BrotliEncoderIsFinished(s *Writer) bool { - return s.stream_state_ == BROTLI_STREAM_FINISHED && !BrotliEncoderHasMoreOutput(s) -} - -func BrotliEncoderHasMoreOutput(s *Writer) bool { +func encoderHasMoreOutput(s *Writer) bool { return s.available_out_ != 0 } -func BrotliEncoderTakeOutput(s *Writer) []byte { +func encoderTakeOutput(s *Writer) []byte { if s.available_out_ == 0 { return nil } result := s.next_out_[:s.available_out_] s.total_out_ += s.available_out_ s.available_out_ = 0 - CheckFlushComplete(s) + checkFlushComplete(s) return result } - -func BrotliEncoderVersion() uint32 { - return BROTLI_VERSION -} diff --git a/encoder_dict.go b/encoder_dict.go index 15e02c8..256f3ce 100644 --- a/encoder_dict.go +++ b/encoder_dict.go @@ -1,8 +1,8 @@ package brotli /* Dictionary data (words and transforms) for 1 possible context */ -type BrotliEncoderDictionary struct { - words *BrotliDictionary +type encoderDictionary struct { + words *dictionary cutoffTransformsCount uint32 cutoffTransforms uint64 hash_table []uint16 @@ -10,8 +10,8 @@ type BrotliEncoderDictionary struct { dict_words []DictWord } -func BrotliInitEncoderDictionary(dict *BrotliEncoderDictionary) { - dict.words = BrotliGetDictionary() +func initEncoderDictionary(dict *encoderDictionary) { + dict.words = getDictionary() dict.hash_table = kStaticDictionaryHash[:] dict.buckets = kStaticDictionaryBuckets[:] diff --git a/entropy_encode.go b/entropy_encode.go index 5c98128..588b7ff 100644 --- a/entropy_encode.go +++ b/entropy_encode.go @@ -16,13 +16,13 @@ package brotli /* Entropy encoding (Huffman) utilities. */ /* A node of a Huffman tree. */ -type HuffmanTree struct { +type huffmanTree struct { total_count_ uint32 index_left_ int16 index_right_or_value_ int16 } -func InitHuffmanTree(self *HuffmanTree, count uint32, left int16, right int16) { +func initHuffmanTree(self *huffmanTree, count uint32, left int16, right int16) { self.total_count_ = count self.index_left_ = left self.index_right_or_value_ = right @@ -58,16 +58,16 @@ func InitHuffmanTree(self *HuffmanTree, count uint32, left int16, right int16) { /* Get the actual bit values for a tree of bit depths. */ /* Input size optimized Shell sort. */ -type HuffmanTreeComparator func(*HuffmanTree, *HuffmanTree) bool +type huffmanTreeComparator func(*huffmanTree, *huffmanTree) bool -var SortHuffmanTreeItems_gaps = []uint{132, 57, 23, 10, 4, 1} +var sortHuffmanTreeItems_gaps = []uint{132, 57, 23, 10, 4, 1} -func SortHuffmanTreeItems(items []HuffmanTree, n uint, comparator HuffmanTreeComparator) { +func sortHuffmanTreeItems(items []huffmanTree, n uint, comparator huffmanTreeComparator) { if n < 13 { /* Insertion sort. */ var i uint for i = 1; i < n; i++ { - var tmp HuffmanTree = items[i] + var tmp huffmanTree = items[i] var k uint = i var j uint = i - 1 for comparator(&tmp, &items[j]) { @@ -92,11 +92,11 @@ func SortHuffmanTreeItems(items []HuffmanTree, n uint, comparator HuffmanTreeCom g = 0 } for ; g < 6; g++ { - var gap uint = SortHuffmanTreeItems_gaps[g] + var gap uint = sortHuffmanTreeItems_gaps[g] var i uint for i = gap; i < n; i++ { var j uint = i - var tmp HuffmanTree = items[i] + var tmp huffmanTree = items[i] for ; j >= gap && comparator(&tmp, &items[j-gap]); j -= gap { items[j] = items[j-gap] } @@ -107,7 +107,7 @@ func SortHuffmanTreeItems(items []HuffmanTree, n uint, comparator HuffmanTreeCom } } -func BrotliSetDepth(p0 int, pool []HuffmanTree, depth []byte, max_depth int) bool { +func setDepth(p0 int, pool []huffmanTree, depth []byte, max_depth int) bool { var stack [16]int var level int = 0 var p int = p0 @@ -138,7 +138,7 @@ func BrotliSetDepth(p0 int, pool []HuffmanTree, depth []byte, max_depth int) boo } /* Sort the root nodes, least popular first. */ -func SortHuffmanTree(v0 *HuffmanTree, v1 *HuffmanTree) bool { +func sortHuffmanTree(v0 *huffmanTree, v1 *huffmanTree) bool { if v0.total_count_ != v1.total_count_ { return v0.total_count_ < v1.total_count_ } @@ -161,10 +161,10 @@ func SortHuffmanTree(v0 *HuffmanTree, v1 *HuffmanTree) bool { we are not planning to use this with extremely long blocks. See http://en.wikipedia.org/wiki/Huffman_coding */ -func BrotliCreateHuffmanTree(data []uint32, length uint, tree_limit int, tree []HuffmanTree, depth []byte) { +func createHuffmanTree(data []uint32, length uint, tree_limit int, tree []huffmanTree, depth []byte) { var count_limit uint32 - var sentinel HuffmanTree - InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1) + var sentinel huffmanTree + initHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1) /* For block sizes below 64 kB, we never need to do a second iteration of this loop. Probably all of our block sizes will be smaller than @@ -179,7 +179,7 @@ func BrotliCreateHuffmanTree(data []uint32, length uint, tree_limit int, tree [] i-- if data[i] != 0 { var count uint32 = brotli_max_uint32_t(data[i], count_limit) - InitHuffmanTree(&tree[n], count, -1, int16(i)) + initHuffmanTree(&tree[n], count, -1, int16(i)) n++ } } @@ -189,7 +189,7 @@ func BrotliCreateHuffmanTree(data []uint32, length uint, tree_limit int, tree [] break } - SortHuffmanTreeItems(tree, n, HuffmanTreeComparator(SortHuffmanTree)) + sortHuffmanTreeItems(tree, n, huffmanTreeComparator(sortHuffmanTree)) /* The nodes are: [0, n): the sorted leaf nodes that we start with. @@ -234,7 +234,7 @@ func BrotliCreateHuffmanTree(data []uint32, length uint, tree_limit int, tree [] } } - if BrotliSetDepth(int(2*n-1), tree[0:], depth, tree_limit) { + if setDepth(int(2*n-1), tree[0:], depth, tree_limit) { /* We need to pack the Huffman tree in tree_limit bits. If this was not successful, add fake entities to the lowest values and retry. */ break @@ -242,7 +242,7 @@ func BrotliCreateHuffmanTree(data []uint32, length uint, tree_limit int, tree [] } } -func Reverse(v []byte, start uint, end uint) { +func reverse(v []byte, start uint, end uint) { end-- for start < end { var tmp byte = v[start] @@ -253,7 +253,7 @@ func Reverse(v []byte, start uint, end uint) { } } -func BrotliWriteHuffmanTreeRepetitions(previous_value byte, value byte, repetitions uint, tree_size *uint, tree []byte, extra_bits_data []byte) { +func writeHuffmanTreeRepetitions(previous_value byte, value byte, repetitions uint, tree_size *uint, tree []byte, extra_bits_data []byte) { assert(repetitions > 0) if previous_value != value { tree[*tree_size] = value @@ -291,12 +291,12 @@ func BrotliWriteHuffmanTreeRepetitions(previous_value byte, value byte, repetiti repetitions-- } - Reverse(tree, start, *tree_size) - Reverse(extra_bits_data, start, *tree_size) + reverse(tree, start, *tree_size) + reverse(extra_bits_data, start, *tree_size) } } -func BrotliWriteHuffmanTreeRepetitionsZeros(repetitions uint, tree_size *uint, tree []byte, extra_bits_data []byte) { +func writeHuffmanTreeRepetitionsZeros(repetitions uint, tree_size *uint, tree []byte, extra_bits_data []byte) { if repetitions == 11 { tree[*tree_size] = 0 extra_bits_data[*tree_size] = 0 @@ -326,12 +326,12 @@ func BrotliWriteHuffmanTreeRepetitionsZeros(repetitions uint, tree_size *uint, t repetitions-- } - Reverse(tree, start, *tree_size) - Reverse(extra_bits_data, start, *tree_size) + reverse(tree, start, *tree_size) + reverse(extra_bits_data, start, *tree_size) } } -func BrotliOptimizeHuffmanCountsForRle(length uint, counts []uint32, good_for_rle []byte) { +func optimizeHuffmanCountsForRLE(length uint, counts []uint32, good_for_rle []byte) { var nonzero_count uint = 0 var stride uint var limit uint @@ -477,7 +477,7 @@ func BrotliOptimizeHuffmanCountsForRle(length uint, counts []uint32, good_for_rl } } -func DecideOverRleUse(depth []byte, length uint, use_rle_for_non_zero *bool, use_rle_for_zero *bool) { +func decideOverRLEUse(depth []byte, length uint, use_rle_for_non_zero *bool, use_rle_for_zero *bool) { var total_reps_zero uint = 0 var total_reps_non_zero uint = 0 var count_reps_zero uint = 1 @@ -508,7 +508,7 @@ func DecideOverRleUse(depth []byte, length uint, use_rle_for_non_zero *bool, use *use_rle_for_zero = total_reps_zero > count_reps_zero*2 } -func BrotliWriteHuffmanTree(depth []byte, length uint, tree_size *uint, tree []byte, extra_bits_data []byte) { +func writeHuffmanTree(depth []byte, length uint, tree_size *uint, tree []byte, extra_bits_data []byte) { var previous_value byte = initialRepeatedCodeLength var i uint var use_rle_for_non_zero bool = false @@ -527,7 +527,7 @@ func BrotliWriteHuffmanTree(depth []byte, length uint, tree_size *uint, tree []b if length > 50 { /* Find RLE coding for longer codes. Shorter codes seem not to benefit from RLE. */ - DecideOverRleUse(depth, new_length, &use_rle_for_non_zero, &use_rle_for_zero) + decideOverRLEUse(depth, new_length, &use_rle_for_non_zero, &use_rle_for_zero) } /* Actual RLE coding. */ @@ -542,9 +542,9 @@ func BrotliWriteHuffmanTree(depth []byte, length uint, tree_size *uint, tree []b } if value == 0 { - BrotliWriteHuffmanTreeRepetitionsZeros(reps, tree_size, tree, extra_bits_data) + writeHuffmanTreeRepetitionsZeros(reps, tree_size, tree, extra_bits_data) } else { - BrotliWriteHuffmanTreeRepetitions(previous_value, value, reps, tree_size, tree, extra_bits_data) + writeHuffmanTreeRepetitions(previous_value, value, reps, tree_size, tree, extra_bits_data) previous_value = value } @@ -552,7 +552,7 @@ func BrotliWriteHuffmanTree(depth []byte, length uint, tree_size *uint, tree []b } } -var BrotliReverseBits_kLut = [16]uint{ +var reverseBits_kLut = [16]uint{ 0x00, 0x08, 0x04, @@ -571,13 +571,13 @@ var BrotliReverseBits_kLut = [16]uint{ 0x0F, } -func BrotliReverseBits(num_bits uint, bits uint16) uint16 { - var retval uint = BrotliReverseBits_kLut[bits&0x0F] +func reverseBits(num_bits uint, bits uint16) uint16 { + var retval uint = reverseBits_kLut[bits&0x0F] var i uint for i = 4; i < num_bits; i += 4 { retval <<= 4 bits = uint16(bits >> 4) - retval |= BrotliReverseBits_kLut[bits&0x0F] + retval |= reverseBits_kLut[bits&0x0F] } retval >>= ((0 - num_bits) & 0x03) @@ -585,11 +585,11 @@ func BrotliReverseBits(num_bits uint, bits uint16) uint16 { } /* 0..15 are values for bits */ -const MAX_HUFFMAN_BITS = 16 +const maxHuffmanBits = 16 -func BrotliConvertBitDepthsToSymbols(depth []byte, len uint, bits []uint16) { - var bl_count = [MAX_HUFFMAN_BITS]uint16{0} - var next_code [MAX_HUFFMAN_BITS]uint16 +func convertBitDepthsToSymbols(depth []byte, len uint, bits []uint16) { + var bl_count = [maxHuffmanBits]uint16{0} + var next_code [maxHuffmanBits]uint16 var i uint /* In Brotli, all bit depths are [1..15] 0 bit depth means that the symbol does not exist. */ @@ -601,14 +601,14 @@ func BrotliConvertBitDepthsToSymbols(depth []byte, len uint, bits []uint16) { bl_count[0] = 0 next_code[0] = 0 - for i = 1; i < MAX_HUFFMAN_BITS; i++ { + for i = 1; i < maxHuffmanBits; i++ { code = (code + int(bl_count[i-1])) << 1 next_code[i] = uint16(code) } for i = 0; i < len; i++ { if depth[i] != 0 { - bits[i] = BrotliReverseBits(uint(depth[i]), next_code[depth[i]]) + bits[i] = reverseBits(uint(depth[i]), next_code[depth[i]]) next_code[depth[i]]++ } } diff --git a/entropy_encode_static.go b/entropy_encode_static.go index 18ca07d..e1c8689 100644 --- a/entropy_encode_static.go +++ b/entropy_encode_static.go @@ -4317,7 +4317,7 @@ var kStaticCommandCodeBits = [numCommandSymbols]uint16{ 2047, } -func StoreStaticCommandHuffmanTree(storage_ix *uint, storage []byte) { +func storeStaticCommandHuffmanTree(storage_ix *uint, storage []byte) { BrotliWriteBits(56, 0x92624416307003, storage_ix, storage) BrotliWriteBits(3, 0x00000000, storage_ix, storage) } @@ -4389,6 +4389,6 @@ var kStaticDistanceCodeBits = [64]uint16{ 63, } -func StoreStaticDistanceHuffmanTree(storage_ix *uint, storage []byte) { +func storeStaticDistanceHuffmanTree(storage_ix *uint, storage []byte) { BrotliWriteBits(28, 0x0369DC03, storage_ix, storage) } diff --git a/fast_log.go b/fast_log.go index 219bffe..e8f0e63 100644 --- a/fast_log.go +++ b/fast_log.go @@ -29,7 +29,7 @@ import "math" */ /* Utilities for fast computation of logarithms. */ -func Log2FloorNonZero(n uint) uint32 { +func log2FloorNonZero(n uint) uint32 { /* TODO: generalize and move to platform.h */ var result uint32 = 0 for { @@ -305,10 +305,8 @@ var kLog2Table = []float32{ 7.9943534368588578, } -const LOG_2_INV = 1.4426950408889634 - /* Faster logarithm for small integers, with the property of log2(0) == 0. */ -func FastLog2(v uint) float64 { +func fastLog2(v uint) float64 { if v < uint(len(kLog2Table)) { return float64(kLog2Table[v]) } diff --git a/find_match_length.go b/find_match_length.go index 39228f3..855c896 100644 --- a/find_match_length.go +++ b/find_match_length.go @@ -15,7 +15,7 @@ package brotli */ /* Function to find maximal matching prefixes of strings. */ -func FindMatchLengthWithLimit(s1 []byte, s2 []byte, limit uint) uint { +func findMatchLengthWithLimit(s1 []byte, s2 []byte, limit uint) uint { var matched uint = 0 for matched < limit && s1[matched] == s2[matched] { matched++ diff --git a/h10.go b/h10.go index 41844f2..e24c13a 100644 --- a/h10.go +++ b/h10.go @@ -15,15 +15,15 @@ import "encoding/binary" position in the input data. The binary tree is sorted by the lexicographic order of the sequences, and it is also a max-heap with respect to the starting positions. */ -func (*H10) HashTypeLength() uint { +func (*h10) HashTypeLength() uint { return 4 } -func (*H10) StoreLookahead() uint { +func (*h10) StoreLookahead() uint { return 128 } -func HashBytesH10(data []byte) uint32 { +func hashBytesH10(data []byte) uint32 { var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, @@ -31,30 +31,22 @@ func HashBytesH10(data []byte) uint32 { return h >> (32 - 17) } -type H10 struct { - HasherCommon +type h10 struct { + hasherCommon window_mask_ uint buckets_ [1 << 17]uint32 invalid_pos_ uint32 forest []uint32 } -func SelfH10(handle HasherHandle) *H10 { - return handle.(*H10) -} - -func ForestH10(self *H10) []uint32 { - return []uint32(self.forest) -} - -func (h *H10) Initialize(params *BrotliEncoderParams) { +func (h *h10) Initialize(params *encoderParams) { h.window_mask_ = (1 << params.lgwin) - 1 h.invalid_pos_ = uint32(0 - h.window_mask_) var num_nodes uint = uint(1) << params.lgwin h.forest = make([]uint32, 2*num_nodes) } -func (h *H10) Prepare(one_shot bool, input_size uint, data []byte) { +func (h *h10) Prepare(one_shot bool, input_size uint, data []byte) { var invalid_pos uint32 = h.invalid_pos_ var i uint32 for i = 0; i < 1<<17; i++ { @@ -62,11 +54,11 @@ func (h *H10) Prepare(one_shot bool, input_size uint, data []byte) { } } -func LeftChildIndexH10(self *H10, pos uint) uint { +func leftChildIndexH10(self *h10, pos uint) uint { return 2 * (pos & self.window_mask_) } -func RightChildIndexH10(self *H10, pos uint) uint { +func rightChildIndexH10(self *h10, pos uint) uint { return 2*(pos&self.window_mask_) + 1 } @@ -80,15 +72,15 @@ func RightChildIndexH10(self *H10, pos uint) uint { current (incomplete) sequence. This function must be called with increasing cur_ix positions. */ -func StoreAndFindMatchesH10(self *H10, data []byte, cur_ix uint, ring_buffer_mask uint, max_length uint, max_backward uint, best_len *uint, matches []BackwardMatch) []BackwardMatch { +func storeAndFindMatchesH10(self *h10, data []byte, cur_ix uint, ring_buffer_mask uint, max_length uint, max_backward uint, best_len *uint, matches []backwardMatch) []backwardMatch { var cur_ix_masked uint = cur_ix & ring_buffer_mask var max_comp_len uint = brotli_min_size_t(max_length, 128) var should_reroot_tree bool = (max_length >= 128) - var key uint32 = HashBytesH10(data[cur_ix_masked:]) - var forest []uint32 = ForestH10(self) + var key uint32 = hashBytesH10(data[cur_ix_masked:]) + var forest []uint32 = self.forest var prev_ix uint = uint(self.buckets_[key]) - var node_left uint = LeftChildIndexH10(self, cur_ix) - var node_right uint = RightChildIndexH10(self, cur_ix) + var node_left uint = leftChildIndexH10(self, cur_ix) + var node_right uint = rightChildIndexH10(self, cur_ix) var best_len_left uint = 0 var best_len_right uint = 0 var depth_remaining uint @@ -122,17 +114,17 @@ func StoreAndFindMatchesH10(self *H10, data []byte, cur_ix uint, ring_buffer_mas var cur_len uint = brotli_min_size_t(best_len_left, best_len_right) var len uint assert(cur_len <= 128) - len = cur_len + FindMatchLengthWithLimit(data[cur_ix_masked+cur_len:], data[prev_ix_masked+cur_len:], max_length-cur_len) + len = cur_len + findMatchLengthWithLimit(data[cur_ix_masked+cur_len:], data[prev_ix_masked+cur_len:], max_length-cur_len) if matches != nil && len > *best_len { *best_len = uint(len) - InitBackwardMatch(&matches[0], backward, uint(len)) + initBackwardMatch(&matches[0], backward, uint(len)) matches = matches[1:] } if len >= max_comp_len { if should_reroot_tree { - forest[node_left] = forest[LeftChildIndexH10(self, prev_ix)] - forest[node_right] = forest[RightChildIndexH10(self, prev_ix)] + forest[node_left] = forest[leftChildIndexH10(self, prev_ix)] + forest[node_right] = forest[rightChildIndexH10(self, prev_ix)] } break @@ -144,7 +136,7 @@ func StoreAndFindMatchesH10(self *H10, data []byte, cur_ix uint, ring_buffer_mas forest[node_left] = uint32(prev_ix) } - node_left = RightChildIndexH10(self, prev_ix) + node_left = rightChildIndexH10(self, prev_ix) prev_ix = uint(forest[node_left]) } else { best_len_right = uint(len) @@ -152,7 +144,7 @@ func StoreAndFindMatchesH10(self *H10, data []byte, cur_ix uint, ring_buffer_mas forest[node_right] = uint32(prev_ix) } - node_right = LeftChildIndexH10(self, prev_ix) + node_right = leftChildIndexH10(self, prev_ix) prev_ix = uint(forest[node_right]) } } @@ -168,8 +160,8 @@ func StoreAndFindMatchesH10(self *H10, data []byte, cur_ix uint, ring_buffer_mas matches in matches[0] to matches[*num_matches - 1]. The matches will be sorted by strictly increasing length and (non-strictly) increasing distance. */ -func FindAllMatchesH10(handle HasherHandle, dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, cur_ix uint, max_length uint, max_backward uint, gap uint, params *BrotliEncoderParams, matches []BackwardMatch) uint { - var orig_matches []BackwardMatch = matches +func findAllMatchesH10(handle *h10, dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, cur_ix uint, max_length uint, max_backward uint, gap uint, params *encoderParams, matches []backwardMatch) uint { + var orig_matches []backwardMatch = matches var cur_ix_masked uint = cur_ix & ring_buffer_mask var best_len uint = 1 var short_match_max_backward uint @@ -196,17 +188,17 @@ func FindAllMatchesH10(handle HasherHandle, dictionary *BrotliEncoderDictionary, continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len > best_len { best_len = uint(len) - InitBackwardMatch(&matches[0], backward, uint(len)) + initBackwardMatch(&matches[0], backward, uint(len)) matches = matches[1:] } } } if best_len < max_length { - matches = StoreAndFindMatchesH10(SelfH10(handle), data, cur_ix, ring_buffer_mask, max_length, max_backward, &best_len, matches) + matches = storeAndFindMatchesH10(handle, data, cur_ix, ring_buffer_mask, max_length, max_backward, &best_len, matches) } for i = 0; i <= BROTLI_MAX_STATIC_DICTIONARY_MATCH_LEN; i++ { @@ -222,7 +214,7 @@ func FindAllMatchesH10(handle HasherHandle, dictionary *BrotliEncoderDictionary, if dict_id < kInvalidMatch { var distance uint = max_backward + gap + uint(dict_id>>5) + 1 if distance <= params.dist.max_distance { - InitDictionaryBackwardMatch(&matches[0], distance, l, uint(dict_id&31)) + initDictionaryBackwardMatch(&matches[0], distance, l, uint(dict_id&31)) matches = matches[1:] } } @@ -236,13 +228,13 @@ func FindAllMatchesH10(handle HasherHandle, dictionary *BrotliEncoderDictionary, /* Stores the hash of the next 4 bytes and re-roots the binary tree at the current sequence, without returning any matches. REQUIRES: ix + 128 <= end-of-current-block */ -func (h *H10) Store(data []byte, mask uint, ix uint) { - var max_backward uint = h.window_mask_ - BROTLI_WINDOW_GAP + 1 +func (h *h10) Store(data []byte, mask uint, ix uint) { + var max_backward uint = h.window_mask_ - windowGap + 1 /* Maximum distance is window size - 16, see section 9.1. of the spec. */ - StoreAndFindMatchesH10(h, data, ix, mask, 128, max_backward, nil, nil) + storeAndFindMatchesH10(h, data, ix, mask, 128, max_backward, nil, nil) } -func (h *H10) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) { +func (h *h10) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) { var i uint = ix_start var j uint = ix_start if ix_start+63 <= ix_end { @@ -260,7 +252,7 @@ func (h *H10) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) { } } -func (h *H10) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { +func (h *h10) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) { if num_bytes >= h.HashTypeLength()-1 && position >= 128 { var i_start uint = position - 128 + 1 var i_end uint = brotli_min_size_t(position, i_start+num_bytes) @@ -274,23 +266,23 @@ func (h *H10) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer [] Furthermore, we have to make sure that we don't look further back from the start of the next block than the window size, otherwise we could access already overwritten areas of the ring-buffer. */ - var max_backward uint = h.window_mask_ - brotli_max_size_t(BROTLI_WINDOW_GAP-1, position-i) + var max_backward uint = h.window_mask_ - brotli_max_size_t(windowGap-1, position-i) /* We know that i + 128 <= position + num_bytes, i.e. the end of the current block and that we have at least 128 tail in the ring-buffer. */ - StoreAndFindMatchesH10(h, ringbuffer, i, ringbuffer_mask, 128, max_backward, nil, nil) + storeAndFindMatchesH10(h, ringbuffer, i, ringbuffer_mask, 128, max_backward, nil, nil) } } } /* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */ -const MAX_NUM_MATCHES_H10 = 128 +const maxNumMatchesH10 = 128 -func (*H10) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (*h10) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { panic("unimplemented") } -func (*H10) PrepareDistanceCache(distance_cache []int) { +func (*h10) PrepareDistanceCache(distance_cache []int) { panic("unimplemented") } diff --git a/h5.go b/h5.go index 162acfc..13761ba 100644 --- a/h5.go +++ b/h5.go @@ -15,16 +15,16 @@ import "encoding/binary" This is a hash map of fixed size (bucket_size_) to a ring buffer of fixed size (block_size_). The ring buffer contains the last block_size_ index positions of the given hash key in the compressed data. */ -func (*H5) HashTypeLength() uint { +func (*h5) HashTypeLength() uint { return 4 } -func (*H5) StoreLookahead() uint { +func (*h5) StoreLookahead() uint { return 4 } /* HashBytes is the function that chooses the bucket to place the address in. */ -func HashBytesH5(data []byte, shift int) uint32 { +func hashBytesH5(data []byte, shift int) uint32 { var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, @@ -32,8 +32,8 @@ func HashBytesH5(data []byte, shift int) uint32 { return uint32(h >> uint(shift)) } -type H5 struct { - HasherCommon +type h5 struct { + hasherCommon bucket_size_ uint block_size_ uint hash_shift_ int @@ -42,19 +42,7 @@ type H5 struct { buckets []uint32 } -func SelfH5(handle HasherHandle) *H5 { - return handle.(*H5) -} - -func NumH5(self *H5) []uint16 { - return []uint16(self.num) -} - -func BucketsH5(self *H5) []uint32 { - return []uint32(self.buckets) -} - -func (h *H5) Initialize(params *BrotliEncoderParams) { +func (h *h5) Initialize(params *encoderParams) { h.hash_shift_ = 32 - h.params.bucket_bits h.bucket_size_ = uint(1) << uint(h.params.bucket_bits) h.block_size_ = uint(1) << uint(h.params.block_bits) @@ -63,14 +51,14 @@ func (h *H5) Initialize(params *BrotliEncoderParams) { h.buckets = make([]uint32, h.block_size_*h.bucket_size_) } -func (h *H5) Prepare(one_shot bool, input_size uint, data []byte) { +func (h *h5) Prepare(one_shot bool, input_size uint, data []byte) { var num []uint16 = h.num var partial_prepare_threshold uint = h.bucket_size_ >> 6 /* Partial preparation is 100 times slower (per socket). */ if one_shot && input_size <= partial_prepare_threshold { var i uint for i = 0; i < input_size; i++ { - var key uint32 = HashBytesH5(data[i:], h.hash_shift_) + var key uint32 = hashBytesH5(data[i:], h.hash_shift_) num[key] = 0 } } else { @@ -82,23 +70,23 @@ func (h *H5) Prepare(one_shot bool, input_size uint, data []byte) { /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and store the value of ix at that position. */ -func (h *H5) Store(data []byte, mask uint, ix uint) { +func (h *h5) Store(data []byte, mask uint, ix uint) { var num []uint16 = h.num - var key uint32 = HashBytesH5(data[ix&mask:], h.hash_shift_) + var key uint32 = hashBytesH5(data[ix&mask:], h.hash_shift_) var minor_ix uint = uint(num[key]) & uint(h.block_mask_) var offset uint = minor_ix + uint(key<= h.HashTypeLength()-1 && position >= 3 { /* Prepare the hashes for three last bytes of the last write. These could not be calculated before, since they require knowledge @@ -109,8 +97,8 @@ func (h *H5) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []b } } -func (h *H5) PrepareDistanceCache(distance_cache []int) { - PrepareDistanceCache(distance_cache, h.params.num_last_distances_to_check) +func (h *h5) PrepareDistanceCache(distance_cache []int) { + prepareDistanceCache(distance_cache, h.params.num_last_distances_to_check) } /* Find a longest backward match of &data[cur_ix] up to the length of @@ -124,7 +112,7 @@ func (h *H5) PrepareDistanceCache(distance_cache []int) { Does not look for matches further away than max_backward. Writes the best match into |out|. |out|->score is updated only if a better match is found. */ -func (h *H5) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *h5) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { var num []uint16 = h.num var buckets []uint32 = h.buckets var cur_ix_masked uint = cur_ix & ring_buffer_mask @@ -156,15 +144,15 @@ func (h *H5) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 3 || (len == 2 && i < 2) { /* Comparing for >= 2 does not change the semantics, but just saves for a few unnecessary binary logarithms in backward reference score, since we are not interested in such short matches. */ - var score uint = BackwardReferenceScoreUsingLastDistance(uint(len)) + var score uint = backwardReferenceScoreUsingLastDistance(uint(len)) if best_score < score { if i != 0 { - score -= BackwardReferencePenaltyUsingLastDistance(i) + score -= backwardReferencePenaltyUsingLastDistance(i) } if best_score < score { best_score = score @@ -178,7 +166,7 @@ func (h *H5) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, } } { - var key uint32 = HashBytesH5(data[cur_ix_masked:], h.hash_shift_) + var key uint32 = hashBytesH5(data[cur_ix_masked:], h.hash_shift_) bucket = buckets[key< h.block_size_ { @@ -200,12 +188,12 @@ func (h *H5) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { /* Comparing for >= 3 does not change the semantics, but just saves for a few unnecessary binary logarithms in backward reference score, since we are not interested in such short matches. */ - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if best_score < score { best_score = score best_len = uint(len) @@ -222,6 +210,6 @@ func (h *H5) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, } if min_score == out.score { - SearchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) + searchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) } } diff --git a/h6.go b/h6.go index c7361c5..cab8768 100644 --- a/h6.go +++ b/h6.go @@ -15,16 +15,16 @@ import "encoding/binary" This is a hash map of fixed size (bucket_size_) to a ring buffer of fixed size (block_size_). The ring buffer contains the last block_size_ index positions of the given hash key in the compressed data. */ -func (*H6) HashTypeLength() uint { +func (*h6) HashTypeLength() uint { return 8 } -func (*H6) StoreLookahead() uint { +func (*h6) StoreLookahead() uint { return 8 } /* HashBytes is the function that chooses the bucket to place the address in. */ -func HashBytesH6(data []byte, mask uint64, shift int) uint32 { +func hashBytesH6(data []byte, mask uint64, shift int) uint32 { var h uint64 = (binary.LittleEndian.Uint64(data) & mask) * kHashMul64Long /* The higher bits contain more mixture from the multiplication, @@ -32,8 +32,8 @@ func HashBytesH6(data []byte, mask uint64, shift int) uint32 { return uint32(h >> uint(shift)) } -type H6 struct { - HasherCommon +type h6 struct { + hasherCommon bucket_size_ uint block_size_ uint hash_shift_ int @@ -43,19 +43,7 @@ type H6 struct { buckets []uint32 } -func SelfH6(handle HasherHandle) *H6 { - return handle.(*H6) -} - -func NumH6(self *H6) []uint16 { - return []uint16(self.num) -} - -func BucketsH6(self *H6) []uint32 { - return []uint32(self.buckets) -} - -func (h *H6) Initialize(params *BrotliEncoderParams) { +func (h *h6) Initialize(params *encoderParams) { h.hash_shift_ = 64 - h.params.bucket_bits h.hash_mask_ = (^(uint64(0))) >> uint(64-8*h.params.hash_len) h.bucket_size_ = uint(1) << uint(h.params.bucket_bits) @@ -65,14 +53,14 @@ func (h *H6) Initialize(params *BrotliEncoderParams) { h.buckets = make([]uint32, h.block_size_*h.bucket_size_) } -func (h *H6) Prepare(one_shot bool, input_size uint, data []byte) { +func (h *h6) Prepare(one_shot bool, input_size uint, data []byte) { var num []uint16 = h.num var partial_prepare_threshold uint = h.bucket_size_ >> 6 /* Partial preparation is 100 times slower (per socket). */ if one_shot && input_size <= partial_prepare_threshold { var i uint for i = 0; i < input_size; i++ { - var key uint32 = HashBytesH6(data[i:], h.hash_mask_, h.hash_shift_) + var key uint32 = hashBytesH6(data[i:], h.hash_mask_, h.hash_shift_) num[key] = 0 } } else { @@ -84,23 +72,23 @@ func (h *H6) Prepare(one_shot bool, input_size uint, data []byte) { /* Look at 4 bytes at &data[ix & mask]. Compute a hash from these, and store the value of ix at that position. */ -func (h *H6) Store(data []byte, mask uint, ix uint) { +func (h *h6) Store(data []byte, mask uint, ix uint) { var num []uint16 = h.num - var key uint32 = HashBytesH6(data[ix&mask:], h.hash_mask_, h.hash_shift_) + var key uint32 = hashBytesH6(data[ix&mask:], h.hash_mask_, h.hash_shift_) var minor_ix uint = uint(num[key]) & uint(h.block_mask_) var offset uint = minor_ix + uint(key<= h.HashTypeLength()-1 && position >= 3 { /* Prepare the hashes for three last bytes of the last write. These could not be calculated before, since they require knowledge @@ -111,8 +99,8 @@ func (h *H6) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []b } } -func (h *H6) PrepareDistanceCache(distance_cache []int) { - PrepareDistanceCache(distance_cache, h.params.num_last_distances_to_check) +func (h *h6) PrepareDistanceCache(distance_cache []int) { + prepareDistanceCache(distance_cache, h.params.num_last_distances_to_check) } /* Find a longest backward match of &data[cur_ix] up to the length of @@ -126,7 +114,7 @@ func (h *H6) PrepareDistanceCache(distance_cache []int) { Does not look for matches further away than max_backward. Writes the best match into |out|. |out|->score is updated only if a better match is found. */ -func (h *H6) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *h6) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { var num []uint16 = h.num var buckets []uint32 = h.buckets var cur_ix_masked uint = cur_ix & ring_buffer_mask @@ -158,15 +146,15 @@ func (h *H6) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 3 || (len == 2 && i < 2) { /* Comparing for >= 2 does not change the semantics, but just saves for a few unnecessary binary logarithms in backward reference score, since we are not interested in such short matches. */ - var score uint = BackwardReferenceScoreUsingLastDistance(uint(len)) + var score uint = backwardReferenceScoreUsingLastDistance(uint(len)) if best_score < score { if i != 0 { - score -= BackwardReferencePenaltyUsingLastDistance(i) + score -= backwardReferencePenaltyUsingLastDistance(i) } if best_score < score { best_score = score @@ -180,7 +168,7 @@ func (h *H6) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, } } { - var key uint32 = HashBytesH6(data[cur_ix_masked:], h.hash_mask_, h.hash_shift_) + var key uint32 = hashBytesH6(data[cur_ix_masked:], h.hash_mask_, h.hash_shift_) bucket = buckets[key< h.block_size_ { @@ -202,12 +190,12 @@ func (h *H6) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { /* Comparing for >= 3 does not change the semantics, but just saves for a few unnecessary binary logarithms in backward reference score, since we are not interested in such short matches. */ - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if best_score < score { best_score = score best_len = uint(len) @@ -224,6 +212,6 @@ func (h *H6) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, } if min_score == out.score { - SearchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) + searchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) } } diff --git a/hash.go b/hash.go index bd134da..f99f13f 100644 --- a/hash.go +++ b/hash.go @@ -18,31 +18,31 @@ import ( * HasherHandle by value. * * Typically hasher data consists of 3 sections: - * * HasherCommon structure + * * hasherCommon structure * * private structured hasher data, depending on hasher type * * private dynamic hasher data, depending on hasher type and parameters * */ -type HasherCommon struct { - params BrotliHasherParams +type hasherCommon struct { + params hasherParams is_prepared_ bool dict_num_lookups uint dict_num_matches uint } -func (h *HasherCommon) Common() *HasherCommon { +func (h *hasherCommon) Common() *hasherCommon { return h } -type HasherHandle interface { - Common() *HasherCommon - Initialize(params *BrotliEncoderParams) +type hasherHandle interface { + Common() *hasherCommon + Initialize(params *encoderParams) Prepare(one_shot bool, input_size uint, data []byte) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) HashTypeLength() uint StoreLookahead() uint PrepareDistanceCache(distance_cache []int) - FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) + FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) Store(data []byte, mask uint, ix uint) } @@ -55,7 +55,7 @@ var kCutoffTransformsCount uint32 = 10 /* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */ var kCutoffTransforms uint64 = 0x071B520ADA2D3200 -type HasherSearchResult struct { +type hasherSearchResult struct { len uint distance uint score uint @@ -74,7 +74,7 @@ var kHashMul64 uint64 = 0x1E35A7BD1E35A7BD var kHashMul64Long uint64 = 0x1FE35A7BD3579BD3 -func Hash14(data []byte) uint32 { +func hash14(data []byte) uint32 { var h uint32 = binary.LittleEndian.Uint32(data) * kHashMul32 /* The higher bits contain more mixture from the multiplication, @@ -82,7 +82,7 @@ func Hash14(data []byte) uint32 { return h >> (32 - 14) } -func PrepareDistanceCache(distance_cache []int, num_distances int) { +func prepareDistanceCache(distance_cache []int, num_distances int) { if num_distances > 4 { var last_distance int = distance_cache[0] distance_cache[4] = last_distance - 1 @@ -103,12 +103,12 @@ func PrepareDistanceCache(distance_cache []int, num_distances int) { } } -const BROTLI_LITERAL_BYTE_SCORE = 135 +const literalByteScore = 135 -const BROTLI_DISTANCE_BIT_PENALTY = 30 +const distanceBitPenalty = 30 /* Score must be positive after applying maximal penalty. */ -const BROTLI_SCORE_BASE = (BROTLI_DISTANCE_BIT_PENALTY * 8 * 8) +const scoreBase = (distanceBitPenalty * 8 * 8) /* Usually, we always choose the longest backward reference. This function allows for the exception of that rule. @@ -126,19 +126,19 @@ const BROTLI_SCORE_BASE = (BROTLI_DISTANCE_BIT_PENALTY * 8 * 8) than the saved literals. backward_reference_offset MUST be positive. */ -func BackwardReferenceScore(copy_length uint, backward_reference_offset uint) uint { - return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE*uint(copy_length) - BROTLI_DISTANCE_BIT_PENALTY*uint(Log2FloorNonZero(backward_reference_offset)) +func backwardReferenceScore(copy_length uint, backward_reference_offset uint) uint { + return scoreBase + literalByteScore*uint(copy_length) - distanceBitPenalty*uint(log2FloorNonZero(backward_reference_offset)) } -func BackwardReferenceScoreUsingLastDistance(copy_length uint) uint { - return BROTLI_LITERAL_BYTE_SCORE*uint(copy_length) + BROTLI_SCORE_BASE + 15 +func backwardReferenceScoreUsingLastDistance(copy_length uint) uint { + return literalByteScore*uint(copy_length) + scoreBase + 15 } -func BackwardReferencePenaltyUsingLastDistance(distance_short_code uint) uint { +func backwardReferencePenaltyUsingLastDistance(distance_short_code uint) uint { return uint(39) + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE) } -func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, data []byte, max_length uint, max_backward uint, max_distance uint, out *HasherSearchResult) bool { +func testStaticDictionaryItem(dictionary *encoderDictionary, item uint, data []byte, max_length uint, max_backward uint, max_distance uint, out *hasherSearchResult) bool { var len uint var word_idx uint var offset uint @@ -152,7 +152,7 @@ func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, da return false } - matchlen = FindMatchLengthWithLimit(data, dictionary.words.data[offset:], uint(len)) + matchlen = findMatchLengthWithLimit(data, dictionary.words.data[offset:], uint(len)) if matchlen+uint(dictionary.cutoffTransformsCount) <= len || matchlen == 0 { return false } @@ -166,7 +166,7 @@ func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, da return false } - score = BackwardReferenceScore(matchlen, backward) + score = backwardReferenceScore(matchlen, backward) if score < out.score { return false } @@ -178,15 +178,15 @@ func TestStaticDictionaryItem(dictionary *BrotliEncoderDictionary, item uint, da return true } -func SearchInStaticDictionary(dictionary *BrotliEncoderDictionary, handle HasherHandle, data []byte, max_length uint, max_backward uint, max_distance uint, out *HasherSearchResult, shallow bool) { +func searchInStaticDictionary(dictionary *encoderDictionary, handle hasherHandle, data []byte, max_length uint, max_backward uint, max_distance uint, out *hasherSearchResult, shallow bool) { var key uint var i uint - var self *HasherCommon = handle.Common() + var self *hasherCommon = handle.Common() if self.dict_num_matches < self.dict_num_lookups>>7 { return } - key = uint(Hash14(data) << 1) + key = uint(hash14(data) << 1) for i = 0; ; (func() { i++; key++ })() { var tmp uint if shallow { @@ -200,7 +200,7 @@ func SearchInStaticDictionary(dictionary *BrotliEncoderDictionary, handle Hasher var item uint = uint(dictionary.hash_table[key]) self.dict_num_lookups++ if item != 0 { - var item_matches bool = TestStaticDictionaryItem(dictionary, item, data, max_length, max_backward, max_distance, out) + var item_matches bool = testStaticDictionaryItem(dictionary, item, data, max_length, max_backward, max_distance, out) if item_matches { self.dict_num_matches++ } @@ -208,17 +208,17 @@ func SearchInStaticDictionary(dictionary *BrotliEncoderDictionary, handle Hasher } } -type BackwardMatch struct { +type backwardMatch struct { distance uint32 length_and_code uint32 } -func InitBackwardMatch(self *BackwardMatch, dist uint, len uint) { +func initBackwardMatch(self *backwardMatch, dist uint, len uint) { self.distance = uint32(dist) self.length_and_code = uint32(len << 5) } -func InitDictionaryBackwardMatch(self *BackwardMatch, dist uint, len uint, len_code uint) { +func initDictionaryBackwardMatch(self *backwardMatch, dist uint, len uint, len_code uint) { self.distance = uint32(dist) var tmp uint if len == len_code { @@ -229,34 +229,27 @@ func InitDictionaryBackwardMatch(self *BackwardMatch, dist uint, len uint, len_c self.length_and_code = uint32(len<<5 | tmp) } -func BackwardMatchLength(self *BackwardMatch) uint { +func backwardMatchLength(self *backwardMatch) uint { return uint(self.length_and_code >> 5) } -func BackwardMatchLengthCode(self *BackwardMatch) uint { +func backwardMatchLengthCode(self *backwardMatch) uint { var code uint = uint(self.length_and_code) & 31 if code != 0 { return code } else { - return BackwardMatchLength(self) + return backwardMatchLength(self) } } -func DestroyHasher(handle *HasherHandle) { - if *handle == nil { - return - } - *handle = nil -} - -func HasherReset(handle HasherHandle) { +func hasherReset(handle hasherHandle) { if handle == nil { return } handle.Common().is_prepared_ = false } -func newHasher(typ int) HasherHandle { +func newHasher(typ int) hasherHandle { switch typ { case 2: return &hashLongestMatchQuickly{ @@ -280,11 +273,11 @@ func newHasher(typ int) HasherHandle { useDictionary: true, } case 5: - return new(H5) + return new(h5) case 6: - return new(H6) + return new(h6) case 10: - return new(H10) + return new(h10) case 35: return &hashComposite{ ha: newHasher(3), @@ -333,9 +326,9 @@ func newHasher(typ int) HasherHandle { panic(fmt.Sprintf("unknown hasher type: %d", typ)) } -func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, position uint, input_size uint, is_last bool) { - var self HasherHandle = nil - var common *HasherCommon = nil +func hasherSetup(handle *hasherHandle, params *encoderParams, data []byte, position uint, input_size uint, is_last bool) { + var self hasherHandle = nil + var common *hasherCommon = nil var one_shot bool = (position == 0 && is_last) if *handle == nil { ChooseHasher(params, ¶ms.hasher) @@ -361,9 +354,9 @@ func HasherSetup(handle *HasherHandle, params *BrotliEncoderParams, data []byte, } } -func InitOrStitchToPreviousBlock(handle *HasherHandle, data []byte, mask uint, params *BrotliEncoderParams, position uint, input_size uint, is_last bool) { - var self HasherHandle - HasherSetup(handle, params, data, position, input_size, is_last) +func initOrStitchToPreviousBlock(handle *hasherHandle, data []byte, mask uint, params *encoderParams, position uint, input_size uint, is_last bool) { + var self hasherHandle + hasherSetup(handle, params, data, position, input_size, is_last) self = *handle self.StitchToPreviousBlock(input_size, position, data, mask) } diff --git a/hash_composite.go b/hash_composite.go index 2e55969..7abe926 100644 --- a/hash_composite.go +++ b/hash_composite.go @@ -32,13 +32,13 @@ func (h *hashComposite) StoreLookahead() uint { } type hashComposite struct { - HasherCommon - ha HasherHandle - hb HasherHandle - params *BrotliEncoderParams + hasherCommon + ha hasherHandle + hb hasherHandle + params *encoderParams } -func (h *hashComposite) Initialize(params *BrotliEncoderParams) { +func (h *hashComposite) Initialize(params *encoderParams) { h.params = params } @@ -48,8 +48,8 @@ func (h *hashComposite) Initialize(params *BrotliEncoderParams) { those params to all hashers InitializehashComposite */ func (h *hashComposite) Prepare(one_shot bool, input_size uint, data []byte) { if h.ha == nil { - var common_a *HasherCommon - var common_b *HasherCommon + var common_a *hasherCommon + var common_b *hasherCommon common_a = h.ha.Common() common_a.params = h.params.hasher @@ -90,7 +90,7 @@ func (h *hashComposite) PrepareDistanceCache(distance_cache []int) { h.hb.PrepareDistanceCache(distance_cache) } -func (h *hashComposite) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *hashComposite) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { h.ha.FindLongestMatch(dictionary, data, ring_buffer_mask, distance_cache, cur_ix, max_length, max_backward, gap, max_distance, out) h.hb.FindLongestMatch(dictionary, data, ring_buffer_mask, distance_cache, cur_ix, max_length, max_backward, gap, max_distance, out) } diff --git a/hash_forgetful_chain.go b/hash_forgetful_chain.go index 882fcd2..456c728 100644 --- a/hash_forgetful_chain.go +++ b/hash_forgetful_chain.go @@ -38,7 +38,7 @@ type slot struct { } type hashForgetfulChain struct { - HasherCommon + hasherCommon bucketBits uint numBanks uint @@ -53,7 +53,7 @@ type hashForgetfulChain struct { max_hops uint } -func (h *hashForgetfulChain) Initialize(params *BrotliEncoderParams) { +func (h *hashForgetfulChain) Initialize(params *encoderParams) { var q uint if params.quality > 6 { q = 7 @@ -144,7 +144,7 @@ func (h *hashForgetfulChain) StitchToPreviousBlock(num_bytes uint, position uint } func (h *hashForgetfulChain) PrepareDistanceCache(distance_cache []int) { - PrepareDistanceCache(distance_cache, h.numLastDistancesToCheck) + prepareDistanceCache(distance_cache, h.numLastDistancesToCheck) } /* Find a longest backward match of &data[cur_ix] up to the length of @@ -158,7 +158,7 @@ func (h *hashForgetfulChain) PrepareDistanceCache(distance_cache []int) { Does not look for matches further away than max_backward. Writes the best match into |out|. |out|->score is updated only if a better match is found. */ -func (h *hashForgetfulChain) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *hashForgetfulChain) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { var cur_ix_masked uint = cur_ix & ring_buffer_mask var min_score uint = out.score var best_score uint = out.score @@ -185,12 +185,12 @@ func (h *hashForgetfulChain) FindLongestMatch(dictionary *BrotliEncoderDictionar prev_ix &= ring_buffer_mask { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 2 { - var score uint = BackwardReferenceScoreUsingLastDistance(uint(len)) + var score uint = backwardReferenceScoreUsingLastDistance(uint(len)) if best_score < score { if i != 0 { - score -= BackwardReferencePenaltyUsingLastDistance(uint(i)) + score -= backwardReferencePenaltyUsingLastDistance(uint(i)) } if best_score < score { best_score = score @@ -228,12 +228,12 @@ func (h *hashForgetfulChain) FindLongestMatch(dictionary *BrotliEncoderDictionar continue } { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { /* Comparing for >= 3 does not change the semantics, but just saves for a few unnecessary binary logarithms in backward reference score, since we are not interested in such short matches. */ - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if best_score < score { best_score = score best_len = uint(len) @@ -249,6 +249,6 @@ func (h *hashForgetfulChain) FindLongestMatch(dictionary *BrotliEncoderDictionar } if out.score == min_score { - SearchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) + searchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false) } } diff --git a/hash_longest_match_quickly.go b/hash_longest_match_quickly.go index d66c61e..025f5e3 100644 --- a/hash_longest_match_quickly.go +++ b/hash_longest_match_quickly.go @@ -37,7 +37,7 @@ func (h *hashLongestMatchQuickly) HashBytes(data []byte) uint32 { This is a hash map of fixed size (1 << 16). Starting from the given index, 1 buckets are used to store values of a key. */ type hashLongestMatchQuickly struct { - HasherCommon + hasherCommon bucketBits uint bucketSweep int @@ -47,7 +47,7 @@ type hashLongestMatchQuickly struct { buckets []uint32 } -func (h *hashLongestMatchQuickly) Initialize(params *BrotliEncoderParams) { +func (h *hashLongestMatchQuickly) Initialize(params *encoderParams) { h.buckets = make([]uint32, 1<score is updated only if a better match is found. */ -func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { var best_len_in uint = out.len var cur_ix_masked uint = cur_ix & ring_buffer_mask var key uint32 = h.HashBytes(data[cur_ix_masked:]) @@ -127,9 +127,9 @@ func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *BrotliEncoderDict if prev_ix < cur_ix { prev_ix &= uint(uint32(ring_buffer_mask)) if compare_char == int(data[prev_ix+best_len]) { - var len uint = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { - var score uint = BackwardReferenceScoreUsingLastDistance(uint(len)) + var score uint = backwardReferenceScoreUsingLastDistance(uint(len)) if best_score < score { best_score = score best_len = uint(len) @@ -164,9 +164,9 @@ func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *BrotliEncoderDict return } - len = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + len = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if best_score < score { out.len = uint(len) out.distance = backward @@ -191,9 +191,9 @@ func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *BrotliEncoderDict continue } - len = FindMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) + len = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length) if len >= 4 { - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if best_score < score { best_score = score best_len = uint(len) @@ -207,7 +207,7 @@ func (h *hashLongestMatchQuickly) FindLongestMatch(dictionary *BrotliEncoderDict } if h.useDictionary && min_score == out.score { - SearchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, true) + searchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, true) } h.buckets[key+uint32((cur_ix>>3)%uint(h.bucketSweep))] = uint32(cur_ix) diff --git a/hash_rolling.go b/hash_rolling.go index 712a7e9..692ce11 100644 --- a/hash_rolling.go +++ b/hash_rolling.go @@ -41,7 +41,7 @@ func (h *hashRolling) HashRollingFunction(state uint32, add byte, rem byte, fact } type hashRolling struct { - HasherCommon + hasherCommon jump int @@ -53,7 +53,7 @@ type hashRolling struct { factor_remove uint32 } -func (h *hashRolling) Initialize(params *BrotliEncoderParams) { +func (h *hashRolling) Initialize(params *encoderParams) { h.state = 0 h.next_ix = 0 @@ -120,7 +120,7 @@ func (h *hashRolling) StitchToPreviousBlock(num_bytes uint, position uint, ringb func (*hashRolling) PrepareDistanceCache(distance_cache []int) { } -func (h *hashRolling) FindLongestMatch(dictionary *BrotliEncoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *HasherSearchResult) { +func (h *hashRolling) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) { var cur_ix_masked uint = cur_ix & ring_buffer_mask var pos uint = h.next_ix @@ -150,9 +150,9 @@ func (h *hashRolling) FindLongestMatch(dictionary *BrotliEncoderDictionary, data var backward uint = uint(uint32(cur_ix - found_ix)) if backward <= max_backward { var found_ix_masked uint = found_ix & ring_buffer_mask - var len uint = FindMatchLengthWithLimit(data[found_ix_masked:], data[cur_ix_masked:], max_length) + var len uint = findMatchLengthWithLimit(data[found_ix_masked:], data[cur_ix_masked:], max_length) if len >= 4 && len > out.len { - var score uint = BackwardReferenceScore(uint(len), backward) + var score uint = backwardReferenceScore(uint(len), backward) if score > out.score { out.len = uint(len) out.distance = backward diff --git a/histogram.go b/histogram.go index f1fad8c..f208ff7 100644 --- a/histogram.go +++ b/histogram.go @@ -3,33 +3,33 @@ package brotli import "math" /* The distance symbols effectively used by "Large Window Brotli" (32-bit). */ -const BROTLI_NUM_HISTOGRAM_DISTANCE_SYMBOLS = 544 +const numHistogramDistanceSymbols = 544 -type HistogramLiteral struct { +type histogramLiteral struct { data_ [numLiteralSymbols]uint32 total_count_ uint bit_cost_ float64 } -func HistogramClearLiteral(self *HistogramLiteral) { +func histogramClearLiteral(self *histogramLiteral) { self.data_ = [numLiteralSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } -func ClearHistogramsLiteral(array []HistogramLiteral, length uint) { +func clearHistogramsLiteral(array []histogramLiteral, length uint) { var i uint for i = 0; i < length; i++ { - HistogramClearLiteral(&array[i:][0]) + histogramClearLiteral(&array[i:][0]) } } -func HistogramAddLiteral(self *HistogramLiteral, val uint) { +func histogramAddLiteral(self *histogramLiteral, val uint) { self.data_[val]++ self.total_count_++ } -func HistogramAddVectorLiteral(self *HistogramLiteral, p []byte, n uint) { +func histogramAddVectorLiteral(self *histogramLiteral, p []byte, n uint) { self.total_count_ += n n += 1 for { @@ -42,7 +42,7 @@ func HistogramAddVectorLiteral(self *HistogramLiteral, p []byte, n uint) { } } -func HistogramAddHistogramLiteral(self *HistogramLiteral, v *HistogramLiteral) { +func histogramAddHistogramLiteral(self *histogramLiteral, v *histogramLiteral) { var i uint self.total_count_ += v.total_count_ for i = 0; i < numLiteralSymbols; i++ { @@ -50,35 +50,35 @@ func HistogramAddHistogramLiteral(self *HistogramLiteral, v *HistogramLiteral) { } } -func HistogramDataSizeLiteral() uint { +func histogramDataSizeLiteral() uint { return numLiteralSymbols } -type HistogramCommand struct { +type histogramCommand struct { data_ [numCommandSymbols]uint32 total_count_ uint bit_cost_ float64 } -func HistogramClearCommand(self *HistogramCommand) { +func histogramClearCommand(self *histogramCommand) { self.data_ = [numCommandSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } -func ClearHistogramsCommand(array []HistogramCommand, length uint) { +func clearHistogramsCommand(array []histogramCommand, length uint) { var i uint for i = 0; i < length; i++ { - HistogramClearCommand(&array[i:][0]) + histogramClearCommand(&array[i:][0]) } } -func HistogramAddCommand(self *HistogramCommand, val uint) { +func histogramAddCommand(self *histogramCommand, val uint) { self.data_[val]++ self.total_count_++ } -func HistogramAddVectorCommand(self *HistogramCommand, p []uint16, n uint) { +func histogramAddVectorCommand(self *histogramCommand, p []uint16, n uint) { self.total_count_ += n n += 1 for { @@ -91,7 +91,7 @@ func HistogramAddVectorCommand(self *HistogramCommand, p []uint16, n uint) { } } -func HistogramAddHistogramCommand(self *HistogramCommand, v *HistogramCommand) { +func histogramAddHistogramCommand(self *histogramCommand, v *histogramCommand) { var i uint self.total_count_ += v.total_count_ for i = 0; i < numCommandSymbols; i++ { @@ -99,35 +99,35 @@ func HistogramAddHistogramCommand(self *HistogramCommand, v *HistogramCommand) { } } -func HistogramDataSizeCommand() uint { +func histogramDataSizeCommand() uint { return numCommandSymbols } -type HistogramDistance struct { +type histogramDistance struct { data_ [numDistanceSymbols]uint32 total_count_ uint bit_cost_ float64 } -func HistogramClearDistance(self *HistogramDistance) { +func histogramClearDistance(self *histogramDistance) { self.data_ = [numDistanceSymbols]uint32{} self.total_count_ = 0 self.bit_cost_ = math.MaxFloat64 } -func ClearHistogramsDistance(array []HistogramDistance, length uint) { +func clearHistogramsDistance(array []histogramDistance, length uint) { var i uint for i = 0; i < length; i++ { - HistogramClearDistance(&array[i:][0]) + histogramClearDistance(&array[i:][0]) } } -func HistogramAddDistance(self *HistogramDistance, val uint) { +func histogramAddDistance(self *histogramDistance, val uint) { self.data_[val]++ self.total_count_++ } -func HistogramAddVectorDistance(self *HistogramDistance, p []uint16, n uint) { +func histogramAddVectorDistance(self *histogramDistance, p []uint16, n uint) { self.total_count_ += n n += 1 for { @@ -140,7 +140,7 @@ func HistogramAddVectorDistance(self *HistogramDistance, p []uint16, n uint) { } } -func HistogramAddHistogramDistance(self *HistogramDistance, v *HistogramDistance) { +func histogramAddHistogramDistance(self *histogramDistance, v *histogramDistance) { var i uint self.total_count_ += v.total_count_ for i = 0; i < numDistanceSymbols; i++ { @@ -148,18 +148,18 @@ func HistogramAddHistogramDistance(self *HistogramDistance, v *HistogramDistance } } -func HistogramDataSizeDistance() uint { +func histogramDataSizeDistance() uint { return numDistanceSymbols } -type BlockSplitIterator struct { +type blockSplitIterator struct { split_ *blockSplit idx_ uint type_ uint length_ uint } -func InitBlockSplitIterator(self *BlockSplitIterator, split *blockSplit) { +func initBlockSplitIterator(self *blockSplitIterator, split *blockSplit) { self.split_ = split self.idx_ = 0 self.type_ = 0 @@ -170,7 +170,7 @@ func InitBlockSplitIterator(self *BlockSplitIterator, split *blockSplit) { } } -func BlockSplitIteratorNext(self *BlockSplitIterator) { +func blockSplitIteratorNext(self *blockSplitIterator) { if self.length_ == 0 { self.idx_++ self.type_ = uint(self.split_.types[self.idx_]) @@ -180,33 +180,33 @@ func BlockSplitIteratorNext(self *BlockSplitIterator) { self.length_-- } -func BrotliBuildHistogramsWithContext(cmds []command, num_commands uint, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit, ringbuffer []byte, start_pos uint, mask uint, prev_byte byte, prev_byte2 byte, context_modes []int, literal_histograms []HistogramLiteral, insert_and_copy_histograms []HistogramCommand, copy_dist_histograms []HistogramDistance) { +func buildHistogramsWithContext(cmds []command, num_commands uint, literal_split *blockSplit, insert_and_copy_split *blockSplit, dist_split *blockSplit, ringbuffer []byte, start_pos uint, mask uint, prev_byte byte, prev_byte2 byte, context_modes []int, literal_histograms []histogramLiteral, insert_and_copy_histograms []histogramCommand, copy_dist_histograms []histogramDistance) { var pos uint = start_pos - var literal_it BlockSplitIterator - var insert_and_copy_it BlockSplitIterator - var dist_it BlockSplitIterator + var literal_it blockSplitIterator + var insert_and_copy_it blockSplitIterator + var dist_it blockSplitIterator var i uint - InitBlockSplitIterator(&literal_it, literal_split) - InitBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split) - InitBlockSplitIterator(&dist_it, dist_split) + initBlockSplitIterator(&literal_it, literal_split) + initBlockSplitIterator(&insert_and_copy_it, insert_and_copy_split) + initBlockSplitIterator(&dist_it, dist_split) for i = 0; i < num_commands; i++ { var cmd *command = &cmds[i] var j uint - BlockSplitIteratorNext(&insert_and_copy_it) - HistogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], uint(cmd.cmd_prefix_)) + blockSplitIteratorNext(&insert_and_copy_it) + histogramAddCommand(&insert_and_copy_histograms[insert_and_copy_it.type_], uint(cmd.cmd_prefix_)) /* TODO: unwrap iterator blocks. */ for j = uint(cmd.insert_len_); j != 0; j-- { var context uint - BlockSplitIteratorNext(&literal_it) + blockSplitIteratorNext(&literal_it) context = literal_it.type_ if context_modes != nil { - var lut ContextLut = BROTLI_CONTEXT_LUT(context_modes[context]) - context = (context << BROTLI_LITERAL_CONTEXT_BITS) + uint(BROTLI_CONTEXT(prev_byte, prev_byte2, lut)) + var lut contextLUT = getContextLUT(context_modes[context]) + context = (context << literalContextBits) + uint(getContext(prev_byte, prev_byte2, lut)) } - HistogramAddLiteral(&literal_histograms[context], uint(ringbuffer[pos&mask])) + histogramAddLiteral(&literal_histograms[context], uint(ringbuffer[pos&mask])) prev_byte2 = prev_byte prev_byte = ringbuffer[pos&mask] pos++ @@ -218,9 +218,9 @@ func BrotliBuildHistogramsWithContext(cmds []command, num_commands uint, literal prev_byte = ringbuffer[(pos-1)&mask] if cmd.cmd_prefix_ >= 128 { var context uint - BlockSplitIteratorNext(&dist_it) - context = uint(uint32(dist_it.type_<> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), where reverse(value, len) is the bit-wise reversal of the len least significant bits of value. */ -func BrotliReverseBits8(num uint64) uint64 { +func reverseBits8(num uint64) uint64 { return uint64(kReverseBits[num]) } /* Stores code in table[0], table[step], table[2*step], ..., table[end] */ /* Assumes that end is an integer multiple of step */ -func ReplicateValue(table []HuffmanCode, step int, end int, code HuffmanCode) { +func replicateValue(table []huffmanCode, step int, end int, code huffmanCode) { for { end -= step table[end] = code @@ -384,9 +384,9 @@ func ReplicateValue(table []HuffmanCode, step int, end int, code HuffmanCode) { /* Returns the table width of the next 2nd level table. |count| is the histogram of bit lengths for the remaining symbols, |len| is the code length of the next processed symbol. */ -func NextTableBitSize(count []uint16, len int, root_bits int) int { +func nextTableBitSize(count []uint16, len int, root_bits int) int { var left int = 1 << uint(len-root_bits) - for len < BROTLI_HUFFMAN_MAX_CODE_LENGTH { + for len < huffmanMaxCodeLength { left -= int(count[len]) if left <= 0 { break @@ -398,26 +398,26 @@ func NextTableBitSize(count []uint16, len int, root_bits int) int { return len - root_bits } -func BrotliBuildCodeLengthsHuffmanTable(table []HuffmanCode, code_lengths []byte, count []uint16) { - var code HuffmanCode /* current table entry */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* step size to replicate values in current table */ /* size of current table */ /* symbols sorted by code length */ +func buildCodeLengthsHuffmanTable(table []huffmanCode, code_lengths []byte, count []uint16) { + var code huffmanCode /* current table entry */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* step size to replicate values in current table */ /* size of current table */ /* symbols sorted by code length */ var symbol int var key uint64 var key_step uint64 var step int var table_size int var sorted [codeLengthCodes]int - var offset [BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]int + var offset [huffmanMaxCodeLengthCodeLength + 1]int var bits int var bits_count int /* offsets in sorted table for each length */ - assert(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX) + assert(huffmanMaxCodeLengthCodeLength <= reverseBitsMax) /* Generate offsets into sorted symbol table by code length. */ symbol = -1 bits = 1 var i int - for i = 0; i < BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH; i++ { + for i = 0; i < huffmanMaxCodeLengthCodeLength; i++ { symbol += int(count[bits]) offset[bits] = symbol bits++ @@ -441,11 +441,11 @@ func BrotliBuildCodeLengthsHuffmanTable(table []HuffmanCode, code_lengths []byte } } - table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + table_size = 1 << huffmanMaxCodeLengthCodeLength /* Special case: all symbols but one have 0 code length. */ if offset[0] == 0 { - code = ConstructHuffmanCode(0, uint16(sorted[0])) + code = constructHuffmanCode(0, uint16(sorted[0])) for key = 0; key < uint64(table_size); key++ { table[key] = code } @@ -456,30 +456,30 @@ func BrotliBuildCodeLengthsHuffmanTable(table []HuffmanCode, code_lengths []byte /* Fill in table. */ key = 0 - key_step = BROTLI_REVERSE_BITS_LOWEST + key_step = reverseBitsLowest symbol = 0 bits = 1 step = 2 for { for bits_count = int(count[bits]); bits_count != 0; bits_count-- { - code = ConstructHuffmanCode(byte(bits), uint16(sorted[symbol])) + code = constructHuffmanCode(byte(bits), uint16(sorted[symbol])) symbol++ - ReplicateValue(table[BrotliReverseBits8(key):], step, table_size, code) + replicateValue(table[reverseBits8(key):], step, table_size, code) key += key_step } step <<= 1 key_step >>= 1 bits++ - if bits > BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH { + if bits > huffmanMaxCodeLengthCodeLength { break } } } -func BrotliBuildHuffmanTable(root_table []HuffmanCode, root_bits int, symbol_lists SymbolList, count []uint16) uint32 { - var code HuffmanCode /* current table entry */ /* next available space in table */ /* current code length */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* 2nd level table prefix code */ /* 2nd level table prefix code addend */ /* step size to replicate values in current table */ /* key length of current table */ /* size of current table */ /* sum of root table size and 2nd level table sizes */ - var table []HuffmanCode +func buildHuffmanTable(root_table []huffmanCode, root_bits int, symbol_lists SymbolList, count []uint16) uint32 { + var code huffmanCode /* current table entry */ /* next available space in table */ /* current code length */ /* symbol index in original or sorted table */ /* prefix code */ /* prefix code addend */ /* 2nd level table prefix code */ /* 2nd level table prefix code addend */ /* step size to replicate values in current table */ /* key length of current table */ /* size of current table */ /* sum of root table size and 2nd level table sizes */ + var table []huffmanCode var len int var symbol int var key uint64 @@ -494,13 +494,13 @@ func BrotliBuildHuffmanTable(root_table []HuffmanCode, root_bits int, symbol_lis var bits int var bits_count int - assert(root_bits <= BROTLI_REVERSE_BITS_MAX) - assert(BROTLI_HUFFMAN_MAX_CODE_LENGTH-root_bits <= BROTLI_REVERSE_BITS_MAX) + assert(root_bits <= reverseBitsMax) + assert(huffmanMaxCodeLength-root_bits <= reverseBitsMax) for SymbolListGet(symbol_lists, max_length) == 0xFFFF { max_length-- } - max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + max_length += huffmanMaxCodeLength + 1 table = root_table table_bits = root_bits @@ -515,15 +515,15 @@ func BrotliBuildHuffmanTable(root_table []HuffmanCode, root_bits int, symbol_lis } key = 0 - key_step = BROTLI_REVERSE_BITS_LOWEST + key_step = reverseBitsLowest bits = 1 step = 2 for { - symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1) + symbol = bits - (huffmanMaxCodeLength + 1) for bits_count = int(count[bits]); bits_count != 0; bits_count-- { symbol = int(SymbolListGet(symbol_lists, symbol)) - code = ConstructHuffmanCode(byte(bits), uint16(symbol)) - ReplicateValue(table[BrotliReverseBits8(key):], step, table_size, code) + code = constructHuffmanCode(byte(bits), uint16(symbol)) + replicateValue(table[reverseBits8(key):], step, table_size, code) key += key_step } @@ -542,29 +542,29 @@ func BrotliBuildHuffmanTable(root_table []HuffmanCode, root_bits int, symbol_lis } /* Fill in 2nd level tables and add pointers to root table. */ - key_step = BROTLI_REVERSE_BITS_LOWEST >> uint(root_bits-1) + key_step = reverseBitsLowest >> uint(root_bits-1) - sub_key = BROTLI_REVERSE_BITS_LOWEST << 1 - sub_key_step = BROTLI_REVERSE_BITS_LOWEST + sub_key = reverseBitsLowest << 1 + sub_key_step = reverseBitsLowest len = root_bits + 1 step = 2 for ; len <= max_length; len++ { - symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1) + symbol = len - (huffmanMaxCodeLength + 1) for ; count[len] != 0; count[len]-- { - if sub_key == BROTLI_REVERSE_BITS_LOWEST<<1 { + if sub_key == reverseBitsLowest<<1 { table = table[table_size:] - table_bits = NextTableBitSize(count, int(len), root_bits) + table_bits = nextTableBitSize(count, int(len), root_bits) table_size = 1 << uint(table_bits) total_size += table_size - sub_key = BrotliReverseBits8(key) + sub_key = reverseBits8(key) key += key_step - root_table[sub_key] = ConstructHuffmanCode(byte(table_bits+root_bits), uint16(uint64(uint(-cap(table)+cap(root_table)))-sub_key)) + root_table[sub_key] = constructHuffmanCode(byte(table_bits+root_bits), uint16(uint64(uint(-cap(table)+cap(root_table)))-sub_key)) sub_key = 0 } symbol = int(SymbolListGet(symbol_lists, symbol)) - code = ConstructHuffmanCode(byte(len-root_bits), uint16(symbol)) - ReplicateValue(table[BrotliReverseBits8(sub_key):], step, table_size, code) + code = constructHuffmanCode(byte(len-root_bits), uint16(symbol)) + replicateValue(table[reverseBits8(sub_key):], step, table_size, code) sub_key += sub_key_step } @@ -575,33 +575,33 @@ func BrotliBuildHuffmanTable(root_table []HuffmanCode, root_bits int, symbol_lis return uint32(total_size) } -func BrotliBuildSimpleHuffmanTable(table []HuffmanCode, root_bits int, val []uint16, num_symbols uint32) uint32 { +func buildSimpleHuffmanTable(table []huffmanCode, root_bits int, val []uint16, num_symbols uint32) uint32 { var table_size uint32 = 1 var goal_size uint32 = 1 << uint(root_bits) switch num_symbols { case 0: - table[0] = ConstructHuffmanCode(0, val[0]) + table[0] = constructHuffmanCode(0, val[0]) case 1: if val[1] > val[0] { - table[0] = ConstructHuffmanCode(1, val[0]) - table[1] = ConstructHuffmanCode(1, val[1]) + table[0] = constructHuffmanCode(1, val[0]) + table[1] = constructHuffmanCode(1, val[1]) } else { - table[0] = ConstructHuffmanCode(1, val[1]) - table[1] = ConstructHuffmanCode(1, val[0]) + table[0] = constructHuffmanCode(1, val[1]) + table[1] = constructHuffmanCode(1, val[0]) } table_size = 2 case 2: - table[0] = ConstructHuffmanCode(1, val[0]) - table[2] = ConstructHuffmanCode(1, val[0]) + table[0] = constructHuffmanCode(1, val[0]) + table[2] = constructHuffmanCode(1, val[0]) if val[2] > val[1] { - table[1] = ConstructHuffmanCode(2, val[1]) - table[3] = ConstructHuffmanCode(2, val[2]) + table[1] = constructHuffmanCode(2, val[1]) + table[3] = constructHuffmanCode(2, val[2]) } else { - table[1] = ConstructHuffmanCode(2, val[2]) - table[3] = ConstructHuffmanCode(2, val[1]) + table[1] = constructHuffmanCode(2, val[2]) + table[3] = constructHuffmanCode(2, val[1]) } table_size = 4 @@ -619,10 +619,10 @@ func BrotliBuildSimpleHuffmanTable(table []HuffmanCode, root_bits int, val []uin } } - 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[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 } @@ -635,14 +635,14 @@ func BrotliBuildSimpleHuffmanTable(table []HuffmanCode, root_bits int, val []uin 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[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 } diff --git a/literal_cost.go b/literal_cost.go index f117735..6a2508c 100644 --- a/literal_cost.go +++ b/literal_cost.go @@ -1,6 +1,6 @@ package brotli -func UTF8Position(last uint, c uint, clamp uint) uint { +func utf8Position(last uint, c uint, clamp uint) uint { if c < 128 { return 0 /* Next one is the 'Byte 1' again. */ } else if c >= 192 { /* Next one is the 'Byte 2' of utf-8 encoding. */ @@ -15,14 +15,14 @@ func UTF8Position(last uint, c uint, clamp uint) uint { } } -func DecideMultiByteStatsLevel(pos uint, len uint, mask uint, data []byte) uint { +func decideMultiByteStatsLevel(pos uint, len uint, mask uint, data []byte) uint { var counts = [3]uint{0} /* should be 2, but 1 compresses better. */ var max_utf8 uint = 1 var last_c uint = 0 var i uint for i = 0; i < len; i++ { var c uint = uint(data[(pos+i)&mask]) - counts[UTF8Position(last_c, c, 2)]++ + counts[utf8Position(last_c, c, 2)]++ last_c = c } @@ -37,8 +37,8 @@ func DecideMultiByteStatsLevel(pos uint, len uint, mask uint, data []byte) uint return max_utf8 } -func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, cost []float32) { - var max_utf8 uint = DecideMultiByteStatsLevel(pos, uint(len), mask, data) +func estimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, cost []float32) { + var max_utf8 uint = decideMultiByteStatsLevel(pos, uint(len), mask, data) /* Bootstrap histograms. */ var histogram = [3][256]uint{[256]uint{0}} var window_half uint = 495 @@ -55,7 +55,7 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, var c uint = uint(data[(pos+i)&mask]) histogram[utf8_pos][c]++ in_window_utf8[utf8_pos]++ - utf8_pos = UTF8Position(last_c, c, max_utf8) + utf8_pos = utf8Position(last_c, c, max_utf8) last_c = c } } @@ -77,7 +77,7 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, } /* Remove a byte in the past. */ - var utf8_pos2 uint = UTF8Position(last_c, c, max_utf8) + var utf8_pos2 uint = utf8Position(last_c, c, max_utf8) histogram[utf8_pos2][data[(pos+i-window_half)&mask]]-- in_window_utf8[utf8_pos2]-- } @@ -87,7 +87,7 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, var last_c uint = uint(data[(pos+i+window_half-2)&mask]) /* Add a byte in the future. */ - var utf8_pos2 uint = UTF8Position(last_c, c, max_utf8) + var utf8_pos2 uint = utf8Position(last_c, c, max_utf8) histogram[utf8_pos2][data[(pos+i+window_half)&mask]]++ in_window_utf8[utf8_pos2]++ } @@ -104,7 +104,7 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, } else { last_c = uint(data[(pos+i-2)&mask]) } - var utf8_pos uint = UTF8Position(last_c, c, max_utf8) + var utf8_pos uint = utf8Position(last_c, c, max_utf8) var masked_pos uint = (pos + i) & mask var histo uint = histogram[utf8_pos][data[masked_pos]] var lit_cost float64 @@ -112,7 +112,7 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, histo = 1 } - lit_cost = FastLog2(in_window_utf8[utf8_pos]) - FastLog2(histo) + lit_cost = fastLog2(in_window_utf8[utf8_pos]) - fastLog2(histo) lit_cost += 0.02905 if lit_cost < 1.0 { lit_cost *= 0.5 @@ -132,9 +132,9 @@ func EstimateBitCostsForLiteralsUTF8(pos uint, len uint, mask uint, data []byte, } } -func BrotliEstimateBitCostsForLiterals(pos uint, len uint, mask uint, data []byte, cost []float32) { +func estimateBitCostsForLiterals(pos uint, len uint, mask uint, data []byte, cost []float32) { if BrotliIsMostlyUTF8(data, pos, mask, uint(len), kMinUTF8Ratio) { - EstimateBitCostsForLiteralsUTF8(pos, uint(len), mask, data, cost) + estimateBitCostsForLiteralsUTF8(pos, uint(len), mask, data, cost) return } else { var histogram = [256]uint{0} @@ -168,7 +168,7 @@ func BrotliEstimateBitCostsForLiterals(pos uint, len uint, mask uint, data []byt histo = 1 } { - var lit_cost float64 = FastLog2(in_window) - FastLog2(histo) + var lit_cost float64 = fastLog2(in_window) - fastLog2(histo) lit_cost += 0.029 if lit_cost < 1.0 { lit_cost *= 0.5 diff --git a/metablock.go b/metablock.go index 66a6247..cd573cb 100644 --- a/metablock.go +++ b/metablock.go @@ -31,7 +31,7 @@ package brotli /* Algorithms for distributing the literals and commands of a metablock between block types and contexts. */ -type MetaBlockSplit struct { +type metaBlockSplit struct { literal_split blockSplit command_split blockSplit distance_split blockSplit @@ -39,15 +39,15 @@ type MetaBlockSplit struct { literal_context_map_size uint distance_context_map []uint32 distance_context_map_size uint - literal_histograms []HistogramLiteral + literal_histograms []histogramLiteral literal_histograms_size uint - command_histograms []HistogramCommand + command_histograms []histogramCommand command_histograms_size uint - distance_histograms []HistogramDistance + distance_histograms []histogramDistance distance_histograms_size uint } -func InitMetaBlockSplit(mb *MetaBlockSplit) { +func initMetaBlockSplit(mb *metaBlockSplit) { initBlockSplit(&mb.literal_split) initBlockSplit(&mb.command_split) initBlockSplit(&mb.distance_split) @@ -63,7 +63,7 @@ func InitMetaBlockSplit(mb *MetaBlockSplit) { mb.distance_histograms_size = 0 } -func DestroyMetaBlockSplit(mb *MetaBlockSplit) { +func destroyMetaBlockSplit(mb *metaBlockSplit) { destroyBlockSplit(&mb.literal_split) destroyBlockSplit(&mb.command_split) destroyBlockSplit(&mb.distance_split) @@ -82,8 +82,8 @@ func DestroyMetaBlockSplit(mb *MetaBlockSplit) { /* Algorithms for distributing the literals and commands of a metablock between block types and contexts. */ -func BrotliInitDistanceParams(params *BrotliEncoderParams, npostfix uint32, ndirect uint32) { - var dist_params *BrotliDistanceParams = ¶ms.dist +func initDistanceParams(params *encoderParams, npostfix uint32, ndirect uint32) { + var dist_params *distanceParams = ¶ms.dist var alphabet_size uint32 var max_distance uint32 @@ -102,11 +102,11 @@ func BrotliInitDistanceParams(params *BrotliEncoderParams, npostfix uint32, ndir a distance larger than BROTLI_MAX_ALLOWED_DISTANCE with all its extra bits set. */ if ndirect < bound[npostfix] { - max_distance = BROTLI_MAX_ALLOWED_DISTANCE - (bound[npostfix] - ndirect) + max_distance = maxAllowedDistance - (bound[npostfix] - ndirect) } else if ndirect >= bound[npostfix]+postfix { max_distance = (3 << 29) - 4 + (ndirect - bound[npostfix]) } else { - max_distance = BROTLI_MAX_ALLOWED_DISTANCE + max_distance = maxAllowedDistance } } @@ -114,7 +114,7 @@ func BrotliInitDistanceParams(params *BrotliEncoderParams, npostfix uint32, ndir dist_params.max_distance = uint(max_distance) } -func RecomputeDistancePrefixes(cmds []command, num_commands uint, orig_params *BrotliDistanceParams, new_params *BrotliDistanceParams) { +func recomputeDistancePrefixes(cmds []command, num_commands uint, orig_params *distanceParams, new_params *distanceParams) { var i uint if orig_params.distance_postfix_bits == new_params.distance_postfix_bits && orig_params.num_direct_distance_codes == new_params.num_direct_distance_codes { @@ -129,14 +129,14 @@ func RecomputeDistancePrefixes(cmds []command, num_commands uint, orig_params *B } } -func ComputeDistanceCost(cmds []command, num_commands uint, orig_params *BrotliDistanceParams, new_params *BrotliDistanceParams, cost *float64) bool { +func computeDistanceCost(cmds []command, num_commands uint, orig_params *distanceParams, new_params *distanceParams, cost *float64) bool { var i uint var equal_params bool = false var dist_prefix uint16 var dist_extra uint32 var extra_bits float64 = 0.0 - var histo HistogramDistance - HistogramClearDistance(&histo) + var histo histogramDistance + histogramClearDistance(&histo) if orig_params.distance_postfix_bits == new_params.distance_postfix_bits && orig_params.num_direct_distance_codes == new_params.num_direct_distance_codes { equal_params = true @@ -156,7 +156,7 @@ func ComputeDistanceCost(cmds []command, num_commands uint, orig_params *BrotliD PrefixEncodeCopyDistance(uint(distance), uint(new_params.num_direct_distance_codes), uint(new_params.distance_postfix_bits), &dist_prefix, &dist_extra) } - HistogramAddDistance(&histo, uint(dist_prefix)&0x3FF) + histogramAddDistance(&histo, uint(dist_prefix)&0x3FF) extra_bits += float64(dist_prefix >> 10) } } @@ -165,11 +165,11 @@ func ComputeDistanceCost(cmds []command, num_commands uint, orig_params *BrotliD return true } -var BrotliBuildMetaBlock_kMaxNumberOfHistograms uint = 256 +var buildMetaBlock_kMaxNumberOfHistograms uint = 256 -func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *BrotliEncoderParams, prev_byte byte, prev_byte2 byte, cmds []command, num_commands uint, literal_context_mode int, mb *MetaBlockSplit) { - var distance_histograms []HistogramDistance - var literal_histograms []HistogramLiteral +func buildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *encoderParams, prev_byte byte, prev_byte2 byte, cmds []command, num_commands uint, literal_context_mode int, mb *metaBlockSplit) { + var distance_histograms []histogramDistance + var literal_histograms []histogramLiteral var literal_context_modes []int = nil var literal_histograms_size uint var distance_histograms_size uint @@ -179,22 +179,22 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli var ndirect_msb uint32 = 0 var check_orig bool = true var best_dist_cost float64 = 1e99 - var orig_params BrotliEncoderParams = *params + var orig_params encoderParams = *params /* Histogram ids need to fit in one byte. */ - var new_params BrotliEncoderParams = *params + var new_params encoderParams = *params for npostfix = 0; npostfix <= maxNpostfix; npostfix++ { for ; ndirect_msb < 16; ndirect_msb++ { var ndirect uint32 = ndirect_msb << npostfix var skip bool var dist_cost float64 - BrotliInitDistanceParams(&new_params, npostfix, ndirect) + initDistanceParams(&new_params, npostfix, ndirect) if npostfix == orig_params.dist.distance_postfix_bits && ndirect == orig_params.dist.num_direct_distance_codes { check_orig = false } - skip = !ComputeDistanceCost(cmds, num_commands, &orig_params.dist, &new_params.dist, &dist_cost) + skip = !computeDistanceCost(cmds, num_commands, &orig_params.dist, &new_params.dist, &dist_cost) if skip || (dist_cost > best_dist_cost) { break } @@ -211,7 +211,7 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli if check_orig { var dist_cost float64 - ComputeDistanceCost(cmds, num_commands, &orig_params.dist, &orig_params.dist, &dist_cost) + computeDistanceCost(cmds, num_commands, &orig_params.dist, &orig_params.dist, &dist_cost) if dist_cost < best_dist_cost { /* NB: currently unused; uncomment when more param tuning is added. */ /* best_dist_cost = dist_cost; */ @@ -219,12 +219,12 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli } } - RecomputeDistancePrefixes(cmds, num_commands, &orig_params.dist, ¶ms.dist) + recomputeDistancePrefixes(cmds, num_commands, &orig_params.dist, ¶ms.dist) splitBlock(cmds, num_commands, ringbuffer, pos, mask, params, &mb.literal_split, &mb.command_split, &mb.distance_split) if !params.disable_literal_context_modeling { - literal_context_multiplier = 1 << BROTLI_LITERAL_CONTEXT_BITS + literal_context_multiplier = 1 << literalContextBits literal_context_modes = make([]int, (mb.literal_split.num_types)) for i = 0; i < mb.literal_split.num_types; i++ { literal_context_modes[i] = literal_context_mode @@ -232,30 +232,30 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli } literal_histograms_size = mb.literal_split.num_types * literal_context_multiplier - literal_histograms = make([]HistogramLiteral, literal_histograms_size) - ClearHistogramsLiteral(literal_histograms, literal_histograms_size) + literal_histograms = make([]histogramLiteral, literal_histograms_size) + clearHistogramsLiteral(literal_histograms, literal_histograms_size) - distance_histograms_size = mb.distance_split.num_types << BROTLI_DISTANCE_CONTEXT_BITS - distance_histograms = make([]HistogramDistance, distance_histograms_size) - ClearHistogramsDistance(distance_histograms, distance_histograms_size) + distance_histograms_size = mb.distance_split.num_types << distanceContextBits + distance_histograms = make([]histogramDistance, distance_histograms_size) + clearHistogramsDistance(distance_histograms, distance_histograms_size) assert(mb.command_histograms == nil) mb.command_histograms_size = mb.command_split.num_types - mb.command_histograms = make([]HistogramCommand, (mb.command_histograms_size)) - ClearHistogramsCommand(mb.command_histograms, mb.command_histograms_size) + mb.command_histograms = make([]histogramCommand, (mb.command_histograms_size)) + clearHistogramsCommand(mb.command_histograms, mb.command_histograms_size) - BrotliBuildHistogramsWithContext(cmds, num_commands, &mb.literal_split, &mb.command_split, &mb.distance_split, ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_modes, literal_histograms, mb.command_histograms, distance_histograms) + buildHistogramsWithContext(cmds, num_commands, &mb.literal_split, &mb.command_split, &mb.distance_split, ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_modes, literal_histograms, mb.command_histograms, distance_histograms) literal_context_modes = nil assert(mb.literal_context_map == nil) - mb.literal_context_map_size = mb.literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS + mb.literal_context_map_size = mb.literal_split.num_types << literalContextBits mb.literal_context_map = make([]uint32, (mb.literal_context_map_size)) assert(mb.literal_histograms == nil) mb.literal_histograms_size = mb.literal_context_map_size - mb.literal_histograms = make([]HistogramLiteral, (mb.literal_histograms_size)) + mb.literal_histograms = make([]histogramLiteral, (mb.literal_histograms_size)) - clusterHistogramsLiteral(literal_histograms, literal_histograms_size, BrotliBuildMetaBlock_kMaxNumberOfHistograms, mb.literal_histograms, &mb.literal_histograms_size, mb.literal_context_map) + clusterHistogramsLiteral(literal_histograms, literal_histograms_size, buildMetaBlock_kMaxNumberOfHistograms, mb.literal_histograms, &mb.literal_histograms_size, mb.literal_context_map) literal_histograms = nil if params.disable_literal_context_modeling { @@ -263,29 +263,29 @@ func BrotliBuildMetaBlock(ringbuffer []byte, pos uint, mask uint, params *Brotli for i = mb.literal_split.num_types; i != 0; { var j uint = 0 i-- - for ; j < 1< 0 { - var entropy [BROTLI_MAX_STATIC_CONTEXTS]float64 - var combined_histo []HistogramLiteral = make([]HistogramLiteral, (2 * num_contexts)) - var combined_entropy [2 * BROTLI_MAX_STATIC_CONTEXTS]float64 + var entropy [maxStaticContexts]float64 + var combined_histo []histogramLiteral = make([]histogramLiteral, (2 * num_contexts)) + var combined_entropy [2 * maxStaticContexts]float64 var diff = [2]float64{0.0} /* Try merging the set of histograms for the current block type with the respective set of histograms for the last and second last block types. @@ -394,7 +394,7 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) var jx uint = j*num_contexts + i var last_histogram_ix uint = self.last_histogram_ix_[j] + i combined_histo[jx] = histograms[curr_histo_ix] - HistogramAddHistogramLiteral(&combined_histo[jx], &histograms[last_histogram_ix]) + histogramAddHistogramLiteral(&combined_histo[jx], &histograms[last_histogram_ix]) combined_entropy[jx] = bitsEntropy(combined_histo[jx].data_[0:], self.alphabet_size_) diff[j] += combined_entropy[jx] - entropy[i] - last_entropy[jx] } @@ -416,7 +416,7 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) split.num_types++ self.curr_histogram_ix_ += num_contexts if self.curr_histogram_ix_ < *self.histograms_size_ { - ClearHistogramsLiteral(self.histograms_[self.curr_histogram_ix_:], self.num_contexts_) + clearHistogramsLiteral(self.histograms_[self.curr_histogram_ix_:], self.num_contexts_) } self.block_size_ = 0 @@ -434,7 +434,7 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) histograms[self.last_histogram_ix_[0]+i] = combined_histo[num_contexts+i] last_entropy[num_contexts+i] = last_entropy[i] last_entropy[i] = combined_entropy[num_contexts+i] - HistogramClearLiteral(&histograms[self.curr_histogram_ix_+i]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_+i]) } self.num_blocks_++ @@ -452,7 +452,7 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) last_entropy[num_contexts+i] = last_entropy[i] } - HistogramClearLiteral(&histograms[self.curr_histogram_ix_+i]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_+i]) } self.block_size_ = 0 @@ -473,36 +473,36 @@ func ContextBlockSplitterFinishBlock(self *ContextBlockSplitter, is_final bool) /* Adds the next symbol to the current block type and context. When the current block reaches the target size, decides on merging the block. */ -func ContextBlockSplitterAddSymbol(self *ContextBlockSplitter, symbol uint, context uint) { - HistogramAddLiteral(&self.histograms_[self.curr_histogram_ix_+context], symbol) +func contextBlockSplitterAddSymbol(self *contextBlockSplitter, symbol uint, context uint) { + histogramAddLiteral(&self.histograms_[self.curr_histogram_ix_+context], symbol) self.block_size_++ if self.block_size_ == self.target_block_size_ { - ContextBlockSplitterFinishBlock(self, false) /* is_final = */ + contextBlockSplitterFinishBlock(self, false) /* is_final = */ } } -func MapStaticContexts(num_contexts uint, static_context_map []uint32, mb *MetaBlockSplit) { +func mapStaticContexts(num_contexts uint, static_context_map []uint32, mb *metaBlockSplit) { var i uint assert(mb.literal_context_map == nil) - mb.literal_context_map_size = mb.literal_split.num_types << BROTLI_LITERAL_CONTEXT_BITS + mb.literal_context_map_size = mb.literal_split.num_types << literalContextBits mb.literal_context_map = make([]uint32, (mb.literal_context_map_size)) for i = 0; i < mb.literal_split.num_types; i++ { var offset uint32 = uint32(i * num_contexts) var j uint - for j = 0; j < 1<= 128 { - BlockSplitterAddSymbolDistance(&dist_blocks, uint(cmd.dist_prefix_)&0x3FF) + blockSplitterAddSymbolDistance(&dist_blocks, uint(cmd.dist_prefix_)&0x3FF) } } } if num_contexts == 1 { - BlockSplitterFinishBlockLiteral(&lit_blocks.plain, true) /* is_final = */ + blockSplitterFinishBlockLiteral(&lit_blocks.plain, true) /* is_final = */ } else { - ContextBlockSplitterFinishBlock(&lit_blocks.ctx, true) /* is_final = */ + contextBlockSplitterFinishBlock(&lit_blocks.ctx, true) /* is_final = */ } - BlockSplitterFinishBlockCommand(&cmd_blocks, true) /* is_final = */ - BlockSplitterFinishBlockDistance(&dist_blocks, true) /* is_final = */ + blockSplitterFinishBlockCommand(&cmd_blocks, true) /* is_final = */ + blockSplitterFinishBlockDistance(&dist_blocks, true) /* is_final = */ if num_contexts > 1 { - MapStaticContexts(num_contexts, static_context_map, mb) + mapStaticContexts(num_contexts, static_context_map, mb) } } -func BrotliBuildMetaBlockGreedy(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut ContextLut, num_contexts uint, static_context_map []uint32, commands []command, n_commands uint, mb *MetaBlockSplit) { +func buildMetaBlockGreedy(ringbuffer []byte, pos uint, mask uint, prev_byte byte, prev_byte2 byte, literal_context_lut contextLUT, num_contexts uint, static_context_map []uint32, commands []command, n_commands uint, mb *metaBlockSplit) { if num_contexts == 1 { - BrotliBuildMetaBlockGreedyInternal(ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_lut, 1, nil, commands, n_commands, mb) + buildMetaBlockGreedyInternal(ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_lut, 1, nil, commands, n_commands, mb) } else { - BrotliBuildMetaBlockGreedyInternal(ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_lut, num_contexts, static_context_map, commands, n_commands, mb) + buildMetaBlockGreedyInternal(ringbuffer, pos, mask, prev_byte, prev_byte2, literal_context_lut, num_contexts, static_context_map, commands, n_commands, mb) } } -func BrotliOptimizeHistograms(num_distance_codes uint32, mb *MetaBlockSplit) { +func optimizeHistograms(num_distance_codes uint32, mb *metaBlockSplit) { var good_for_rle [numCommandSymbols]byte var i uint for i = 0; i < mb.literal_histograms_size; i++ { - BrotliOptimizeHuffmanCountsForRle(256, mb.literal_histograms[i].data_[:], good_for_rle[:]) + optimizeHuffmanCountsForRLE(256, mb.literal_histograms[i].data_[:], good_for_rle[:]) } for i = 0; i < mb.command_histograms_size; i++ { - BrotliOptimizeHuffmanCountsForRle(numCommandSymbols, mb.command_histograms[i].data_[:], good_for_rle[:]) + optimizeHuffmanCountsForRLE(numCommandSymbols, mb.command_histograms[i].data_[:], good_for_rle[:]) } for i = 0; i < mb.distance_histograms_size; i++ { - BrotliOptimizeHuffmanCountsForRle(uint(num_distance_codes), mb.distance_histograms[i].data_[:], good_for_rle[:]) + optimizeHuffmanCountsForRLE(uint(num_distance_codes), mb.distance_histograms[i].data_[:], good_for_rle[:]) } } diff --git a/metablock_command.go b/metablock_command.go index 4cef356..dfa6a1c 100644 --- a/metablock_command.go +++ b/metablock_command.go @@ -9,13 +9,13 @@ package brotli /* Greedy block splitter for one block category (literal, command or distance). */ -type BlockSplitterCommand struct { +type blockSplitterCommand struct { alphabet_size_ uint min_block_size_ uint split_threshold_ float64 num_blocks_ uint split_ *blockSplit - histograms_ []HistogramCommand + histograms_ []histogramCommand histograms_size_ *uint target_block_size_ uint block_size_ uint @@ -25,7 +25,7 @@ type BlockSplitterCommand struct { merge_last_count_ uint } -func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramCommand, histograms_size *uint) { +func initBlockSplitterCommand(self *blockSplitterCommand, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramCommand, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block @@ -46,11 +46,11 @@ func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, mi self.split_.num_blocks = max_num_blocks assert(*histograms == nil) *histograms_size = max_num_types - *histograms = make([]HistogramCommand, (*histograms_size)) + *histograms = make([]histogramCommand, (*histograms_size)) self.histograms_ = *histograms /* Clear only current histogram. */ - HistogramClearCommand(&self.histograms_[0]) + histogramClearCommand(&self.histograms_[0]) self.last_histogram_ix_[1] = 0 self.last_histogram_ix_[0] = self.last_histogram_ix_[1] @@ -60,10 +60,10 @@ func InitBlockSplitterCommand(self *BlockSplitterCommand, alphabet_size uint, mi (1) emits the current block with a new block type; (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ -func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) { +func blockSplitterFinishBlockCommand(self *blockSplitterCommand, is_final bool) { var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] - var histograms []HistogramCommand = self.histograms_ + var histograms []histogramCommand = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) if self.num_blocks_ == 0 { /* Create first block. */ @@ -76,19 +76,19 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearCommand(&histograms[self.curr_histogram_ix_]) + histogramClearCommand(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 } else if self.block_size_ > 0 { var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_) - var combined_histo [2]HistogramCommand + var combined_histo [2]histogramCommand var combined_entropy [2]float64 var diff [2]float64 var j uint for j = 0; j < 2; j++ { var last_histogram_ix uint = self.last_histogram_ix_[j] combined_histo[j] = histograms[self.curr_histogram_ix_] - HistogramAddHistogramCommand(&combined_histo[j], &histograms[last_histogram_ix]) + histogramAddHistogramCommand(&combined_histo[j], &histograms[last_histogram_ix]) combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_) diff[j] = combined_entropy[j] - entropy - last_entropy[j] } @@ -106,7 +106,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearCommand(&histograms[self.curr_histogram_ix_]) + histogramClearCommand(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 self.merge_last_count_ = 0 @@ -124,7 +124,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) last_entropy[0] = combined_entropy[1] self.num_blocks_++ self.block_size_ = 0 - HistogramClearCommand(&histograms[self.curr_histogram_ix_]) + histogramClearCommand(&histograms[self.curr_histogram_ix_]) self.merge_last_count_ = 0 self.target_block_size_ = self.min_block_size_ } else { @@ -138,7 +138,7 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) } self.block_size_ = 0 - HistogramClearCommand(&histograms[self.curr_histogram_ix_]) + histogramClearCommand(&histograms[self.curr_histogram_ix_]) self.merge_last_count_++ if self.merge_last_count_ > 1 { self.target_block_size_ += self.min_block_size_ @@ -154,10 +154,10 @@ func BlockSplitterFinishBlockCommand(self *BlockSplitterCommand, is_final bool) /* Adds the next symbol to the current histogram. When the current histogram reaches the target size, decides on merging the block. */ -func BlockSplitterAddSymbolCommand(self *BlockSplitterCommand, symbol uint) { - HistogramAddCommand(&self.histograms_[self.curr_histogram_ix_], symbol) +func blockSplitterAddSymbolCommand(self *blockSplitterCommand, symbol uint) { + histogramAddCommand(&self.histograms_[self.curr_histogram_ix_], symbol) self.block_size_++ if self.block_size_ == self.target_block_size_ { - BlockSplitterFinishBlockCommand(self, false) /* is_final = */ + blockSplitterFinishBlockCommand(self, false) /* is_final = */ } } diff --git a/metablock_distance.go b/metablock_distance.go index 574d7a3..e2b2ddc 100644 --- a/metablock_distance.go +++ b/metablock_distance.go @@ -9,13 +9,13 @@ package brotli /* Greedy block splitter for one block category (literal, command or distance). */ -type BlockSplitterDistance struct { +type blockSplitterDistance struct { alphabet_size_ uint min_block_size_ uint split_threshold_ float64 num_blocks_ uint split_ *blockSplit - histograms_ []HistogramDistance + histograms_ []histogramDistance histograms_size_ *uint target_block_size_ uint block_size_ uint @@ -25,7 +25,7 @@ type BlockSplitterDistance struct { merge_last_count_ uint } -func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramDistance, histograms_size *uint) { +func initBlockSplitterDistance(self *blockSplitterDistance, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramDistance, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block @@ -46,11 +46,11 @@ func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, self.split_.num_blocks = max_num_blocks assert(*histograms == nil) *histograms_size = max_num_types - *histograms = make([]HistogramDistance, (*histograms_size)) + *histograms = make([]histogramDistance, (*histograms_size)) self.histograms_ = *histograms /* Clear only current histogram. */ - HistogramClearDistance(&self.histograms_[0]) + histogramClearDistance(&self.histograms_[0]) self.last_histogram_ix_[1] = 0 self.last_histogram_ix_[0] = self.last_histogram_ix_[1] @@ -60,10 +60,10 @@ func InitBlockSplitterDistance(self *BlockSplitterDistance, alphabet_size uint, (1) emits the current block with a new block type; (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ -func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool) { +func blockSplitterFinishBlockDistance(self *blockSplitterDistance, is_final bool) { var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] - var histograms []HistogramDistance = self.histograms_ + var histograms []histogramDistance = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) if self.num_blocks_ == 0 { /* Create first block. */ @@ -76,19 +76,19 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearDistance(&histograms[self.curr_histogram_ix_]) + histogramClearDistance(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 } else if self.block_size_ > 0 { var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_) - var combined_histo [2]HistogramDistance + var combined_histo [2]histogramDistance var combined_entropy [2]float64 var diff [2]float64 var j uint for j = 0; j < 2; j++ { var last_histogram_ix uint = self.last_histogram_ix_[j] combined_histo[j] = histograms[self.curr_histogram_ix_] - HistogramAddHistogramDistance(&combined_histo[j], &histograms[last_histogram_ix]) + histogramAddHistogramDistance(&combined_histo[j], &histograms[last_histogram_ix]) combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_) diff[j] = combined_entropy[j] - entropy - last_entropy[j] } @@ -106,7 +106,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearDistance(&histograms[self.curr_histogram_ix_]) + histogramClearDistance(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 self.merge_last_count_ = 0 @@ -124,7 +124,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool last_entropy[0] = combined_entropy[1] self.num_blocks_++ self.block_size_ = 0 - HistogramClearDistance(&histograms[self.curr_histogram_ix_]) + histogramClearDistance(&histograms[self.curr_histogram_ix_]) self.merge_last_count_ = 0 self.target_block_size_ = self.min_block_size_ } else { @@ -138,7 +138,7 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool } self.block_size_ = 0 - HistogramClearDistance(&histograms[self.curr_histogram_ix_]) + histogramClearDistance(&histograms[self.curr_histogram_ix_]) self.merge_last_count_++ if self.merge_last_count_ > 1 { self.target_block_size_ += self.min_block_size_ @@ -154,10 +154,10 @@ func BlockSplitterFinishBlockDistance(self *BlockSplitterDistance, is_final bool /* Adds the next symbol to the current histogram. When the current histogram reaches the target size, decides on merging the block. */ -func BlockSplitterAddSymbolDistance(self *BlockSplitterDistance, symbol uint) { - HistogramAddDistance(&self.histograms_[self.curr_histogram_ix_], symbol) +func blockSplitterAddSymbolDistance(self *blockSplitterDistance, symbol uint) { + histogramAddDistance(&self.histograms_[self.curr_histogram_ix_], symbol) self.block_size_++ if self.block_size_ == self.target_block_size_ { - BlockSplitterFinishBlockDistance(self, false) /* is_final = */ + blockSplitterFinishBlockDistance(self, false) /* is_final = */ } } diff --git a/metablock_literal.go b/metablock_literal.go index 2552334..1b6ef18 100644 --- a/metablock_literal.go +++ b/metablock_literal.go @@ -9,13 +9,13 @@ package brotli /* Greedy block splitter for one block category (literal, command or distance). */ -type BlockSplitterLiteral struct { +type blockSplitterLiteral struct { alphabet_size_ uint min_block_size_ uint split_threshold_ float64 num_blocks_ uint split_ *blockSplit - histograms_ []HistogramLiteral + histograms_ []histogramLiteral histograms_size_ *uint target_block_size_ uint block_size_ uint @@ -25,7 +25,7 @@ type BlockSplitterLiteral struct { merge_last_count_ uint } -func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]HistogramLiteral, histograms_size *uint) { +func initBlockSplitterLiteral(self *blockSplitterLiteral, alphabet_size uint, min_block_size uint, split_threshold float64, num_symbols uint, split *blockSplit, histograms *[]histogramLiteral, histograms_size *uint) { var max_num_blocks uint = num_symbols/min_block_size + 1 var max_num_types uint = brotli_min_size_t(max_num_blocks, maxNumberOfBlockTypes+1) /* We have to allocate one more histogram than the maximum number of block @@ -46,11 +46,11 @@ func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, mi self.split_.num_blocks = max_num_blocks assert(*histograms == nil) *histograms_size = max_num_types - *histograms = make([]HistogramLiteral, (*histograms_size)) + *histograms = make([]histogramLiteral, (*histograms_size)) self.histograms_ = *histograms /* Clear only current histogram. */ - HistogramClearLiteral(&self.histograms_[0]) + histogramClearLiteral(&self.histograms_[0]) self.last_histogram_ix_[1] = 0 self.last_histogram_ix_[0] = self.last_histogram_ix_[1] @@ -60,10 +60,10 @@ func InitBlockSplitterLiteral(self *BlockSplitterLiteral, alphabet_size uint, mi (1) emits the current block with a new block type; (2) emits the current block with the type of the second last block; (3) merges the current block with the last block. */ -func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) { +func blockSplitterFinishBlockLiteral(self *blockSplitterLiteral, is_final bool) { var split *blockSplit = self.split_ var last_entropy []float64 = self.last_entropy_[:] - var histograms []HistogramLiteral = self.histograms_ + var histograms []histogramLiteral = self.histograms_ self.block_size_ = brotli_max_size_t(self.block_size_, self.min_block_size_) if self.num_blocks_ == 0 { /* Create first block. */ @@ -76,19 +76,19 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearLiteral(&histograms[self.curr_histogram_ix_]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 } else if self.block_size_ > 0 { var entropy float64 = bitsEntropy(histograms[self.curr_histogram_ix_].data_[:], self.alphabet_size_) - var combined_histo [2]HistogramLiteral + var combined_histo [2]histogramLiteral var combined_entropy [2]float64 var diff [2]float64 var j uint for j = 0; j < 2; j++ { var last_histogram_ix uint = self.last_histogram_ix_[j] combined_histo[j] = histograms[self.curr_histogram_ix_] - HistogramAddHistogramLiteral(&combined_histo[j], &histograms[last_histogram_ix]) + histogramAddHistogramLiteral(&combined_histo[j], &histograms[last_histogram_ix]) combined_entropy[j] = bitsEntropy(combined_histo[j].data_[0:], self.alphabet_size_) diff[j] = combined_entropy[j] - entropy - last_entropy[j] } @@ -106,7 +106,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) split.num_types++ self.curr_histogram_ix_++ if self.curr_histogram_ix_ < *self.histograms_size_ { - HistogramClearLiteral(&histograms[self.curr_histogram_ix_]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_]) } self.block_size_ = 0 self.merge_last_count_ = 0 @@ -124,7 +124,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) last_entropy[0] = combined_entropy[1] self.num_blocks_++ self.block_size_ = 0 - HistogramClearLiteral(&histograms[self.curr_histogram_ix_]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_]) self.merge_last_count_ = 0 self.target_block_size_ = self.min_block_size_ } else { @@ -138,7 +138,7 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) } self.block_size_ = 0 - HistogramClearLiteral(&histograms[self.curr_histogram_ix_]) + histogramClearLiteral(&histograms[self.curr_histogram_ix_]) self.merge_last_count_++ if self.merge_last_count_ > 1 { self.target_block_size_ += self.min_block_size_ @@ -154,10 +154,10 @@ func BlockSplitterFinishBlockLiteral(self *BlockSplitterLiteral, is_final bool) /* Adds the next symbol to the current histogram. When the current histogram reaches the target size, decides on merging the block. */ -func BlockSplitterAddSymbolLiteral(self *BlockSplitterLiteral, symbol uint) { - HistogramAddLiteral(&self.histograms_[self.curr_histogram_ix_], symbol) +func blockSplitterAddSymbolLiteral(self *blockSplitterLiteral, symbol uint) { + histogramAddLiteral(&self.histograms_[self.curr_histogram_ix_], symbol) self.block_size_++ if self.block_size_ == self.target_block_size_ { - BlockSplitterFinishBlockLiteral(self, false) /* is_final = */ + blockSplitterFinishBlockLiteral(self, false) /* is_final = */ } } diff --git a/params.go b/params.go index 8110b34..f577d1c 100644 --- a/params.go +++ b/params.go @@ -199,7 +199,7 @@ package brotli */ /* Parameters for the Brotli encoder with chosen quality levels. */ -type BrotliHasherParams struct { +type hasherParams struct { type_ int bucket_bits int block_bits int @@ -207,7 +207,7 @@ type BrotliHasherParams struct { num_last_distances_to_check int } -type BrotliDistanceParams struct { +type distanceParams struct { distance_postfix_bits uint32 num_direct_distance_codes uint32 alphabet_size uint32 @@ -215,7 +215,7 @@ type BrotliDistanceParams struct { } /* Encoding parameters */ -type BrotliEncoderParams struct { +type encoderParams struct { mode int quality int lgwin uint @@ -223,7 +223,7 @@ type BrotliEncoderParams struct { size_hint uint disable_literal_context_modeling bool large_window bool - hasher BrotliHasherParams - dist BrotliDistanceParams - dictionary BrotliEncoderDictionary + hasher hasherParams + dist distanceParams + dictionary encoderDictionary } diff --git a/prefix.go b/prefix.go index 83a3cf0..0b8cec9 100644 --- a/prefix.go +++ b/prefix.go @@ -39,7 +39,7 @@ func PrefixEncodeCopyDistance(distance_code uint, num_direct_codes uint, postfix return } else { var dist uint = (uint(1) << (postfix_bits + 2)) + (distance_code - numDistanceShortCodes - num_direct_codes) - var bucket uint = uint(Log2FloorNonZero(dist) - 1) + var bucket uint = uint(log2FloorNonZero(dist) - 1) var postfix_mask uint = (1 << postfix_bits) - 1 var postfix uint = dist & postfix_mask var prefix uint = (dist >> bucket) & 1 diff --git a/prefix_dec.go b/prefix_dec.go index 84958b5..b24c8d0 100644 --- a/prefix_dec.go +++ b/prefix_dec.go @@ -11,7 +11,7 @@ package brotli /* Represents the range of values belonging to a prefix code: [offset, offset + 2^nbits) */ -type PrefixCodeRange1 struct { +type prefixCodeRange1 struct { offset uint16 nbits byte } diff --git a/quality.go b/quality.go index d33c930..3fa45c1 100644 --- a/quality.go +++ b/quality.go @@ -45,7 +45,7 @@ const MAX_ZOPFLI_LEN_QUALITY_11 = 325 /* Do not thoroughly search when a long copy is found. */ const BROTLI_LONG_COPY_QUICK_STEP = 16384 -func MaxZopfliLen(params *BrotliEncoderParams) uint { +func MaxZopfliLen(params *encoderParams) uint { if params.quality <= 10 { return MAX_ZOPFLI_LEN_QUALITY_10 } else { @@ -54,7 +54,7 @@ func MaxZopfliLen(params *BrotliEncoderParams) uint { } /* Number of best candidates to evaluate to expand Zopfli chain. */ -func MaxZopfliCandidates(params *BrotliEncoderParams) uint { +func MaxZopfliCandidates(params *encoderParams) uint { if params.quality <= 10 { return 1 } else { @@ -62,20 +62,20 @@ func MaxZopfliCandidates(params *BrotliEncoderParams) uint { } } -func SanitizeParams(params *BrotliEncoderParams) { - params.quality = brotli_min_int(BROTLI_MAX_QUALITY, brotli_max_int(BROTLI_MIN_QUALITY, params.quality)) +func SanitizeParams(params *encoderParams) { + params.quality = brotli_min_int(maxQuality, brotli_max_int(minQuality, params.quality)) if params.quality <= MAX_QUALITY_FOR_STATIC_ENTROPY_CODES { params.large_window = false } - if params.lgwin < BROTLI_MIN_WINDOW_BITS { - params.lgwin = BROTLI_MIN_WINDOW_BITS + if params.lgwin < minWindowBits { + params.lgwin = minWindowBits } else { var max_lgwin int if params.large_window { - max_lgwin = BROTLI_LARGE_MAX_WINDOW_BITS + max_lgwin = largeMaxWindowBits } else { - max_lgwin = BROTLI_MAX_WINDOW_BITS + max_lgwin = maxWindowBits } if params.lgwin > uint(max_lgwin) { params.lgwin = uint(max_lgwin) @@ -84,7 +84,7 @@ func SanitizeParams(params *BrotliEncoderParams) { } /* Returns optimized lg_block value. */ -func ComputeLgBlock(params *BrotliEncoderParams) int { +func ComputeLgBlock(params *encoderParams) int { var lgblock int = params.lgblock if params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY || params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY { lgblock = int(params.lgwin) @@ -96,7 +96,7 @@ func ComputeLgBlock(params *BrotliEncoderParams) int { lgblock = brotli_min_int(18, int(params.lgwin)) } } else { - lgblock = brotli_min_int(BROTLI_MAX_INPUT_BLOCK_BITS, brotli_max_int(BROTLI_MIN_INPUT_BLOCK_BITS, lgblock)) + lgblock = brotli_min_int(maxInputBlockBits, brotli_max_int(minInputBlockBits, lgblock)) } return lgblock @@ -107,12 +107,12 @@ func ComputeLgBlock(params *BrotliEncoderParams) int { added block fits there completely and we still get lgwin bits and at least read_block_size_bits + 1 bits because the copy tail length needs to be smaller than ring-buffer size. */ -func ComputeRbBits(params *BrotliEncoderParams) int { +func ComputeRbBits(params *encoderParams) int { return 1 + brotli_max_int(int(params.lgwin), params.lgblock) } -func MaxMetablockSize(params *BrotliEncoderParams) uint { - var bits int = brotli_min_int(ComputeRbBits(params), BROTLI_MAX_INPUT_BLOCK_BITS) +func MaxMetablockSize(params *encoderParams) uint { + var bits int = brotli_min_int(ComputeRbBits(params), maxInputBlockBits) return uint(1) << uint(bits) } @@ -122,7 +122,7 @@ func MaxMetablockSize(params *BrotliEncoderParams) uint { At first 8 byte strides are taken and every second byte is put to hasher. After 4x more literals stride by 16 bytes, every put 4-th byte to hasher. Applied only to qualities 2 to 9. */ -func LiteralSpreeLengthForSparseSearch(params *BrotliEncoderParams) uint { +func LiteralSpreeLengthForSparseSearch(params *encoderParams) uint { if params.quality < 9 { return 64 } else { @@ -130,7 +130,7 @@ func LiteralSpreeLengthForSparseSearch(params *BrotliEncoderParams) uint { } } -func ChooseHasher(params *BrotliEncoderParams, hparams *BrotliHasherParams) { +func ChooseHasher(params *encoderParams, hparams *hasherParams) { if params.quality > 9 { hparams.type_ = 10 } else if params.quality == 4 && params.size_hint >= 1<<20 { diff --git a/reader.go b/reader.go index b9447da..75720eb 100644 --- a/reader.go +++ b/reader.go @@ -8,7 +8,7 @@ import ( type decodeError int func (err decodeError) Error() string { - return "brotli: " + string(BrotliDecoderErrorString(int(err))) + return "brotli: " + string(decoderErrorString(int(err))) } var errExcessiveInput = errors.New("brotli: excessive input") @@ -30,7 +30,7 @@ func NewReader(src io.Reader) *Reader { } func (r *Reader) Read(p []byte) (n int, err error) { - if !BrotliDecoderHasMoreOutput(r) && len(r.in) == 0 { + if !decoderHasMoreOutput(r) && len(r.in) == 0 { m, readErr := r.src.Read(r.buf) if m == 0 { // If readErr is `nil`, we just proxy underlying stream behavior. @@ -49,24 +49,24 @@ func (r *Reader) Read(p []byte) (n int, err error) { out_len := uint(len(p)) in_remaining := in_len out_remaining := out_len - result := BrotliDecoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p) + result := decoderDecompressStream(r, &in_remaining, &r.in, &out_remaining, &p) written = out_len - out_remaining n = int(written) switch result { - case BROTLI_DECODER_RESULT_SUCCESS: + case decoderResultSuccess: if len(r.in) > 0 { return n, errExcessiveInput } return n, nil - case BROTLI_DECODER_RESULT_ERROR: - return n, decodeError(BrotliDecoderGetErrorCode(r)) - case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: + case decoderResultError: + return n, decodeError(decoderGetErrorCode(r)) + case decoderResultNeedsMoreOutput: if n == 0 { return 0, io.ErrShortBuffer } return n, nil - case BROTLI_DECODER_NEEDS_MORE_INPUT: + case decoderNeedsMoreInput: } if len(r.in) != 0 { diff --git a/ringbuffer.go b/ringbuffer.go index 5f5d929..4027295 100644 --- a/ringbuffer.go +++ b/ringbuffer.go @@ -42,7 +42,7 @@ func RingBufferInit(rb *RingBuffer) { rb.buffer_ = nil } -func RingBufferSetup(params *BrotliEncoderParams, rb *RingBuffer) { +func RingBufferSetup(params *encoderParams, rb *RingBuffer) { var window_bits int = ComputeRbBits(params) var tail_bits int = params.lgblock *(*uint32)(&rb.size_) = 1 << uint(window_bits) diff --git a/state.go b/state.go index 7a686b6..d72d578 100644 --- a/state.go +++ b/state.go @@ -115,15 +115,15 @@ type Reader struct { sub_loop_counter uint32 ringbuffer []byte ringbuffer_end []byte - htree_command []HuffmanCode + htree_command []huffmanCode context_lookup []byte context_map_slice []byte dist_context_map_slice []byte - literal_hgroup HuffmanTreeGroup - insert_copy_hgroup HuffmanTreeGroup - distance_hgroup HuffmanTreeGroup - block_type_trees []HuffmanCode - block_len_trees []HuffmanCode + literal_hgroup huffmanTreeGroup + insert_copy_hgroup huffmanTreeGroup + distance_hgroup huffmanTreeGroup + block_type_trees []huffmanCode + block_len_trees []huffmanCode trivial_literal_context int distance_context int meta_block_remaining_len int @@ -136,7 +136,7 @@ type Reader struct { distance_postfix_mask int num_dist_htrees uint32 dist_context_map []byte - literal_htree []HuffmanCode + literal_htree []huffmanCode dist_htree_index byte repeat_code_len uint32 prev_code_len uint32 @@ -147,18 +147,18 @@ type Reader struct { symbol uint32 repeat uint32 space uint32 - table [32]HuffmanCode + table [32]huffmanCode symbol_lists SymbolList - symbols_lists_array [BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + numCommandSymbols]uint16 + symbols_lists_array [huffmanMaxCodeLength + 1 + numCommandSymbols]uint16 next_symbol [32]int code_length_code_lengths [codeLengthCodes]byte code_length_histo [16]uint16 htree_index int - next []HuffmanCode + next []huffmanCode context_index uint32 max_run_length_prefix uint32 code uint32 - context_map_table [BROTLI_HUFFMAN_MAX_SIZE_272]HuffmanCode + context_map_table [huffmanMaxSize272]huffmanCode substate_metablock_header int substate_tree_group int substate_context_map int @@ -178,7 +178,7 @@ type Reader struct { num_literal_htrees uint32 context_map []byte context_modes []byte - dictionary *BrotliDictionary + dictionary *dictionary transforms *BrotliTransforms trivial_literal_contexts [8]uint32 } @@ -242,9 +242,9 @@ func BrotliDecoderStateInit(s *Reader) bool { s.block_len_trees = nil s.symbol_lists.storage = s.symbols_lists_array[:] - s.symbol_lists.offset = BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + s.symbol_lists.offset = huffmanMaxCodeLength + 1 - s.dictionary = BrotliGetDictionary() + s.dictionary = getDictionary() s.transforms = BrotliGetTransforms() return true @@ -296,12 +296,12 @@ func BrotliDecoderStateCleanup(s *Reader) { s.block_type_trees = nil } -func BrotliDecoderHuffmanTreeGroupInit(s *Reader, group *HuffmanTreeGroup, alphabet_size uint32, max_symbol uint32, ntrees uint32) bool { +func BrotliDecoderHuffmanTreeGroupInit(s *Reader, group *huffmanTreeGroup, alphabet_size uint32, max_symbol uint32, ntrees uint32) bool { var max_table_size uint = uint(kMaxHuffmanTableSize[(alphabet_size+31)>>5]) group.alphabet_size = uint16(alphabet_size) group.max_symbol = uint16(max_symbol) group.num_htrees = uint16(ntrees) - group.htrees = make([][]HuffmanCode, ntrees) - group.codes = make([]HuffmanCode, (uint(ntrees) * max_table_size)) + group.htrees = make([][]huffmanCode, ntrees) + group.codes = make([]huffmanCode, (uint(ntrees) * max_table_size)) return !(group.codes == nil) } diff --git a/static_dict.go b/static_dict.go index 80e6363..a044819 100644 --- a/static_dict.go +++ b/static_dict.go @@ -31,24 +31,24 @@ func AddMatch(distance uint, len uint, len_code uint, matches []uint32) { matches[len] = brotli_min_uint32_t(matches[len], match) } -func DictMatchLength(dictionary *BrotliDictionary, data []byte, id uint, len uint, maxlen uint) uint { - var offset uint = uint(dictionary.offsets_by_length[len]) + len*id - return FindMatchLengthWithLimit(dictionary.data[offset:], data, brotli_min_size_t(uint(len), maxlen)) +func DictMatchLength(dict *dictionary, data []byte, id uint, len uint, maxlen uint) uint { + var offset uint = uint(dict.offsets_by_length[len]) + len*id + return findMatchLengthWithLimit(dict.data[offset:], data, brotli_min_size_t(uint(len), maxlen)) } -func IsMatch(dictionary *BrotliDictionary, w DictWord, data []byte, max_length uint) bool { +func IsMatch(d *dictionary, w DictWord, data []byte, max_length uint) bool { if uint(w.len) > max_length { return false } else { - var offset uint = uint(dictionary.offsets_by_length[w.len]) + uint(w.len)*uint(w.idx) - var dict []byte = dictionary.data[offset:] + var offset uint = uint(d.offsets_by_length[w.len]) + uint(w.len)*uint(w.idx) + var dict []byte = d.data[offset:] if w.transform == 0 { /* Match against base dictionary word. */ - return FindMatchLengthWithLimit(dict, data, uint(w.len)) == uint(w.len) + return findMatchLengthWithLimit(dict, data, uint(w.len)) == uint(w.len) } else if w.transform == 10 { /* Match against uppercase first transform. Note that there are only ASCII uppercase words in the lookup table. */ - return dict[0] >= 'a' && dict[0] <= 'z' && (dict[0]^32) == data[0] && FindMatchLengthWithLimit(dict[1:], data[1:], uint(w.len)-1) == uint(w.len-1) + return dict[0] >= 'a' && dict[0] <= 'z' && (dict[0]^32) == data[0] && findMatchLengthWithLimit(dict[1:], data[1:], uint(w.len)-1) == uint(w.len-1) } else { /* Match against uppercase all transform. Note that there are only ASCII uppercase words in the lookup table. */ @@ -70,22 +70,22 @@ func IsMatch(dictionary *BrotliDictionary, w DictWord, data []byte, max_length u } } -func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, data []byte, min_length uint, max_length uint, matches []uint32) bool { +func BrotliFindAllStaticDictionaryMatches(dict *encoderDictionary, data []byte, min_length uint, max_length uint, matches []uint32) bool { var has_found_match bool = false { - var offset uint = uint(dictionary.buckets[Hash(data)]) + var offset uint = uint(dict.buckets[Hash(data)]) var end bool = offset == 0 for !end { var w DictWord - w = dictionary.dict_words[offset] + w = dict.dict_words[offset] offset++ var l uint = uint(w.len) & 0x1F - var n uint = uint(1) << dictionary.words.size_bits_by_length[l] + var n uint = uint(1) << dict.words.size_bits_by_length[l] var id uint = uint(w.idx) end = !(w.len&0x80 == 0) w.len = byte(l) if w.transform == 0 { - var matchlen uint = DictMatchLength(dictionary.words, data, id, l, max_length) + var matchlen uint = DictMatchLength(dict.words, data, id, l, max_length) var s []byte var minlen uint var maxlen uint @@ -117,7 +117,7 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d maxlen = brotli_min_size_t(matchlen, l-2) for len = minlen; len <= maxlen; len++ { var cut uint = l - len - var transform_id uint = (cut << 2) + uint((dictionary.cutoffTransforms>>(cut*6))&0x3F) + var transform_id uint = (cut << 2) + uint((dict.cutoffTransforms>>(cut*6))&0x3F) AddMatch(id+transform_id*n, uint(len), l, matches) has_found_match = true } @@ -305,7 +305,7 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d transform. */ var s []byte - if !IsMatch(dictionary.words, w, data, max_length) { + if !IsMatch(dict.words, w, data, max_length) { continue } @@ -427,20 +427,20 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d /* Transforms with prefixes " " and "." */ if max_length >= 5 && (data[0] == ' ' || data[0] == '.') { var is_space bool = (data[0] == ' ') - var offset uint = uint(dictionary.buckets[Hash(data[1:])]) + var offset uint = uint(dict.buckets[Hash(data[1:])]) var end bool = offset == 0 for !end { var w DictWord - w = dictionary.dict_words[offset] + w = dict.dict_words[offset] offset++ var l uint = uint(w.len) & 0x1F - var n uint = uint(1) << dictionary.words.size_bits_by_length[l] + var n uint = uint(1) << dict.words.size_bits_by_length[l] var id uint = uint(w.idx) end = !(w.len&0x80 == 0) w.len = byte(l) if w.transform == 0 { var s []byte - if !IsMatch(dictionary.words, w, data[1:], max_length-1) { + if !IsMatch(dict.words, w, data[1:], max_length-1) { continue } @@ -506,7 +506,7 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d transform. */ var s []byte - if !IsMatch(dictionary.words, w, data[1:], max_length-1) { + if !IsMatch(dict.words, w, data[1:], max_length-1) { continue } @@ -592,18 +592,18 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d if max_length >= 6 { /* Transforms with prefixes "e ", "s ", ", " and "\xC2\xA0" */ if (data[1] == ' ' && (data[0] == 'e' || data[0] == 's' || data[0] == ',')) || (data[0] == 0xC2 && data[1] == 0xA0) { - var offset uint = uint(dictionary.buckets[Hash(data[2:])]) + var offset uint = uint(dict.buckets[Hash(data[2:])]) var end bool = offset == 0 for !end { var w DictWord - w = dictionary.dict_words[offset] + w = dict.dict_words[offset] offset++ var l uint = uint(w.len) & 0x1F - var n uint = uint(1) << dictionary.words.size_bits_by_length[l] + var n uint = uint(1) << dict.words.size_bits_by_length[l] var id uint = uint(w.idx) end = !(w.len&0x80 == 0) w.len = byte(l) - if w.transform == 0 && IsMatch(dictionary.words, w, data[2:], max_length-2) { + if w.transform == 0 && IsMatch(dict.words, w, data[2:], max_length-2) { if data[0] == 0xC2 { AddMatch(id+102*n, l+2, l, matches) has_found_match = true @@ -625,18 +625,18 @@ func BrotliFindAllStaticDictionaryMatches(dictionary *BrotliEncoderDictionary, d if max_length >= 9 { /* Transforms with prefixes " the " and ".com/" */ if (data[0] == ' ' && data[1] == 't' && data[2] == 'h' && data[3] == 'e' && data[4] == ' ') || (data[0] == '.' && data[1] == 'c' && data[2] == 'o' && data[3] == 'm' && data[4] == '/') { - var offset uint = uint(dictionary.buckets[Hash(data[5:])]) + var offset uint = uint(dict.buckets[Hash(data[5:])]) var end bool = offset == 0 for !end { var w DictWord - w = dictionary.dict_words[offset] + w = dict.dict_words[offset] offset++ var l uint = uint(w.len) & 0x1F - var n uint = uint(1) << dictionary.words.size_bits_by_length[l] + var n uint = uint(1) << dict.words.size_bits_by_length[l] var id uint = uint(w.idx) end = !(w.len&0x80 == 0) w.len = byte(l) - if w.transform == 0 && IsMatch(dictionary.words, w, data[5:], max_length-5) { + if w.transform == 0 && IsMatch(dict.words, w, data[5:], max_length-5) { var tmp int if data[0] == ' ' { tmp = 41 diff --git a/writer.go b/writer.go index 8bb32c2..2be9021 100644 --- a/writer.go +++ b/writer.go @@ -23,7 +23,7 @@ var ( // NewWriter initializes new Writer instance. func NewWriter(dst io.Writer, options WriterOptions) *Writer { w := new(Writer) - BrotliEncoderInitState(w) + encoderInitState(w) w.params.quality = options.Quality if options.LGWin > 0 { w.params.lgwin = uint(options.LGWin) @@ -40,7 +40,7 @@ func (w *Writer) writeChunk(p []byte, op int) (n int, err error) { for { availableIn := uint(len(p)) nextIn := p - success := BrotliEncoderCompressStream(w, op, &availableIn, &nextIn) + success := encoderCompressStream(w, op, &availableIn, &nextIn) bytesConsumed := len(p) - int(availableIn) p = p[bytesConsumed:] n += bytesConsumed @@ -48,7 +48,7 @@ func (w *Writer) writeChunk(p []byte, op int) (n int, err error) { return n, errEncode } - outputData := BrotliEncoderTakeOutput(w) + outputData := encoderTakeOutput(w) if len(outputData) > 0 { _, err = w.dst.Write(outputData) @@ -67,14 +67,14 @@ func (w *Writer) writeChunk(p []byte, op int) (n int, err error) { // not yet complete until after Close. // Flush has a negative impact on compression. func (w *Writer) Flush() error { - _, err := w.writeChunk(nil, BROTLI_OPERATION_FLUSH) + _, err := w.writeChunk(nil, operationFlush) return err } // Close flushes remaining data to the decorated writer. func (w *Writer) Close() error { // If stream is already closed, it is reported by `writeChunk`. - _, err := w.writeChunk(nil, BROTLI_OPERATION_FINISH) + _, err := w.writeChunk(nil, operationFinish) w.dst = nil return err } @@ -82,5 +82,5 @@ func (w *Writer) Close() error { // Write implements io.Writer. Flush or Close must be called to ensure that the // encoded bytes are actually flushed to the underlying Writer. func (w *Writer) Write(p []byte) (n int, err error) { - return w.writeChunk(p, BROTLI_OPERATION_PROCESS) + return w.writeChunk(p, operationProcess) }