-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicereader.go
142 lines (122 loc) · 3.03 KB
/
slicereader.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
package mp4
import (
"encoding/binary"
"errors"
)
// SliceReader - read integers from a slice
type SliceReader struct {
slice []byte
pos int
}
// NewSliceReader - create a new slice reader reading from data
func NewSliceReader(data []byte) *SliceReader {
return &SliceReader{
slice: data,
pos: 0,
}
}
// ReadUint8 - read uint8 from slice
func (s *SliceReader) ReadUint8() byte {
res := s.slice[s.pos]
s.pos++
return res
}
// ReadUint16 - read uint16 from slice
func (s *SliceReader) ReadUint16() uint16 {
res := binary.BigEndian.Uint16(s.slice[s.pos : s.pos+2])
s.pos += 2
return res
}
// ReadInt16 - read int16 from slice
func (s *SliceReader) ReadInt16() int16 {
res := binary.BigEndian.Uint16(s.slice[s.pos : s.pos+2])
s.pos += 2
return int16(res)
}
// ReadUint24 - read uint24 from slice
func (s *SliceReader) ReadUint24() uint32 {
p1 := s.ReadUint8()
p2 := s.ReadUint16()
return (uint32(p1) << 16) + uint32(p2)
}
// ReadUint32 - read uint32 from slice
func (s *SliceReader) ReadUint32() uint32 {
res := binary.BigEndian.Uint32(s.slice[s.pos : s.pos+4])
s.pos += 4
return res
}
// ReadInt32 - read int32 from slice
func (s *SliceReader) ReadInt32() int32 {
res := binary.BigEndian.Uint32(s.slice[s.pos : s.pos+4])
s.pos += 4
return int32(res)
}
// ReadUint64 - read uint64 from slice
func (s *SliceReader) ReadUint64() uint64 {
res := binary.BigEndian.Uint64(s.slice[s.pos : s.pos+8])
s.pos += 8
return res
}
// ReadInt64 - read int64 from slice
func (s *SliceReader) ReadInt64() int64 {
res := binary.BigEndian.Uint64(s.slice[s.pos : s.pos+8])
s.pos += 8
return int64(res)
}
// ReadFixedLengthString - read string of specified length
func (s *SliceReader) ReadFixedLengthString(length int) string {
res := string(s.slice[s.pos : s.pos+length])
s.pos += length
return res
}
// ReadZeroTerminatedString - read string until zero
func (s *SliceReader) ReadZeroTerminatedString() (string, error) {
startPos := s.pos
for {
c := s.slice[s.pos]
if c == 0 {
str := string(s.slice[startPos:s.pos])
s.pos++ // Next position to read
return str, nil
}
s.pos++
if s.pos >= len(s.slice) {
return "", errors.New("Did not find terminating zero")
}
}
}
// ReadBytes - read a slice of bytes
func (s *SliceReader) ReadBytes(n int) []byte {
res := s.slice[s.pos : s.pos+n]
s.pos += n
return res
}
// RemainingBytes - return remaining bytes of this slice
func (s *SliceReader) RemainingBytes() []byte {
res := s.slice[s.pos:]
s.pos = s.Length()
return res
}
// NrRemaingingByts - return number of bytes remaining
func (s *SliceReader) NrRemainingBytes() int {
return s.Length() - s.GetPos()
}
// SkipBytes - skip passed n bytes
func (s *SliceReader) SkipBytes(n int) {
if s.pos+n > s.Length() {
panic("Skipping past end of box")
}
s.pos += n
}
// SetPos - set read position is slice
func (s *SliceReader) SetPos(pos int) {
s.pos = pos
}
// GetPos - get read position is slice
func (s *SliceReader) GetPos() int {
return s.pos
}
// Length - get length of slice
func (s *SliceReader) Length() int {
return len(s.slice)
}