forked from teamgram/marmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_input.go
187 lines (169 loc) · 3.81 KB
/
buffer_input.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
// Copyright 2022 Teamgram Authors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: teamgramio (teamgram.io@gmail.com)
//
package utils
import (
"encoding/binary"
"encoding/hex"
"io"
"math"
)
type BufferInput struct {
buf []byte
off int
size int
err error
}
func NewBufferInput(buf []byte) *BufferInput {
return &BufferInput{buf, 0, len(buf), nil}
}
func (m *BufferInput) Error() error {
return m.err
}
func (m *BufferInput) Buf() ([]byte, error) {
if m.err != nil {
return nil, m.err
}
return m.buf[m.off:], nil
}
func (m *BufferInput) Byte() byte {
if m.err != nil {
return 0
}
if m.off+1 > m.size {
// logx.Errorf("decode byte error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := m.buf[m.off]
m.off += 1
return x
}
func (m *BufferInput) Int16() int16 {
if m.err != nil {
return 0
}
if m.off+2 > m.size {
// logx.Errorf("decode int16 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := binary.LittleEndian.Uint16(m.buf[m.off : m.off+2])
m.off += 2
return int16(x)
}
func (m *BufferInput) UInt16() uint16 {
if m.err != nil {
return 0
}
if m.off+2 > m.size {
// log.Errorf("decode uint16 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := binary.LittleEndian.Uint16(m.buf[m.off : m.off+2])
m.off += 2
return x
}
func (m *BufferInput) Int32() int32 {
if m.err != nil {
return 0
}
if m.off+4 > m.size {
// log.Errorf("decode int32 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := binary.LittleEndian.Uint32(m.buf[m.off : m.off+4])
m.off += 4
return int32(x)
}
func (m *BufferInput) UInt32() uint32 {
if m.err != nil {
return 0
}
if m.off+4 > m.size {
// log.Errorf("decode uint32 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := binary.LittleEndian.Uint32(m.buf[m.off : m.off+4])
m.off += 4
return x
}
func (m *BufferInput) UInt64() uint64 {
if m.err != nil {
return 0
}
if m.off+8 > m.size {
// log.Errorf("decode uint64 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := binary.LittleEndian.Uint64(m.buf[m.off : m.off+8])
m.off += 8
return x
}
func (m *BufferInput) Int64() int64 {
if m.err != nil {
return 0
}
if m.off+8 > m.size {
// log.Errorf("decode int64 error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := int64(binary.LittleEndian.Uint64(m.buf[m.off : m.off+8]))
m.off += 8
return x
}
func (m *BufferInput) Double() float64 {
if m.err != nil {
return 0
}
if m.off+8 > m.size {
// log.Errorf("decode double error - {off: %d, size: %d}", m.off, m.size)
m.err = io.EOF
return 0
}
x := math.Float64frombits(binary.LittleEndian.Uint64(m.buf[m.off : m.off+8]))
m.off += 8
return x
}
func (m *BufferInput) Bytes(size int) []byte {
if m.err != nil {
return nil
}
if m.off+size > m.size {
// log.Errorf("decode size_bytes(%d) error - {off: %d, size: %d}", size, m.off, m.size)
m.err = io.EOF
return nil
}
x := make([]byte, size)
copy(x, m.buf[m.off:m.off+size])
m.off += size
return x
}
func (m *BufferInput) DumpSize(size int) string {
if m.off+size > m.size {
size = m.size - m.off
}
return hex.Dump(m.buf[m.off : m.off+size])
}
func (m *BufferInput) Dump() string {
return hex.Dump(m.buf[m.off:m.size])
}