diff options
Diffstat (limited to 'vendor/github.com/ulikunitz/xz/lzma/properties.go')
-rw-r--r-- | vendor/github.com/ulikunitz/xz/lzma/properties.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/ulikunitz/xz/lzma/properties.go b/vendor/github.com/ulikunitz/xz/lzma/properties.go new file mode 100644 index 0000000..23418e2 --- /dev/null +++ b/vendor/github.com/ulikunitz/xz/lzma/properties.go | |||
@@ -0,0 +1,69 @@ | |||
1 | // Copyright 2014-2017 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. | ||
4 | |||
5 | package lzma | ||
6 | |||
7 | import ( | ||
8 | "errors" | ||
9 | "fmt" | ||
10 | ) | ||
11 | |||
12 | // maximum and minimum values for the LZMA properties. | ||
13 | const ( | ||
14 | minPB = 0 | ||
15 | maxPB = 4 | ||
16 | ) | ||
17 | |||
18 | // maxPropertyCode is the possible maximum of a properties code byte. | ||
19 | const maxPropertyCode = (maxPB+1)*(maxLP+1)*(maxLC+1) - 1 | ||
20 | |||
21 | // Properties contains the parameters LC, LP and PB. The parameter LC | ||
22 | // defines the number of literal context bits; parameter LP the number | ||
23 | // of literal position bits and PB the number of position bits. | ||
24 | type Properties struct { | ||
25 | LC int | ||
26 | LP int | ||
27 | PB int | ||
28 | } | ||
29 | |||
30 | // String returns the properties in a string representation. | ||
31 | func (p *Properties) String() string { | ||
32 | return fmt.Sprintf("LC %d LP %d PB %d", p.LC, p.LP, p.PB) | ||
33 | } | ||
34 | |||
35 | // PropertiesForCode converts a properties code byte into a Properties value. | ||
36 | func PropertiesForCode(code byte) (p Properties, err error) { | ||
37 | if code > maxPropertyCode { | ||
38 | return p, errors.New("lzma: invalid properties code") | ||
39 | } | ||
40 | p.LC = int(code % 9) | ||
41 | code /= 9 | ||
42 | p.LP = int(code % 5) | ||
43 | code /= 5 | ||
44 | p.PB = int(code % 5) | ||
45 | return p, err | ||
46 | } | ||
47 | |||
48 | // verify checks the properties for correctness. | ||
49 | func (p *Properties) verify() error { | ||
50 | if p == nil { | ||
51 | return errors.New("lzma: properties are nil") | ||
52 | } | ||
53 | if !(minLC <= p.LC && p.LC <= maxLC) { | ||
54 | return errors.New("lzma: lc out of range") | ||
55 | } | ||
56 | if !(minLP <= p.LP && p.LP <= maxLP) { | ||
57 | return errors.New("lzma: lp out of range") | ||
58 | } | ||
59 | if !(minPB <= p.PB && p.PB <= maxPB) { | ||
60 | return errors.New("lzma: pb out of range") | ||
61 | } | ||
62 | return nil | ||
63 | } | ||
64 | |||
65 | // Code converts the properties to a byte. The function assumes that | ||
66 | // the properties components are all in range. | ||
67 | func (p Properties) Code() byte { | ||
68 | return byte((p.PB*5+p.LP)*9 + p.LC) | ||
69 | } | ||