initial load of upstream version 1.06.32
[xmlrpc-c] / include / xmlrpc-c / base.h
1 /* Copyright and license information is at the end of the file */
2
3 #ifndef XMLRPC_H_INCLUDED
4 #define XMLRPC_H_INCLUDED
5
6 #include <stddef.h>
7 #include <stdarg.h>
8 #include <time.h>
9 #include <xmlrpc-c/util.h>
10 #include <xmlrpc-c/config.h>  /* Defines XMLRPC_HAVE_WCHAR */
11
12 #if XMLRPC_HAVE_WCHAR
13 #include <wchar.h>
14 #endif
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20
21 /*=========================================================================
22 **  Typedefs
23 **=========================================================================
24 **  We define names for these types, because they may change from platform
25 **  to platform.
26 */
27
28 typedef signed int xmlrpc_int;  
29     /* An integer of the type defined by XML-RPC <int>; i.e. 32 bit */
30 typedef signed int xmlrpc_int32;
31     /* An integer of the type defined by XML-RPC <int4>; i.e. 32 bit */
32 typedef int xmlrpc_bool;
33     /* A boolean (of the type defined by XML-RPC <boolean>, but there's
34        really only one kind)
35     */
36 typedef double xmlrpc_double;
37     /* A double precision floating point number as defined by
38        XML-RPC <float>.  But the C "double" type is universally the same,
39        so it's probably clearer just to use that.  This typedef is here 
40        for mathematical completeness.
41     */
42
43 #ifdef _WIN32
44 typedef SOCKET xmlrpc_socket;
45 #else
46 typedef int xmlrpc_socket;
47 #endif
48
49 #define XMLRPC_INT32_MAX (2147483647)
50 #define XMLRPC_INT32_MIN (-XMLRPC_INT32_MAX - 1)
51
52
53
54 /*=========================================================================
55 **  xmlrpc_value
56 **=========================================================================
57 **  An XML-RPC value (of any type).
58 */
59
60 typedef enum {
61     XMLRPC_TYPE_INT      = 0,
62     XMLRPC_TYPE_BOOL     = 1,
63     XMLRPC_TYPE_DOUBLE   = 2,
64     XMLRPC_TYPE_DATETIME = 3,
65     XMLRPC_TYPE_STRING   = 4,
66     XMLRPC_TYPE_BASE64   = 5,
67     XMLRPC_TYPE_ARRAY    = 6,
68     XMLRPC_TYPE_STRUCT   = 7,
69     XMLRPC_TYPE_C_PTR    = 8,
70     XMLRPC_TYPE_NIL      = 9,
71     XMLRPC_TYPE_DEAD     = 0xDEAD
72 } xmlrpc_type;
73
74 /* These are *always* allocated on the heap. No exceptions. */
75 typedef struct _xmlrpc_value xmlrpc_value;
76
77 void
78 xmlrpc_abort_if_array_bad(xmlrpc_value * const arrayP);
79
80 #define XMLRPC_ASSERT_ARRAY_OK(val) \
81     xmlrpc_abort_if_array_bad(val)
82
83 /* Increment the reference count of an xmlrpc_value. */
84 extern void xmlrpc_INCREF (xmlrpc_value* value);
85
86 /* Decrement the reference count of an xmlrpc_value. If there
87 ** are no more references, free it. */
88 extern void xmlrpc_DECREF (xmlrpc_value* value);
89
90 /* Get the type of an XML-RPC value. */
91 extern xmlrpc_type xmlrpc_value_type (xmlrpc_value* value);
92
93 xmlrpc_value *
94 xmlrpc_int_new(xmlrpc_env * const envP,
95                int          const intValue);
96
97 void 
98 xmlrpc_read_int(xmlrpc_env *         const envP,
99                 const xmlrpc_value * const valueP,
100                 int *                const intValueP);
101
102 xmlrpc_value *
103 xmlrpc_bool_new(xmlrpc_env * const envP,
104                 xmlrpc_bool  const boolValue);
105
106 void
107 xmlrpc_read_bool(xmlrpc_env *         const envP,
108                  const xmlrpc_value * const valueP,
109                  xmlrpc_bool *        const boolValueP);
110
111 xmlrpc_value *
112 xmlrpc_double_new(xmlrpc_env * const envP,
113                   double       const doubleValue);
114
115 void
116 xmlrpc_read_double(xmlrpc_env *         const envP,
117                    const xmlrpc_value * const valueP,
118                    xmlrpc_double *      const doubleValueP);
119
120 xmlrpc_value *
121 xmlrpc_datetime_new_str(xmlrpc_env * const envP,
122                         const char * const value);
123
124 xmlrpc_value *
125 xmlrpc_datetime_new_sec(xmlrpc_env * const envP, 
126                         time_t       const value);
127
128 void
129 xmlrpc_read_datetime_str(xmlrpc_env *         const envP,
130                          const xmlrpc_value * const valueP,
131                          const char **        const stringValueP);
132
133 void
134 xmlrpc_read_datetime_sec(xmlrpc_env *         const envP,
135                          const xmlrpc_value * const valueP,
136                          time_t *             const timeValueP);
137
138 xmlrpc_value *
139 xmlrpc_string_new(xmlrpc_env * const envP,
140                   const char * const stringValue);
141
142 xmlrpc_value *
143 xmlrpc_string_new_lp(xmlrpc_env * const envP, 
144                      size_t       const length,
145                      const char * const stringValue);
146
147 void
148 xmlrpc_read_string(xmlrpc_env *         const envP,
149                    const xmlrpc_value * const valueP,
150                    const char **        const stringValueP);
151
152
153 void
154 xmlrpc_read_string_lp(xmlrpc_env *         const envP,
155                       const xmlrpc_value * const valueP,
156                       size_t *             const lengthP,
157                       const char **        const stringValueP);
158
159 #if XMLRPC_HAVE_WCHAR
160 xmlrpc_value *
161 xmlrpc_string_w_new(xmlrpc_env *    const envP,
162                     const wchar_t * const stringValue);
163
164 xmlrpc_value *
165 xmlrpc_string_w_new_lp(xmlrpc_env *    const envP, 
166                        size_t          const length,
167                        const wchar_t * const stringValue);
168
169 void
170 xmlrpc_read_string_w(xmlrpc_env *     const envP,
171                      xmlrpc_value *   const valueP,
172                      const wchar_t ** const stringValueP);
173
174 void
175 xmlrpc_read_string_w_lp(xmlrpc_env *     const envP,
176                         xmlrpc_value *   const valueP,
177                         size_t *         const lengthP,
178                         const wchar_t ** const stringValueP);
179
180 #endif /* XMLRPC_HAVE_WCHAR */
181
182 xmlrpc_value *
183 xmlrpc_base64_new(xmlrpc_env *          const envP, 
184                   size_t                const length,
185                   const unsigned char * const value);
186
187 void
188 xmlrpc_read_base64(xmlrpc_env *           const envP,
189                    const xmlrpc_value *   const valueP,
190                    size_t *               const lengthP,
191                    const unsigned char ** const bytestringValueP);
192
193 void
194 xmlrpc_read_base64_size(xmlrpc_env *           const envP,
195                         const xmlrpc_value *   const valueP,
196                         size_t *               const lengthP);
197
198 xmlrpc_value *
199 xmlrpc_array_new(xmlrpc_env * const envP);
200
201 /* Return the number of elements in an XML-RPC array.
202 ** Sets XMLRPC_TYPE_ERROR if 'array' is not an array. */
203 int 
204 xmlrpc_array_size(xmlrpc_env *         const env, 
205                   const xmlrpc_value * const array);
206
207 /* Append an item to an XML-RPC array.
208 ** Sets XMLRPC_TYPE_ERROR if 'array' is not an array. */
209 extern void
210 xmlrpc_array_append_item (xmlrpc_env   * envP,
211                           xmlrpc_value * arrayP,
212                           xmlrpc_value * valueP);
213
214 void
215 xmlrpc_array_read_item(xmlrpc_env *         const envP,
216                        const xmlrpc_value * const arrayP,
217                        unsigned int         const index,
218                        xmlrpc_value **      const valuePP);
219
220 /* Deprecated.  Use xmlrpc_array_read_item() instead.
221
222    Get an item from an XML-RPC array.
223    Does not increment the reference count of the returned value.
224    Sets XMLRPC_TYPE_ERROR if 'array' is not an array.
225    Sets XMLRPC_INDEX_ERROR if 'index' is out of bounds.
226 */
227 xmlrpc_value * 
228 xmlrpc_array_get_item(xmlrpc_env *         const envP,
229                       const xmlrpc_value * const arrayP,
230                       int                  const index);
231
232 /* Not implemented--we don't need it yet.
233 extern 
234 int xmlrpc_array_set_item (xmlrpc_env* env,
235 xmlrpc_value* array,
236 int index,
237                                   xmlrpc_value* value);
238 */
239
240 void
241 xmlrpc_read_nil(xmlrpc_env *   const envP,
242                 xmlrpc_value * const valueP);
243                 
244
245 void
246 xmlrpc_read_cptr(xmlrpc_env *         const envP,
247                  const xmlrpc_value * const valueP,
248                  void **              const ptrValueP);
249
250 xmlrpc_value *
251 xmlrpc_struct_new(xmlrpc_env * env);
252
253 /* Return the number of key/value pairs in a struct.
254 ** Sets XMLRPC_TYPE_ERROR if 'strct' is not a struct. */
255 int
256 xmlrpc_struct_size (xmlrpc_env   * env, 
257                     xmlrpc_value * strct);
258
259 /* Returns true iff 'strct' contains 'key'.
260 ** Sets XMLRPC_TYPE_ERROR if 'strct' is not a struct. */
261 int 
262 xmlrpc_struct_has_key(xmlrpc_env *   const envP,
263                       xmlrpc_value * const strctP,
264                       const char *   const key);
265
266 /* The same as the above, but the key may contain zero bytes.
267    Deprecated.  xmlrpc_struct_get_value_v() is more general, and this
268    case is not common enough to warrant a shortcut.
269 */
270 int 
271 xmlrpc_struct_has_key_n(xmlrpc_env   * const envP,
272                         xmlrpc_value * const strctP,
273                         const char *   const key, 
274                         size_t         const key_len);
275
276 #if 0
277 /* Not implemented yet, but needed for completeness. */
278 int
279 xmlrpc_struct_has_key_v(xmlrpc_env *   env, 
280                         xmlrpc_value * strct,
281                         xmlrpc_value * const keyval);
282 #endif
283
284
285 void
286 xmlrpc_struct_find_value(xmlrpc_env *    const envP,
287                          xmlrpc_value *  const structP,
288                          const char *    const key,
289                          xmlrpc_value ** const valuePP);
290
291
292 void
293 xmlrpc_struct_find_value_v(xmlrpc_env *    const envP,
294                            xmlrpc_value *  const structP,
295                            xmlrpc_value *  const keyP,
296                            xmlrpc_value ** const valuePP);
297
298 void
299 xmlrpc_struct_read_value_v(xmlrpc_env *    const envP,
300                            xmlrpc_value *  const structP,
301                            xmlrpc_value *  const keyP,
302                            xmlrpc_value ** const valuePP);
303
304 void
305 xmlrpc_struct_read_value(xmlrpc_env *    const envP,
306                          xmlrpc_value *  const strctP,
307                          const char *    const key,
308                          xmlrpc_value ** const valuePP);
309
310 /* The "get_value" functions are deprecated.  Use the "find_value"
311    and "read_value" functions instead.
312 */
313 xmlrpc_value * 
314 xmlrpc_struct_get_value(xmlrpc_env *   const envP,
315                         xmlrpc_value * const strctP,
316                         const char *   const key);
317
318 /* The same as above, but the key may contain zero bytes. 
319    Deprecated.  xmlrpc_struct_get_value_v() is more general, and this
320    case is not common enough to warrant a shortcut.
321 */
322 xmlrpc_value * 
323 xmlrpc_struct_get_value_n(xmlrpc_env *   const envP,
324                           xmlrpc_value * const strctP,
325                           const char *   const key, 
326                           size_t         const key_len);
327
328 /* Set the value associated with 'key' in 'strct' to 'value'.
329    Sets XMLRPC_TYPE_ERROR if 'strct' is not a struct. 
330 */
331 void 
332 xmlrpc_struct_set_value(xmlrpc_env *   const env,
333                         xmlrpc_value * const strct,
334                         const char *   const key,
335                         xmlrpc_value * const value);
336
337 /* The same as above, but the key may contain zero bytes.  Deprecated.
338    The general way to set a structure value is xmlrpc_struct_set_value_v(),
339    and this case is not common enough to deserve a shortcut.
340 */
341 void 
342 xmlrpc_struct_set_value_n(xmlrpc_env *    const env,
343                           xmlrpc_value *  const strct,
344                           const char *    const key, 
345                           size_t          const key_len,
346                           xmlrpc_value *  const value);
347
348 /* The same as above, but the key must be an XML-RPC string.
349 ** Fails with XMLRPC_TYPE_ERROR if 'keyval' is not a string. */
350 void 
351 xmlrpc_struct_set_value_v(xmlrpc_env *   const env,
352                           xmlrpc_value * const strct,
353                           xmlrpc_value * const keyval,
354                           xmlrpc_value * const value);
355
356 /* Given a zero-based index, return the matching key and value. This
357 ** is normally used in conjunction with xmlrpc_struct_size.
358 ** Fails with XMLRPC_TYPE_ERROR if 'struct' is not a struct.
359 ** Fails with XMLRPC_INDEX_ERROR if 'index' is out of bounds. */
360
361 void 
362 xmlrpc_struct_read_member(xmlrpc_env *    const envP,
363                           xmlrpc_value *  const structP,
364                           unsigned int    const index,
365                           xmlrpc_value ** const keyvalP,
366                           xmlrpc_value ** const valueP);
367
368 /* The same as above, but does not increment the reference count of the
369    two values it returns, and return NULL for both if it fails, and
370    takes a signed integer for the index (but fails if it is negative).
371
372    Deprecated.  Use xmlrpc_struct_read_member() instead.
373 */
374 void
375 xmlrpc_struct_get_key_and_value(xmlrpc_env *    env,
376                                 xmlrpc_value *  strct,
377                                 int             index,
378                                 xmlrpc_value ** out_keyval,
379                                 xmlrpc_value ** out_value);
380
381 xmlrpc_value *
382 xmlrpc_cptr_new(xmlrpc_env * const envP,
383                 void *       const value);
384
385 xmlrpc_value *
386 xmlrpc_nil_new(xmlrpc_env * const envP);
387
388
389 /* Build an xmlrpc_value from a format string. */
390
391 xmlrpc_value * 
392 xmlrpc_build_value(xmlrpc_env * const env,
393                    const char * const format, 
394                    ...);
395
396 /* The same as the above, but using a va_list and more general */
397 void
398 xmlrpc_build_value_va(xmlrpc_env *    const env,
399                       const char *    const format,
400                       va_list               args,
401                       xmlrpc_value ** const valPP,
402                       const char **   const tailP);
403
404 void 
405 xmlrpc_decompose_value(xmlrpc_env *   const envP,
406                        xmlrpc_value * const value,
407                        const char *   const format, 
408                        ...);
409
410 void 
411 xmlrpc_decompose_value_va(xmlrpc_env *   const envP,
412                           xmlrpc_value * const value,
413                           const char *   const format,
414                           va_list              args);
415
416 /* xmlrpc_parse_value... is the same as xmlrpc_decompose_value... except
417    that it doesn't do proper memory management -- it returns xmlrpc_value's
418    without incrementing the reference count and returns pointers to data
419    inside an xmlrpc_value structure.
420
421    These are deprecated.  Use xmlrpc_decompose_value... instead.
422 */
423 void 
424 xmlrpc_parse_value(xmlrpc_env *   const envP,
425                    xmlrpc_value * const value,
426                    const char *   const format, 
427                    ...);
428
429 /* The same as the above, but using a va_list. */
430 void 
431 xmlrpc_parse_value_va(xmlrpc_env *   const envP,
432                       xmlrpc_value * const value,
433                       const char *   const format,
434                       va_list              args);
435
436 /*=========================================================================
437 **  Encoding XML
438 **=======================================================================*/
439
440 /* Serialize an XML value without any XML header. This is primarily used
441 ** for testing purposes. */
442 void
443 xmlrpc_serialize_value(xmlrpc_env *       env,
444                        xmlrpc_mem_block * output,
445                        xmlrpc_value *     value);
446
447 /* Serialize a list of parameters without any XML header. This is
448 ** primarily used for testing purposes. */
449 void
450 xmlrpc_serialize_params(xmlrpc_env *       env,
451                         xmlrpc_mem_block * output,
452                         xmlrpc_value *     param_array);
453
454 /* Serialize an XML-RPC call. */
455 void 
456 xmlrpc_serialize_call (xmlrpc_env *       const env,
457                        xmlrpc_mem_block * const output,
458                        const char *       const method_name,
459                        xmlrpc_value *     const param_array);
460
461 /* Serialize an XML-RPC return value. */
462 extern void
463 xmlrpc_serialize_response(xmlrpc_env *       env,
464                           xmlrpc_mem_block * output,
465                           xmlrpc_value *     value);
466
467 /* Serialize an XML-RPC fault (as specified by 'fault'). */
468 extern void
469 xmlrpc_serialize_fault(xmlrpc_env *       env,
470                        xmlrpc_mem_block * output,
471                        xmlrpc_env *       fault);
472
473
474 /*=========================================================================
475 **  Decoding XML
476 **=======================================================================*/
477
478 /* Parse an XML-RPC call. If an error occurs, set a fault and set
479 ** the output variables to NULL.
480 ** The caller is responsible for calling free(*out_method_name) and
481 ** xmlrpc_DECREF(*out_param_array). */
482 void 
483 xmlrpc_parse_call(xmlrpc_env *    const envP,
484                   const char *    const xml_data,
485                   size_t          const xml_len,
486                   const char **   const out_method_name,
487                   xmlrpc_value ** const out_param_array);
488
489 void
490 xmlrpc_parse_response2(xmlrpc_env *    const envP,
491                        const char *    const xmlData,
492                        size_t          const xmlDataLen,
493                        xmlrpc_value ** const resultPP,
494                        int *           const faultCodeP,
495                        const char **   const faultStringP);
496
497
498 /* xmlrpc_parse_response() is for backward compatibility */
499
500 xmlrpc_value *
501 xmlrpc_parse_response(xmlrpc_env * const envP, 
502                       const char * const xmlData, 
503                       size_t       const xmlDataLen);
504
505
506 /*=========================================================================
507 **  XML-RPC Base64 Utilities
508 **=========================================================================
509 **  Here are some lightweight utilities which can be used to encode and
510 **  decode Base64 data. These are exported mainly for testing purposes.
511 */
512
513 /* This routine inserts newlines every 76 characters, as required by the
514 ** Base64 specification. */
515 xmlrpc_mem_block *
516 xmlrpc_base64_encode(xmlrpc_env *    env,
517                      unsigned char * bin_data,
518                      size_t          bin_len);
519
520 /* This routine encodes everything in one line. This is needed for HTTP
521 ** authentication and similar tasks. */
522 xmlrpc_mem_block *
523 xmlrpc_base64_encode_without_newlines(xmlrpc_env *    env,
524                                       unsigned char * bin_data,
525                                       size_t          bin_len);
526
527 /* This decodes Base64 data with or without newlines. */
528 extern xmlrpc_mem_block *
529 xmlrpc_base64_decode(xmlrpc_env * const envP,
530                      const char * const ascii_data,
531                      size_t       const ascii_len);
532
533
534 /*=========================================================================
535 **  UTF-8 Encoding and Decoding
536 **=========================================================================
537 **  We need a correct, reliable and secure UTF-8 decoder. This decoder
538 **  raises a fault if it encounters invalid UTF-8.
539 **
540 **  Note that ANSI C does not precisely define the representation used
541 **  by wchar_t--it may be UCS-2, UTF-16, UCS-4, or something from outer
542 **  space. If your platform does something especially bizarre, you may
543 **  need to reimplement these routines.
544 */
545
546 /* Ensure that a string contains valid, legally-encoded UTF-8 data.
547    (Incorrectly-encoded UTF-8 strings are often used to bypass security
548    checks.)
549 */
550 void 
551 xmlrpc_validate_utf8 (xmlrpc_env * const env,
552                       const char * const utf8_data,
553                       size_t       const utf8_len);
554
555 /* Decode a UTF-8 string. */
556 xmlrpc_mem_block *
557 xmlrpc_utf8_to_wcs(xmlrpc_env * const envP,
558                    const char * const utf8_data,
559                    size_t       const utf8_len);
560
561 /* Encode a UTF-8 string. */
562
563 #if XMLRPC_HAVE_WCHAR
564 xmlrpc_mem_block *
565 xmlrpc_wcs_to_utf8(xmlrpc_env * env,
566                    wchar_t *    wcs_data,
567                    size_t       wcs_len);
568 #endif
569
570 /*=========================================================================
571 **  Authorization Cookie Handling
572 **=========================================================================
573 **  Routines to get and set values for authorizing via authorization
574 **  cookies. Both the client and server use HTTP_COOKIE_AUTH to store
575 **  the representation of the authorization value, which is actually
576 **  just a base64 hash of username:password. (This entire method is
577 **  a cookie replacement of basic authentication.)
578 **/
579
580 extern void xmlrpc_authcookie_set(xmlrpc_env * env,
581                                   const char * username,
582                                   const char * password);
583
584 char *xmlrpc_authcookie(void);
585
586 #ifdef __cplusplus
587 }
588 #endif
589
590 /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
591 **
592 ** Redistribution and use in source and binary forms, with or without
593 ** modification, are permitted provided that the following conditions
594 ** are met:
595 ** 1. Redistributions of source code must retain the above copyright
596 **    notice, this list of conditions and the following disclaimer.
597 ** 2. Redistributions in binary form must reproduce the above copyright
598 **    notice, this list of conditions and the following disclaimer in the
599 **    documentation and/or other materials provided with the distribution.
600 ** 3. The name of the author may not be used to endorse or promote products
601 **    derived from this software without specific prior written permission. 
602 **  
603 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
604 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
605 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
606 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
607 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
608 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
609 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
610 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
611 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
612 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
613 ** SUCH DAMAGE. */
614
615 #endif
616