1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package yaml
24
25 import (
26 "io"
27 )
28
29
30 func yaml_parser_set_reader_error(parser *yaml_parser_t, problem string, offset int, value int) bool {
31 parser.error = yaml_READER_ERROR
32 parser.problem = problem
33 parser.problem_offset = offset
34 parser.problem_value = value
35 return false
36 }
37
38
39 const (
40 bom_UTF8 = "\xef\xbb\xbf"
41 bom_UTF16LE = "\xff\xfe"
42 bom_UTF16BE = "\xfe\xff"
43 )
44
45
46
47 func yaml_parser_determine_encoding(parser *yaml_parser_t) bool {
48
49 for !parser.eof && len(parser.raw_buffer)-parser.raw_buffer_pos < 3 {
50 if !yaml_parser_update_raw_buffer(parser) {
51 return false
52 }
53 }
54
55
56 buf := parser.raw_buffer
57 pos := parser.raw_buffer_pos
58 avail := len(buf) - pos
59 if avail >= 2 && buf[pos] == bom_UTF16LE[0] && buf[pos+1] == bom_UTF16LE[1] {
60 parser.encoding = yaml_UTF16LE_ENCODING
61 parser.raw_buffer_pos += 2
62 parser.offset += 2
63 } else if avail >= 2 && buf[pos] == bom_UTF16BE[0] && buf[pos+1] == bom_UTF16BE[1] {
64 parser.encoding = yaml_UTF16BE_ENCODING
65 parser.raw_buffer_pos += 2
66 parser.offset += 2
67 } else if avail >= 3 && buf[pos] == bom_UTF8[0] && buf[pos+1] == bom_UTF8[1] && buf[pos+2] == bom_UTF8[2] {
68 parser.encoding = yaml_UTF8_ENCODING
69 parser.raw_buffer_pos += 3
70 parser.offset += 3
71 } else {
72 parser.encoding = yaml_UTF8_ENCODING
73 }
74 return true
75 }
76
77
78 func yaml_parser_update_raw_buffer(parser *yaml_parser_t) bool {
79 size_read := 0
80
81
82 if parser.raw_buffer_pos == 0 && len(parser.raw_buffer) == cap(parser.raw_buffer) {
83 return true
84 }
85
86
87 if parser.eof {
88 return true
89 }
90
91
92 if parser.raw_buffer_pos > 0 && parser.raw_buffer_pos < len(parser.raw_buffer) {
93 copy(parser.raw_buffer, parser.raw_buffer[parser.raw_buffer_pos:])
94 }
95 parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)-parser.raw_buffer_pos]
96 parser.raw_buffer_pos = 0
97
98
99 size_read, err := parser.read_handler(parser, parser.raw_buffer[len(parser.raw_buffer):cap(parser.raw_buffer)])
100 parser.raw_buffer = parser.raw_buffer[:len(parser.raw_buffer)+size_read]
101 if err == io.EOF {
102 parser.eof = true
103 } else if err != nil {
104 return yaml_parser_set_reader_error(parser, "input error: "+err.Error(), parser.offset, -1)
105 }
106 return true
107 }
108
109
110
111
112
113 func yaml_parser_update_buffer(parser *yaml_parser_t, length int) bool {
114 if parser.read_handler == nil {
115 panic("read handler must be set")
116 }
117
118
119
120
121
122
123 if parser.eof && parser.raw_buffer_pos == len(parser.raw_buffer) {
124
125
126
127
128
129
130 }
131
132
133 if parser.unread >= length {
134 return true
135 }
136
137
138 if parser.encoding == yaml_ANY_ENCODING {
139 if !yaml_parser_determine_encoding(parser) {
140 return false
141 }
142 }
143
144
145 buffer_len := len(parser.buffer)
146 if parser.buffer_pos > 0 && parser.buffer_pos < buffer_len {
147 copy(parser.buffer, parser.buffer[parser.buffer_pos:])
148 buffer_len -= parser.buffer_pos
149 parser.buffer_pos = 0
150 } else if parser.buffer_pos == buffer_len {
151 buffer_len = 0
152 parser.buffer_pos = 0
153 }
154
155
156 parser.buffer = parser.buffer[:cap(parser.buffer)]
157
158
159 first := true
160 for parser.unread < length {
161
162
163 if !first || parser.raw_buffer_pos == len(parser.raw_buffer) {
164 if !yaml_parser_update_raw_buffer(parser) {
165 parser.buffer = parser.buffer[:buffer_len]
166 return false
167 }
168 }
169 first = false
170
171
172 inner:
173 for parser.raw_buffer_pos != len(parser.raw_buffer) {
174 var value rune
175 var width int
176
177 raw_unread := len(parser.raw_buffer) - parser.raw_buffer_pos
178
179
180 switch parser.encoding {
181 case yaml_UTF8_ENCODING:
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 octet := parser.raw_buffer[parser.raw_buffer_pos]
202 switch {
203 case octet&0x80 == 0x00:
204 width = 1
205 case octet&0xE0 == 0xC0:
206 width = 2
207 case octet&0xF0 == 0xE0:
208 width = 3
209 case octet&0xF8 == 0xF0:
210 width = 4
211 default:
212
213 return yaml_parser_set_reader_error(parser,
214 "invalid leading UTF-8 octet",
215 parser.offset, int(octet))
216 }
217
218
219 if width > raw_unread {
220 if parser.eof {
221 return yaml_parser_set_reader_error(parser,
222 "incomplete UTF-8 octet sequence",
223 parser.offset, -1)
224 }
225 break inner
226 }
227
228
229 switch {
230 case octet&0x80 == 0x00:
231 value = rune(octet & 0x7F)
232 case octet&0xE0 == 0xC0:
233 value = rune(octet & 0x1F)
234 case octet&0xF0 == 0xE0:
235 value = rune(octet & 0x0F)
236 case octet&0xF8 == 0xF0:
237 value = rune(octet & 0x07)
238 default:
239 value = 0
240 }
241
242
243 for k := 1; k < width; k++ {
244 octet = parser.raw_buffer[parser.raw_buffer_pos+k]
245
246
247 if (octet & 0xC0) != 0x80 {
248 return yaml_parser_set_reader_error(parser,
249 "invalid trailing UTF-8 octet",
250 parser.offset+k, int(octet))
251 }
252
253
254 value = (value << 6) + rune(octet&0x3F)
255 }
256
257
258 switch {
259 case width == 1:
260 case width == 2 && value >= 0x80:
261 case width == 3 && value >= 0x800:
262 case width == 4 && value >= 0x10000:
263 default:
264 return yaml_parser_set_reader_error(parser,
265 "invalid length of a UTF-8 sequence",
266 parser.offset, -1)
267 }
268
269
270 if value >= 0xD800 && value <= 0xDFFF || value > 0x10FFFF {
271 return yaml_parser_set_reader_error(parser,
272 "invalid Unicode character",
273 parser.offset, int(value))
274 }
275
276 case yaml_UTF16LE_ENCODING, yaml_UTF16BE_ENCODING:
277 var low, high int
278 if parser.encoding == yaml_UTF16LE_ENCODING {
279 low, high = 0, 1
280 } else {
281 low, high = 1, 0
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 if raw_unread < 2 {
310 if parser.eof {
311 return yaml_parser_set_reader_error(parser,
312 "incomplete UTF-16 character",
313 parser.offset, -1)
314 }
315 break inner
316 }
317
318
319 value = rune(parser.raw_buffer[parser.raw_buffer_pos+low]) +
320 (rune(parser.raw_buffer[parser.raw_buffer_pos+high]) << 8)
321
322
323 if value&0xFC00 == 0xDC00 {
324 return yaml_parser_set_reader_error(parser,
325 "unexpected low surrogate area",
326 parser.offset, int(value))
327 }
328
329
330 if value&0xFC00 == 0xD800 {
331 width = 4
332
333
334 if raw_unread < 4 {
335 if parser.eof {
336 return yaml_parser_set_reader_error(parser,
337 "incomplete UTF-16 surrogate pair",
338 parser.offset, -1)
339 }
340 break inner
341 }
342
343
344 value2 := rune(parser.raw_buffer[parser.raw_buffer_pos+low+2]) +
345 (rune(parser.raw_buffer[parser.raw_buffer_pos+high+2]) << 8)
346
347
348 if value2&0xFC00 != 0xDC00 {
349 return yaml_parser_set_reader_error(parser,
350 "expected low surrogate area",
351 parser.offset+2, int(value2))
352 }
353
354
355 value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF)
356 } else {
357 width = 2
358 }
359
360 default:
361 panic("impossible")
362 }
363
364
365
366
367
368 switch {
369 case value == 0x09:
370 case value == 0x0A:
371 case value == 0x0D:
372 case value >= 0x20 && value <= 0x7E:
373 case value == 0x85:
374 case value >= 0xA0 && value <= 0xD7FF:
375 case value >= 0xE000 && value <= 0xFFFD:
376 case value >= 0x10000 && value <= 0x10FFFF:
377 default:
378 return yaml_parser_set_reader_error(parser,
379 "control characters are not allowed",
380 parser.offset, int(value))
381 }
382
383
384 parser.raw_buffer_pos += width
385 parser.offset += width
386
387
388 if value <= 0x7F {
389
390 parser.buffer[buffer_len+0] = byte(value)
391 buffer_len += 1
392 } else if value <= 0x7FF {
393
394 parser.buffer[buffer_len+0] = byte(0xC0 + (value >> 6))
395 parser.buffer[buffer_len+1] = byte(0x80 + (value & 0x3F))
396 buffer_len += 2
397 } else if value <= 0xFFFF {
398
399 parser.buffer[buffer_len+0] = byte(0xE0 + (value >> 12))
400 parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 6) & 0x3F))
401 parser.buffer[buffer_len+2] = byte(0x80 + (value & 0x3F))
402 buffer_len += 3
403 } else {
404
405 parser.buffer[buffer_len+0] = byte(0xF0 + (value >> 18))
406 parser.buffer[buffer_len+1] = byte(0x80 + ((value >> 12) & 0x3F))
407 parser.buffer[buffer_len+2] = byte(0x80 + ((value >> 6) & 0x3F))
408 parser.buffer[buffer_len+3] = byte(0x80 + (value & 0x3F))
409 buffer_len += 4
410 }
411
412 parser.unread++
413 }
414
415
416 if parser.eof {
417 parser.buffer[buffer_len] = 0
418 buffer_len++
419 parser.unread++
420 break
421 }
422 }
423
424
425
426
427
428 for buffer_len < length {
429 parser.buffer[buffer_len] = 0
430 buffer_len++
431 }
432 parser.buffer = parser.buffer[:buffer_len]
433 return true
434 }
435
View as plain text