summaryrefslogtreecommitdiffstats
path: root/Utilities/cmxmlrpc/xmlrpc_support.c
blob: ac29a12eca6238c660de5e48c9eae457e2c3b9dc (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
/* 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef WIN32
# define vsnprintf _vsnprintf
#endif

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

#ifdef EFENCE
                /* when looking for corruption don't allocate extra slop */
#define BLOCK_ALLOC_MIN (1)
#else
#define BLOCK_ALLOC_MIN (16)
#endif
#define BLOCK_ALLOC_MAX (128 * 1024 * 1024)

#define ERROR_BUFFER_SZ (256)


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

void
xmlrpc_strfree(const char * const string) {
    free((void*)string);
}


/*=========================================================================
**  Assertions and Error Handling
**=========================================================================
**  Support code for XMLRPC_ASSERT and xmlrpc_env.
*/ 

void xmlrpc_assertion_failed (char* file, int line)
{
    fprintf(stderr, "%s:%d: assertion failed\n", file, line);
    abort();
}

static char* default_fault_string = "Not enough memory for error message";

void xmlrpc_env_init (xmlrpc_env* env)
{
    XMLRPC_ASSERT(env != NULL);

    env->fault_occurred = 0;
    env->fault_code     = 0;
    env->fault_string   = NULL;
}

void xmlrpc_env_clean (xmlrpc_env* env)
{
    XMLRPC_ASSERT(env != NULL);

    /* env->fault_string may be one of three things:
    **   1) a NULL pointer
    **   2) a pointer to the default_fault_string
    **   3) a pointer to a malloc'd fault string
    ** If we have case (3), we'll need to free it. */
    if (env->fault_string && env->fault_string != default_fault_string)
        free(env->fault_string);
    env->fault_string = XMLRPC_BAD_POINTER;
}



void 
xmlrpc_env_set_fault(xmlrpc_env * const env, 
                     int          const faultCode, 
                     const char * const faultDescription) {

    XMLRPC_ASSERT(env != NULL); 
    XMLRPC_ASSERT(faultDescription != NULL);

    /* Clean up any leftover pointers. */
    xmlrpc_env_clean(env);

    env->fault_occurred = 1;
    env->fault_code     = faultCode;

    /* Try to copy the fault string. If this fails, use a default. */
    env->fault_string = (char*) malloc(strlen(faultDescription) + 1);
    if (env->fault_string)
        strcpy(env->fault_string, faultDescription);
    else
        env->fault_string = default_fault_string;
}



void 
xmlrpc_env_set_fault_formatted (xmlrpc_env * const envP, 
                                int          const code,
                                const char * const format, 
                                ...) {
    va_list args;
    char buffer[ERROR_BUFFER_SZ];

    XMLRPC_ASSERT(envP != NULL);
    XMLRPC_ASSERT(format != NULL);

    /* Print our error message to the buffer. */
    va_start(args, format);
    vsnprintf(buffer, ERROR_BUFFER_SZ, format, args);
    va_end(args);

    /* vsnprintf is guaranteed to terminate the buffer, but we're paranoid. */
    buffer[ERROR_BUFFER_SZ - 1] = '\0';

    /* Set the fault. */
    xmlrpc_env_set_fault(envP, code, buffer);
}



void xmlrpc_fatal_error (char* file, int line, char* msg)
{
    fprintf(stderr, "%s:%d: %s\n", file, line, msg);
    exit(1);
}


/*=========================================================================
**  Resource Limits
**=========================================================================
*/ 

static size_t limits[XMLRPC_LAST_LIMIT_ID + 1] = {
    XMLRPC_NESTING_LIMIT_DEFAULT,
    XMLRPC_XML_SIZE_LIMIT_DEFAULT
};

void xmlrpc_limit_set (int limit_id, size_t value)
{
    XMLRPC_ASSERT(0 <= limit_id && limit_id <= XMLRPC_LAST_LIMIT_ID);
    limits[limit_id] = value;
}

size_t xmlrpc_limit_get (int limit_id)
{
    XMLRPC_ASSERT(0 <= limit_id && limit_id <= XMLRPC_LAST_LIMIT_ID);
    return limits[limit_id];
}


/*=========================================================================
**  xmlrpc_mem_block
**=========================================================================
**  We support resizable blocks of memory. We need these just about
**  everywhere.
*/

xmlrpc_mem_block * 
xmlrpc_mem_block_new(xmlrpc_env * const env, 
                     size_t       const size) {
    xmlrpc_mem_block* block;

    XMLRPC_ASSERT_ENV_OK(env);

    block = (xmlrpc_mem_block*) malloc(sizeof(xmlrpc_mem_block));
    XMLRPC_FAIL_IF_NULL(block, env, XMLRPC_INTERNAL_ERROR,
                        "Can't allocate memory block");

    xmlrpc_mem_block_init(env, block, size);
    XMLRPC_FAIL_IF_FAULT(env);

                     cleanup:
    if (env->fault_occurred) {
        if (block)
            free(block);
        return NULL;
    } else {
        return block;
    }
}

/* Destroy an existing xmlrpc_mem_block, and everything it contains. */
void xmlrpc_mem_block_free (xmlrpc_mem_block* block)
{
    XMLRPC_ASSERT(block != NULL);
    XMLRPC_ASSERT(block->_block != NULL);

    xmlrpc_mem_block_clean(block);
    free(block);
}

/* Initialize the contents of the provided xmlrpc_mem_block. */
void xmlrpc_mem_block_init (xmlrpc_env* env,
                            xmlrpc_mem_block* block,
                            size_t size)
{
    XMLRPC_ASSERT_ENV_OK(env);
    XMLRPC_ASSERT(block != NULL);

    block->_size = size;
    if (size < BLOCK_ALLOC_MIN)
        block->_allocated = BLOCK_ALLOC_MIN;
    else
        block->_allocated = size;

    block->_block = (void*) malloc(block->_allocated);
    if (!block->_block)
        xmlrpc_env_set_fault_formatted(
            env, XMLRPC_INTERNAL_ERROR,
            "Can't allocate %u-byte memory block",
            block->_allocated);
}

/* Deallocate the contents of the provided xmlrpc_mem_block, but not the
** block itself. */
void xmlrpc_mem_block_clean (xmlrpc_mem_block* block)
{
    XMLRPC_ASSERT(block != NULL);
    XMLRPC_ASSERT(block->_block != NULL);

    free(block->_block);
    block->_block = XMLRPC_BAD_POINTER;
}



/* Get the size of the xmlrpc_mem_block. */
size_t 
xmlrpc_mem_block_size(const xmlrpc_mem_block * const block) {

    XMLRPC_ASSERT(block != NULL);
    return block->_size;
}



/* Get the contents of the xmlrpc_mem_block. */
void * 
xmlrpc_mem_block_contents(const xmlrpc_mem_block * const block) {

    XMLRPC_ASSERT(block != NULL);
    return block->_block;
}



/* Resize an xmlrpc_mem_block, preserving as much of the contents as
** possible. */
void 
xmlrpc_mem_block_resize (xmlrpc_env *       const env,
                         xmlrpc_mem_block * const block,
                         size_t             const size) {

    size_t proposed_alloc;
    void* new_block;

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

    /* Check to see if we already have enough space. Maybe we'll get lucky. */
    if (size <= block->_allocated) {
        block->_size = size;
        return;
    }

    /* Calculate a new allocation size. */
#ifdef EFENCE
    proposed_alloc = size;
#else
    proposed_alloc = block->_allocated;
    while (proposed_alloc < size && proposed_alloc <= BLOCK_ALLOC_MAX)
        proposed_alloc *= 2;
#endif /* DEBUG_MEM_ERRORS */

    if (proposed_alloc > BLOCK_ALLOC_MAX)
        XMLRPC_FAIL(env, XMLRPC_INTERNAL_ERROR, "Memory block too large");

    /* Allocate our new memory block. */
    new_block = (void*) malloc(proposed_alloc);
    XMLRPC_FAIL_IF_NULL(new_block, env, XMLRPC_INTERNAL_ERROR,
                        "Can't resize memory block");

    /* Copy over our data and update the xmlrpc_mem_block struct. */
    memcpy(new_block, block->_block, block->_size);
    free(block->_block);
    block->_block     = new_block;
    block->_size      = size;
    block->_allocated = proposed_alloc;

 cleanup:
    return;
}

/* Append data to an existing xmlrpc_mem_block. */
void 
xmlrpc_mem_block_append(xmlrpc_env *       const env,
                        xmlrpc_mem_block * const block,
                        void *             const data, 
                        size_t             const len)
{
    size_t size;

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

    size = block->_size;
    xmlrpc_mem_block_resize(env, block, size + len);
    XMLRPC_FAIL_IF_FAULT(env);

    memcpy(((unsigned char*) block->_block) + size, data, len);

 cleanup:
    return;
}



void
xmlrpc_traceXml(const char * const label, 
                const char * const xml,
                unsigned int const xmlLength) {

    if (getenv("XMLRPC_TRACE_XML")) {
        unsigned int nonPrintableCount;
        unsigned int i;

        nonPrintableCount = 0;  /* Initial value */

        for (i = 0; i < xmlLength; ++i) {
            if (!isprint((int)(xml[i])) && xml[i] != '\n' && xml[i] != '\r')
                ++nonPrintableCount;
        }
        if (nonPrintableCount > 0)
            fprintf(stderr, "%s contains %u nonprintable characters.\n", 
                    label, nonPrintableCount);

        fprintf(stderr, "%s:\n %.*s\n", label, (int)xmlLength, xml);
    }
}