1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 */
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.
214 return GPE_OUT_OF_MEMORY;
217 /* memcpy to support binary blobs */
218 memcpy(*buf, buf64, src_len);
220 *(*buf + src_len) = '\0';
230 * This returns whole contents of specified XML-like section and subsection
231 * from the given file. This is mostly used to retrieve a specific part from
232 * a test definition file for consumption by test suite servers.
234 * Data is returned in a dynamically allocated buffer, a pointer to this data
235 * and the size of the data is stored at the addresses that caller specifies.
237 * If the returned data is a string the returned size will be the length of
238 * the string excluding null termination. Otherwise it will just be the size
239 * of the returned binary data.
241 * Calling function is responsible to free returned buffer.
243 * This function may return:
244 * GPE_NO_BUFFER_SPACE
249 int getpart(char **outbuf, size_t *outlen,
250 const char *main, const char *sub, FILE *stream)
252 # define MAX_TAG_LEN 79
253 char couter[MAX_TAG_LEN+1]; /* current outermost section */
254 char cmain[MAX_TAG_LEN+1]; /* current main section */
255 char csub[MAX_TAG_LEN+1]; /* current sub section */
256 char ptag[MAX_TAG_LEN+1]; /* potential tag */
257 char patt[MAX_TAG_LEN+1]; /* potential attributes */
266 size_t outalloc = 256;
267 int in_wanted_part = 0;
277 } state = STATE_OUTSIDE;
280 *outbuf = malloc(outalloc);
282 return GPE_OUT_OF_MEMORY;
285 couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0';
287 while((error = readline(&buffer, &bufsize, stream)) == GPE_OK) {
294 show(("=> %s", buffer));
295 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
306 ** closing section tag
312 if((len.sig = end - ptr) > MAX_TAG_LEN) {
313 error = GPE_NO_BUFFER_SPACE;
316 memcpy(ptag, ptr, len.uns);
317 ptag[len.uns] = '\0';
319 if((STATE_INSUB == state) && !strcmp(csub, ptag)) {
320 /* end of current sub section */
321 state = STATE_INMAIN;
324 /* end of wanted part */
327 /* Do we need to base64 decode the data? */
329 error = decodedata(outbuf, outlen);
336 else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) {
337 /* end of current main section */
341 /* end of wanted part */
344 /* Do we need to base64 decode the data? */
346 error = decodedata(outbuf, outlen);
353 else if((STATE_OUTER == state) && !strcmp(couter, ptag)) {
354 /* end of outermost file section */
355 state = STATE_OUTSIDE;
358 /* end of wanted part */
365 else if(!in_wanted_part) {
367 ** opening section tag
370 /* get potential tag */
373 if((len.sig = end - ptr) > MAX_TAG_LEN) {
374 error = GPE_NO_BUFFER_SPACE;
377 memcpy(ptag, ptr, len.uns);
378 ptag[len.uns] = '\0';
380 /* ignore comments, doctypes and xml declarations */
381 if(('!' == ptag[0]) || ('?' == ptag[0])) {
382 show(("* ignoring (%s)", buffer));
386 /* get all potential attributes */
390 while(*end && ('>' != *end))
392 if((len.sig = end - ptr) > MAX_TAG_LEN) {
393 error = GPE_NO_BUFFER_SPACE;
396 memcpy(patt, ptr, len.uns);
397 patt[len.uns] = '\0';
399 if(STATE_OUTSIDE == state) {
400 /* outermost element (<testcase>) */
401 strcpy(couter, ptag);
405 else if(STATE_OUTER == state) {
406 /* start of a main section */
408 state = STATE_INMAIN;
411 else if(STATE_INMAIN == state) {
412 /* start of a sub section */
415 if(!strcmp(cmain, main) && !strcmp(csub, sub)) {
416 /* start of wanted part */
418 if(strstr(patt, "base64="))
419 /* bit rough test, but "mostly" functional, */
420 /* treat wanted part data as base64 encoded */
429 show(("=> %s", buffer));
430 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
439 if(error != GPE_OK) {
440 if(error == GPE_END_OF_FILE)