2
0

mime.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "mime.h"
  25. #include "non-ascii.h"
  26. #include "warnless.h"
  27. #include "urldata.h"
  28. #include "sendf.h"
  29. #if (!defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_MIME)) || \
  30. !defined(CURL_DISABLE_SMTP) || !defined(CURL_DISABLE_IMAP)
  31. #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
  32. #include <libgen.h>
  33. #endif
  34. #include "rand.h"
  35. #include "slist.h"
  36. #include "strcase.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. #ifdef WIN32
  42. # ifndef R_OK
  43. # define R_OK 4
  44. # endif
  45. #endif
  46. #define READ_ERROR ((size_t) -1)
  47. #define STOP_FILLING ((size_t) -2)
  48. static size_t mime_subparts_read(char *buffer, size_t size, size_t nitems,
  49. void *instream, bool *hasread);
  50. /* Encoders. */
  51. static size_t encoder_nop_read(char *buffer, size_t size, bool ateof,
  52. curl_mimepart *part);
  53. static curl_off_t encoder_nop_size(curl_mimepart *part);
  54. static size_t encoder_7bit_read(char *buffer, size_t size, bool ateof,
  55. curl_mimepart *part);
  56. static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
  57. curl_mimepart *part);
  58. static curl_off_t encoder_base64_size(curl_mimepart *part);
  59. static size_t encoder_qp_read(char *buffer, size_t size, bool ateof,
  60. curl_mimepart *part);
  61. static curl_off_t encoder_qp_size(curl_mimepart *part);
  62. static const struct mime_encoder encoders[] = {
  63. {"binary", encoder_nop_read, encoder_nop_size},
  64. {"8bit", encoder_nop_read, encoder_nop_size},
  65. {"7bit", encoder_7bit_read, encoder_nop_size},
  66. {"base64", encoder_base64_read, encoder_base64_size},
  67. {"quoted-printable", encoder_qp_read, encoder_qp_size},
  68. {ZERO_NULL, ZERO_NULL, ZERO_NULL}
  69. };
  70. /* Base64 encoding table */
  71. static const char base64[] =
  72. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  73. /* Quoted-printable character class table.
  74. *
  75. * We cannot rely on ctype functions since quoted-printable input data
  76. * is assumed to be ascii-compatible, even on non-ascii platforms. */
  77. #define QP_OK 1 /* Can be represented by itself. */
  78. #define QP_SP 2 /* Space or tab. */
  79. #define QP_CR 3 /* Carriage return. */
  80. #define QP_LF 4 /* Line-feed. */
  81. static const unsigned char qp_class[] = {
  82. 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 07 */
  83. 0, QP_SP, QP_LF, 0, 0, QP_CR, 0, 0, /* 08 - 0F */
  84. 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 17 */
  85. 0, 0, 0, 0, 0, 0, 0, 0, /* 18 - 1F */
  86. QP_SP, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 20 - 27 */
  87. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 28 - 2F */
  88. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 30 - 37 */
  89. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, 0 , QP_OK, QP_OK, /* 38 - 3F */
  90. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 40 - 47 */
  91. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 48 - 4F */
  92. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 50 - 57 */
  93. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 58 - 5F */
  94. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 60 - 67 */
  95. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 68 - 6F */
  96. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, /* 70 - 77 */
  97. QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, QP_OK, 0, /* 78 - 7F */
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
  106. };
  107. /* Binary --> hexadecimal ASCII table. */
  108. static const char aschex[] =
  109. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x41\x42\x43\x44\x45\x46";
  110. #ifndef __VMS
  111. #define filesize(name, stat_data) (stat_data.st_size)
  112. #define fopen_read fopen
  113. #else
  114. #include <fabdef.h>
  115. /*
  116. * get_vms_file_size does what it takes to get the real size of the file
  117. *
  118. * For fixed files, find out the size of the EOF block and adjust.
  119. *
  120. * For all others, have to read the entire file in, discarding the contents.
  121. * Most posted text files will be small, and binary files like zlib archives
  122. * and CD/DVD images should be either a STREAM_LF format or a fixed format.
  123. *
  124. */
  125. curl_off_t VmsRealFileSize(const char *name,
  126. const struct_stat *stat_buf)
  127. {
  128. char buffer[8192];
  129. curl_off_t count;
  130. int ret_stat;
  131. FILE * file;
  132. file = fopen(name, FOPEN_READTEXT); /* VMS */
  133. if(!file)
  134. return 0;
  135. count = 0;
  136. ret_stat = 1;
  137. while(ret_stat > 0) {
  138. ret_stat = fread(buffer, 1, sizeof(buffer), file);
  139. if(ret_stat)
  140. count += ret_stat;
  141. }
  142. fclose(file);
  143. return count;
  144. }
  145. /*
  146. *
  147. * VmsSpecialSize checks to see if the stat st_size can be trusted and
  148. * if not to call a routine to get the correct size.
  149. *
  150. */
  151. static curl_off_t VmsSpecialSize(const char *name,
  152. const struct_stat *stat_buf)
  153. {
  154. switch(stat_buf->st_fab_rfm) {
  155. case FAB$C_VAR:
  156. case FAB$C_VFC:
  157. return VmsRealFileSize(name, stat_buf);
  158. break;
  159. default:
  160. return stat_buf->st_size;
  161. }
  162. }
  163. #define filesize(name, stat_data) VmsSpecialSize(name, &stat_data)
  164. /*
  165. * vmsfopenread
  166. *
  167. * For upload to work as expected on VMS, different optional
  168. * parameters must be added to the fopen command based on
  169. * record format of the file.
  170. *
  171. */
  172. static FILE * vmsfopenread(const char *file, const char *mode)
  173. {
  174. struct_stat statbuf;
  175. int result;
  176. result = stat(file, &statbuf);
  177. switch(statbuf.st_fab_rfm) {
  178. case FAB$C_VAR:
  179. case FAB$C_VFC:
  180. case FAB$C_STMCR:
  181. return fopen(file, FOPEN_READTEXT); /* VMS */
  182. break;
  183. default:
  184. return fopen(file, FOPEN_READTEXT, "rfm=stmlf", "ctx=stm");
  185. }
  186. }
  187. #define fopen_read vmsfopenread
  188. #endif
  189. #ifndef HAVE_BASENAME
  190. /*
  191. (Quote from The Open Group Base Specifications Issue 6 IEEE Std 1003.1, 2004
  192. Edition)
  193. The basename() function shall take the pathname pointed to by path and
  194. return a pointer to the final component of the pathname, deleting any
  195. trailing '/' characters.
  196. If the string pointed to by path consists entirely of the '/' character,
  197. basename() shall return a pointer to the string "/". If the string pointed
  198. to by path is exactly "//", it is implementation-defined whether '/' or "//"
  199. is returned.
  200. If path is a null pointer or points to an empty string, basename() shall
  201. return a pointer to the string ".".
  202. The basename() function may modify the string pointed to by path, and may
  203. return a pointer to static storage that may then be overwritten by a
  204. subsequent call to basename().
  205. The basename() function need not be reentrant. A function that is not
  206. required to be reentrant is not required to be thread-safe.
  207. */
  208. static char *Curl_basename(char *path)
  209. {
  210. /* Ignore all the details above for now and make a quick and simple
  211. implementation here */
  212. char *s1;
  213. char *s2;
  214. s1 = strrchr(path, '/');
  215. s2 = strrchr(path, '\\');
  216. if(s1 && s2) {
  217. path = (s1 > s2? s1 : s2) + 1;
  218. }
  219. else if(s1)
  220. path = s1 + 1;
  221. else if(s2)
  222. path = s2 + 1;
  223. return path;
  224. }
  225. #define basename(x) Curl_basename((x))
  226. #endif
  227. /* Set readback state. */
  228. static void mimesetstate(struct mime_state *state,
  229. enum mimestate tok, void *ptr)
  230. {
  231. state->state = tok;
  232. state->ptr = ptr;
  233. state->offset = 0;
  234. }
  235. /* Escape header string into allocated memory. */
  236. static char *escape_string(const char *src)
  237. {
  238. size_t bytecount = 0;
  239. size_t i;
  240. char *dst;
  241. for(i = 0; src[i]; i++)
  242. if(src[i] == '"' || src[i] == '\\')
  243. bytecount++;
  244. bytecount += i;
  245. dst = malloc(bytecount + 1);
  246. if(!dst)
  247. return NULL;
  248. for(i = 0; *src; src++) {
  249. if(*src == '"' || *src == '\\')
  250. dst[i++] = '\\';
  251. dst[i++] = *src;
  252. }
  253. dst[i] = '\0';
  254. return dst;
  255. }
  256. /* Check if header matches. */
  257. static char *match_header(struct curl_slist *hdr, const char *lbl, size_t len)
  258. {
  259. char *value = NULL;
  260. if(strncasecompare(hdr->data, lbl, len) && hdr->data[len] == ':')
  261. for(value = hdr->data + len + 1; *value == ' '; value++)
  262. ;
  263. return value;
  264. }
  265. /* Get a header from an slist. */
  266. static char *search_header(struct curl_slist *hdrlist, const char *hdr)
  267. {
  268. size_t len = strlen(hdr);
  269. char *value = NULL;
  270. for(; !value && hdrlist; hdrlist = hdrlist->next)
  271. value = match_header(hdrlist, hdr, len);
  272. return value;
  273. }
  274. static char *strippath(const char *fullfile)
  275. {
  276. char *filename;
  277. char *base;
  278. filename = strdup(fullfile); /* duplicate since basename() may ruin the
  279. buffer it works on */
  280. if(!filename)
  281. return NULL;
  282. base = strdup(basename(filename));
  283. free(filename); /* free temporary buffer */
  284. return base; /* returns an allocated string or NULL ! */
  285. }
  286. /* Initialize data encoder state. */
  287. static void cleanup_encoder_state(struct mime_encoder_state *p)
  288. {
  289. p->pos = 0;
  290. p->bufbeg = 0;
  291. p->bufend = 0;
  292. }
  293. /* Dummy encoder. This is used for 8bit and binary content encodings. */
  294. static size_t encoder_nop_read(char *buffer, size_t size, bool ateof,
  295. struct curl_mimepart *part)
  296. {
  297. struct mime_encoder_state *st = &part->encstate;
  298. size_t insize = st->bufend - st->bufbeg;
  299. (void) ateof;
  300. if(!size)
  301. return STOP_FILLING;
  302. if(size > insize)
  303. size = insize;
  304. if(size)
  305. memcpy(buffer, st->buf + st->bufbeg, size);
  306. st->bufbeg += size;
  307. return size;
  308. }
  309. static curl_off_t encoder_nop_size(curl_mimepart *part)
  310. {
  311. return part->datasize;
  312. }
  313. /* 7bit encoder: the encoder is just a data validity check. */
  314. static size_t encoder_7bit_read(char *buffer, size_t size, bool ateof,
  315. curl_mimepart *part)
  316. {
  317. struct mime_encoder_state *st = &part->encstate;
  318. size_t cursize = st->bufend - st->bufbeg;
  319. (void) ateof;
  320. if(!size)
  321. return STOP_FILLING;
  322. if(size > cursize)
  323. size = cursize;
  324. for(cursize = 0; cursize < size; cursize++) {
  325. *buffer = st->buf[st->bufbeg];
  326. if(*buffer++ & 0x80)
  327. return cursize? cursize: READ_ERROR;
  328. st->bufbeg++;
  329. }
  330. return cursize;
  331. }
  332. /* Base64 content encoder. */
  333. static size_t encoder_base64_read(char *buffer, size_t size, bool ateof,
  334. curl_mimepart *part)
  335. {
  336. struct mime_encoder_state *st = &part->encstate;
  337. size_t cursize = 0;
  338. int i;
  339. char *ptr = buffer;
  340. while(st->bufbeg < st->bufend) {
  341. /* Line full ? */
  342. if(st->pos > MAX_ENCODED_LINE_LENGTH - 4) {
  343. /* Yes, we need 2 characters for CRLF. */
  344. if(size < 2) {
  345. if(!cursize)
  346. return STOP_FILLING;
  347. break;
  348. }
  349. *ptr++ = '\r';
  350. *ptr++ = '\n';
  351. st->pos = 0;
  352. cursize += 2;
  353. size -= 2;
  354. }
  355. /* Be sure there is enough space and input data for a base64 group. */
  356. if(size < 4) {
  357. if(!cursize)
  358. return STOP_FILLING;
  359. break;
  360. }
  361. if(st->bufend - st->bufbeg < 3)
  362. break;
  363. /* Encode three bytes as four characters. */
  364. i = st->buf[st->bufbeg++] & 0xFF;
  365. i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
  366. i = (i << 8) | (st->buf[st->bufbeg++] & 0xFF);
  367. *ptr++ = base64[(i >> 18) & 0x3F];
  368. *ptr++ = base64[(i >> 12) & 0x3F];
  369. *ptr++ = base64[(i >> 6) & 0x3F];
  370. *ptr++ = base64[i & 0x3F];
  371. cursize += 4;
  372. st->pos += 4;
  373. size -= 4;
  374. }
  375. /* If at eof, we have to flush the buffered data. */
  376. if(ateof) {
  377. if(size < 4) {
  378. if(!cursize)
  379. return STOP_FILLING;
  380. }
  381. else {
  382. /* Buffered data size can only be 0, 1 or 2. */
  383. ptr[2] = ptr[3] = '=';
  384. i = 0;
  385. switch(st->bufend - st->bufbeg) {
  386. case 2:
  387. i = (st->buf[st->bufbeg + 1] & 0xFF) << 8;
  388. /* FALLTHROUGH */
  389. case 1:
  390. i |= (st->buf[st->bufbeg] & 0xFF) << 16;
  391. ptr[0] = base64[(i >> 18) & 0x3F];
  392. ptr[1] = base64[(i >> 12) & 0x3F];
  393. if(++st->bufbeg != st->bufend) {
  394. ptr[2] = base64[(i >> 6) & 0x3F];
  395. st->bufbeg++;
  396. }
  397. cursize += 4;
  398. st->pos += 4;
  399. break;
  400. }
  401. }
  402. }
  403. #ifdef CURL_DOES_CONVERSIONS
  404. /* This is now textual data, Convert character codes. */
  405. if(part->easy && cursize) {
  406. CURLcode result = Curl_convert_to_network(part->easy, buffer, cursize);
  407. if(result)
  408. return READ_ERROR;
  409. }
  410. #endif
  411. return cursize;
  412. }
  413. static curl_off_t encoder_base64_size(curl_mimepart *part)
  414. {
  415. curl_off_t size = part->datasize;
  416. if(size <= 0)
  417. return size; /* Unknown size or no data. */
  418. /* Compute base64 character count. */
  419. size = 4 * (1 + (size - 1) / 3);
  420. /* Effective character count must include CRLFs. */
  421. return size + 2 * ((size - 1) / MAX_ENCODED_LINE_LENGTH);
  422. }
  423. /* Quoted-printable lookahead.
  424. *
  425. * Check if a CRLF or end of data is in input buffer at current position + n.
  426. * Return -1 if more data needed, 1 if CRLF or end of data, else 0.
  427. */
  428. static int qp_lookahead_eol(struct mime_encoder_state *st, int ateof, size_t n)
  429. {
  430. n += st->bufbeg;
  431. if(n >= st->bufend && ateof)
  432. return 1;
  433. if(n + 2 > st->bufend)
  434. return ateof? 0: -1;
  435. if(qp_class[st->buf[n] & 0xFF] == QP_CR &&
  436. qp_class[st->buf[n + 1] & 0xFF] == QP_LF)
  437. return 1;
  438. return 0;
  439. }
  440. /* Quoted-printable encoder. */
  441. static size_t encoder_qp_read(char *buffer, size_t size, bool ateof,
  442. curl_mimepart *part)
  443. {
  444. struct mime_encoder_state *st = &part->encstate;
  445. char *ptr = buffer;
  446. size_t cursize = 0;
  447. int softlinebreak;
  448. char buf[4];
  449. /* On all platforms, input is supposed to be ASCII compatible: for this
  450. reason, we use hexadecimal ASCII codes in this function rather than
  451. character constants that can be interpreted as non-ascii on some
  452. platforms. Preserve ASCII encoding on output too. */
  453. while(st->bufbeg < st->bufend) {
  454. size_t len = 1;
  455. size_t consumed = 1;
  456. int i = st->buf[st->bufbeg];
  457. buf[0] = (char) i;
  458. buf[1] = aschex[(i >> 4) & 0xF];
  459. buf[2] = aschex[i & 0xF];
  460. switch(qp_class[st->buf[st->bufbeg] & 0xFF]) {
  461. case QP_OK: /* Not a special character. */
  462. break;
  463. case QP_SP: /* Space or tab. */
  464. /* Spacing must be escaped if followed by CRLF. */
  465. switch(qp_lookahead_eol(st, ateof, 1)) {
  466. case -1: /* More input data needed. */
  467. return cursize;
  468. case 0: /* No encoding needed. */
  469. break;
  470. default: /* CRLF after space or tab. */
  471. buf[0] = '\x3D'; /* '=' */
  472. len = 3;
  473. break;
  474. }
  475. break;
  476. case QP_CR: /* Carriage return. */
  477. /* If followed by a line-feed, output the CRLF pair.
  478. Else escape it. */
  479. switch(qp_lookahead_eol(st, ateof, 0)) {
  480. case -1: /* Need more data. */
  481. return cursize;
  482. case 1: /* CRLF found. */
  483. buf[len++] = '\x0A'; /* Append '\n'. */
  484. consumed = 2;
  485. break;
  486. default: /* Not followed by LF: escape. */
  487. buf[0] = '\x3D'; /* '=' */
  488. len = 3;
  489. break;
  490. }
  491. break;
  492. default: /* Character must be escaped. */
  493. buf[0] = '\x3D'; /* '=' */
  494. len = 3;
  495. break;
  496. }
  497. /* Be sure the encoded character fits within maximum line length. */
  498. if(buf[len - 1] != '\x0A') { /* '\n' */
  499. softlinebreak = st->pos + len > MAX_ENCODED_LINE_LENGTH;
  500. if(!softlinebreak && st->pos + len == MAX_ENCODED_LINE_LENGTH) {
  501. /* We may use the current line only if end of data or followed by
  502. a CRLF. */
  503. switch(qp_lookahead_eol(st, ateof, consumed)) {
  504. case -1: /* Need more data. */
  505. return cursize;
  506. case 0: /* Not followed by a CRLF. */
  507. softlinebreak = 1;
  508. break;
  509. }
  510. }
  511. if(softlinebreak) {
  512. strcpy(buf, "\x3D\x0D\x0A"); /* "=\r\n" */
  513. len = 3;
  514. consumed = 0;
  515. }
  516. }
  517. /* If the output buffer would overflow, do not store. */
  518. if(len > size) {
  519. if(!cursize)
  520. return STOP_FILLING;
  521. break;
  522. }
  523. /* Append to output buffer. */
  524. memcpy(ptr, buf, len);
  525. cursize += len;
  526. ptr += len;
  527. size -= len;
  528. st->pos += len;
  529. if(buf[len - 1] == '\x0A') /* '\n' */
  530. st->pos = 0;
  531. st->bufbeg += consumed;
  532. }
  533. return cursize;
  534. }
  535. static curl_off_t encoder_qp_size(curl_mimepart *part)
  536. {
  537. /* Determining the size can only be done by reading the data: unless the
  538. data size is 0, we return it as unknown (-1). */
  539. return part->datasize? -1: 0;
  540. }
  541. /* In-memory data callbacks. */
  542. /* Argument is a pointer to the mime part. */
  543. static size_t mime_mem_read(char *buffer, size_t size, size_t nitems,
  544. void *instream)
  545. {
  546. curl_mimepart *part = (curl_mimepart *) instream;
  547. size_t sz = curlx_sotouz(part->datasize - part->state.offset);
  548. (void) size; /* Always 1.*/
  549. if(!nitems)
  550. return STOP_FILLING;
  551. if(sz > nitems)
  552. sz = nitems;
  553. if(sz)
  554. memcpy(buffer, part->data + curlx_sotouz(part->state.offset), sz);
  555. return sz;
  556. }
  557. static int mime_mem_seek(void *instream, curl_off_t offset, int whence)
  558. {
  559. curl_mimepart *part = (curl_mimepart *) instream;
  560. switch(whence) {
  561. case SEEK_CUR:
  562. offset += part->state.offset;
  563. break;
  564. case SEEK_END:
  565. offset += part->datasize;
  566. break;
  567. }
  568. if(offset < 0 || offset > part->datasize)
  569. return CURL_SEEKFUNC_FAIL;
  570. part->state.offset = offset;
  571. return CURL_SEEKFUNC_OK;
  572. }
  573. static void mime_mem_free(void *ptr)
  574. {
  575. Curl_safefree(((curl_mimepart *) ptr)->data);
  576. }
  577. /* Named file callbacks. */
  578. /* Argument is a pointer to the mime part. */
  579. static int mime_open_file(curl_mimepart *part)
  580. {
  581. /* Open a MIMEKIND_FILE part. */
  582. if(part->fp)
  583. return 0;
  584. part->fp = fopen_read(part->data, "rb");
  585. return part->fp? 0: -1;
  586. }
  587. static size_t mime_file_read(char *buffer, size_t size, size_t nitems,
  588. void *instream)
  589. {
  590. curl_mimepart *part = (curl_mimepart *) instream;
  591. if(!nitems)
  592. return STOP_FILLING;
  593. if(mime_open_file(part))
  594. return READ_ERROR;
  595. return fread(buffer, size, nitems, part->fp);
  596. }
  597. static int mime_file_seek(void *instream, curl_off_t offset, int whence)
  598. {
  599. curl_mimepart *part = (curl_mimepart *) instream;
  600. if(whence == SEEK_SET && !offset && !part->fp)
  601. return CURL_SEEKFUNC_OK; /* Not open: implicitly already at BOF. */
  602. if(mime_open_file(part))
  603. return CURL_SEEKFUNC_FAIL;
  604. return fseek(part->fp, (long) offset, whence)?
  605. CURL_SEEKFUNC_CANTSEEK: CURL_SEEKFUNC_OK;
  606. }
  607. static void mime_file_free(void *ptr)
  608. {
  609. curl_mimepart *part = (curl_mimepart *) ptr;
  610. if(part->fp) {
  611. fclose(part->fp);
  612. part->fp = NULL;
  613. }
  614. Curl_safefree(part->data);
  615. part->data = NULL;
  616. }
  617. /* Subparts callbacks. */
  618. /* Argument is a pointer to the mime structure. */
  619. /* Readback a byte string segment. */
  620. static size_t readback_bytes(struct mime_state *state,
  621. char *buffer, size_t bufsize,
  622. const char *bytes, size_t numbytes,
  623. const char *trail)
  624. {
  625. size_t sz;
  626. size_t offset = curlx_sotouz(state->offset);
  627. if(numbytes > offset) {
  628. sz = numbytes - offset;
  629. bytes += offset;
  630. }
  631. else {
  632. size_t tsz = strlen(trail);
  633. sz = offset - numbytes;
  634. if(sz >= tsz)
  635. return 0;
  636. bytes = trail + sz;
  637. sz = tsz - sz;
  638. }
  639. if(sz > bufsize)
  640. sz = bufsize;
  641. memcpy(buffer, bytes, sz);
  642. state->offset += sz;
  643. return sz;
  644. }
  645. /* Read a non-encoded part content. */
  646. static size_t read_part_content(curl_mimepart *part,
  647. char *buffer, size_t bufsize, bool *hasread)
  648. {
  649. size_t sz = 0;
  650. switch(part->lastreadstatus) {
  651. case 0:
  652. case CURL_READFUNC_ABORT:
  653. case CURL_READFUNC_PAUSE:
  654. case READ_ERROR:
  655. return part->lastreadstatus;
  656. default:
  657. break;
  658. }
  659. /* If we can determine we are at end of part data, spare a read. */
  660. if(part->datasize != (curl_off_t) -1 &&
  661. part->state.offset >= part->datasize) {
  662. /* sz is already zero. */
  663. }
  664. else {
  665. switch(part->kind) {
  666. case MIMEKIND_MULTIPART:
  667. /*
  668. * Cannot be processed as other kinds since read function requires
  669. * an additional parameter and is highly recursive.
  670. */
  671. sz = mime_subparts_read(buffer, 1, bufsize, part->arg, hasread);
  672. break;
  673. case MIMEKIND_FILE:
  674. if(part->fp && feof(part->fp))
  675. break; /* At EOF. */
  676. /* FALLTHROUGH */
  677. default:
  678. if(part->readfunc) {
  679. if(!(part->flags & MIME_FAST_READ)) {
  680. if(*hasread)
  681. return STOP_FILLING;
  682. *hasread = TRUE;
  683. }
  684. sz = part->readfunc(buffer, 1, bufsize, part->arg);
  685. }
  686. break;
  687. }
  688. }
  689. switch(sz) {
  690. case STOP_FILLING:
  691. break;
  692. case 0:
  693. case CURL_READFUNC_ABORT:
  694. case CURL_READFUNC_PAUSE:
  695. case READ_ERROR:
  696. part->lastreadstatus = sz;
  697. break;
  698. default:
  699. part->state.offset += sz;
  700. part->lastreadstatus = sz;
  701. break;
  702. }
  703. return sz;
  704. }
  705. /* Read and encode part content. */
  706. static size_t read_encoded_part_content(curl_mimepart *part, char *buffer,
  707. size_t bufsize, bool *hasread)
  708. {
  709. struct mime_encoder_state *st = &part->encstate;
  710. size_t cursize = 0;
  711. size_t sz;
  712. bool ateof = FALSE;
  713. for(;;) {
  714. if(st->bufbeg < st->bufend || ateof) {
  715. /* Encode buffered data. */
  716. sz = part->encoder->encodefunc(buffer, bufsize, ateof, part);
  717. switch(sz) {
  718. case 0:
  719. if(ateof)
  720. return cursize;
  721. break;
  722. case READ_ERROR:
  723. case STOP_FILLING:
  724. return cursize? cursize: sz;
  725. default:
  726. cursize += sz;
  727. buffer += sz;
  728. bufsize -= sz;
  729. continue;
  730. }
  731. }
  732. /* We need more data in input buffer. */
  733. if(st->bufbeg) {
  734. size_t len = st->bufend - st->bufbeg;
  735. if(len)
  736. memmove(st->buf, st->buf + st->bufbeg, len);
  737. st->bufbeg = 0;
  738. st->bufend = len;
  739. }
  740. if(st->bufend >= sizeof(st->buf))
  741. return cursize? cursize: READ_ERROR; /* Buffer full. */
  742. sz = read_part_content(part, st->buf + st->bufend,
  743. sizeof(st->buf) - st->bufend, hasread);
  744. switch(sz) {
  745. case 0:
  746. ateof = TRUE;
  747. break;
  748. case CURL_READFUNC_ABORT:
  749. case CURL_READFUNC_PAUSE:
  750. case READ_ERROR:
  751. case STOP_FILLING:
  752. return cursize? cursize: sz;
  753. default:
  754. st->bufend += sz;
  755. break;
  756. }
  757. }
  758. /* NOTREACHED */
  759. }
  760. /* Readback a mime part. */
  761. static size_t readback_part(curl_mimepart *part,
  762. char *buffer, size_t bufsize, bool *hasread)
  763. {
  764. size_t cursize = 0;
  765. #ifdef CURL_DOES_CONVERSIONS
  766. char *convbuf = buffer;
  767. #endif
  768. /* Readback from part. */
  769. while(bufsize) {
  770. size_t sz = 0;
  771. struct curl_slist *hdr = (struct curl_slist *) part->state.ptr;
  772. switch(part->state.state) {
  773. case MIMESTATE_BEGIN:
  774. mimesetstate(&part->state,
  775. (part->flags & MIME_BODY_ONLY)?
  776. MIMESTATE_BODY: MIMESTATE_CURLHEADERS,
  777. part->curlheaders);
  778. break;
  779. case MIMESTATE_USERHEADERS:
  780. if(!hdr) {
  781. mimesetstate(&part->state, MIMESTATE_EOH, NULL);
  782. break;
  783. }
  784. if(match_header(hdr, "Content-Type", 12)) {
  785. mimesetstate(&part->state, MIMESTATE_USERHEADERS, hdr->next);
  786. break;
  787. }
  788. /* FALLTHROUGH */
  789. case MIMESTATE_CURLHEADERS:
  790. if(!hdr)
  791. mimesetstate(&part->state, MIMESTATE_USERHEADERS, part->userheaders);
  792. else {
  793. sz = readback_bytes(&part->state, buffer, bufsize,
  794. hdr->data, strlen(hdr->data), "\r\n");
  795. if(!sz)
  796. mimesetstate(&part->state, part->state.state, hdr->next);
  797. }
  798. break;
  799. case MIMESTATE_EOH:
  800. sz = readback_bytes(&part->state, buffer, bufsize, "\r\n", 2, "");
  801. if(!sz)
  802. mimesetstate(&part->state, MIMESTATE_BODY, NULL);
  803. break;
  804. case MIMESTATE_BODY:
  805. #ifdef CURL_DOES_CONVERSIONS
  806. if(part->easy && convbuf < buffer) {
  807. CURLcode result = Curl_convert_to_network(part->easy, convbuf,
  808. buffer - convbuf);
  809. if(result)
  810. return READ_ERROR;
  811. convbuf = buffer;
  812. }
  813. #endif
  814. cleanup_encoder_state(&part->encstate);
  815. mimesetstate(&part->state, MIMESTATE_CONTENT, NULL);
  816. break;
  817. case MIMESTATE_CONTENT:
  818. if(part->encoder)
  819. sz = read_encoded_part_content(part, buffer, bufsize, hasread);
  820. else
  821. sz = read_part_content(part, buffer, bufsize, hasread);
  822. switch(sz) {
  823. case 0:
  824. mimesetstate(&part->state, MIMESTATE_END, NULL);
  825. /* Try sparing open file descriptors. */
  826. if(part->kind == MIMEKIND_FILE && part->fp) {
  827. fclose(part->fp);
  828. part->fp = NULL;
  829. }
  830. /* FALLTHROUGH */
  831. case CURL_READFUNC_ABORT:
  832. case CURL_READFUNC_PAUSE:
  833. case READ_ERROR:
  834. case STOP_FILLING:
  835. return cursize? cursize: sz;
  836. }
  837. break;
  838. case MIMESTATE_END:
  839. return cursize;
  840. default:
  841. break; /* Other values not in part state. */
  842. }
  843. /* Bump buffer and counters according to read size. */
  844. cursize += sz;
  845. buffer += sz;
  846. bufsize -= sz;
  847. }
  848. #ifdef CURL_DOES_CONVERSIONS
  849. if(part->easy && convbuf < buffer &&
  850. part->state.state < MIMESTATE_BODY) {
  851. CURLcode result = Curl_convert_to_network(part->easy, convbuf,
  852. buffer - convbuf);
  853. if(result)
  854. return READ_ERROR;
  855. }
  856. #endif
  857. return cursize;
  858. }
  859. /* Readback from mime. Warning: not a read callback function. */
  860. static size_t mime_subparts_read(char *buffer, size_t size, size_t nitems,
  861. void *instream, bool *hasread)
  862. {
  863. curl_mime *mime = (curl_mime *) instream;
  864. size_t cursize = 0;
  865. #ifdef CURL_DOES_CONVERSIONS
  866. char *convbuf = buffer;
  867. #endif
  868. (void) size; /* Always 1. */
  869. while(nitems) {
  870. size_t sz = 0;
  871. curl_mimepart *part = mime->state.ptr;
  872. switch(mime->state.state) {
  873. case MIMESTATE_BEGIN:
  874. case MIMESTATE_BODY:
  875. #ifdef CURL_DOES_CONVERSIONS
  876. convbuf = buffer;
  877. #endif
  878. mimesetstate(&mime->state, MIMESTATE_BOUNDARY1, mime->firstpart);
  879. /* The first boundary always follows the header termination empty line,
  880. so is always preceded by a CRLF. We can then spare 2 characters
  881. by skipping the leading CRLF in boundary. */
  882. mime->state.offset += 2;
  883. break;
  884. case MIMESTATE_BOUNDARY1:
  885. sz = readback_bytes(&mime->state, buffer, nitems, "\r\n--", 4, "");
  886. if(!sz)
  887. mimesetstate(&mime->state, MIMESTATE_BOUNDARY2, part);
  888. break;
  889. case MIMESTATE_BOUNDARY2:
  890. sz = readback_bytes(&mime->state, buffer, nitems, mime->boundary,
  891. strlen(mime->boundary), part? "\r\n": "--\r\n");
  892. if(!sz) {
  893. #ifdef CURL_DOES_CONVERSIONS
  894. if(mime->easy && convbuf < buffer) {
  895. CURLcode result = Curl_convert_to_network(mime->easy, convbuf,
  896. buffer - convbuf);
  897. if(result)
  898. return READ_ERROR;
  899. convbuf = buffer;
  900. }
  901. #endif
  902. mimesetstate(&mime->state, MIMESTATE_CONTENT, part);
  903. }
  904. break;
  905. case MIMESTATE_CONTENT:
  906. if(!part) {
  907. mimesetstate(&mime->state, MIMESTATE_END, NULL);
  908. break;
  909. }
  910. sz = readback_part(part, buffer, nitems, hasread);
  911. switch(sz) {
  912. case CURL_READFUNC_ABORT:
  913. case CURL_READFUNC_PAUSE:
  914. case READ_ERROR:
  915. case STOP_FILLING:
  916. return cursize? cursize: sz;
  917. case 0:
  918. #ifdef CURL_DOES_CONVERSIONS
  919. convbuf = buffer;
  920. #endif
  921. mimesetstate(&mime->state, MIMESTATE_BOUNDARY1, part->nextpart);
  922. break;
  923. }
  924. break;
  925. case MIMESTATE_END:
  926. return cursize;
  927. default:
  928. break; /* other values not used in mime state. */
  929. }
  930. /* Bump buffer and counters according to read size. */
  931. cursize += sz;
  932. buffer += sz;
  933. nitems -= sz;
  934. }
  935. #ifdef CURL_DOES_CONVERSIONS
  936. if(mime->easy && convbuf < buffer &&
  937. mime->state.state <= MIMESTATE_CONTENT) {
  938. CURLcode result = Curl_convert_to_network(mime->easy, convbuf,
  939. buffer - convbuf);
  940. if(result)
  941. return READ_ERROR;
  942. }
  943. #endif
  944. return cursize;
  945. }
  946. static int mime_part_rewind(curl_mimepart *part)
  947. {
  948. int res = CURL_SEEKFUNC_OK;
  949. enum mimestate targetstate = MIMESTATE_BEGIN;
  950. if(part->flags & MIME_BODY_ONLY)
  951. targetstate = MIMESTATE_BODY;
  952. cleanup_encoder_state(&part->encstate);
  953. if(part->state.state > targetstate) {
  954. res = CURL_SEEKFUNC_CANTSEEK;
  955. if(part->seekfunc) {
  956. res = part->seekfunc(part->arg, (curl_off_t) 0, SEEK_SET);
  957. switch(res) {
  958. case CURL_SEEKFUNC_OK:
  959. case CURL_SEEKFUNC_FAIL:
  960. case CURL_SEEKFUNC_CANTSEEK:
  961. break;
  962. case -1: /* For fseek() error. */
  963. res = CURL_SEEKFUNC_CANTSEEK;
  964. break;
  965. default:
  966. res = CURL_SEEKFUNC_FAIL;
  967. break;
  968. }
  969. }
  970. }
  971. if(res == CURL_SEEKFUNC_OK)
  972. mimesetstate(&part->state, targetstate, NULL);
  973. part->lastreadstatus = 1; /* Successful read status. */
  974. return res;
  975. }
  976. static int mime_subparts_seek(void *instream, curl_off_t offset, int whence)
  977. {
  978. curl_mime *mime = (curl_mime *) instream;
  979. curl_mimepart *part;
  980. int result = CURL_SEEKFUNC_OK;
  981. if(whence != SEEK_SET || offset)
  982. return CURL_SEEKFUNC_CANTSEEK; /* Only support full rewind. */
  983. if(mime->state.state == MIMESTATE_BEGIN)
  984. return CURL_SEEKFUNC_OK; /* Already rewound. */
  985. for(part = mime->firstpart; part; part = part->nextpart) {
  986. int res = mime_part_rewind(part);
  987. if(res != CURL_SEEKFUNC_OK)
  988. result = res;
  989. }
  990. if(result == CURL_SEEKFUNC_OK)
  991. mimesetstate(&mime->state, MIMESTATE_BEGIN, NULL);
  992. return result;
  993. }
  994. /* Release part content. */
  995. static void cleanup_part_content(curl_mimepart *part)
  996. {
  997. if(part->freefunc)
  998. part->freefunc(part->arg);
  999. part->readfunc = NULL;
  1000. part->seekfunc = NULL;
  1001. part->freefunc = NULL;
  1002. part->arg = (void *) part; /* Defaults to part itself. */
  1003. part->data = NULL;
  1004. part->fp = NULL;
  1005. part->datasize = (curl_off_t) 0; /* No size yet. */
  1006. cleanup_encoder_state(&part->encstate);
  1007. part->kind = MIMEKIND_NONE;
  1008. part->flags &= ~MIME_FAST_READ;
  1009. part->lastreadstatus = 1; /* Successful read status. */
  1010. part->state.state = MIMESTATE_BEGIN;
  1011. }
  1012. static void mime_subparts_free(void *ptr)
  1013. {
  1014. curl_mime *mime = (curl_mime *) ptr;
  1015. if(mime && mime->parent) {
  1016. mime->parent->freefunc = NULL; /* Be sure we won't be called again. */
  1017. cleanup_part_content(mime->parent); /* Avoid dangling pointer in part. */
  1018. }
  1019. curl_mime_free(mime);
  1020. }
  1021. /* Do not free subparts: unbind them. This is used for the top level only. */
  1022. static void mime_subparts_unbind(void *ptr)
  1023. {
  1024. curl_mime *mime = (curl_mime *) ptr;
  1025. if(mime && mime->parent) {
  1026. mime->parent->freefunc = NULL; /* Be sure we won't be called again. */
  1027. cleanup_part_content(mime->parent); /* Avoid dangling pointer in part. */
  1028. mime->parent = NULL;
  1029. }
  1030. }
  1031. void Curl_mime_cleanpart(curl_mimepart *part)
  1032. {
  1033. cleanup_part_content(part);
  1034. curl_slist_free_all(part->curlheaders);
  1035. if(part->flags & MIME_USERHEADERS_OWNER)
  1036. curl_slist_free_all(part->userheaders);
  1037. Curl_safefree(part->mimetype);
  1038. Curl_safefree(part->name);
  1039. Curl_safefree(part->filename);
  1040. Curl_mime_initpart(part, part->easy);
  1041. }
  1042. /* Recursively delete a mime handle and its parts. */
  1043. void curl_mime_free(curl_mime *mime)
  1044. {
  1045. curl_mimepart *part;
  1046. if(mime) {
  1047. mime_subparts_unbind(mime); /* Be sure it's not referenced anymore. */
  1048. while(mime->firstpart) {
  1049. part = mime->firstpart;
  1050. mime->firstpart = part->nextpart;
  1051. Curl_mime_cleanpart(part);
  1052. free(part);
  1053. }
  1054. free(mime);
  1055. }
  1056. }
  1057. CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src)
  1058. {
  1059. curl_mime *mime;
  1060. curl_mimepart *d;
  1061. const curl_mimepart *s;
  1062. CURLcode res = CURLE_OK;
  1063. DEBUGASSERT(dst);
  1064. /* Duplicate content. */
  1065. switch(src->kind) {
  1066. case MIMEKIND_NONE:
  1067. break;
  1068. case MIMEKIND_DATA:
  1069. res = curl_mime_data(dst, src->data, (size_t) src->datasize);
  1070. break;
  1071. case MIMEKIND_FILE:
  1072. res = curl_mime_filedata(dst, src->data);
  1073. /* Do not abort duplication if file is not readable. */
  1074. if(res == CURLE_READ_ERROR)
  1075. res = CURLE_OK;
  1076. break;
  1077. case MIMEKIND_CALLBACK:
  1078. res = curl_mime_data_cb(dst, src->datasize, src->readfunc,
  1079. src->seekfunc, src->freefunc, src->arg);
  1080. break;
  1081. case MIMEKIND_MULTIPART:
  1082. /* No one knows about the cloned subparts, thus always attach ownership
  1083. to the part. */
  1084. mime = curl_mime_init(dst->easy);
  1085. res = mime? curl_mime_subparts(dst, mime): CURLE_OUT_OF_MEMORY;
  1086. /* Duplicate subparts. */
  1087. for(s = ((curl_mime *) src->arg)->firstpart; !res && s; s = s->nextpart) {
  1088. d = curl_mime_addpart(mime);
  1089. res = d? Curl_mime_duppart(d, s): CURLE_OUT_OF_MEMORY;
  1090. }
  1091. break;
  1092. default: /* Invalid kind: should not occur. */
  1093. res = CURLE_BAD_FUNCTION_ARGUMENT; /* Internal error? */
  1094. break;
  1095. }
  1096. /* Duplicate headers. */
  1097. if(!res && src->userheaders) {
  1098. struct curl_slist *hdrs = Curl_slist_duplicate(src->userheaders);
  1099. if(!hdrs)
  1100. res = CURLE_OUT_OF_MEMORY;
  1101. else {
  1102. /* No one but this procedure knows about the new header list,
  1103. so always take ownership. */
  1104. res = curl_mime_headers(dst, hdrs, TRUE);
  1105. if(res)
  1106. curl_slist_free_all(hdrs);
  1107. }
  1108. }
  1109. if(!res) {
  1110. /* Duplicate other fields. */
  1111. dst->encoder = src->encoder;
  1112. res = curl_mime_type(dst, src->mimetype);
  1113. }
  1114. if(!res)
  1115. res = curl_mime_name(dst, src->name);
  1116. if(!res)
  1117. res = curl_mime_filename(dst, src->filename);
  1118. /* If an error occurred, rollback. */
  1119. if(res)
  1120. Curl_mime_cleanpart(dst);
  1121. return res;
  1122. }
  1123. /*
  1124. * Mime build functions.
  1125. */
  1126. /* Create a mime handle. */
  1127. curl_mime *curl_mime_init(struct Curl_easy *easy)
  1128. {
  1129. curl_mime *mime;
  1130. mime = (curl_mime *) malloc(sizeof(*mime));
  1131. if(mime) {
  1132. mime->easy = easy;
  1133. mime->parent = NULL;
  1134. mime->firstpart = NULL;
  1135. mime->lastpart = NULL;
  1136. memset(mime->boundary, '-', 24);
  1137. if(Curl_rand_hex(easy, (unsigned char *) &mime->boundary[24],
  1138. MIME_RAND_BOUNDARY_CHARS + 1)) {
  1139. /* failed to get random separator, bail out */
  1140. free(mime);
  1141. return NULL;
  1142. }
  1143. mimesetstate(&mime->state, MIMESTATE_BEGIN, NULL);
  1144. }
  1145. return mime;
  1146. }
  1147. /* Initialize a mime part. */
  1148. void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy)
  1149. {
  1150. memset((char *) part, 0, sizeof(*part));
  1151. part->easy = easy;
  1152. part->lastreadstatus = 1; /* Successful read status. */
  1153. mimesetstate(&part->state, MIMESTATE_BEGIN, NULL);
  1154. }
  1155. /* Create a mime part and append it to a mime handle's part list. */
  1156. curl_mimepart *curl_mime_addpart(curl_mime *mime)
  1157. {
  1158. curl_mimepart *part;
  1159. if(!mime)
  1160. return NULL;
  1161. part = (curl_mimepart *) malloc(sizeof(*part));
  1162. if(part) {
  1163. Curl_mime_initpart(part, mime->easy);
  1164. part->parent = mime;
  1165. if(mime->lastpart)
  1166. mime->lastpart->nextpart = part;
  1167. else
  1168. mime->firstpart = part;
  1169. mime->lastpart = part;
  1170. }
  1171. return part;
  1172. }
  1173. /* Set mime part name. */
  1174. CURLcode curl_mime_name(curl_mimepart *part, const char *name)
  1175. {
  1176. if(!part)
  1177. return CURLE_BAD_FUNCTION_ARGUMENT;
  1178. Curl_safefree(part->name);
  1179. part->name = NULL;
  1180. if(name) {
  1181. part->name = strdup(name);
  1182. if(!part->name)
  1183. return CURLE_OUT_OF_MEMORY;
  1184. }
  1185. return CURLE_OK;
  1186. }
  1187. /* Set mime part remote file name. */
  1188. CURLcode curl_mime_filename(curl_mimepart *part, const char *filename)
  1189. {
  1190. if(!part)
  1191. return CURLE_BAD_FUNCTION_ARGUMENT;
  1192. Curl_safefree(part->filename);
  1193. part->filename = NULL;
  1194. if(filename) {
  1195. part->filename = strdup(filename);
  1196. if(!part->filename)
  1197. return CURLE_OUT_OF_MEMORY;
  1198. }
  1199. return CURLE_OK;
  1200. }
  1201. /* Set mime part content from memory data. */
  1202. CURLcode curl_mime_data(curl_mimepart *part,
  1203. const char *data, size_t datasize)
  1204. {
  1205. if(!part)
  1206. return CURLE_BAD_FUNCTION_ARGUMENT;
  1207. cleanup_part_content(part);
  1208. if(data) {
  1209. if(datasize == CURL_ZERO_TERMINATED)
  1210. datasize = strlen(data);
  1211. part->data = malloc(datasize + 1);
  1212. if(!part->data)
  1213. return CURLE_OUT_OF_MEMORY;
  1214. part->datasize = datasize;
  1215. if(datasize)
  1216. memcpy(part->data, data, datasize);
  1217. part->data[datasize] = '\0'; /* Set a null terminator as sentinel. */
  1218. part->readfunc = mime_mem_read;
  1219. part->seekfunc = mime_mem_seek;
  1220. part->freefunc = mime_mem_free;
  1221. part->flags |= MIME_FAST_READ;
  1222. part->kind = MIMEKIND_DATA;
  1223. }
  1224. return CURLE_OK;
  1225. }
  1226. /* Set mime part content from named local file. */
  1227. CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
  1228. {
  1229. CURLcode result = CURLE_OK;
  1230. if(!part)
  1231. return CURLE_BAD_FUNCTION_ARGUMENT;
  1232. cleanup_part_content(part);
  1233. if(filename) {
  1234. char *base;
  1235. struct_stat sbuf;
  1236. if(stat(filename, &sbuf) || access(filename, R_OK))
  1237. result = CURLE_READ_ERROR;
  1238. part->data = strdup(filename);
  1239. if(!part->data)
  1240. result = CURLE_OUT_OF_MEMORY;
  1241. part->datasize = -1;
  1242. if(!result && S_ISREG(sbuf.st_mode)) {
  1243. part->datasize = filesize(filename, sbuf);
  1244. part->seekfunc = mime_file_seek;
  1245. }
  1246. part->readfunc = mime_file_read;
  1247. part->freefunc = mime_file_free;
  1248. part->kind = MIMEKIND_FILE;
  1249. /* As a side effect, set the filename to the current file's base name.
  1250. It is possible to withdraw this by explicitly calling
  1251. curl_mime_filename() with a NULL filename argument after the current
  1252. call. */
  1253. base = strippath(filename);
  1254. if(!base)
  1255. result = CURLE_OUT_OF_MEMORY;
  1256. else {
  1257. CURLcode res = curl_mime_filename(part, base);
  1258. if(res)
  1259. result = res;
  1260. free(base);
  1261. }
  1262. }
  1263. return result;
  1264. }
  1265. /* Set mime part type. */
  1266. CURLcode curl_mime_type(curl_mimepart *part, const char *mimetype)
  1267. {
  1268. if(!part)
  1269. return CURLE_BAD_FUNCTION_ARGUMENT;
  1270. Curl_safefree(part->mimetype);
  1271. part->mimetype = NULL;
  1272. if(mimetype) {
  1273. part->mimetype = strdup(mimetype);
  1274. if(!part->mimetype)
  1275. return CURLE_OUT_OF_MEMORY;
  1276. }
  1277. return CURLE_OK;
  1278. }
  1279. /* Set mime data transfer encoder. */
  1280. CURLcode curl_mime_encoder(curl_mimepart *part, const char *encoding)
  1281. {
  1282. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  1283. const struct mime_encoder *mep;
  1284. if(!part)
  1285. return result;
  1286. part->encoder = NULL;
  1287. if(!encoding)
  1288. return CURLE_OK; /* Removing current encoder. */
  1289. for(mep = encoders; mep->name; mep++)
  1290. if(strcasecompare(encoding, mep->name)) {
  1291. part->encoder = mep;
  1292. result = CURLE_OK;
  1293. }
  1294. return result;
  1295. }
  1296. /* Set mime part headers. */
  1297. CURLcode curl_mime_headers(curl_mimepart *part,
  1298. struct curl_slist *headers, int take_ownership)
  1299. {
  1300. if(!part)
  1301. return CURLE_BAD_FUNCTION_ARGUMENT;
  1302. if(part->flags & MIME_USERHEADERS_OWNER) {
  1303. if(part->userheaders != headers) /* Allow setting twice the same list. */
  1304. curl_slist_free_all(part->userheaders);
  1305. part->flags &= ~MIME_USERHEADERS_OWNER;
  1306. }
  1307. part->userheaders = headers;
  1308. if(headers && take_ownership)
  1309. part->flags |= MIME_USERHEADERS_OWNER;
  1310. return CURLE_OK;
  1311. }
  1312. /* Set mime part content from callback. */
  1313. CURLcode curl_mime_data_cb(curl_mimepart *part, curl_off_t datasize,
  1314. curl_read_callback readfunc,
  1315. curl_seek_callback seekfunc,
  1316. curl_free_callback freefunc, void *arg)
  1317. {
  1318. if(!part)
  1319. return CURLE_BAD_FUNCTION_ARGUMENT;
  1320. cleanup_part_content(part);
  1321. if(readfunc) {
  1322. part->readfunc = readfunc;
  1323. part->seekfunc = seekfunc;
  1324. part->freefunc = freefunc;
  1325. part->arg = arg;
  1326. part->datasize = datasize;
  1327. part->kind = MIMEKIND_CALLBACK;
  1328. }
  1329. return CURLE_OK;
  1330. }
  1331. /* Set mime part content from subparts. */
  1332. CURLcode Curl_mime_set_subparts(curl_mimepart *part,
  1333. curl_mime *subparts, int take_ownership)
  1334. {
  1335. curl_mime *root;
  1336. if(!part)
  1337. return CURLE_BAD_FUNCTION_ARGUMENT;
  1338. /* Accept setting twice the same subparts. */
  1339. if(part->kind == MIMEKIND_MULTIPART && part->arg == subparts)
  1340. return CURLE_OK;
  1341. cleanup_part_content(part);
  1342. if(subparts) {
  1343. /* Must belong to the same data handle. */
  1344. if(part->easy && subparts->easy && part->easy != subparts->easy)
  1345. return CURLE_BAD_FUNCTION_ARGUMENT;
  1346. /* Should not have been attached already. */
  1347. if(subparts->parent)
  1348. return CURLE_BAD_FUNCTION_ARGUMENT;
  1349. /* Should not be the part's root. */
  1350. root = part->parent;
  1351. if(root) {
  1352. while(root->parent && root->parent->parent)
  1353. root = root->parent->parent;
  1354. if(subparts == root) {
  1355. if(part->easy)
  1356. failf(part->easy, "Can't add itself as a subpart!");
  1357. return CURLE_BAD_FUNCTION_ARGUMENT;
  1358. }
  1359. }
  1360. subparts->parent = part;
  1361. /* Subparts are processed internally: no read callback. */
  1362. part->seekfunc = mime_subparts_seek;
  1363. part->freefunc = take_ownership? mime_subparts_free: mime_subparts_unbind;
  1364. part->arg = subparts;
  1365. part->datasize = -1;
  1366. part->kind = MIMEKIND_MULTIPART;
  1367. }
  1368. return CURLE_OK;
  1369. }
  1370. CURLcode curl_mime_subparts(curl_mimepart *part, curl_mime *subparts)
  1371. {
  1372. return Curl_mime_set_subparts(part, subparts, TRUE);
  1373. }
  1374. /* Readback from top mime. */
  1375. /* Argument is the dummy top part. */
  1376. size_t Curl_mime_read(char *buffer, size_t size, size_t nitems, void *instream)
  1377. {
  1378. curl_mimepart *part = (curl_mimepart *) instream;
  1379. size_t ret;
  1380. bool hasread;
  1381. (void) size; /* Always 1. */
  1382. do {
  1383. hasread = FALSE;
  1384. ret = readback_part(part, buffer, nitems, &hasread);
  1385. /*
  1386. * If this is not possible to get some data without calling more than
  1387. * one read callback (probably because a content encoder is not able to
  1388. * deliver a new bunch for the few data accumulated so far), force another
  1389. * read until we get enough data or a special exit code.
  1390. */
  1391. } while(ret == STOP_FILLING);
  1392. return ret;
  1393. }
  1394. /* Rewind mime stream. */
  1395. CURLcode Curl_mime_rewind(curl_mimepart *part)
  1396. {
  1397. return mime_part_rewind(part) == CURL_SEEKFUNC_OK?
  1398. CURLE_OK: CURLE_SEND_FAIL_REWIND;
  1399. }
  1400. /* Compute header list size. */
  1401. static size_t slist_size(struct curl_slist *s,
  1402. size_t overhead, const char *skip)
  1403. {
  1404. size_t size = 0;
  1405. size_t skiplen = skip? strlen(skip): 0;
  1406. for(; s; s = s->next)
  1407. if(!skip || !match_header(s, skip, skiplen))
  1408. size += strlen(s->data) + overhead;
  1409. return size;
  1410. }
  1411. /* Get/compute multipart size. */
  1412. static curl_off_t multipart_size(curl_mime *mime)
  1413. {
  1414. curl_off_t size;
  1415. size_t boundarysize;
  1416. curl_mimepart *part;
  1417. if(!mime)
  1418. return 0; /* Not present -> empty. */
  1419. boundarysize = 4 + strlen(mime->boundary) + 2;
  1420. size = boundarysize; /* Final boundary - CRLF after headers. */
  1421. for(part = mime->firstpart; part; part = part->nextpart) {
  1422. curl_off_t sz = Curl_mime_size(part);
  1423. if(sz < 0)
  1424. size = sz;
  1425. if(size >= 0)
  1426. size += boundarysize + sz;
  1427. }
  1428. return size;
  1429. }
  1430. /* Get/compute mime size. */
  1431. curl_off_t Curl_mime_size(curl_mimepart *part)
  1432. {
  1433. curl_off_t size;
  1434. if(part->kind == MIMEKIND_MULTIPART)
  1435. part->datasize = multipart_size(part->arg);
  1436. size = part->datasize;
  1437. if(part->encoder)
  1438. size = part->encoder->sizefunc(part);
  1439. if(size >= 0 && !(part->flags & MIME_BODY_ONLY)) {
  1440. /* Compute total part size. */
  1441. size += slist_size(part->curlheaders, 2, NULL);
  1442. size += slist_size(part->userheaders, 2, "Content-Type");
  1443. size += 2; /* CRLF after headers. */
  1444. }
  1445. return size;
  1446. }
  1447. /* Add a header. */
  1448. /* VARARGS2 */
  1449. CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
  1450. {
  1451. struct curl_slist *hdr = NULL;
  1452. char *s = NULL;
  1453. va_list ap;
  1454. va_start(ap, fmt);
  1455. s = curl_mvaprintf(fmt, ap);
  1456. va_end(ap);
  1457. if(s) {
  1458. hdr = Curl_slist_append_nodup(*slp, s);
  1459. if(hdr)
  1460. *slp = hdr;
  1461. else
  1462. free(s);
  1463. }
  1464. return hdr? CURLE_OK: CURLE_OUT_OF_MEMORY;
  1465. }
  1466. /* Add a content type header. */
  1467. static CURLcode add_content_type(struct curl_slist **slp,
  1468. const char *type, const char *boundary)
  1469. {
  1470. return Curl_mime_add_header(slp, "Content-Type: %s%s%s", type,
  1471. boundary? "; boundary=": "",
  1472. boundary? boundary: "");
  1473. }
  1474. const char *Curl_mime_contenttype(const char *filename)
  1475. {
  1476. /*
  1477. * If no content type was specified, we scan through a few well-known
  1478. * extensions and pick the first we match!
  1479. */
  1480. struct ContentType {
  1481. const char *extension;
  1482. const char *type;
  1483. };
  1484. static const struct ContentType ctts[] = {
  1485. {".gif", "image/gif"},
  1486. {".jpg", "image/jpeg"},
  1487. {".jpeg", "image/jpeg"},
  1488. {".png", "image/png"},
  1489. {".svg", "image/svg+xml"},
  1490. {".txt", "text/plain"},
  1491. {".htm", "text/html"},
  1492. {".html", "text/html"},
  1493. {".pdf", "application/pdf"},
  1494. {".xml", "application/xml"}
  1495. };
  1496. if(filename) {
  1497. size_t len1 = strlen(filename);
  1498. const char *nameend = filename + len1;
  1499. unsigned int i;
  1500. for(i = 0; i < sizeof(ctts) / sizeof(ctts[0]); i++) {
  1501. size_t len2 = strlen(ctts[i].extension);
  1502. if(len1 >= len2 && strcasecompare(nameend - len2, ctts[i].extension))
  1503. return ctts[i].type;
  1504. }
  1505. }
  1506. return NULL;
  1507. }
  1508. static bool content_type_match(const char *contenttype, const char *target)
  1509. {
  1510. size_t len = strlen(target);
  1511. if(contenttype && strncasecompare(contenttype, target, len))
  1512. switch(contenttype[len]) {
  1513. case '\0':
  1514. case '\t':
  1515. case '\r':
  1516. case '\n':
  1517. case ' ':
  1518. case ';':
  1519. return TRUE;
  1520. }
  1521. return FALSE;
  1522. }
  1523. CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
  1524. const char *contenttype,
  1525. const char *disposition,
  1526. enum mimestrategy strategy)
  1527. {
  1528. curl_mime *mime = NULL;
  1529. const char *boundary = NULL;
  1530. char *customct;
  1531. const char *cte = NULL;
  1532. CURLcode ret = CURLE_OK;
  1533. /* Get rid of previously prepared headers. */
  1534. curl_slist_free_all(part->curlheaders);
  1535. part->curlheaders = NULL;
  1536. /* Be sure we won't access old headers later. */
  1537. if(part->state.state == MIMESTATE_CURLHEADERS)
  1538. mimesetstate(&part->state, MIMESTATE_CURLHEADERS, NULL);
  1539. /* Check if content type is specified. */
  1540. customct = part->mimetype;
  1541. if(!customct)
  1542. customct = search_header(part->userheaders, "Content-Type");
  1543. if(customct)
  1544. contenttype = customct;
  1545. /* If content type is not specified, try to determine it. */
  1546. if(!contenttype) {
  1547. switch(part->kind) {
  1548. case MIMEKIND_MULTIPART:
  1549. contenttype = MULTIPART_CONTENTTYPE_DEFAULT;
  1550. break;
  1551. case MIMEKIND_FILE:
  1552. contenttype = Curl_mime_contenttype(part->filename);
  1553. if(!contenttype)
  1554. contenttype = Curl_mime_contenttype(part->data);
  1555. if(!contenttype && part->filename)
  1556. contenttype = FILE_CONTENTTYPE_DEFAULT;
  1557. break;
  1558. default:
  1559. contenttype = Curl_mime_contenttype(part->filename);
  1560. break;
  1561. }
  1562. }
  1563. if(part->kind == MIMEKIND_MULTIPART) {
  1564. mime = (curl_mime *) part->arg;
  1565. if(mime)
  1566. boundary = mime->boundary;
  1567. }
  1568. else if(contenttype && !customct &&
  1569. content_type_match(contenttype, "text/plain"))
  1570. if(strategy == MIMESTRATEGY_MAIL || !part->filename)
  1571. contenttype = NULL;
  1572. /* Issue content-disposition header only if not already set by caller. */
  1573. if(!search_header(part->userheaders, "Content-Disposition")) {
  1574. if(!disposition)
  1575. if(part->filename || part->name ||
  1576. (contenttype && !strncasecompare(contenttype, "multipart/", 10)))
  1577. disposition = DISPOSITION_DEFAULT;
  1578. if(disposition && curl_strequal(disposition, "attachment") &&
  1579. !part->name && !part->filename)
  1580. disposition = NULL;
  1581. if(disposition) {
  1582. char *name = NULL;
  1583. char *filename = NULL;
  1584. if(part->name) {
  1585. name = escape_string(part->name);
  1586. if(!name)
  1587. ret = CURLE_OUT_OF_MEMORY;
  1588. }
  1589. if(!ret && part->filename) {
  1590. filename = escape_string(part->filename);
  1591. if(!filename)
  1592. ret = CURLE_OUT_OF_MEMORY;
  1593. }
  1594. if(!ret)
  1595. ret = Curl_mime_add_header(&part->curlheaders,
  1596. "Content-Disposition: %s%s%s%s%s%s%s",
  1597. disposition,
  1598. name? "; name=\"": "",
  1599. name? name: "",
  1600. name? "\"": "",
  1601. filename? "; filename=\"": "",
  1602. filename? filename: "",
  1603. filename? "\"": "");
  1604. Curl_safefree(name);
  1605. Curl_safefree(filename);
  1606. if(ret)
  1607. return ret;
  1608. }
  1609. }
  1610. /* Issue Content-Type header. */
  1611. if(contenttype) {
  1612. ret = add_content_type(&part->curlheaders, contenttype, boundary);
  1613. if(ret)
  1614. return ret;
  1615. }
  1616. /* Content-Transfer-Encoding header. */
  1617. if(!search_header(part->userheaders, "Content-Transfer-Encoding")) {
  1618. if(part->encoder)
  1619. cte = part->encoder->name;
  1620. else if(contenttype && strategy == MIMESTRATEGY_MAIL &&
  1621. part->kind != MIMEKIND_MULTIPART)
  1622. cte = "8bit";
  1623. if(cte) {
  1624. ret = Curl_mime_add_header(&part->curlheaders,
  1625. "Content-Transfer-Encoding: %s", cte);
  1626. if(ret)
  1627. return ret;
  1628. }
  1629. }
  1630. /* If we were reading curl-generated headers, restart with new ones (this
  1631. should not occur). */
  1632. if(part->state.state == MIMESTATE_CURLHEADERS)
  1633. mimesetstate(&part->state, MIMESTATE_CURLHEADERS, part->curlheaders);
  1634. /* Process subparts. */
  1635. if(part->kind == MIMEKIND_MULTIPART && mime) {
  1636. curl_mimepart *subpart;
  1637. disposition = NULL;
  1638. if(content_type_match(contenttype, "multipart/form-data"))
  1639. disposition = "form-data";
  1640. for(subpart = mime->firstpart; subpart; subpart = subpart->nextpart) {
  1641. ret = Curl_mime_prepare_headers(subpart, NULL, disposition, strategy);
  1642. if(ret)
  1643. return ret;
  1644. }
  1645. }
  1646. return ret;
  1647. }
  1648. /* Recursively reset paused status in the given part. */
  1649. void Curl_mime_unpause(curl_mimepart *part)
  1650. {
  1651. if(part) {
  1652. if(part->lastreadstatus == CURL_READFUNC_PAUSE)
  1653. part->lastreadstatus = 1; /* Successful read status. */
  1654. if(part->kind == MIMEKIND_MULTIPART) {
  1655. curl_mime *mime = (curl_mime *) part->arg;
  1656. if(mime) {
  1657. curl_mimepart *subpart;
  1658. for(subpart = mime->firstpart; subpart; subpart = subpart->nextpart)
  1659. Curl_mime_unpause(subpart);
  1660. }
  1661. }
  1662. }
  1663. }
  1664. #else /* !CURL_DISABLE_HTTP || !CURL_DISABLE_SMTP || !CURL_DISABLE_IMAP */
  1665. /* Mime not compiled in: define stubs for externally-referenced functions. */
  1666. curl_mime *curl_mime_init(CURL *easy)
  1667. {
  1668. (void) easy;
  1669. return NULL;
  1670. }
  1671. void curl_mime_free(curl_mime *mime)
  1672. {
  1673. (void) mime;
  1674. }
  1675. curl_mimepart *curl_mime_addpart(curl_mime *mime)
  1676. {
  1677. (void) mime;
  1678. return NULL;
  1679. }
  1680. CURLcode curl_mime_name(curl_mimepart *part, const char *name)
  1681. {
  1682. (void) part;
  1683. (void) name;
  1684. return CURLE_NOT_BUILT_IN;
  1685. }
  1686. CURLcode curl_mime_filename(curl_mimepart *part, const char *filename)
  1687. {
  1688. (void) part;
  1689. (void) filename;
  1690. return CURLE_NOT_BUILT_IN;
  1691. }
  1692. CURLcode curl_mime_type(curl_mimepart *part, const char *mimetype)
  1693. {
  1694. (void) part;
  1695. (void) mimetype;
  1696. return CURLE_NOT_BUILT_IN;
  1697. }
  1698. CURLcode curl_mime_encoder(curl_mimepart *part, const char *encoding)
  1699. {
  1700. (void) part;
  1701. (void) encoding;
  1702. return CURLE_NOT_BUILT_IN;
  1703. }
  1704. CURLcode curl_mime_data(curl_mimepart *part,
  1705. const char *data, size_t datasize)
  1706. {
  1707. (void) part;
  1708. (void) data;
  1709. (void) datasize;
  1710. return CURLE_NOT_BUILT_IN;
  1711. }
  1712. CURLcode curl_mime_filedata(curl_mimepart *part, const char *filename)
  1713. {
  1714. (void) part;
  1715. (void) filename;
  1716. return CURLE_NOT_BUILT_IN;
  1717. }
  1718. CURLcode curl_mime_data_cb(curl_mimepart *part,
  1719. curl_off_t datasize,
  1720. curl_read_callback readfunc,
  1721. curl_seek_callback seekfunc,
  1722. curl_free_callback freefunc,
  1723. void *arg)
  1724. {
  1725. (void) part;
  1726. (void) datasize;
  1727. (void) readfunc;
  1728. (void) seekfunc;
  1729. (void) freefunc;
  1730. (void) arg;
  1731. return CURLE_NOT_BUILT_IN;
  1732. }
  1733. CURLcode curl_mime_subparts(curl_mimepart *part, curl_mime *subparts)
  1734. {
  1735. (void) part;
  1736. (void) subparts;
  1737. return CURLE_NOT_BUILT_IN;
  1738. }
  1739. CURLcode curl_mime_headers(curl_mimepart *part,
  1740. struct curl_slist *headers, int take_ownership)
  1741. {
  1742. (void) part;
  1743. (void) headers;
  1744. (void) take_ownership;
  1745. return CURLE_NOT_BUILT_IN;
  1746. }
  1747. CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
  1748. {
  1749. (void)slp;
  1750. (void)fmt;
  1751. return CURLE_NOT_BUILT_IN;
  1752. }
  1753. #endif /* if disabled */