summaryrefslogtreecommitdiffstats
path: root/Utilities/cmxmlrpc/xmlrpc_serialize.c
blob: 2ff27a0fe453a957c0cbb0a81dd73e2136384f29 (plain)
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
/* Copyright (C) 2001 by First Peer, Inc. 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. */

#include "xmlrpc_config.h"

#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "xmlrpc.h"
#include "xmlrpc_int.h"

#define CRLF "\015\012"
#define SMALL_BUFFER_SZ (128)
#define XML_PROLOGUE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"CRLF


/*=========================================================================
**  format_out
**=========================================================================
**  A lightweight print routine for use with various serialization
**  functions. Only use this routine for printing small objects--it uses
**  a fixed-size internal buffer and returns an error on overflow.
**  In particular, do NOT use this routine to print XML-RPC string values!
*/

static void 
format_out(xmlrpc_env *env,
           xmlrpc_mem_block *output,
           char *format_string,
           ...) {

    va_list args;
    char buffer[SMALL_BUFFER_SZ];
    int count;

    XMLRPC_ASSERT_ENV_OK(env);

    va_start(args, format_string);

    /* We assume that this function is present and works correctly. Right. */
    count = vsnprintf(buffer, SMALL_BUFFER_SZ, format_string, args);

    /* Old C libraries return -1 if vsnprintf overflows its buffer.
    ** New C libraries return the number of characters which *would* have
    ** been printed if the error did not occur. This is impressively vile.
    ** Thank the C99 committee for this bright idea. But wait! We also
    ** need to keep track of the trailing NULL. */
    if (count < 0 || count >= (SMALL_BUFFER_SZ - 1))
    XMLRPC_FAIL(env, XMLRPC_INTERNAL_ERROR,
                "format_out overflowed internal buffer");

    /* Append our new data to our output. */
    XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, buffer, count);
    XMLRPC_FAIL_IF_FAULT(env);

cleanup:
    va_end(args);
}


/*=========================================================================
**  Warnings About Invalid UTF-8
**=========================================================================
**  We claim to send UTF-8 data to the network.  But we rely on application
**  programs to pass us correctly-formed UTF-8 data, which is very naive
**  and optimistic.
**
**  In debudding mode, we call this routine to issue dire-sounding
**  warnings.  For the sake of safety, this routine never exits the
**  program or does anything else drastic.
**
**  This routine almost certainly slows down our output.
*/

#if !defined NDEBUG && defined HAVE_UNICODE_WCHAR

static void 
sanity_check_utf8(const char * const str,
                  size_t       const len) {

    xmlrpc_env env;

    xmlrpc_env_init(&env);
    xmlrpc_validate_utf8(&env, str, len);
    if (env.fault_occurred)
        fprintf(stderr, "*** xmlrpc-c WARNING ***: %s (%s)\n",
                "Application sending corrupted UTF-8 data to network",
                env.fault_string);
    xmlrpc_env_clean(&env);
}
#endif



/*=========================================================================
**  Escaping Strings
**=========================================================================
*/

static xmlrpc_mem_block * 
escape_string(xmlrpc_env * const env, 
              const char * const str,
              size_t       const len) {

    xmlrpc_mem_block *retval;
    size_t i, needed;
    char *out;

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(str != NULL);

    /* Sanity-check this string before we print it. */
#if !defined NDEBUG && defined HAVE_UNICODE_WCHAR
    sanity_check_utf8(str, len);
#endif    

    /* Calculate the amount of space we'll need. */
    needed = 0;
    for (i = 0; i < len; i++) {
        if (str[i] == '<')
            needed += 4; /* &lt; */
        else if (str[i] == '>')
            needed += 4; /* &gt; */
        else if (str[i] == '&')
            needed += 5; /* &amp; */
        else
            needed++;
    }

    /* Allocate our memory block. */
    retval = XMLRPC_TYPED_MEM_BLOCK_NEW(char, env, needed);
    XMLRPC_FAIL_IF_FAULT(env);

    /* Copy over the newly-allocated data. */
    out = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, retval);
    for (i = 0; i < len; i++) {
        if (str[i] == '<') {
            *out++ = '&';
            *out++ = 'l';
            *out++ = 't';
            *out++ = ';';
        } else if (str[i] == '>') {
            *out++ = '&';
            *out++ = 'g';
            *out++ = 't';
            *out++ = ';';
        } else if (str[i] == '&') {
            *out++ = '&';
            *out++ = 'a';
            *out++ = 'm';
            *out++ = 'p';
            *out++ = ';';
        } else {
            *out++ = str[i];
        }
    }

 cleanup:
    if (env->fault_occurred) {
        if (retval)
            XMLRPC_TYPED_MEM_BLOCK_FREE(char, retval);
        retval = NULL;
    }
    return retval;
}



static xmlrpc_mem_block* 
escape_block (xmlrpc_env *env,
              xmlrpc_mem_block *block) {

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(block != NULL);

    return escape_string(env,
                         XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, block),
                         XMLRPC_TYPED_MEM_BLOCK_SIZE(char, block));
}



/*=========================================================================
**  xmlrpc_serialize_string_data
**=========================================================================
**  Escape and print the contents of a string.
*/                

static void 
xmlrpc_serialize_string_data(xmlrpc_env *env,
                             xmlrpc_mem_block *output,
                             xmlrpc_value *string) {

    xmlrpc_mem_block *escaped;
    char *contents;
    size_t size;

    /* Since this routine can only be called internally, we only need
    ** an assertion here, not a runtime type check.
    ** XXX - Temporarily disabled because we're using this code to
    ** print <dateTime.iso8601> values as well. */
    /* XMLRPC_ASSERT(string->_type == XMLRPC_TYPE_STRING); */

    /* Escape any '&' and '<' characters in the string. */
    escaped = escape_block(env, &string->_block);
    XMLRPC_FAIL_IF_FAULT(env);
    contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, escaped);
    size = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, escaped) - 1;
    
    /* Print the string. */
    XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, contents, size);
    XMLRPC_FAIL_IF_FAULT(env);
    
 cleanup:
    if (escaped)
        XMLRPC_TYPED_MEM_BLOCK_FREE(char, escaped);
}



/*=========================================================================
**  xmlrpc_serialize_base64_data
**=========================================================================
**  Print the contents of a memory block as well-formed Base64 data.
*/                

static void 
xmlrpc_serialize_base64_data (xmlrpc_env *env,
                              xmlrpc_mem_block *output,
                              unsigned char* data, size_t len) {

    xmlrpc_mem_block *encoded;
    unsigned char *contents;
    size_t size;

    /* Encode the data. */
    encoded = xmlrpc_base64_encode(env, data, len);
    XMLRPC_FAIL_IF_FAULT(env);
    contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(unsigned char, encoded);
    size = XMLRPC_TYPED_MEM_BLOCK_SIZE(unsigned char, encoded);

    /* Print the data. */
    XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, contents, size);
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:
    if (encoded)
        XMLRPC_TYPED_MEM_BLOCK_FREE(char, encoded);
}



/*=========================================================================
**  xmlrpc_serialize_struct
**=========================================================================
**  Dump the contents of a struct.
*/                

static void 
xmlrpc_serialize_struct(xmlrpc_env *env,
                        xmlrpc_mem_block *output,
                        xmlrpc_value *strct) {

    size_t size;
    size_t i;
    xmlrpc_value *key, *value;

    format_out(env, output, "<struct>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

    size = xmlrpc_struct_size(env, strct);
    XMLRPC_FAIL_IF_FAULT(env);
    for (i = 0; i < size; i++) {
        xmlrpc_struct_get_key_and_value(env, strct, i, &key, &value);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "<member><name>");
        XMLRPC_FAIL_IF_FAULT(env);
        xmlrpc_serialize_string_data(env, output, key);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</name>"CRLF);
        XMLRPC_FAIL_IF_FAULT(env);
        xmlrpc_serialize_value(env, output, value);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</member>"CRLF);
        XMLRPC_FAIL_IF_FAULT(env);
    }

    format_out(env, output, "</struct>");
    XMLRPC_FAIL_IF_FAULT(env);

cleanup:
    return;
}



/*=========================================================================
**  xmlrpc_serialize_value
**=========================================================================
**  Dump a value in the appropriate fashion.
*/                

void 
xmlrpc_serialize_value(xmlrpc_env *env,
                       xmlrpc_mem_block *output,
                       xmlrpc_value *value) {

    xmlrpc_value *item;
    size_t size;
    unsigned char* contents;
    size_t i;

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(output != NULL);
    XMLRPC_ASSERT_VALUE_OK(value);

    /* Print our ubiquitous header. */
    format_out(env, output, "<value>");
    XMLRPC_FAIL_IF_FAULT(env);

    switch (value->_type) {

    case XMLRPC_TYPE_INT:
        /* XXX - We assume that '%i' is the appropriate format specifier
        ** for an xmlrpc_int32 value. We should add some test cases to
        ** make sure this works. */
        format_out(env, output, "<i4>%i</i4>", value->_value.i);
        break;

    case XMLRPC_TYPE_BOOL:
        /* XXX - We assume that '%i' is the appropriate format specifier
        ** for an xmlrpc_bool value. */
        format_out(env, output, "<boolean>%i</boolean>",
                   (value->_value.b) ? 1 : 0);
        break;

    case XMLRPC_TYPE_DOUBLE:
        /* We must output a number of the form [+-]?\d*.\d*. */
        format_out(env, output, "<double>%.17g</double>", value->_value.d);
        break;

    case XMLRPC_TYPE_STRING:
        format_out(env, output, "<string>");
        XMLRPC_FAIL_IF_FAULT(env);
        xmlrpc_serialize_string_data(env, output, value);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</string>");
        break;

    case XMLRPC_TYPE_ARRAY:
        format_out(env, output, "<array><data>"CRLF);
        XMLRPC_FAIL_IF_FAULT(env);

        /* Serialize each item. */
        size = xmlrpc_array_size(env, value);
        XMLRPC_FAIL_IF_FAULT(env);
        for (i = 0; i < size; i++) {
            item = xmlrpc_array_get_item(env, value, i);
            XMLRPC_FAIL_IF_FAULT(env);
            xmlrpc_serialize_value(env, output, item);
            XMLRPC_FAIL_IF_FAULT(env);
            format_out(env, output, CRLF);
            XMLRPC_FAIL_IF_FAULT(env);
        }

        format_out(env, output, "</data></array>");
        break;

    case XMLRPC_TYPE_STRUCT:
        xmlrpc_serialize_struct(env, output, value);
        break;

    case XMLRPC_TYPE_BASE64:
        format_out(env, output, "<base64>"CRLF);
        XMLRPC_FAIL_IF_FAULT(env);
        contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(unsigned char,
                                                   &value->_block);
        size = XMLRPC_TYPED_MEM_BLOCK_SIZE(unsigned char, &value->_block);
        xmlrpc_serialize_base64_data(env, output, contents, size);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</base64>");
        break;      

    case XMLRPC_TYPE_DATETIME:
        format_out(env, output, "<dateTime.iso8601>");
        XMLRPC_FAIL_IF_FAULT(env);
        xmlrpc_serialize_string_data(env, output, value);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</dateTime.iso8601>");
        break;

    case XMLRPC_TYPE_C_PTR:
        xmlrpc_env_set_fault_formatted(
            env, XMLRPC_INTERNAL_ERROR,
            "Tried to serialize a C pointer value.");
        break;

    case XMLRPC_TYPE_DEAD:
        xmlrpc_env_set_fault_formatted(
            env, XMLRPC_INTERNAL_ERROR,
            "Tried to serialize a deaad value.");
        break;

    default:
        xmlrpc_env_set_fault_formatted(
            env, XMLRPC_INTERNAL_ERROR,
            "Invalid xmlrpc_value type: %d", value->_type);
    }
    XMLRPC_FAIL_IF_FAULT(env);

    /* Print our ubiquitous footer. */
    format_out(env, output, "</value>");
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:
    return;
}



/*=========================================================================
**  xmlrpc_serialize_params
**=========================================================================
**  Serialize a list as a set of parameters.
*/                

void 
xmlrpc_serialize_params(xmlrpc_env *env,
                        xmlrpc_mem_block *output,
                        xmlrpc_value *param_array) {

    size_t size, i;
    xmlrpc_value *item;

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(output != NULL);
    XMLRPC_ASSERT_VALUE_OK(param_array);

    format_out(env, output, "<params>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

    /* Dump each parameter. */
    size = xmlrpc_array_size(env, param_array);
    XMLRPC_FAIL_IF_FAULT(env);
    for (i = 0; i < size; i++) {
        format_out(env, output, "<param>");
        XMLRPC_FAIL_IF_FAULT(env);
        item = xmlrpc_array_get_item(env, param_array, i);
        XMLRPC_FAIL_IF_FAULT(env);
        xmlrpc_serialize_value(env, output, item);
        XMLRPC_FAIL_IF_FAULT(env);
        format_out(env, output, "</param>"CRLF);
        XMLRPC_FAIL_IF_FAULT(env);
    }

    format_out(env, output, "</params>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:    
    return;
}



/*=========================================================================
**  xmlrpc_serialize_call
**=========================================================================
**  Serialize an XML-RPC call.
*/                

void 
xmlrpc_serialize_call(xmlrpc_env *       const env,
                      xmlrpc_mem_block * const output,
                      const char *       const method_name,
                      xmlrpc_value *     const param_array) {

    xmlrpc_mem_block *escaped;
    char *contents;
    size_t size;

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(output != NULL);
    XMLRPC_ASSERT(method_name != NULL);
    XMLRPC_ASSERT_VALUE_OK(param_array);
    
    /* Set up our error-handling preconditions. */
    escaped = NULL;

    /* Dump our header. */
    format_out(env, output, XML_PROLOGUE);
    XMLRPC_FAIL_IF_FAULT(env);
    format_out(env, output, "<methodCall>"CRLF"<methodName>");
    XMLRPC_FAIL_IF_FAULT(env);

    /* Dump the method name. */
    escaped = escape_string(env, method_name, strlen(method_name));
    XMLRPC_FAIL_IF_FAULT(env);
    contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, escaped);
    size = XMLRPC_TYPED_MEM_BLOCK_SIZE(char, escaped);
    XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, contents, size);
    XMLRPC_FAIL_IF_FAULT(env);    

    /* Dump our parameters and footer. */
    format_out(env, output, "</methodName>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);
    xmlrpc_serialize_params(env, output, param_array);
    XMLRPC_FAIL_IF_FAULT(env);
    format_out(env, output, "</methodCall>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:
    if (escaped)
        xmlrpc_mem_block_free(escaped);
}



/*=========================================================================
**  xmlrpc_serialize_response
**=========================================================================
**  Serialize the (non-fault) response to an XML-RPC call.
*/                

void 
xmlrpc_serialize_response (xmlrpc_env *env,
                           xmlrpc_mem_block *output,
                           xmlrpc_value *value) {

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(output != NULL);
    XMLRPC_ASSERT_VALUE_OK(value);

    format_out(env, output, XML_PROLOGUE);
    XMLRPC_FAIL_IF_FAULT(env);
    format_out(env, output, "<methodResponse>"CRLF"<params>"CRLF"<param>");
    XMLRPC_FAIL_IF_FAULT(env);

    xmlrpc_serialize_value(env, output, value);
    XMLRPC_FAIL_IF_FAULT(env);

    format_out(env, output,
               "</param>"CRLF"</params>"CRLF"</methodResponse>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);
    
 cleanup:
    return;
}



/*=========================================================================
**  xmlrpc_serialize_fault
**=========================================================================
**  Serialize an XML-RPC fault.
**
**  If this function fails, it will set up the first env argument. You'll
**  need to take some other drastic action to produce a serialized fault
**  of your own. (This function should only fail in an out-of-memory
**  situation, AFAIK.)
*/                

void 
xmlrpc_serialize_fault(xmlrpc_env *env,
                       xmlrpc_mem_block *output,
                       xmlrpc_env *fault) {

    xmlrpc_value *strct;

    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(output != NULL);
    XMLRPC_ASSERT(fault != NULL && fault->fault_occurred);

    /* Build a fault structure. */
    strct = xmlrpc_build_value(env, "{s:i,s:s}",
                   "faultCode", (xmlrpc_int32) fault->fault_code,
                   "faultString", fault->fault_string);
    XMLRPC_FAIL_IF_FAULT(env);

    /* Output our header. */
    format_out(env, output, XML_PROLOGUE);
    XMLRPC_FAIL_IF_FAULT(env);
    format_out(env, output, "<methodResponse>"CRLF"<fault>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

    /* Serialize our fault structure. */
    xmlrpc_serialize_value(env, output, strct);
    XMLRPC_FAIL_IF_FAULT(env);

    /* Output our footer. */
    format_out(env, output, CRLF"</fault>"CRLF"</methodResponse>"CRLF);
    XMLRPC_FAIL_IF_FAULT(env);

 cleanup:
    if (strct)
        xmlrpc_DECREF(strct);
}