Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package json |
| 6 | |
| 7 | import ( |
| 8 | "io" |
| 9 | "math/bits" |
| 10 | "strconv" |
| 11 | "unicode" |
| 12 | "unicode/utf16" |
| 13 | "unicode/utf8" |
| 14 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 36dc22d | 2019-07-09 23:20:27 -0700 | [diff] [blame^] | 16 | "google.golang.org/protobuf/internal/strs" |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 17 | ) |
| 18 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 19 | func appendString(out []byte, in string) ([]byte, error) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 20 | out = append(out, '"') |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 21 | i := indexNeedEscapeInString(in) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 22 | in, out = in[i:], append(out, in[:i]...) |
| 23 | for len(in) > 0 { |
| 24 | switch r, n := utf8.DecodeRuneInString(in); { |
| 25 | case r == utf8.RuneError && n == 1: |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 26 | return out, errors.InvalidUTF8("") |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 27 | case r < ' ' || r == '"' || r == '\\': |
| 28 | out = append(out, '\\') |
| 29 | switch r { |
| 30 | case '"', '\\': |
| 31 | out = append(out, byte(r)) |
| 32 | case '\b': |
| 33 | out = append(out, 'b') |
| 34 | case '\f': |
| 35 | out = append(out, 'f') |
| 36 | case '\n': |
| 37 | out = append(out, 'n') |
| 38 | case '\r': |
| 39 | out = append(out, 'r') |
| 40 | case '\t': |
| 41 | out = append(out, 't') |
| 42 | default: |
| 43 | out = append(out, 'u') |
| 44 | out = append(out, "0000"[1+(bits.Len32(uint32(r))-1)/4:]...) |
| 45 | out = strconv.AppendUint(out, uint64(r), 16) |
| 46 | } |
| 47 | in = in[n:] |
| 48 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 49 | i := indexNeedEscapeInString(in[n:]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 50 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 51 | } |
| 52 | } |
| 53 | out = append(out, '"') |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 54 | return out, nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 57 | func (d *Decoder) parseString(in []byte) (string, int, error) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 58 | in0 := in |
| 59 | if len(in) == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 60 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 61 | } |
| 62 | if in[0] != '"' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 63 | return "", 0, d.newSyntaxError("invalid character %q at start of string", in[0]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 64 | } |
| 65 | in = in[1:] |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 66 | i := indexNeedEscapeInBytes(in) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 67 | in, out := in[i:], in[:i:i] // set cap to prevent mutations |
| 68 | for len(in) > 0 { |
| 69 | switch r, n := utf8.DecodeRune(in); { |
| 70 | case r == utf8.RuneError && n == 1: |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 71 | return "", 0, d.newSyntaxError("invalid UTF-8 in string") |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 72 | case r < ' ': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 73 | return "", 0, d.newSyntaxError("invalid character %q in string", r) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 74 | case r == '"': |
| 75 | in = in[1:] |
| 76 | n := len(in0) - len(in) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 77 | return string(out), n, nil |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 78 | case r == '\\': |
| 79 | if len(in) < 2 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 80 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 81 | } |
| 82 | switch r := in[1]; r { |
| 83 | case '"', '\\', '/': |
| 84 | in, out = in[2:], append(out, r) |
| 85 | case 'b': |
| 86 | in, out = in[2:], append(out, '\b') |
| 87 | case 'f': |
| 88 | in, out = in[2:], append(out, '\f') |
| 89 | case 'n': |
| 90 | in, out = in[2:], append(out, '\n') |
| 91 | case 'r': |
| 92 | in, out = in[2:], append(out, '\r') |
| 93 | case 't': |
| 94 | in, out = in[2:], append(out, '\t') |
| 95 | case 'u': |
| 96 | if len(in) < 6 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 97 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 98 | } |
| 99 | v, err := strconv.ParseUint(string(in[2:6]), 16, 16) |
| 100 | if err != nil { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 101 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:6]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 102 | } |
| 103 | in = in[6:] |
| 104 | |
| 105 | r := rune(v) |
| 106 | if utf16.IsSurrogate(r) { |
| 107 | if len(in) < 6 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 108 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 109 | } |
| 110 | v, err := strconv.ParseUint(string(in[2:6]), 16, 16) |
| 111 | r = utf16.DecodeRune(r, rune(v)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 112 | if in[0] != '\\' || in[1] != 'u' || |
| 113 | r == unicode.ReplacementChar || err != nil { |
| 114 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:6]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 115 | } |
| 116 | in = in[6:] |
| 117 | } |
| 118 | out = append(out, string(r)...) |
| 119 | default: |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 120 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:2]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 121 | } |
| 122 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 123 | i := indexNeedEscapeInBytes(in[n:]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 124 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 125 | } |
| 126 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 127 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 130 | // indexNeedEscapeInString returns the index of the character that needs |
| 131 | // escaping. If no characters need escaping, this returns the input length. |
| 132 | func indexNeedEscapeInString(s string) int { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 133 | for i, r := range s { |
| 134 | if r < ' ' || r == '\\' || r == '"' || r == utf8.RuneError { |
| 135 | return i |
| 136 | } |
| 137 | } |
| 138 | return len(s) |
| 139 | } |
Joe Tsai | 36dc22d | 2019-07-09 23:20:27 -0700 | [diff] [blame^] | 140 | func indexNeedEscapeInBytes(b []byte) int { return indexNeedEscapeInString(strs.UnsafeString(b)) } |