mime.c 53 KB

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