Menu

[r3316]: / trunk / src / xmlrpc_data.c  Maximize  Restore  History

Download this file

723 lines (513 with data), 19.9 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
/* Copyright information is at end of file */
#include "xmlrpc_config.h"
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc-c/lock.h"
#include "xmlrpc-c/lock_platform.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
/*=============================================================================
xmlrpc_value is designed to enable cheap copies by sharing pointers and
maintaining reference counts. Multiple threads can use an xmlrpc_value
simultaneously because there is locking around the reference count
manipulation (but only since Xmlrpc-c 1.33). But there is no copy on
write, so the scheme depends upon the user not modifying an xmlrpc_value
after building it, and not copying it while building it. Another reason
to observe this sequence is that there is no locking around modifications,
so a reader could see a half-updated xmlrpc_value.
We could enforce a prohibition against modifying an xmlrpc_value that has
references other than the one by the party doing the modifying, but we
don't because we allowed it for a long time before we noticed the problem
adn we're afraid of breaking an existing program that does these updates in
such a way that it actually works.
=============================================================================*/
static void
destroyCptr(xmlrpc_value * const valueP) {
if (valueP->_value.cptr.dtor)
valueP->_value.cptr.dtor(valueP->_value.cptr.dtorContext,
valueP->_value.cptr.objectP);
}
static void
destroyValue(xmlrpc_value * const valueP) {
/* First, we need to destroy this value's contents, if any. */
switch (valueP->_type) {
case XMLRPC_TYPE_INT:
break;
case XMLRPC_TYPE_BOOL:
break;
case XMLRPC_TYPE_DOUBLE:
break;
case XMLRPC_TYPE_DATETIME:
xmlrpc_destroyDatetime(valueP);
break;
case XMLRPC_TYPE_STRING:
xmlrpc_destroyString(valueP);
break;
case XMLRPC_TYPE_BASE64:
xmlrpc_mem_block_free(valueP->blockP);
break;
case XMLRPC_TYPE_ARRAY:
xmlrpc_destroyArrayContents(valueP);
break;
case XMLRPC_TYPE_STRUCT:
xmlrpc_destroyStruct(valueP);
break;
case XMLRPC_TYPE_C_PTR:
destroyCptr(valueP);
break;
case XMLRPC_TYPE_NIL:
break;
case XMLRPC_TYPE_I8:
break;
case XMLRPC_TYPE_DEAD:
XMLRPC_ASSERT(false); /* Can't happen, per entry conditions */
break;
default:
XMLRPC_ASSERT(false); /* There are no other possible values */
}
valueP->lockP->destroy(valueP->lockP);
/* Next, we mark this value as invalid, to help catch refcount errors.
*/
valueP->_type = XMLRPC_TYPE_DEAD;
/* Finally, we destroy the value itself. */
free(valueP);
}
/*===========================================================================
Reference Counting
=============================================================================
Some simple reference-counting code. The xmlrpc_DECREF routine is in
charge of destroying values when their reference count reaches zero.
============================================================================*/
void
xmlrpc_INCREF (xmlrpc_value * const valueP) {
XMLRPC_ASSERT_VALUE_OK(valueP);
valueP->lockP->acquire(valueP->lockP);
XMLRPC_ASSERT(valueP->refcount > 0);
++valueP->refcount;
valueP->lockP->release(valueP->lockP);
}
void
xmlrpc_DECREF (xmlrpc_value * const valueP) {
bool died;
XMLRPC_ASSERT_VALUE_OK(valueP);
valueP->lockP->acquire(valueP->lockP);
XMLRPC_ASSERT(valueP->refcount > 0);
XMLRPC_ASSERT(valueP->_type != XMLRPC_TYPE_DEAD);
--valueP->refcount;
died = (valueP->refcount == 0);
valueP->lockP->release(valueP->lockP);
if (died)
destroyValue(valueP);
}
/*=========================================================================
Utiltiies
=========================================================================*/
const char *
xmlrpc_type_name(xmlrpc_type const type) {
switch (type) {
case XMLRPC_TYPE_INT: return "INT";
case XMLRPC_TYPE_BOOL: return "BOOL";
case XMLRPC_TYPE_DOUBLE: return "DOUBLE";
case XMLRPC_TYPE_DATETIME: return "DATETIME";
case XMLRPC_TYPE_STRING: return "STRING";
case XMLRPC_TYPE_BASE64: return "BASE64";
case XMLRPC_TYPE_ARRAY: return "ARRAY";
case XMLRPC_TYPE_STRUCT: return "STRUCT";
case XMLRPC_TYPE_C_PTR: return "C_PTR";
case XMLRPC_TYPE_NIL: return "NIL";
case XMLRPC_TYPE_I8: return "I8";
case XMLRPC_TYPE_DEAD: return "DEAD";
default: return "???";
}
}
static void
validateType(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_type const expectedType) {
if (valueP->_type != expectedType) {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR, "Value of type %s supplied where "
"type %s was expected.",
xmlrpc_type_name(valueP->_type), xmlrpc_type_name(expectedType));
}
}
/*=========================================================================
Extracting XML-RPC value
===========================================================================
These routines extract XML-RPC values into ordinary C data types.
For array and struct values, see the separates files xmlrpc_array.c
and xmlrpc_struct.c.
=========================================================================*/
void
xmlrpc_read_int(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_int32 * const intValueP) {
validateType(envP, valueP, XMLRPC_TYPE_INT);
if (!envP->fault_occurred)
*intValueP = valueP->_value.i;
}
void
xmlrpc_read_bool(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_bool * const boolValueP) {
validateType(envP, valueP, XMLRPC_TYPE_BOOL);
if (!envP->fault_occurred)
*boolValueP = valueP->_value.b;
}
void
xmlrpc_read_double(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_double * const doubleValueP) {
validateType(envP, valueP, XMLRPC_TYPE_DOUBLE);
if (!envP->fault_occurred)
*doubleValueP = valueP->_value.d;
}
/* datetime stuff is in xmlrpc_datetime.c */
/* string stuff is in xmlrpc_string.c */
void
xmlrpc_read_base64(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const unsigned char ** const byteStringValueP) {
validateType(envP, valueP, XMLRPC_TYPE_BASE64);
if (!envP->fault_occurred) {
size_t const size =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP);
const char * const contents =
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
char * byteStringValue;
byteStringValue = malloc(size);
if (byteStringValue == NULL)
xmlrpc_faultf(envP,
"Unable to allocate %u bytes for byte string.",
(unsigned)size);
else {
memcpy(byteStringValue, contents, size);
*byteStringValueP = (const unsigned char *)byteStringValue;
*lengthP = size;
}
}
}
void
xmlrpc_read_base64_old(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP,
const unsigned char ** const byteStringValueP) {
validateType(envP, valueP, XMLRPC_TYPE_BASE64);
if (!envP->fault_occurred) {
*lengthP =
XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP);
*byteStringValueP = (const unsigned char *)
XMLRPC_MEMBLOCK_CONTENTS(char, valueP->blockP);
}
}
void
xmlrpc_read_base64_size(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
size_t * const lengthP) {
validateType(envP, valueP, XMLRPC_TYPE_BASE64);
if (!envP->fault_occurred)
*lengthP = XMLRPC_MEMBLOCK_SIZE(char, valueP->blockP);
}
void
xmlrpc_read_cptr(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
void ** const ptrValueP) {
validateType(envP, valueP, XMLRPC_TYPE_C_PTR);
if (!envP->fault_occurred)
*ptrValueP = valueP->_value.cptr.objectP;
}
void
xmlrpc_read_nil(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
/*----------------------------------------------------------------------------
Read out the value of a nil value. It doesn't have one, of course, so
this is essentially a no-op. But it does validate the type and is
necessary to match all the other types.
-----------------------------------------------------------------------------*/
validateType(envP, valueP, XMLRPC_TYPE_NIL);
}
void
xmlrpc_read_i8(xmlrpc_env * const envP,
const xmlrpc_value * const valueP,
xmlrpc_int64 * const intValueP) {
validateType(envP, valueP, XMLRPC_TYPE_I8);
if (!envP->fault_occurred)
*intValueP = valueP->_value.i8;
}
xmlrpc_type xmlrpc_value_type (xmlrpc_value* const value)
{
XMLRPC_ASSERT_VALUE_OK(value);
return value->_type;
}
void
xmlrpc_createXmlrpcValue(xmlrpc_env * const envP,
xmlrpc_value ** const valPP) {
/*----------------------------------------------------------------------------
Create a blank xmlrpc_value to be filled in.
Set the reference count to 1.
-----------------------------------------------------------------------------*/
xmlrpc_value * valP;
MALLOCVAR(valP);
if (!valP)
xmlrpc_faultf(envP, "Could not allocate memory for xmlrpc_value");
else {
valP->lockP = xmlrpc_lock_create();
if (!valP->lockP)
xmlrpc_faultf(envP, "Could not allocate memory for lock for "
"xmlrpc_value");
else
valP->refcount = 1;
if (envP->fault_occurred) {
free(valP);
valP = NULL;
}
}
*valPP = valP;
}
xmlrpc_value *
xmlrpc_value_new(xmlrpc_env * const envP,
xmlrpc_value * const sourceValP) {
switch (sourceValP->_type) {
case XMLRPC_TYPE_INT:
return xmlrpc_int_new_value(envP, sourceValP);
case XMLRPC_TYPE_I8:
return xmlrpc_i8_new_value(envP, sourceValP);
case XMLRPC_TYPE_BOOL:
return xmlrpc_bool_new_value(envP, sourceValP);
case XMLRPC_TYPE_DOUBLE:
return xmlrpc_double_new_value(envP, sourceValP);
case XMLRPC_TYPE_DATETIME:
return xmlrpc_datetime_new_value(envP, sourceValP);
case XMLRPC_TYPE_STRING:
return xmlrpc_string_new_value(envP, sourceValP);
case XMLRPC_TYPE_BASE64:
return xmlrpc_base64_new_value(envP, sourceValP);
case XMLRPC_TYPE_ARRAY:
return xmlrpc_array_new_value(envP, sourceValP);
case XMLRPC_TYPE_STRUCT:
return xmlrpc_struct_new_value(envP, sourceValP);
case XMLRPC_TYPE_C_PTR:
return xmlrpc_cptr_new_value(envP, sourceValP);
case XMLRPC_TYPE_NIL:
return xmlrpc_nil_new(envP);
case XMLRPC_TYPE_DEAD:
xmlrpc_faultf(envP, "Attempt to copy a dead xmlrpc_value");
return NULL;
}
assert(false); /* All cases are handled above */
return NULL; /* Quiet compiler warning */
}
xmlrpc_value *
xmlrpc_int_new(xmlrpc_env * const envP,
xmlrpc_int32 const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_INT;
valP->_value.i = value;
}
return valP;
}
xmlrpc_value *
xmlrpc_int_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_INT) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not an integer. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_int_new(envP, valueP->_value.i);
return retval;
}
xmlrpc_value *
xmlrpc_i8_new(xmlrpc_env * const envP,
xmlrpc_int64 const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_I8;
valP->_value.i8 = value;
}
return valP;
}
xmlrpc_value *
xmlrpc_i8_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_I8) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a 64-bit integer. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_i8_new(envP, valueP->_value.i8);
return retval;
}
xmlrpc_value *
xmlrpc_bool_new(xmlrpc_env * const envP,
xmlrpc_bool const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_BOOL;
valP->_value.b = value;
}
return valP;
}
xmlrpc_value *
xmlrpc_bool_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_BOOL) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a boolean. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_bool_new(envP, valueP->_value.b);
return retval;
}
xmlrpc_value *
xmlrpc_double_new(xmlrpc_env * const envP,
double const value) {
xmlrpc_value * valP;
if (!XMLRPC_FINITE(value)) {
xmlrpc_faultf(envP, "Value is not a finite number, "
"so cannot be represented in XML-RPC");
valP = NULL;
} else {
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_DOUBLE;
valP->_value.d = value;
}
}
return valP;
}
xmlrpc_value *
xmlrpc_double_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_DOUBLE) {
xmlrpc_env_set_fault_formatted(
envP, XMLRPC_TYPE_ERROR,
"Value is not a floating point number. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_double_new(envP, valueP->_value.d);
return retval;
}
xmlrpc_value *
xmlrpc_base64_new(xmlrpc_env * const envP,
size_t const length,
const unsigned char * const value) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_BASE64;
valP->blockP = xmlrpc_mem_block_new(envP, length);
if (!envP->fault_occurred) {
char * const contents =
xmlrpc_mem_block_contents(valP->blockP);
memcpy(contents, value, length);
}
if (envP->fault_occurred)
free(valP);
}
return valP;
}
xmlrpc_value *
xmlrpc_base64_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_BASE64) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a datetime. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_base64_new(envP,
xmlrpc_mem_block_size(valueP->blockP),
xmlrpc_mem_block_contents(valueP->blockP));
return retval;
}
/* array stuff is in xmlrpc_array.c */
xmlrpc_value *
xmlrpc_cptr_new(xmlrpc_env * const envP,
void * const value) {
return xmlrpc_cptr_new_dtor(envP, value, NULL, NULL);
}
xmlrpc_value *
xmlrpc_cptr_new_dtor(xmlrpc_env * const envP,
void * const value,
xmlrpc_cptr_dtor_fn const dtor,
void * const dtorContext) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred) {
valP->_type = XMLRPC_TYPE_C_PTR;
valP->_value.cptr.objectP = value;
valP->_value.cptr.dtor = dtor;
valP->_value.cptr.dtorContext = dtorContext;
}
return valP;
}
xmlrpc_value *
xmlrpc_cptr_new_value(xmlrpc_env * const envP,
xmlrpc_value * const valueP) {
xmlrpc_value * retval;
if (valueP->_type != XMLRPC_TYPE_C_PTR) {
xmlrpc_env_set_fault_formatted(envP, XMLRPC_TYPE_ERROR,
"Value is not a C poitner. "
"It is type #%d", valueP->_type);
retval = NULL;
} else
retval = xmlrpc_cptr_new_dtor(envP,
valueP->_value.cptr.objectP,
valueP->_value.cptr.dtor,
valueP->_value.cptr.dtorContext);
return retval;
}
xmlrpc_value *
xmlrpc_nil_new(xmlrpc_env * const envP) {
xmlrpc_value * valP;
xmlrpc_createXmlrpcValue(envP, &valP);
if (!envP->fault_occurred)
valP->_type = XMLRPC_TYPE_NIL;
return valP;
}
/* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
** Copyright (C) 2001 by Eric Kidd. All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE. */
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.