-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes_iterator.go
More file actions
118 lines (100 loc) · 3 KB
/
bytes_iterator.go
File metadata and controls
118 lines (100 loc) · 3 KB
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
package dvb
import "fmt"
type BytesIterator struct {
buf []uint8
offset int
}
func newBytesIterator(buf []uint8) *BytesIterator {
return &BytesIterator{buf: buf}
}
func (i *BytesIterator) NextByte() (uint8, error) {
if len(i.buf) < i.offset+1 {
return uint8(0), fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset)
}
b := i.buf[i.offset]
i.offset++
return b, nil
}
// NextWordLE returns next U16 value with little endian byte order
func (i *BytesIterator) NextWordLE() (uint16, error) {
if len(i.buf) < i.offset+2 {
return uint16(0), fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+1)
}
b := i.buf[i.offset : i.offset+2]
i.offset += 2
return uint16(b[0]) + uint16(b[1])*256, nil
}
// NextWordBE returns next U16 value with big endian byte order
func (i *BytesIterator) NextWordBE() (uint16, error) {
if len(i.buf) < i.offset+2 {
return uint16(0), fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+1)
}
b := i.buf[i.offset : i.offset+2]
i.offset += 2
return uint16(b[0])*256 + uint16(b[1]), nil
}
// NextWords returns next n U16 values
func (i *BytesIterator) NextWordsLE(n int) ([]uint16, error) {
if len(i.buf) < i.offset+n*2 {
return []uint16{}, fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+n*2-1)
}
buf2 := make([]uint8, n*2)
copy(buf2, i.buf[i.offset:i.offset+n*2])
i.offset += n * 2
return sliceU8toU16LE(buf2), nil
}
func (i *BytesIterator) NextWordsBE(n int) ([]uint16, error) {
if len(i.buf) < i.offset+n*2 {
return []uint16{}, fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+n*2-1)
}
buf2 := make([]uint8, n*2)
copy(buf2, i.buf[i.offset:i.offset+n*2])
i.offset += n * 2
return sliceU8toU16BE(buf2), nil
}
func (i *BytesIterator) NextBytes(n int) ([]uint8, error) {
if len(i.buf) < i.offset+n {
return []uint8{}, fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+n-1)
}
buf2 := make([]uint8, n)
copy(buf2, i.buf[i.offset:i.offset+n])
i.offset += n
return buf2, nil
}
func (i *BytesIterator) NextBytesNoCopy(n int) ([]uint8, error) {
if len(i.buf) < i.offset+n {
return []uint8{}, fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+n-1)
}
sl := i.buf[i.offset : i.offset+n]
i.offset += n
return sl, nil
}
func (i *BytesIterator) Seek(n int) {
i.offset = n
}
func (i *BytesIterator) Skip(n int) {
i.offset += n
}
func (i *BytesIterator) Revert(n int) {
i.offset -= n
}
func (i *BytesIterator) Offset() int {
return i.offset
}
func (i *BytesIterator) Len() int {
return len(i.buf)
}
func (i *BytesIterator) Buffer() []uint8 {
return i.buf
}
func (i *BytesIterator) Slice(from int, to int) []uint8 {
return i.buf[from:to]
}
func (i *BytesIterator) SliceN(n int) ([]uint8, error) {
if len(i.buf) < i.offset+n {
return []uint8{}, fmt.Errorf("slice length is %d, offset %d is invalid", len(i.buf), i.offset+n)
}
buf2 := make([]uint8, n)
copy(buf2, i.buf[i.offset:i.offset+n])
return buf2, nil
}