Menu

[r1953]: / advanced / src / xmlrpc_decompose.c  Maximize  Restore  History

Download this file

1151 lines (903 with data), 36.2 kB

   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
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
/* By Bryan Henderson July 2006.
Contributed to the public domain.
*/
#include "xmlrpc_config.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "bool.h"
#include "c_util.h"
#include "mallocvar.h"
#include "stdargx.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
/* THE DECOMPOSITION TREE
We execute xmlrpc_decompose_value() in two steps:
1) Create a "decomposition tree" that tells how Caller wants the XML-RPC
value decomposed.
2) Using that tree, decompose the value. I.e. store stuff in the variables
in which Caller wants it stored.
The decomposition tree is composed of information from the format
string and the variable arguments that the format string describes.
Nothing in the tree is derived from the actual XML-RPC value being
decomposed, and the tree may in fact be invalid for the particular
XML-RPC value it's meant for.
If the XML-RPC value is a simple value such as an integer, the
decomposition tree is trivial -- it's a single node that says
"store the value of an integer via pointer P".
Where it gets interesting is where the XML-RPC value to be decomposed
is a complex value (array or struct). Then, the root node of the tree
says, e.g., "decompose a 5-item array according to the following
5 decomposition trees" and it points to 5 additional nodes. Each of
those nodes is the root of another decomposition tree (which can also
be called a branch in this context). Each of those branches tells
how to decompose one of the items of the array.
Roots, interior nodes, and leaves are all essentially the same data
type.
*/
struct integerDecomp {
xmlrpc_int32 * valueP;
};
struct boolDecomp {
xmlrpc_bool * valueP;
};
struct doubleDecomp {
double * valueP;
};
struct datetimeTDecomp {
time_t * valueP;
};
struct datetime8Decomp {
const char ** valueP;
};
struct stringDecomp {
const char ** valueP;
size_t * sizeP;
/* NULL means don't store a size */
};
struct wideStringDecomp {
#if HAVE_UNICODE_WCHAR
const wchar_t ** valueP;
#endif
size_t * sizeP;
/* NULL means don't store a size */
};
struct bitStringDecomp {
const unsigned char ** valueP;
size_t * sizeP;
};
struct cptrDecomp {
void ** valueP;
};
struct i8Decomp {
xmlrpc_int64 * valueP;
};
struct valueDecomp {
xmlrpc_value ** valueP;
};
struct arrayValDecomp {
xmlrpc_value ** valueP;
};
struct structValDecomp {
xmlrpc_value ** valueP;
};
struct arrayDecomp {
unsigned int itemCnt;
bool ignoreExcess;
/* If there are more that 'itemCnt' items in the array, just
extract the first 'itemCnt' and ignore the rest, rather than
fail the decomposition.
*/
struct decompTreeNode * itemArray[16];
/* Only first 'itemCnt' elements of this array are defined */
};
struct mbrDecomp {
const char * key;
/* The key for the member whose value client wants to extract */
struct decompTreeNode * decompTreeP;
/* Instructions on how to decompose (extract) member's value */
};
struct structDecomp {
unsigned int mbrCnt;
struct mbrDecomp mbrArray[16];
};
struct decompTreeNode {
char formatSpecChar;
/* e.g. 'i', 'b', '8', 'A'. '(' means array; '{' means struct */
union {
/*------------------------------------------------------------------------
'formatSpecChar' selects among these members.
-------------------------------------------------------------------------*/
struct integerDecomp Tinteger;
struct boolDecomp Tbool;
struct doubleDecomp Tdouble;
struct datetimeTDecomp TdatetimeT;
struct datetime8Decomp Tdatetime8;
struct stringDecomp Tstring;
struct wideStringDecomp TwideString;
struct bitStringDecomp TbitString;
struct cptrDecomp Tcptr;
struct i8Decomp Ti8;
struct valueDecomp Tvalue;
struct arrayValDecomp TarrayVal;
struct structValDecomp TstructVal;
struct arrayDecomp Tarray;
struct structDecomp Tstruct;
} store;
};
/* prototype for recursive calls */
static void
releaseDecomposition(const struct decompTreeNode * const decompRootP,
bool const oldstyleMemMgmt);
static void
releaseDecompArray(struct arrayDecomp const arrayDecomp,
bool const oldstyleMemMgmt) {
unsigned int i;
for (i = 0; i < arrayDecomp.itemCnt; ++i) {
releaseDecomposition(arrayDecomp.itemArray[i], oldstyleMemMgmt);
}
}
static void
releaseDecompStruct(struct structDecomp const structDecomp,
bool const oldstyleMemMgmt) {
unsigned int i;
for (i = 0; i < structDecomp.mbrCnt; ++i) {
releaseDecomposition(structDecomp.mbrArray[i].decompTreeP,
oldstyleMemMgmt);
}
}
static void
releaseDecomposition(const struct decompTreeNode * const decompRootP,
bool const oldstyleMemMgmt) {
/*----------------------------------------------------------------------------
Assuming that Caller has decomposed something according to 'decompRootP',
release whatever resources the decomposed information occupies.
E.g. if it's an XML-RPC string, Caller would have allocated memory
for the C string that represents the decomposed value of XML-RPC string,
and we release that memory.
-----------------------------------------------------------------------------*/
switch (decompRootP->formatSpecChar) {
case 'i':
case 'b':
case 'd':
case 'n':
case 'I':
case 't':
case 'p':
/* Nothing was allocated; nothing to release */
break;
case '8':
xmlrpc_strfree(*decompRootP->store.Tdatetime8.valueP);
break;
case 's':
xmlrpc_strfree(*decompRootP->store.Tstring.valueP);
break;
case 'w':
free((void*)*decompRootP->store.TwideString.valueP);
break;
case '6':
free((void*)*decompRootP->store.TbitString.valueP);
break;
case 'V':
xmlrpc_DECREF(*decompRootP->store.Tvalue.valueP);
break;
case 'A':
xmlrpc_DECREF(*decompRootP->store.TarrayVal.valueP);
break;
case 'S':
xmlrpc_DECREF(*decompRootP->store.TstructVal.valueP);
break;
case '(':
releaseDecompArray(decompRootP->store.Tarray, oldstyleMemMgmt);
break;
case '{':
releaseDecompStruct(decompRootP->store.Tstruct, oldstyleMemMgmt);
break;
}
}
/* Prototype for recursive invocation: */
static void
decomposeValueWithTree(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
bool const oldstyleMemMgmt,
const struct decompTreeNode * const decompRootP);
static void
validateArraySize(xmlrpc_env * const envP,
const xmlrpc_value * const arrayP,
struct arrayDecomp const arrayDecomp) {
unsigned int size;
size = xmlrpc_array_size(envP, arrayP);
if (!envP->fault_occurred) {
if (arrayDecomp.itemCnt > size)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INDEX_ERROR,
"Format string requests %u items from array, but array "
"has only %u items.", arrayDecomp.itemCnt, size);
else if (arrayDecomp.itemCnt < size && !arrayDecomp.ignoreExcess)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_INDEX_ERROR,
"Format string requests exactly %u items from array, "
"but array has %u items. (A '*' at the end would avoid "
"this failure)", arrayDecomp.itemCnt, size);
}
}
static void
parsearray(xmlrpc_env * const envP,
const xmlrpc_value * const arrayP,
struct arrayDecomp const arrayDecomp,
bool const oldstyleMemMgmt) {
validateArraySize(envP, arrayP, arrayDecomp);
if (!envP->fault_occurred) {
unsigned int doneCnt;
doneCnt = 0;
while(doneCnt < arrayDecomp.itemCnt && !envP->fault_occurred) {
xmlrpc_value * itemP;
xmlrpc_array_read_item(envP, arrayP, doneCnt, &itemP);
if (!envP->fault_occurred) {
XMLRPC_ASSERT(doneCnt < ARRAY_SIZE(arrayDecomp.itemArray));
decomposeValueWithTree(envP, itemP, oldstyleMemMgmt,
arrayDecomp.itemArray[doneCnt]);
if (!envP->fault_occurred)
++doneCnt;
xmlrpc_DECREF(itemP);
}
}
if (envP->fault_occurred) {
/* Release the items we completed before we failed. */
unsigned int i;
for (i = 0; i < doneCnt; ++i)
releaseDecomposition(arrayDecomp.itemArray[i],
oldstyleMemMgmt);
}
}
}
static void
parsestruct(xmlrpc_env * const envP,
xmlrpc_value * const structP,
struct structDecomp const structDecomp,
bool const oldstyleMemMgmt) {
unsigned int doneCount;
doneCount = 0; /* No members done yet */
while (doneCount < structDecomp.mbrCnt && !envP->fault_occurred) {
const char * const key = structDecomp.mbrArray[doneCount].key;
xmlrpc_value * valueP;
xmlrpc_struct_read_value(envP, structP, key, &valueP);
if (!envP->fault_occurred) {
decomposeValueWithTree(
envP, valueP, oldstyleMemMgmt,
structDecomp.mbrArray[doneCount].decompTreeP);
if (!envP->fault_occurred)
++doneCount;
xmlrpc_DECREF(valueP);
}
}
if (envP->fault_occurred) {
unsigned int i;
for (i = 0; i < doneCount; ++i)
releaseDecomposition(structDecomp.mbrArray[i].decompTreeP,
oldstyleMemMgmt);
}
}
static void
readString(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt) {
xmlrpc_read_string_old(envP, valueP, stringValueP);
} else
xmlrpc_read_string(envP, valueP, stringValueP);
}
static void
readStringLp(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const char ** const stringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt) {
xmlrpc_read_string_lp_old(envP, valueP, lengthP, stringValueP);
} else
xmlrpc_read_string_lp(envP, valueP, lengthP, stringValueP);
}
#if HAVE_UNICODE_WCHAR
static void
readStringW(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const wchar_t ** const stringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt) {
xmlrpc_read_string_w_old(envP, valueP, stringValueP);
} else
xmlrpc_read_string_w(envP, valueP, stringValueP);
}
static void
readStringWLp(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
size_t * const lengthP,
const wchar_t ** const stringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt) {
xmlrpc_read_string_w_lp_old(envP, valueP, lengthP, stringValueP);
} else
xmlrpc_read_string_w_lp(envP, valueP, lengthP, stringValueP);
}
#endif
static void
readDatetime8Str(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
const char ** const stringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt)
xmlrpc_read_datetime_str_old(envP, valueP, stringValueP);
else
xmlrpc_read_datetime_str(envP, valueP, stringValueP);
}
static void
readBase64(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const unsigned char ** const byteStringValueP,
bool const oldstyleMemMgmt) {
if (oldstyleMemMgmt)
xmlrpc_read_base64_old(envP, valueP, lengthP, byteStringValueP);
else
xmlrpc_read_base64(envP, valueP, lengthP, byteStringValueP);
}
static void
decomposeValueWithTree(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
bool const oldstyleMemMgmt,
const struct decompTreeNode * const decompRootP) {
/*----------------------------------------------------------------------------
Decompose XML-RPC value *valueP, given the decomposition tree
*decompRootP. The decomposition tree tells what structure *valueP
is expected to have and where to put the various components of it
(e.g. it says "it's an array of 3 integers. Put their values at
locations x, y, and z")
-----------------------------------------------------------------------------*/
switch (decompRootP->formatSpecChar) {
case '-':
/* There's nothing to validate or return */
break;
case 'i':
xmlrpc_read_int(envP, valueP, decompRootP->store.Tinteger.valueP);
break;
case 'b':
xmlrpc_read_bool(envP, valueP, decompRootP->store.Tbool.valueP);
break;
case 'd':
xmlrpc_read_double(envP, valueP, decompRootP->store.Tdouble.valueP);
break;
case 't':
xmlrpc_read_datetime_sec(envP, valueP,
decompRootP->store.TdatetimeT.valueP);
break;
case '8':
readDatetime8Str(envP, valueP, decompRootP->store.Tdatetime8.valueP,
oldstyleMemMgmt);
break;
case 's':
if (decompRootP->store.Tstring.sizeP)
readStringLp(envP, valueP,
decompRootP->store.Tstring.sizeP,
decompRootP->store.Tstring.valueP,
oldstyleMemMgmt);
else
readString(envP, valueP, decompRootP->store.Tstring.valueP,
oldstyleMemMgmt);
break;
case 'w':
#if HAVE_UNICODE_WCHAR
if (decompRootP->store.Tstring.sizeP)
readStringWLp(envP, valueP,
decompRootP->store.TwideString.sizeP,
decompRootP->store.TwideString.valueP,
oldstyleMemMgmt);
else
readStringW(envP, valueP, decompRootP->store.TwideString.valueP,
oldstyleMemMgmt);
#else
XMLRPC_ASSERT(false);
#endif /* HAVE_UNICODE_WCHAR */
break;
case '6':
readBase64(envP, valueP,
decompRootP->store.TbitString.sizeP,
decompRootP->store.TbitString.valueP,
oldstyleMemMgmt);
break;
case 'n':
xmlrpc_read_nil(envP, valueP);
break;
case 'I':
xmlrpc_read_i8(envP, valueP, decompRootP->store.Ti8.valueP);
break;
case 'p':
xmlrpc_read_cptr(envP, valueP, decompRootP->store.Tcptr.valueP);
break;
case 'V':
*decompRootP->store.Tvalue.valueP = valueP;
if (!oldstyleMemMgmt)
xmlrpc_INCREF(valueP);
break;
case 'A':
if (xmlrpc_value_type(valueP) != XMLRPC_TYPE_ARRAY)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value to be decomposed is of type "
"%s, but the 'A' specifier requires type ARRAY",
xmlrpc_type_name(xmlrpc_value_type(valueP)));
else {
*decompRootP->store.TarrayVal.valueP = valueP;
if (!oldstyleMemMgmt)
xmlrpc_INCREF(valueP);
}
break;
case 'S':
if (xmlrpc_value_type(valueP) != XMLRPC_TYPE_STRUCT)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value to be decomposed is of type "
"%s, but the 'S' specifier requires type STRUCT.",
xmlrpc_type_name(xmlrpc_value_type(valueP)));
else {
*decompRootP->store.TstructVal.valueP = valueP;
if (!oldstyleMemMgmt)
xmlrpc_INCREF(valueP);
}
break;
case '(':
if (xmlrpc_value_type(valueP) != XMLRPC_TYPE_ARRAY)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value to be decomposed is of type "
"%s, but the '(...)' specifier requires type ARRAY",
xmlrpc_type_name(xmlrpc_value_type(valueP)));
else
parsearray(envP, valueP, decompRootP->store.Tarray,
oldstyleMemMgmt);
break;
case '{':
if (xmlrpc_value_type(valueP) != XMLRPC_TYPE_STRUCT)
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value to be decomposed is of type "
"%s, but the '{...}' specifier requires type STRUCT",
xmlrpc_type_name(xmlrpc_value_type(valueP)));
else
parsestruct(envP, valueP, decompRootP->store.Tstruct,
oldstyleMemMgmt);
break;
default:
/* Every format character that is allowed in a decomposition tree
node is handled above.
*/
XMLRPC_ASSERT(false);
}
}
/* Forward declaration for recursive calls */
static void
createDecompTreeNext(xmlrpc_env * const envP,
const char ** const formatP,
va_listx * const argsP,
struct decompTreeNode ** const decompNodePP);
static void
buildWideStringNode(xmlrpc_env * const envP ATTR_UNUSED,
const char ** const formatP,
va_listx * const argsP,
struct decompTreeNode * const decompNodeP) {
#if HAVE_UNICODE_WCHAR
decompNodeP->store.TwideString.valueP =
(const wchar_t**) va_arg(argsP->v, wchar_t**);
if (**formatP == '#') {
decompNodeP->store.TwideString.sizeP =
(size_t*) va_arg(argsP->v, size_t**);
(*formatP)++;
} else
decompNodeP->store.TwideString.sizeP = NULL;
#else
xmlrpc_faultf(envP,
"This XML-RPC For C/C++ library was built without Unicode "
"wide character capability. 'w' isn't available.");
#endif /* HAVE_UNICODE_WCHAR */
}
static void
destroyDecompTree(struct decompTreeNode * const decompRootP) {
switch (decompRootP->formatSpecChar) {
case '(': {
unsigned int i;
for (i = 0; i < decompRootP->store.Tarray.itemCnt; ++i)
destroyDecompTree(decompRootP->store.Tarray.itemArray[i]);
} break;
case '{': {
unsigned int i;
for (i = 0; i < decompRootP->store.Tstruct.mbrCnt; ++i)
destroyDecompTree(
decompRootP->store.Tstruct.mbrArray[i].decompTreeP);
} break;
}
free(decompRootP);
}
static void
processArraySpecTail(xmlrpc_env * const envP,
const char ** const formatP,
bool * const hasTrailingAsteriskP,
char const delim) {
if (**formatP == '*') {
*hasTrailingAsteriskP = true;
++*formatP;
if (!**formatP)
xmlrpc_faultf(envP, "missing closing delimiter ('%c')", delim);
else if (**formatP != delim)
xmlrpc_faultf(envP, "character following '*' in array "
"specification should be the closing delimiter "
"'%c', but is '%c'", delim, **formatP);
} else {
*hasTrailingAsteriskP = false;
if (!**formatP)
xmlrpc_faultf(envP, "missing closing delimiter ('%c')", delim);
}
if (!envP->fault_occurred)
XMLRPC_ASSERT(**formatP == delim);
}
static void
buildArrayDecompBranch(xmlrpc_env * const envP,
const char ** const formatP,
char const delim,
va_listx * const argsP,
struct decompTreeNode * const decompNodeP) {
/*----------------------------------------------------------------------------
Fill in the decomposition tree node *decompNodeP to cover an array
whose items are described by *formatP. To wit, they are the values
described by successive format specifiers in *formatP up to but not
including the next 'delim' character.
Plus, the last character before the delimiter might be a '*', which
means "ignore any additional items in the array."
We create a node (and whole branch if required) to describe each array
item.
The pointers to where those items are to be stored are given by
'argsP'.
We advance *formatP to the delimiter character, and advance 'argsP'
past whatever arguments we use.
-----------------------------------------------------------------------------*/
unsigned int itemCnt;
/* Number of array items in the branch so far */
itemCnt = 0; /* Branch is empty so far */
while (**formatP && **formatP != delim && **formatP != '*' &&
!envP->fault_occurred) {
if (itemCnt >= ARRAY_SIZE(decompNodeP->store.Tarray.itemArray))
xmlrpc_faultf(envP, "Too many array items in format string. "
"The most items you can have for an array in "
"a format string is %u.", (unsigned)
ARRAY_SIZE(decompNodeP->store.Tarray.itemArray));
else {
struct decompTreeNode * itemNodeP;
createDecompTreeNext(envP, formatP, argsP, &itemNodeP);
if (!envP->fault_occurred)
decompNodeP->store.Tarray.itemArray[itemCnt++] = itemNodeP;
}
}
if (!envP->fault_occurred) {
decompNodeP->store.Tarray.itemCnt = itemCnt;
processArraySpecTail(envP, formatP,
&decompNodeP->store.Tarray.ignoreExcess,
delim);
}
if (envP->fault_occurred) {
unsigned int i;
for (i = 0; i < itemCnt; ++i)
destroyDecompTree(decompNodeP->store.Tarray.itemArray[i]);
}
}
static void
doStructValue(xmlrpc_env * const envP,
const char ** const formatP,
va_listx * const argsP,
struct mbrDecomp * const mbrP) {
struct decompTreeNode * valueNodeP;
mbrP->key = (const char*) va_arg(argsP->v, char*);
createDecompTreeNext(envP, formatP, argsP, &valueNodeP);
if (!envP->fault_occurred)
mbrP->decompTreeP = valueNodeP;
}
static void
skipAsterisk(xmlrpc_env * const envP,
const char ** const formatP,
char const delim) {
if (**formatP == '*') {
++*formatP;
if (!**formatP)
xmlrpc_faultf(envP, "missing closing delimiter ('%c')", delim);
else if (**formatP != delim)
xmlrpc_faultf(envP, "junk after '*' in the specifier of an "
"array. First character='%c'", **formatP);
} else
/* Conceptually, one can make it an error to leave some struct
members behind, but we have never had code that knows how to
recognize that case.
*/
xmlrpc_faultf(envP,
"You must put a trailing '*' in the specifiers for "
"struct members to signify it's OK if there are "
"additional members you didn't get.");
}
static void
skipColon(xmlrpc_env * const envP,
const char ** const formatP,
char const delim) {
if (**formatP == '\0')
xmlrpc_faultf(envP, "format string ends in the middle of a struct "
"member specifier");
else if (**formatP == delim)
xmlrpc_faultf(envP, "member list ends in the middle of a member");
else if (**formatP != ':')
xmlrpc_faultf(envP, "In a struct specifier, '%c' found "
"where a colon (':') separating key and "
"value was expected.", **formatP);
}
static void
skipComma(xmlrpc_env * const envP,
const char ** const formatP,
char const delim) {
if (**formatP && **formatP != delim) {
if (**formatP == ',')
++*formatP; /* skip over comma */
else
xmlrpc_faultf(envP, "'%c' where we expected a ',' "
"to separate struct members", **formatP);
}
}
static void
buildStructDecompBranch(xmlrpc_env * const envP,
const char ** const formatP,
char const delim,
va_listx * const argsP,
struct decompTreeNode * const decompNodeP) {
/*----------------------------------------------------------------------------
Fill in the decomposition tree node *decompNodeP to cover a struct
whose members are described by *formatP. To wit, they are the values
described by successive format specifiers in *formatP up to but not
including the next 'delim' character.
We create a node (and whole branch if required) to describe each
struct member value.
The pointers to where those values are to be stored are given by
'argsP'.
The names of the members to be extracted are also given by 'argsP'.
We advance *formatP to the delimiter character, and advance 'argsP'
past whatever arguments we use.
-----------------------------------------------------------------------------*/
unsigned int memberCnt;
/* Number of struct members in the branch so far */
memberCnt = 0; /* Branch is empty so far */
while (**formatP && **formatP != delim && **formatP != '*' &&
!envP->fault_occurred) {
if (memberCnt >= ARRAY_SIZE(decompNodeP->store.Tstruct.mbrArray))
xmlrpc_faultf(envP,
"Too many structure members in format string. "
"The most members you can specify in "
"a format string is %u.", (unsigned)
ARRAY_SIZE(decompNodeP->store.Tstruct.mbrArray));
else {
struct mbrDecomp * const mbrP =
&decompNodeP->store.Tstruct.mbrArray[memberCnt];
if (**formatP != 's')
xmlrpc_faultf(envP, "In a struct specifier, the specifier "
"for the key is '%c', but it must be 's'.",
**formatP);
else {
++*formatP;
skipColon(envP, formatP, delim);
if (!envP->fault_occurred) {
++*formatP;
doStructValue(envP, formatP, argsP, mbrP);
if (!envP->fault_occurred)
++memberCnt;
skipComma(envP, formatP, delim);
}
}
}
}
decompNodeP->store.Tstruct.mbrCnt = memberCnt;
if (!envP->fault_occurred) {
skipAsterisk(envP, formatP, delim);
if (!envP->fault_occurred)
XMLRPC_ASSERT(**formatP == delim);
}
if (envP->fault_occurred) {
unsigned int i;
for (i = 0; i < memberCnt; ++i)
destroyDecompTree(
decompNodeP->store.Tstruct.mbrArray[i].decompTreeP);
}
}
static void
createDecompTreeNext(xmlrpc_env * const envP,
const char ** const formatP,
va_listx * const argsP,
struct decompTreeNode ** const decompNodePP) {
/*----------------------------------------------------------------------------
Create a branch of a decomposition tree that applies to the first
value described by '*formatP', and advance *formatP past the description
of that first value. E.g.:
- If *formatP is "isb", we create a branch consisting of one
node -- for an integer. We advance *formatP by one character, so
it points to the "s".
- If *formatP is "(isb)s", we create a branch that represents the
array (isb) and advance *formatP past the closing parenthesis to
point to the final "s". We return as *decompNodePP a pointer to
a node for the array, and that array in turn points to nodes for
each of the 3 array items: one for an integer, one for a string,
and one for a boolean.
The locations at which the components of that value are to be
stored (which is the main contents of the branch we create) are
given by 'argsP'.
Return as *decompNodeP a pointer to the root node of the branch we
generate.
-----------------------------------------------------------------------------*/
struct decompTreeNode * decompNodeP;
MALLOCVAR(decompNodeP);
if (decompNodeP == NULL)
xmlrpc_faultf(envP, "Could not allocate space for a decomposition "
"tree node");
else {
decompNodeP->formatSpecChar = *(*formatP)++;
switch (decompNodeP->formatSpecChar) {
case '-':
/* There's nothing to store */
break;
case 'i':
decompNodeP->store.Tinteger.valueP =
(xmlrpc_int32*) va_arg(argsP->v, xmlrpc_int32*);
break;
case 'b':
decompNodeP->store.Tbool.valueP =
(xmlrpc_bool*) va_arg(argsP->v, xmlrpc_bool*);
break;
case 'd':
decompNodeP->store.Tdouble.valueP =
(double*) va_arg(argsP->v, double*);
break;
case 't':
decompNodeP->store.TdatetimeT.valueP =
va_arg(argsP->v, time_t*);
break;
case '8':
decompNodeP->store.Tdatetime8.valueP =
(const char**) va_arg(argsP->v, char**);
break;
case 's':
decompNodeP->store.Tstring.valueP =
(const char**) va_arg(argsP->v, char**);
if (**formatP == '#') {
decompNodeP->store.Tstring.sizeP =
(size_t*) va_arg(argsP->v, size_t**);
++*formatP;
} else
decompNodeP->store.Tstring.sizeP = NULL;
break;
case 'w':
buildWideStringNode(envP, formatP, argsP, decompNodeP);
break;
case '6':
decompNodeP->store.TbitString.valueP =
(const unsigned char**) va_arg(argsP->v, unsigned char**);
decompNodeP->store.TbitString.sizeP =
(size_t*) va_arg(argsP->v, size_t**);
break;
case 'n':
/* There's no value to store */
break;
case 'I':
decompNodeP->store.Ti8.valueP =
(xmlrpc_int64 *) va_arg(argsP->v, xmlrpc_int64 *);
break;
case 'p':
decompNodeP->store.Tcptr.valueP =
(void**) va_arg(argsP->v, void**);
break;
case 'V':
decompNodeP->store.Tvalue.valueP =
(xmlrpc_value**) va_arg(argsP->v, xmlrpc_value**);
break;
case 'A':
decompNodeP->store.TarrayVal.valueP =
(xmlrpc_value**) va_arg(argsP->v, xmlrpc_value**);
break;
case 'S':
decompNodeP->store.TstructVal.valueP =
(xmlrpc_value**) va_arg(argsP->v, xmlrpc_value**);
break;
case '(':
buildArrayDecompBranch(envP, formatP, ')', argsP, decompNodeP);
++(*formatP); /* skip past closing ')' */
break;
case '{':
buildStructDecompBranch(envP, formatP, '}', argsP, decompNodeP);
++(*formatP); /* skip past closing '}' */
break;
default:
xmlrpc_faultf(envP, "Invalid format character '%c'",
decompNodeP->formatSpecChar);
}
if (envP->fault_occurred)
free(decompNodeP);
else
*decompNodePP = decompNodeP;
}
}
static void
createDecompTree(xmlrpc_env * const envP,
const char * const format,
va_listx const args,
struct decompTreeNode ** const decompRootPP) {
const char * formatCursor;
struct decompTreeNode * decompRootP;
va_listx currentArgs;
currentArgs = args;
formatCursor = &format[0];
createDecompTreeNext(envP, &formatCursor, &currentArgs, &decompRootP);
if (!envP->fault_occurred) {
if (*formatCursor != '\0')
xmlrpc_faultf(envP, "format string '%s' has garbage at the end: "
"'%s'. It should be a specifier of a single value "
"(but that might be a complex value, such as an "
"array)", format, formatCursor);
if (envP->fault_occurred)
destroyDecompTree(decompRootP);
}
*decompRootPP = decompRootP;
}
static void
decomposeValue(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
bool const oldstyleMemMgmt,
const char * const format,
va_listx const args) {
struct decompTreeNode * decompRootP;
XMLRPC_ASSERT_ENV_OK(envP);
XMLRPC_ASSERT_VALUE_OK(valueP);
XMLRPC_ASSERT(format != NULL);
createDecompTree(envP, format, args, &decompRootP);
if (!envP->fault_occurred) {
decomposeValueWithTree(envP, valueP, oldstyleMemMgmt, decompRootP);
destroyDecompTree(decompRootP);
}
}
void
xmlrpc_decompose_value_va(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const char * const format,
va_list const args) {
bool const oldstyleMemMgtFalse = false;
va_listx argsx;
init_va_listx(&argsx, args);
decomposeValue(envP, valueP, oldstyleMemMgtFalse, format, argsx);
}
void
xmlrpc_decompose_value(xmlrpc_env * const envP,
xmlrpc_value * const value,
const char * const format,
...) {
va_list args;
va_start(args, format);
xmlrpc_decompose_value_va(envP, value, format, args);
va_end(args);
}
void
xmlrpc_parse_value_va(xmlrpc_env * const envP,
xmlrpc_value * const valueP,
const char * const format,
va_list const args) {
bool const oldstyleMemMgmtTrue = true;
va_listx argsx;
init_va_listx(&argsx, args);
decomposeValue(envP, valueP, oldstyleMemMgmtTrue, format, argsx);
}
void
xmlrpc_parse_value(xmlrpc_env * const envP,
xmlrpc_value * const value,
const char * const format,
...) {
va_list args;
va_start(args, format);
xmlrpc_parse_value_va(envP, value, format, args);
va_end(args);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.