lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * |
| 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. |
| 13 | * |
| 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. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | #include "server_setup.h" |
| 23 | |
| 24 | #include "getpart.h" |
| 25 | |
| 26 | #define ENABLE_CURLX_PRINTF |
| 27 | /* make the curlx header define all printf() functions to use the curlx_* |
| 28 | versions instead */ |
| 29 | #include "curlx.h" /* from the private lib dir */ |
| 30 | |
| 31 | /* just to please curl_base64.h we create a fake struct */ |
| 32 | struct Curl_easy { |
| 33 | int fake; |
| 34 | }; |
| 35 | |
| 36 | #include "curl_base64.h" |
| 37 | #include "curl_memory.h" |
| 38 | |
| 39 | /* include memdebug.h last */ |
| 40 | #include "memdebug.h" |
| 41 | |
| 42 | #define EAT_SPACE(p) while(*(p) && ISSPACE(*(p))) (p)++ |
| 43 | |
| 44 | #define EAT_WORD(p) while(*(p) && !ISSPACE(*(p)) && ('>' != *(p))) (p)++ |
| 45 | |
| 46 | #ifdef DEBUG_GETPART |
| 47 | #define show(x) printf x |
| 48 | #else |
| 49 | #define show(x) Curl_nop_stmt |
| 50 | #endif |
| 51 | |
| 52 | #if defined(_MSC_VER) && defined(_DLL) |
| 53 | # pragma warning(disable:4232) /* MSVC extension, dllimport identity */ |
| 54 | #endif |
| 55 | |
| 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; |
| 63 | #endif |
| 64 | |
| 65 | #if defined(_MSC_VER) && defined(_DLL) |
| 66 | # pragma warning(default:4232) /* MSVC extension, dllimport identity */ |
| 67 | #endif |
| 68 | |
| 69 | /* |
| 70 | * readline() |
| 71 | * |
| 72 | * Reads a complete line from a file into a dynamically allocated buffer. |
| 73 | * |
| 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. |
| 78 | * |
| 79 | * Calling function is responsible to free allocated buffer. |
| 80 | * |
| 81 | * This function may return: |
| 82 | * GPE_OUT_OF_MEMORY |
| 83 | * GPE_END_OF_FILE |
| 84 | * GPE_OK |
| 85 | */ |
| 86 | |
| 87 | static int readline(char **buffer, size_t *bufsize, FILE *stream) |
| 88 | { |
| 89 | size_t offset = 0; |
| 90 | size_t length; |
| 91 | char *newptr; |
| 92 | |
| 93 | if(!*buffer) { |
| 94 | *buffer = malloc(128); |
| 95 | if(!*buffer) |
| 96 | return GPE_OUT_OF_MEMORY; |
| 97 | *bufsize = 128; |
| 98 | } |
| 99 | |
| 100 | for(;;) { |
| 101 | int bytestoread = curlx_uztosi(*bufsize - offset); |
| 102 | |
| 103 | if(!fgets(*buffer + offset, bytestoread, stream)) |
| 104 | return (offset != 0) ? GPE_OK : GPE_END_OF_FILE; |
| 105 | |
| 106 | length = offset + strlen(*buffer + offset); |
| 107 | if(*(*buffer + length - 1) == '\n') |
| 108 | break; |
| 109 | offset = length; |
| 110 | if(length < *bufsize - 1) |
| 111 | continue; |
| 112 | |
| 113 | newptr = realloc(*buffer, *bufsize * 2); |
| 114 | if(!newptr) |
| 115 | return GPE_OUT_OF_MEMORY; |
| 116 | *buffer = newptr; |
| 117 | *bufsize *= 2; |
| 118 | } |
| 119 | |
| 120 | return GPE_OK; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * appenddata() |
| 125 | * |
| 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'. |
| 134 | * |
| 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. |
| 138 | * |
| 139 | * Destination buffer will be enlarged and relocated as needed. |
| 140 | * |
| 141 | * Calling function is responsible to provide preallocated destination |
| 142 | * buffer and also to deallocate it when no longer needed. |
| 143 | * |
| 144 | * This function may return: |
| 145 | * GPE_OUT_OF_MEMORY |
| 146 | * GPE_OK |
| 147 | */ |
| 148 | |
| 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 */ |
| 154 | { |
| 155 | size_t need_alloc = 0; |
| 156 | size_t src_len = strlen(src_buf); |
| 157 | |
| 158 | if(!src_len) |
| 159 | return GPE_OK; |
| 160 | |
| 161 | need_alloc = src_len + *dst_len + 1; |
| 162 | |
| 163 | if(src_b64) { |
| 164 | if(src_buf[src_len - 1] == '\r') |
| 165 | src_len--; |
| 166 | |
| 167 | if(src_buf[src_len - 1] == '\n') |
| 168 | src_len--; |
| 169 | } |
| 170 | |
| 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); |
| 175 | if(!newptr) { |
| 176 | return GPE_OUT_OF_MEMORY; |
| 177 | } |
| 178 | *dst_alloc = newsize; |
| 179 | *dst_buf = newptr; |
| 180 | } |
| 181 | |
| 182 | /* memcpy to support binary blobs */ |
| 183 | memcpy(*dst_buf + *dst_len, src_buf, src_len); |
| 184 | *dst_len += src_len; |
| 185 | *(*dst_buf + *dst_len) = '\0'; |
| 186 | |
| 187 | return GPE_OK; |
| 188 | } |
| 189 | |
| 190 | static int decodedata(char **buf, /* dest buffer */ |
| 191 | size_t *len) /* dest buffer data length */ |
| 192 | { |
| 193 | CURLcode error = CURLE_OK; |
| 194 | unsigned char *buf64 = NULL; |
| 195 | size_t src_len = 0; |
| 196 | |
| 197 | if(!*len) |
| 198 | return GPE_OK; |
| 199 | |
| 200 | /* base64 decode the given buffer */ |
| 201 | error = Curl_base64_decode(*buf, &buf64, &src_len); |
| 202 | if(error) |
| 203 | return GPE_OUT_OF_MEMORY; |
| 204 | |
| 205 | if(!src_len) { |
| 206 | /* |
| 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. |
| 211 | */ |
| 212 | free(buf64); |
| 213 | |
| 214 | return GPE_OUT_OF_MEMORY; |
| 215 | } |
| 216 | |
| 217 | /* memcpy to support binary blobs */ |
| 218 | memcpy(*buf, buf64, src_len); |
| 219 | *len = src_len; |
| 220 | *(*buf + src_len) = '\0'; |
| 221 | |
| 222 | free(buf64); |
| 223 | |
| 224 | return GPE_OK; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * getpart() |
| 229 | * |
| 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. |
| 233 | * |
| 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. |
| 236 | * |
| 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. |
| 240 | * |
| 241 | * Calling function is responsible to free returned buffer. |
| 242 | * |
| 243 | * This function may return: |
| 244 | * GPE_NO_BUFFER_SPACE |
| 245 | * GPE_OUT_OF_MEMORY |
| 246 | * GPE_OK |
| 247 | */ |
| 248 | |
| 249 | int getpart(char **outbuf, size_t *outlen, |
| 250 | const char *main, const char *sub, FILE *stream) |
| 251 | { |
| 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 */ |
| 258 | char *buffer = NULL; |
| 259 | char *ptr; |
| 260 | char *end; |
| 261 | union { |
| 262 | ssize_t sig; |
| 263 | size_t uns; |
| 264 | } len; |
| 265 | size_t bufsize = 0; |
| 266 | size_t outalloc = 256; |
| 267 | int in_wanted_part = 0; |
| 268 | int base64 = 0; |
| 269 | int error; |
| 270 | |
| 271 | enum { |
| 272 | STATE_OUTSIDE = 0, |
| 273 | STATE_OUTER = 1, |
| 274 | STATE_INMAIN = 2, |
| 275 | STATE_INSUB = 3, |
| 276 | STATE_ILLEGAL = 4 |
| 277 | } state = STATE_OUTSIDE; |
| 278 | |
| 279 | *outlen = 0; |
| 280 | *outbuf = malloc(outalloc); |
| 281 | if(!*outbuf) |
| 282 | return GPE_OUT_OF_MEMORY; |
| 283 | *(*outbuf) = '\0'; |
| 284 | |
| 285 | couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0'; |
| 286 | |
| 287 | while((error = readline(&buffer, &bufsize, stream)) == GPE_OK) { |
| 288 | |
| 289 | ptr = buffer; |
| 290 | EAT_SPACE(ptr); |
| 291 | |
| 292 | if('<' != *ptr) { |
| 293 | if(in_wanted_part) { |
| 294 | show(("=> %s", buffer)); |
| 295 | error = appenddata(outbuf, outlen, &outalloc, buffer, base64); |
| 296 | if(error) |
| 297 | break; |
| 298 | } |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | ptr++; |
| 303 | |
| 304 | if('/' == *ptr) { |
| 305 | /* |
| 306 | ** closing section tag |
| 307 | */ |
| 308 | |
| 309 | ptr++; |
| 310 | end = ptr; |
| 311 | EAT_WORD(end); |
| 312 | len.sig = end - ptr; |
| 313 | if(len.sig > MAX_TAG_LEN) { |
| 314 | error = GPE_NO_BUFFER_SPACE; |
| 315 | break; |
| 316 | } |
| 317 | memcpy(ptag, ptr, len.uns); |
| 318 | ptag[len.uns] = '\0'; |
| 319 | |
| 320 | if((STATE_INSUB == state) && !strcmp(csub, ptag)) { |
| 321 | /* end of current sub section */ |
| 322 | state = STATE_INMAIN; |
| 323 | csub[0] = '\0'; |
| 324 | if(in_wanted_part) { |
| 325 | /* end of wanted part */ |
| 326 | in_wanted_part = 0; |
| 327 | |
| 328 | /* Do we need to base64 decode the data? */ |
| 329 | if(base64) { |
| 330 | error = decodedata(outbuf, outlen); |
| 331 | if(error) |
| 332 | return error; |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) { |
| 338 | /* end of current main section */ |
| 339 | state = STATE_OUTER; |
| 340 | cmain[0] = '\0'; |
| 341 | if(in_wanted_part) { |
| 342 | /* end of wanted part */ |
| 343 | in_wanted_part = 0; |
| 344 | |
| 345 | /* Do we need to base64 decode the data? */ |
| 346 | if(base64) { |
| 347 | error = decodedata(outbuf, outlen); |
| 348 | if(error) |
| 349 | return error; |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | else if((STATE_OUTER == state) && !strcmp(couter, ptag)) { |
| 355 | /* end of outermost file section */ |
| 356 | state = STATE_OUTSIDE; |
| 357 | couter[0] = '\0'; |
| 358 | if(in_wanted_part) { |
| 359 | /* end of wanted part */ |
| 360 | in_wanted_part = 0; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | else if(!in_wanted_part) { |
| 367 | /* |
| 368 | ** opening section tag |
| 369 | */ |
| 370 | |
| 371 | /* get potential tag */ |
| 372 | end = ptr; |
| 373 | EAT_WORD(end); |
| 374 | len.sig = end - ptr; |
| 375 | if(len.sig > MAX_TAG_LEN) { |
| 376 | error = GPE_NO_BUFFER_SPACE; |
| 377 | break; |
| 378 | } |
| 379 | memcpy(ptag, ptr, len.uns); |
| 380 | ptag[len.uns] = '\0'; |
| 381 | |
| 382 | /* ignore comments, doctypes and xml declarations */ |
| 383 | if(('!' == ptag[0]) || ('?' == ptag[0])) { |
| 384 | show(("* ignoring (%s)", buffer)); |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | /* get all potential attributes */ |
| 389 | ptr = end; |
| 390 | EAT_SPACE(ptr); |
| 391 | end = ptr; |
| 392 | while(*end && ('>' != *end)) |
| 393 | end++; |
| 394 | len.sig = end - ptr; |
| 395 | if(len.sig > MAX_TAG_LEN) { |
| 396 | error = GPE_NO_BUFFER_SPACE; |
| 397 | break; |
| 398 | } |
| 399 | memcpy(patt, ptr, len.uns); |
| 400 | patt[len.uns] = '\0'; |
| 401 | |
| 402 | if(STATE_OUTSIDE == state) { |
| 403 | /* outermost element (<testcase>) */ |
| 404 | strcpy(couter, ptag); |
| 405 | state = STATE_OUTER; |
| 406 | continue; |
| 407 | } |
| 408 | else if(STATE_OUTER == state) { |
| 409 | /* start of a main section */ |
| 410 | strcpy(cmain, ptag); |
| 411 | state = STATE_INMAIN; |
| 412 | continue; |
| 413 | } |
| 414 | else if(STATE_INMAIN == state) { |
| 415 | /* start of a sub section */ |
| 416 | strcpy(csub, ptag); |
| 417 | state = STATE_INSUB; |
| 418 | if(!strcmp(cmain, main) && !strcmp(csub, sub)) { |
| 419 | /* start of wanted part */ |
| 420 | in_wanted_part = 1; |
| 421 | if(strstr(patt, "base64=")) |
| 422 | /* bit rough test, but "mostly" functional, */ |
| 423 | /* treat wanted part data as base64 encoded */ |
| 424 | base64 = 1; |
| 425 | } |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | } |
| 430 | |
| 431 | if(in_wanted_part) { |
| 432 | show(("=> %s", buffer)); |
| 433 | error = appenddata(outbuf, outlen, &outalloc, buffer, base64); |
| 434 | if(error) |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | } /* while */ |
| 439 | |
| 440 | free(buffer); |
| 441 | |
| 442 | if(error != GPE_OK) { |
| 443 | if(error == GPE_END_OF_FILE) |
| 444 | error = GPE_OK; |
| 445 | else { |
| 446 | free(*outbuf); |
| 447 | *outbuf = NULL; |
| 448 | *outlen = 0; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return error; |
| 453 | } |
| 454 | |