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} 50