getpart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "server_setup.h"
  25. #include "getpart.h"
  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. /* just to please curl_base64.h we create a fake struct */
  31. struct Curl_easy {
  32. int fake;
  33. };
  34. #include "curl_base64.h"
  35. #include "curl_memory.h"
  36. /* include memdebug.h last */
  37. #include "memdebug.h"
  38. #define EAT_SPACE(p) while(*(p) && ISSPACE(*(p))) (p)++
  39. #define EAT_WORD(p) while(*(p) && !ISSPACE(*(p)) && ('>' != *(p))) (p)++
  40. #ifdef DEBUG_GETPART
  41. #define show(x) printf x
  42. #else
  43. #define show(x) Curl_nop_stmt
  44. #endif
  45. #if defined(_MSC_VER) && defined(_DLL)
  46. # pragma warning(disable:4232) /* MSVC extension, dllimport identity */
  47. #endif
  48. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  49. curl_free_callback Curl_cfree = (curl_free_callback)free;
  50. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  51. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
  52. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  53. #if defined(WIN32) && defined(UNICODE)
  54. curl_wcsdup_callback Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
  55. #endif
  56. #if defined(_MSC_VER) && defined(_DLL)
  57. # pragma warning(default:4232) /* MSVC extension, dllimport identity */
  58. #endif
  59. /*
  60. * Curl_convert_clone() returns a malloced copy of the source string (if
  61. * returning CURLE_OK), with the data converted to network format. This
  62. * function is used by base64 code in libcurl built to support data
  63. * conversion. This is a DUMMY VERSION that returns data unmodified - for
  64. * use by the test server only.
  65. */
  66. CURLcode Curl_convert_clone(struct Curl_easy *data,
  67. const char *indata,
  68. size_t insize,
  69. char **outbuf);
  70. CURLcode Curl_convert_clone(struct Curl_easy *data,
  71. const char *indata,
  72. size_t insize,
  73. char **outbuf)
  74. {
  75. char *convbuf;
  76. (void)data;
  77. convbuf = malloc(insize);
  78. if(!convbuf)
  79. return CURLE_OUT_OF_MEMORY;
  80. memcpy(convbuf, indata, insize);
  81. *outbuf = convbuf;
  82. return CURLE_OK;
  83. }
  84. /*
  85. * line_length()
  86. *
  87. * Counts the number of characters in a line including a new line.
  88. * Unlike strlen() it does not stop at nul bytes.
  89. *
  90. */
  91. static size_t line_length(const char *buffer, int bytestocheck)
  92. {
  93. size_t length = 1;
  94. while(*buffer != '\n' && --bytestocheck) {
  95. length++;
  96. buffer++;
  97. }
  98. if(*buffer != '\n') {
  99. /*
  100. * We didn't find a new line so the last byte must be a
  101. * '\0' character inserted by fgets() which we should not
  102. * count.
  103. */
  104. length--;
  105. }
  106. return length;
  107. }
  108. /*
  109. * readline()
  110. *
  111. * Reads a complete line from a file into a dynamically allocated buffer.
  112. *
  113. * Calling function may call this multiple times with same 'buffer'
  114. * and 'bufsize' pointers to avoid multiple buffer allocations. Buffer
  115. * will be reallocated and 'bufsize' increased until whole line fits in
  116. * buffer before returning it.
  117. *
  118. * Calling function is responsible to free allocated buffer.
  119. *
  120. * This function may return:
  121. * GPE_OUT_OF_MEMORY
  122. * GPE_END_OF_FILE
  123. * GPE_OK
  124. */
  125. static int readline(char **buffer, size_t *bufsize, size_t *length,
  126. FILE *stream)
  127. {
  128. size_t offset = 0;
  129. char *newptr;
  130. if(!*buffer) {
  131. *buffer = malloc(128);
  132. if(!*buffer)
  133. return GPE_OUT_OF_MEMORY;
  134. *bufsize = 128;
  135. }
  136. for(;;) {
  137. int bytestoread = curlx_uztosi(*bufsize - offset);
  138. if(!fgets(*buffer + offset, bytestoread, stream))
  139. return (offset != 0) ? GPE_OK : GPE_END_OF_FILE;
  140. *length = offset + line_length(*buffer + offset, bytestoread);
  141. if(*(*buffer + *length - 1) == '\n')
  142. break;
  143. offset = *length;
  144. if(*length < *bufsize - 1)
  145. continue;
  146. newptr = realloc(*buffer, *bufsize * 2);
  147. if(!newptr)
  148. return GPE_OUT_OF_MEMORY;
  149. *buffer = newptr;
  150. *bufsize *= 2;
  151. }
  152. return GPE_OK;
  153. }
  154. /*
  155. * appenddata()
  156. *
  157. * This appends data from a given source buffer to the end of the used part of
  158. * a destination buffer. Arguments relative to the destination buffer are, the
  159. * address of a pointer to the destination buffer 'dst_buf', the length of data
  160. * in destination buffer excluding potential null string termination 'dst_len',
  161. * the allocated size of destination buffer 'dst_alloc'. All three destination
  162. * buffer arguments may be modified by this function. Arguments relative to the
  163. * source buffer are, a pointer to the source buffer 'src_buf' and indication
  164. * whether the source buffer is base64 encoded or not 'src_b64'.
  165. *
  166. * If the source buffer is indicated to be base64 encoded, this appends the
  167. * decoded data, binary or whatever, to the destination. The source buffer
  168. * may not hold binary data, only a null terminated string is valid content.
  169. *
  170. * Destination buffer will be enlarged and relocated as needed.
  171. *
  172. * Calling function is responsible to provide preallocated destination
  173. * buffer and also to deallocate it when no longer needed.
  174. *
  175. * This function may return:
  176. * GPE_OUT_OF_MEMORY
  177. * GPE_OK
  178. */
  179. static int appenddata(char **dst_buf, /* dest buffer */
  180. size_t *dst_len, /* dest buffer data length */
  181. size_t *dst_alloc, /* dest buffer allocated size */
  182. char *src_buf, /* source buffer */
  183. size_t src_len, /* source buffer length */
  184. int src_b64) /* != 0 if source is base64 encoded */
  185. {
  186. size_t need_alloc = 0;
  187. if(!src_len)
  188. return GPE_OK;
  189. need_alloc = src_len + *dst_len + 1;
  190. if(src_b64) {
  191. if(src_buf[src_len - 1] == '\r')
  192. src_len--;
  193. if(src_buf[src_len - 1] == '\n')
  194. src_len--;
  195. }
  196. /* enlarge destination buffer if required */
  197. if(need_alloc > *dst_alloc) {
  198. size_t newsize = need_alloc * 2;
  199. char *newptr = realloc(*dst_buf, newsize);
  200. if(!newptr) {
  201. return GPE_OUT_OF_MEMORY;
  202. }
  203. *dst_alloc = newsize;
  204. *dst_buf = newptr;
  205. }
  206. /* memcpy to support binary blobs */
  207. memcpy(*dst_buf + *dst_len, src_buf, src_len);
  208. *dst_len += src_len;
  209. *(*dst_buf + *dst_len) = '\0';
  210. return GPE_OK;
  211. }
  212. static int decodedata(char **buf, /* dest buffer */
  213. size_t *len) /* dest buffer data length */
  214. {
  215. CURLcode error = CURLE_OK;
  216. unsigned char *buf64 = NULL;
  217. size_t src_len = 0;
  218. if(!*len)
  219. return GPE_OK;
  220. /* base64 decode the given buffer */
  221. error = Curl_base64_decode(*buf, &buf64, &src_len);
  222. if(error)
  223. return GPE_OUT_OF_MEMORY;
  224. if(!src_len) {
  225. /*
  226. ** currently there is no way to tell apart an OOM condition in
  227. ** Curl_base64_decode() from zero length decoded data. For now,
  228. ** let's just assume it is an OOM condition, currently we have
  229. ** no input for this function that decodes to zero length data.
  230. */
  231. free(buf64);
  232. return GPE_OUT_OF_MEMORY;
  233. }
  234. /* memcpy to support binary blobs */
  235. memcpy(*buf, buf64, src_len);
  236. *len = src_len;
  237. *(*buf + src_len) = '\0';
  238. free(buf64);
  239. return GPE_OK;
  240. }
  241. /*
  242. * getpart()
  243. *
  244. * This returns whole contents of specified XML-like section and subsection
  245. * from the given file. This is mostly used to retrieve a specific part from
  246. * a test definition file for consumption by test suite servers.
  247. *
  248. * Data is returned in a dynamically allocated buffer, a pointer to this data
  249. * and the size of the data is stored at the addresses that caller specifies.
  250. *
  251. * If the returned data is a string the returned size will be the length of
  252. * the string excluding null termination. Otherwise it will just be the size
  253. * of the returned binary data.
  254. *
  255. * Calling function is responsible to free returned buffer.
  256. *
  257. * This function may return:
  258. * GPE_NO_BUFFER_SPACE
  259. * GPE_OUT_OF_MEMORY
  260. * GPE_OK
  261. */
  262. int getpart(char **outbuf, size_t *outlen,
  263. const char *main, const char *sub, FILE *stream)
  264. {
  265. # define MAX_TAG_LEN 200
  266. char couter[MAX_TAG_LEN + 1]; /* current outermost section */
  267. char cmain[MAX_TAG_LEN + 1]; /* current main section */
  268. char csub[MAX_TAG_LEN + 1]; /* current sub section */
  269. char ptag[MAX_TAG_LEN + 1]; /* potential tag */
  270. char patt[MAX_TAG_LEN + 1]; /* potential attributes */
  271. char *buffer = NULL;
  272. char *ptr;
  273. char *end;
  274. union {
  275. ssize_t sig;
  276. size_t uns;
  277. } len;
  278. size_t bufsize = 0;
  279. size_t outalloc = 256;
  280. size_t datalen;
  281. int in_wanted_part = 0;
  282. int base64 = 0;
  283. int nonewline = 0;
  284. int error;
  285. enum {
  286. STATE_OUTSIDE = 0,
  287. STATE_OUTER = 1,
  288. STATE_INMAIN = 2,
  289. STATE_INSUB = 3,
  290. STATE_ILLEGAL = 4
  291. } state = STATE_OUTSIDE;
  292. *outlen = 0;
  293. *outbuf = malloc(outalloc);
  294. if(!*outbuf)
  295. return GPE_OUT_OF_MEMORY;
  296. *(*outbuf) = '\0';
  297. couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0';
  298. while((error = readline(&buffer, &bufsize, &datalen, stream)) == GPE_OK) {
  299. ptr = buffer;
  300. EAT_SPACE(ptr);
  301. if('<' != *ptr) {
  302. if(in_wanted_part) {
  303. show(("=> %s", buffer));
  304. error = appenddata(outbuf, outlen, &outalloc, buffer, datalen,
  305. base64);
  306. if(error)
  307. break;
  308. }
  309. continue;
  310. }
  311. ptr++;
  312. if('/' == *ptr) {
  313. /*
  314. ** closing section tag
  315. */
  316. ptr++;
  317. end = ptr;
  318. EAT_WORD(end);
  319. len.sig = end - ptr;
  320. if(len.sig > MAX_TAG_LEN) {
  321. error = GPE_NO_BUFFER_SPACE;
  322. break;
  323. }
  324. memcpy(ptag, ptr, len.uns);
  325. ptag[len.uns] = '\0';
  326. if((STATE_INSUB == state) && !strcmp(csub, ptag)) {
  327. /* end of current sub section */
  328. state = STATE_INMAIN;
  329. csub[0] = '\0';
  330. if(in_wanted_part) {
  331. /* end of wanted part */
  332. in_wanted_part = 0;
  333. /* Do we need to base64 decode the data? */
  334. if(base64) {
  335. error = decodedata(outbuf, outlen);
  336. if(error)
  337. return error;
  338. }
  339. if(nonewline)
  340. (*outlen)--;
  341. break;
  342. }
  343. }
  344. else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) {
  345. /* end of current main section */
  346. state = STATE_OUTER;
  347. cmain[0] = '\0';
  348. if(in_wanted_part) {
  349. /* end of wanted part */
  350. in_wanted_part = 0;
  351. /* Do we need to base64 decode the data? */
  352. if(base64) {
  353. error = decodedata(outbuf, outlen);
  354. if(error)
  355. return error;
  356. }
  357. if(nonewline)
  358. (*outlen)--;
  359. break;
  360. }
  361. }
  362. else if((STATE_OUTER == state) && !strcmp(couter, ptag)) {
  363. /* end of outermost file section */
  364. state = STATE_OUTSIDE;
  365. couter[0] = '\0';
  366. if(in_wanted_part) {
  367. /* end of wanted part */
  368. in_wanted_part = 0;
  369. break;
  370. }
  371. }
  372. }
  373. else if(!in_wanted_part) {
  374. /*
  375. ** opening section tag
  376. */
  377. /* get potential tag */
  378. end = ptr;
  379. EAT_WORD(end);
  380. len.sig = end - ptr;
  381. if(len.sig > MAX_TAG_LEN) {
  382. error = GPE_NO_BUFFER_SPACE;
  383. break;
  384. }
  385. memcpy(ptag, ptr, len.uns);
  386. ptag[len.uns] = '\0';
  387. /* ignore comments, doctypes and xml declarations */
  388. if(('!' == ptag[0]) || ('?' == ptag[0])) {
  389. show(("* ignoring (%s)", buffer));
  390. continue;
  391. }
  392. /* get all potential attributes */
  393. ptr = end;
  394. EAT_SPACE(ptr);
  395. end = ptr;
  396. while(*end && ('>' != *end))
  397. end++;
  398. len.sig = end - ptr;
  399. if(len.sig > MAX_TAG_LEN) {
  400. error = GPE_NO_BUFFER_SPACE;
  401. break;
  402. }
  403. memcpy(patt, ptr, len.uns);
  404. patt[len.uns] = '\0';
  405. if(STATE_OUTSIDE == state) {
  406. /* outermost element (<testcase>) */
  407. strcpy(couter, ptag);
  408. state = STATE_OUTER;
  409. continue;
  410. }
  411. else if(STATE_OUTER == state) {
  412. /* start of a main section */
  413. strcpy(cmain, ptag);
  414. state = STATE_INMAIN;
  415. continue;
  416. }
  417. else if(STATE_INMAIN == state) {
  418. /* start of a sub section */
  419. strcpy(csub, ptag);
  420. state = STATE_INSUB;
  421. if(!strcmp(cmain, main) && !strcmp(csub, sub)) {
  422. /* start of wanted part */
  423. in_wanted_part = 1;
  424. if(strstr(patt, "base64="))
  425. /* bit rough test, but "mostly" functional, */
  426. /* treat wanted part data as base64 encoded */
  427. base64 = 1;
  428. if(strstr(patt, "nonewline=")) {
  429. show(("* setting nonewline\n"));
  430. nonewline = 1;
  431. }
  432. }
  433. continue;
  434. }
  435. }
  436. if(in_wanted_part) {
  437. show(("=> %s", buffer));
  438. error = appenddata(outbuf, outlen, &outalloc, buffer, datalen, base64);
  439. if(error)
  440. break;
  441. }
  442. } /* while */
  443. free(buffer);
  444. if(error != GPE_OK) {
  445. if(error == GPE_END_OF_FILE)
  446. error = GPE_OK;
  447. else {
  448. free(*outbuf);
  449. *outbuf = NULL;
  450. *outlen = 0;
  451. }
  452. }
  453. return error;
  454. }