Debian Codegraph @

internal/decoder/unmarshal_text.go

raw
1 package decoder
2
3 import (
4 "encoding"
5 "fmt"
6 "reflect"
7 "unicode"
8 "unicode/utf16"
9 "unicode/utf8"
10 "unsafe"
11
12 "github.com/goccy/go-json/internal/runtime"
13 )
14
15 type unmarshalTextDecoder struct {
16 typ reflect.Type
17 structName string
18 fieldName string
19 // nullSetsZero is set for a value which null sets to its zero value ( see textUnmarshalerNullSetsZero ): null
20 // leaves any other value as it is, and is not given to UnmarshalText.
21 nullSetsZero bool
22 }
23
24 func newUnmarshalTextDecoder(typ reflect.Type, structName, fieldName string) *unmarshalTextDecoder {
25 return &unmarshalTextDecoder{
26 typ: typ,
27 structName: structName,
28 fieldName: fieldName,
29 nullSetsZero: textUnmarshalerNullSetsZero(typ.Elem().Kind()),
30 }
31 }
32
33 var (
34 nullbytes = []byte(`null`)
35 )
36
37 func (d *unmarshalTextDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
38 buf := ctx.Buf
39 cursor = skipWhiteSpace(buf, cursor)
40 start := cursor
41 end, err := skipValue(buf, cursor, depth)
42 if err != nil {
43 return 0, err
44 }
45 src := buf[start:end]
46 switch c := src[0]; {
47 case c == 'n':
48 if d.nullSetsZero {
49 reflect.NewAt(d.typ.Elem(), p).Elem().SetZero()
50 }
51 return end, nil
52 case c != '"':
53 // a value of another kind than a string, which is a type error
54 return ctx.textUnmarshalerKindError(start, depth, d.typ)
55 }
56
57 if s, ok := unquoteBytes(src); ok {
58 src = s
59 }
60 v := *(*any)(unsafe.Pointer(&emptyInterface{
61 typ: runtime.TypePtr(d.typ),
62 ptr: *(*unsafe.Pointer)(unsafe.Pointer(&p)),
63 }))
64 if err := v.(encoding.TextUnmarshaler).UnmarshalText(src); err != nil {
65 return ctx.methodError(cursor, end, err, d.structName, d.fieldName)
66 }
67 return end, nil
68 }
69
70 func (d *unmarshalTextDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
71 return nil, 0, fmt.Errorf("json: unmarshal text decoder does not support decode path")
72 }
73
74 func unquoteBytes(s []byte) (t []byte, ok bool) { //nolint: nonamedreturns
75 length := len(s)
76 if length < 2 || s[0] != '"' || s[length-1] != '"' {
77 return
78 }
79 s = s[1 : length-1]
80 length -= 2
81
82 // Check for unusual characters. If there are none,
83 // then no unquoting is needed, so return a slice of the
84 // original bytes.
85 r := 0
86 for r < length {
87 c := s[r]
88 if c == '\\' || c == '"' || c < ' ' {
89 break
90 }
91 if c < utf8.RuneSelf {
92 r++
93 continue
94 }
95 rr, size := utf8.DecodeRune(s[r:])
96 if rr == utf8.RuneError && size == 1 {
97 break
98 }
99 r += size
100 }
101 if r == length {
102 return s, true
103 }
104
105 b := make([]byte, length+2*utf8.UTFMax)
106 w := copy(b, s[0:r])
107 for r < length {
108 // Out of room? Can only happen if s is full of
109 // malformed UTF-8 and we're replacing each
110 // byte with RuneError.
111 if w >= len(b)-2*utf8.UTFMax {
112 nb := make([]byte, (len(b)+utf8.UTFMax)*2)
113 copy(nb, b[0:w])
114 b = nb
115 }
116 switch c := s[r]; {
117 case c == '\\':
118 r++
119 if r >= length {
120 return
121 }
122 switch s[r] {
123 default:
124 return
125 case '"', '\\', '/', '\'':
126 b[w] = s[r]
127 r++
128 w++
129 case 'b':
130 b[w] = '\b'
131 r++
132 w++
133 case 'f':
134 b[w] = '\f'
135 r++
136 w++
137 case 'n':
138 b[w] = '\n'
139 r++
140 w++
141 case 'r':
142 b[w] = '\r'
143 r++
144 w++
145 case 't':
146 b[w] = '\t'
147 r++
148 w++
149 case 'u':
150 r--
151 rr := getu4(s[r:])
152 if rr < 0 {
153 return
154 }
155 r += 6
156 if utf16.IsSurrogate(rr) {
157 rr1 := getu4(s[r:])
158 if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar {
159 // A valid pair; consume.
160 r += 6
161 w += utf8.EncodeRune(b[w:], dec)
162 break
163 }
164 // Invalid surrogate; fall back to replacement rune.
165 rr = unicode.ReplacementChar
166 }
167 w += utf8.EncodeRune(b[w:], rr)
168 }
169
170 // Quote, control characters are invalid.
171 case c == '"', c < ' ':
172 return
173
174 // ASCII
175 case c < utf8.RuneSelf:
176 b[w] = c
177 r++
178 w++
179
180 // Coerce to well-formed UTF-8.
181 default:
182 rr, size := utf8.DecodeRune(s[r:])
183 r += size
184 w += utf8.EncodeRune(b[w:], rr)
185 }
186 }
187 return b[0:w], true
188 }
189
190 func getu4(s []byte) rune {
191 if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
192 return -1
193 }
194 var r rune
195 for _, c := range s[2:6] {
196 switch {
197 case '0' <= c && c <= '9':
198 c = c - '0'
199 case 'a' <= c && c <= 'f':
200 c = c - 'a' + 10
201 case 'A' <= c && c <= 'F':
202 c = c - 'A' + 10
203 default:
204 return -1
205 }
206 r = r*16 + rune(c)
207 }
208 return r
209 }