mime.c 53 KB

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