-
Notifications
You must be signed in to change notification settings - Fork 37
/
convert.go
287 lines (258 loc) · 5 KB
/
convert.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
// GoMySQL - A MySQL client library for Go
//
// Copyright 2010-2011 Phil Bayfield. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysql
import (
"math"
"os"
"strconv"
)
// bytes to int
func btoi(b []byte) int {
return int(btoui(b))
}
// int to bytes
func itob(n int) []byte {
return uitob(uint(n))
}
// bytes to uint
func btoui(b []byte) (n uint) {
for i := uint8(0); i < uint8(strconv.IntSize)/8; i++ {
n |= uint(b[i]) << (i * 8)
}
return
}
// uint to bytes
func uitob(n uint) (b []byte) {
b = make([]byte, strconv.IntSize/8)
for i := uint8(0); i < uint8(strconv.IntSize)/8; i++ {
b[i] = byte(n >> (i * 8))
}
return
}
// bytes to int16
func btoi16(b []byte) int16 {
return int16(btoui16(b))
}
// int16 to bytes
func i16tob(n int16) []byte {
return ui16tob(uint16(n))
}
// bytes to uint16
func btoui16(b []byte) (n uint16) {
n |= uint16(b[0])
n |= uint16(b[1]) << 8
return
}
// uint16 to bytes
func ui16tob(n uint16) (b []byte) {
b = make([]byte, 2)
b[0] = byte(n)
b[1] = byte(n >> 8)
return
}
// bytes to int24
func btoi24(b []byte) (n int32) {
u := btoui24(b)
if u&0x800000 != 0 {
u |= 0xff000000
}
n = int32(u)
return
}
// int24 to bytes
func i24tob(n int32) []byte {
return ui24tob(uint32(n))
}
// bytes to uint24
func btoui24(b []byte) (n uint32) {
for i := uint8(0); i < 3; i++ {
n |= uint32(b[i]) << (i * 8)
}
return
}
// uint24 to bytes
func ui24tob(n uint32) (b []byte) {
b = make([]byte, 3)
for i := uint8(0); i < 3; i++ {
b[i] = byte(n >> (i * 8))
}
return
}
// bytes to int32
func btoi32(b []byte) int32 {
return int32(btoui32(b))
}
// int32 to bytes
func i32tob(n int32) []byte {
return ui32tob(uint32(n))
}
// bytes to uint32
func btoui32(b []byte) (n uint32) {
for i := uint8(0); i < 4; i++ {
n |= uint32(b[i]) << (i * 8)
}
return
}
// uint32 to bytes
func ui32tob(n uint32) (b []byte) {
b = make([]byte, 4)
for i := uint8(0); i < 4; i++ {
b[i] = byte(n >> (i * 8))
}
return
}
// bytes to int64
func btoi64(b []byte) int64 {
return int64(btoui64(b))
}
// int64 to bytes
func i64tob(n int64) []byte {
return ui64tob(uint64(n))
}
// bytes to uint64
func btoui64(b []byte) (n uint64) {
for i := uint8(0); i < 8; i++ {
n |= uint64(b[i]) << (i * 8)
}
return
}
// uint64 to bytes
func ui64tob(n uint64) (b []byte) {
b = make([]byte, 8)
for i := uint8(0); i < 8; i++ {
b[i] = byte(n >> (i * 8))
}
return
}
// bytes to float32
func btof32(b []byte) float32 {
return math.Float32frombits(btoui32(b))
}
// float32 to bytes
func f32tob(f float32) []byte {
return ui32tob(math.Float32bits(f))
}
// bytes to float64
func btof64(b []byte) float64 {
return math.Float64frombits(btoui64(b))
}
// float64 to bytes
func f64tob(f float64) []byte {
return ui64tob(math.Float64bits(f))
}
// bytes to length
func btolcb(b []byte) (num uint64, n int, err os.Error) {
switch {
// 0-250 = value of first byte
case b[0] <= 250:
num = uint64(b[0])
n = 1
return
// 251 column value = NULL
case b[0] == 251:
num = 0
n = 1
return
// 252 following 2 = value of following 16-bit word
case b[0] == 252:
n = 3
// 253 following 3 = value of following 24-bit word
case b[0] == 253:
n = 4
// 254 following 8 = value of following 64-bit word
case b[0] == 254:
n = 9
}
// Check there are enough bytes
if len(b) < n {
err = os.EOF
return
}
// Get uint64
t := make([]byte, 8)
copy(t, b[1:n])
num = btoui64(t)
return
}
// length to bytes
func lcbtob(n uint64) (b []byte) {
switch {
// <= 250 = 1 byte
case n <= 250:
b = []byte{byte(n)}
// <= 0xffff = 252 + 2 bytes
case n <= 0xffff:
b = []byte{0xfc, byte(n), byte(n >> 8)}
// <= 0xffffff = 253 + 3 bytes
case n <= 0xffffff:
b = []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
// Due to max packet size the 8 byte version is never actually used so is ommited
}
return
}
// any to uint64
func atoui64(i interface{}) (n uint64) {
switch t := i.(type) {
case int64:
n = uint64(t)
case uint64:
return t
case string:
// Convert to int64 first for signing bit
in, err := strconv.Atoi64(t)
if err != nil {
panic("Invalid string for integer conversion")
}
n = uint64(in)
default:
panic("Not a numeric type")
}
return
}
// any to float64
func atof64(i interface{}) (f float64) {
switch t := i.(type) {
case float32:
f = float64(t)
case float64:
return t
case string:
var err os.Error
f, err = strconv.Atof64(t)
if err != nil {
panic("Invalid string for floating point conversion")
}
default:
panic("Not a floating point type")
}
return
}
// any to string
func atos(i interface{}) (s string) {
switch t := i.(type) {
case int64:
s = strconv.Itoa64(t)
case uint64:
s = strconv.Uitoa64(t)
case float32:
s = strconv.Ftoa32(t, 'f', -1)
case float64:
s = strconv.Ftoa64(t, 'f', -1)
case []byte:
s = string(t)
case Date:
return t.String()
case Time:
return t.String()
case DateTime:
return t.String()
case string:
return t
default:
panic("Not a string or compatible type")
}
return
}