forked from xelabs/go-mysqlstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
442 lines (377 loc) · 8.08 KB
/
buffer.go
File metadata and controls
442 lines (377 loc) · 8.08 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* go-mysqlstack
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/
package common
import (
"bytes"
"io"
)
var (
// ErrIOEOF used for io.EOF.
ErrIOEOF = io.EOF
)
// Buffer represents the buffer tuple.
type Buffer struct {
pos int
seek int
cap int
buf []byte
}
// NewBuffer creates a new buffer.
func NewBuffer(cap int) *Buffer {
return &Buffer{pos: 0,
cap: cap,
buf: make([]byte, cap),
}
}
// ReadBuffer used to read buffer from datas.
func ReadBuffer(b []byte) *Buffer {
return &Buffer{
buf: b,
pos: len(b),
}
}
// Reset used to reset a buffer.
func (b *Buffer) Reset(data []byte) {
b.buf = data
b.pos = len(data)
b.seek = 0
}
// Datas returns the datas of the buffer.
func (b *Buffer) Datas() []byte {
return b.buf[:b.pos]
}
// Length returns the last position of the buffer.
func (b *Buffer) Length() int {
return b.pos
}
// Seek returns the seek position of the buffer.
func (b *Buffer) Seek() int {
return b.seek
}
func (b *Buffer) extend(n int) {
if (b.pos + n) > b.cap {
// allocate double what's needed, for future growth
b.cap = (b.pos + n) * 2
t := make([]byte, b.cap)
copy(t, b.buf)
b.buf = t
}
}
// WriteU8 used to write uint8.
func (b *Buffer) WriteU8(v uint8) {
b.extend(1)
b.buf[b.pos] = v
b.pos++
}
// ReadU8 used read uint8.
func (b *Buffer) ReadU8() (v uint8, err error) {
if (b.seek + 1) > b.pos {
err = ErrIOEOF
return
}
v = uint8(b.buf[b.seek])
b.seek++
return
}
// WriteU16 used to write uint16.
func (b *Buffer) WriteU16(v uint16) {
b.extend(2)
b.buf[b.pos] = byte(v)
b.buf[b.pos+1] = byte(v >> 8)
b.pos += 2
}
// ReadU16 used to read uint16.
func (b *Buffer) ReadU16() (v uint16, err error) {
if (b.seek + 2) > b.pos {
err = ErrIOEOF
return
}
v = uint16(b.buf[b.seek]) |
uint16(b.buf[b.seek+1])<<8
b.seek += 2
return
}
// WriteU24 used to write uint24.
func (b *Buffer) WriteU24(v uint32) {
b.extend(3)
b.buf[b.pos] = byte(v)
b.buf[b.pos+1] = byte(v >> 8)
b.buf[b.pos+2] = byte(v >> 16)
b.pos += 3
}
// ReadU24 used to read uint24.
func (b *Buffer) ReadU24() (v uint32, err error) {
if (b.seek + 3) > b.pos {
err = ErrIOEOF
return
}
v = uint32(b.buf[b.seek]) |
uint32(b.buf[b.seek+1])<<8 |
uint32(b.buf[b.seek+2])<<16
b.seek += 3
return
}
// WriteU32 used to write uint32.
func (b *Buffer) WriteU32(v uint32) {
b.extend(4)
b.buf[b.pos] = byte(v)
b.buf[b.pos+1] = byte(v >> 8)
b.buf[b.pos+2] = byte(v >> 16)
b.buf[b.pos+3] = byte(v >> 24)
b.pos += 4
}
// ReadU32 used to read uint32.
func (b *Buffer) ReadU32() (v uint32, err error) {
if (b.seek + 4) > b.pos {
err = ErrIOEOF
return
}
v = uint32(b.buf[b.seek]) |
uint32(b.buf[b.seek+1])<<8 |
uint32(b.buf[b.seek+2])<<16 |
uint32(b.buf[b.seek+3])<<24
b.seek += 4
return
}
// WriteU64 used to write uint64.
func (b *Buffer) WriteU64(v uint64) {
b.extend(8)
b.buf[b.pos] = byte(v)
b.buf[b.pos+1] = byte(v >> 8)
b.buf[b.pos+2] = byte(v >> 16)
b.buf[b.pos+3] = byte(v >> 24)
b.buf[b.pos+4] = byte(v >> 32)
b.buf[b.pos+5] = byte(v >> 40)
b.buf[b.pos+6] = byte(v >> 48)
b.buf[b.pos+7] = byte(v >> 56)
b.pos += 8
}
// ReadU64 used to read uint64.
func (b *Buffer) ReadU64() (v uint64, err error) {
if (b.seek + 8) > b.pos {
err = ErrIOEOF
return
}
v = uint64(b.buf[b.seek]) |
uint64(b.buf[b.seek+1])<<8 |
uint64(b.buf[b.seek+2])<<16 |
uint64(b.buf[b.seek+3])<<24 |
uint64(b.buf[b.seek+4])<<32 |
uint64(b.buf[b.seek+5])<<40 |
uint64(b.buf[b.seek+6])<<48 |
uint64(b.buf[b.seek+7])<<56
b.seek += 8
return
}
// WriteLenEncode used to write variable length.
// https://dev.mysql.com/doc/internals/en/integer.html#length-encoded-integer
func (b *Buffer) WriteLenEncode(v uint64) {
switch {
case v < 251:
b.WriteU8(uint8(v))
case v >= 251 && v < (1<<16):
b.WriteU8(0xfc)
b.WriteU16(uint16(v))
case v >= (1<<16) && v < (1<<24):
b.WriteU8(0xfd)
b.WriteU24(uint32(v))
default:
b.WriteU8(0xfe)
b.WriteU64(v)
}
}
// WriteLenEncodeNUL used to write NUL>
// 0xfb is represents a NULL in a ProtocolText::ResultsetRow
func (b *Buffer) WriteLenEncodeNUL() {
b.WriteU8(0xfb)
}
// ReadLenEncode used to read variable length.
func (b *Buffer) ReadLenEncode() (v uint64, err error) {
var u8 uint8
var u16 uint16
var u24 uint32
if u8, err = b.ReadU8(); err != nil {
return
}
switch u8 {
case 0xfb:
// nil value
// we set the length to maxuint64.
v = ^uint64(0)
return
case 0xfc:
if u16, err = b.ReadU16(); err != nil {
return
}
v = uint64(u16)
return
case 0xfd:
if u24, err = b.ReadU24(); err != nil {
return
}
v = uint64(u24)
return
case 0xfe:
if v, err = b.ReadU64(); err != nil {
return
}
return
default:
return uint64(u8), nil
}
}
// WriteLenEncodeString used to write variable string.
func (b *Buffer) WriteLenEncodeString(s string) {
l := len(s)
b.WriteLenEncode(uint64(l))
b.WriteString(s)
}
// ReadLenEncodeString used to read variable string.
func (b *Buffer) ReadLenEncodeString() (s string, err error) {
var l uint64
if l, err = b.ReadLenEncode(); err != nil {
return
}
if s, err = b.ReadString(int(l)); err != nil {
return
}
return
}
// WriteLenEncodeBytes used to write variable bytes.
func (b *Buffer) WriteLenEncodeBytes(v []byte) {
l := len(v)
b.WriteLenEncode(uint64(l))
b.WriteBytes(v)
}
// ReadLenEncodeBytes used to read variable bytes.
func (b *Buffer) ReadLenEncodeBytes() (v []byte, err error) {
var l uint64
if l, err = b.ReadLenEncode(); err != nil {
return
}
// nil value.
if l == ^uint64(0) {
return
}
if l == 0 {
return []byte{}, nil
}
if v, err = b.ReadBytes(int(l)); err != nil {
return
}
return
}
// WriteEOF used to write EOF.
func (b *Buffer) WriteEOF(n int) {
b.extend(n)
for i := 0; i < n; i++ {
b.buf[b.pos] = 0xfe
b.pos++
}
}
// ReadEOF used to read EOF.
func (b *Buffer) ReadEOF(n int) (err error) {
return b.ReadZero(n)
}
// WriteZero used to write zero.
func (b *Buffer) WriteZero(n int) {
b.extend(n)
for i := 0; i < n; i++ {
b.buf[b.pos] = 0
b.pos++
}
}
// ReadZero used to read zero.
func (b *Buffer) ReadZero(n int) (err error) {
if (b.seek + n) > b.pos {
err = ErrIOEOF
return
}
b.seek += n
return
}
// WriteString used to write string.
func (b *Buffer) WriteString(s string) {
n := len(s)
b.extend(n)
copy(b.buf[b.pos:], s)
b.pos += n
}
// ReadString used to read string.
func (b *Buffer) ReadString(n int) (s string, err error) {
if (b.seek + n) > b.pos {
err = ErrIOEOF
return
}
s = string(b.buf[b.seek:(b.seek + n)])
b.seek += n
return
}
// ReadStringNUL reads until the first NUL in the buffer
// returning a string containing the data up to and not including the NUL
func (b *Buffer) ReadStringNUL() (s string, err error) {
var v []byte
if v, err = b.readBytesWithToken(0x00); err != nil {
return
}
s = string(v)
return
}
// ReadStringEOF reads until the first EOF in the buffer
// returning a string containing the data up to and not including the EOF
func (b *Buffer) ReadStringEOF() (s string, err error) {
var v []byte
if v, err = b.readBytesWithToken(0xfe); err != nil {
return
}
s = string(v)
return
}
// ReadBytesNUL reads until the first NUL in the buffer
// returning a byte slice containing the data up to and not including the NUL
func (b *Buffer) ReadBytesNUL() (v []byte, err error) {
return b.readBytesWithToken(0x00)
}
// ReadBytesEOF reads until the first EOF in the buffer
// returning a byte slice containing the data up to and not including the EOF
func (b *Buffer) ReadBytesEOF() (v []byte, err error) {
return b.readBytesWithToken(0xfe)
}
func (b *Buffer) readBytesWithToken(token uint8) (v []byte, err error) {
i := bytes.IndexByte(b.buf[b.seek:], token)
end := b.seek + i + 1
if i < 0 {
b.seek = len(b.buf)
err = ErrIOEOF
return
}
v = b.buf[b.seek : end-1]
b.seek = end
return
}
// WriteBytes used to write bytes.
func (b *Buffer) WriteBytes(bs []byte) {
n := len(bs)
b.extend(n)
copy(b.buf[b.pos:], bs)
b.pos += n
}
// ReadBytes used to read bytes.
func (b *Buffer) ReadBytes(n int) (v []byte, err error) {
if n == 0 {
return nil, nil
}
if (b.seek + n) > b.pos {
err = ErrIOEOF
return
}
v = b.buf[b.seek:(b.seek + n)]
b.seek += n
return
}