Changeset 182
- Timestamp:
- 06/01/06 16:19:43 (7 years ago)
- Location:
- libyaml/trunk
- Files:
-
- 3 edited
-
include/yaml/yaml.h (modified) (1 diff)
-
src/reader.c (modified) (17 diffs)
-
tests/test-reader.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libyaml/trunk/include/yaml/yaml.h
r181 r182 301 301 size_t problem_offset; 302 302 303 /** The problematic value (@c -1 is none). */ 304 int problem_value; 305 303 306 /** 304 307 * @} -
libyaml/trunk/src/reader.c
r181 r182 13 13 14 14 int 15 yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem) 15 yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem, 16 size_t offset, int value) 16 17 { 17 18 parser->error = YAML_READER_ERROR; 18 19 parser->problem = problem; 19 parser->problem_offset = parser->offset; 20 parser->problem_offset = offset; 21 parser->problem_value = value; 20 22 21 23 return 0; … … 33 35 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) 34 36 { 35 /* If the EOF flag is set , do nothing. */36 37 if (parser->eof )37 /* If the EOF flag is set and the raw buffer is empty, do nothing. */ 38 39 if (parser->eof && !parser->raw_unread) 38 40 return 1; 39 41 … … 72 74 if (!yaml_parser_update_raw_buffer(parser)) return 0; 73 75 74 /* If the raw buffer is empty, it is EOF. */75 76 if (!parser->raw_unread) return 1;77 78 76 /* Decode the raw buffer. */ 79 77 … … 82 80 unsigned int value, value2; 83 81 int incomplete = 0; 84 unsigned char utf8_octet;85 unsigned int utf8_length;82 unsigned char octet; 83 unsigned int width; 86 84 int k, low, high; 87 85 … … 114 112 /* Determine the length of the UTF-8 sequence. */ 115 113 116 utf8_octet = parser->raw_pointer[0]; 117 utf8_length = ( 118 (utf8_octet & 0x80) == 0x00 ? 1 : 119 (utf8_octet & 0xE0) == 0xC0 ? 2 : 120 (utf8_octet & 0xF0) == 0xE0 ? 3 : 121 (utf8_octet & 0xF8) == 0xF0 ? 4 : 0); 114 octet = parser->raw_pointer[0]; 115 width = (octet & 0x80) == 0x00 ? 1 : 116 (octet & 0xE0) == 0xC0 ? 2 : 117 (octet & 0xF0) == 0xE0 ? 3 : 118 (octet & 0xF8) == 0xF0 ? 4 : 0; 122 119 123 120 /* Check if the leading octet is valid. */ 124 121 125 if (! utf8_length)122 if (!width) 126 123 return yaml_parser_set_reader_error(parser, 127 "Invalid leading UTF-8 octet"); 124 "Invalid leading UTF-8 octet", 125 parser->offset, octet); 128 126 129 127 /* Check if the raw buffer contains an incomplete character. */ 130 128 131 if ( utf8_length > parser->raw_unread) {129 if (width > parser->raw_unread) { 132 130 if (parser->eof) { 133 131 return yaml_parser_set_reader_error(parser, 134 "Incomplete UTF-8 octet sequence"); 132 "Incomplete UTF-8 octet sequence", 133 parser->offset, -1); 135 134 } 136 135 incomplete = 1; … … 140 139 /* Decode the leading octet. */ 141 140 142 value = ( 143 (utf8_octet & 0x80) == 0x00 ? utf8_octet & 0x7F : 144 (utf8_octet & 0xE0) == 0xC0 ? utf8_octet & 0x1F : 145 (utf8_octet & 0xF0) == 0xE0 ? utf8_octet & 0x0F : 146 (utf8_octet & 0xF8) == 0xF0 ? utf8_octet & 0x07 : 0); 141 value = (octet & 0x80) == 0x00 ? octet & 0x7F : 142 (octet & 0xE0) == 0xC0 ? octet & 0x1F : 143 (octet & 0xF0) == 0xE0 ? octet & 0x0F : 144 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; 147 145 148 146 /* Check and decode the trailing octets. */ 149 147 150 for (k = 1; k < utf8_length; k ++)148 for (k = 1; k < width; k ++) 151 149 { 152 utf8_octet = parser->raw_pointer[k];150 octet = parser->raw_pointer[k]; 153 151 154 152 /* Check if the octet is valid. */ 155 153 156 if (( utf8_octet & 0xC0) != 0x80)154 if ((octet & 0xC0) != 0x80) 157 155 return yaml_parser_set_reader_error(parser, 158 "Invalid trailing UTF-8 octet"); 156 "Invalid trailing UTF-8 octet", 157 parser->offset+k, octet); 159 158 160 159 /* Decode the octet. */ 161 160 162 value = (value << 6) + ( utf8_octet & 0x3F);161 value = (value << 6) + (octet & 0x3F); 163 162 } 164 163 165 164 /* Check the length of the sequence against the value. */ 166 165 167 if (!(( utf8_length == 1) ||168 ( utf8_length == 2 && value >= 0x80) ||169 ( utf8_length == 3 && value >= 0x800) ||170 ( utf8_length == 4 && value >= 0x10000)))166 if (!((width == 1) || 167 (width == 2 && value >= 0x80) || 168 (width == 3 && value >= 0x800) || 169 (width == 4 && value >= 0x10000))) 171 170 return yaml_parser_set_reader_error(parser, 172 "Invalid length of a UTF-8 sequence"); 171 "Invalid length of a UTF-8 sequence", 172 parser->offset, -1); 173 173 174 174 /* Check the range of the value. */ … … 176 176 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) 177 177 return yaml_parser_set_reader_error(parser, 178 "Invalid Unicode character"); 179 180 parser->raw_pointer += utf8_length; 181 parser->raw_unread -= utf8_length; 182 parser->offset += utf8_length; 178 "Invalid Unicode character", 179 parser->offset, value); 183 180 184 181 break; … … 221 218 if (parser->eof) { 222 219 return yaml_parser_set_reader_error(parser, 223 "Incomplete UTF-16 character"); 220 "Incomplete UTF-16 character", 221 parser->offset, -1); 224 222 } 225 223 incomplete = 1; … … 236 234 if ((value & 0xFC00) == 0xDC00) 237 235 return yaml_parser_set_reader_error(parser, 238 "Unexpected low surrogate area"); 236 "Unexpected low surrogate area", 237 parser->offset, value); 239 238 240 239 /* Check for a high surrogate area. */ 241 240 242 241 if ((value & 0xFC00) == 0xD800) { 242 243 width = 4; 243 244 244 245 /* Check for incomplete surrogate pair. */ … … 247 248 if (parser->eof) { 248 249 return yaml_parser_set_reader_error(parser, 249 "Incomplete UTF-16 surrogate pair"); 250 "Incomplete UTF-16 surrogate pair", 251 parser->offset, -1); 250 252 } 251 253 incomplete = 1; … … 262 264 if ((value2 & 0xFC00) != 0xDC00) 263 265 return yaml_parser_set_reader_error(parser, 264 "Expected low surrogate area"); 266 "Expected low surrogate area", 267 parser->offset+2, value2); 265 268 266 269 /* Generate the value of the surrogate pair. */ 267 270 268 271 value = 0x10000 + ((value & 0x3FF) << 10) + (value2 & 0x3FF); 269 270 parser->raw_pointer += 4;271 parser->raw_unread -= 4;272 parser->offset += 4;273 272 } 274 273 275 274 else { 276 parser->raw_pointer += 2; 277 parser->raw_unread -= 2; 278 parser->offset += 4; 275 width = 2; 279 276 } 280 277 … … 299 296 || (value >= 0x10000 && value <= 0x10FFFF))) 300 297 return yaml_parser_set_reader_error(parser, 301 "Control characters are not allowed"); 298 "Control characters are not allowed", 299 parser->offset, value); 300 301 /* Move the raw pointers. */ 302 303 parser->raw_pointer += width; 304 parser->raw_unread -= width; 305 parser->offset += width; 302 306 303 307 /* Finally put the character into the buffer. */ … … 310 314 else if (value <= 0x7FF) { 311 315 *(parser->buffer_end++) = 0xC0 + (value >> 6); 312 *(parser->buffer_end++) = 0x80 + value & 0x3F;316 *(parser->buffer_end++) = 0x80 + (value & 0x3F); 313 317 } 314 318 /* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */ 315 319 else if (value <= 0xFFFF) { 316 320 *(parser->buffer_end++) = 0xE0 + (value >> 12); 317 *(parser->buffer_end++) = 0x80 + ( value >> 6) & 0x3F;318 *(parser->buffer_end++) = 0x80 + value & 0x3F;321 *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); 322 *(parser->buffer_end++) = 0x80 + (value & 0x3F); 319 323 } 320 324 /* 0001 0000-0010 FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ 321 325 else { 322 326 *(parser->buffer_end++) = 0xF0 + (value >> 18); 323 *(parser->buffer_end++) = 0x80 + ( value >> 12) & 0x3F;324 *(parser->buffer_end++) = 0x80 + ( value >> 6) & 0x3F;325 *(parser->buffer_end++) = 0x80 + value & 0x3F;327 *(parser->buffer_end++) = 0x80 + ((value >> 12) & 0x3F); 328 *(parser->buffer_end++) = 0x80 + ((value >> 6) & 0x3F); 329 *(parser->buffer_end++) = 0x80 + (value & 0x3F); 326 330 } 331 332 parser->unread ++; 327 333 } 334 335 /* On EOF, put NUL into the buffer and return. */ 336 337 if (parser->eof) { 338 *(parser->buffer_end++) = '\0'; 339 parser->unread ++; 340 return 1; 341 } 342 328 343 } 329 344 … … 358 373 parser->raw_pointer += 2; 359 374 parser->raw_unread -= 2; 375 parser->offset += 2; 360 376 } 361 377 else if (parser->raw_unread >= 2 … … 364 380 parser->raw_pointer += 2; 365 381 parser->raw_unread -= 2; 382 parser->offset += 2; 366 383 } 367 384 else if (parser->raw_unread >= 3 … … 370 387 parser->raw_pointer += 3; 371 388 parser->raw_unread -= 3; 389 parser->offset += 3; 372 390 } 373 391 else { … … 408 426 YAML_RAW_BUFFER_SIZE - parser->raw_unread, 409 427 &size_read)) { 410 return yaml_parser_set_reader_error(parser, "Input error"); 428 return yaml_parser_set_reader_error(parser, "Input error", 429 parser->offset, -1); 411 430 } 412 431 parser->raw_unread += size_read; -
libyaml/trunk/tests/test-reader.c
r181 r182 17 17 18 18 test_case utf8_sequences[] = { 19 /* {"title", "test 1|test 2|...|test N!", (0 or 1)}, */20 21 {"a simple test", "'test' is '\xd0\xbf\xd1\x80\xd0\xbe\xd0\xb2\xd0\xb5\xd1\x80\xd0\xba\xd0\xb0' in Russian!", 1},22 {"an empty line", "!", 1},23 24 {"u-0 is a control character", "\x00!", 0},25 {"u-80 is a control character", "\xc2\x80!", 0},26 {"u-800 is valid", "\xe0\xa0\x80!", 1},27 {"u-10000 is valid", "\xf0\x90\x80\x80!", 1},28 {"5 bytes sequences are not allowed", "\xf8\x88\x80\x80\x80!", 0},29 {"6 bytes sequences are not allowed", "\xfc\x84\x80\x80\x80\x80!", 0},30 31 {"u-7f is a control character", "\x7f!", 0},32 {"u-7FF is valid", "\xdf\xbf!", 1},33 {"u-FFFF is a control character", "\xef\xbf\xbf!", 0},34 {"u-1FFFFF is too large", "\xf7\xbf\xbf\xbf!", 0},35 {"u-3FFFFFF is 5 bytes", "\xfb\xbf\xbf\xbf\xbf!", 0},36 {"u-7FFFFFFF is 6 bytes", "\xfd\xbf\xbf\xbf\xbf\xbf!", 0},37 38 {"u-D7FF", "\xed\x9f\xbf!", 1},39 {"u-E000", "\xee\x80\x80!", 1},40 {"u-FFFD", "\xef\xbf\xbd!", 1},41 {"u-10FFFF", "\xf4\x8f\xbf\xbf!", 1},42 {"u-110000", "\xf4\x90\x80\x80!", 0},43 44 {"first continuation byte", "\x80!", 0},45 {"last continuation byte", "\xbf!", 0},46 47 {"2 continuation bytes", "\x80\xbf!", 0},48 {"3 continuation bytes", "\x80\xbf\x80!", 0},49 {"4 continuation bytes", "\x80\xbf\x80\xbf!", 0},50 {"5 continuation bytes", "\x80\xbf\x80\xbf\x80!", 0},51 {"6 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf!", 0},52 {"7 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf\x80!", 0},53 54 {"sequence of all 64 possible continuation bytes",55 "\x80|\x81|\x82|\x83|\x84|\x85|\x86|\x87|\x88|\x89|\x8a|\x8b|\x8c|\x8d|\x8e|\x8f|"56 "\x90|\x91|\x92|\x93|\x94|\x95|\x96|\x97|\x98|\x99|\x9a|\x9b|\x9c|\x9d|\x9e|\x9f|"57 "\xa0|\xa1|\xa2|\xa3|\xa4|\xa5|\xa6|\xa7|\xa8|\xa9|\xaa|\xab|\xac|\xad|\xae|\xaf|"58 "\xb0|\xb1|\xb2|\xb3|\xb4|\xb5|\xb6|\xb7|\xb8|\xb9|\xba|\xbb|\xbc|\xbd|\xbe|\xbf!", 0},59 {"32 first bytes of 2-byte sequences {0xc0-0xdf}",60 "\xc0 |\xc1 |\xc2 |\xc3 |\xc4 |\xc5 |\xc6 |\xc7 |\xc8 |\xc9 |\xca |\xcb |\xcc |\xcd |\xce |\xcf |"61 "\xd0 |\xd1 |\xd2 |\xd3 |\xd4 |\xd5 |\xd6 |\xd7 |\xd8 |\xd9 |\xda |\xdb |\xdc |\xdd |\xde |\xdf !", 0},62 {"16 first bytes of 3-byte sequences {0xe0-0xef}",63 "\xe0 |\xe1 |\xe2 |\xe3 |\xe4 |\xe5 |\xe6 |\xe7 |\xe8 |\xe9 |\xea |\xeb |\xec |\xed |\xee |\xef !", 0},64 {"8 first bytes of 4-byte sequences {0xf0-0xf7}", "\xf0 |\xf1 |\xf2 |\xf3 |\xf4 |\xf5 |\xf6 |\xf7 !", 0},65 {"4 first bytes of 5-byte sequences {0xf8-0xfb}", "\xf8 |\xf9 |\xfa |\xfb !", 0},66 {"2 first bytes of 6-byte sequences {0xfc-0xfd}", "\xfc |\xfd !", 0},67 68 {"sequences with last byte missing {u-0}",69 "\xc0|\xe0\x80|\xf0\x80\x80|\xf8\x80\x80\x80|\xfc\x80\x80\x80\x80!", 0},70 {"sequences with last byte missing {u-...FF}",71 "\xdf|\xef\xbf|\xf7\xbf\xbf|\xfb\xbf\xbf\xbf|\xfd\xbf\xbf\xbf\xbf!", 0},72 73 {"impossible bytes", "\xfe|\xff|\xfe\xfe\xff\xff!", 0},74 75 {"overlong sequences {u-2f}",76 "\xc0\xaf|\xe0\x80\xaf|\xf0\x80\x80\xaf|\xf8\x80\x80\x80\xaf|\xfc\x80\x80\x80\x80\xaf!", 0},77 78 {"maximum overlong sequences",79 "\xc1\xbf|\xe0\x9f\xbf|\xf0\x8f\xbf\xbf|\xf8\x87\xbf\xbf\xbf|\xfc\x83\xbf\xbf\xbf\xbf!", 0},80 81 {"overlong representation of the NUL character",82 "\xc0\x80|\xe0\x80\x80|\xf0\x80\x80\x80|\xf8\x80\x80\x80\x80|\xfc\x80\x80\x80\x80\x80!", 0},83 84 {"single UTF-16 surrogates",85 "\xed\xa0\x80|\xed\xad\xbf|\xed\xae\x80|\xed\xaf\xbf|\xed\xb0\x80|\xed\xbe\x80|\xed\xbf\xbf!", 0},86 87 {"paired UTF-16 surrogates",88 "\xed\xa0\x80\xed\xb0\x80|\xed\xa0\x80\xed\xbf\xbf|\xed\xad\xbf\xed\xb0\x80|"89 "\xed\xad\xbf\xed\xbf\xbf|\xed\xae\x80\xed\xb0\x80|\xed\xae\x80\xed\xbf\xbf|"90 "\xed\xaf\xbf\xed\xb0\x80|\xed\xaf\xbf\xed\xbf\xbf!", 0},91 92 {"other illegal code positions", "\xef\xbf\xbe|\xef\xbf\xbf!", 0},93 94 {NULL, NULL, 0}19 /* {"title", "test 1|test 2|...|test N!", (0 or 1)}, */ 20 21 {"a simple test", "'test' is '\xd0\xbf\xd1\x80\xd0\xbe\xd0\xb2\xd0\xb5\xd1\x80\xd0\xba\xd0\xb0' in Russian!", 1}, 22 {"an empty line", "!", 1}, 23 24 {"u-0 is a control character", "\x00!", 0}, 25 {"u-80 is a control character", "\xc2\x80!", 0}, 26 {"u-800 is valid", "\xe0\xa0\x80!", 1}, 27 {"u-10000 is valid", "\xf0\x90\x80\x80!", 1}, 28 {"5 bytes sequences are not allowed", "\xf8\x88\x80\x80\x80!", 0}, 29 {"6 bytes sequences are not allowed", "\xfc\x84\x80\x80\x80\x80!", 0}, 30 31 {"u-7f is a control character", "\x7f!", 0}, 32 {"u-7FF is valid", "\xdf\xbf!", 1}, 33 {"u-FFFF is a control character", "\xef\xbf\xbf!", 0}, 34 {"u-1FFFFF is too large", "\xf7\xbf\xbf\xbf!", 0}, 35 {"u-3FFFFFF is 5 bytes", "\xfb\xbf\xbf\xbf\xbf!", 0}, 36 {"u-7FFFFFFF is 6 bytes", "\xfd\xbf\xbf\xbf\xbf\xbf!", 0}, 37 38 {"u-D7FF", "\xed\x9f\xbf!", 1}, 39 {"u-E000", "\xee\x80\x80!", 1}, 40 {"u-FFFD", "\xef\xbf\xbd!", 1}, 41 {"u-10FFFF", "\xf4\x8f\xbf\xbf!", 1}, 42 {"u-110000", "\xf4\x90\x80\x80!", 0}, 43 44 {"first continuation byte", "\x80!", 0}, 45 {"last continuation byte", "\xbf!", 0}, 46 47 {"2 continuation bytes", "\x80\xbf!", 0}, 48 {"3 continuation bytes", "\x80\xbf\x80!", 0}, 49 {"4 continuation bytes", "\x80\xbf\x80\xbf!", 0}, 50 {"5 continuation bytes", "\x80\xbf\x80\xbf\x80!", 0}, 51 {"6 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf!", 0}, 52 {"7 continuation bytes", "\x80\xbf\x80\xbf\x80\xbf\x80!", 0}, 53 54 {"sequence of all 64 possible continuation bytes", 55 "\x80|\x81|\x82|\x83|\x84|\x85|\x86|\x87|\x88|\x89|\x8a|\x8b|\x8c|\x8d|\x8e|\x8f|" 56 "\x90|\x91|\x92|\x93|\x94|\x95|\x96|\x97|\x98|\x99|\x9a|\x9b|\x9c|\x9d|\x9e|\x9f|" 57 "\xa0|\xa1|\xa2|\xa3|\xa4|\xa5|\xa6|\xa7|\xa8|\xa9|\xaa|\xab|\xac|\xad|\xae|\xaf|" 58 "\xb0|\xb1|\xb2|\xb3|\xb4|\xb5|\xb6|\xb7|\xb8|\xb9|\xba|\xbb|\xbc|\xbd|\xbe|\xbf!", 0}, 59 {"32 first bytes of 2-byte sequences {0xc0-0xdf}", 60 "\xc0 |\xc1 |\xc2 |\xc3 |\xc4 |\xc5 |\xc6 |\xc7 |\xc8 |\xc9 |\xca |\xcb |\xcc |\xcd |\xce |\xcf |" 61 "\xd0 |\xd1 |\xd2 |\xd3 |\xd4 |\xd5 |\xd6 |\xd7 |\xd8 |\xd9 |\xda |\xdb |\xdc |\xdd |\xde |\xdf !", 0}, 62 {"16 first bytes of 3-byte sequences {0xe0-0xef}", 63 "\xe0 |\xe1 |\xe2 |\xe3 |\xe4 |\xe5 |\xe6 |\xe7 |\xe8 |\xe9 |\xea |\xeb |\xec |\xed |\xee |\xef !", 0}, 64 {"8 first bytes of 4-byte sequences {0xf0-0xf7}", "\xf0 |\xf1 |\xf2 |\xf3 |\xf4 |\xf5 |\xf6 |\xf7 !", 0}, 65 {"4 first bytes of 5-byte sequences {0xf8-0xfb}", "\xf8 |\xf9 |\xfa |\xfb !", 0}, 66 {"2 first bytes of 6-byte sequences {0xfc-0xfd}", "\xfc |\xfd !", 0}, 67 68 {"sequences with last byte missing {u-0}", 69 "\xc0|\xe0\x80|\xf0\x80\x80|\xf8\x80\x80\x80|\xfc\x80\x80\x80\x80!", 0}, 70 {"sequences with last byte missing {u-...FF}", 71 "\xdf|\xef\xbf|\xf7\xbf\xbf|\xfb\xbf\xbf\xbf|\xfd\xbf\xbf\xbf\xbf!", 0}, 72 73 {"impossible bytes", "\xfe|\xff|\xfe\xfe\xff\xff!", 0}, 74 75 {"overlong sequences {u-2f}", 76 "\xc0\xaf|\xe0\x80\xaf|\xf0\x80\x80\xaf|\xf8\x80\x80\x80\xaf|\xfc\x80\x80\x80\x80\xaf!", 0}, 77 78 {"maximum overlong sequences", 79 "\xc1\xbf|\xe0\x9f\xbf|\xf0\x8f\xbf\xbf|\xf8\x87\xbf\xbf\xbf|\xfc\x83\xbf\xbf\xbf\xbf!", 0}, 80 81 {"overlong representation of the NUL character", 82 "\xc0\x80|\xe0\x80\x80|\xf0\x80\x80\x80|\xf8\x80\x80\x80\x80|\xfc\x80\x80\x80\x80\x80!", 0}, 83 84 {"single UTF-16 surrogates", 85 "\xed\xa0\x80|\xed\xad\xbf|\xed\xae\x80|\xed\xaf\xbf|\xed\xb0\x80|\xed\xbe\x80|\xed\xbf\xbf!", 0}, 86 87 {"paired UTF-16 surrogates", 88 "\xed\xa0\x80\xed\xb0\x80|\xed\xa0\x80\xed\xbf\xbf|\xed\xad\xbf\xed\xb0\x80|" 89 "\xed\xad\xbf\xed\xbf\xbf|\xed\xae\x80\xed\xb0\x80|\xed\xae\x80\xed\xbf\xbf|" 90 "\xed\xaf\xbf\xed\xb0\x80|\xed\xaf\xbf\xed\xbf\xbf!", 0}, 91 92 {"other illegal code positions", "\xef\xbf\xbe|\xef\xbf\xbf!", 0}, 93 94 {NULL, NULL, 0} 95 95 }; 96 97 test_case boms[] = { 98 99 /* {"title", "test!", lenth}, */ 100 101 {"no bom (utf-8)", "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13}, 102 {"bom (utf-8)", "\xef\xbb\xbfHi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82!", 13}, 103 {"bom (utf-16-le)", "\xff\xfeH\x00i\x00 \x00i\x00s\x00 \x00\x1f\x04@\x04""8\x04""2\x04""5\x04""B\x04!", 13}, 104 {"bom (utf-16-be)", "\xfe\xff\x00H\x00i\x00 \x00i\x00s\x00 \x04\x1f\x04@\x04""8\x04""2\x04""5\x04""B!", 13} 105 }; 106 107 char *bom_original = "Hi is \xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82"; 96 108 97 109 int check_utf8_sequences(void) … … 125 137 } 126 138 else if (parser->error == YAML_READER_ERROR) { 127 printf("(reader error: %s at %d)\n", parser->problem, parser->problem_offset); 139 if (parser->problem_value != -1) { 140 printf("(reader error: %s: #%X at %d)\n", 141 parser->problem, parser->problem_value, parser->problem_offset); 142 } 143 else { 144 printf("(reader error: %s at %d)\n", 145 parser->problem, parser->problem_offset); 146 } 128 147 } 129 148 if (*end == '!') break; … … 137 156 } 138 157 158 int check_boms(void) 159 { 160 yaml_parser_t *parser; 161 int failed = 0; 162 int k; 163 printf("checking boms...\n"); 164 for (k = 0; boms[k].test; k++) { 165 char *title = boms[k].title; 166 int check = boms[k].result; 167 int result; 168 char *start = boms[k].test; 169 char *end = start; 170 while (*end != '!') end++; 171 printf("\t%s: ", title); 172 parser = yaml_parser_new(); 173 assert(parser); 174 yaml_parser_set_input_string(parser, (unsigned char *)start, end-start); 175 result = yaml_parser_update_buffer(parser, end-start); 176 if (!result) { 177 printf("- (reader error: %s at %d)\n", parser->problem, parser->problem_offset); 178 failed++; 179 } 180 else { 181 if (parser->unread != check) { 182 printf("- (length=%d while expected length=%d)\n", parser->unread, check); 183 failed++; 184 } 185 else if (memcmp(parser->buffer, bom_original, check) != 0) { 186 printf("- (value '%s' does not equal to the original value '%s')\n", parser->buffer, bom_original); 187 failed++; 188 } 189 else { 190 printf("+\n"); 191 } 192 } 193 yaml_parser_delete(parser); 194 } 195 printf("checking boms: %d fail(s)\n", failed); 196 return failed; 197 } 198 199 #define LONG 100000 200 201 int check_long_utf8(void) 202 { 203 yaml_parser_t *parser; 204 int k = 0; 205 int j; 206 int failed = 0; 207 unsigned char ch0, ch1; 208 unsigned char *buffer = malloc(3+LONG*2); 209 assert(buffer); 210 printf("checking a long utf8 sequence...\n"); 211 buffer[k++] = '\xef'; 212 buffer[k++] = '\xbb'; 213 buffer[k++] = '\xbf'; 214 for (j = 0; j < LONG; j ++) { 215 if (j % 2) { 216 buffer[k++] = '\xd0'; 217 buffer[k++] = '\x90'; 218 } 219 else { 220 buffer[k++] = '\xd0'; 221 buffer[k++] = '\xaf'; 222 } 223 } 224 parser = yaml_parser_new(); 225 assert(parser); 226 yaml_parser_set_input_string(parser, buffer, 3+LONG*2); 227 for (k = 0; k < LONG; k++) { 228 if (!parser->unread) { 229 if (!yaml_parser_update_buffer(parser, 1)) { 230 printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); 231 failed = 1; 232 break; 233 } 234 } 235 if (!parser->unread) { 236 printf("\tnot enough characters at %d\n", k); 237 failed = 1; 238 break; 239 } 240 if (k % 2) { 241 ch0 = '\xd0'; 242 ch1 = '\x90'; 243 } 244 else { 245 ch0 = '\xd0'; 246 ch1 = '\xaf'; 247 } 248 if (parser->pointer[0] != ch0 || parser->pointer[1] != ch1) { 249 printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n", 250 (int)parser->pointer[0], (int)parser->pointer[1], 251 (int)ch0, (int)ch1); 252 failed = 1; 253 break; 254 } 255 parser->pointer += 2; 256 parser->unread -= 1; 257 } 258 if (!failed) { 259 if (!yaml_parser_update_buffer(parser, 1)) { 260 printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); 261 failed = 1; 262 } 263 else if (parser->pointer[0] != '\0') { 264 printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser->pointer[0], parser->eof, parser->unread); 265 failed = 1; 266 } 267 } 268 yaml_parser_delete(parser); 269 free(buffer); 270 printf("checking a long utf8 sequence: %d fail(s)\n", failed); 271 return failed; 272 } 273 274 int check_long_utf16(void) 275 { 276 yaml_parser_t *parser; 277 int k = 0; 278 int j; 279 int failed = 0; 280 unsigned char ch0, ch1; 281 unsigned char *buffer = malloc(2+LONG*2); 282 assert(buffer); 283 printf("checking a long utf16 sequence...\n"); 284 buffer[k++] = '\xff'; 285 buffer[k++] = '\xfe'; 286 for (j = 0; j < LONG; j ++) { 287 if (j % 2) { 288 buffer[k++] = '\x10'; 289 buffer[k++] = '\x04'; 290 } 291 else { 292 buffer[k++] = '/'; 293 buffer[k++] = '\x04'; 294 } 295 } 296 parser = yaml_parser_new(); 297 assert(parser); 298 yaml_parser_set_input_string(parser, buffer, 2+LONG*2); 299 for (k = 0; k < LONG; k++) { 300 if (!parser->unread) { 301 if (!yaml_parser_update_buffer(parser, 1)) { 302 printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); 303 failed = 1; 304 break; 305 } 306 } 307 if (!parser->unread) { 308 printf("\tnot enough characters at %d\n", k); 309 failed = 1; 310 break; 311 } 312 if (k % 2) { 313 ch0 = '\xd0'; 314 ch1 = '\x90'; 315 } 316 else { 317 ch0 = '\xd0'; 318 ch1 = '\xaf'; 319 } 320 if (parser->pointer[0] != ch0 || parser->pointer[1] != ch1) { 321 printf("\tincorrect UTF-8 sequence: %X %X instead of %X %X\n", 322 (int)parser->pointer[0], (int)parser->pointer[1], 323 (int)ch0, (int)ch1); 324 failed = 1; 325 break; 326 } 327 parser->pointer += 2; 328 parser->unread -= 1; 329 } 330 if (!failed) { 331 if (!yaml_parser_update_buffer(parser, 1)) { 332 printf("\treader error: %s at %d\n", parser->problem, parser->problem_offset); 333 failed = 1; 334 } 335 else if (parser->pointer[0] != '\0') { 336 printf("\texpected NUL, found %X (eof=%d, unread=%d)\n", (int)parser->pointer[0], parser->eof, parser->unread); 337 failed = 1; 338 } 339 } 340 yaml_parser_delete(parser); 341 free(buffer); 342 printf("checking a long utf16 sequence: %d fail(s)\n", failed); 343 return failed; 344 } 139 345 140 346 int 141 347 main(void) 142 348 { 143 return check_utf8_sequences() ;144 } 349 return check_utf8_sequences() + check_boms() + check_long_utf8() + check_long_utf16(); 350 }
Note: See TracChangeset
for help on using the changeset viewer.
