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
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: Andrey Zmievski <andrey@ispi.net> |
+----------------------------------------------------------------------+
*/
/* $Id: wddx_a.c,v 1.14 2000/05/25 12:20:29 andrei Exp $ */
#include "php.h"
#include "internal_functions.h"
#include "php3_string.h"
#include "php3_list.h"
#include "php3_wddx_a.h"
#if HAVE_LIBEXPAT
#include "DList.h"
#define WDDX_PACKET_S "<wddxPacket version='0.9'>"
#define WDDX_PACKET_E "</wddxPacket>"
#define WDDX_HEADER "<header/>"
#define WDDX_HEADER_COMMENT "<header comment='%s'/>"
#define WDDX_DATA_S "<data>"
#define WDDX_DATA_E "</data>"
#define WDDX_STRING_S "<string>"
#define WDDX_STRING_E "</string>"
#define WDDX_CHAR "<char code='%02X'/>"
#define WDDX_NUMBER "<number>%s</number>"
#define WDDX_ARRAY_S "<array length='%d'>"
#define WDDX_ARRAY_E "</array>"
#define WDDX_VAR_S "<var name='%s'>"
#define WDDX_VAR_E "</var>"
#define WDDX_STRUCT_S "<struct>"
#define WDDX_STRUCT_E "</struct>"
#define WDDX_BUF_LEN 256
#define EL_STRING "string"
#define EL_CHAR "char"
#define EL_CHAR_CODE "code"
#define EL_NUMBER "number"
#define EL_ARRAY "array"
#define EL_STRUCT "struct"
#define EL_VAR "var"
#define EL_VAR_NAME "name"
#define EL_PACKET "wddxPacket"
#define EL_VERSION "version"
static int le_wddx;
typedef struct {
DLIST *packet_head;
int packet_length;
} wddx_packet;
typedef struct {
pval *data;
enum {
ST_STRING,
ST_NUMBER,
ST_ARRAY,
ST_STRUCT
} type;
char *varname;
} st_entry;
typedef struct {
int top, max;
char *varname;
void **elements;
} wddx_stack;
/* {{{ function prototypes */
static void _php3_wddx_serialize_var(wddx_packet *packet, pval *var, char *name);
static void _php3_wddx_process_data(void *user_data, const char *s, int len);
/* }}} */
/* {{{ module definition structures */
function_entry wddx_functions_a[] = {
PHP_FE(wddx_serialize_value, NULL)
PHP_FE(wddx_serialize_vars, NULL)
PHP_FE(wddx_packet_start, NULL)
PHP_FE(wddx_packet_end, NULL)
PHP_FE(wddx_add_vars, NULL)
PHP_FE(wddx_deserialize, NULL)
{NULL, NULL, NULL}
};
php3_module_entry wddx_module_entry_a = {
"WDDX_A", wddx_functions_a, php3_minit_wddx, NULL, NULL, NULL, NULL, STANDARD_MODULE_PROPERTIES
};
/* }}} */
/* {{{ int wddx_stack_init(wddx_stack *stack) */
static int wddx_stack_init(wddx_stack *stack)
{
stack->top = 0;
stack->elements = (void **) emalloc(sizeof(void **) * STACK_BLOCK_SIZE);
if (!stack->elements) {
return FAILURE;
} else {
stack->max = STACK_BLOCK_SIZE;
stack->varname = NULL;
return SUCCESS;
}
}
/* }}} */
/* {{{ int wddx_stack_push(wddx_stack *stack, void *element, int size) */
static int wddx_stack_push(wddx_stack *stack, void *element, int size)
{
if (stack->top >= stack->max) { /* we need to allocate more memory */
stack->elements = (void **) erealloc(stack->elements,
(sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)));
if (!stack->elements) {
return FAILURE;
}
}
stack->elements[stack->top] = (void *) emalloc(size);
memcpy(stack->elements[stack->top], element, size);
return stack->top++;
}
/* }}} */
/* {{{ int wddx_stack_top(wddx_stack *stack, void **element) */
static int wddx_stack_top(wddx_stack *stack, void **element)
{
if (stack->top > 0) {
*element = stack->elements[stack->top - 1];
return SUCCESS;
} else {
*element = NULL;
return FAILURE;
}
}
/* }}} */
/* {{{ int wddx_stack_is_empty(wddx_stack *stack) */
static int wddx_stack_is_empty(wddx_stack *stack)
{
if (stack->top == 0) {
return 1;
} else {
return 0;
}
}
/* }}} */
/* {{{ int wddx_stack_destroy(wddx_stack *stack) */
static int wddx_stack_destroy(wddx_stack *stack)
{
register int i;
if (stack->elements) {
for (i = 0; i < stack->top; i++) {
if (((st_entry *)stack->elements[i])->data)
{
pval_destructor(((st_entry *)stack->elements[i])->data);
efree(((st_entry *)stack->elements[i])->data);
}
efree(stack->elements[i]);
}
efree(stack->elements);
}
return SUCCESS;
}
/* }}} */
/* {{{ _php3_free_packet_chunk */
static void _php3_free_packet_chunk(char **chunk_ptr)
{
if ((*chunk_ptr))
efree((*chunk_ptr));
}
/* }}} */
/* {{{ _php3_wddx_destructor */
static void _php3_wddx_destructor(wddx_packet *packet)
{
dlst_kill(packet->packet_head, (void (*)(void *))_php3_free_packet_chunk);
efree(packet);
}
/* }}} */
/* {{{ php3_minit_wddx */
int php3_minit_wddx(INIT_FUNC_ARGS)
{
le_wddx = register_list_destructors(_php3_wddx_destructor, NULL);
return SUCCESS;
}
/* }}} */
/* {{{ _php3_wddx_add_chunk */
static void _php3_wddx_add_chunk(wddx_packet *packet, char *str)
{
char **chunk_ptr;
chunk_ptr = (char**)dlst_newnode(sizeof(char *));
(*chunk_ptr) = estrdup(str);
dlst_insertafter(packet->packet_head, chunk_ptr, PHP_DLST_TAIL(packet->packet_head));
packet->packet_length += strlen(str);
}
/* }}} */
/* {{{ _php3_wddx_gather */
static char* _php3_wddx_gather(wddx_packet *packet)
{
char **chunk;
char *buf;
buf = (char *)emalloc(packet->packet_length+1);
buf[0] = '\0';
for(chunk=dlst_first(packet->packet_head);
chunk!=NULL;
chunk = dlst_next(chunk)) {
strcat(buf, *chunk);
}
return buf;
}
/* }}} */
/* {{{ void _php3_wddx_packet_start */
static void _php3_wddx_packet_start(wddx_packet *packet, char *comment)
{
char tmp_buf[WDDX_BUF_LEN];
_php3_wddx_add_chunk(packet, WDDX_PACKET_S);
if (comment)
{
sprintf(tmp_buf, WDDX_HEADER_COMMENT, comment);
_php3_wddx_add_chunk(packet, tmp_buf);
}
else
_php3_wddx_add_chunk(packet, WDDX_HEADER);
_php3_wddx_add_chunk(packet, WDDX_DATA_S);
}
/* }}} */
/* {{{ int _php3_wddx_packet_end */
static void _php3_wddx_packet_end(wddx_packet *packet)
{
_php3_wddx_add_chunk(packet, WDDX_DATA_E);
_php3_wddx_add_chunk(packet, WDDX_PACKET_E);
}
/* }}} */
/* {{{ void _php3_wddx_serialize_var(wddx_packet *packet, pval *var) */
static void _php3_wddx_serialize_string(wddx_packet *packet, pval *var)
{
char *buf,
*c,
control_buf[WDDX_BUF_LEN];
int i;
_php3_wddx_add_chunk(packet, WDDX_STRING_S);
i = 0;
buf = (char *)emalloc(var->value.str.len + 1);
for(c=var->value.str.val; *c!='\0'; c++)
{
if (iscntrl((int)*c))
{
if (*buf)
{
buf[i] = '\0';
_php3_wddx_add_chunk(packet, buf);
i = 0;
buf[i] = '\0';
}
sprintf(control_buf, WDDX_CHAR, *c);
_php3_wddx_add_chunk(packet, control_buf);
}
else
buf[i++] = *c;
}
buf[i] = '\0';
if (*buf)
_php3_wddx_add_chunk(packet, buf);
efree(buf);
_php3_wddx_add_chunk(packet, WDDX_STRING_E);
}
/* }}} */
/* {{{ void _php3_wddx_serialize_number(wddx_packet *packet, pval *var) */
static void _php3_wddx_serialize_number(wddx_packet *packet, pval *var)
{
char tmp_buf[WDDX_BUF_LEN];
convert_to_string(var);
sprintf(tmp_buf, WDDX_NUMBER, var->value.str.val);
_php3_wddx_add_chunk(packet, tmp_buf);
}
/* }}} */
/* {{{ void _php3_wddx_serialize_hash(wddx_packet *packet, pval *var) */
static void _php3_wddx_serialize_hash(wddx_packet *packet, pval *var)
{
pval *ent;
char *key;
int hash_type, ent_type;
ulong idx;
char tmp_buf[WDDX_BUF_LEN];
_php3_hash_internal_pointer_reset(var->value.ht);
while(_php3_hash_get_current_data(var->value.ht, (void**)&ent) == SUCCESS) {
if (ent->type == IS_STRING &&
ent->value.str.val == undefined_variable_string)
_php3_hash_move_forward(var->value.ht);
else
break;
}
hash_type = _php3_hash_get_current_key(var->value.ht, &key, &idx);
if (hash_type == HASH_KEY_IS_STRING) {
_php3_wddx_add_chunk(packet, WDDX_STRUCT_S);
efree(key);
} else {
sprintf(tmp_buf, WDDX_ARRAY_S, _php3_hash_num_elements(var->value.ht));
_php3_wddx_add_chunk(packet, tmp_buf);
}
while(_php3_hash_get_current_data(var->value.ht, (void**)&ent) == SUCCESS) {
if (!(ent->type == IS_STRING &&
ent->value.str.val == undefined_variable_string)) {
if (hash_type == HASH_KEY_IS_STRING) {
ent_type = _php3_hash_get_current_key(var->value.ht, &key, &idx);
if (ent_type == HASH_KEY_IS_STRING) {
_php3_wddx_serialize_var(packet, ent, key);
efree(key);
} else {
sprintf(tmp_buf, "%ld", idx);
_php3_wddx_serialize_var(packet, ent, tmp_buf);
}
} else
_php3_wddx_serialize_var(packet, ent, NULL);
}
_php3_hash_move_forward(var->value.ht);
}
if (hash_type == HASH_KEY_IS_STRING)
_php3_wddx_add_chunk(packet, WDDX_STRUCT_E);
else
_php3_wddx_add_chunk(packet, WDDX_ARRAY_E);
}
/* }}} */
/* {{{ void _php3_wddx_serialize_var(wddx_packet *packet, pval *var, char *name) */
static void _php3_wddx_serialize_var(wddx_packet *packet, pval *var, char *name)
{
char tmp_buf[WDDX_BUF_LEN];
if (name) {
sprintf(tmp_buf, WDDX_VAR_S, name);
_php3_wddx_add_chunk(packet, tmp_buf);
}
switch(var->type) {
case IS_STRING:
_php3_wddx_serialize_string(packet, var);
break;
case IS_LONG:
case IS_DOUBLE:
_php3_wddx_serialize_number(packet, var);
break;
case IS_ARRAY:
case IS_OBJECT:
_php3_wddx_serialize_hash(packet, var);
break;
}
if (name) {
_php3_wddx_add_chunk(packet, WDDX_VAR_E);
}
}
/* }}} */
/* {{{ void _php3_wddx_add_var(wddx_packet *packet, pval *name_var) */
static void _php3_wddx_add_var(wddx_packet *packet, pval *name_var)
{
pval *val;
if (name_var->type & IS_STRING)
{
if (_php3_hash_find(GLOBAL(active_symbol_table), name_var->value.str.val,
name_var->value.str.len+1, (void**)&val) != FAILURE) {
_php3_wddx_serialize_var(packet, val, name_var->value.str.val);
}
}
else if (name_var->type & IS_ARRAY)
{
_php3_hash_internal_pointer_reset(name_var->value.ht);
while(_php3_hash_get_current_data(name_var->value.ht, (void**)&val) == SUCCESS) {
if (!(val->type == IS_STRING &&
val->value.str.val == undefined_variable_string))
_php3_wddx_add_var(packet, val);
_php3_hash_move_forward(name_var->value.ht);
}
}
}
/* }}} */
/* {{{ void _php3_wddx_push_element(void *user_data, const char *name, const char **atts) */
static void _php3_wddx_push_element(void *user_data, const char *name, const char **atts)
{
st_entry ent;
wddx_stack *stack = (wddx_stack *)user_data;
if (!strcmp(name, EL_PACKET)) {
int i;
for (i=0; atts[i]; i++) {
if (!strcmp(atts[i], EL_VERSION)) {
}
}
} else if (!strcmp(name, EL_STRING)) {
ent.type = ST_STRING;
if (stack->varname) {
ent.varname = estrdup(stack->varname);
efree(stack->varname);
stack->varname = NULL;
} else
ent.varname = NULL;
ent.data = (pval *)emalloc(sizeof(pval));
ent.data->value.str.val = NULL;
ent.data->value.str.len = 0;
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp(name, EL_CHAR)) {
int i;
char tmp_buf[2];
for (i=0; atts[i]; i++) {
if (!strcmp(atts[i], EL_CHAR_CODE) && atts[i+1]) {
sprintf(tmp_buf, "%c", (char)strtol(atts[i+1], NULL, 16));
_php3_wddx_process_data(user_data, tmp_buf, strlen(tmp_buf));
}
}
} else if (!strcmp(name, EL_NUMBER)) {
ent.type = ST_NUMBER;
if (stack->varname) {
ent.varname = estrdup(stack->varname);
efree(stack->varname);
stack->varname = NULL;
} else
ent.varname = NULL;
ent.data = (pval *)emalloc(sizeof(pval));
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp(name, EL_ARRAY)) {
ent.type = ST_ARRAY;
if (stack->varname) {
ent.varname = estrdup(stack->varname);
efree(stack->varname);
stack->varname = NULL;
} else
ent.varname = NULL;
ent.data = (pval *)emalloc(sizeof(pval));
array_init(ent.data);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp(name, EL_STRUCT)) {
ent.type = ST_STRUCT;
if (stack->varname) {
ent.varname = estrdup(stack->varname);
efree(stack->varname);
stack->varname = NULL;
} else
ent.varname = NULL;
ent.data = (pval *)emalloc(sizeof(pval));
array_init(ent.data);
wddx_stack_push((wddx_stack *)stack, &ent, sizeof(st_entry));
} else if (!strcmp(name, EL_VAR)) {
int i;
for (i=0; atts[i]; i++) {
if (!strcmp(atts[i], EL_VAR_NAME) && atts[i+1]) {
stack->varname = estrdup(atts[i+1]);
}
}
}
}
/* }}} */
/* {{{ void _php3_wddx_pop_element(void *user_data, const char *name) */
static void _php3_wddx_pop_element(void *user_data, const char *name)
{
st_entry *ent1, *ent2;
wddx_stack *stack = (wddx_stack *)user_data;
if (!strcmp(name, EL_STRING) || !strcmp(name, EL_NUMBER) ||
!strcmp(name, EL_ARRAY) || !strcmp(name, EL_STRUCT)) {
if (stack->top > 1) {
wddx_stack_top(stack, (void**)&ent1);
stack->top--;
wddx_stack_top(stack, (void**)&ent2);
if (ent2->data->type == IS_ARRAY) {
if (ent1->varname) {
_php3_hash_update(ent2->data->value.ht, ent1->varname, strlen(ent1->varname)+1,
ent1->data, sizeof(pval), NULL);
efree(ent1->varname);
} else
_php3_hash_next_index_insert(ent2->data->value.ht, ent1->data,
sizeof(pval), NULL);
}
efree(ent1->data);
efree(ent1);
}
}
else if (!strcmp(name, EL_VAR) && stack->varname)
efree(stack->varname);
}
/* }}} */
/* {{{ void _php3_wddx_process_data(void *user_data, const char *s, int len) */
static void _php3_wddx_process_data(void *user_data, const char *s, int len)
{
st_entry *ent;
wddx_stack *stack = (wddx_stack *)user_data;
if (!wddx_stack_is_empty(stack)) {
wddx_stack_top(stack, (void**)&ent);
switch (ent->type) {
case ST_STRING:
ent->data->type = IS_STRING;
if (ent->data->value.str.len == 0) {
ent->data->value.str.val = estrndup(s, len);
ent->data->value.str.len = len;
} else {
ent->data->value.str.val = erealloc(ent->data->value.str.val,
ent->data->value.str.len + len + 1);
strncpy(ent->data->value.str.val+ent->data->value.str.len, s, len);
ent->data->value.str.len += len;
ent->data->value.str.val[ent->data->value.str.len] = '\0';
}
break;
case ST_NUMBER:
ent->data->type = IS_STRING;
ent->data->value.str.len = len;
ent->data->value.str.val = estrndup(s, len);
convert_string_to_number(ent->data);
break;
default:
break;
}
}
}
/* }}} */
/* {{{ void _php3_wddx_deserialize(pval *packet, pval *return_value) */
static void _php3_wddx_deserialize(pval *packet, pval *return_value)
{
wddx_stack stack;
XML_Parser parser;
st_entry *ent;
wddx_stack_init(&stack);
parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, &stack);
XML_SetElementHandler(parser, _php3_wddx_push_element, _php3_wddx_pop_element);
XML_SetCharacterDataHandler(parser, _php3_wddx_process_data);
XML_Parse(parser, packet->value.str.val, packet->value.str.len, 1);
XML_ParserFree(parser);
if (!wddx_stack_is_empty(&stack)) {
wddx_stack_top(&stack, (void**)&ent);
*return_value = *(ent->data);
pval_copy_constructor(return_value);
}
wddx_stack_destroy(&stack);
}
/* }}} */
/* {{{ proto string wddx_serialize_value(mixed var [, string comment])
Creates a new packet and serializes the given value */
PHP_FUNCTION(wddx_serialize_value)
{
int argc;
pval *var,
*comment;
wddx_packet *packet;
char *buf;
argc = ARG_COUNT(ht);
if(argc < 1 || argc > 2 || getParameters(ht, argc, &var, &comment) == FAILURE) {
WRONG_PARAM_COUNT;
}
packet = emalloc(sizeof(wddx_packet));
if (!packet) {
php3_error(E_WARNING, "Unable to allocate memory in php3_wddx_packet_start");
RETURN_FALSE;
}
packet->packet_head = dlst_init();
packet->packet_length = 0;
if (argc == 2)
{
convert_to_string(comment);
_php3_wddx_packet_start(packet, comment->value.str.val);
}
else
_php3_wddx_packet_start(packet, NULL);
_php3_wddx_serialize_var(packet, var, NULL);
_php3_wddx_packet_end(packet);
buf = _php3_wddx_gather(packet);
_php3_wddx_destructor(packet);
RETURN_STRING(buf, 0);
}
/* }}} */
/* {{{ proto string wddx_serialize_vars(mixed var_name [, mixed ...])
Creates a new packet and serializes given variables into a struct */
PHP_FUNCTION(wddx_serialize_vars)
{
int argc, i;
wddx_packet *packet;
pval *name_var;
char *buf;
argc = ARG_COUNT(ht);
packet = emalloc(sizeof(wddx_packet));
if (!packet) {
php3_error(E_WARNING, "Unable to allocate memory in php3_wddx_packet_start");
RETURN_FALSE;
}
packet->packet_head = dlst_init();
packet->packet_length = 0;
_php3_wddx_packet_start(packet, NULL);
_php3_wddx_add_chunk(packet, WDDX_STRUCT_S);
for (i=0; i<argc; i++) {
if (_php3_hash_index_find(ht, i, (void**)&name_var) == FAILURE) {
php3_error(E_WARNING, "error in wddx_serialize_vars()");
RETURN_FALSE;
}
_php3_wddx_add_var(packet, name_var);
}
_php3_wddx_add_chunk(packet, WDDX_STRUCT_E);
_php3_wddx_packet_end(packet);
buf = _php3_wddx_gather(packet);
_php3_wddx_destructor(packet);
RETURN_STRING(buf, 0);
}
/* }}} */
/* {{{ proto int wddx_packet_start([string comment])
Starts a WDDX packet with optional comment and returns the packet id */
PHP_FUNCTION(wddx_packet_start)
{
int argc;
pval *comment;
wddx_packet *packet;
comment = NULL;
argc = ARG_COUNT(ht);
if (argc>1 || (argc==1 && getParameters(ht, 1, &comment)==FAILURE)) {
WRONG_PARAM_COUNT;
}
packet = emalloc(sizeof(wddx_packet));
if (!packet) {
php3_error(E_WARNING, "Unable to allocate memory in php3_wddx_packet_start");
RETURN_FALSE;
}
packet->packet_head = dlst_init();
packet->packet_length = 0;
if (argc == 1) {
convert_to_string(comment);
_php3_wddx_packet_start(packet, comment->value.str.val);
}
else
_php3_wddx_packet_start(packet, NULL);
_php3_wddx_add_chunk(packet, WDDX_STRUCT_S);
RETURN_LONG(php3_list_insert(packet, le_wddx));
}
/* }}} */
/* {{{ proto string wddx_packet_end(int packet_id)
Ends specified WDDX packet and returns the string containing the packet */
PHP_FUNCTION(wddx_packet_end)
{
pval *packet_id;
char *buf;
wddx_packet *packet;
int type, id;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &packet_id)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(packet_id);
id = packet_id->value.lval;
packet = php3_list_find(id, &type);
if (type!=le_wddx) {
php3_error(E_WARNING, "%d is not a valid WDDX packet id", id);
RETURN_FALSE;
}
_php3_wddx_add_chunk(packet, WDDX_STRUCT_E);
_php3_wddx_packet_end(packet);
buf = _php3_wddx_gather(packet);
php3_list_delete(id);
RETURN_STRING(buf, 0);
}
/* }}} */
/* {{{ proto int wddx_add_vars(int packet_id, mixed name_var [, mixed ...])
Serializes given variables and adds them to packet given by packet_id */
PHP_FUNCTION(wddx_add_vars)
{
int argc, type, id, i;
pval *name_var;
pval *packet_id;
wddx_packet *packet;
argc = ARG_COUNT(ht);
if (argc < 2 || getParameters(ht, 1, &packet_id)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(packet_id);
id = packet_id->value.lval;
packet = php3_list_find(id, &type);
if (type!=le_wddx) {
php3_error(E_WARNING, "%d is not a valid WDDX packet id", id);
RETURN_FALSE;
}
for (i=1; i<argc; i++) {
if (_php3_hash_index_find(ht, i, (void**)&name_var) == FAILURE) {
php3_error(E_WARNING, "error in wddx_add_vars()");
RETURN_FALSE;
}
_php3_wddx_add_var(packet, name_var);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto mixed wddx_deserialized(string packet)
Deserializes given packet and returns a PHP value */
PHP_FUNCTION(wddx_deserialize)
{
pval *packet;
if (ARG_COUNT(ht)!=1 || getParameters(ht, 1, &packet) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(packet);
if (packet->value.str.len == 0)
return;
_php3_wddx_deserialize(packet, return_value);
}
/* }}} */
#endif /* HAVE_LIBEXPAT */
|