1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 ***************************************************************************/
23 #define CURL_NO_OLDIES
29 #define ENABLE_CURLX_PRINTF
30 /* make the curlx header define all printf() functions to use the curlx_*
32 #include "curlx.h" /* from the private lib dir */
34 /* just to please base64.h we create a fake struct */
35 struct SessionHandle {
39 #include "curl_base64.h"
40 #include "curl_memory.h"
42 /* include memdebug.h last */
45 #define EAT_SPACE(p) while(*(p) && ISSPACE(*(p))) (p)++
47 #define EAT_WORD(p) while(*(p) && !ISSPACE(*(p)) && ('>' != *(p))) (p)++
50 #define show(x) printf x
55 #if defined(_MSC_VER) && defined(_DLL)
56 # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
59 curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
60 curl_free_callback Curl_cfree = (curl_free_callback)free;
61 curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
62 curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
63 curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
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, src_len;
157 unsigned char *as_uchar;
161 src_len = strlen(src_buf);
165 buf64.as_char = NULL;
168 /* base64 decode the given buffer */
169 src_len = Curl_base64_decode(src_buf, &buf64.as_uchar);
170 src_buf = buf64.as_char;
171 if(!src_len || !src_buf) {
173 ** currently there is no way to tell apart an OOM condition in
174 ** Curl_base64_decode() from zero length decoded data. For now,
175 ** let's just assume it is an OOM condition, currently we have
176 ** no input for this function that decodes to zero length data.
180 return GPE_OUT_OF_MEMORY;
184 need_alloc = src_len + *dst_len + 1;
186 /* enlarge destination buffer if required */
187 if(need_alloc > *dst_alloc) {
188 size_t newsize = need_alloc * 2;
189 char *newptr = realloc(*dst_buf, newsize);
193 return GPE_OUT_OF_MEMORY;
195 *dst_alloc = newsize;
199 /* memcpy to support binary blobs */
200 memcpy(*dst_buf + *dst_len, src_buf, src_len);
202 *(*dst_buf + *dst_len) = '\0';
213 * This returns whole contents of specified XML-like section and subsection
214 * from the given file. This is mostly used to retrieve a specific part from
215 * a test definition file for consumption by test suite servers.
217 * Data is returned in a dynamically allocated buffer, a pointer to this data
218 * and the size of the data is stored at the addresses that caller specifies.
220 * If the returned data is a string the returned size will be the length of
221 * the string excluding null termination. Otherwise it will just be the size
222 * of the returned binary data.
224 * Calling function is responsible to free returned buffer.
226 * This function may return:
227 * GPE_NO_BUFFER_SPACE
232 int getpart(char **outbuf, size_t *outlen,
233 const char *main, const char *sub, FILE *stream)
235 # define MAX_TAG_LEN 79
236 char couter[MAX_TAG_LEN+1]; /* current outermost section */
237 char cmain[MAX_TAG_LEN+1]; /* current main section */
238 char csub[MAX_TAG_LEN+1]; /* current sub section */
239 char ptag[MAX_TAG_LEN+1]; /* potential tag */
240 char patt[MAX_TAG_LEN+1]; /* potential attributes */
249 size_t outalloc = 256;
250 int in_wanted_part = 0;
260 } state = STATE_OUTSIDE;
263 *outbuf = malloc(outalloc);
265 return GPE_OUT_OF_MEMORY;
268 couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0';
270 while((error = readline(&buffer, &bufsize, stream)) == GPE_OK) {
277 show(("=> %s", buffer));
278 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
289 ** closing section tag
295 if((len.sig = end - ptr) > MAX_TAG_LEN) {
296 error = GPE_NO_BUFFER_SPACE;
299 memcpy(ptag, ptr, len.uns);
300 ptag[len.uns] = '\0';
302 if((STATE_INSUB == state) && !strcmp(csub, ptag)) {
303 /* end of current sub section */
304 state = STATE_INMAIN;
307 /* end of wanted part */
312 else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) {
313 /* end of current main section */
317 /* end of wanted part */
322 else if((STATE_OUTER == state) && !strcmp(couter, ptag)) {
323 /* end of outermost file section */
324 state = STATE_OUTSIDE;
327 /* end of wanted part */
334 else if(!in_wanted_part) {
336 ** opening section tag
339 /* get potential tag */
342 if((len.sig = end - ptr) > MAX_TAG_LEN) {
343 error = GPE_NO_BUFFER_SPACE;
346 memcpy(ptag, ptr, len.uns);
347 ptag[len.uns] = '\0';
349 /* ignore comments, doctypes and xml declarations */
350 if(('!' == ptag[0]) || ('?' == ptag[0])) {
351 show(("* ignoring (%s)", buffer));
355 /* get all potential attributes */
359 while(*end && ('>' != *end))
361 if((len.sig = end - ptr) > MAX_TAG_LEN) {
362 error = GPE_NO_BUFFER_SPACE;
365 memcpy(patt, ptr, len.uns);
366 patt[len.uns] = '\0';
368 if(STATE_OUTSIDE == state) {
369 /* outermost element (<testcase>) */
370 strcpy(couter, ptag);
374 else if(STATE_OUTER == state) {
375 /* start of a main section */
377 state = STATE_INMAIN;
380 else if(STATE_INMAIN == state) {
381 /* start of a sub section */
384 if(!strcmp(cmain, main) && !strcmp(csub, sub)) {
385 /* start of wanted part */
387 if(strstr(patt, "base64="))
388 /* bit rough test, but "mostly" functional, */
389 /* treat wanted part data as base64 encoded */
398 show(("=> %s", buffer));
399 error = appenddata(outbuf, outlen, &outalloc, buffer, base64);
409 if(error != GPE_OK) {
410 if(error == GPE_END_OF_FILE)