-
Notifications
You must be signed in to change notification settings - Fork 6
/
bytereader.go
68 lines (57 loc) · 1.29 KB
/
bytereader.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package dtls
import (
"bytes"
"encoding/binary"
"io"
)
type byteReader struct {
rdr io.Reader
ofs int
}
func newByteReader(data []byte) *byteReader {
return &byteReader{rdr: bytes.NewReader(data), ofs: 0}
}
func (r *byteReader) GetUint8() uint8 {
raw := make([]byte, 1)
_, _ = r.rdr.Read(raw)
r.ofs += 1
return raw[0]
}
func (r *byteReader) GetUint16() uint16 {
raw := make([]byte, 2)
_, _ = r.rdr.Read(raw)
r.ofs += 2
return binary.BigEndian.Uint16(raw)
}
func (r *byteReader) GetUint24() uint32 {
raw := make([]byte, 3, 4)
_, _ = r.rdr.Read(raw)
raw = append(raw, 0x00)
u24 := binary.BigEndian.Uint32(raw)
r.ofs += 3
return u24 >> 8
}
func (r *byteReader) GetUint32() uint32 {
raw := make([]byte, 4)
_, _ = r.rdr.Read(raw)
r.ofs += 4
return binary.BigEndian.Uint32(raw)
}
func (r *byteReader) GetUint64() uint64 {
raw := make([]byte, 8)
_, _ = r.rdr.Read(raw)
r.ofs += 8
return binary.BigEndian.Uint64(raw)
}
func (r *byteReader) GetBytes(l int) []byte {
raw := make([]byte, l)
_, _ = r.rdr.Read(raw)
r.ofs += l
return raw
}
func (r *byteReader) Offset() int {
return r.ofs
}