getpart.c 13 KB

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