-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathencode.go
404 lines (367 loc) · 12 KB
/
encode.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
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
// Copyright 2023 Dolthub, Inc.
//
// 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.
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in licenses/BSD-vitess.txt.
// Portions of this file are additionally subject to the following
// license and copyright.
//
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// This code was derived from https://github.com/youtube/vitess.
package lex
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"unicode/utf8"
"github.com/cockroachdb/errors"
"github.com/dolthub/doltgresql/postgres/parser/pgcode"
"github.com/dolthub/doltgresql/postgres/parser/pgerror"
"github.com/dolthub/doltgresql/postgres/parser/stringencoding"
)
var mustQuoteMap = map[byte]bool{
' ': true,
',': true,
'{': true,
'}': true,
}
// EncodeFlags influence the formatting of strings and identifiers.
type EncodeFlags int
// HasFlags tests whether the given flags are set.
func (f EncodeFlags) HasFlags(subset EncodeFlags) bool {
return f&subset == subset
}
const (
// EncNoFlags indicates nothing special should happen while encoding.
EncNoFlags EncodeFlags = 0
// EncBareStrings indicates that strings will be rendered without
// wrapping quotes if they contain no special characters.
EncBareStrings EncodeFlags = 1 << iota
// EncBareIdentifiers indicates that identifiers will be rendered
// without wrapping quotes.
EncBareIdentifiers
// EncFirstFreeFlagBit needs to remain unused; it is used as base
// bit offset for tree.FmtFlags.
EncFirstFreeFlagBit
)
// EncodeSQLString writes a string literal to buf. All unicode and
// non-printable characters are escaped.
func EncodeSQLString(buf *bytes.Buffer, in string) {
EncodeSQLStringWithFlags(buf, in, EncNoFlags)
}
// EscapeSQLString returns an escaped SQL representation of the given
// string. This is suitable for safely producing a SQL string valid
// for input to the parser.
func EscapeSQLString(in string) string {
var buf bytes.Buffer
EncodeSQLString(&buf, in)
return buf.String()
}
// EncodeSQLStringWithFlags writes a string literal to buf. All
// unicode and non-printable characters are escaped. flags controls
// the output format: if encodeBareString is set, the output string
// will not be wrapped in quotes if the strings contains no special
// characters.
func EncodeSQLStringWithFlags(buf *bytes.Buffer, in string, flags EncodeFlags) {
// See http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html
start := 0
escapedString := false
bareStrings := flags.HasFlags(EncBareStrings)
// Loop through each unicode code point.
for i, r := range in {
if i < start {
continue
}
ch := byte(r)
if r >= 0x20 && r < 0x7F {
if mustQuoteMap[ch] {
// We have to quote this string - ignore bareStrings setting
bareStrings = false
}
if !stringencoding.NeedEscape(ch) && ch != '\'' {
continue
}
}
if !escapedString {
buf.WriteString("e'") // begin e'xxx' string
escapedString = true
}
buf.WriteString(in[start:i])
ln := utf8.RuneLen(r)
if ln < 0 {
start = i + 1
} else {
start = i + ln
}
stringencoding.EncodeEscapedChar(buf, in, r, ch, i, '\'')
}
quote := !escapedString && !bareStrings
if quote {
buf.WriteByte('\'') // begin 'xxx' string if nothing was escaped
}
if start < len(in) {
buf.WriteString(in[start:])
}
if escapedString || quote {
buf.WriteByte('\'')
}
}
// EncodeUnrestrictedSQLIdent writes the identifier in s to buf.
// The identifier is only quoted if the flags don't tell otherwise and
// the identifier contains special characters.
func EncodeUnrestrictedSQLIdent(buf *bytes.Buffer, s string, flags EncodeFlags) {
if flags.HasFlags(EncBareIdentifiers) || isBareIdentifier(s) {
buf.WriteString(s)
return
}
EncodeEscapedSQLIdent(buf, s)
}
// EncodeRestrictedSQLIdent writes the identifier in s to buf. The
// identifier is quoted if either the flags ask for it, the identifier
// contains special characters, or the identifier is a reserved SQL
// keyword.
func EncodeRestrictedSQLIdent(buf *bytes.Buffer, s string, flags EncodeFlags) {
if flags.HasFlags(EncBareIdentifiers) || (!isReservedKeyword(s) && isBareIdentifier(s)) {
buf.WriteString(s)
return
}
EncodeEscapedSQLIdent(buf, s)
}
// EncodeEscapedSQLIdent writes the identifier in s to buf. The
// identifier is always quoted. Double quotes inside the identifier
// are escaped.
func EncodeEscapedSQLIdent(buf *bytes.Buffer, s string) {
buf.WriteByte('"')
start := 0
for i, n := 0, len(s); i < n; i++ {
ch := s[i]
// The only character that requires escaping is a double quote.
if ch == '"' {
if start != i {
buf.WriteString(s[start:i])
}
start = i + 1
buf.WriteByte(ch)
buf.WriteByte(ch) // add extra copy of ch
}
}
if start < len(s) {
buf.WriteString(s[start:])
}
buf.WriteByte('"')
}
// EncodeLocaleName writes the locale identifier in s to buf. Any dash
// characters are mapped to underscore characters. Underscore characters do not
// need to be quoted, and they are considered equivalent to dash characters by
// the CLDR standard: http://cldr.unicode.org/.
func EncodeLocaleName(buf *bytes.Buffer, s string) {
for i, n := 0, len(s); i < n; i++ {
ch := s[i]
if ch == '-' {
buf.WriteByte('_')
} else {
buf.WriteByte(ch)
}
}
}
// EncodeSQLBytes encodes the SQL byte array in 'in' to buf, to a
// format suitable for re-scanning. We don't use a straightforward hex
// encoding here with x'...' because the result would be less
// compact. We are trading a little more time during the encoding to
// have a little less bytes on the wire.
func EncodeSQLBytes(buf *bytes.Buffer, in string) {
start := 0
buf.WriteString("b'")
// Loop over the bytes of the string (i.e., don't use range over unicode
// code points).
for i, n := 0, len(in); i < n; i++ {
ch := in[i]
if encodedChar := stringencoding.EncodeMap[ch]; encodedChar != stringencoding.DontEscape {
buf.WriteString(in[start:i])
buf.WriteByte('\\')
buf.WriteByte(encodedChar)
start = i + 1
} else if ch == '\'' {
// We can't just fold this into stringencoding.EncodeMap because
// stringencoding.EncodeMap is also used for strings which
// aren't quoted with single-quotes
buf.WriteString(in[start:i])
buf.WriteByte('\\')
buf.WriteByte(ch)
start = i + 1
} else if ch < 0x20 || ch >= 0x7F {
buf.WriteString(in[start:i])
// Escape non-printable characters.
buf.Write(stringencoding.HexMap[ch])
start = i + 1
}
}
buf.WriteString(in[start:])
buf.WriteByte('\'')
}
// EncodeByteArrayToRawBytes converts a SQL-level byte array into raw
// bytes according to the encoding specification in "be".
// If the skipHexPrefix argument is set, the hexadecimal encoding does not
// prefix the output with "\x". This is suitable e.g. for the encode()
// built-in.
func EncodeByteArrayToRawBytes(data string, be BytesEncodeFormat, skipHexPrefix bool) string {
switch be {
case BytesEncodeHex:
head := 2
if skipHexPrefix {
head = 0
}
res := make([]byte, head+hex.EncodedLen(len(data)))
if !skipHexPrefix {
res[0] = '\\'
res[1] = 'x'
}
hex.Encode(res[head:], []byte(data))
return string(res)
case BytesEncodeEscape:
// PostgreSQL does not allow all the escapes formats recognized by
// CockroachDB's scanner. It only recognizes octal and \\ for the
// backslash itself.
// See https://www.postgresql.org/docs/current/static/datatype-binary.html#AEN5667
res := make([]byte, 0, len(data))
for _, c := range []byte(data) {
if c == '\\' {
res = append(res, '\\', '\\')
} else if c < 32 || c >= 127 {
// Escape the character in octal.
//
// Note: CockroachDB only supports UTF-8 for which all values
// below 128 are ASCII. There is no locale-dependent escaping
// in that case.
res = append(res, '\\', '0'+(c>>6), '0'+((c>>3)&7), '0'+(c&7))
} else {
res = append(res, c)
}
}
return string(res)
case BytesEncodeBase64:
return base64.StdEncoding.EncodeToString([]byte(data))
default:
panic(errors.AssertionFailedf("unhandled format: %s", be))
}
}
// DecodeRawBytesToByteArray converts raw bytes to a SQL-level byte array
// according to the encoding specification in "be".
// When using the Hex format, the caller is responsible for skipping the
// "\x" prefix, if any. See DecodeRawBytesToByteArrayAuto() below for
// an alternative.
func DecodeRawBytesToByteArray(data string, be BytesEncodeFormat) ([]byte, error) {
switch be {
case BytesEncodeHex:
return hex.DecodeString(data)
case BytesEncodeEscape:
// PostgreSQL does not allow all the escapes formats recognized by
// CockroachDB's scanner. It only recognizes octal and \\ for the
// backslash itself.
// See https://www.postgresql.org/docs/current/static/datatype-binary.html#AEN5667
res := make([]byte, 0, len(data))
for i := 0; i < len(data); i++ {
ch := data[i]
if ch != '\\' {
res = append(res, ch)
continue
}
if i >= len(data)-1 {
return nil, pgerror.New(pgcode.InvalidEscapeSequence,
"bytea encoded value ends with escape character")
}
if data[i+1] == '\\' {
res = append(res, '\\')
i++
continue
}
if i+3 >= len(data) {
return nil, pgerror.New(pgcode.InvalidEscapeSequence,
"bytea encoded value ends with incomplete escape sequence")
}
b := byte(0)
for j := 1; j <= 3; j++ {
octDigit := data[i+j]
if octDigit < '0' || octDigit > '7' || (j == 1 && octDigit > '3') {
return nil, pgerror.New(pgcode.InvalidEscapeSequence,
"invalid bytea escape sequence")
}
b = (b << 3) | (octDigit - '0')
}
res = append(res, b)
i += 3
}
return res, nil
case BytesEncodeBase64:
return base64.StdEncoding.DecodeString(data)
default:
return nil, errors.AssertionFailedf("unhandled format: %s", be)
}
}
// DecodeRawBytesToByteArrayAuto detects which format to use with
// DecodeRawBytesToByteArray(). It only supports hex ("\x" prefix)
// and escape.
func DecodeRawBytesToByteArrayAuto(data []byte) ([]byte, error) {
if len(data) >= 2 && data[0] == '\\' && (data[1] == 'x' || data[1] == 'X') {
return DecodeRawBytesToByteArray(string(data[2:]), BytesEncodeHex)
}
return DecodeRawBytesToByteArray(string(data), BytesEncodeEscape)
}
// BytesEncodeFormat controls which format to use for BYTES->STRING
// conversions.
type BytesEncodeFormat int
const (
// BytesEncodeHex uses the hex format: e'abc\n'::BYTES::STRING -> '\x61626312'.
// This is the default, for compatibility with PostgreSQL.
BytesEncodeHex BytesEncodeFormat = iota
// BytesEncodeEscape uses the escaped format: e'abc\n'::BYTES::STRING -> 'abc\012'.
BytesEncodeEscape
// BytesEncodeBase64 uses base64 encoding.
BytesEncodeBase64
)
func (f BytesEncodeFormat) String() string {
switch f {
case BytesEncodeHex:
return "hex"
case BytesEncodeEscape:
return "escape"
case BytesEncodeBase64:
return "base64"
default:
return fmt.Sprintf("invalid (%d)", f)
}
}
// BytesEncodeFormatFromString converts a string into a BytesEncodeFormat.
func BytesEncodeFormatFromString(val string) (_ BytesEncodeFormat, ok bool) {
switch strings.ToUpper(val) {
case "HEX":
return BytesEncodeHex, true
case "ESCAPE":
return BytesEncodeEscape, true
case "BASE64":
return BytesEncodeBase64, true
default:
return -1, false
}
}