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 | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 18 | func appendString(out []byte, in string) ([]byte, error) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 19 | var nerr errors.NonFatal |
| 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: |
| 26 | nerr.AppendInvalidUTF8("") |
| 27 | in, out = in[1:], append(out, in[0]) // preserve invalid byte |
| 28 | case r < ' ' || r == '"' || r == '\\': |
| 29 | out = append(out, '\\') |
| 30 | switch r { |
| 31 | case '"', '\\': |
| 32 | out = append(out, byte(r)) |
| 33 | case '\b': |
| 34 | out = append(out, 'b') |
| 35 | case '\f': |
| 36 | out = append(out, 'f') |
| 37 | case '\n': |
| 38 | out = append(out, 'n') |
| 39 | case '\r': |
| 40 | out = append(out, 'r') |
| 41 | case '\t': |
| 42 | out = append(out, 't') |
| 43 | default: |
| 44 | out = append(out, 'u') |
| 45 | out = append(out, "0000"[1+(bits.Len32(uint32(r))-1)/4:]...) |
| 46 | out = strconv.AppendUint(out, uint64(r), 16) |
| 47 | } |
| 48 | in = in[n:] |
| 49 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 50 | i := indexNeedEscapeInString(in[n:]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 51 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 52 | } |
| 53 | } |
| 54 | out = append(out, '"') |
| 55 | return out, nerr.E |
| 56 | } |
| 57 | |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 58 | func (d *Decoder) parseString(in []byte) (string, int, error) { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 59 | var nerr errors.NonFatal |
| 60 | in0 := in |
| 61 | if len(in) == 0 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 62 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 63 | } |
| 64 | if in[0] != '"' { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 65 | 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] | 66 | } |
| 67 | in = in[1:] |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 68 | i := indexNeedEscapeInBytes(in) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 69 | in, out := in[i:], in[:i:i] // set cap to prevent mutations |
| 70 | for len(in) > 0 { |
| 71 | switch r, n := utf8.DecodeRune(in); { |
| 72 | case r == utf8.RuneError && n == 1: |
| 73 | nerr.AppendInvalidUTF8("") |
| 74 | in, out = in[1:], append(out, in[0]) // preserve invalid byte |
| 75 | case r < ' ': |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 76 | return "", 0, d.newSyntaxError("invalid character %q in string", r) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 77 | case r == '"': |
| 78 | in = in[1:] |
| 79 | n := len(in0) - len(in) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 80 | return string(out), n, nerr.E |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 81 | case r == '\\': |
| 82 | if len(in) < 2 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 83 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 84 | } |
| 85 | switch r := in[1]; r { |
| 86 | case '"', '\\', '/': |
| 87 | in, out = in[2:], append(out, r) |
| 88 | case 'b': |
| 89 | in, out = in[2:], append(out, '\b') |
| 90 | case 'f': |
| 91 | in, out = in[2:], append(out, '\f') |
| 92 | case 'n': |
| 93 | in, out = in[2:], append(out, '\n') |
| 94 | case 'r': |
| 95 | in, out = in[2:], append(out, '\r') |
| 96 | case 't': |
| 97 | in, out = in[2:], append(out, '\t') |
| 98 | case 'u': |
| 99 | if len(in) < 6 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 100 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 101 | } |
| 102 | v, err := strconv.ParseUint(string(in[2:6]), 16, 16) |
| 103 | if err != nil { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 104 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:6]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 105 | } |
| 106 | in = in[6:] |
| 107 | |
| 108 | r := rune(v) |
| 109 | if utf16.IsSurrogate(r) { |
| 110 | if len(in) < 6 { |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 111 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 112 | } |
| 113 | v, err := strconv.ParseUint(string(in[2:6]), 16, 16) |
| 114 | r = utf16.DecodeRune(r, rune(v)) |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 115 | if in[0] != '\\' || in[1] != 'u' || |
| 116 | r == unicode.ReplacementChar || err != nil { |
| 117 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:6]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 118 | } |
| 119 | in = in[6:] |
| 120 | } |
| 121 | out = append(out, string(r)...) |
| 122 | default: |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 123 | return "", 0, d.newSyntaxError("invalid escape code %q in string", in[:2]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 124 | } |
| 125 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 126 | i := indexNeedEscapeInBytes(in[n:]) |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 127 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 128 | } |
| 129 | } |
Herbie Ong | d3f8f2d | 2019-03-06 00:28:23 -0800 | [diff] [blame] | 130 | return "", 0, io.ErrUnexpectedEOF |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 133 | // indexNeedEscapeInString returns the index of the character that needs |
| 134 | // escaping. If no characters need escaping, this returns the input length. |
| 135 | func indexNeedEscapeInString(s string) int { |
Joe Tsai | 879b18d | 2018-08-03 17:22:24 -0700 | [diff] [blame] | 136 | for i, r := range s { |
| 137 | if r < ' ' || r == '\\' || r == '"' || r == utf8.RuneError { |
| 138 | return i |
| 139 | } |
| 140 | } |
| 141 | return len(s) |
| 142 | } |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 143 | |
| 144 | // indexNeedEscapeInBytes returns the index of the character that needs |
| 145 | // escaping. If no characters need escaping, this returns the input length. |
| 146 | // TODO: Remove this duplicate function when https://golang.org/issue/31506 gets |
| 147 | // resolved. |
| 148 | func indexNeedEscapeInBytes(b []byte) int { |
| 149 | for i := 0; i < len(b); { |
| 150 | r, n := utf8.DecodeRune(b[i:]) |
| 151 | if r < ' ' || r == '\\' || r == '"' || r == utf8.RuneError { |
| 152 | return i |
| 153 | } |
| 154 | i += n |
| 155 | } |
| 156 | return len(b) |
| 157 | } |