summaryrefslogtreecommitdiff
path: root/vendor/github.com/ulikunitz/xz/lzma/properties.go
blob: f229fc9fe8bad562c270c2c3ae32c2a5c7c4c639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2014-2021 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package lzma

import (
	"errors"
	"fmt"
)

// maximum and minimum values for the LZMA properties.
const (
	minPB = 0
	maxPB = 4
)

// maxPropertyCode is the possible maximum of a properties code byte.
const maxPropertyCode = (maxPB+1)*(maxLP+1)*(maxLC+1) - 1

// Properties contains the parameters LC, LP and PB. The parameter LC
// defines the number of literal context bits; parameter LP the number
// of literal position bits and PB the number of position bits.
type Properties struct {
	LC int
	LP int
	PB int
}

// String returns the properties in a string representation.
func (p *Properties) String() string {
	return fmt.Sprintf("LC %d LP %d PB %d", p.LC, p.LP, p.PB)
}

// PropertiesForCode converts a properties code byte into a Properties value.
func PropertiesForCode(code byte) (p Properties, err error) {
	if code > maxPropertyCode {
		return p, errors.New("lzma: invalid properties code")
	}
	p.LC = int(code % 9)
	code /= 9
	p.LP = int(code % 5)
	code /= 5
	p.PB = int(code % 5)
	return p, err
}

// verify checks the properties for correctness.
func (p *Properties) verify() error {
	if p == nil {
		return errors.New("lzma: properties are nil")
	}
	if !(minLC <= p.LC && p.LC <= maxLC) {
		return errors.New("lzma: lc out of range")
	}
	if !(minLP <= p.LP && p.LP <= maxLP) {
		return errors.New("lzma: lp out of range")
	}
	if !(minPB <= p.PB && p.PB <= maxPB) {
		return errors.New("lzma: pb out of range")
	}
	return nil
}

// Code converts the properties to a byte. The function assumes that
// the properties components are all in range.
func (p Properties) Code() byte {
	return byte((p.PB*5+p.LP)*9 + p.LC)
}