Changeset 183
- Timestamp:
- 06/02/06 09:03:14 (2 years ago)
- Files:
-
- libyaml/trunk/doc/doxygen.cfg (modified) (1 diff)
- libyaml/trunk/include/yaml/yaml.h (modified) (18 diffs)
- libyaml/trunk/src/api.c (modified) (10 diffs)
- libyaml/trunk/src/reader.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libyaml/trunk/doc/doxygen.cfg
r178 r183 175 175 #--------------------------------------------------------------------------- 176 176 ENABLE_PREPROCESSING = YES 177 MACRO_EXPANSION = NO178 EXPAND_ONLY_PREDEF = NO177 MACRO_EXPANSION = YES 178 EXPAND_ONLY_PREDEF = YES 179 179 SEARCH_INCLUDES = YES 180 180 INCLUDE_PATH = 181 181 INCLUDE_FILE_PATTERNS = 182 PREDEFINED = 182 PREDEFINED = "YAML_DECLARE(type)=type" 183 183 EXPAND_AS_DEFINED = 184 184 SKIP_FUNCTION_MACROS = YES libyaml/trunk/include/yaml/yaml.h
r182 r183 21 21 22 22 /** 23 * @defgroup Export Definitions 24 * @{ 25 */ 26 27 /** The public API declaration. */ 28 29 #ifdef WIN32 30 # if defined(YAML_DECLARE_STATIC) 31 # define YAML_DECLARE(type) type 32 # elif defined(YAML_DECLARE_EXPORT) 33 # define YAML_DECLARE(type) __declspec(dllexport) type 34 # else 35 # define YAML_DECLARE(type) __declspec(dllimport) type 36 # endif 37 #else 38 # define YAML_DECLARE(type) type 39 #endif 40 41 /** @} */ 42 43 /** 23 44 * @defgroup version Version Information 24 45 * @{ … … 33 54 */ 34 55 35 const char * 56 YAML_DECLARE(const char *) 36 57 yaml_get_version_string(void); 37 58 … … 44 65 */ 45 66 46 void 67 YAML_DECLARE(void) 47 68 yaml_get_version(int *major, int *minor, int *patch); 48 69 … … 54 75 */ 55 76 56 /** The character type . */77 /** The character type (UTF-8 octet). */ 57 78 typedef unsigned char yaml_char_t; 58 79 … … 79 100 } yaml_error_type_t; 80 101 102 /** The pointer position. */ 103 typedef struct { 104 /** The position index. */ 105 size_t index; 106 107 /** The position line. */ 108 size_t line; 109 110 /** The position column. */ 111 size_t column; 112 } yaml_mark_t; 113 81 114 /** @} */ 82 115 83 /* 84 116 /** 117 * @defgroup Node Styles 118 * @{ 119 */ 120 121 /** Scalar styles. */ 85 122 typedef enum { 86 123 YAML_ANY_SCALAR_STYLE, 124 87 125 YAML_PLAIN_SCALAR_STYLE, 126 88 127 YAML_SINGLE_QUOTED_SCALAR_STYLE, 89 128 YAML_DOUBLE_QUOTED_SCALAR_STYLE, 129 90 130 YAML_LITERAL_SCALAR_STYLE, 91 131 YAML_FOLDED_SCALAR_STYLE 92 132 } yaml_scalar_style_t; 93 133 134 135 /** Sequence styles. */ 94 136 typedef enum { 95 137 YAML_ANY_SEQUENCE_STYLE, 138 96 139 YAML_BLOCK_SEQUENCE_STYLE, 97 140 YAML_FLOW_SEQUENCE_STYLE 98 141 } yaml_sequence_style_t; 99 142 143 /** Mapping styles. */ 100 144 typedef enum { 101 145 YAML_ANY_MAPPING_STYLE, 146 102 147 YAML_BLOCK_MAPPING_STYLE, 103 148 YAML_FLOW_MAPPING_STYLE 104 149 } yaml_mapping_style_t; 105 150 151 /** @} */ 152 153 /** 154 * @defgroup Tokens 155 * @{ 156 */ 157 158 /** Token types. */ 106 159 typedef enum { 107 160 YAML_STREAM_START_TOKEN, … … 133 186 } yaml_token_type_t; 134 187 188 /** The token structure. */ 189 typedef struct { 190 191 /** The token type. */ 192 yaml_token_type_t type; 193 194 /** The token data. */ 195 union { 196 197 /** The stream encoding (for @c YAML_STREAM_START_TOKEN). */ 198 yaml_encoding_t encoding; 199 200 /** The anchor (for @c YAML_ALIAS_TOKEN and @c YAML_ANCHOR_TOKEN). */ 201 yaml_char_t *anchor; 202 203 /** The tag (for @c YAML_TAG_TOKEN). */ 204 struct { 205 /** The tag handle. */ 206 yaml_char_t *handle; 207 208 /** The tag suffix. */ 209 yaml_char_t *suffix; 210 } tag; 211 212 /** The scalar value (for @c YAML_SCALAR_TOKEN). */ 213 struct { 214 215 /** The scalar value. */ 216 yaml_char_t *value; 217 218 /** The length of the scalar value. */ 219 size_t length; 220 221 /** The scalar style. */ 222 yaml_scalar_style_t style; 223 } scalar; 224 225 /** The version directive (for @c YAML_VERSION_DIRECTIVE_TOKEN). */ 226 struct { 227 /** The major version number. */ 228 int major; 229 230 /** The minor version number. */ 231 int minor; 232 } version_directive; 233 234 /** The tag directive (for @c YAML_TAG_DIRECTIVE_TOKEN). */ 235 struct { 236 /** The tag handle. */ 237 yaml_char_t *handle; 238 239 /** The tag prefix. */ 240 yaml_char_t *prefix; 241 } tag_directive; 242 } data; 243 244 /** The beginning of the token. */ 245 yaml_mark_t start_mark; 246 247 /** The end of the token. */ 248 yaml_mark_t end_mark; 249 250 } yaml_token_t; 251 252 /** 253 * Create a new token without assigning any data. 254 * 255 * This function can be used for constructing indicator tokens: 256 * @c YAML_DOCUMENT_START, @c YAML_DOCUMENT_END, 257 * @c YAML_BLOCK_SEQUENCE_START_TOKEN, @c YAML_BLOCK_MAPPING_START_TOKEN, 258 * @c YAML_BLOCK_END_TOKEN, 259 * @c YAML_FLOW_SEQUENCE_START_TOKEN, @c YAML_FLOW_SEQUENCE_END_TOKEN, 260 * @c YAML_FLOW_MAPPING_START_TOKEN, @c YAML_FLOW_MAPPING_END_TOKEN, 261 * @c YAML_BLOCK_ENTRY_TOKEN, @c YAML_FLOW_ENTRY_TOKEN, 262 * @c YAML_KEY_TOKEN, @c YAML_VALUE_TOKEN. 263 * 264 * @param[in] type The token type. 265 * @param[in] start_mark The beginning of the token. 266 * @param[in] end_mark The end of the token. 267 * 268 * @returns A new token object, or @c NULL on error. 269 */ 270 271 YAML_DECLARE(yaml_token_t *) 272 yaml_token_new(yaml_token_type_t type, 273 yaml_mark_t start_mark, yaml_mark_t end_mark); 274 275 /** 276 * Create a new @c YAML_STREAM_START_TOKEN token with the specified encoding. 277 * 278 * @param[in] encoding The stream encoding. 279 * @param[in] start_mark The beginning of the token. 280 * @param[in] end_mark The end of the token. 281 * 282 * @returns A new token object, or @c NULL on error. 283 */ 284 285 YAML_DECLARE(yaml_token_t *) 286 yaml_stream_start_token_new(yaml_encoding_t encoding, 287 yaml_mark_t start_mark, yaml_mark_t end_mark); 288 289 /** 290 * Create a new @c YAML_STREAM_END_TOKEN token. 291 * 292 * @param[in] start_mark The beginning of the token. 293 * @param[in] end_mark The end of the token. 294 * 295 * @returns A new token object, or @c NULL on error. 296 */ 297 298 YAML_DECLARE(yaml_token_t *) 299 yaml_stream_end_token_new(yaml_mark_t start_mark, yaml_mark_t end_mark); 300 301 /** 302 * Create a new @c YAML_VERSION_DIRECTIVE_TOKEN token with the specified 303 * version numbers. 304 * 305 * @param[in] major The major version number. 306 * @param[in] minor The minor version number. 307 * @param[in] start_mark The beginning of the token. 308 * @param[in] end_mark The end of the token. 309 * 310 * @returns A new token object, or @c NULL on error. 311 */ 312 313 YAML_DECLARE(yaml_token_t *) 314 yaml_version_directive_token_new(int major, int minor, 315 yaml_mark_t start_mark, yaml_mark_t end_mark); 316 317 /** 318 * Create a new @c YAML_TAG_DIRECTIVE_TOKEN token with the specified tag 319 * handle and prefix. 320 * 321 * Note that the @a handle and the @a prefix pointers will be freed by 322 * the token descructor. 323 * 324 * @param[in] handle The tag handle. 325 * @param[in] prefix The tag prefix. 326 * @param[in] start_mark The beginning of the token. 327 * @param[in] end_mark The end of the token. 328 * 329 * @returns A new token object, or @c NULL on error. 330 */ 331 332 YAML_DECLARE(yaml_token_t *) 333 yaml_tag_directive_token_new(yaml_char_t *handle, yaml_char_t *prefix, 334 yaml_mark_t start_mark, yaml_mark_t end_mark); 335 336 /** 337 * Create a new @c YAML_ALIAS_TOKEN token with the specified anchor. 338 * 339 * Note that the @a anchor pointer will be freed by the token descructor. 340 * 341 * @param[in] anchor The anchor. 342 * @param[in] start_mark The beginning of the token. 343 * @param[in] end_mark The end of the token. 344 * 345 * @returns A new token object, or @c NULL on error. 346 */ 347 348 YAML_DECLARE(yaml_token_t *) 349 yaml_alias_token_new(yaml_char_t *anchor, 350 yaml_mark_t start_mark, yaml_mark_t end_mark); 351 352 /** 353 * Create a new @c YAML_ANCHOR_TOKEN token with the specified anchor. 354 * 355 * Note that the @a anchor pointer will be freed by the token descructor. 356 * 357 * @param[in] anchor The anchor. 358 * @param[in] start_mark The beginning of the token. 359 * @param[in] end_mark The end of the token. 360 * 361 * @returns A new token object, or @c NULL on error. 362 */ 363 364 YAML_DECLARE(yaml_token_t *) 365 yaml_anchor_token_new(yaml_char_t *anchor, 366 yaml_mark_t start_mark, yaml_mark_t end_mark); 367 368 /** 369 * Create a new @c YAML_TAG_TOKEN token with the specified tag handle and 370 * suffix. 371 * 372 * Note that the @a handle and the @a suffix pointers will be freed by 373 * the token descructor. 374 * 375 * @param[in] handle The tag handle. 376 * @param[in] suffix The tag suffix. 377 * @param[in] start_mark The beginning of the token. 378 * @param[in] end_mark The end of the token. 379 * 380 * @returns A new token object, or @c NULL on error. 381 */ 382 383 YAML_DECLARE(yaml_token_t *) 384 yaml_tag_token_new(yaml_char_t *handle, yaml_char_t *suffix, 385 yaml_mark_t start_mark, yaml_mark_t end_mark); 386 387 /** 388 * Create a new @c YAML_SCALAR_TOKEN token with the specified scalar value, 389 * length, and style. 390 * 391 * Note that the scalar value may contain the @c NUL character, therefore 392 * the value length is also required. The scalar value always ends with 393 * @c NUL. 394 * 395 * Note that the @a value pointer will be freed by the token descructor. 396 * 397 * @param[in] value The scalar value. 398 * @param[in] length The value length. 399 * @param[in] style The scalar style. 400 * @param[in] start_mark The beginning of the token. 401 * @param[in] end_mark The end of the token. 402 * 403 * @returns A new token object, or @c NULL on error. 404 */ 405 406 YAML_DECLARE(yaml_token_t *) 407 yaml_scalar_token_new(yaml_char_t *value, size_t length, 408 yaml_scalar_style_t style, 409 yaml_mark_t start_mark, yaml_mark_t end_mark); 410 411 /** 412 * Destroy a token object. 413 * 414 * @param[in] token A token object. 415 */ 416 417 YAML_DECLARE(void) 418 yaml_token_delete(yaml_token_t *token); 419 420 /** @} */ 421 422 /* 423 135 424 typedef enum { 136 425 YAML_STREAM_START_EVENT, … … 149 438 YAML_MAPPING_END_EVENT 150 439 } yaml_event_type_t; 151 152 typedef struct {153 size_t offset;154 size_t index;155 size_t line;156 size_t column;157 } yaml_mark_t;158 159 typedef struct {160 yaml_error_type_t type;161 char *context;162 yaml_mark_t context_mark;163 char *problem;164 yaml_mark_t problem_mark;165 } yaml_error_t;166 167 typedef struct {168 yaml_token_type_t type;169 union {170 yaml_encoding_t encoding;171 char *anchor;172 char *tag;173 struct {174 char *value;175 size_t length;176 yaml_scalar_style_t style;177 } scalar;178 struct {179 int major;180 int minor;181 } version;182 struct {183 char *handle;184 char *prefix;185 } tag_pair;186 } data;187 yaml_mark_t start_mark;188 yaml_mark_t end_mark;189 } yaml_token_t;190 440 191 441 typedef struct { … … 273 523 274 524 typedef struct { 525 /** The string start pointer. */ 275 526 unsigned char *start; 527 528 /** The string end pointer. */ 276 529 unsigned char *end; 530 531 /** The string current position. */ 277 532 unsigned char *current; 278 533 } yaml_string_input_t; … … 376 631 */ 377 632 378 yaml_parser_t * 633 YAML_DECLARE(yaml_parser_t *) 379 634 yaml_parser_new(void); 380 635 … … 385 640 */ 386 641 387 void 642 YAML_DECLARE(void) 388 643 yaml_parser_delete(yaml_parser_t *parser); 389 644 … … 397 652 * @param[in] parser A parser object. 398 653 * @param[in] input A source data. 399 * @param[in] lengthThe length of the source data in bytes.400 */ 401 402 void 654 * @param[in] size The length of the source data in bytes. 655 */ 656 657 YAML_DECLARE(void) 403 658 yaml_parser_set_input_string(yaml_parser_t *parser, 404 659 unsigned char *input, size_t size); … … 415 670 */ 416 671 417 void 672 YAML_DECLARE(void) 418 673 yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file); 419 674 … … 426 681 */ 427 682 428 void 683 YAML_DECLARE(void) 429 684 yaml_parser_set_input(yaml_parser_t *parser, 430 685 yaml_read_handler_t *handler, void *data); … … 433 688 * Set the source encoding. 434 689 * 690 * @param[in] parser A parser object. 435 691 * @param[in] encoding The source encoding. 436 692 */ 437 693 438 void 694 YAML_DECLARE(void) 439 695 yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding); 440 696 … … 460 716 */ 461 717 462 void * 718 YAML_DECLARE(void *) 463 719 yaml_malloc(size_t size); 464 720 … … 474 730 */ 475 731 476 void * 732 YAML_DECLARE(void *) 477 733 yaml_realloc(void *ptr, size_t size); 478 734 … … 484 740 */ 485 741 486 void 742 YAML_DECLARE(void) 487 743 yaml_free(void *ptr); 488 744 … … 508 764 */ 509 765 510 int 766 YAML_DECLARE(int) 511 767 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length); 512 768 libyaml/trunk/src/api.c
r180 r183 12 12 */ 13 13 14 void * 14 YAML_DECLARE(void *) 15 15 yaml_malloc(size_t size) 16 16 { … … 22 22 */ 23 23 24 void * 24 YAML_DECLARE(void *) 25 25 yaml_realloc(void *ptr, size_t size) 26 26 { … … 32 32 */ 33 33 34 void 34 YAML_DECLARE(void) 35 35 yaml_free(void *ptr) 36 36 { … … 42 42 */ 43 43 44 yaml_parser_t * 44 YAML_DECLARE(yaml_parser_t *) 45 45 yaml_parser_new(void) 46 46 { … … 83 83 */ 84 84 85 void 85 YAML_DECLARE(void) 86 86 yaml_parser_delete(yaml_parser_t *parser) 87 87 { … … 137 137 */ 138 138 139 void 139 YAML_DECLARE(void) 140 140 yaml_parser_set_input_string(yaml_parser_t *parser, 141 141 unsigned char *input, size_t size) … … 157 157 */ 158 158 159 void 159 YAML_DECLARE(void) 160 160 yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) 161 161 { … … 172 172 */ 173 173 174 void 174 YAML_DECLARE(void) 175 175 yaml_parser_set_input(yaml_parser_t *parser, 176 176 yaml_read_handler_t *handler, void *data) … … 188 188 */ 189 189 190 void 190 YAML_DECLARE(void) 191 191 yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) 192 192 { … … 197 197 } 198 198 199 /* 200 * Create a token. 201 */ 202 203 YAML_DECLARE(yaml_token_t *) 204 yaml_token_new(yaml_token_type_t type, 205 yaml_mark_t start_mark, yaml_mark_t end_mark) 206 { 207 yaml_token_t *token = yaml_malloc(sizeof(yaml_token_t)); 208 209 if (!token) return NULL; 210 211 memset(token, 0, sizeof(yaml_token_t)); 212 213 token->type = type; 214 token->start_mark = start_mark; 215 token->end_mark = end_mark; 216 217 return token; 218 } 219 220 /* 221 * Create a STREAM-START token. 222 */ 223 224 YAML_DECLARE(yaml_token_t *) 225 yaml_stream_start_token(yaml_encoding_t encoding, 226 yaml_mark_t start_mark, yaml_mark_t end_mark) 227 { 228 yaml_token_t *token = yaml_token_new(YAML_STREAM_START_TOKEN, 229 start_mark, end_mark); 230 231 if (!token) return NULL; 232 233 token->data.encoding = encoding; 234 235 return token; 236 } 237 238 /* 239 * Create a STREAM-END token. 240 */ 241 242 YAML_DECLARE(yaml_token_t *) 243 yaml_stream_end_token(yaml_mark_t start_mark, yaml_mark_t end_mark) 244 { 245 yaml_token_t *token = yaml_token_new(YAML_STREAM_END_TOKEN, 246 start_mark, end_mark); 247 248 if (!token) return NULL; 249 250 return token; 251 } 252 253 /* 254 * Create a VERSION-DIRECTIVE token. 255 */ 256 257 YAML_DECLARE(yaml_token_t *) 258 yaml_version_directive_token_new(int major, int minor, 259 yaml_mark_t start_mark, yaml_mark_t end_mark) 260 { 261 yaml_token_t *token = yaml_token_new(YAML_VERSION_DIRECTIVE_TOKEN, 262 start_mark, end_mark); 263 264 if (!token) return NULL; 265 266 token->data.version_directive.major = major; 267 token->data.version_directive.minor = minor; 268 269 return token; 270 } 271 272 /* 273 * Create a TAG-DIRECTIVE token. 274 */ 275 276 YAML_DECLARE(yaml_token_t *) 277 yaml_tag_directive_token_new(yaml_char_t *handle, yaml_char_t *prefix, 278 yaml_mark_t start_mark, yaml_mark_t end_mark) 279 { 280 yaml_token_t *token = yaml_token_new(YAML_TAG_DIRECTIVE_TOKEN, 281 start_mark, end_mark); 282 283 if (!token) return NULL; 284 285 token->data.tag_directive.handle = handle; 286 token->data.tag_directive.prefix = prefix; 287 288 return token; 289 } 290 291 /* 292 * Create an ALIAS token. 293 */ 294 295 YAML_DECLARE(yaml_token_t *) 296 yaml_alias_token_new(yaml_char_t *anchor, 297 yaml_mark_t start_mark, yaml_mark_t end_mark) 298 { 299 yaml_token_t *token = yaml_token_new(YAML_ALIAS_TOKEN, 300 start_mark, end_mark); 301 302 if (!token) return NULL; 303 304 token->data.anchor = anchor; 305 306 return token; 307 } 308 309 /* 310 * Create an ANCHOR token. 311 */ 312 313 YAML_DECLARE(yaml_token_t *) 314 yaml_anchor_token_new(yaml_char_t *anchor, 315 yaml_mark_t start_mark, yaml_mark_t end_mark) 316 { 317 yaml_token_t *token = yaml_token_new(YAML_ANCHOR_TOKEN, 318 start_mark, end_mark); 319 320 if (!token) return NULL; 321 322 token->data.anchor = anchor; 323 324 return token; 325 } 326 327 /* 328 * Create a TAG token. 329 */ 330 331 YAML_DECLARE(yaml_token_t *) 332 yaml_tag_token_new(yaml_char_t *handle, yaml_char_t *suffix, 333 yaml_mark_t start_mark, yaml_mark_t end_mark) 334 { 335 yaml_token_t *token = yaml_token_new(YAML_TAG_TOKEN, 336 start_mark, end_mark); 337 338 if (!token) return NULL; 339 340 token->data.tag.handle = handle; 341 token->data.tag.suffix = suffix; 342 343 return token; 344 } 345 346 /* 347 * Create a SCALAR token. 348 */ 349 350 YAML_DECLARE(yaml_token_t *) 351 yaml_scalar_token_new(yaml_char_t *value, size_t length, 352 yaml_scalar_style_t style, 353 yaml_mark_t start_mark, yaml_mark_t end_mark) 354 { 355 yaml_token_t *token = yaml_token_new(YAML_SCALAR_TOKEN, 356 start_mark, end_mark); 357 358 if (!token) return NULL; 359 360 token->data.scalar.value = value; 361 token->data.scalar.length = length; 362 token->data.scalar.style = style; 363 364 return token; 365 } 366 367 /* 368 * Destroy a token object. 369 */ 370 371 YAML_DECLARE(void) 372 yaml_token_delete(yaml_token_t *token) 373 { 374 assert(token); /* Non-NULL token object expected. */ 375 376 switch (token->type) 377 { 378 case YAML_TAG_DIRECTIVE_TOKEN: 379 yaml_free(token->data.tag_directive.handle); 380 yaml_free(token->data.tag_directive.prefix); 381 break; 382 383 case YAML_ALIAS_TOKEN: 384 case YAML_ANCHOR_TOKEN: 385 yaml_free(token->data.anchor); 386 break; 387 388 case YAML_TAG_TOKEN: 389 yaml_free(token->data.tag.handle); 390 yaml_free(token->data.tag.suffix); 391 break; 392 393 case YAML_SCALAR_TOKEN: 394 yaml_free(token->data.scalar.value); 395 break; 396 } 397 398 memset(token, 0, sizeof(yaml_token_t)); 399 400 yaml_free(token); 401 } 402 libyaml/trunk/src/reader.c
r182 r183 12 12 */ 13 13 14 int14 static int 15 15 yaml_parser_set_reader_error(yaml_parser_t *parser, const char *problem, 16 16 size_t offset, int value) … … 24 24 } 25 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 } 26 116 27 117 /* … … 32 122 */ 33 123 34 int 124 YAML_DECLARE(int) 35 125 yaml_parser_update_buffer(yaml_parser_t *parser, size_t length) 36 126 { … … 346 436 } 347 437 348 /*349 * Determine the input stream encoding by checking the BOM symbol. If no BOM is350 * found, the UTF-8 encoding is assumed. Return 1 on success, 0 on failure.351 */352 353 #define BOM_UTF8 "\xef\xbb\xbf"354 #define BOM_UTF16LE "\xff\xfe"355 #define BOM_UTF16BE "\xfe\xff"356 357 int358 yaml_parser_determine_encoding(yaml_parser_t *parser)359 {360 /* Ensure that we had enough bytes in the raw buffer. */361 362 while (!parser->eof && parser->raw_unread < 3) {363 if (!yaml_parser_update_raw_buffer(parser)) {364 return 0;365 }366 }367 368 /* Determine the encoding. */369 370 if (parser->raw_unread >= 2371 && !memcmp(parser->raw_pointer, BOM_UTF16LE, 2)) {372 parser->encoding = YAML_UTF16LE_ENCODING;373 parser->raw_pointer += 2;374 parser->raw_unread -= 2;375 parser->offset += 2;376 }377 else if (parser->raw_unread >= 2378 && !memcmp(parser->raw_pointer, BOM_UTF16BE, 2)) {379 parser->encoding = YAML_UTF16BE_ENCODING;380 parser->raw_pointer += 2;381 parser->raw_unread -= 2;382 parser->offset += 2;383 }384 else if (parser->raw_unread >= 3385 && !memcmp(parser->raw_pointer, BOM_UTF8, 3)) {386 parser->encoding = YAML_UTF8_ENCODING;387 parser->raw_pointer += 3;388 parser->raw_unread -= 3;389 parser->offset += 3;390 }391 else {392 parser->encoding = YAML_UTF8_ENCODING;393 }394 395 return 1;396 }397 398 /*399 * Update the raw buffer.400 */401 402 int403 yaml_parser_update_raw_buffer(yaml_parser_t *parser)404 {405 size_t size_read = 0;406 407 /* Return if the raw buffer is full. */408 409 if (parser->raw_unread == YAML_RAW_BUFFER_SIZE) return 1;410 411 /* Return on EOF. */412 413 if (parser->eof) return 1;414 415 /* Move the remaining bytes in the raw buffer to the beginning. */416 417 if (parser->raw_unread && parser->raw_buffer < parser->raw_pointer) {418 memmove(parser->raw_buffer, parser->raw_pointer, parser->raw_unread);419 }420 parser->raw_pointer = parser->raw_buffer;421 422 /* Call the read handler to fill the buffer. */423 424 if (!parser->read_handler(parser->read_handler_data,425 parser->raw_buffer + parser->raw_unread,426 YAML_RAW_BUFFER_SIZE - parser->raw_unread,427 &size_read)) {428 return yaml_parser_set_reader_error(parser, "Input error",429 parser->offset, -1);430 }431 parser->raw_unread += size_read;432 if (!size_read) {433 parser->eof = 1;434 }435 436 return 1;437 }438
