1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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 * $Id: getpart.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
22 ***************************************************************************/
28 #define _MPRINTF_REPLACE /* use our functions only */
29 #include <curl/mprintf.h>
31 /* just to please base64.h we create a fake struct */
32 struct SessionHandle
{
36 #include "curl_base64.h"
38 /* include memdebug.h last */
41 #define EAT_SPACE(ptr) while( ptr && *ptr && ISSPACE(*ptr) ) ptr++
42 #define EAT_WORD(ptr) while( ptr && *ptr && !ISSPACE(*ptr) && \
46 #define show(x) printf x
51 curl_malloc_callback Curl_cmalloc
= (curl_malloc_callback
)malloc
;
52 curl_free_callback Curl_cfree
= (curl_free_callback
)free
;
53 curl_realloc_callback Curl_crealloc
= (curl_realloc_callback
)realloc
;
54 curl_strdup_callback Curl_cstrdup
= (curl_strdup_callback
)strdup
;
55 curl_calloc_callback Curl_ccalloc
= (curl_calloc_callback
)calloc
;
58 char *appendstring(char *string
, /* original string */
59 char *buffer
, /* to append */
60 size_t *stringlen
, /* length of string */
61 size_t *stralloc
, /* allocated size */
62 char base64
) /* 1 if base64 encoded */
64 size_t len
= strlen(buffer
);
65 size_t needed_len
= len
+ *stringlen
+ 1;
69 /* decode the given buffer first */
70 len
= Curl_base64_decode(buffer
, (unsigned char**)&buf64
); /* updated len */
72 needed_len
= len
+ *stringlen
+ 1; /* recalculate */
75 if(needed_len
>= *stralloc
) {
77 size_t newsize
= needed_len
*2; /* get twice the needed size */
79 newptr
= realloc(string
, newsize
);
90 /* memcpy to support binary blobs */
91 memcpy(&string
[*stringlen
], buffer
, len
);
101 const char *spitout(FILE *stream
,
103 const char *sub
, size_t *size
)
105 char buffer
[8192]; /* big enough for anything */
106 char cmain
[128]=""; /* current main section */
107 char csub
[128]=""; /* current sub section */
115 char base64
= 0; /* set to 1 if true */
123 } state
= STATE_OUTSIDE
;
125 string
= (char *)malloc(stralloc
);
129 string
[0] = 0; /* zero first byte in case of no data */
131 while(fgets(buffer
, sizeof(buffer
), stream
)) {
135 /* pass white spaces */
140 show(("=> %s", buffer
));
141 string
= appendstring(string
, buffer
, &stringlen
, &stralloc
, base64
);
142 show(("* %s\n", buffer
));
151 /* end of a section */
159 if((state
== STATE_INSUB
) &&
160 !strcmp(csub
, ptr
)) {
161 /* this is the end of the currently read sub section */
163 csub
[0]=0; /* no sub anymore */
166 else if((state
== STATE_INMAIN
) &&
167 !strcmp(cmain
, ptr
)) {
168 /* this is the end of the currently read main section */
170 cmain
[0]=0; /* no main anymore */
173 else if(state
== STATE_OUTER
) {
174 /* this is the end of the outermost file section */
179 /* this is the beginning of a section */
186 /* Skip over the outermost element (<testcase>), but if it turns out
187 to be a comment, completely ignore it below */
193 state
= STATE_INMAIN
;
204 /* There might be attributes here. Check for those we know of and care
206 if(strstr(&end
[1], "base64=")) {
207 /* rough and dirty, but "mostly" functional */
208 /* Treat all data as base64 encoded */
214 string
= appendstring(string
, buffer
, &stringlen
, &stralloc
, base64
);
215 show(("* %s\n", buffer
));
218 if((STATE_INSUB
== state
) &&
219 !strcmp(cmain
, main
) &&
220 !strcmp(csub
, sub
)) {
221 show(("* (%d bytes) %s\n", stringlen
, buffer
));
222 display
= 1; /* start displaying */
224 else if ((*cmain
== '?') || (*cmain
== '!') || (*csub
== '!')) {
225 /* Ignore comments, DOCTYPEs and XML declarations */
226 show(("%d ignoring (%s/%s)\n", state
, cmain
, csub
));
230 show(("%d (%s/%s): %s\n", state
, cmain
, csub
, buffer
));
231 display
= 0; /* no display */