| 1 |
// Copyright 2018 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package parser |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"strings" |
| 20 |
"unicode/utf8" |
| 21 |
) |
| 22 |
|
| 23 |
// Unescape takes a quoted string, unquotes, and unescapes it. |
| 24 |
// |
| 25 |
// This function performs escaping compatible with GoogleSQL. |
| 26 |
func unescape(value string, isBytes bool) (string, error) { |
| 27 |
// All strings normalize newlines to the \n representation. |
| 28 |
value = newlineNormalizer.Replace(value) |
| 29 |
n := len(value) |
| 30 |
|
| 31 |
// Nothing to unescape / decode. |
| 32 |
if n < 2 { |
| 33 |
return value, errors.New("unable to unescape string") |
| 34 |
} |
| 35 |
|
| 36 |
// Raw string preceded by the 'r|R' prefix. |
| 37 |
isRawLiteral := false |
| 38 |
if value[0] == 'r' || value[0] == 'R' { |
| 39 |
value = value[1:] |
| 40 |
n = len(value) |
| 41 |
isRawLiteral = true |
| 42 |
} |
| 43 |
|
| 44 |
// Quoted string of some form, must have same first and last char. |
| 45 |
if value[0] != value[n-1] || (value[0] != '"' && value[0] != '\'') { |
| 46 |
return value, errors.New("unable to unescape string") |
| 47 |
} |
| 48 |
|
| 49 |
// Normalize the multi-line CEL string representation to a standard |
| 50 |
// Go quoted string. |
| 51 |
if n >= 6 { |
| 52 |
if strings.HasPrefix(value, "'''") { |
| 53 |
if !strings.HasSuffix(value, "'''") { |
| 54 |
return value, errors.New("unable to unescape string") |
| 55 |
} |
| 56 |
value = "\"" + value[3:n-3] + "\"" |
| 57 |
} else if strings.HasPrefix(value, `"""`) { |
| 58 |
if !strings.HasSuffix(value, `"""`) { |
| 59 |
return value, errors.New("unable to unescape string") |
| 60 |
} |
| 61 |
value = "\"" + value[3:n-3] + "\"" |
| 62 |
} |
| 63 |
n = len(value) |
| 64 |
} |
| 65 |
value = value[1 : n-1] |
| 66 |
// If there is nothing to escape, then return. |
| 67 |
if isRawLiteral || !strings.ContainsRune(value, '\\') { |
| 68 |
return value, nil |
| 69 |
} |
| 70 |
|
| 71 |
// Otherwise the string contains escape characters. |
| 72 |
// The following logic is adapted from `strconv/quote.go` |
| 73 |
var runeTmp [utf8.UTFMax]byte |
| 74 |
buf := make([]byte, 0, 3*n/2) |
| 75 |
for len(value) > 0 { |
| 76 |
c, encode, rest, err := unescapeChar(value, isBytes) |
| 77 |
if err != nil { |
| 78 |
return "", err |
| 79 |
} |
| 80 |
value = rest |
| 81 |
if c < utf8.RuneSelf || !encode { |
| 82 |
buf = append(buf, byte(c)) |
| 83 |
} else { |
| 84 |
n := utf8.EncodeRune(runeTmp[:], c) |
| 85 |
buf = append(buf, runeTmp[:n]...) |
| 86 |
} |
| 87 |
} |
| 88 |
return string(buf), nil |
| 89 |
} |
| 90 |
|
| 91 |
// unescapeChar takes a string input and returns the following info: |
| 92 |
// |
| 93 |
// value - the escaped unicode rune at the front of the string. |
| 94 |
// encode - the value should be unicode-encoded |
| 95 |
// tail - the remainder of the input string. |
| 96 |
// err - error value, if the character could not be unescaped. |
| 97 |
// |
| 98 |
// When encode is true the return value may still fit within a single byte, |
| 99 |
// but unicode encoding is attempted which is more expensive than when the |
| 100 |
// value is known to self-represent as a single byte. |
| 101 |
// |
| 102 |
// If isBytes is set, unescape as a bytes literal so octal and hex escapes |
| 103 |
// represent byte values, not unicode code points. |
| 104 |
func unescapeChar(s string, isBytes bool) (value rune, encode bool, tail string, err error) { |
| 105 |
// 1. Character is not an escape sequence. |
| 106 |
switch c := s[0]; { |
| 107 |
case c >= utf8.RuneSelf: |
| 108 |
r, size := utf8.DecodeRuneInString(s) |
| 109 |
return r, true, s[size:], nil |
| 110 |
case c != '\\': |
| 111 |
return rune(s[0]), false, s[1:], nil |
| 112 |
} |
| 113 |
|
| 114 |
// 2. Last character is the start of an escape sequence. |
| 115 |
if len(s) <= 1 { |
| 116 |
err = errors.New("unable to unescape string, found '\\' as last character") |
| 117 |
return |
| 118 |
} |
| 119 |
|
| 120 |
c := s[1] |
| 121 |
s = s[2:] |
| 122 |
// 3. Common escape sequences shared with Google SQL |
| 123 |
switch c { |
| 124 |
case 'a': |
| 125 |
value = '\a' |
| 126 |
case 'b': |
| 127 |
value = '\b' |
| 128 |
case 'f': |
| 129 |
value = '\f' |
| 130 |
case 'n': |
| 131 |
value = '\n' |
| 132 |
case 'r': |
| 133 |
value = '\r' |
| 134 |
case 't': |
| 135 |
value = '\t' |
| 136 |
case 'v': |
| 137 |
value = '\v' |
| 138 |
case '\\': |
| 139 |
value = '\\' |
| 140 |
case '\'': |
| 141 |
value = '\'' |
| 142 |
case '"': |
| 143 |
value = '"' |
| 144 |
case '`': |
| 145 |
value = '`' |
| 146 |
case '?': |
| 147 |
value = '?' |
| 148 |
|
| 149 |
// 4. Unicode escape sequences, reproduced from `strconv/quote.go` |
| 150 |
case 'x', 'X', 'u', 'U': |
| 151 |
n := 0 |
| 152 |
encode = true |
| 153 |
switch c { |
| 154 |
case 'x', 'X': |
| 155 |
n = 2 |
| 156 |
encode = !isBytes |
| 157 |
case 'u': |
| 158 |
n = 4 |
| 159 |
if isBytes { |
| 160 |
err = errors.New("unable to unescape string") |
| 161 |
return |
| 162 |
} |
| 163 |
case 'U': |
| 164 |
n = 8 |
| 165 |
if isBytes { |
| 166 |
err = errors.New("unable to unescape string") |
| 167 |
return |
| 168 |
} |
| 169 |
} |
| 170 |
var v rune |
| 171 |
if len(s) < n { |
| 172 |
err = errors.New("unable to unescape string") |
| 173 |
return |
| 174 |
} |
| 175 |
for j := 0; j < n; j++ { |
| 176 |
x, ok := unhex(s[j]) |
| 177 |
if !ok { |
| 178 |
err = errors.New("unable to unescape string") |
| 179 |
return |
| 180 |
} |
| 181 |
v = v<<4 | x |
| 182 |
} |
| 183 |
s = s[n:] |
| 184 |
if !isBytes && !utf8.ValidRune(v) { |
| 185 |
err = errors.New("invalid unicode code point") |
| 186 |
return |
| 187 |
} |
| 188 |
value = v |
| 189 |
|
| 190 |
// 5. Octal escape sequences, must be three digits \[0-3][0-7][0-7] |
| 191 |
case '0', '1', '2', '3': |
| 192 |
if len(s) < 2 { |
| 193 |
err = errors.New("unable to unescape octal sequence in string") |
| 194 |
return |
| 195 |
} |
| 196 |
v := rune(c - '0') |
| 197 |
for j := 0; j < 2; j++ { |
| 198 |
x := s[j] |
| 199 |
if x < '0' || x > '7' { |
| 200 |
err = errors.New("unable to unescape octal sequence in string") |
| 201 |
return |
| 202 |
} |
| 203 |
v = v*8 + rune(x-'0') |
| 204 |
} |
| 205 |
if !isBytes && !utf8.ValidRune(v) { |
| 206 |
err = errors.New("invalid unicode code point") |
| 207 |
return |
| 208 |
} |
| 209 |
value = v |
| 210 |
s = s[2:] |
| 211 |
encode = !isBytes |
| 212 |
|
| 213 |
// Unknown escape sequence. |
| 214 |
default: |
| 215 |
err = errors.New("unable to unescape string") |
| 216 |
} |
| 217 |
|
| 218 |
tail = s |
| 219 |
return |
| 220 |
} |
| 221 |
|
| 222 |
func unhex(b byte) (rune, bool) { |
| 223 |
c := rune(b) |
| 224 |
switch { |
| 225 |
case '0' <= c && c <= '9': |
| 226 |
return c - '0', true |
| 227 |
case 'a' <= c && c <= 'f': |
| 228 |
return c - 'a' + 10, true |
| 229 |
case 'A' <= c && c <= 'F': |
| 230 |
return c - 'A' + 10, true |
| 231 |
} |
| 232 |
return 0, false |
| 233 |
} |
| 234 |
|
| 235 |
var ( |
| 236 |
newlineNormalizer = strings.NewReplacer("\r\n", "\n", "\r", "\n") |
| 237 |
) |