forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixedslicereader.go
286 lines (263 loc) · 5.95 KB
/
fixedslicereader.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package bits
import (
"encoding/binary"
"errors"
"fmt"
)
// FixedSliceReader - read integers and other data from a fixed slice.
// Accumulates error, and the first error can be retrieved.
// If err != nil, 0 or empty string is returned
type FixedSliceReader struct {
err error
slice []byte
pos int
len int
}
// bits.NewFixedSliceReader creates a new slice reader reading from data
func NewFixedSliceReader(data []byte) *FixedSliceReader {
return &FixedSliceReader{
slice: data,
pos: 0,
len: len(data),
err: nil,
}
}
// AccError - get accumulated error after read operations
func (s *FixedSliceReader) AccError() error {
return s.err
}
// ReadUint8 - read uint8 from slice
func (s *FixedSliceReader) ReadUint8() byte {
if s.err != nil {
return 0
}
if s.pos > s.len-1 {
s.err = ErrSliceRead
return 0
}
res := s.slice[s.pos]
s.pos++
return res
}
// ReadUint16 - read uint16 from slice
func (s *FixedSliceReader) ReadUint16() uint16 {
if s.err != nil {
return 0
}
if s.pos > s.len-2 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint16(s.slice[s.pos : s.pos+2])
s.pos += 2
return res
}
// ReadInt16 - read int16 from slice
func (s *FixedSliceReader) ReadInt16() int16 {
if s.err != nil {
return 0
}
if s.pos > s.len-2 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint16(s.slice[s.pos : s.pos+2])
s.pos += 2
return int16(res)
}
// ReadUint24 - read uint24 from slice
func (s *FixedSliceReader) ReadUint24() uint32 {
if s.err != nil {
return 0
}
if s.pos > s.len-3 {
s.err = ErrSliceRead
return 0
}
res := uint32(binary.BigEndian.Uint16(s.slice[s.pos : s.pos+2]))
res = res<<8 | uint32(s.slice[s.pos+2])
s.pos += 3
return res
}
// ReadUint32 - read uint32 from slice
func (s *FixedSliceReader) ReadUint32() uint32 {
if s.err != nil {
return 0
}
if s.pos > s.len-4 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint32(s.slice[s.pos : s.pos+4])
s.pos += 4
return res
}
// ReadInt32 - read int32 from slice
func (s *FixedSliceReader) ReadInt32() int32 {
if s.err != nil {
return 0
}
if s.pos > s.len-4 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint32(s.slice[s.pos : s.pos+4])
s.pos += 4
return int32(res)
}
// ReadUint64 - read uint64 from slice
func (s *FixedSliceReader) ReadUint64() uint64 {
if s.err != nil {
return 0
}
if s.pos > s.len-8 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint64(s.slice[s.pos : s.pos+8])
s.pos += 8
return res
}
// ReadInt64 - read int64 from slice
func (s *FixedSliceReader) ReadInt64() int64 {
if s.err != nil {
return 0
}
if s.pos > s.len-8 {
s.err = ErrSliceRead
return 0
}
res := binary.BigEndian.Uint64(s.slice[s.pos : s.pos+8])
s.pos += 8
return int64(res)
}
// ReadFixedLengthString - read string of specified length n.
// Sets err and returns empty string if full length not available
func (s *FixedSliceReader) ReadFixedLengthString(n int) string {
if s.err != nil {
return ""
}
if s.pos > s.len-n {
s.err = ErrSliceRead
return ""
}
res := string(s.slice[s.pos : s.pos+n])
s.pos += n
return res
}
// ReadZeroTerminatedString - read string until zero byte but at most maxLen
// Set err and return empty string if no zero byte found.
func (s *FixedSliceReader) ReadZeroTerminatedString(maxLen int) string {
if s.err != nil {
return ""
}
startPos := s.pos
maxPos := startPos + maxLen
if maxPos > s.len {
maxPos = s.len
}
for {
if s.pos >= maxPos {
s.err = errors.New("did not find terminating zero")
return ""
}
c := s.slice[s.pos]
if c == 0 {
str := string(s.slice[startPos:s.pos])
s.pos++ // Next position to read
return str
}
s.pos++
}
}
// ReadPossiblyZeroTerminatedString - read string until zero byte but at most maxLen.
// If maxLen is reached and no zero-byte, return string and ok = false
func (s *FixedSliceReader) ReadPossiblyZeroTerminatedString(maxLen int) (str string, ok bool) {
startPos := s.pos
maxPos := startPos + maxLen
for {
if s.pos == maxPos {
return string(s.slice[startPos:s.pos]), true
}
if s.pos > maxPos {
s.err = errors.New("did not find terminating zero")
return "", false
}
c := s.slice[s.pos]
if c == 0 {
str = string(s.slice[startPos:s.pos])
s.pos++ // Next position to read
return str, true
}
s.pos++
}
}
// ReadBytes - read a slice of n bytes
// Return empty slice if n bytes not available
func (s *FixedSliceReader) ReadBytes(n int) []byte {
if n < 0 {
s.err = fmt.Errorf("attempt to read negative number of bytes: %d", n)
return []byte{}
}
if s.err != nil {
return []byte{}
}
if s.pos > s.len-n {
s.err = ErrSliceRead
return []byte{}
}
res := s.slice[s.pos : s.pos+n]
s.pos += n
return res
}
// RemainingBytes - return remaining bytes of this slice
func (s *FixedSliceReader) RemainingBytes() []byte {
if s.err != nil {
return []byte{}
}
res := s.slice[s.pos:]
s.pos = s.Length()
return res
}
// NrRemainingBytes - return number of bytes remaining
func (s *FixedSliceReader) NrRemainingBytes() int {
if s.err != nil {
return 0
}
return s.Length() - s.GetPos()
}
// SkipBytes - skip passed n bytes
func (s *FixedSliceReader) SkipBytes(n int) {
if s.err != nil {
return
}
if s.pos+n > s.Length() {
s.err = fmt.Errorf("attempt to skip bytes to pos %d beyond slice len %d", s.pos+n, s.len)
return
}
s.pos += n
}
// SetPos - set read position is slice
func (s *FixedSliceReader) SetPos(pos int) {
if pos > s.len {
s.err = fmt.Errorf("attempt to set pos %d beyond slice len %d", pos, s.len)
return
}
s.pos = pos
}
// GetPos - get read position is slice
func (s *FixedSliceReader) GetPos() int {
return s.pos
}
// Length - get length of slice
func (s *FixedSliceReader) Length() int {
return s.len
}
// LookAhead returns data ahead of current pos if within bounds.
func (s *FixedSliceReader) LookAhead(offset int, data []byte) error {
if s.pos+offset+len(data) > s.len {
return fmt.Errorf("out of bounds")
}
copy(data, s.slice[s.pos+offset:])
return nil
}