2020-09-08 15:58:35 -04:00
|
|
|
package d2datautils
|
2020-01-26 00:39:13 -05:00
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:07:38 -04:00
|
|
|
const (
|
2020-07-23 12:56:50 -04:00
|
|
|
maxBits = 16
|
2020-07-18 18:07:38 -04:00
|
|
|
bitsPerByte = 8
|
|
|
|
)
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// BitStream is a utility class for reading groups of bits from a stream
|
|
|
|
type BitStream struct {
|
|
|
|
data []byte
|
|
|
|
dataPosition int
|
|
|
|
current int
|
|
|
|
bitCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBitStream creates a new BitStream
|
|
|
|
func CreateBitStream(newData []byte) *BitStream {
|
|
|
|
result := &BitStream{
|
|
|
|
data: newData,
|
|
|
|
dataPosition: 0,
|
|
|
|
current: 0,
|
|
|
|
bitCount: 0,
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadBits reads the specified number of bits and returns the value
|
|
|
|
func (v *BitStream) ReadBits(bitCount int) int {
|
2020-07-18 18:07:38 -04:00
|
|
|
if bitCount > maxBits {
|
2020-01-26 00:39:13 -05:00
|
|
|
log.Panic("Maximum BitCount is 16")
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if !v.EnsureBits(bitCount) {
|
|
|
|
return -1
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2021-02-02 06:00:31 -05:00
|
|
|
// nolint:gomnd // byte expresion
|
2020-07-18 18:07:38 -04:00
|
|
|
result := v.current & (0xffff >> uint(maxBits-bitCount))
|
2020-01-26 00:39:13 -05:00
|
|
|
v.WasteBits(bitCount)
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeekByte returns the current byte without adjusting the position
|
|
|
|
func (v *BitStream) PeekByte() int {
|
2020-07-18 18:07:38 -04:00
|
|
|
if !v.EnsureBits(bitsPerByte) {
|
2020-01-26 00:39:13 -05:00
|
|
|
return -1
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2021-02-02 06:00:31 -05:00
|
|
|
// nolint:gomnd // byte
|
2020-01-26 00:39:13 -05:00
|
|
|
return v.current & 0xff
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureBits ensures that the specified number of bits are available
|
|
|
|
func (v *BitStream) EnsureBits(bitCount int) bool {
|
|
|
|
if bitCount <= v.bitCount {
|
|
|
|
return true
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if v.dataPosition >= len(v.data) {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
nextValue := v.data[v.dataPosition]
|
|
|
|
v.dataPosition++
|
|
|
|
v.current |= int(nextValue) << uint(v.bitCount)
|
|
|
|
v.bitCount += 8
|
2020-07-08 09:16:56 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WasteBits dry-reads the specified number of bits
|
|
|
|
func (v *BitStream) WasteBits(bitCount int) {
|
2020-07-08 09:16:56 -04:00
|
|
|
// noinspection GoRedundantConversion
|
2020-01-26 00:39:13 -05:00
|
|
|
v.current >>= uint(bitCount)
|
|
|
|
v.bitCount -= bitCount
|
|
|
|
}
|