getpart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 = calloc(1, 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. memset(&newptr[*bufsize], 0, *bufsize);
  150. *buffer = newptr;
  151. *bufsize *= 2;
  152. }
  153. return GPE_OK;
  154. }
  155. /*
  156. * appenddata()
  157. *
  158. * This appends data from a given source buffer to the end of the used part of
  159. * a destination buffer. Arguments relative to the destination buffer are, the
  160. * address of a pointer to the destination buffer 'dst_buf', the length of data
  161. * in destination buffer excluding potential null string termination 'dst_len',
  162. * the allocated size of destination buffer 'dst_alloc'. All three destination
  163. * buffer arguments may be modified by this function. Arguments relative to the
  164. * source buffer are, a pointer to the source buffer 'src_buf' and indication
  165. * whether the source buffer is base64 encoded or not 'src_b64'.
  166. *
  167. * If the source buffer is indicated to be base64 encoded, this appends the
  168. * decoded data, binary or whatever, to the destination. The source buffer
  169. * may not hold binary data, only a null terminated string is valid content.
  170. *
  171. * Destination buffer will be enlarged and relocated as needed.
  172. *
  173. * Calling function is responsible to provide preallocated destination
  174. * buffer and also to deallocate it when no longer needed.
  175. *
  176. * This function may return:
  177. * GPE_OUT_OF_MEMORY
  178. * GPE_OK
  179. */
  180. static int appenddata(char **dst_buf, /* dest buffer */
  181. size_t *dst_len, /* dest buffer data length */
  182. size_t *dst_alloc, /* dest buffer allocated size */
  183. char *src_buf, /* source buffer */
  184. size_t src_len, /* source buffer length */
  185. int src_b64) /* != 0 if source is base64 encoded */
  186. {
  187. size_t need_alloc = 0;
  188. if(!src_len)
  189. return GPE_OK;
  190. need_alloc = src_len + *dst_len + 1;
  191. if(src_b64) {
  192. if(src_buf[src_len - 1] == '\r')
  193. src_len--;
  194. if(src_buf[src_len - 1] == '\n')
  195. src_len--;
  196. }
  197. /* enlarge destination buffer if required */
  198. if(need_alloc > *dst_alloc) {
  199. size_t newsize = need_alloc * 2;
  200. char *newptr = realloc(*dst_buf, newsize);
  201. if(!newptr) {
  202. return GPE_OUT_OF_MEMORY;
  203. }
  204. *dst_alloc = newsize;
  205. *dst_buf = newptr;
  206. }
  207. /* memcpy to support binary blobs */
  208. memcpy(*dst_buf + *dst_len, src_buf, src_len);
  209. *dst_len += src_len;
  210. *(*dst_buf + *dst_len) = '\0';
  211. return GPE_OK;
  212. }
  213. static int decodedata(char **buf, /* dest buffer */
  214. size_t *len) /* dest buffer data length */
  215. {
  216. CURLcode error = CURLE_OK;
  217. unsigned char *buf64 = NULL;
  218. size_t src_len = 0;
  219. if(!*len)
  220. return GPE_OK;
  221. /* base64 decode the given buffer */
  222. error = Curl_base64_decode(*buf, &buf64, &src_len);
  223. if(error)
  224. return GPE_OUT_OF_MEMORY;
  225. if(!src_len) {
  226. /*
  227. ** currently there is no way to tell apart an OOM condition in
  228. ** Curl_base64_decode() from zero length decoded data. For now,
  229. ** let's just assume it is an OOM condition, currently we have
  230. ** no input for this function that decodes to zero length data.
  231. */
  232. free(buf64);
  233. return GPE_OUT_OF_MEMORY;
  234. }
  235. /* memcpy to support binary blobs */
  236. memcpy(*buf, buf64, src_len);
  237. *len = src_len;
  238. *(*buf + src_len) = '\0';
  239. free(buf64);
  240. return GPE_OK;
  241. }
  242. /*
  243. * getpart()
  244. *
  245. * This returns whole contents of specified XML-like section and subsection
  246. * from the given file. This is mostly used to retrieve a specific part from
  247. * a test definition file for consumption by test suite servers.
  248. *
  249. * Data is returned in a dynamically allocated buffer, a pointer to this data
  250. * and the size of the data is stored at the addresses that caller specifies.
  251. *
  252. * If the returned data is a string the returned size will be the length of
  253. * the string excluding null termination. Otherwise it will just be the size
  254. * of the returned binary data.
  255. *
  256. * Calling function is responsible to free returned buffer.
  257. *
  258. * This function may return:
  259. * GPE_NO_BUFFER_SPACE
  260. * GPE_OUT_OF_MEMORY
  261. * GPE_OK
  262. */
  263. int getpart(char **outbuf, size_t *outlen,
  264. const char *main, const char *sub, FILE *stream)
  265. {
  266. # define MAX_TAG_LEN 200
  267. char couter[MAX_TAG_LEN + 1]; /* current outermost section */
  268. char cmain[MAX_TAG_LEN + 1]; /* current main section */
  269. char csub[MAX_TAG_LEN + 1]; /* current sub section */
  270. char ptag[MAX_TAG_LEN + 1]; /* potential tag */
  271. char patt[MAX_TAG_LEN + 1]; /* potential attributes */
  272. char *buffer = NULL;
  273. char *ptr;
  274. char *end;
  275. union {
  276. ssize_t sig;
  277. size_t uns;
  278. } len;
  279. size_t bufsize = 0;
  280. size_t outalloc = 256;
  281. size_t datalen;
  282. int in_wanted_part = 0;
  283. int base64 = 0;
  284. int nonewline = 0;
  285. int error;
  286. enum {
  287. STATE_OUTSIDE = 0,
  288. STATE_OUTER = 1,
  289. STATE_INMAIN = 2,
  290. STATE_INSUB = 3,
  291. STATE_ILLEGAL = 4
  292. } state = STATE_OUTSIDE;
  293. *outlen = 0;
  294. *outbuf = malloc(outalloc);
  295. if(!*outbuf)
  296. return GPE_OUT_OF_MEMORY;
  297. *(*outbuf) = '\0';
  298. couter[0] = cmain[0] = csub[0] = ptag[0] = patt[0] = '\0';
  299. while((error = readline(&buffer, &bufsize, &datalen, stream)) == GPE_OK) {
  300. ptr = buffer;
  301. EAT_SPACE(ptr);
  302. if('<' != *ptr) {
  303. if(in_wanted_part) {
  304. show(("=> %s", buffer));
  305. error = appenddata(outbuf, outlen, &outalloc, buffer, datalen,
  306. base64);
  307. if(error)
  308. break;
  309. }
  310. continue;
  311. }
  312. ptr++;
  313. if('/' == *ptr) {
  314. /*
  315. ** closing section tag
  316. */
  317. ptr++;
  318. end = ptr;
  319. EAT_WORD(end);
  320. len.sig = end - ptr;
  321. if(len.sig > MAX_TAG_LEN) {
  322. error = GPE_NO_BUFFER_SPACE;
  323. break;
  324. }
  325. memcpy(ptag, ptr, len.uns);
  326. ptag[len.uns] = '\0';
  327. if((STATE_INSUB == state) && !strcmp(csub, ptag)) {
  328. /* end of current sub section */
  329. state = STATE_INMAIN;
  330. csub[0] = '\0';
  331. if(in_wanted_part) {
  332. /* end of wanted part */
  333. in_wanted_part = 0;
  334. /* Do we need to base64 decode the data? */
  335. if(base64) {
  336. error = decodedata(outbuf, outlen);
  337. if(error)
  338. return error;
  339. }
  340. if(nonewline)
  341. (*outlen)--;
  342. break;
  343. }
  344. }
  345. else if((STATE_INMAIN == state) && !strcmp(cmain, ptag)) {
  346. /* end of current main section */
  347. state = STATE_OUTER;
  348. cmain[0] = '\0';
  349. if(in_wanted_part) {
  350. /* end of wanted part */
  351. in_wanted_part = 0;
  352. /* Do we need to base64 decode the data? */
  353. if(base64) {
  354. error = decodedata(outbuf, outlen);
  355. if(error)
  356. return error;
  357. }
  358. if(nonewline)
  359. (*outlen)--;
  360. break;
  361. }
  362. }
  363. else if((STATE_OUTER == state) && !strcmp(couter, ptag)) {
  364. /* end of outermost file section */
  365. state = STATE_OUTSIDE;
  366. couter[0] = '\0';
  367. if(in_wanted_part) {
  368. /* end of wanted part */
  369. in_wanted_part = 0;
  370. break;
  371. }
  372. }
  373. }
  374. else if(!in_wanted_part) {
  375. /*
  376. ** opening section tag
  377. */
  378. /* get potential tag */
  379. end = ptr;
  380. EAT_WORD(end);
  381. len.sig = end - ptr;
  382. if(len.sig > MAX_TAG_LEN) {
  383. error = GPE_NO_BUFFER_SPACE;
  384. break;
  385. }
  386. memcpy(ptag, ptr, len.uns);
  387. ptag[len.uns] = '\0';
  388. /* ignore comments, doctypes and xml declarations */
  389. if(('!' == ptag[0]) || ('?' == ptag[0])) {
  390. show(("* ignoring (%s)", buffer));
  391. continue;
  392. }
  393. /* get all potential attributes */
  394. ptr = end;
  395. EAT_SPACE(ptr);
  396. end = ptr;
  397. while(*end && ('>' != *end))
  398. end++;
  399. len.sig = end - ptr;
  400. if(len.sig > MAX_TAG_LEN) {
  401. error = GPE_NO_BUFFER_SPACE;
  402. break;
  403. }
  404. memcpy(patt, ptr, len.uns);
  405. patt[len.uns] = '\0';
  406. if(STATE_OUTSIDE == state) {
  407. /* outermost element (<testcase>) */
  408. strcpy(couter, ptag);
  409. state = STATE_OUTER;
  410. continue;
  411. }
  412. else if(STATE_OUTER == state) {
  413. /* start of a main section */
  414. strcpy(cmain, ptag);
  415. state = STATE_INMAIN;
  416. continue;
  417. }
  418. else if(STATE_INMAIN == state) {
  419. /* start of a sub section */
  420. strcpy(csub, ptag);
  421. state = STATE_INSUB;
  422. if(!strcmp(cmain, main) && !strcmp(csub, sub)) {
  423. /* start of wanted part */
  424. in_wanted_part = 1;
  425. if(strstr(patt, "base64="))
  426. /* bit rough test, but "mostly" functional, */
  427. /* treat wanted part data as base64 encoded */
  428. base64 = 1;
  429. if(strstr(patt, "nonewline=")) {
  430. show(("* setting nonewline\n"));
  431. nonewline = 1;
  432. }
  433. }
  434. continue;
  435. }
  436. }
  437. if(in_wanted_part) {
  438. show(("=> %s", buffer));
  439. error = appenddata(outbuf, outlen, &outalloc, buffer, datalen, base64);
  440. if(error)
  441. break;
  442. }
  443. } /* while */
  444. free(buffer);
  445. if(error != GPE_OK) {
  446. if(error == GPE_END_OF_FILE)
  447. error = GPE_OK;
  448. else {
  449. free(*outbuf);
  450. *outbuf = NULL;
  451. *outlen = 0;
  452. }
  453. }
  454. return error;
  455. }