2020-01-26 00:39:13 -05:00
|
|
|
package d2common
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
|
|
|
// BitMuncher is used for parsing files that are not byte-aligned such as the DCC files.
|
2020-01-26 00:39:13 -05:00
|
|
|
type BitMuncher struct {
|
|
|
|
data []byte
|
2020-06-29 20:21:09 -04:00
|
|
|
offset int
|
|
|
|
bitsRead int
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
const (
|
|
|
|
twosComplimentNegativeOne = 4294967295
|
|
|
|
byteLen = 8
|
|
|
|
oneBit = 0x01
|
|
|
|
fourBytes = byteLen * 4
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateBitMuncher Creates a BitMuncher
|
|
|
|
func CreateBitMuncher(data []byte, offset int) d2interface.BitMuncher {
|
|
|
|
return (&BitMuncher{}).Init(data, offset)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// CopyBitMuncher Creates a copy of the source BitMuncher
|
|
|
|
func CopyBitMuncher(source d2interface.BitMuncher) d2interface.BitMuncher {
|
|
|
|
return source.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the BitMuncher with data and an offset
|
|
|
|
func (v *BitMuncher) Init(data []byte, offset int) d2interface.BitMuncher {
|
|
|
|
v.data = data
|
|
|
|
v.offset = offset
|
|
|
|
v.bitsRead = 0
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of a BitMuncher
|
|
|
|
func (v BitMuncher) Copy() d2interface.BitMuncher {
|
|
|
|
v.bitsRead = 0
|
|
|
|
return &v
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// Offset returns the offset of the BitMuncher
|
|
|
|
func (v *BitMuncher) Offset() int {
|
|
|
|
return v.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset sets the offset of the BitMuncher
|
|
|
|
func (v *BitMuncher) SetOffset(n int) {
|
|
|
|
v.offset = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// BitsRead returns the number of bits the BitMuncher has read
|
|
|
|
func (v *BitMuncher) BitsRead() int {
|
|
|
|
return v.bitsRead
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBitsRead sets the number of bits the BitMuncher has read
|
|
|
|
func (v *BitMuncher) SetBitsRead(n int) {
|
|
|
|
v.bitsRead = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBit reads a bit and returns it as uint32
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetBit() uint32 {
|
2020-06-29 20:21:09 -04:00
|
|
|
result := uint32(v.data[v.offset/byteLen]>>uint(v.offset%byteLen)) & oneBit
|
|
|
|
v.offset++
|
|
|
|
v.bitsRead++
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// SkipBits skips bits, incrementing the offset and bits read
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) SkipBits(bits int) {
|
2020-06-29 20:21:09 -04:00
|
|
|
v.offset += bits
|
|
|
|
v.bitsRead += bits
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// GetByte reads a byte from data
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetByte() byte {
|
2020-06-29 20:21:09 -04:00
|
|
|
return byte(v.GetBits(byteLen))
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// GetInt32 reads an int32 from data
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetInt32() int32 {
|
2020-06-29 20:21:09 -04:00
|
|
|
return v.MakeSigned(v.GetBits(fourBytes), fourBytes)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// GetUInt32 reads an unsigned uint32 from data
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetUInt32() uint32 {
|
2020-06-29 20:21:09 -04:00
|
|
|
return v.GetBits(fourBytes)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// GetBits given a number of bits to read, reads that number of
|
|
|
|
// bits and retruns as a uint32
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetBits(bits int) uint32 {
|
|
|
|
if bits == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2020-06-29 20:21:09 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
result := uint32(0)
|
|
|
|
for i := 0; i < bits; i++ {
|
|
|
|
result |= v.GetBit() << uint(i)
|
|
|
|
}
|
2020-06-29 20:21:09 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// GetSignedBits Given a number of bits, reads that many bits and returns as int
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) GetSignedBits(bits int) int {
|
|
|
|
return int(v.MakeSigned(v.GetBits(bits), bits))
|
|
|
|
}
|
|
|
|
|
2020-06-29 20:21:09 -04:00
|
|
|
// MakeSigned converts a uint32 value into an int32
|
2020-01-26 00:39:13 -05:00
|
|
|
func (v *BitMuncher) MakeSigned(value uint32, bits int) int32 {
|
|
|
|
if bits == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// If its a single bit, a value of 1 is -1 automagically
|
|
|
|
if bits == 1 {
|
|
|
|
return -int32(value)
|
|
|
|
}
|
|
|
|
// If there is no sign bit, return the value as is
|
|
|
|
if (value & (1 << uint(bits-1))) == 0 {
|
|
|
|
return int32(value)
|
|
|
|
}
|
2020-06-29 20:21:09 -04:00
|
|
|
|
|
|
|
// We need to extend the signed bit out so that the negative value
|
|
|
|
// representation still works with the 2s compliment rule.
|
|
|
|
result := uint32(twosComplimentNegativeOne)
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
for i := byte(0); i < byte(bits); i++ {
|
|
|
|
if ((value >> uint(i)) & 1) == 0 {
|
|
|
|
result -= uint32(1 << uint(i))
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 20:21:09 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// Force casting to a signed value
|
|
|
|
return int32(result)
|
|
|
|
}
|