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