forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
314 lines (285 loc) · 8.49 KB
/
packet.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
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
package binlog
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"strconv"
"time"
"github.com/LightKool/mysql-go"
)
type binlogPacket struct {
*mysql.Packet
}
func newBinlogPacket(data []byte) *binlogPacket {
return &binlogPacket{mysql.NewPacket(data)}
}
func (p *binlogPacket) readByte() byte {
return p.Read(1)[0]
}
func (p *binlogPacket) readUint16() uint16 {
return uint16(p.ReadUintBySize(2))
}
func (p *binlogPacket) readUint24() uint32 {
return uint32(p.ReadUintBySize(3))
}
func (p *binlogPacket) readUint32() uint32 {
return uint32(p.ReadUintBySize(4))
}
func (p *binlogPacket) readUint64() uint64 {
return p.ReadUintBySize(8)
}
func (p *binlogPacket) readTableColumnMeta(columnTypes []byte) ([]uint16, error) {
data, err := p.ReadPackedString()
if err != nil {
return nil, err
}
// decode table column metadata
meta := make([]uint16, len(columnTypes))
pos := 0
for i, v := range columnTypes {
switch v {
case fieldTypeFloat, fieldTypeDouble, fieldTypeBLOB, fieldTypeJSON, fieldTypeGeometry:
meta[i] = uint16(data[pos])
pos++
case fieldTypeBit, fieldTypeVarChar, fieldTypeVarString:
// - fieldTypeBit: {length of the field}/8, {length of the field} % 8
// - fieldTypeVarChar | fieldTypeVarChar: {length of the field}(2 bytes)
meta[i] = binary.LittleEndian.Uint16(data[pos:])
pos += 2
case fieldTypeString, fieldTypeNewDecimal:
// - fieldTypeString: {real type}, {pack of field length}
// - fieldTypeNewDecimal: {precision}, {scale}
meta[i] = binary.BigEndian.Uint16(data[pos:])
pos += 2
case fieldTypeTimestampV2, fieldTypeDateTimeV2, fieldTypeTimeV2:
meta[i] = uint16(data[pos])
pos++
default:
meta[i] = 0
}
}
return meta, nil
}
func (p *binlogPacket) readTableColumnValue(typ byte, meta uint16) (v interface{}, err error) {
var length int
if typ == fieldTypeString {
if meta >= 256 {
realType := byte(meta >> 8)
if realType&0x30 != 0x30 {
length = int(uint16(meta&0xFF) | uint16((realType&0x30)^0x30)<<4)
typ = realType | 0x30
} else {
length = int(meta & 0xFF)
typ = realType
}
} else {
length = int(meta)
}
}
switch typ {
case fieldTypeTiny:
b := p.readByte()
v = int64(b)
case fieldTypeShort:
v = int64(p.readUint16())
case fieldTypeInt24:
v = int64(p.readUint24())
case fieldTypeLong:
v = int64(p.readUint32())
case fieldTypeLongLong:
u64 := p.readUint64()
if u64 > math.MaxInt64 {
v = fmt.Sprintln(u64)
} else {
v = int64(u64)
}
case fieldTypeFloat:
v = math.Float32frombits(p.readUint32())
case fieldTypeDouble:
v = math.Float64frombits(p.readUint64())
case fieldTypeNewDecimal:
v, err = p.readNewDecimal(meta)
case fieldTypeYear:
v = 1900 + int(p.readByte())
case fieldTypeDate:
u32 := uint32(p.ReadUintBySize(3))
v = fmt.Sprintf("%04d-%02d-%02d", u32>>9, (u32>>5)%16, u32%32)
case fieldTypeTime:
u32 := uint32(p.ReadUintBySize(3))
v = fmt.Sprintf("%02d:%02d:%02d", u32/10000, (u32%10000)/100, u32%100)
case fieldTypeTimeV2:
v = p.readTimeV2(meta)
case fieldTypeDateTime:
// a number like YYYYMMDDhhmmss
u64 := p.readUint64()
d := u64 / 1000000
t := u64 % 1000000
v = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", d/10000, (d%10000)/100, d%100, t/10000, (t%10000)/100, t%100)
case fieldTypeDateTimeV2:
v = p.readDateTimeV2(meta)
case fieldTypeTimestamp:
v = time.Unix(int64(p.readUint32()), 0).UnixNano()
case fieldTypeTimestampV2:
sec := int64(p.ReadUintBySizeBE(4))
msec := p.readMicroSeconds(int(meta), false)
v = time.Unix(sec, msec*1000).UnixNano()
case fieldTypeVarChar, fieldTypeVarString:
length = int(meta)
fallthrough
case fieldTypeString:
if length < 256 {
length = int(p.readByte())
} else {
length = int(p.readUint16())
}
v = string(p.Read(length))
case fieldTypeEnum:
if length == 1 || length == 2 {
v = int64(p.ReadUintBySize(length))
} else {
err = fmt.Errorf("Unknown ENUM pack legnth: %d", length)
}
case fieldTypeSet:
if length >= 0 && length <= 8 {
v = int64(p.ReadUintBySizeBE(length))
} else {
err = fmt.Errorf("Unknown SET pack length: %d", length)
}
case fieldTypeBit:
nbits := (meta>>8)*8 + meta&0xFF
length = (int(nbits) + 7) / 8
if length >= 0 && length <= 8 {
v = int64(p.ReadUintBySizeBE(length))
} else {
err = fmt.Errorf("Unknown BIT pack length: %d", length)
}
case fieldTypeBLOB, fieldTypeGeometry: // MySQL saves Geometry as Blob in binlog
length = int(meta)
blobLen := p.ReadUintBySize(length)
v = p.Read(int(blobLen))
case fieldTypeJSON:
// TODO
length = int(meta)
blobLen := p.ReadUintBySize(length)
v = p.Read(int(blobLen))
}
return
}
var digitsPerInteger = 9
var compressedBytes = []int{0, 1, 1, 2, 2, 3, 3, 4, 4, 4}
// Refer to https://github.com/mysql/mysql-server/blob/5.6/strings/decimal.c (line 1341: decimal2bin())
func (p *binlogPacket) readNewDecimal(meta uint16) (float64, error) {
precision, scale := int(meta>>8), int(meta&0xFF)
integral := precision - scale // digits number to the left of the decimal point
intg, frac := integral/digitsPerInteger, scale/digitsPerInteger
intgx, fracx := integral%digitsPerInteger, scale%digitsPerInteger
size := compressedBytes[intgx] + intg*4 + frac*4 + compressedBytes[fracx]
data := p.Read(size)
var buf bytes.Buffer
negative := data[0]&0x80 == 0
if negative {
for i := range data {
data[i] ^= 0xFF // if negative, convert to positive
}
buf.WriteString("-")
}
data[0] ^= 0x80 // remove the sign bit
var length int
packet := mysql.NewPacket(data)
// compressed integer part
length = compressedBytes[intgx]
buf.WriteString(fmt.Sprintf("%d", packet.ReadUintBySizeBE(length)))
// uncompressed integer part
for i := 0; i < intg; i++ {
buf.WriteString(fmt.Sprintf("%09d", packet.ReadUintBySizeBE(4)))
}
// decimal point
buf.WriteString(".")
// uncompressed fractional part
for i := 0; i < frac; i++ {
buf.WriteString(fmt.Sprintf("%09d", packet.ReadUintBySizeBE(4)))
}
// compressed fractional part
length = compressedBytes[fracx]
buf.WriteString(fmt.Sprintf("%0*d", fracx, packet.ReadUintBySizeBE(length)))
return strconv.ParseFloat(buf.String(), 64)
}
// readMicroSeconds reads fractional part of MySQL timestamp/datetime/time fields
func (p *binlogPacket) readMicroSeconds(dec int, negative bool) int64 {
// dec is in the range(0,6)
msecLen := (dec + 1) / 2
var msec int64
msec = int64(p.ReadUintBySizeBE(msecLen))
if msec != 0 {
if negative {
msec -= int64(math.Pow(0x100, float64(msecLen)))
}
msec *= int64(math.Pow(100, float64(3-msecLen)))
}
return msec
}
func (p *binlogPacket) readDateTimeV2(meta uint16) (v string) {
/*
1 bit sign (1 = positive, 0 = negative ignored) the negative value of a datetime doesn't make much sense
17 bits year*13+month (year 0-9999, month 0-12)
5 bits day (0-31)
5 bits hour (0-23)
6 bits minute (0-59)
6 bits second (0-59)
24 bits microseconds (0-999999)
Total: 64 bits = 8 bytes
SYYYYYYY.YYYYYYYY.YYdddddh.hhhhmmmm.mmssssss.ffffffff.ffffffff.ffffffff
*/
datetime := p.ReadUintBySizeBE(5)
dec := int(meta)
msec := p.readMicroSeconds(dec, false)
yearmonth := datetime >> (40 - 1 - 17) & (1<<17 - 1)
year := yearmonth / 13
month := yearmonth % 13
day := datetime >> (40 - 18 - 5) & (1<<5 - 1)
hour := datetime >> (40 - 23 - 5) & (1<<5 - 1)
minute := datetime >> (40 - 28 - 6) & (1<<6 - 1)
sec := datetime >> (40 - 34 - 6) & (1<<6 - 1)
if dec > 0 {
v = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%0*d", year, month, day, hour, minute, sec, dec, msec)
} else {
v = fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, sec)
}
return
}
func (p *binlogPacket) readTimeV2(meta uint16) (v string) {
/*
1 bit sign (1 = positive, 0 = negative)
1 bit unused (reserved for future extensions)
10 bits hour (0-838)
6 bits minute (0-59)
6 bits second (0-59)
(3 bytes in total)
+ fractional-seconds storage (size depends on meta)
*/
time := int64(p.ReadUintBySizeBE(3)) - 0x800000
negative := time < 0
dec := int(meta)
msec := p.readMicroSeconds(dec, negative)
var sign string
if negative {
if msec != 0 {
time++
}
time = time<<24 + msec
time = -time
msec = time % (1 << 24)
time = time >> 24
sign = "-"
}
hour := time >> (24 - 2 - 10) & (1<<10 - 1)
minute := time >> (24 - 12 - 6) & (1<<6 - 1)
sec := time >> (24 - 18 - 6) & (1<<6 - 1)
if dec > 0 {
v = fmt.Sprintf("%s%02d:%02d:%02d.%0*d", sign, hour, minute, sec, dec, msec)
} else {
v = fmt.Sprintf("%s%02d:%02d:%02d", sign, hour, minute, sec)
}
return
}