getpart.c 12 KB

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