1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "server_setup.h"
26 #define ENABLE_CURLX_PRINTF
27 /* make the curlx header define all printf() functions to use the curlx_*
29 #include "curlx.h" /* from the private lib dir */
31 /* just to please curl_base64.h we create a fake struct */
32 struct SessionHandle {
36 #include "curl_base64.h"
37 #include "curl_memory.h"
39 /* include memdebug.h last */
42 #define EAT_SPACE(p) while(*(p) && ISSPACE(*(p))) (p)++
44 #define EAT_WORD(p) while(*(p) && !ISSPACE(*(p)) && ('>' != *(p))) (p)++
47 #define show(x) printf x
49 #define show(x) Curl_nop_stmt
52 #if defined(_MSC_VER) && defined(_DLL)
53 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
56 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
57 curl_free_callback Curl_cfree = (curl_free_callback)free;
58 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
59 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
60 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
61 #if defined(WIN32) && defined(UNICODE)
62 curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
65 #if defined(_MSC_VER) && defined(_DLL)
66 # pragma warning(default:4232) /* MSVC extension, dllimport identity */
72 * Reads a complete line from a file into a dynamically allocated buffer.
74 * Calling function may call this multiple times with same 'buffer'
75 * and 'bufsize' pointers to avoid multiple buffer allocations. Buffer
76 * will be reallocated and 'bufsize' increased until whole line fits in
77 * buffer before returning it.
79 * Calling function is responsible to free allocated buffer.
81 * This function may return:
87 static int readline(char **buffer, size_t *bufsize, FILE *stream)
94 *buffer = malloc(128);
96 return GPE_OUT_OF_MEMORY;
101 int bytestoread = curlx_uztosi(*bufsize - offset);
103 if(!fgets(*buffer + offset, bytestoread, stream))
104 return (offset != 0) ? GPE_OK : GPE_END_OF_FILE ;
106 length = offset + strlen(*buffer + offset);
107 if(*(*buffer + length - 1) == '\n')
110 if(length < *bufsize - 1)
113 newptr = realloc(*buffer, *bufsize * 2);
115 return GPE_OUT_OF_MEMORY;
126 * This appends data from a given source buffer to the end of the used part of
127 * a destination buffer. Arguments relative to the destination buffer are, the
128 * address of a pointer to the destination buffer 'dst_buf', the length of data
129 * in destination buffer excluding potential null string termination 'dst_len',
130 * the allocated size of destination buffer 'dst_alloc'. All three destination
131 * buffer arguments may be modified by this function. Arguments relative to the
132 * source buffer are, a pointer to the source buffer 'src_buf' and indication
133 * whether the source buffer is base64 encoded or not 'src_b64'.
135 * If the source buffer is indicated to be base64 encoded, this appends the
136 * decoded data, binary or whatever, to the destination. The source buffer
137 * may not hold binary data, only a null terminated string is valid content.
139 * Destination buffer will be enlarged and relocated as needed.
141 * Calling function is responsible to provide preallocated destination
142 * buffer and also to deallocate it when no longer needed.
144 * This function may return:
149 static int appenddata(char **dst_buf, /* dest buffer */
150 size_t *dst_len, /* dest buffer data length */
151 size_t *dst_alloc, /* dest buffer allocated size */
152 char *src_buf, /* source buffer */
153 int src_b64) /* != 0 if source is base64 encoded */
155 size_t need_alloc = 0;
156 size_t src_len = strlen(src_buf);
161 need_alloc = src_len + *dst_len + 1;
164 if(src_buf[src_len - 1] == '\r')
167 if(src_buf[src_len - 1] == '\n')
171 /* enlarge destination buffer if required */
172 if(need_alloc > *dst_alloc) {
173 size_t newsize = need_alloc * 2;
174 char *newptr = realloc(*dst_buf, newsize);
176 return GPE_OUT_OF_MEMORY;
178 *dst_alloc = newsize;
182 /* memcpy to support binary blobs */
183 memcpy(*dst_buf + *dst_len, src_buf, src_len);
185 *(*dst_buf + *dst_len) = '\0';
190 static int decodedata(char **buf, /* dest buffer */
191 size_t *len) /* dest buffer data length */
194 unsigned char *buf64 = NULL;
200 /* base64 decode the given buffer */
201 error = (int) Curl_base64_decode(*buf, &buf64, &src_len);
203 return GPE_OUT_OF_MEMORY;
207 ** currently there is no way to tell apart an OOM condition in
208 ** Curl_base64_decode() from zero length decoded data. For now,
209 ** let's just assume it is an OOM condition, currently we have
210 ** no input for this function that decodes to zero length data.
215 return GPE_OUT_OF_MEMORY;
218 /* memcpy to support binary blobs */
219 memcpy(*buf, buf64, src_len);
221 *(*buf + src_len) = '\0';
231 * This returns whole contents of specified XML-like section and subsection
232 * from the given file. This is mostly used to retrieve a specific part from
233 * a test definition file for consumption by test suite servers.
235 * Data is returned in a dynamically allocated buffer, a pointer to this data
236 * and the size of the data is stored at the addresses that caller specifies.
238 * If the returned data is a string the returned size will be the length of
239 * the string excluding null termination. Otherwise it will just be the size
240 * of the returned binary data.
242 * Calling function is responsible to free returned buffer.
244 * This function may return:
245 * GPE_NO_BUFFER_SPACE
250 int getpart(char **outbuf, size_t *outlen,
251 const char *main, const char *sub, FILE *stream)
253 # define MAX_TAG_LEN 79
254 char couter[MAX_TAG_LEN+1]; /* current outermost section */
255 char cmain[MAX_TAG_LEN+1]; /* current main section */
256 char csub[MAX_TAG_LEN+1]; /* current sub section */
257 char ptag[MAX_TAG_LEN+1]; /* potential tag */
258 char patt[MAX_TAG_LEN+1]; /* potential attributes */
267 size_t outalloc = 256;
268 int in_wanted_part = 0;
278 } state = STATE_OUTSIDE;
281 *outbuf = malloc(outalloc);
283 return GPE_OUT_OF_MEMORY;
286 couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0';
288 while((error = readline(&buffer, &bufsize, stream)) == GPE_OK) {
295 show(("=> %s", buffer));
296 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
307 ** closing section tag
313 if((len.sig = end - ptr) > MAX_TAG_LEN) {
314 error = GPE_NO_BUFFER_SPACE;
317 memcpy(ptag, ptr, len.uns);
318 ptag[len.uns] = '\0';
320 if((STATE_INSUB == state) && !strcmp(csub, ptag)) {
321 /* end of current sub section */
322 state = STATE_INMAIN;
325 /* end of wanted part */
328 /* Do we need to base64 decode the data? */
330 error = decodedata(outbuf, outlen);
337 else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) {
338 /* end of current main section */
342 /* end of wanted part */
345 /* Do we need to base64 decode the data? */
347 error = decodedata(outbuf, outlen);
354 else if((STATE_OUTER == state) && !strcmp(couter, ptag)) {
355 /* end of outermost file section */
356 state = STATE_OUTSIDE;
359 /* end of wanted part */
366 else if(!in_wanted_part) {
368 ** opening section tag
371 /* get potential tag */
374 if((len.sig = end - ptr) > MAX_TAG_LEN) {
375 error = GPE_NO_BUFFER_SPACE;
378 memcpy(ptag, ptr, len.uns);
379 ptag[len.uns] = '\0';
381 /* ignore comments, doctypes and xml declarations */
382 if(('!' == ptag[0]) || ('?' == ptag[0])) {
383 show(("* ignoring (%s)", buffer));
387 /* get all potential attributes */
391 while(*end && ('>' != *end))
393 if((len.sig = end - ptr) > MAX_TAG_LEN) {
394 error = GPE_NO_BUFFER_SPACE;
397 memcpy(patt, ptr, len.uns);
398 patt[len.uns] = '\0';
400 if(STATE_OUTSIDE == state) {
401 /* outermost element (<testcase>) */
402 strcpy(couter, ptag);
406 else if(STATE_OUTER == state) {
407 /* start of a main section */
409 state = STATE_INMAIN;
412 else if(STATE_INMAIN == state) {
413 /* start of a sub section */
416 if(!strcmp(cmain, main) && !strcmp(csub, sub)) {
417 /* start of wanted part */
419 if(strstr(patt, "base64="))
420 /* bit rough test, but "mostly" functional, */
421 /* treat wanted part data as base64 encoded */
430 show(("=> %s", buffer));
431 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
441 if(error != GPE_OK) {
442 if(error == GPE_END_OF_FILE)