File: codecs.go

package info (click to toggle)
golang-github-ziutek-mymysql 1.5.4%2Bgit20170206.23.0582bcf-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 388 kB
  • sloc: makefile: 8; sh: 2
file content (458 lines) | stat: -rw-r--r-- 8,357 bytes parent folder | download
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package native

import (
	"github.com/ziutek/mymysql/mysql"
	"time"
)

// Integers

func DecodeU16(buf []byte) uint16 {
	return uint16(buf[1])<<8 | uint16(buf[0])
}
func (pr *pktReader) readU16() uint16 {
	buf := pr.buf[:2]
	pr.readFull(buf)
	return DecodeU16(buf)
}

func DecodeU24(buf []byte) uint32 {
	return (uint32(buf[2])<<8|uint32(buf[1]))<<8 | uint32(buf[0])
}
func (pr *pktReader) readU24() uint32 {
	buf := pr.buf[:3]
	pr.readFull(buf)
	return DecodeU24(buf)
}

func DecodeU32(buf []byte) uint32 {
	return ((uint32(buf[3])<<8|uint32(buf[2]))<<8|
		uint32(buf[1]))<<8 | uint32(buf[0])
}
func (pr *pktReader) readU32() uint32 {
	buf := pr.buf[:4]
	pr.readFull(buf)
	return DecodeU32(buf)
}

func DecodeU64(buf []byte) (rv uint64) {
	for ii, vv := range buf {
		rv |= uint64(vv) << uint(ii*8)
	}
	return
}
func (pr *pktReader) readU64() (rv uint64) {
	buf := pr.buf[:8]
	pr.readFull(buf)
	return DecodeU64(buf)
}

func EncodeU16(buf []byte, val uint16) {
	buf[0] = byte(val)
	buf[1] = byte(val >> 8)
}
func (pw *pktWriter) writeU16(val uint16) {
	buf := pw.buf[:2]
	EncodeU16(buf, val)
	pw.write(buf)
}

func EncodeU24(buf []byte, val uint32) {
	buf[0] = byte(val)
	buf[1] = byte(val >> 8)
	buf[2] = byte(val >> 16)
}
func (pw *pktWriter) writeU24(val uint32) {
	buf := pw.buf[:3]
	EncodeU24(buf, val)
	pw.write(buf)
}

func EncodeU32(buf []byte, val uint32) {
	buf[0] = byte(val)
	buf[1] = byte(val >> 8)
	buf[2] = byte(val >> 16)
	buf[3] = byte(val >> 24)
}
func (pw *pktWriter) writeU32(val uint32) {
	buf := pw.buf[:4]
	EncodeU32(buf, val)
	pw.write(buf)
}

func EncodeU64(buf []byte, val uint64) {
	buf[0] = byte(val)
	buf[1] = byte(val >> 8)
	buf[2] = byte(val >> 16)
	buf[3] = byte(val >> 24)
	buf[4] = byte(val >> 32)
	buf[5] = byte(val >> 40)
	buf[6] = byte(val >> 48)
	buf[7] = byte(val >> 56)
}
func (pw *pktWriter) writeU64(val uint64) {
	buf := pw.buf[:8]
	EncodeU64(buf, val)
	pw.write(buf)
}

// Variable length values

func (pr *pktReader) readNullLCB() (lcb uint64, null bool) {
	bb := pr.readByte()
	switch bb {
	case 251:
		null = true
	case 252:
		lcb = uint64(pr.readU16())
	case 253:
		lcb = uint64(pr.readU24())
	case 254:
		lcb = pr.readU64()
	default:
		lcb = uint64(bb)
	}
	return
}

func (pr *pktReader) readLCB() uint64 {
	lcb, null := pr.readNullLCB()
	if null {
		panic(mysql.ErrUnexpNullLCB)
	}
	return lcb
}

func (pw *pktWriter) writeLCB(val uint64) {
	switch {
	case val <= 250:
		pw.writeByte(byte(val))

	case val <= 0xffff:
		pw.writeByte(252)
		pw.writeU16(uint16(val))

	case val <= 0xffffff:
		pw.writeByte(253)
		pw.writeU24(uint32(val))

	default:
		pw.writeByte(254)
		pw.writeU64(val)
	}
}

func lenLCB(val uint64) int {
	switch {
	case val <= 250:
		return 1

	case val <= 0xffff:
		return 3

	case val <= 0xffffff:
		return 4
	}
	return 9
}

func (pr *pktReader) readNullBin() (buf []byte, null bool) {
	var l uint64
	l, null = pr.readNullLCB()
	if null {
		return
	}
	buf = make([]byte, l)
	pr.readFull(buf)
	return
}

func (pr *pktReader) readBin() []byte {
	buf, null := pr.readNullBin()
	if null {
		panic(mysql.ErrUnexpNullLCS)
	}
	return buf
}

func (pr *pktReader) skipBin() {
	n, _ := pr.readNullLCB()
	pr.skipN(int(n))
}

func (pw *pktWriter) writeBin(buf []byte) {
	pw.writeLCB(uint64(len(buf)))
	pw.write(buf)
}

func lenBin(buf []byte) int {
	return lenLCB(uint64(len(buf))) + len(buf)
}

func lenStr(str string) int {
	return lenLCB(uint64(len(str))) + len(str)
}

func (pw *pktWriter) writeLC(v interface{}) {
	switch val := v.(type) {
	case []byte:
		pw.writeBin(val)
	case *[]byte:
		pw.writeBin(*val)
	case string:
		pw.writeBin([]byte(val))
	case *string:
		pw.writeBin([]byte(*val))
	default:
		panic("Unknown data type for write as length coded string")
	}
}

func lenLC(v interface{}) int {
	switch val := v.(type) {
	case []byte:
		return lenBin(val)
	case *[]byte:
		return lenBin(*val)
	case string:
		return lenStr(val)
	case *string:
		return lenStr(*val)
	}
	panic("Unknown data type for write as length coded string")
}

func (pr *pktReader) readNTB() (buf []byte) {
	for {
		ch := pr.readByte()
		if ch == 0 {
			break
		}
		buf = append(buf, ch)
	}
	return
}

func (pw *pktWriter) writeNTB(buf []byte) {
	pw.write(buf)
	pw.writeByte(0)
}

func (pw *pktWriter) writeNT(v interface{}) {
	switch val := v.(type) {
	case []byte:
		pw.writeNTB(val)
	case string:
		pw.writeNTB([]byte(val))
	default:
		panic("Unknown type for write as null terminated data")
	}
}

// Date and time

func (pr *pktReader) readDuration() time.Duration {
	dlen := pr.readByte()
	switch dlen {
	case 251:
		// Null
		panic(mysql.ErrUnexpNullTime)
	case 0:
		// 00:00:00
		return 0
	case 5, 8, 12:
		// Properly time length
	default:
		panic(mysql.ErrWrongDateLen)
	}
	buf := pr.buf[:dlen]
	pr.readFull(buf)
	tt := int64(0)
	switch dlen {
	case 12:
		// Nanosecond part
		tt += int64(DecodeU32(buf[8:]))
		fallthrough
	case 8:
		// HH:MM:SS part
		tt += int64(int(buf[5])*3600+int(buf[6])*60+int(buf[7])) * 1e9
		fallthrough
	case 5:
		// Day part
		tt += int64(DecodeU32(buf[1:5])) * (24 * 3600 * 1e9)
	}
	if buf[0] != 0 {
		tt = -tt
	}
	return time.Duration(tt)
}

func EncodeDuration(buf []byte, d time.Duration) int {
	buf[0] = 0
	if d < 0 {
		buf[1] = 1
		d = -d
	}
	if ns := uint32(d % 1e9); ns != 0 {
		EncodeU32(buf[9:13], ns) // nanosecond
		buf[0] += 4
	}
	d /= 1e9
	if hms := int(d % (24 * 3600)); buf[0] != 0 || hms != 0 {
		buf[8] = byte(hms % 60) // second
		hms /= 60
		buf[7] = byte(hms % 60) // minute
		buf[6] = byte(hms / 60) // hour
		buf[0] += 3
	}
	if day := uint32(d / (24 * 3600)); buf[0] != 0 || day != 0 {
		EncodeU32(buf[2:6], day) // day
		buf[0] += 4
	}
	buf[0]++ // For sign byte
	return int(buf[0] + 1)
}

func (pw *pktWriter) writeDuration(d time.Duration) {
	buf := pw.buf[:13]
	n := EncodeDuration(buf, d)
	pw.write(buf[:n])
}

func lenDuration(d time.Duration) int {
	if d == 0 {
		return 2
	}
	if d%1e9 != 0 {
		return 13
	}
	d /= 1e9
	if d%(24*3600) != 0 {
		return 9
	}
	return 6
}

func (pr *pktReader) readTime() time.Time {
	dlen := pr.readByte()
	switch dlen {
	case 251:
		// Null
		panic(mysql.ErrUnexpNullDate)
	case 0:
		// return 0000-00-00 converted to time.Time zero
		return time.Time{}
	case 4, 7, 11:
		// Properly datetime length
	default:
		panic(mysql.ErrWrongDateLen)
	}

	buf := pr.buf[:dlen]
	pr.readFull(buf)
	var y, mon, d, h, m, s, u int
	switch dlen {
	case 11:
		// 2006-01-02 15:04:05.001004005
		u = int(DecodeU32(buf[7:]))
		fallthrough
	case 7:
		// 2006-01-02 15:04:05
		h = int(buf[4])
		m = int(buf[5])
		s = int(buf[6])
		fallthrough
	case 4:
		// 2006-01-02
		y = int(DecodeU16(buf[0:2]))
		mon = int(buf[2])
		d = int(buf[3])
	}
	n := u * int(time.Microsecond)
	return time.Date(y, time.Month(mon), d, h, m, s, n, time.Local)
}

func encodeNonzeroTime(buf []byte, y int16, mon, d, h, m, s byte, u uint32) int {
	buf[0] = 0
	switch {
	case u != 0:
		EncodeU32(buf[8:12], u)
		buf[0] += 4
		fallthrough
	case s != 0 || m != 0 || h != 0:
		buf[7] = s
		buf[6] = m
		buf[5] = h
		buf[0] += 3
	}
	buf[4] = d
	buf[3] = mon
	EncodeU16(buf[1:3], uint16(y))
	buf[0] += 4
	return int(buf[0] + 1)
}

func getTimeMicroseconds(t time.Time) int {
	return (t.Nanosecond() + int(time.Microsecond/2)) / int(time.Microsecond)
}

func EncodeTime(buf []byte, t time.Time) int {
	if t.IsZero() {
		// MySQL zero
		buf[0] = 0
		return 1 // MySQL zero
	}
	y, mon, d := t.Date()
	h, m, s := t.Clock()
	u:= getTimeMicroseconds(t)
	return encodeNonzeroTime(
		buf,
		int16(y), byte(mon), byte(d),
		byte(h), byte(m), byte(s), uint32(u),
	)
}

func (pw *pktWriter) writeTime(t time.Time) {
	buf := pw.buf[:12]
	n := EncodeTime(buf, t)
	pw.write(buf[:n])
}

func lenTime(t time.Time) int {
	switch {
	case t.IsZero():
		return 1
	case getTimeMicroseconds(t) != 0:
		return 12
	case t.Second() != 0 || t.Minute() != 0 || t.Hour() != 0:
		return 8
	}
	return 5
}

func (pr *pktReader) readDate() mysql.Date {
	y, m, d := pr.readTime().Date()
	return mysql.Date{int16(y), byte(m), byte(d)}
}

func EncodeDate(buf []byte, d mysql.Date) int {
	if d.IsZero() {
		// MySQL zero
		buf[0] = 0
		return 1
	}
	return encodeNonzeroTime(buf, d.Year, d.Month, d.Day, 0, 0, 0, 0)
}

func (pw *pktWriter) writeDate(d mysql.Date) {
	buf := pw.buf[:5]
	n := EncodeDate(buf, d)
	pw.write(buf[:n])
}

func lenDate(d mysql.Date) int {
	if d.IsZero() {
		return 1
	}
	return 5
}