ccsidcurl.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  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. ***************************************************************************/
  25. /* CCSID API wrappers for OS/400. */
  26. #include <iconv.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stddef.h>
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #pragma enum(int)
  33. #include "curl.h"
  34. #include "mprintf.h"
  35. #include "slist.h"
  36. #include "urldata.h"
  37. #include "url.h"
  38. #include "setopt.h"
  39. #include "getinfo.h"
  40. #include "ccsidcurl.h"
  41. #include "os400sys.h"
  42. #ifndef SIZE_MAX
  43. #define SIZE_MAX ((size_t) ~0) /* Is unsigned on OS/400. */
  44. #endif
  45. #define ASCII_CCSID 819 /* Use ISO-8859-1 as ASCII. */
  46. #define NOCONV_CCSID 65535 /* No conversion. */
  47. #define ICONV_ID_SIZE 32 /* Size of iconv_open() code identifier. */
  48. #define ICONV_OPEN_ERROR(t) ((t).return_value == -1)
  49. #define ALLOC_GRANULE 8 /* Alloc. granule for curl_formadd_ccsid(). */
  50. static void
  51. makeOS400IconvCode(char buf[ICONV_ID_SIZE], unsigned int ccsid)
  52. {
  53. /**
  54. *** Convert a CCSID to the corresponding IBM iconv_open() character
  55. *** code identifier.
  56. *** This code is specific to the OS400 implementation of the iconv library.
  57. *** CCSID 65535 (no conversion) is replaced by the ASCII CCSID.
  58. *** CCSID 0 is interpreted by the OS400 as the job's CCSID.
  59. **/
  60. ccsid &= 0xFFFF;
  61. if(ccsid == NOCONV_CCSID)
  62. ccsid = ASCII_CCSID;
  63. memset(buf, 0, ICONV_ID_SIZE);
  64. curl_msprintf(buf, "IBMCCSID%05u0000000", ccsid);
  65. }
  66. static iconv_t
  67. iconv_open_CCSID(unsigned int ccsidout, unsigned int ccsidin,
  68. unsigned int cstr)
  69. {
  70. char fromcode[ICONV_ID_SIZE];
  71. char tocode[ICONV_ID_SIZE];
  72. /**
  73. *** Like iconv_open(), but character codes are given as CCSIDs.
  74. *** If `cstr' is non-zero, conversion is set up to stop whenever a
  75. *** null character is encountered.
  76. *** See iconv_open() IBM description in "National Language Support API".
  77. **/
  78. makeOS400IconvCode(fromcode, ccsidin);
  79. makeOS400IconvCode(tocode, ccsidout);
  80. memset(tocode + 13, 0, sizeof(tocode) - 13); /* Dest. code id format. */
  81. if(cstr)
  82. fromcode[18] = '1'; /* Set null-terminator flag. */
  83. return iconv_open(tocode, fromcode);
  84. }
  85. static int
  86. convert(char *d, size_t dlen, int dccsid,
  87. const char *s, int slen, int sccsid)
  88. {
  89. int i;
  90. iconv_t cd;
  91. size_t lslen;
  92. /**
  93. *** Convert `sccsid'-coded `slen'-data bytes at `s' into `dccsid'-coded
  94. *** data stored in the `dlen'-byte buffer at `d'.
  95. *** If `slen' < 0, source string is null-terminated.
  96. *** CCSID 65535 (no conversion) is replaced by the ASCII CCSID.
  97. *** Return the converted destination byte count, or -1 if error.
  98. **/
  99. if(sccsid == 65535)
  100. sccsid = ASCII_CCSID;
  101. if(dccsid == 65535)
  102. dccsid = ASCII_CCSID;
  103. if(sccsid == dccsid) {
  104. lslen = slen >= 0 ? slen : strlen(s) + 1;
  105. i = lslen < dlen ? lslen : dlen;
  106. if(s != d && i > 0)
  107. memcpy(d, s, i);
  108. return i;
  109. }
  110. if(slen < 0) {
  111. lslen = 0;
  112. cd = iconv_open_CCSID(dccsid, sccsid, 1);
  113. }
  114. else {
  115. lslen = (size_t) slen;
  116. cd = iconv_open_CCSID(dccsid, sccsid, 0);
  117. }
  118. if(ICONV_OPEN_ERROR(cd))
  119. return -1;
  120. i = dlen;
  121. if((int) iconv(cd, (char * *) &s, &lslen, &d, &dlen) < 0)
  122. i = -1;
  123. else
  124. i -= dlen;
  125. iconv_close(cd);
  126. return i;
  127. }
  128. static char *dynconvert(int dccsid, const char *s, int slen, int sccsid)
  129. {
  130. char *d;
  131. char *cp;
  132. size_t dlen;
  133. int l;
  134. static const char nullbyte = 0;
  135. /* Like convert, but the destination is allocated and returned. */
  136. dlen = (size_t) (slen < 0 ? strlen(s) : slen) + 1;
  137. dlen *= MAX_CONV_EXPANSION; /* Allow some expansion. */
  138. d = malloc(dlen);
  139. if(!d)
  140. return (char *) NULL;
  141. l = convert(d, dlen, dccsid, s, slen, sccsid);
  142. if(l < 0) {
  143. free(d);
  144. return (char *) NULL;
  145. }
  146. if(slen < 0) {
  147. /* Need to null-terminate even when source length is given.
  148. Since destination code size is unknown, use a conversion to generate
  149. terminator. */
  150. int l2 = convert(d + l, dlen - l, dccsid, &nullbyte, -1, ASCII_CCSID);
  151. if(l2 < 0) {
  152. free(d);
  153. return (char *) NULL;
  154. }
  155. l += l2;
  156. }
  157. if((size_t) l < dlen) {
  158. cp = realloc(d, l); /* Shorten to minimum needed. */
  159. if(cp)
  160. d = cp;
  161. }
  162. return d;
  163. }
  164. static struct curl_slist *
  165. slist_convert(int dccsid, struct curl_slist *from, int sccsid)
  166. {
  167. struct curl_slist *to = (struct curl_slist *) NULL;
  168. for(; from; from = from->next) {
  169. struct curl_slist *nl;
  170. char *cp = dynconvert(dccsid, from->data, -1, sccsid);
  171. if(!cp) {
  172. curl_slist_free_all(to);
  173. return (struct curl_slist *) NULL;
  174. }
  175. nl = Curl_slist_append_nodup(to, cp);
  176. if(!nl) {
  177. curl_slist_free_all(to);
  178. free(cp);
  179. return NULL;
  180. }
  181. to = nl;
  182. }
  183. return to;
  184. }
  185. static char *
  186. keyed_string(localkey_t key, const char *ascii, unsigned int ccsid)
  187. {
  188. int i;
  189. char *ebcdic;
  190. if(!ascii)
  191. return (char *) NULL;
  192. i = MAX_CONV_EXPANSION * (strlen(ascii) + 1);
  193. ebcdic = Curl_thread_buffer(key, i);
  194. if(!ebcdic)
  195. return ebcdic;
  196. if(convert(ebcdic, i, ccsid, ascii, -1, ASCII_CCSID) < 0)
  197. return (char *) NULL;
  198. return ebcdic;
  199. }
  200. const char *
  201. curl_to_ccsid(const char *s, unsigned int ccsid)
  202. {
  203. if(s)
  204. s = dynconvert(ccsid, s, -1, ASCII_CCSID);
  205. return s;
  206. }
  207. const char *
  208. curl_from_ccsid(const char *s, unsigned int ccsid)
  209. {
  210. if(s)
  211. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  212. return s;
  213. }
  214. char *
  215. curl_version_ccsid(unsigned int ccsid)
  216. {
  217. return keyed_string(LK_CURL_VERSION, curl_version(), ccsid);
  218. }
  219. char *
  220. curl_easy_escape_ccsid(CURL *handle, const char *string, int length,
  221. unsigned int sccsid, unsigned int dccsid)
  222. {
  223. char *s;
  224. char *d;
  225. if(!string) {
  226. errno = EINVAL;
  227. return (char *) NULL;
  228. }
  229. s = dynconvert(ASCII_CCSID, string, length ? length : -1, sccsid);
  230. if(!s)
  231. return (char *) NULL;
  232. d = curl_easy_escape(handle, s, 0);
  233. free(s);
  234. if(!d)
  235. return (char *) NULL;
  236. s = dynconvert(dccsid, d, -1, ASCII_CCSID);
  237. free(d);
  238. return s;
  239. }
  240. char *
  241. curl_easy_unescape_ccsid(CURL *handle, const char *string, int length,
  242. int *outlength,
  243. unsigned int sccsid, unsigned int dccsid)
  244. {
  245. char *s;
  246. char *d;
  247. if(!string) {
  248. errno = EINVAL;
  249. return (char *) NULL;
  250. }
  251. s = dynconvert(ASCII_CCSID, string, length ? length : -1, sccsid);
  252. if(!s)
  253. return (char *) NULL;
  254. d = curl_easy_unescape(handle, s, 0, outlength);
  255. free(s);
  256. if(!d)
  257. return (char *) NULL;
  258. s = dynconvert(dccsid, d, -1, ASCII_CCSID);
  259. free(d);
  260. if(s && outlength)
  261. *outlength = strlen(s);
  262. return s;
  263. }
  264. struct curl_slist *
  265. curl_slist_append_ccsid(struct curl_slist *list,
  266. const char *data, unsigned int ccsid)
  267. {
  268. char *s;
  269. s = (char *) NULL;
  270. if(!data)
  271. return curl_slist_append(list, data);
  272. s = dynconvert(ASCII_CCSID, data, -1, ccsid);
  273. if(!s)
  274. return (struct curl_slist *) NULL;
  275. list = curl_slist_append(list, s);
  276. free(s);
  277. return list;
  278. }
  279. time_t
  280. curl_getdate_ccsid(const char *p, const time_t *unused, unsigned int ccsid)
  281. {
  282. char *s;
  283. time_t t;
  284. if(!p)
  285. return curl_getdate(p, unused);
  286. s = dynconvert(ASCII_CCSID, p, -1, ccsid);
  287. if(!s)
  288. return (time_t) -1;
  289. t = curl_getdate(s, unused);
  290. free(s);
  291. return t;
  292. }
  293. static int
  294. convert_version_info_string(const char **stringp,
  295. char **bufp, int *left, unsigned int ccsid)
  296. {
  297. /* Helper for curl_version_info_ccsid(): convert a string if defined.
  298. Result is stored in the `*left'-byte buffer at `*bufp'.
  299. `*bufp' and `*left' are updated accordingly.
  300. Return 0 if ok, else -1. */
  301. if(*stringp) {
  302. int l = convert(*bufp, *left, ccsid, *stringp, -1, ASCII_CCSID);
  303. if(l <= 0)
  304. return -1;
  305. *stringp = *bufp;
  306. *bufp += l;
  307. *left -= l;
  308. }
  309. return 0;
  310. }
  311. curl_version_info_data *
  312. curl_version_info_ccsid(CURLversion stamp, unsigned int ccsid)
  313. {
  314. curl_version_info_data *p;
  315. char *cp;
  316. int n;
  317. int nproto;
  318. curl_version_info_data *id;
  319. int i;
  320. const char **cpp;
  321. static const size_t charfields[] = {
  322. offsetof(curl_version_info_data, version),
  323. offsetof(curl_version_info_data, host),
  324. offsetof(curl_version_info_data, ssl_version),
  325. offsetof(curl_version_info_data, libz_version),
  326. offsetof(curl_version_info_data, ares),
  327. offsetof(curl_version_info_data, libidn),
  328. offsetof(curl_version_info_data, libssh_version),
  329. offsetof(curl_version_info_data, brotli_version),
  330. offsetof(curl_version_info_data, nghttp2_version),
  331. offsetof(curl_version_info_data, quic_version),
  332. offsetof(curl_version_info_data, cainfo),
  333. offsetof(curl_version_info_data, capath),
  334. offsetof(curl_version_info_data, zstd_version),
  335. offsetof(curl_version_info_data, hyper_version),
  336. offsetof(curl_version_info_data, gsasl_version),
  337. offsetof(curl_version_info_data, feature_names),
  338. offsetof(curl_version_info_data, rtmp_version)
  339. };
  340. /* The assertion below is possible, because although the second operand
  341. is an enum member, the first is a #define. In that case, the OS/400 C
  342. compiler seems to compare string values after substitution. */
  343. #if CURLVERSION_NOW != CURLVERSION_ELEVENTH
  344. #error curl_version_info_data structure has changed: upgrade this procedure.
  345. #endif
  346. /* If caller has been compiled with a newer version, error. */
  347. if(stamp > CURLVERSION_NOW)
  348. return (curl_version_info_data *) NULL;
  349. p = curl_version_info(stamp);
  350. if(!p)
  351. return p;
  352. /* Measure thread space needed. */
  353. n = 0;
  354. nproto = 0;
  355. if(p->protocols) {
  356. while(p->protocols[nproto])
  357. n += strlen(p->protocols[nproto++]);
  358. n += nproto++;
  359. }
  360. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  361. cpp = (const char **) ((char *) p + charfields[i]);
  362. if(*cpp)
  363. n += strlen(*cpp) + 1;
  364. }
  365. /* Allocate thread space. */
  366. n *= MAX_CONV_EXPANSION;
  367. if(nproto)
  368. n += nproto * sizeof(const char *);
  369. cp = Curl_thread_buffer(LK_VERSION_INFO_DATA, n);
  370. id = (curl_version_info_data *) Curl_thread_buffer(LK_VERSION_INFO,
  371. sizeof(*id));
  372. if(!id || !cp)
  373. return (curl_version_info_data *) NULL;
  374. /* Copy data and convert strings. */
  375. memcpy((char *) id, (char *) p, sizeof(*p));
  376. if(id->protocols) {
  377. i = nproto * sizeof(id->protocols[0]);
  378. id->protocols = (const char * const *) cp;
  379. memcpy(cp, (char *) p->protocols, i);
  380. cp += i;
  381. n -= i;
  382. for(i = 0; id->protocols[i]; i++)
  383. if(convert_version_info_string(((const char * *) id->protocols) + i,
  384. &cp, &n, ccsid))
  385. return (curl_version_info_data *) NULL;
  386. }
  387. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  388. cpp = (const char **) ((char *) p + charfields[i]);
  389. if(*cpp && convert_version_info_string(cpp, &cp, &n, ccsid))
  390. return (curl_version_info_data *) NULL;
  391. }
  392. return id;
  393. }
  394. const char *
  395. curl_easy_strerror_ccsid(CURLcode error, unsigned int ccsid)
  396. {
  397. return keyed_string(LK_EASY_STRERROR, curl_easy_strerror(error), ccsid);
  398. }
  399. const char *
  400. curl_share_strerror_ccsid(CURLSHcode error, unsigned int ccsid)
  401. {
  402. return keyed_string(LK_SHARE_STRERROR, curl_share_strerror(error), ccsid);
  403. }
  404. const char *
  405. curl_multi_strerror_ccsid(CURLMcode error, unsigned int ccsid)
  406. {
  407. return keyed_string(LK_MULTI_STRERROR, curl_multi_strerror(error), ccsid);
  408. }
  409. const char *
  410. curl_url_strerror_ccsid(CURLUcode error, unsigned int ccsid)
  411. {
  412. return keyed_string(LK_URL_STRERROR, curl_url_strerror(error), ccsid);
  413. }
  414. void
  415. curl_certinfo_free_all(struct curl_certinfo *info)
  416. {
  417. /* Free all memory used by certificate info. */
  418. if(info) {
  419. if(info->certinfo) {
  420. int i;
  421. for(i = 0; i < info->num_of_certs; i++)
  422. curl_slist_free_all(info->certinfo[i]);
  423. free((char *) info->certinfo);
  424. }
  425. free((char *) info);
  426. }
  427. }
  428. CURLcode
  429. curl_easy_getinfo_ccsid(CURL *curl, CURLINFO info, ...)
  430. {
  431. va_list arg;
  432. void *paramp;
  433. CURLcode ret;
  434. struct Curl_easy *data;
  435. /* WARNING: unlike curl_easy_getinfo(), the strings returned by this
  436. procedure have to be free'ed. */
  437. data = (struct Curl_easy *) curl;
  438. va_start(arg, info);
  439. paramp = va_arg(arg, void *);
  440. ret = Curl_getinfo(data, info, paramp);
  441. if(ret == CURLE_OK) {
  442. unsigned int ccsid;
  443. char **cpp;
  444. struct curl_slist **slp;
  445. struct curl_certinfo *cipf;
  446. struct curl_certinfo *cipt;
  447. switch((int) info & CURLINFO_TYPEMASK) {
  448. case CURLINFO_STRING:
  449. ccsid = va_arg(arg, unsigned int);
  450. cpp = (char * *) paramp;
  451. if(*cpp) {
  452. *cpp = dynconvert(ccsid, *cpp, -1, ASCII_CCSID);
  453. if(!*cpp)
  454. ret = CURLE_OUT_OF_MEMORY;
  455. }
  456. break;
  457. case CURLINFO_SLIST:
  458. ccsid = va_arg(arg, unsigned int);
  459. switch(info) {
  460. case CURLINFO_CERTINFO:
  461. cipf = *(struct curl_certinfo * *) paramp;
  462. if(cipf) {
  463. cipt = (struct curl_certinfo *) malloc(sizeof(*cipt));
  464. if(!cipt)
  465. ret = CURLE_OUT_OF_MEMORY;
  466. else {
  467. cipt->certinfo = (struct curl_slist **)
  468. calloc(cipf->num_of_certs +
  469. 1, sizeof(struct curl_slist *));
  470. if(!cipt->certinfo)
  471. ret = CURLE_OUT_OF_MEMORY;
  472. else {
  473. int i;
  474. cipt->num_of_certs = cipf->num_of_certs;
  475. for(i = 0; i < cipf->num_of_certs; i++)
  476. if(cipf->certinfo[i])
  477. if(!(cipt->certinfo[i] = slist_convert(ccsid,
  478. cipf->certinfo[i],
  479. ASCII_CCSID))) {
  480. ret = CURLE_OUT_OF_MEMORY;
  481. break;
  482. }
  483. }
  484. }
  485. if(ret != CURLE_OK) {
  486. curl_certinfo_free_all(cipt);
  487. cipt = (struct curl_certinfo *) NULL;
  488. }
  489. *(struct curl_certinfo * *) paramp = cipt;
  490. }
  491. break;
  492. case CURLINFO_TLS_SESSION:
  493. case CURLINFO_TLS_SSL_PTR:
  494. case CURLINFO_SOCKET:
  495. break;
  496. default:
  497. slp = (struct curl_slist **) paramp;
  498. if(*slp) {
  499. *slp = slist_convert(ccsid, *slp, ASCII_CCSID);
  500. if(!*slp)
  501. ret = CURLE_OUT_OF_MEMORY;
  502. }
  503. break;
  504. }
  505. }
  506. }
  507. va_end(arg);
  508. return ret;
  509. }
  510. static int
  511. Curl_is_formadd_string(CURLformoption option)
  512. {
  513. switch(option) {
  514. case CURLFORM_FILENAME:
  515. case CURLFORM_CONTENTTYPE:
  516. case CURLFORM_BUFFER:
  517. case CURLFORM_FILE:
  518. case CURLFORM_FILECONTENT:
  519. case CURLFORM_COPYCONTENTS:
  520. case CURLFORM_COPYNAME:
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. static void
  526. Curl_formadd_release_local(struct curl_forms *forms, int nargs, int skip)
  527. {
  528. while(nargs--)
  529. if(nargs != skip)
  530. if(Curl_is_formadd_string(forms[nargs].option))
  531. if(forms[nargs].value)
  532. free((char *) forms[nargs].value);
  533. free((char *) forms);
  534. }
  535. static int
  536. Curl_formadd_convert(struct curl_forms *forms,
  537. int formx, int lengthx, unsigned int ccsid)
  538. {
  539. int l;
  540. char *cp;
  541. char *cp2;
  542. if(formx < 0 || !forms[formx].value)
  543. return 0;
  544. if(lengthx >= 0)
  545. l = (int) forms[lengthx].value;
  546. else
  547. l = strlen(forms[formx].value) + 1;
  548. cp = malloc(MAX_CONV_EXPANSION * l);
  549. if(!cp)
  550. return -1;
  551. l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
  552. forms[formx].value, l, ccsid);
  553. if(l < 0) {
  554. free(cp);
  555. return -1;
  556. }
  557. cp2 = realloc(cp, l); /* Shorten buffer to the string size. */
  558. if(cp2)
  559. cp = cp2;
  560. forms[formx].value = cp;
  561. if(lengthx >= 0)
  562. forms[lengthx].value = (char *) l; /* Update length after conversion. */
  563. return l;
  564. }
  565. CURLFORMcode
  566. curl_formadd_ccsid(struct curl_httppost **httppost,
  567. struct curl_httppost **last_post, ...)
  568. {
  569. va_list arg;
  570. CURLformoption option;
  571. CURLFORMcode result;
  572. struct curl_forms *forms;
  573. struct curl_forms *lforms;
  574. struct curl_forms *tforms;
  575. unsigned int lformlen;
  576. const char *value;
  577. unsigned int ccsid;
  578. int nargs;
  579. int namex;
  580. int namelengthx;
  581. int contentx;
  582. int lengthx;
  583. unsigned int contentccsid;
  584. unsigned int nameccsid;
  585. /* A single curl_formadd() call cannot be split in several calls to deal
  586. with all parameters: the original parameters are thus copied to a local
  587. curl_forms array and converted to ASCII when needed.
  588. CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
  589. CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
  590. parameters is not defined; for this reason, the actual conversion is
  591. delayed to the end of parameter processing. The same applies to
  592. CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
  593. several times in the parameter list; the problem resides here in knowing
  594. which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
  595. when we can be sure to have both info for conversion: end of parameter
  596. list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
  597. natural separator between content data definitions; this seems to be
  598. in accordance with FormAdd() behavior. */
  599. /* Allocate the local curl_forms array. */
  600. lformlen = ALLOC_GRANULE;
  601. lforms = malloc(lformlen * sizeof(*lforms));
  602. if(!lforms)
  603. return CURL_FORMADD_MEMORY;
  604. /* Process the arguments, copying them into local array, latching conversion
  605. indexes and converting when needed. */
  606. result = CURL_FORMADD_OK;
  607. nargs = 0;
  608. contentx = -1;
  609. lengthx = -1;
  610. namex = -1;
  611. namelengthx = -1;
  612. forms = (struct curl_forms *) NULL;
  613. va_start(arg, last_post);
  614. for(;;) {
  615. /* Make sure there is still room for an item in local array. */
  616. if(nargs >= lformlen) {
  617. lformlen += ALLOC_GRANULE;
  618. tforms = realloc(lforms, lformlen * sizeof(*lforms));
  619. if(!tforms) {
  620. result = CURL_FORMADD_MEMORY;
  621. break;
  622. }
  623. lforms = tforms;
  624. }
  625. /* Get next option. */
  626. if(forms) {
  627. /* Get option from array. */
  628. option = forms->option;
  629. value = forms->value;
  630. forms++;
  631. }
  632. else {
  633. /* Get option from arguments. */
  634. option = va_arg(arg, CURLformoption);
  635. if(option == CURLFORM_END)
  636. break;
  637. }
  638. /* Dispatch by option. */
  639. switch(option) {
  640. case CURLFORM_END:
  641. forms = (struct curl_forms *) NULL; /* Leave array mode. */
  642. continue;
  643. case CURLFORM_ARRAY:
  644. if(!forms) {
  645. forms = va_arg(arg, struct curl_forms *);
  646. continue;
  647. }
  648. result = CURL_FORMADD_ILLEGAL_ARRAY;
  649. break;
  650. case CURLFORM_COPYNAME:
  651. option = CURLFORM_PTRNAME; /* Static for now. */
  652. case CURLFORM_PTRNAME:
  653. if(namex >= 0)
  654. result = CURL_FORMADD_OPTION_TWICE;
  655. namex = nargs;
  656. if(!forms) {
  657. value = va_arg(arg, char *);
  658. nameccsid = (unsigned int) va_arg(arg, long);
  659. }
  660. else {
  661. nameccsid = (unsigned int) forms->value;
  662. forms++;
  663. }
  664. break;
  665. case CURLFORM_COPYCONTENTS:
  666. if(contentx >= 0)
  667. result = CURL_FORMADD_OPTION_TWICE;
  668. contentx = nargs;
  669. if(!forms) {
  670. value = va_arg(arg, char *);
  671. contentccsid = (unsigned int) va_arg(arg, long);
  672. }
  673. else {
  674. contentccsid = (unsigned int) forms->value;
  675. forms++;
  676. }
  677. break;
  678. case CURLFORM_PTRCONTENTS:
  679. case CURLFORM_BUFFERPTR:
  680. if(!forms)
  681. value = va_arg(arg, char *); /* No conversion. */
  682. break;
  683. case CURLFORM_CONTENTSLENGTH:
  684. lengthx = nargs;
  685. if(!forms)
  686. value = (char *) va_arg(arg, long);
  687. break;
  688. case CURLFORM_CONTENTLEN:
  689. lengthx = nargs;
  690. if(!forms)
  691. value = (char *) va_arg(arg, curl_off_t);
  692. break;
  693. case CURLFORM_NAMELENGTH:
  694. namelengthx = nargs;
  695. if(!forms)
  696. value = (char *) va_arg(arg, long);
  697. break;
  698. case CURLFORM_BUFFERLENGTH:
  699. if(!forms)
  700. value = (char *) va_arg(arg, long);
  701. break;
  702. case CURLFORM_CONTENTHEADER:
  703. if(!forms)
  704. value = (char *) va_arg(arg, struct curl_slist *);
  705. break;
  706. case CURLFORM_STREAM:
  707. if(!forms)
  708. value = (char *) va_arg(arg, void *);
  709. break;
  710. case CURLFORM_CONTENTTYPE:
  711. /* If a previous content has been encountered, convert it now. */
  712. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
  713. result = CURL_FORMADD_MEMORY;
  714. break;
  715. }
  716. contentx = -1;
  717. lengthx = -1;
  718. /* Fall into default. */
  719. default:
  720. /* Must be a convertible string. */
  721. if(!Curl_is_formadd_string(option)) {
  722. result = CURL_FORMADD_UNKNOWN_OPTION;
  723. break;
  724. }
  725. if(!forms) {
  726. value = va_arg(arg, char *);
  727. ccsid = (unsigned int) va_arg(arg, long);
  728. }
  729. else {
  730. ccsid = (unsigned int) forms->value;
  731. forms++;
  732. }
  733. /* Do the conversion. */
  734. lforms[nargs].value = value;
  735. if(Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
  736. result = CURL_FORMADD_MEMORY;
  737. break;
  738. }
  739. value = lforms[nargs].value;
  740. }
  741. if(result != CURL_FORMADD_OK)
  742. break;
  743. lforms[nargs].value = value;
  744. lforms[nargs++].option = option;
  745. }
  746. va_end(arg);
  747. /* Convert the name and the last content, now that we know their lengths. */
  748. if(result == CURL_FORMADD_OK && namex >= 0) {
  749. if(Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
  750. result = CURL_FORMADD_MEMORY;
  751. else
  752. lforms[namex].option = CURLFORM_COPYNAME; /* Force copy. */
  753. }
  754. if(result == CURL_FORMADD_OK) {
  755. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
  756. result = CURL_FORMADD_MEMORY;
  757. else
  758. contentx = -1;
  759. }
  760. /* Do the formadd with our converted parameters. */
  761. if(result == CURL_FORMADD_OK) {
  762. lforms[nargs].option = CURLFORM_END;
  763. result = curl_formadd(httppost, last_post,
  764. CURLFORM_ARRAY, lforms, CURLFORM_END);
  765. }
  766. /* Terminate. */
  767. Curl_formadd_release_local(lforms, nargs, contentx);
  768. return result;
  769. }
  770. struct cfcdata {
  771. curl_formget_callback append;
  772. void * arg;
  773. unsigned int ccsid;
  774. };
  775. static size_t
  776. Curl_formget_callback_ccsid(void *arg, const char *buf, size_t len)
  777. {
  778. struct cfcdata *p;
  779. char *b;
  780. int l;
  781. size_t ret;
  782. p = (struct cfcdata *) arg;
  783. if((long) len <= 0)
  784. return (*p->append)(p->arg, buf, len);
  785. b = malloc(MAX_CONV_EXPANSION * len);
  786. if(!b)
  787. return (size_t) -1;
  788. l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
  789. if(l < 0) {
  790. free(b);
  791. return (size_t) -1;
  792. }
  793. ret = (*p->append)(p->arg, b, l);
  794. free(b);
  795. return ret == l ? len : -1;
  796. }
  797. int
  798. curl_formget_ccsid(struct curl_httppost *form, void *arg,
  799. curl_formget_callback append, unsigned int ccsid)
  800. {
  801. struct cfcdata lcfc;
  802. lcfc.append = append;
  803. lcfc.arg = arg;
  804. lcfc.ccsid = ccsid;
  805. return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
  806. }
  807. CURLcode
  808. curl_easy_setopt_ccsid(CURL *easy, CURLoption tag, ...)
  809. {
  810. CURLcode result;
  811. va_list arg;
  812. char *s;
  813. char *cp = NULL;
  814. unsigned int ccsid;
  815. curl_off_t pfsize;
  816. struct Curl_easy *data = easy;
  817. va_start(arg, tag);
  818. switch(tag) {
  819. /* BEGIN TRANSLATABLE STRING OPTIONS */
  820. /* Keep option symbols in alphanumeric order and retain the BEGIN/END
  821. armor comments. */
  822. case CURLOPT_ABSTRACT_UNIX_SOCKET:
  823. case CURLOPT_ACCEPT_ENCODING:
  824. case CURLOPT_ALTSVC:
  825. case CURLOPT_AWS_SIGV4:
  826. case CURLOPT_CAINFO:
  827. case CURLOPT_CAPATH:
  828. case CURLOPT_COOKIE:
  829. case CURLOPT_COOKIEFILE:
  830. case CURLOPT_COOKIEJAR:
  831. case CURLOPT_COOKIELIST:
  832. case CURLOPT_CRLFILE:
  833. case CURLOPT_CUSTOMREQUEST:
  834. case CURLOPT_DEFAULT_PROTOCOL:
  835. case CURLOPT_DNS_INTERFACE:
  836. case CURLOPT_DNS_LOCAL_IP4:
  837. case CURLOPT_DNS_LOCAL_IP6:
  838. case CURLOPT_DNS_SERVERS:
  839. case CURLOPT_DOH_URL:
  840. case CURLOPT_ECH:
  841. case CURLOPT_EGDSOCKET:
  842. case CURLOPT_FTPPORT:
  843. case CURLOPT_FTP_ACCOUNT:
  844. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  845. case CURLOPT_HAPROXY_CLIENT_IP:
  846. case CURLOPT_HSTS:
  847. case CURLOPT_INTERFACE:
  848. case CURLOPT_ISSUERCERT:
  849. case CURLOPT_KEYPASSWD:
  850. case CURLOPT_KRBLEVEL:
  851. case CURLOPT_LOGIN_OPTIONS:
  852. case CURLOPT_MAIL_AUTH:
  853. case CURLOPT_MAIL_FROM:
  854. case CURLOPT_NETRC_FILE:
  855. case CURLOPT_NOPROXY:
  856. case CURLOPT_PASSWORD:
  857. case CURLOPT_PINNEDPUBLICKEY:
  858. case CURLOPT_PRE_PROXY:
  859. case CURLOPT_PROTOCOLS_STR:
  860. case CURLOPT_PROXY:
  861. case CURLOPT_PROXYPASSWORD:
  862. case CURLOPT_PROXYUSERNAME:
  863. case CURLOPT_PROXYUSERPWD:
  864. case CURLOPT_PROXY_CAINFO:
  865. case CURLOPT_PROXY_CAPATH:
  866. case CURLOPT_PROXY_CRLFILE:
  867. case CURLOPT_PROXY_ISSUERCERT:
  868. case CURLOPT_PROXY_KEYPASSWD:
  869. case CURLOPT_PROXY_PINNEDPUBLICKEY:
  870. case CURLOPT_PROXY_SERVICE_NAME:
  871. case CURLOPT_PROXY_SSLCERT:
  872. case CURLOPT_PROXY_SSLCERTTYPE:
  873. case CURLOPT_PROXY_SSLKEY:
  874. case CURLOPT_PROXY_SSLKEYTYPE:
  875. case CURLOPT_PROXY_SSL_CIPHER_LIST:
  876. case CURLOPT_PROXY_TLS13_CIPHERS:
  877. case CURLOPT_PROXY_TLSAUTH_PASSWORD:
  878. case CURLOPT_PROXY_TLSAUTH_TYPE:
  879. case CURLOPT_PROXY_TLSAUTH_USERNAME:
  880. case CURLOPT_RANDOM_FILE:
  881. case CURLOPT_RANGE:
  882. case CURLOPT_REDIR_PROTOCOLS_STR:
  883. case CURLOPT_REFERER:
  884. case CURLOPT_REQUEST_TARGET:
  885. case CURLOPT_RTSP_SESSION_ID:
  886. case CURLOPT_RTSP_STREAM_URI:
  887. case CURLOPT_RTSP_TRANSPORT:
  888. case CURLOPT_SASL_AUTHZID:
  889. case CURLOPT_SERVICE_NAME:
  890. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  891. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  892. case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
  893. case CURLOPT_SSH_KNOWNHOSTS:
  894. case CURLOPT_SSH_PRIVATE_KEYFILE:
  895. case CURLOPT_SSH_PUBLIC_KEYFILE:
  896. case CURLOPT_SSLCERT:
  897. case CURLOPT_SSLCERTTYPE:
  898. case CURLOPT_SSLENGINE:
  899. case CURLOPT_SSLKEY:
  900. case CURLOPT_SSLKEYTYPE:
  901. case CURLOPT_SSL_CIPHER_LIST:
  902. case CURLOPT_SSL_EC_CURVES:
  903. case CURLOPT_TLS13_CIPHERS:
  904. case CURLOPT_TLSAUTH_PASSWORD:
  905. case CURLOPT_TLSAUTH_TYPE:
  906. case CURLOPT_TLSAUTH_USERNAME:
  907. case CURLOPT_UNIX_SOCKET_PATH:
  908. case CURLOPT_URL:
  909. case CURLOPT_USERAGENT:
  910. case CURLOPT_USERNAME:
  911. case CURLOPT_USERPWD:
  912. case CURLOPT_XOAUTH2_BEARER:
  913. /* END TRANSLATABLE STRING OPTIONS */
  914. s = va_arg(arg, char *);
  915. ccsid = va_arg(arg, unsigned int);
  916. if(s) {
  917. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  918. if(!s) {
  919. result = CURLE_OUT_OF_MEMORY;
  920. break;
  921. }
  922. }
  923. result = curl_easy_setopt(easy, tag, s);
  924. free(s);
  925. break;
  926. case CURLOPT_COPYPOSTFIELDS:
  927. /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
  928. prior to this call. In this case, convert the given byte count and
  929. replace the length according to the conversion result. */
  930. s = va_arg(arg, char *);
  931. ccsid = va_arg(arg, unsigned int);
  932. pfsize = data->set.postfieldsize;
  933. if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
  934. result = curl_easy_setopt(easy, CURLOPT_COPYPOSTFIELDS, s);
  935. break;
  936. }
  937. if(pfsize == -1) {
  938. /* Data is null-terminated. */
  939. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  940. if(!s) {
  941. result = CURLE_OUT_OF_MEMORY;
  942. break;
  943. }
  944. }
  945. else {
  946. /* Data length specified. */
  947. size_t len;
  948. if(pfsize < 0 || pfsize > SIZE_MAX) {
  949. result = CURLE_OUT_OF_MEMORY;
  950. break;
  951. }
  952. len = pfsize;
  953. pfsize = len * MAX_CONV_EXPANSION;
  954. if(pfsize > SIZE_MAX)
  955. pfsize = SIZE_MAX;
  956. cp = malloc(pfsize);
  957. if(!cp) {
  958. result = CURLE_OUT_OF_MEMORY;
  959. break;
  960. }
  961. pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
  962. if(pfsize < 0) {
  963. result = CURLE_OUT_OF_MEMORY;
  964. break;
  965. }
  966. data->set.postfieldsize = pfsize; /* Replace data size. */
  967. s = cp;
  968. }
  969. result = curl_easy_setopt(easy, CURLOPT_POSTFIELDS, s);
  970. data->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
  971. break;
  972. default:
  973. if(tag / 10000 == CURLOPTTYPE_BLOB) {
  974. struct curl_blob *bp = va_arg(arg, struct curl_blob *);
  975. struct curl_blob blob;
  976. ccsid = va_arg(arg, unsigned int);
  977. if(bp && bp->data && bp->len &&
  978. ccsid != NOCONV_CCSID && ccsid != ASCII_CCSID) {
  979. pfsize = (curl_off_t) bp->len * MAX_CONV_EXPANSION;
  980. if(pfsize > SIZE_MAX)
  981. pfsize = SIZE_MAX;
  982. cp = malloc(pfsize);
  983. if(!cp) {
  984. result = CURLE_OUT_OF_MEMORY;
  985. break;
  986. }
  987. pfsize = convert(cp, pfsize, ASCII_CCSID, bp->data, bp->len, ccsid);
  988. if(pfsize < 0) {
  989. result = CURLE_OUT_OF_MEMORY;
  990. break;
  991. }
  992. blob.data = cp;
  993. blob.len = pfsize;
  994. blob.flags = bp->flags | CURL_BLOB_COPY;
  995. bp = &blob;
  996. }
  997. result = curl_easy_setopt(easy, tag, &blob);
  998. break;
  999. }
  1000. FALLTHROUGH();
  1001. case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
  1002. result = Curl_vsetopt(easy, tag, arg);
  1003. break;
  1004. }
  1005. va_end(arg);
  1006. free(cp);
  1007. return result;
  1008. }
  1009. /* ILE/RPG helper functions. */
  1010. char *
  1011. curl_form_long_value(long value)
  1012. {
  1013. /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
  1014. return (char *) value;
  1015. }
  1016. CURLcode
  1017. curl_easy_setopt_RPGnum_(CURL *easy, CURLoption tag, curl_off_t arg)
  1018. {
  1019. /* ILE/RPG procedure overloading cannot discriminate between different
  1020. size and/or signedness of format arguments. This provides a generic
  1021. wrapper that adapts size to the given tag expectation.
  1022. This procedure is not intended to be explicitly called from user code. */
  1023. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1024. return curl_easy_setopt(easy, tag, (long) arg);
  1025. return curl_easy_setopt(easy, tag, arg);
  1026. }
  1027. CURLcode
  1028. curl_multi_setopt_RPGnum_(CURLM *multi, CURLMoption tag, curl_off_t arg)
  1029. {
  1030. /* Likewise, for multi handle. */
  1031. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1032. return curl_multi_setopt(multi, tag, (long) arg);
  1033. return curl_multi_setopt(multi, tag, arg);
  1034. }
  1035. char *
  1036. curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
  1037. size_t num, unsigned int ccsid)
  1038. {
  1039. char *d = (char *) NULL;
  1040. char *s = curl_pushheader_bynum(h, num);
  1041. if(s)
  1042. d = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1043. return d;
  1044. }
  1045. char *
  1046. curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
  1047. unsigned int ccsidin, unsigned int ccsidout)
  1048. {
  1049. char *d = (char *) NULL;
  1050. if(header) {
  1051. header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
  1052. if(header) {
  1053. char *s = curl_pushheader_byname(h, header);
  1054. free((char *) header);
  1055. if(s)
  1056. d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
  1057. }
  1058. }
  1059. return d;
  1060. }
  1061. static CURLcode
  1062. mime_string_call(curl_mimepart *part, const char *string, unsigned int ccsid,
  1063. CURLcode (*mimefunc)(curl_mimepart *part, const char *string))
  1064. {
  1065. char *s = (char *) NULL;
  1066. CURLcode result;
  1067. if(!string)
  1068. return mimefunc(part, string);
  1069. s = dynconvert(ASCII_CCSID, string, -1, ccsid);
  1070. if(!s)
  1071. return CURLE_OUT_OF_MEMORY;
  1072. result = mimefunc(part, s);
  1073. free(s);
  1074. return result;
  1075. }
  1076. CURLcode
  1077. curl_mime_name_ccsid(curl_mimepart *part, const char *name, unsigned int ccsid)
  1078. {
  1079. return mime_string_call(part, name, ccsid, curl_mime_name);
  1080. }
  1081. CURLcode
  1082. curl_mime_filename_ccsid(curl_mimepart *part,
  1083. const char *filename, unsigned int ccsid)
  1084. {
  1085. return mime_string_call(part, filename, ccsid, curl_mime_filename);
  1086. }
  1087. CURLcode
  1088. curl_mime_type_ccsid(curl_mimepart *part,
  1089. const char *mimetype, unsigned int ccsid)
  1090. {
  1091. return mime_string_call(part, mimetype, ccsid, curl_mime_type);
  1092. }
  1093. CURLcode
  1094. curl_mime_encoder_ccsid(curl_mimepart *part,
  1095. const char *encoding, unsigned int ccsid)
  1096. {
  1097. return mime_string_call(part, encoding, ccsid, curl_mime_encoder);
  1098. }
  1099. CURLcode
  1100. curl_mime_filedata_ccsid(curl_mimepart *part,
  1101. const char *filename, unsigned int ccsid)
  1102. {
  1103. return mime_string_call(part, filename, ccsid, curl_mime_filedata);
  1104. }
  1105. CURLcode
  1106. curl_mime_data_ccsid(curl_mimepart *part,
  1107. const char *data, size_t datasize, unsigned int ccsid)
  1108. {
  1109. char *s = (char *) NULL;
  1110. CURLcode result;
  1111. if(!data)
  1112. return curl_mime_data(part, data, datasize);
  1113. s = dynconvert(ASCII_CCSID, data, datasize, ccsid);
  1114. if(!s)
  1115. return CURLE_OUT_OF_MEMORY;
  1116. result = curl_mime_data(part, s, datasize);
  1117. free(s);
  1118. return result;
  1119. }
  1120. CURLUcode
  1121. curl_url_get_ccsid(CURLU *handle, CURLUPart what, char **part,
  1122. unsigned int flags, unsigned int ccsid)
  1123. {
  1124. char *s = (char *)NULL;
  1125. CURLUcode result;
  1126. if(!part)
  1127. return CURLUE_BAD_PARTPOINTER;
  1128. *part = (char *)NULL;
  1129. result = curl_url_get(handle, what, &s, flags);
  1130. if(result == CURLUE_OK) {
  1131. if(s) {
  1132. *part = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1133. if(!*part)
  1134. result = CURLUE_OUT_OF_MEMORY;
  1135. }
  1136. }
  1137. if(s)
  1138. free(s);
  1139. return result;
  1140. }
  1141. CURLUcode
  1142. curl_url_set_ccsid(CURLU *handle, CURLUPart what, const char *part,
  1143. unsigned int flags, unsigned int ccsid)
  1144. {
  1145. char *s = (char *)NULL;
  1146. CURLUcode result;
  1147. if(part) {
  1148. s = dynconvert(ASCII_CCSID, part, -1, ccsid);
  1149. if(!s)
  1150. return CURLUE_OUT_OF_MEMORY;
  1151. }
  1152. result = curl_url_set(handle, what, s, flags);
  1153. if(s)
  1154. free(s);
  1155. return result;
  1156. }
  1157. const struct curl_easyoption *
  1158. curl_easy_option_by_name_ccsid(const char *name, unsigned int ccsid)
  1159. {
  1160. const struct curl_easyoption *option = NULL;
  1161. if(name) {
  1162. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1163. if(s) {
  1164. option = curl_easy_option_by_name(s);
  1165. free(s);
  1166. }
  1167. }
  1168. return option;
  1169. }
  1170. /* Return option name in the given ccsid. */
  1171. const char *
  1172. curl_easy_option_get_name_ccsid(const struct curl_easyoption *option,
  1173. unsigned int ccsid)
  1174. {
  1175. char *name = NULL;
  1176. if(option && option->name)
  1177. name = dynconvert(ccsid, option->name, -1, ASCII_CCSID);
  1178. return (const char *) name;
  1179. }
  1180. /* Header API CCSID support. */
  1181. CURLHcode
  1182. curl_easy_header_ccsid(CURL *easy, const char *name, size_t index,
  1183. unsigned int origin, int request,
  1184. struct curl_header **hout, unsigned int ccsid)
  1185. {
  1186. CURLHcode result = CURLHE_BAD_ARGUMENT;
  1187. if(name) {
  1188. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1189. result = CURLHE_OUT_OF_MEMORY;
  1190. if(s) {
  1191. result = curl_easy_header(easy, s, index, origin, request, hout);
  1192. free(s);
  1193. }
  1194. }
  1195. return result;
  1196. }