-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
fn_base64.go
176 lines (144 loc) · 4.33 KB
/
fn_base64.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
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package evalengine
import (
"bytes"
"encoding/base64"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)
type (
builtinToBase64 struct {
CallExpr
collate collations.ID
}
builtinFromBase64 struct {
CallExpr
}
)
var _ Expr = (*builtinToBase64)(nil)
var _ Expr = (*builtinFromBase64)(nil)
// MySQL wraps every 76 characters with a newline. That maps
// to a 57 byte input. So we encode here in blocks of 57 bytes
// with then each a newline.
var mysqlBase64OutLineLength = 76
var mysqlBase64InLineLength = (mysqlBase64OutLineLength / 4) * 3
func mysqlBase64Encode(in []byte) []byte {
newlines := len(in) / mysqlBase64InLineLength
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(in))+newlines)
out := encoded
for len(in) > mysqlBase64InLineLength {
base64.StdEncoding.Encode(out, in[:mysqlBase64InLineLength])
in = in[mysqlBase64InLineLength:]
out[mysqlBase64OutLineLength] = '\n'
out = out[mysqlBase64OutLineLength+1:]
}
base64.StdEncoding.Encode(out, in)
return encoded
}
func mysqlBase64Decode(in []byte) ([]byte, error) {
in = bytes.Trim(in, " \t\r\n")
decoded := make([]byte, len(in)/4*3)
n, err := base64.StdEncoding.Decode(decoded, in)
if err != nil {
return nil, err
}
return decoded[:n], nil
}
func (call *builtinToBase64) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
b := evalToBinary(arg)
encoded := mysqlBase64Encode(b.bytes)
if arg.SQLType() == sqltypes.Blob || arg.SQLType() == sqltypes.TypeJSON {
return newEvalRaw(sqltypes.Text, encoded, defaultCoercionCollation(call.collate)), nil
}
return newEvalText(encoded, defaultCoercionCollation(call.collate)), nil
}
func (call *builtinToBase64) typeof(env *ExpressionEnv, fields []*querypb.Field) (sqltypes.Type, typeFlag) {
tt, f := call.Arguments[0].typeof(env, fields)
if tt == sqltypes.Blob || tt == sqltypes.TypeJSON {
return sqltypes.Text, f
}
return sqltypes.VarChar, f
}
func (call *builtinToBase64) compile(c *compiler) (ctype, error) {
str, err := call.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck1(str)
t := sqltypes.VarChar
if str.Type == sqltypes.Blob || str.Type == sqltypes.TypeJSON {
t = sqltypes.Text
}
switch {
case str.isTextual():
default:
c.asm.Convert_xb(1, t, 0, false)
}
col := defaultCoercionCollation(c.cfg.Collation)
c.asm.Fn_TO_BASE64(t, col)
c.asm.jumpDestination(skip)
return ctype{Type: t, Col: col}, nil
}
func (call *builtinFromBase64) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
b := evalToBinary(arg)
decoded, err := mysqlBase64Decode(b.bytes)
if err != nil {
return nil, nil
}
if arg.SQLType() == sqltypes.Text || arg.SQLType() == sqltypes.TypeJSON {
return newEvalRaw(sqltypes.Blob, decoded, collationBinary), nil
}
return newEvalBinary(decoded), nil
}
func (call *builtinFromBase64) typeof(env *ExpressionEnv, fields []*querypb.Field) (sqltypes.Type, typeFlag) {
tt, f := call.Arguments[0].typeof(env, fields)
if tt == sqltypes.Text || tt == sqltypes.TypeJSON {
return sqltypes.Blob, f | flagNullable
}
return sqltypes.VarBinary, f | flagNullable
}
func (call *builtinFromBase64) compile(c *compiler) (ctype, error) {
str, err := call.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck1(str)
t := sqltypes.VarBinary
if str.Type == sqltypes.Blob || str.Type == sqltypes.TypeJSON {
t = sqltypes.Blob
}
switch {
case str.isTextual():
default:
c.asm.Convert_xb(1, t, 0, false)
}
c.asm.Fn_FROM_BASE64(t)
c.asm.jumpDestination(skip)
return ctype{Type: t, Col: collationBinary}, nil
}