2019-10-25 16:02:31 -04:00
|
|
|
package Common
|
|
|
|
|
2019-10-25 17:09:07 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2019-10-26 20:09:33 -04:00
|
|
|
"log"
|
2019-10-25 17:09:07 -04:00
|
|
|
)
|
|
|
|
|
2019-10-25 16:02:31 -04:00
|
|
|
// StreamReader allows you to read data from a byte array in various formats
|
|
|
|
type StreamReader struct {
|
|
|
|
data []byte
|
|
|
|
position uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateStreamReader creates an instance of the stream reader
|
|
|
|
func CreateStreamReader(source []byte) *StreamReader {
|
|
|
|
result := &StreamReader{
|
|
|
|
data: source,
|
|
|
|
position: 0,
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPosition returns the current stream position
|
|
|
|
func (v *StreamReader) GetPosition() uint64 {
|
|
|
|
return v.position
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSize returns the total size of the stream in bytes
|
|
|
|
func (v *StreamReader) GetSize() uint64 {
|
|
|
|
return uint64(len(v.data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByte returns a byte from the stream
|
|
|
|
func (v *StreamReader) GetByte() byte {
|
|
|
|
result := v.data[v.position]
|
|
|
|
v.position++
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
// GetUInt16 returns a uint16 word from the stream
|
|
|
|
func (v *StreamReader) GetUInt16() uint16 {
|
2019-10-25 16:02:31 -04:00
|
|
|
result := uint16(v.data[v.position])
|
2019-10-26 20:09:33 -04:00
|
|
|
result += uint16(v.data[v.position+1]) << 8
|
2019-10-25 16:02:31 -04:00
|
|
|
v.position += 2
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
// GetInt16 returns a int16 word from the stream
|
|
|
|
func (v *StreamReader) GetInt16() int16 {
|
2019-10-25 17:09:07 -04:00
|
|
|
var result int16
|
2019-10-26 20:09:33 -04:00
|
|
|
err := binary.Read(bytes.NewReader([]byte{v.data[v.position], v.data[v.position+1]}), binary.LittleEndian, &result)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2019-10-25 17:09:07 -04:00
|
|
|
v.position += 2
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
func (v *StreamReader) SetPosition(newPosition uint64) {
|
|
|
|
v.position = newPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUInt32 returns a uint32 word from the stream
|
|
|
|
func (v *StreamReader) GetUInt32() uint32 {
|
|
|
|
var result uint32
|
|
|
|
err := binary.Read(bytes.NewReader(
|
|
|
|
[]byte{
|
|
|
|
v.data[v.position],
|
|
|
|
v.data[v.position+1],
|
|
|
|
v.data[v.position+2],
|
|
|
|
v.data[v.position+3],
|
|
|
|
}), binary.LittleEndian, &result)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2019-10-25 16:02:31 -04:00
|
|
|
v.position += 4
|
|
|
|
return result
|
|
|
|
}
|
2019-10-25 17:09:07 -04:00
|
|
|
|
2019-10-27 21:35:16 -04:00
|
|
|
// GetInt32 returns an int32 word from the stream
|
|
|
|
func (v *StreamReader) GetInt32() int32 {
|
|
|
|
var result int32
|
|
|
|
err := binary.Read(bytes.NewReader(
|
|
|
|
[]byte{
|
|
|
|
v.data[v.position],
|
|
|
|
v.data[v.position+1],
|
|
|
|
v.data[v.position+2],
|
|
|
|
v.data[v.position+3],
|
|
|
|
}), binary.LittleEndian, &result)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
v.position += 4
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-25 17:09:07 -04:00
|
|
|
// ReadByte implements io.ByteReader
|
|
|
|
func (v *StreamReader) ReadByte() (byte, error) {
|
|
|
|
return v.GetByte(), nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 17:24:21 -04:00
|
|
|
// ReadBytes reads multiple bytes
|
|
|
|
func (v *StreamReader) ReadBytes(count int) ([]byte, error) {
|
|
|
|
result := make([]byte, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
result[i] = v.GetByte()
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-10-28 17:51:17 -04:00
|
|
|
func (v *StreamReader) SkipBytes(count int) {
|
|
|
|
v.position += uint64(count)
|
|
|
|
}
|
|
|
|
|
2019-10-25 17:09:07 -04:00
|
|
|
// Read implements io.Reader
|
|
|
|
func (v *StreamReader) Read(p []byte) (n int, err error) {
|
|
|
|
streamLength := v.GetSize()
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
if v.GetPosition() >= streamLength {
|
|
|
|
return i, io.EOF
|
|
|
|
}
|
|
|
|
if i >= len(p) {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
p[i] = v.GetByte()
|
|
|
|
}
|
|
|
|
}
|
2019-10-27 17:24:21 -04:00
|
|
|
|
|
|
|
func (v *StreamReader) Eof() bool {
|
|
|
|
return v.position >= uint64(len(v.data))
|
|
|
|
}
|