-
Notifications
You must be signed in to change notification settings - Fork 16
/
byte_stream_in.go
177 lines (137 loc) · 4.6 KB
/
byte_stream_in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package nex
import (
"errors"
crunch "github.com/superwhiskers/crunch/v3"
)
// ByteStreamIn is an input stream abstraction of github.com/superwhiskers/crunch/v3 with nex type support
type ByteStreamIn struct {
*crunch.Buffer
LibraryVersions *LibraryVersions
Settings *ByteStreamSettings
}
// StringLengthSize returns the expected size of String length fields
func (bsi *ByteStreamIn) StringLengthSize() int {
size := 2
if bsi.Settings != nil {
size = bsi.Settings.StringLengthSize
}
return size
}
// PIDSize returns the size of PID types
func (bsi *ByteStreamIn) PIDSize() int {
size := 4
if bsi.Settings != nil {
size = bsi.Settings.PIDSize
}
return size
}
// UseStructureHeader determines if Structure headers should be used
func (bsi *ByteStreamIn) UseStructureHeader() bool {
useStructureHeader := false
if bsi.Settings != nil {
useStructureHeader = bsi.Settings.UseStructureHeader
}
return useStructureHeader
}
// Remaining returns the amount of data left to be read in the buffer
func (bsi *ByteStreamIn) Remaining() uint64 {
return uint64(len(bsi.Bytes()[bsi.ByteOffset():]))
}
// ReadRemaining reads all the data left to be read in the buffer
func (bsi *ByteStreamIn) ReadRemaining() []byte {
// * Can safely ignore this error, since bsi.Remaining() will never be less than itself
remaining, _ := bsi.Read(uint64(bsi.Remaining()))
return remaining
}
// Read reads the specified number of bytes. Returns an error if OOB
func (bsi *ByteStreamIn) Read(length uint64) ([]byte, error) {
if bsi.Remaining() < length {
return []byte{}, errors.New("Read is OOB")
}
return bsi.ReadBytesNext(int64(length)), nil
}
// ReadPrimitiveUInt8 reads a uint8
func (bsi *ByteStreamIn) ReadPrimitiveUInt8() (uint8, error) {
if bsi.Remaining() < 1 {
return 0, errors.New("Not enough data to read uint8")
}
return uint8(bsi.ReadByteNext()), nil
}
// ReadPrimitiveUInt16LE reads a Little-Endian encoded uint16
func (bsi *ByteStreamIn) ReadPrimitiveUInt16LE() (uint16, error) {
if bsi.Remaining() < 2 {
return 0, errors.New("Not enough data to read uint16")
}
return bsi.ReadU16LENext(1)[0], nil
}
// ReadPrimitiveUInt32LE reads a Little-Endian encoded uint32
func (bsi *ByteStreamIn) ReadPrimitiveUInt32LE() (uint32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read uint32")
}
return bsi.ReadU32LENext(1)[0], nil
}
// ReadPrimitiveUInt64LE reads a Little-Endian encoded uint64
func (bsi *ByteStreamIn) ReadPrimitiveUInt64LE() (uint64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read uint64")
}
return bsi.ReadU64LENext(1)[0], nil
}
// ReadPrimitiveInt8 reads a uint8
func (bsi *ByteStreamIn) ReadPrimitiveInt8() (int8, error) {
if bsi.Remaining() < 1 {
return 0, errors.New("Not enough data to read int8")
}
return int8(bsi.ReadByteNext()), nil
}
// ReadPrimitiveInt16LE reads a Little-Endian encoded int16
func (bsi *ByteStreamIn) ReadPrimitiveInt16LE() (int16, error) {
if bsi.Remaining() < 2 {
return 0, errors.New("Not enough data to read int16")
}
return int16(bsi.ReadU16LENext(1)[0]), nil
}
// ReadPrimitiveInt32LE reads a Little-Endian encoded int32
func (bsi *ByteStreamIn) ReadPrimitiveInt32LE() (int32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read int32")
}
return int32(bsi.ReadU32LENext(1)[0]), nil
}
// ReadPrimitiveInt64LE reads a Little-Endian encoded int64
func (bsi *ByteStreamIn) ReadPrimitiveInt64LE() (int64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read int64")
}
return int64(bsi.ReadU64LENext(1)[0]), nil
}
// ReadPrimitiveFloat32LE reads a Little-Endian encoded float32
func (bsi *ByteStreamIn) ReadPrimitiveFloat32LE() (float32, error) {
if bsi.Remaining() < 4 {
return 0, errors.New("Not enough data to read float32")
}
return bsi.ReadF32LENext(1)[0], nil
}
// ReadPrimitiveFloat64LE reads a Little-Endian encoded float64
func (bsi *ByteStreamIn) ReadPrimitiveFloat64LE() (float64, error) {
if bsi.Remaining() < 8 {
return 0, errors.New("Not enough data to read float64")
}
return bsi.ReadF64LENext(1)[0], nil
}
// ReadPrimitiveBool reads a bool
func (bsi *ByteStreamIn) ReadPrimitiveBool() (bool, error) {
if bsi.Remaining() < 1 {
return false, errors.New("Not enough data to read bool")
}
return bsi.ReadByteNext() == 1, nil
}
// NewByteStreamIn returns a new NEX input byte stream
func NewByteStreamIn(data []byte, libraryVersions *LibraryVersions, settings *ByteStreamSettings) *ByteStreamIn {
return &ByteStreamIn{
Buffer: crunch.NewBuffer(data),
LibraryVersions: libraryVersions,
Settings: settings,
}
}