/
buffer.go
214 lines (172 loc) · 4.18 KB
/
buffer.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
// nolint
// Copyright 20xx The Alipay Authors.
//
// @authors[0]: bingwu.ybw(bingwu.ybw@antfin.com|detailyang@gmail.com)
// @authors[1]: robotx(robotx@antfin.com)
//
// *Legal Disclaimer*
// Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.
// *法律免责声明*
// 关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。
//
//
package buffer
import (
"encoding/binary"
"errors"
"hash/crc32"
"unsafe"
)
var ErrNotEnough = errors.New("buffer: not enough")
type Buffer struct {
pos int
data []byte
}
func New(data []byte) *Buffer {
return &Buffer{
pos: 0,
data: data,
}
}
func (b *Buffer) Remain() int {
return len(b.data) - b.pos
}
func (b *Buffer) Check(size int) bool {
return b.pos+size <= len(b.data)
}
func (b *Buffer) Gap() int {
return cap(b.data) - len(b.data)
}
func (b *Buffer) CapTo(size int) {
b.data = append(b.data, make([]byte, size)...)
}
func (b *Buffer) Append(data []byte) *Buffer {
b.data = append(b.data, data...)
return b
}
func (b *Buffer) Peek(i int) byte {
return b.data[b.pos+i]
}
func (b *Buffer) MustAppend(size int, data []byte) []byte {
data = append(data, b.data[b.pos:b.pos+size]...)
b.pos += size
return data
}
func (b *Buffer) Uint8() (uint8, error) {
if !b.Check(1) {
return 0, ErrNotEnough
}
u8 := b.data[b.pos]
b.pos += 1
return u8, nil
}
func (b *Buffer) Uint32() (uint32, error) {
if !b.Check(4) {
return 0, ErrNotEnough
}
u32 := binary.BigEndian.Uint32(b.data[b.pos:])
b.pos += 4
return u32, nil
}
func (b *Buffer) Uint32String() (string, error) {
if !b.Check(4) {
return "", ErrNotEnough
}
var u32 uint32
b.MustUint32(&u32)
if !b.Check(int(u32)) {
return "", ErrNotEnough
}
var data []byte
b.MustRef(int(u32), &data)
return b2s(data), nil
}
func (b *Buffer) MustUint8(u8 *uint8) *Buffer {
*u8 = b.data[b.pos]
b.pos += 1
return b
}
func (b *Buffer) MustUint16(u16 *uint16) *Buffer {
// Use builtin API: It does some optimzing for compiler
*u16 = binary.BigEndian.Uint16(b.data[b.pos:])
b.pos += 2
return b
}
func (b *Buffer) MustUint32(u32 *uint32) *Buffer {
*u32 = binary.BigEndian.Uint32(b.data[b.pos:])
b.pos += 4
return b
}
func (b *Buffer) MustUint64(u64 *uint64) *Buffer {
*u64 = binary.BigEndian.Uint64(b.data[b.pos:])
b.pos += 8
return b
}
func (b *Buffer) MustTake(dst []byte) *Buffer {
copy(dst, b.data[b.pos:b.pos+len(dst)])
return b
}
func (b *Buffer) MustCopy(size int, bp []byte) *Buffer {
copy(bp, b.data[b.pos:b.pos+size])
b.pos += size
return b
}
func (b *Buffer) MustRef(size int, bp *[]byte) *Buffer {
*bp = b.data[b.pos : b.pos+size]
b.pos += size
return b
}
func (b *Buffer) MustPutUint8(u8 uint8) *Buffer {
b.data[b.pos] = u8
b.pos += 1
return b
}
func (b *Buffer) MustPutUint16(u16 uint16) *Buffer {
binary.BigEndian.PutUint16(b.data[b.pos:], u16)
b.pos += 2
return b
}
func (b *Buffer) MustPutUint32(u32 uint32) *Buffer {
binary.BigEndian.PutUint32(b.data[b.pos:], u32)
b.pos += 4
return b
}
//
//
// Go is sutpid and not way to inline below
func (b *Buffer) MustPutUint64(u64 uint64) *Buffer {
binary.BigEndian.PutUint64(b.data[b.pos:], u64)
b.pos += 8
return b
}
func (b *Buffer) MustPutBytes(data []byte) *Buffer {
copy(b.data[b.pos:b.pos+len(data)], data)
b.pos += len(data)
return b
}
func (b *Buffer) MustPutCRC32() *Buffer {
c32 := crc32.Checksum(b.data, crc32.IEEETable)
return b.MustPutUint32(c32)
}
func (b *Buffer) MustPutUint32String(s string) *Buffer {
b.MustPutUint32(uint32(len(s))).MustPutBytes(s2b(s))
return b
}
func (b *Buffer) MustSkip(n int) *Buffer {
b.pos += n
return b
}
func (b *Buffer) Pos() int {
return b.pos
}
func (b *Buffer) Bytes() []byte {
return b.data
}
// nolint
func b2s(bs []byte) string {
return *(*string)(unsafe.Pointer(&bs))
}
// nolint
func s2b(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}