1
// Copyright 2014-2016 Ulrich Kunitz. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
9
// maxPosBits defines the number of bits of the position value that are used to
10
// to compute the posState value. The value is used to select the tree codec
11
// for length encoding and decoding.
14
// minMatchLen and maxMatchLen give the minimum and maximum values for
15
// encoding and decoding length values. minMatchLen is also used as base
16
// for the encoded length values.
19
maxMatchLen = minMatchLen + 16 + 256 - 1
22
// lengthCodec support the encoding of the length value.
23
type lengthCodec struct {
25
low [1 << maxPosBits]treeCodec
26
mid [1 << maxPosBits]treeCodec
30
// deepcopy initializes the lc value as deep copy of the source value.
31
func (lc *lengthCodec) deepcopy(src *lengthCodec) {
35
lc.choice = src.choice
36
for i := range lc.low {
37
lc.low[i].deepcopy(&src.low[i])
39
for i := range lc.mid {
40
lc.mid[i].deepcopy(&src.mid[i])
42
lc.high.deepcopy(&src.high)
45
// init initializes a new length codec.
46
func (lc *lengthCodec) init() {
47
for i := range lc.choice {
48
lc.choice[i] = probInit
50
for i := range lc.low {
51
lc.low[i] = makeTreeCodec(3)
53
for i := range lc.mid {
54
lc.mid[i] = makeTreeCodec(3)
56
lc.high = makeTreeCodec(8)
59
// lBits gives the number of bits used for the encoding of the l value
60
// provided to the range encoder.
61
func lBits(l uint32) int {
72
// Encode encodes the length offset. The length offset l can be compute by
73
// subtracting minMatchLen (2) from the actual length.
75
// l = length - minMatchLen
77
func (lc *lengthCodec) Encode(e *rangeEncoder, l uint32, posState uint32,
79
if l > maxMatchLen-minMatchLen {
80
return errors.New("lengthCodec.Encode: l out of range")
83
if err = lc.choice[0].Encode(e, 0); err != nil {
86
return lc.low[posState].Encode(e, l)
88
if err = lc.choice[0].Encode(e, 1); err != nil {
92
if err = lc.choice[1].Encode(e, 0); err != nil {
95
return lc.mid[posState].Encode(e, l-8)
97
if err = lc.choice[1].Encode(e, 1); err != nil {
100
if err = lc.high.Encode(e, l-16); err != nil {
106
// Decode reads the length offset. Add minMatchLen to compute the actual length
107
// to the length offset l.
108
func (lc *lengthCodec) Decode(d *rangeDecoder, posState uint32,
109
) (l uint32, err error) {
111
if b, err = lc.choice[0].Decode(d); err != nil {
115
l, err = lc.low[posState].Decode(d)
118
if b, err = lc.choice[1].Decode(d); err != nil {
122
l, err = lc.mid[posState].Decode(d)
126
l, err = lc.high.Decode(d)