/
protocol.go
155 lines (129 loc) · 3.81 KB
/
protocol.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
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0
// The MIT License (MIT)
//
// Copyright (c) 2014 wandoulabs
// Copyright (c) 2014 siddontang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
//
// 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 http://mozilla.org/MPL/2.0/.
package net
import (
"bytes"
"io"
)
func ParseLengthEncodedInt(b []byte) (num uint64, isNull bool, n int) {
switch b[0] {
// 251: NULL
case 0xfb:
n = 1
isNull = true
return
// 252: value of following 2
case 0xfc:
num = uint64(b[1]) | uint64(b[2])<<8
n = 3
return
// 253: value of following 3
case 0xfd:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
n = 4
return
// 254: value of following 8
case 0xfe:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
uint64(b[7])<<48 | uint64(b[8])<<56
n = 9
return
}
// https://dev.mysql.com/doc/internals/en/integer.html#length-encoded-integer: If the first byte of a packet is a length-encoded integer and its byte value is 0xfe, you must check the length of the packet to verify that it has enough space for a 8-byte integer.
// TODO: 0xff is undefined
// 0-250: value of first byte
num = uint64(b[0])
n = 1
return
}
// SkipLengthEncodedInt skips the int and only returns the length of the encoded int.
func SkipLengthEncodedInt(b []byte) int {
switch b[0] {
case 0xfc:
return 3
case 0xfd:
return 4
case 0xfe:
return 9
default:
return 1
}
}
func ParseLengthEncodedBytes(b []byte) ([]byte, bool, int, error) {
// Get length
num, isNull, n := ParseLengthEncodedInt(b)
if num < 1 {
return nil, isNull, n, nil
}
n += int(num)
// Check data length
if len(b) >= n {
return b[n-int(num) : n], false, n, nil
}
return nil, false, n, io.EOF
}
func ParseNullTermString(b []byte) (str []byte, remain []byte) {
off := bytes.IndexByte(b, 0)
if off == -1 {
return nil, b
}
return b[:off], b[off+1:]
}
var tinyIntCache [251][]byte
func init() {
for i := 0; i < len(tinyIntCache); i++ {
tinyIntCache[i] = []byte{byte(i)}
}
}
func DumpLengthEncodedInt(buffer []byte, n uint64) []byte {
switch {
case n <= 250:
return append(buffer, tinyIntCache[n]...)
case n <= 0xffff:
return append(buffer, 0xfc, byte(n), byte(n>>8))
case n <= 0xffffff:
return append(buffer, 0xfd, byte(n), byte(n>>8), byte(n>>16))
case n <= 0xffffffffffffffff:
return append(buffer, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
}
return buffer
}
// DumpLengthEncodedString dumps string<int>.
func DumpLengthEncodedString(buffer []byte, bytes []byte) []byte {
buffer = DumpLengthEncodedInt(buffer, uint64(len(bytes)))
buffer = append(buffer, bytes...)
return buffer
}
func DumpUint32(buffer []byte, n uint32) []byte {
buffer = append(buffer, byte(n))
buffer = append(buffer, byte(n>>8))
buffer = append(buffer, byte(n>>16))
buffer = append(buffer, byte(n>>24))
return buffer
}
func DumpUint16(buffer []byte, n uint16) []byte {
buffer = append(buffer, byte(n))
buffer = append(buffer, byte(n>>8))
return buffer
}