getpart.c 11 KB

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