-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
300 lines (284 loc) · 6.53 KB
/
json.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
package binlog
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"time"
)
// https://dev.mysql.com/worklog/task/?id=8132#tabs-8132-4
type jsonDecoder struct{}
const (
jsonSmallObj = 0x00
jsonLargeObj = 0x01
jsonSmallArr = 0x02
jsonLargeArr = 0x03
jsonLiteral = 0x04
jsonInt16 = 0x05
jsonUInt16 = 0x06
jsonInt32 = 0x07
jsonUInt32 = 0x08
jsonInt64 = 0x09
jsonUInt64 = 0x0a
jsonDouble = 0x0b
jsonString = 0x0c
jsonCustom = 0x0f
)
func (d *jsonDecoder) decodeValue(data []byte) (interface{}, error) {
if len(data) < 1 {
return nil, io.ErrUnexpectedEOF
}
return d.decodeValueType(data[0], data[1:])
}
func (d *jsonDecoder) decodeValueType(typ byte, data []byte) (interface{}, error) {
switch typ {
case jsonSmallObj:
return d.decodeComposite(data, true, true)
case jsonLargeObj:
return d.decodeComposite(data, false, true)
case jsonSmallArr:
return d.decodeComposite(data, true, false)
case jsonLargeArr:
return d.decodeComposite(data, false, false)
case jsonLiteral:
return d.decodeLiteral(data)
case jsonInt16:
v, err := d.decodeUInt16(data)
return int16(v), err
case jsonUInt16:
return d.decodeUInt16(data)
case jsonInt32:
v, err := d.decodeUInt32(data)
return int32(v), err
case jsonUInt32:
return d.decodeUInt32(data)
case jsonInt64:
v, err := d.decodeUInt64(data)
return int64(v), err
case jsonUInt64:
return d.decodeUInt64(data)
case jsonDouble:
v, err := d.decodeUInt64(data)
return math.Float64frombits(v), err
case jsonString:
return d.decodeString(data)
case jsonCustom:
return d.decodeCustom(data)
}
return nil, fmt.Errorf("binlog: invalid json valueType %#02x", typ)
}
func (d *jsonDecoder) decodeComposite(data []byte, small bool, obj bool) (interface{}, error) {
var off int
decodeUInt := func() (uint32, error) {
if small {
v, err := d.decodeUInt16(data[off:])
if err != nil {
return 0, err
}
off += 2
return uint32(v), nil
} else {
v, err := d.decodeUInt32(data[off:])
off += 4
return v, err
}
}
elemCount, err := decodeUInt()
if err != nil {
return nil, err
}
size, err := decodeUInt()
if err != nil {
return nil, err
}
_ = size
var keys []string
if obj {
keys = make([]string, elemCount)
for i := uint32(0); i < elemCount; i++ {
keyOff, err := decodeUInt()
if err != nil {
return nil, err
}
keyLen, err := d.decodeUInt16(data[off:])
if err != nil {
return nil, err
}
off += 2
if len(data) < int(keyOff+uint32(keyLen)) {
return nil, io.ErrUnexpectedEOF
}
keys[i] = string(data[keyOff : keyOff+uint32(keyLen)])
}
}
inlineValue := func(typ byte) bool {
switch typ {
case jsonLiteral, jsonInt16, jsonUInt16:
return true
case jsonInt32, jsonUInt32:
return !small
}
return false
}
vals := make([]interface{}, elemCount)
for i := uint32(0); i < elemCount; i++ {
typ := data[off]
off++
if inlineValue(typ) {
v, err := d.decodeValueType(typ, data[off:])
if err != nil {
return nil, err
}
vals[i] = v
if small {
off += 2
} else {
off += 4
}
} else {
valueOff, err := decodeUInt()
if err != nil {
return nil, err
}
v, err := d.decodeValueType(typ, data[valueOff:])
if err != nil {
return nil, err
}
vals[i] = v
}
}
if obj {
m := make(map[string]interface{})
for i, key := range keys {
m[key] = vals[i]
}
return m, nil
}
return vals, nil
}
func (d *jsonDecoder) decodeLiteral(data []byte) (interface{}, error) {
if len(data) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch data[0] {
case 0x00:
return nil, nil
case 0x01:
return true, nil
case 0x02:
return false, nil
}
return nil, fmt.Errorf("binlog: invalid json literalType %#02x", data[0])
}
func (d *jsonDecoder) decodeUInt16(data []byte) (uint16, error) {
if len(data) < 2 {
return 0, io.ErrUnexpectedEOF
}
return binary.LittleEndian.Uint16(data), nil
}
func (d *jsonDecoder) decodeUInt32(data []byte) (uint32, error) {
if len(data) < 4 {
return 0, io.ErrUnexpectedEOF
}
return binary.LittleEndian.Uint32(data), nil
}
func (d *jsonDecoder) decodeUInt64(data []byte) (uint64, error) {
if len(data) < 8 {
return 0, io.ErrUnexpectedEOF
}
return binary.LittleEndian.Uint64(data), nil
}
func (d *jsonDecoder) decodeDataLen(data []byte) (uint64, []byte, error) {
const max = 5 // math.MaxUint32 can be encoded in 5 bytes
var size uint64
for i := 0; i < max; i++ {
if len(data) == 0 {
return 0, data, io.ErrUnexpectedEOF
}
v := data[0]
data = data[1:]
size |= uint64(v&0x7F) << uint(7*i)
if highBit := v & (1 << 7); highBit == 0 {
return size, data, nil
}
}
return 0, nil, errors.New("binlog: invalid json stringDataLen")
}
func (d *jsonDecoder) decodeString(data []byte) (string, error) {
size, data, err := d.decodeDataLen(data)
if err != nil {
return "", err
}
if len(data) < int(size) {
return "", io.ErrUnexpectedEOF
}
return string(data[:size]), nil
}
func (d *jsonDecoder) decodeCustom(data []byte) (interface{}, error) {
if len(data) == 0 {
return nil, io.ErrUnexpectedEOF
}
typ := data[0]
data = data[1:]
size, data, err := d.decodeDataLen(data)
if err != nil {
return nil, err
}
if len(data) < int(size) {
return nil, io.ErrUnexpectedEOF
}
switch ColumnType(typ) {
case TypeNewDecimal:
precision := int(data[0])
scale := int(data[1])
return decodeDecimal(data[2:], precision, scale)
case TypeTime:
if len(data) < 8 {
return nil, io.ErrUnexpectedEOF
}
v := int64(binary.LittleEndian.Uint64(data))
var hour, min, sec, frac int64
var sign = 1
if v != 0 {
if v < 0 {
v = -v
sign = -1
}
frac = v % (1 << 24)
v = v >> 24
hour = (v >> 12) % (1 << 10)
min = (v >> 6) % (1 << 6)
sec = v % (1 << 6)
}
return time.Duration(sign) * (time.Duration(hour)*time.Hour +
time.Duration(min)*time.Minute +
time.Duration(sec)*time.Second +
time.Duration(frac)*time.Microsecond), nil
case TypeDate, TypeDateTime, TypeTimestamp:
if len(data) < 8 {
return nil, io.ErrUnexpectedEOF
}
v := binary.LittleEndian.Uint64(data)
var year, month, day, hour, min, sec, frac uint64
if v != 0 {
if v < 0 {
v = -v
}
frac = v % (1 << 24)
v = v >> 24
ymd := v >> 17
ym := ymd >> 5
year, month, day = ym/13, ym%13, ymd%(1<<5)
hms := v % (1 << 17)
hour, min, sec = hms>>12, (hms>>6)%(1<<6), hms%(1<<6)
}
var loc = time.UTC
if ColumnType(typ) == TypeTimestamp {
loc = time.Local
}
return time.Date(int(year), time.Month(month), int(day), int(hour), int(min), int(sec), int(frac*1000), loc), nil
default:
return string(data), nil
}
}