SYS-REQ-115
(config Config, in, out []byte)
| 183 | |
| 184 | // SYS-REQ-115 |
| 185 | func unescapeToUTF8Config(config Config, in, out []byte) (inLen int, outLen int) { |
| 186 | if len(in) < 2 || in[0] != '\\' { |
| 187 | // Invalid escape due to insufficient characters for any escape or no initial backslash |
| 188 | return -1, -1 |
| 189 | } |
| 190 | |
| 191 | // https://tools.ietf.org/html/rfc7159#section-7 |
| 192 | switch e := in[1]; e { |
| 193 | case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': |
| 194 | // Valid basic 2-character escapes (use lookup table) |
| 195 | out[0] = backslashCharEscapeTable[e] |
| 196 | return 2, 1 |
| 197 | case '\'': |
| 198 | if config.AllowSingleQuotes { |
| 199 | out[0] = e |
| 200 | return 2, 1 |
| 201 | } |
| 202 | case 'u': |
| 203 | // Unicode escape |
| 204 | if r, inLen := decodeUnicodeEscape(in); inLen == -1 { |
| 205 | // Invalid Unicode escape |
| 206 | return -1, -1 |
| 207 | } else { |
| 208 | // Valid Unicode escape; re-encode as UTF8 |
| 209 | outLen := utf8.EncodeRune(out, r) |
| 210 | return inLen, outLen |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if config.AllowUnknownEscapes { |
| 215 | // Lenient mode treats the escaped byte as a literal and discards the |
| 216 | // escape marker. A trailing '\' and malformed \u escape remain errors: |
| 217 | // they are truncated/invalid encodings, not unknown escape names. |
| 218 | out[0] = in[1] |
| 219 | return 2, 1 |
| 220 | } |
| 221 | |
| 222 | return -1, -1 |
| 223 | } |
| 224 | |
| 225 | const lowerHex = "0123456789abcdef" |
| 226 |
no test coverage detected