aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/ulikunitz/xz/lzma/directcodec.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/ulikunitz/xz/lzma/directcodec.go')
-rw-r--r--vendor/github.com/ulikunitz/xz/lzma/directcodec.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/github.com/ulikunitz/xz/lzma/directcodec.go b/vendor/github.com/ulikunitz/xz/lzma/directcodec.go
new file mode 100644
index 0000000..e08eb98
--- /dev/null
+++ b/vendor/github.com/ulikunitz/xz/lzma/directcodec.go
@@ -0,0 +1,49 @@
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
5package lzma
6
7import "fmt"
8
9// directCodec allows the encoding and decoding of values with a fixed number
10// of bits. The number of bits must be in the range [1,32].
11type directCodec byte
12
13// makeDirectCodec creates a directCodec. The function panics if the number of
14// bits is not in the range [1,32].
15func makeDirectCodec(bits int) directCodec {
16 if !(1 <= bits && bits <= 32) {
17 panic(fmt.Errorf("bits=%d out of range", bits))
18 }
19 return directCodec(bits)
20}
21
22// Bits returns the number of bits supported by this codec.
23func (dc directCodec) Bits() int {
24 return int(dc)
25}
26
27// Encode uses the range encoder to encode a value with the fixed number of
28// bits. The most-significant bit is encoded first.
29func (dc directCodec) Encode(e *rangeEncoder, v uint32) error {
30 for i := int(dc) - 1; i >= 0; i-- {
31 if err := e.DirectEncodeBit(v >> uint(i)); err != nil {
32 return err
33 }
34 }
35 return nil
36}
37
38// Decode uses the range decoder to decode a value with the given number of
39// given bits. The most-significant bit is decoded first.
40func (dc directCodec) Decode(d *rangeDecoder) (v uint32, err error) {
41 for i := int(dc) - 1; i >= 0; i-- {
42 x, err := d.DirectDecodeBit()
43 if err != nil {
44 return 0, err
45 }
46 v = (v << 1) | x
47 }
48 return v, nil
49}