| [179] | 1 | |
|---|
| [180] | 2 | #if HAVE_CONFIG_H |
|---|
| 3 | #include <config.h> |
|---|
| 4 | #endif |
|---|
| [179] | 5 | |
|---|
| [180] | 6 | #include <yaml/yaml.h> |
|---|
| 7 | |
|---|
| 8 | #include <assert.h> |
|---|
| 9 | |
|---|
| [179] | 10 | /* |
|---|
| [181] | 11 | * Set the reader error and return 0. |
|---|
| [180] | 12 | */ |
|---|
| 13 | |
|---|
| [183] | 14 | static int |
|---|
| [182] | 15 | yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem, |
|---|
| 16 | size_t offset, int value) |
|---|
| [181] | 17 | { |
|---|
| 18 | parser->error = YAML_READER_ERROR; |
|---|
| 19 | parser->problem = problem; |
|---|
| [182] | 20 | parser->problem_offset = offset; |
|---|
| 21 | parser->problem_value = value; |
|---|
| [180] | 22 | |
|---|
| [181] | 23 | return 0; |
|---|
| 24 | } |
|---|
| [180] | 25 | |
|---|
| [183] | 26 | /* |
|---|
| 27 | * Update the raw buffer. |
|---|
| 28 | */ |
|---|
| [180] | 29 | |
|---|
| [183] | 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 | |
|---|
| [180] | 67 | /* |
|---|
| [183] | 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 | /* |
|---|
| [179] | 118 | * Ensure that the buffer contains at least length characters. |
|---|
| 119 | * Return 1 on success, 0 on failure. |
|---|
| [180] | 120 | * |
|---|
| 121 | * The length is supposed to be significantly less that the buffer size. |
|---|
| [179] | 122 | */ |
|---|
| 123 | |
|---|
| [183] | 124 | YAML_DECLARE(int) |
|---|
| [180] | 125 | yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) |
|---|
| [179] | 126 | { |
|---|
| [182] | 127 | /* If the EOF flag is set and the raw buffer is empty, do nothing. */ |
|---|
| [179] | 128 | |
|---|
| [182] | 129 | if (parser->eof && !parser->raw_unread) |
|---|
| [179] | 130 | return 1; |
|---|
| 131 | |
|---|
| [180] | 132 | /* Return if the buffer contains enough characters. */ |
|---|
| [179] | 133 | |
|---|
| [180] | 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)) |
|---|
| [179] | 141 | return 0; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| [180] | 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 | { |
|---|
| [181] | 170 | unsigned int value, value2; |
|---|
| [180] | 171 | int incomplete = 0; |
|---|
| [182] | 172 | unsigned char octet; |
|---|
| 173 | unsigned int width; |
|---|
| [181] | 174 | int k, low, high; |
|---|
| [180] | 175 | |
|---|
| 176 | /* Decode the next character. */ |
|---|
| 177 | |
|---|
| 178 | switch (parser->encoding) |
|---|
| 179 | { |
|---|
| 180 | case YAML_UTF8_ENCODING: |
|---|
| 181 | |
|---|
| [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 | */ |
|---|
| [180] | 201 | |
|---|
| [181] | 202 | /* Determine the length of the UTF-8 sequence. */ |
|---|
| 203 | |
|---|
| [182] | 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; |
|---|
| [181] | 209 | |
|---|
| 210 | /* Check if the leading octet is valid. */ |
|---|
| 211 | |
|---|
| [182] | 212 | if (!width) |
|---|
| [181] | 213 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 214 | "Invalid leading UTF-8 octet", |
|---|
| 215 | parser->offset, octet); |
|---|
| [181] | 216 | |
|---|
| [180] | 217 | /* Check if the raw buffer contains an incomplete character. */ |
|---|
| 218 | |
|---|
| [182] | 219 | if (width > parser->raw_unread) { |
|---|
| [180] | 220 | if (parser->eof) { |
|---|
| [181] | 221 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 222 | "Incomplete UTF-8 octet sequence", |
|---|
| 223 | parser->offset, -1); |
|---|
| [180] | 224 | } |
|---|
| 225 | incomplete = 1; |
|---|
| [181] | 226 | break; |
|---|
| [180] | 227 | } |
|---|
| 228 | |
|---|
| [181] | 229 | /* Decode the leading octet. */ |
|---|
| [180] | 230 | |
|---|
| [182] | 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; |
|---|
| [181] | 235 | |
|---|
| 236 | /* Check and decode the trailing octets. */ |
|---|
| 237 | |
|---|
| [182] | 238 | for (k = 1; k < width; k ++) |
|---|
| [181] | 239 | { |
|---|
| [182] | 240 | octet = parser->raw_pointer[k]; |
|---|
| [181] | 241 | |
|---|
| 242 | /* Check if the octet is valid. */ |
|---|
| 243 | |
|---|
| [182] | 244 | if ((octet & 0xC0) != 0x80) |
|---|
| [181] | 245 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 246 | "Invalid trailing UTF-8 octet", |
|---|
| 247 | parser->offset+k, octet); |
|---|
| [181] | 248 | |
|---|
| 249 | /* Decode the octet. */ |
|---|
| 250 | |
|---|
| [182] | 251 | value = (value << 6) + (octet & 0x3F); |
|---|
| [180] | 252 | } |
|---|
| 253 | |
|---|
| [181] | 254 | /* Check the length of the sequence against the value. */ |
|---|
| 255 | |
|---|
| [182] | 256 | if (!((width == 1) || |
|---|
| 257 | (width == 2 && value >= 0x80) || |
|---|
| 258 | (width == 3 && value >= 0x800) || |
|---|
| 259 | (width == 4 && value >= 0x10000))) |
|---|
| [181] | 260 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 261 | "Invalid length of a UTF-8 sequence", |
|---|
| 262 | parser->offset, -1); |
|---|
| [181] | 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, |
|---|
| [182] | 268 | "Invalid Unicode character", |
|---|
| 269 | parser->offset, value); |
|---|
| [181] | 270 | |
|---|
| [180] | 271 | break; |
|---|
| 272 | |
|---|
| 273 | case YAML_UTF16LE_ENCODING: |
|---|
| [181] | 274 | case YAML_UTF16BE_ENCODING: |
|---|
| [180] | 275 | |
|---|
| [181] | 276 | low = (parser->encoding == YAML_UTF16LE_ENCODING ? 0 : 1); |
|---|
| 277 | high = (parser->encoding == YAML_UTF16LE_ENCODING ? 1 : 0); |
|---|
| [180] | 278 | |
|---|
| [181] | 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 | |
|---|
| [180] | 307 | if (parser->raw_unread < 2) { |
|---|
| 308 | if (parser->eof) { |
|---|
| [181] | 309 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 310 | "Incomplete UTF-16 character", |
|---|
| 311 | parser->offset, -1); |
|---|
| [180] | 312 | } |
|---|
| 313 | incomplete = 1; |
|---|
| [181] | 314 | break; |
|---|
| [180] | 315 | } |
|---|
| 316 | |
|---|
| [181] | 317 | /* Get the character. */ |
|---|
| [180] | 318 | |
|---|
| [181] | 319 | value = parser->raw_pointer[low] |
|---|
| 320 | + (parser->raw_pointer[high] << 8); |
|---|
| [180] | 321 | |
|---|
| [181] | 322 | /* Check for unexpected low surrogate area. */ |
|---|
| [180] | 323 | |
|---|
| [181] | 324 | if ((value & 0xFC00) == 0xDC00) |
|---|
| 325 | return yaml_parser_set_reader_error(parser, |
|---|
| [182] | 326 | "Unexpected low surrogate area", |
|---|
| 327 | parser->offset, value); |
|---|
| [180] | 328 | |
|---|
| [181] | 329 | /* Check for a high surrogate area. */ |
|---|
| [180] | 330 | |
|---|
| [181] | 331 | if ((value & 0xFC00) == 0xD800) { |
|---|
| 332 | |
|---|
| [182] | 333 | width = 4; |
|---|
| 334 | |
|---|
| [181] | 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, |
|---|
| [182] | 340 | "Incomplete UTF-16 surrogate pair", |
|---|
| 341 | parser->offset, -1); |
|---|
| [181] | 342 | } |
|---|
| 343 | incomplete = 1; |
|---|
| 344 | break; |
|---|
| [180] | 345 | } |
|---|
| [181] | 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, |
|---|
| [182] | 356 | "Expected low surrogate area", |
|---|
| 357 | parser->offset+2, value2); |
|---|
| [181] | 358 | |
|---|
| 359 | /* Generate the value of the surrogate pair. */ |
|---|
| 360 | |
|---|
| 361 | value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF); |
|---|
| [180] | 362 | } |
|---|
| 363 | |
|---|
| [181] | 364 | else { |
|---|
| [182] | 365 | width = 2; |
|---|
| [181] | 366 | } |
|---|
| [180] | 367 | |
|---|
| 368 | break; |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| [181] | 371 | /* Check if the raw buffer contains enough bytes to form a character. */ |
|---|
| 372 | |
|---|
| 373 | if (incomplete) break; |
|---|
| 374 | |
|---|
| [180] | 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 | |
|---|
| [181] | 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, |
|---|
| [182] | 388 | "Control characters are not allowed", |
|---|
| 389 | parser->offset, value); |
|---|
| [180] | 390 | |
|---|
| [182] | 391 | /* Move the raw pointers. */ |
|---|
| 392 | |
|---|
| 393 | parser->raw_pointer += width; |
|---|
| 394 | parser->raw_unread -= width; |
|---|
| 395 | parser->offset += width; |
|---|
| 396 | |
|---|
| [180] | 397 | /* Finally put the character into the buffer. */ |
|---|
| 398 | |
|---|
| 399 | /* 0000 0000-0000 007F -> 0xxxxxxx */ |
|---|
| [181] | 400 | if (value <= 0x7F) { |
|---|
| 401 | *(parser->buffer_end++) = value; |
|---|
| [180] | 402 | } |
|---|
| 403 | /* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */ |
|---|
| [181] | 404 | else if (value <= 0x7FF) { |
|---|
| 405 | *(parser->buffer_end++) = 0xC0 + (value >> 6); |
|---|
| [182] | 406 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); |
|---|
| [180] | 407 | } |
|---|
| 408 | /* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */ |
|---|
| [181] | 409 | else if (value <= 0xFFFF) { |
|---|
| 410 | *(parser->buffer_end++) = 0xE0 + (value >> 12); |
|---|
| [182] | 411 | *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); |
|---|
| 412 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); |
|---|
| [180] | 413 | } |
|---|
| 414 | /* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ |
|---|
| 415 | else { |
|---|
| [181] | 416 | *(parser->buffer_end++) = 0xF0 + (value >> 18); |
|---|
| [182] | 417 | *(parser->buffer_end++) = 0x80 + ((value >> 12) & 0x3F); |
|---|
| 418 | *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); |
|---|
| 419 | *(parser->buffer_end++) = 0x80 + (value & 0x3F); |
|---|
| [180] | 420 | } |
|---|
| [182] | 421 | |
|---|
| 422 | parser->unread ++; |
|---|
| [179] | 423 | } |
|---|
| [182] | 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 | |
|---|
| [179] | 433 | } |
|---|
| 434 | |
|---|
| [181] | 435 | return 1; |
|---|
| [180] | 436 | } |
|---|
| [179] | 437 | |
|---|