getpart.c 13 KB

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