Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -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 text |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "io" |
| 10 | "math" |
| 11 | "math/bits" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "unicode" |
| 15 | "unicode/utf16" |
| 16 | "unicode/utf8" |
| 17 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 18 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 36dc22d | 2019-07-09 23:20:27 -0700 | [diff] [blame] | 19 | "google.golang.org/protobuf/internal/strs" |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func (p *encoder) marshalString(v Value) error { |
| 23 | var err error |
| 24 | p.out, err = appendString(p.out, v, p.outputASCII) |
| 25 | return err |
| 26 | } |
| 27 | func appendString(out []byte, v Value, outputASCII bool) ([]byte, error) { |
| 28 | if v.Type() != String { |
| 29 | return nil, errors.New("invalid type %v, expected string", v.Type()) |
| 30 | } |
| 31 | if len(v.raw) > 0 { |
| 32 | return append(out, v.raw...), nil |
| 33 | } |
| 34 | in := v.String() |
| 35 | |
| 36 | out = append(out, '"') |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 37 | i := indexNeedEscapeInString(in) |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 38 | in, out = in[i:], append(out, in[:i]...) |
| 39 | for len(in) > 0 { |
| 40 | switch r, n := utf8.DecodeRuneInString(in); { |
| 41 | case r == utf8.RuneError && n == 1: |
| 42 | // We do not report invalid UTF-8 because strings in the text format |
| 43 | // are used to represent both the proto string and bytes type. |
| 44 | r = rune(in[0]) |
| 45 | fallthrough |
| 46 | case r < ' ' || r == '"' || r == '\\': |
| 47 | out = append(out, '\\') |
| 48 | switch r { |
| 49 | case '"', '\\': |
| 50 | out = append(out, byte(r)) |
| 51 | case '\n': |
| 52 | out = append(out, 'n') |
| 53 | case '\r': |
| 54 | out = append(out, 'r') |
| 55 | case '\t': |
| 56 | out = append(out, 't') |
| 57 | default: |
| 58 | out = append(out, 'x') |
| 59 | out = append(out, "00"[1+(bits.Len32(uint32(r))-1)/4:]...) |
| 60 | out = strconv.AppendUint(out, uint64(r), 16) |
| 61 | } |
| 62 | in = in[n:] |
| 63 | case outputASCII && r >= utf8.RuneSelf: |
| 64 | out = append(out, '\\') |
| 65 | if r <= math.MaxUint16 { |
| 66 | out = append(out, 'u') |
| 67 | out = append(out, "0000"[1+(bits.Len32(uint32(r))-1)/4:]...) |
| 68 | out = strconv.AppendUint(out, uint64(r), 16) |
| 69 | } else { |
| 70 | out = append(out, 'U') |
| 71 | out = append(out, "00000000"[1+(bits.Len32(uint32(r))-1)/4:]...) |
| 72 | out = strconv.AppendUint(out, uint64(r), 16) |
| 73 | } |
| 74 | in = in[n:] |
| 75 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 76 | i := indexNeedEscapeInString(in[n:]) |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 77 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 78 | } |
| 79 | } |
| 80 | out = append(out, '"') |
| 81 | return out, nil |
| 82 | } |
| 83 | |
| 84 | func (p *decoder) unmarshalString() (Value, error) { |
| 85 | v, n, err := consumeString(p.in) |
| 86 | p.consume(n) |
| 87 | return v, err |
| 88 | } |
| 89 | func consumeString(in []byte) (Value, int, error) { |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 90 | in0 := in |
| 91 | if len(in) == 0 { |
| 92 | return Value{}, 0, io.ErrUnexpectedEOF |
| 93 | } |
| 94 | quote := in[0] |
| 95 | if in[0] != '"' && in[0] != '\'' { |
| 96 | return Value{}, 0, newSyntaxError("invalid character %q at start of string", in[0]) |
| 97 | } |
| 98 | in = in[1:] |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 99 | i := indexNeedEscapeInBytes(in) |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 100 | in, out := in[i:], in[:i:i] // set cap to prevent mutations |
| 101 | for len(in) > 0 { |
| 102 | switch r, n := utf8.DecodeRune(in); { |
| 103 | case r == utf8.RuneError && n == 1: |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 104 | return Value{}, 0, newSyntaxError("invalid UTF-8 detected") |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 105 | case r == 0 || r == '\n': |
| 106 | return Value{}, 0, newSyntaxError("invalid character %q in string", r) |
| 107 | case r == rune(quote): |
| 108 | in = in[1:] |
| 109 | n := len(in0) - len(in) |
| 110 | v := rawValueOf(string(out), in0[:n:n]) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 111 | return v, n, nil |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 112 | case r == '\\': |
| 113 | if len(in) < 2 { |
| 114 | return Value{}, 0, io.ErrUnexpectedEOF |
| 115 | } |
| 116 | switch r := in[1]; r { |
| 117 | case '"', '\'', '\\', '?': |
| 118 | in, out = in[2:], append(out, r) |
| 119 | case 'a': |
| 120 | in, out = in[2:], append(out, '\a') |
| 121 | case 'b': |
| 122 | in, out = in[2:], append(out, '\b') |
| 123 | case 'n': |
| 124 | in, out = in[2:], append(out, '\n') |
| 125 | case 'r': |
| 126 | in, out = in[2:], append(out, '\r') |
| 127 | case 't': |
| 128 | in, out = in[2:], append(out, '\t') |
| 129 | case 'v': |
| 130 | in, out = in[2:], append(out, '\v') |
| 131 | case 'f': |
| 132 | in, out = in[2:], append(out, '\f') |
| 133 | case '0', '1', '2', '3', '4', '5', '6', '7': |
| 134 | // One, two, or three octal characters. |
| 135 | n := len(in[1:]) - len(bytes.TrimLeft(in[1:], "01234567")) |
| 136 | if n > 3 { |
| 137 | n = 3 |
| 138 | } |
| 139 | v, err := strconv.ParseUint(string(in[1:1+n]), 8, 8) |
| 140 | if err != nil { |
| 141 | return Value{}, 0, newSyntaxError("invalid octal escape code %q in string", in[:1+n]) |
| 142 | } |
| 143 | in, out = in[1+n:], append(out, byte(v)) |
| 144 | case 'x': |
| 145 | // One or two hexadecimal characters. |
| 146 | n := len(in[2:]) - len(bytes.TrimLeft(in[2:], "0123456789abcdefABCDEF")) |
| 147 | if n > 2 { |
| 148 | n = 2 |
| 149 | } |
| 150 | v, err := strconv.ParseUint(string(in[2:2+n]), 16, 8) |
| 151 | if err != nil { |
| 152 | return Value{}, 0, newSyntaxError("invalid hex escape code %q in string", in[:2+n]) |
| 153 | } |
| 154 | in, out = in[2+n:], append(out, byte(v)) |
| 155 | case 'u', 'U': |
| 156 | // Four or eight hexadecimal characters |
| 157 | n := 6 |
| 158 | if r == 'U' { |
| 159 | n = 10 |
| 160 | } |
| 161 | if len(in) < n { |
| 162 | return Value{}, 0, io.ErrUnexpectedEOF |
| 163 | } |
| 164 | v, err := strconv.ParseUint(string(in[2:n]), 16, 32) |
| 165 | if utf8.MaxRune < v || err != nil { |
| 166 | return Value{}, 0, newSyntaxError("invalid Unicode escape code %q in string", in[:n]) |
| 167 | } |
| 168 | in = in[n:] |
| 169 | |
| 170 | r := rune(v) |
| 171 | if utf16.IsSurrogate(r) { |
| 172 | if len(in) < 6 { |
| 173 | return Value{}, 0, io.ErrUnexpectedEOF |
| 174 | } |
| 175 | v, err := strconv.ParseUint(string(in[2:6]), 16, 16) |
| 176 | r = utf16.DecodeRune(r, rune(v)) |
| 177 | if in[0] != '\\' || in[1] != 'u' || r == unicode.ReplacementChar || err != nil { |
| 178 | return Value{}, 0, newSyntaxError("invalid Unicode escape code %q in string", in[:6]) |
| 179 | } |
| 180 | in = in[6:] |
| 181 | } |
| 182 | out = append(out, string(r)...) |
| 183 | default: |
| 184 | return Value{}, 0, newSyntaxError("invalid escape code %q in string", in[:2]) |
| 185 | } |
| 186 | default: |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 187 | i := indexNeedEscapeInBytes(in[n:]) |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 188 | in, out = in[n+i:], append(out, in[:n+i]...) |
| 189 | } |
| 190 | } |
| 191 | return Value{}, 0, io.ErrUnexpectedEOF |
| 192 | } |
| 193 | |
| 194 | // unmarshalStrings unmarshals multiple strings. |
| 195 | // This differs from unmarshalString since the text format allows |
| 196 | // multiple back-to-back string literals where they are semantically treated |
| 197 | // as a single large string with all values concatenated. |
| 198 | // |
| 199 | // E.g., `"foo" "bar" "baz"` => ValueOf("foobarbaz") |
| 200 | func (p *decoder) unmarshalStrings() (Value, error) { |
| 201 | // Note that the ending quote is sufficient to unambiguously mark the end |
| 202 | // of a string. Thus, the text grammar does not require intervening |
| 203 | // whitespace or control characters in-between strings. |
| 204 | // Thus, the following is valid: |
| 205 | // `"foo"'bar'"baz"` => ValueOf("foobarbaz") |
| 206 | b := p.in |
| 207 | var ss []string |
| 208 | for len(p.in) > 0 && (p.in[0] == '"' || p.in[0] == '\'') { |
| 209 | v, err := p.unmarshalString() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 210 | if err != nil { |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 211 | return Value{}, err |
| 212 | } |
| 213 | ss = append(ss, v.String()) |
| 214 | } |
| 215 | b = b[:len(b)-len(p.in)] |
| 216 | return rawValueOf(strings.Join(ss, ""), b[:len(b):len(b)]), nil |
| 217 | } |
| 218 | |
Herbie Ong | 1e09691 | 2019-04-16 00:14:03 -0700 | [diff] [blame] | 219 | // indexNeedEscapeInString returns the index of the character that needs |
| 220 | // escaping. If no characters need escaping, this returns the input length. |
| 221 | func indexNeedEscapeInString(s string) int { |
Joe Tsai | 27c2a76 | 2018-08-01 16:48:18 -0700 | [diff] [blame] | 222 | for i := 0; i < len(s); i++ { |
| 223 | if c := s[i]; c < ' ' || c == '"' || c == '\'' || c == '\\' || c >= utf8.RuneSelf { |
| 224 | return i |
| 225 | } |
| 226 | } |
| 227 | return len(s) |
| 228 | } |
Joe Tsai | 36dc22d | 2019-07-09 23:20:27 -0700 | [diff] [blame] | 229 | func indexNeedEscapeInBytes(b []byte) int { return indexNeedEscapeInString(strs.UnsafeString(b)) } |