]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | #if HAVE_CONFIG_H | |
3 | #include <config.h> | |
4 | #endif | |
5 | ||
6 | #include <yaml/yaml.h> | |
7 | ||
8 | #include <assert.h> | |
9 | ||
10 | /* | |
11 | * Set the reader error and return 0. | |
12 | */ | |
13 | ||
14 | static int | |
15 | yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem, | |
16 | size_t offset, int value) | |
17 | { | |
18 | parser->error = YAML_READER_ERROR; | |
19 | parser->problem = problem; | |
20 | parser->problem_offset = offset; | |
21 | parser->problem_value = value; | |
22 | ||
23 | return 0; | |
24 | } | |
25 | ||
26 | /* | |
27 | * Update the raw buffer. | |
28 | */ | |
29 | ||
30 | static int | |
31 | yaml_parser_update_raw_buffer(yaml_parser_t *parser) | |
32 | { | |
33 | size_t size_read = 0; | |
34 | ||
35 | /* Return if the raw buffer is full. */ | |
36 | ||
37 | if (parser->raw_unread == YAML_RAW_BUFFER_SIZE) return 1; | |
38 | ||
39 | /* Return on EOF. */ | |
40 | ||
41 | if (parser->eof) return 1; | |
42 | ||
43 | /* Move the remaining bytes in the raw buffer to the beginning. */ | |
44 | ||
45 | if (parser->raw_unread && parser->raw_buffer < parser->raw_pointer) { | |
46 | memmove(parser->raw_buffer, parser->raw_pointer, parser->raw_unread); | |
47 | } | |
48 | parser->raw_pointer = parser->raw_buffer; | |
49 | ||
50 | /* Call the read handler to fill the buffer. */ | |
51 | ||
52 | if (!parser->read_handler(parser->read_handler_data, | |
53 | parser->raw_buffer + parser->raw_unread, | |
54 | YAML_RAW_BUFFER_SIZE - parser->raw_unread, | |
55 | &size_read)) { | |
56 | return yaml_parser_set_reader_error(parser, "Input error", | |
57 | parser->offset, -1); | |
58 | } | |
59 | parser->raw_unread += size_read; | |
60 | if (!size_read) { | |
61 | parser->eof = 1; | |
62 | } | |
63 | ||
64 | return 1; | |
65 | } | |
66 | ||
67 | /* | |
68 | * Determine the input stream encoding by checking the BOM symbol. If no BOM is | |
69 | * found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure. | |
70 | */ | |
71 | ||
72 | #define BOM_UTF8 "\xef\xbb\xbf" | |
73 | #define BOM_UTF16LE "\xff\xfe" | |
74 | #define BOM_UTF16BE "\xfe\xff" | |
75 | ||
76 | static int | |
77 | yaml_parser_determine_encoding(yaml_parser_t *parser) | |
78 | { | |
79 | /* Ensure that we had enough bytes in the raw buffer. */ | |
80 | ||
81 | while (!parser->eof && parser->raw_unread < 3) { | |
82 | if (!yaml_parser_update_raw_buffer(parser)) { | |
83 | return 0; | |
84 | } | |
85 | } | |
86 | ||
87 | /* Determine the encoding. */ | |
88 | ||
89 | if (parser->raw_unread >= 2 | |
90 | && !memcmp(parser->raw_pointer, BOM_UTF16LE, 2)) { | |
91 | parser->encoding = YAML_UTF16LE_ENCODING; | |
92 | parser->raw_pointer += 2; | |
93 | parser->raw_unread -= 2; | |
94 | parser->offset += 2; | |
95 | } | |
96 | else if (parser->raw_unread >= 2 | |
97 | && !memcmp(parser->raw_pointer, BOM_UTF16BE, 2)) { | |
98 | parser->encoding = YAML_UTF16BE_ENCODING; | |
99 | parser->raw_pointer += 2; | |
100 | parser->raw_unread -= 2; | |
101 | parser->offset += 2; | |
102 | } | |
103 | else if (parser->raw_unread >= 3 | |
104 | && !memcmp(parser->raw_pointer, BOM_UTF8, 3)) { | |
105 | parser->encoding = YAML_UTF8_ENCODING; | |
106 | parser->raw_pointer += 3; | |
107 | parser->raw_unread -= 3; | |
108 | parser->offset += 3; | |
109 | } | |
110 | else { | |
111 | parser->encoding = YAML_UTF8_ENCODING; | |
112 | } | |
113 | ||
114 | return 1; | |
115 | } | |
116 | ||
117 | /* | |
118 | * Ensure that the buffer contains at least length characters. | |
119 | * Return 1 on success, 0 on failure. | |
120 | * | |
121 | * The length is supposed to be significantly less that the buffer size. | |
122 | */ | |
123 | ||
124 | YAML_DECLARE(int) | |
125 | yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) | |
126 | { | |
127 | /* If the EOF flag is set and the raw buffer is empty, do nothing. */ | |
128 | ||
129 | if (parser->eof && !parser->raw_unread) | |
130 | return 1; | |
131 | ||
132 | /* Return if the buffer contains enough characters. */ | |
133 | ||
134 | if (parser->unread >= length) | |
135 | return 1; | |
136 | ||
137 | /* Determine the input encoding if it is not known yet. */ | |
138 | ||
139 | if (!parser->encoding) { | |
140 | if (!yaml_parser_determine_encoding(parser)) | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* Move the unread characters to the beginning of the buffer. */ | |
145 | ||
146 | if (parser->buffer < parser->pointer | |
147 | && parser->pointer < parser->buffer_end) { | |
148 | size_t size = parser->buffer_end - parser->pointer; | |
149 | memmove(parser->buffer, parser->pointer, size); | |
150 | parser->pointer = parser->buffer; | |
151 | parser->buffer_end -= size; | |
152 | } | |
153 | else if (parser->pointer == parser->buffer_end) { | |
154 | parser->pointer = parser->buffer; | |
155 | parser->buffer_end = parser->buffer; | |
156 | } | |
157 | ||
158 | /* Fill the buffer until it has enough characters. */ | |
159 | ||
160 | while (parser->unread < length) | |
161 | { | |
162 | /* Fill the raw buffer. */ | |
163 | ||
164 | if (!yaml_parser_update_raw_buffer(parser)) return 0; | |
165 | ||
166 | /* Decode the raw buffer. */ | |
167 | ||
168 | while (parser->raw_unread) | |
169 | { | |
170 | unsigned int value, value2; | |
171 | int incomplete = 0; | |
172 | unsigned char octet; | |
173 | unsigned int width; | |
174 | int k, low, high; | |
175 | ||
176 | /* Decode the next character. */ | |
177 | ||
178 | switch (parser->encoding) | |
179 | { | |
180 | case YAML_UTF8_ENCODING: | |
181 | ||
182 | /* | |
183 | * Decode a UTF-8 character. Check RFC 3629 | |
184 | * (http://www.ietf.org/rfc/rfc3629.txt) for more details. | |
185 | * | |
186 | * The following table (taken from the RFC) is used for | |
187 | * decoding. | |
188 | * | |
189 | * Char. number range | UTF-8 octet sequence | |
190 | * (hexadecimal) | (binary) | |
191 | * --------------------+------------------------------------ | |
192 | * 0000 0000-0000 007F | 0xxxxxxx | |
193 | * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx | |
194 | * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx | |
195 | * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | |
196 | * | |
197 | * Additionally, the characters in the range 0xD800-0xDFFF | |
198 | * are prohibited as they are reserved for use with UTF-16 | |
199 | * surrogate pairs. | |
200 | */ | |
201 | ||
202 | /* Determine the length of the UTF-8 sequence. */ | |
203 | ||
204 | octet = parser->raw_pointer[0]; | |
205 | width = (octet & 0x80) == 0x00 ? 1 : | |
206 | (octet & 0xE0) == 0xC0 ? 2 : | |
207 | (octet & 0xF0) == 0xE0 ? 3 : | |
208 | (octet & 0xF8) == 0xF0 ? 4 : 0; | |
209 | ||
210 | /* Check if the leading octet is valid. */ | |
211 | ||
212 | if (!width) | |
213 | return yaml_parser_set_reader_error(parser, | |
214 | "Invalid leading UTF-8 octet", | |
215 | parser->offset, octet); | |
216 | ||
217 | /* Check if the raw buffer contains an incomplete character. */ | |
218 | ||
219 | if (width > parser->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 | incomplete = 1; | |
226 | break; | |
227 | } | |
228 | ||
229 | /* Decode the leading octet. */ | |
230 | ||
231 | value = (octet & 0x80) == 0x00 ? octet & 0x7F : | |
232 | (octet & 0xE0) == 0xC0 ? octet & 0x1F : | |
233 | (octet & 0xF0) == 0xE0 ? octet & 0x0F : | |
234 | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; | |
235 | ||
236 | /* Check and decode the trailing octets. */ | |
237 | ||
238 | for (k = 1; k < width; k ++) | |
239 | { | |
240 | octet = parser->raw_pointer[k]; | |
241 | ||
242 | /* Check if the octet is valid. */ | |
243 | ||
244 | if ((octet & 0xC0) != 0x80) | |
245 | return yaml_parser_set_reader_error(parser, | |
246 | "Invalid trailing UTF-8 octet", | |
247 | parser->offset+k, octet); | |
248 | ||
249 | /* Decode the octet. */ | |
250 | ||
251 | value = (value << 6) + (octet & 0x3F); | |
252 | } | |
253 | ||
254 | /* Check the length of the sequence against the value. */ | |
255 | ||
256 | if (!((width == 1) || | |
257 | (width == 2 && value >= 0x80) || | |
258 | (width == 3 && value >= 0x800) || | |
259 | (width == 4 && value >= 0x10000))) | |
260 | return yaml_parser_set_reader_error(parser, | |
261 | "Invalid length of a UTF-8 sequence", | |
262 | parser->offset, -1); | |
263 | ||
264 | /* Check the range of the value. */ | |
265 | ||
266 | if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) | |
267 | return yaml_parser_set_reader_error(parser, | |
268 | "Invalid Unicode character", | |
269 | parser->offset, value); | |
270 | ||
271 | break; | |
272 | ||
273 | case YAML_UTF16LE_ENCODING: | |
274 | case YAML_UTF16BE_ENCODING: | |
275 | ||
276 | low = (parser->encoding == YAML_UTF16LE_ENCODING ? 0 : 1); | |
277 | high = (parser->encoding == YAML_UTF16LE_ENCODING ? 1 : 0); | |
278 | ||
279 | /* | |
280 | * The UTF-16 encoding is not as simple as one might | |
281 | * naively think. Check RFC 2781 | |
282 | * (http://www.ietf.org/rfc/rfc2781.txt). | |
283 | * | |
284 | * Normally, two subsequent bytes describe a Unicode | |
285 | * character. However a special technique (called a | |
286 | * surrogate pair) is used for specifying character | |
287 | * values larger than 0xFFFF. | |
288 | * | |
289 | * A surrogate pair consists of two pseudo-characters: | |
290 | * high surrogate area (0xD800-0xDBFF) | |
291 | * low surrogate area (0xDC00-0xDFFF) | |
292 | * | |
293 | * The following formulas are used for decoding | |
294 | * and encoding characters using surrogate pairs: | |
295 | * | |
296 | * U = U' + 0x10000 (0x01 00 00 <= U <= 0x10 FF FF) | |
297 | * U' = yyyyyyyyyyxxxxxxxxxx (0 <= U' <= 0x0F FF FF) | |
298 | * W1 = 110110yyyyyyyyyy | |
299 | * W2 = 110111xxxxxxxxxx | |
300 | * | |
301 | * where U is the character value, W1 is the high surrogate | |
302 | * area, W2 is the low surrogate area. | |
303 | */ | |
304 | ||
305 | /* Check for incomplete UTF-16 character. */ | |
306 | ||
307 | if (parser->raw_unread < 2) { | |
308 | if (parser->eof) { | |
309 | return yaml_parser_set_reader_error(parser, | |
310 | "Incomplete UTF-16 character", | |
311 | parser->offset, -1); | |
312 | } | |
313 | incomplete = 1; | |
314 | break; | |
315 | } | |
316 | ||
317 | /* Get the character. */ | |
318 | ||
319 | value = parser->raw_pointer[low] | |
320 | + (parser->raw_pointer[high] << 8); | |
321 | ||
322 | /* Check for unexpected low surrogate area. */ | |
323 | ||
324 | if ((value & 0xFC00) == 0xDC00) | |
325 | return yaml_parser_set_reader_error(parser, | |
326 | "Unexpected low surrogate area", | |
327 | parser->offset, value); | |
328 | ||
329 | /* Check for a high surrogate area. */ | |
330 | ||
331 | if ((value & 0xFC00) == 0xD800) { | |
332 | ||
333 | width = 4; | |
334 | ||
335 | /* Check for incomplete surrogate pair. */ | |
336 | ||
337 | if (parser->raw_unread < 4) { | |
338 | if (parser->eof) { | |
339 | return yaml_parser_set_reader_error(parser, | |
340 | "Incomplete UTF-16 surrogate pair", | |
341 | parser->offset, -1); | |
342 | } | |
343 | incomplete = 1; | |
344 | break; | |
345 | } | |
346 | ||
347 | /* Get the next character. */ | |
348 | ||
349 | unsigned int value2 = parser->raw_pointer[low+2] | |
350 | + (parser->raw_pointer[high+2] << 8); | |
351 | ||
352 | /* Check for a low surrogate area. */ | |
353 | ||
354 | if ((value2 & 0xFC00) != 0xDC00) | |
355 | return yaml_parser_set_reader_error(parser, | |
356 | "Expected low surrogate area", | |
357 | parser->offset+2, value2); | |
358 | ||
359 | /* Generate the value of the surrogate pair. */ | |
360 | ||
361 | value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF); | |
362 | } | |
363 | ||
364 | else { | |
365 | width = 2; | |
366 | } | |
367 | ||
368 | break; | |
369 | } | |
370 | ||
371 | /* Check if the raw buffer contains enough bytes to form a character. */ | |
372 | ||
373 | if (incomplete) break; | |
374 | ||
375 | /* | |
376 | * Check if the character is in the allowed range: | |
377 | * #x9 | #xA | #xD | [#x20-#x7E] (8 bit) | |
378 | * | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] (16 bit) | |
379 | * | [#x10000-#x10FFFF] (32 bit) | |
380 | */ | |
381 | ||
382 | if (! (value == 0x09 || value == 0x0A || value == 0x0D | |
383 | || (value >= 0x20 && value <= 0x7E) | |
384 | || (value == 0x85) || (value >= 0xA0 && value <= 0xD7FF) | |
385 | || (value >= 0xE000 && value <= 0xFFFD) | |
386 | || (value >= 0x10000 && value <= 0x10FFFF))) | |
387 | return yaml_parser_set_reader_error(parser, | |
388 | "Control characters are not allowed", | |
389 | parser->offset, value); | |
390 | ||
391 | /* Move the raw pointers. */ | |
392 | ||
393 | parser->raw_pointer += width; | |
394 | parser->raw_unread -= width; | |
395 | parser->offset += width; | |
396 | ||
397 | /* Finally put the character into the buffer. */ | |
398 | ||
399 | /* 0000 0000-0000 007F -> 0xxxxxxx */ | |
400 | if (value <= 0x7F) { | |
401 | *(parser->buffer_end++) = value; | |
402 | } | |
403 | /* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */ | |
404 | else if (value <= 0x7FF) { | |
405 | *(parser->buffer_end++) = 0xC0 + (value >> 6); | |
406 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); | |
407 | } | |
408 | /* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */ | |
409 | else if (value <= 0xFFFF) { | |
410 | *(parser->buffer_end++) = 0xE0 + (value >> 12); | |
411 | *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); | |
412 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); | |
413 | } | |
414 | /* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ | |
415 | else { | |
416 | *(parser->buffer_end++) = 0xF0 + (value >> 18); | |
417 | *(parser->buffer_end++) = 0x80 + ((value >> 12) & 0x3F); | |
418 | *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); | |
419 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); | |
420 | } | |
421 | ||
422 | parser->unread ++; | |
423 | } | |
424 | ||
425 | /* On EOF, put NUL into the buffer and return. */ | |
426 | ||
427 | if (parser->eof) { | |
428 | *(parser->buffer_end++) = '\0'; | |
429 | parser->unread ++; | |
430 | return 1; | |
431 | } | |
432 | ||
433 | } | |
434 | ||
435 | return 1; | |
436 | } | |
437 |