1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
/**
* YAML parser and emitter PHP extension
*
* Copyright (c) 2007 Ryusuke SEKIYAMA. All rights reserved.
* Copyright (c) 2009 Keynetics Inc. All rights reserved.
* Copyright (c) 2015 Bryan Davis and contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* @package php_yaml
* @author Ryusuke SEKIYAMA <rsky0711@gmail.com>
* @author Bryan Davis <bd808@bd808.com>
* @copyright 2007 Ryusuke SEKIYAMA
* @copyright 2009 Keynetics Inc
* @copyright 2015 Bryan Davis and contributors
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
#include "php_yaml.h"
#include "php_yaml_int.h"
/* {{{ local macros
*/
#define y_event_init_failed(e) \
yaml_event_delete(e); \
php_error_docref(NULL TSRMLS_CC, E_WARNING,\
"Memory error: Not enough memory for creating an event (libyaml)")
#define Y_ARRAY_SEQUENCE 1
#define Y_ARRAY_MAP 2
/* }}} */
/* {{{ local prototypes
*/
static int y_event_emit(
const y_emit_state_t *state, yaml_event_t *event TSRMLS_DC);
static void y_handle_emitter_error(const y_emit_state_t *state TSRMLS_DC);
static int y_array_is_sequence(HashTable *ht TSRMLS_DC);
static void y_scan_recursion(const y_emit_state_t *state, zval *data TSRMLS_DC);
static zend_long y_search_recursive(
const y_emit_state_t *state, const zend_ulong addr TSRMLS_DC);
static int y_write_zval(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_null(
const y_emit_state_t *state, yaml_char_t *tag TSRMLS_DC);
static int y_write_bool(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_long(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_double(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_string(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_array(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_timestamp(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_object(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC);
static int y_write_object_callback (
const y_emit_state_t *state, zval *callback, zval *data,
const char *clazz_name TSRMLS_DC);
static inline unsigned int get_next_char(
const unsigned char *str, size_t str_len, size_t *cursor, int *status);
/* }}} */
/* {{{ y_event_emit()
* send an event to the emitter
*/
static int
y_event_emit(const y_emit_state_t *state, yaml_event_t *event TSRMLS_DC)
{
if (!yaml_emitter_emit(state->emitter, event)) {
yaml_event_delete(event);
y_handle_emitter_error(state TSRMLS_CC);
return FAILURE;
} else {
return SUCCESS;
}
}
/* }}} */
/* {{{ y_handle_emitter_error()
* Emit a warning about an emitter error
*/
static void y_handle_emitter_error(const y_emit_state_t *state TSRMLS_DC)
{
switch (state->emitter->error) {
case YAML_MEMORY_ERROR:
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Memory error: Not enough memory for emitting");
break;
case YAML_WRITER_ERROR:
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Writer error: %s", state->emitter->problem);
break;
case YAML_EMITTER_ERROR:
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Emitter error: %s", state->emitter->problem);
break;
default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal error");
break;
}
}
/* }}} */
/* {{{ y_array_is_sequence()
* Does the array encode a sequence?
*/
static int y_array_is_sequence(HashTable *ht TSRMLS_DC)
{
zend_ulong kidx, idx;
zend_string *str_key;
idx = 0;
ZEND_HASH_FOREACH_KEY(ht, kidx, str_key) {
if (str_key) {
/* non-numeric key found */
return Y_ARRAY_MAP;
} else if (kidx != idx) {
/* gap in sequence found */
return Y_ARRAY_MAP;
}
idx++;
} ZEND_HASH_FOREACH_END();
return Y_ARRAY_SEQUENCE;
}
/* }}} */
/* {{{ y_scan_recursion()
* walk an object graph looking for recursive references
*/
static void y_scan_recursion(const y_emit_state_t *state, zval *data TSRMLS_DC)
{
HashTable *ht;
zval *elm;
ZVAL_DEREF(data);
ht = HASH_OF(data);
if (!ht) {
/* data isn't an array or object, so we're done */
return;
}
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE) && GC_IS_RECURSIVE(ht)) {
#else
if (ZEND_HASH_APPLY_PROTECTION(ht) && ht->u.v.nApplyCount > 0) {
#endif
zval tmp;
ZVAL_LONG(&tmp, (zend_ulong) ht);
/* we've seen this before, so record address */
zend_hash_next_index_insert(state->recursive, &tmp);
return;
}
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
GC_PROTECT_RECURSION(ht);
}
#else
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
ht->u.v.nApplyCount++;
}
#endif
ZEND_HASH_FOREACH_VAL(ht, elm) {
y_scan_recursion(state, elm TSRMLS_CC);
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(ht);
}
#else
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
ht->u.v.nApplyCount--;
}
#endif
return;
}
/* }}} */
/* {{{ y_search_recursive()
* Search the recursive state hash for an address
*/
static zend_long y_search_recursive(
const y_emit_state_t *state, const zend_ulong addr TSRMLS_DC)
{
zval *entry;
zend_ulong num_key;
zend_ulong found;
ZEND_HASH_FOREACH_NUM_KEY_VAL(state->recursive, num_key, entry) {
found = Z_LVAL_P(entry);
if (addr == found) {
return num_key;
}
} ZEND_HASH_FOREACH_END();
return -1;
}
/* }}} */
/* {{{ y_write_zval()
* Write a php zval to the emitter
*/
static int y_write_zval(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
int status = FAILURE;
switch (Z_TYPE_P(data)) {
case IS_REFERENCE:
status = y_write_zval(state, Z_REFVAL_P(data), tag);
break;
case IS_NULL:
status = y_write_null(state, tag TSRMLS_CC);
break;
case IS_TRUE:
case IS_FALSE:
status = y_write_bool(state, data, tag TSRMLS_CC);
break;
case IS_LONG:
status = y_write_long(state, data, tag TSRMLS_CC);
break;
case IS_DOUBLE:
status = y_write_double(state, data, tag TSRMLS_CC);
break;
case IS_STRING:
status = y_write_string(state, data, tag TSRMLS_CC);
break;
case IS_ARRAY:
status = y_write_array(state, data, tag TSRMLS_CC);
break;
case IS_OBJECT:
status = y_write_object(state, data, tag TSRMLS_CC);
break;
case IS_RESOURCE: /* unsupported object */
php_error_docref(NULL TSRMLS_CC, E_NOTICE,
"Unable to emit PHP resources.");
break;
default: /* something we didn't think of */
php_error_docref(NULL TSRMLS_CC, E_NOTICE,
"Unsupported php zval type %d.", Z_TYPE_P(data));
break;
}
return status;
}
/* }}} */
/* {{{ y_write_null()
*/
static int y_write_null(const y_emit_state_t *state, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
if (NULL == tag) {
tag = (yaml_char_t *) YAML_NULL_TAG;
omit_tag = 1;
}
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) "~", strlen("~"),
omit_tag, omit_tag, YAML_PLAIN_SCALAR_STYLE);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_bool()
*/
static int y_write_bool(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
const char *res = Z_TYPE_P(data) == IS_TRUE ? "true" : "false";
if (NULL == tag) {
tag = (yaml_char_t *) YAML_BOOL_TAG;
omit_tag = 1;
}
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) res, strlen(res),
omit_tag, omit_tag, YAML_PLAIN_SCALAR_STYLE);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_long()
*/
static int y_write_long(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
char *res = { 0 };
size_t res_size;
if (NULL == tag) {
tag = (yaml_char_t *) YAML_INT_TAG;
omit_tag = 1;
}
res_size = snprintf(res, 0, "%ld", Z_LVAL_P(data));
res = (char*) emalloc(res_size + 1);
snprintf(res, res_size + 1, "%ld", Z_LVAL_P(data));
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) res, strlen(res),
omit_tag, omit_tag, YAML_PLAIN_SCALAR_STYLE);
efree(res);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_double()
*/
static int y_write_double(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
char *res = { 0 };
size_t res_size;
if (NULL == tag) {
tag = (yaml_char_t *) YAML_FLOAT_TAG;
omit_tag = 1;
}
res_size = snprintf(res, 0, "%f", Z_DVAL_P(data));
res = (char*) emalloc(res_size + 1);
snprintf(res, res_size + 1, "%f", Z_DVAL_P(data));
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) res, strlen(res),
omit_tag, omit_tag, YAML_PLAIN_SCALAR_STYLE);
efree(res);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_string()
*/
static int y_write_string(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
yaml_scalar_style_t style = YAML_PLAIN_SCALAR_STYLE;
if (NULL == tag) {
tag = (yaml_char_t *) YAML_STR_TAG;
omit_tag = 1;
}
if (NULL != detect_scalar_type(Z_STRVAL_P(data), Z_STRLEN_P(data), NULL)) {
/* looks like some other type to us, make sure it's quoted */
style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
} else {
size_t pos = 0, us;
int j;
const unsigned char *s = (const unsigned char *)Z_STRVAL_P(data);
int len = Z_STRLEN_P(data);
for (j = 0; pos < len; j++) {
us = get_next_char(s, len, &pos, &status);
if (status != SUCCESS) {
/* invalid UTF-8 character found */
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Invalid UTF-8 sequence in argument");
return FAILURE;
} else if ('\n' == us) {
/* has newline(s), make sure they are preserved */
style = YAML_LITERAL_SCALAR_STYLE;
}
}
}
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) Z_STRVAL_P(data), Z_STRLEN_P(data),
omit_tag, omit_tag, style);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_array()
*/
static int y_write_array(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
HashTable *ht = Z_ARRVAL_P(data);
zval *elm;
int array_type;
zval key_zval;
zend_ulong kidx;
zend_string *kstr;
zend_long recursive_idx = -1;
char *anchor = { 0 };
size_t anchor_size;
array_type = y_array_is_sequence(ht TSRMLS_CC);
if (NULL == tag) {
if (Y_ARRAY_SEQUENCE == array_type) {
tag = (yaml_char_t *) YAML_SEQ_TAG;
} else {
tag = (yaml_char_t *) YAML_MAP_TAG;
}
omit_tag = 1;
}
/* syck does a check to see if the array is small and all scalars
* if it is then it outputs in inline form
*/
/* search state->recursive for this ht.
* if it exists:
* anchor = "id%04d" % index
* if ht->nApplyCount > 0:
* emit a ref
*/
recursive_idx = y_search_recursive(state, (zend_ulong) ht TSRMLS_CC);
if (-1 != recursive_idx) {
/* create anchor to refer to this structure */
anchor_size = snprintf(anchor, 0, "refid%ld", recursive_idx + 1);
anchor = (char*) emalloc(anchor_size + 1);
snprintf(anchor, anchor_size + 1, "refid%ld", recursive_idx + 1);
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE) && GC_IS_RECURSIVE(ht)) {
#else
if (ZEND_HASH_APPLY_PROTECTION(ht) && ht->u.v.nApplyCount > 0) {
#endif
/* node has been visited before */
status = yaml_alias_event_initialize(
&event, (yaml_char_t *) anchor);
if (!status) {
y_event_init_failed(&event);
efree(anchor);
return FAILURE;
}
status = y_event_emit(state, &event TSRMLS_CC);
efree(anchor);
return status;
}
}
if (Y_ARRAY_SEQUENCE == array_type) {
status = yaml_sequence_start_event_initialize(&event,
(yaml_char_t *) anchor, tag, omit_tag,
YAML_ANY_SEQUENCE_STYLE);
} else {
status = yaml_mapping_start_event_initialize(&event,
(yaml_char_t *) anchor, tag, omit_tag,
YAML_ANY_MAPPING_STYLE);
}
if (!status) {
y_event_init_failed(&event);
if (anchor) {
efree(anchor);
}
return FAILURE;
}
status = y_event_emit(state, &event TSRMLS_CC);
if (anchor) {
efree(anchor);
}
if (FAILURE == status) {
return FAILURE;
}
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
/* increment access count for hash */
GC_PROTECT_RECURSION(ht);
}
#else
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
/* increment access count for hash */
ht->u.v.nApplyCount++;
}
#endif
/* emit array elements */
ZEND_HASH_FOREACH_KEY_VAL(ht, kidx, kstr, elm) {
ZVAL_DEREF(elm);
if (Y_ARRAY_MAP == array_type) {
/* create zval for key */
if (kstr) {
ZVAL_STR(&key_zval, kstr);
} else {
ZVAL_LONG(&key_zval, kidx);
}
/* emit key */
status = y_write_zval(state, &key_zval, NULL TSRMLS_CC);
if (SUCCESS != status) {
return FAILURE;
}
}
status = y_write_zval(state, elm, NULL TSRMLS_CC);
if (SUCCESS != status) {
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(ht);
}
#else
if (ZEND_HASH_APPLY_PROTECTION(ht)) {
ht->u.v.nApplyCount--;
}
#endif
if (Y_ARRAY_SEQUENCE == array_type) {
status = yaml_sequence_end_event_initialize(&event);
} else {
status = yaml_mapping_end_event_initialize(&event);
}
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* y_write_timestamp()
*/
static int y_write_timestamp(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int omit_tag = 0;
int status;
zend_class_entry *clazz = Z_OBJCE_P(data);
zval timestamp = {{0} };
zval dtfmt;
if (NULL == tag) {
tag = (yaml_char_t *) YAML_TIMESTAMP_TAG;
omit_tag = 1;
}
/* iso-8601 format specifier including milliseconds */
ZVAL_STRING(&dtfmt, "Y-m-d\\TH:i:s.uP");
/* format date as iso-8601 string */
zend_call_method_with_1_params(data, clazz, NULL, "format", ×tamp, &dtfmt);
/* emit formatted date */
status = yaml_scalar_event_initialize(&event, NULL, tag,
(yaml_char_t *) Z_STRVAL_P(×tamp), Z_STRLEN_P(×tamp),
omit_tag, omit_tag, YAML_PLAIN_SCALAR_STYLE);
zval_ptr_dtor(×tamp);
if (!status) {
y_event_init_failed(&event);
return FAILURE;
}
return y_event_emit(state, &event TSRMLS_CC);
}
/* }}} */
/* {{{ y_write_object()
*/
static int y_write_object(
const y_emit_state_t *state, zval *data, yaml_char_t *tag TSRMLS_DC)
{
yaml_event_t event;
int status;
zend_string *clazz_name;
zval *callback;
clazz_name = Z_OBJCE_P(data)->name;
/* TODO check for a "yamlSerialize()" instance method */
if (NULL != state->callbacks && (callback = zend_hash_find(
state->callbacks, clazz_name)) != NULL) {
/* found a registered callback for this class */
status = y_write_object_callback(
state, callback, data, clazz_name->val TSRMLS_CC);
} else if (0 == strncmp(clazz_name->val, "DateTime", clazz_name->len)) {
status = y_write_timestamp(state, data, tag TSRMLS_CC);
} else {
/* tag and emit serialized version of object */
php_serialize_data_t var_hash;
smart_str buf = { 0 };
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(&buf, data, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
status = yaml_scalar_event_initialize(&event,
NULL, (yaml_char_t *) YAML_PHP_TAG, (yaml_char_t *) buf.s->val, buf.s->len,
0, 0, YAML_DOUBLE_QUOTED_SCALAR_STYLE);
if (!status) {
y_event_init_failed(&event);
status = FAILURE;
} else {
status = y_event_emit(state, &event TSRMLS_CC);
}
smart_str_free(&buf);
}
return status;
}
/* }}} */
/* {{{ y_write_object_callback()
*/
static int
y_write_object_callback (
const y_emit_state_t *state, zval *callback, zval *data,
const char *clazz_name TSRMLS_DC) {
zval argv[1];
argv[0] = *data;
zval zret;
zval *ztag;
zval *zdata;
zend_string *str_key;
/* call the user function */
if (FAILURE == call_user_function_ex(EG(function_table), NULL,
callback, &zret, 1, argv, 0, NULL TSRMLS_CC) ||
Z_TYPE_P(&zret) == IS_UNDEF) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Failed to apply callback for class '%s'"
" with user defined function", clazz_name);
return FAILURE;
}
/* return val should be array */
if (IS_ARRAY != Z_TYPE_P(&zret)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Expected callback for class '%s'"
" to return an array", clazz_name);
return FAILURE;
}
/* pull out the tag and surrogate object */
str_key = zend_string_init("tag", sizeof("tag") - 1, 0);
if ((ztag = zend_hash_find(Z_ARRVAL_P(&zret), str_key)) == NULL || Z_TYPE_P(ztag) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Expected callback result for class '%s'"
" to contain a key named 'tag' with a string value",
clazz_name);
zend_string_release(str_key);
return FAILURE;
}
zend_string_release(str_key);
str_key = zend_string_init("data", sizeof("data") - 1, 0);
if ((zdata = zend_hash_find(Z_ARRVAL_P(&zret), str_key)) == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Expected callback result for class '%s'"
" to contain a key named 'data'",
clazz_name);
zend_string_release(str_key);
return FAILURE;
}
zend_string_release(str_key);
/* emit surrogate object and tag */
return y_write_zval(
state, zdata, (yaml_char_t *) Z_STRVAL_P(ztag) TSRMLS_CC);
}
/* }}} */
/* {{{ php_yaml_write_impl()
* Common stream writing logic shared by yaml_emit and yaml_emit_file.
*/
int
php_yaml_write_impl(
yaml_emitter_t *emitter, zval *data,
yaml_encoding_t encoding, HashTable *callbacks TSRMLS_DC)
{
y_emit_state_t state;
yaml_event_t event;
int status;
state.emitter = emitter;
/* scan for recursive objects */
ALLOC_HASHTABLE(state.recursive);
zend_hash_init(state.recursive, 8, NULL, NULL, 0);
y_scan_recursion(&state, data TSRMLS_CC);
state.callbacks = callbacks;
/* start stream */
status = yaml_stream_start_event_initialize(&event, encoding);
if (!status) {
y_event_init_failed(&event);
status = FAILURE;
goto cleanup;
}
if (FAILURE == y_event_emit(&state, &event TSRMLS_CC)) {
status = FAILURE;
goto cleanup;
}
/* start document */
status = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
if (!status) {
y_event_init_failed(&event);
status = FAILURE;
goto cleanup;
}
if (FAILURE == y_event_emit(&state, &event TSRMLS_CC)) {
status = FAILURE;
goto cleanup;
}
/* output data */
if (FAILURE == y_write_zval(&state, data, NULL TSRMLS_CC)) {
status = FAILURE;
goto cleanup;
}
/* end document */
status = yaml_document_end_event_initialize(&event, 0);
if (!status) {
y_event_init_failed(&event);
status = FAILURE;
goto cleanup;
}
if (FAILURE == y_event_emit(&state, &event TSRMLS_CC)) {
status = FAILURE;
goto cleanup;
}
/* end stream */
status = yaml_stream_end_event_initialize(&event);
if (!status) {
y_event_init_failed(&event);
status = FAILURE;
goto cleanup;
}
if (FAILURE == y_event_emit(&state, &event TSRMLS_CC)) {
status = FAILURE;
goto cleanup;
}
yaml_emitter_flush(state.emitter);
status = SUCCESS;
cleanup:
zend_hash_destroy(state.recursive);
FREE_HASHTABLE(state.recursive);
return status;
}
/* }}} */
/* {{{ php_yaml_write_to_buffer()
* Emitter string buffer
*/
int
php_yaml_write_to_buffer(void *data, unsigned char *buffer, size_t size)
{
smart_string_appendl((smart_string *) data, (char *) buffer, size);
return 1;
}
/* }}} */
/* {{{ get_next_char
* Copied from ext/standard/html.c @ a37ff1fa4bb149dc81fc812d03cdf7685c499403
* and trimmed to include only utf8 code branch. I would have liked to use
* php_next_utf8_char() but it isn't available until php 5.4.
*
* Thank you cataphract@php.net!
*/
#define CHECK_LEN(pos, chars_need) ((str_len - (pos)) >= (chars_need))
#define MB_FAILURE(pos, advance) do { \
*cursor = pos + (advance); \
*status = FAILURE; \
return 0; \
} while (0)
#define utf8_lead(c) ((c) < 0x80 || ((c) >= 0xC2 && (c) <= 0xF4))
#define utf8_trail(c) ((c) >= 0x80 && (c) <= 0xBF)
static inline unsigned int get_next_char(
const unsigned char *str,
size_t str_len,
size_t *cursor,
int *status)
{
size_t pos = *cursor;
unsigned int this_char = 0;
unsigned char c;
*status = SUCCESS;
assert(pos <= str_len);
if (!CHECK_LEN(pos, 1))
MB_FAILURE(pos, 1);
/* We'll follow strategy 2. from section 3.6.1 of UTR #36:
* "In a reported illegal byte sequence, do not include any
* non-initial byte that encodes a valid character or is a leading
* byte for a valid sequence." */
c = str[pos];
if (c < 0x80) {
this_char = c;
pos++;
} else if (c < 0xc2) {
MB_FAILURE(pos, 1);
} else if (c < 0xe0) {
if (!CHECK_LEN(pos, 2))
MB_FAILURE(pos, 1);
if (!utf8_trail(str[pos + 1])) {
MB_FAILURE(pos, utf8_lead(str[pos + 1]) ? 1 : 2);
}
this_char = ((c & 0x1f) << 6) | (str[pos + 1] & 0x3f);
if (this_char < 0x80) { /* non-shortest form */
MB_FAILURE(pos, 2);
}
pos += 2;
} else if (c < 0xf0) {
size_t avail = str_len - pos;
if (avail < 3 ||
!utf8_trail(str[pos + 1]) || !utf8_trail(str[pos + 2])) {
if (avail < 2 || utf8_lead(str[pos + 1]))
MB_FAILURE(pos, 1);
else if (avail < 3 || utf8_lead(str[pos + 2]))
MB_FAILURE(pos, 2);
else
MB_FAILURE(pos, 3);
}
this_char = ((c & 0x0f) << 12) | ((str[pos + 1] & 0x3f) << 6) |
(str[pos + 2] & 0x3f);
if (this_char < 0x800) {
/* non-shortest form */
MB_FAILURE(pos, 3);
} else if (this_char >= 0xd800 && this_char <= 0xdfff) {
/* surrogate */
MB_FAILURE(pos, 3);
}
pos += 3;
} else if (c < 0xf5) {
size_t avail = str_len - pos;
if (avail < 4 ||
!utf8_trail(str[pos + 1]) || !utf8_trail(str[pos + 2]) ||
!utf8_trail(str[pos + 3])) {
if (avail < 2 || utf8_lead(str[pos + 1]))
MB_FAILURE(pos, 1);
else if (avail < 3 || utf8_lead(str[pos + 2]))
MB_FAILURE(pos, 2);
else if (avail < 4 || utf8_lead(str[pos + 3]))
MB_FAILURE(pos, 3);
else
MB_FAILURE(pos, 4);
}
this_char = ((c & 0x07) << 18) | ((str[pos + 1] & 0x3f) << 12) |
((str[pos + 2] & 0x3f) << 6) | (str[pos + 3] & 0x3f);
if (this_char < 0x10000 || this_char > 0x10FFFF) {
/* non-shortest form or outside range */
MB_FAILURE(pos, 4);
}
pos += 4;
} else {
MB_FAILURE(pos, 1);
}
*cursor = pos;
return this_char;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|