ccsidcurl.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. };
  339. /* The assertion below is possible, because although the second operand
  340. is an enum member, the first is a #define. In that case, the OS/400 C
  341. compiler seems to compare string values after substitution. */
  342. #if CURLVERSION_NOW != CURLVERSION_ELEVENTH
  343. #error curl_version_info_data structure has changed: upgrade this procedure.
  344. #endif
  345. /* If caller has been compiled with a newer version, error. */
  346. if(stamp > CURLVERSION_NOW)
  347. return (curl_version_info_data *) NULL;
  348. p = curl_version_info(stamp);
  349. if(!p)
  350. return p;
  351. /* Measure thread space needed. */
  352. n = 0;
  353. nproto = 0;
  354. if(p->protocols) {
  355. while(p->protocols[nproto])
  356. n += strlen(p->protocols[nproto++]);
  357. n += nproto++;
  358. }
  359. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  360. cpp = (const char **) ((char *) p + charfields[i]);
  361. if(*cpp)
  362. n += strlen(*cpp) + 1;
  363. }
  364. /* Allocate thread space. */
  365. n *= MAX_CONV_EXPANSION;
  366. if(nproto)
  367. n += nproto * sizeof(const char *);
  368. cp = Curl_thread_buffer(LK_VERSION_INFO_DATA, n);
  369. id = (curl_version_info_data *) Curl_thread_buffer(LK_VERSION_INFO,
  370. sizeof(*id));
  371. if(!id || !cp)
  372. return (curl_version_info_data *) NULL;
  373. /* Copy data and convert strings. */
  374. memcpy((char *) id, (char *) p, sizeof(*p));
  375. if(id->protocols) {
  376. i = nproto * sizeof(id->protocols[0]);
  377. id->protocols = (const char * const *) cp;
  378. memcpy(cp, (char *) p->protocols, i);
  379. cp += i;
  380. n -= i;
  381. for(i = 0; id->protocols[i]; i++)
  382. if(convert_version_info_string(((const char * *) id->protocols) + i,
  383. &cp, &n, ccsid))
  384. return (curl_version_info_data *) NULL;
  385. }
  386. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  387. cpp = (const char **) ((char *) p + charfields[i]);
  388. if(*cpp && convert_version_info_string(cpp, &cp, &n, ccsid))
  389. return (curl_version_info_data *) NULL;
  390. }
  391. return id;
  392. }
  393. const char *
  394. curl_easy_strerror_ccsid(CURLcode error, unsigned int ccsid)
  395. {
  396. return keyed_string(LK_EASY_STRERROR, curl_easy_strerror(error), ccsid);
  397. }
  398. const char *
  399. curl_share_strerror_ccsid(CURLSHcode error, unsigned int ccsid)
  400. {
  401. return keyed_string(LK_SHARE_STRERROR, curl_share_strerror(error), ccsid);
  402. }
  403. const char *
  404. curl_multi_strerror_ccsid(CURLMcode error, unsigned int ccsid)
  405. {
  406. return keyed_string(LK_MULTI_STRERROR, curl_multi_strerror(error), ccsid);
  407. }
  408. const char *
  409. curl_url_strerror_ccsid(CURLUcode error, unsigned int ccsid)
  410. {
  411. return keyed_string(LK_URL_STRERROR, curl_url_strerror(error), ccsid);
  412. }
  413. void
  414. curl_certinfo_free_all(struct curl_certinfo *info)
  415. {
  416. /* Free all memory used by certificate info. */
  417. if(info) {
  418. if(info->certinfo) {
  419. int i;
  420. for(i = 0; i < info->num_of_certs; i++)
  421. curl_slist_free_all(info->certinfo[i]);
  422. free((char *) info->certinfo);
  423. }
  424. free((char *) info);
  425. }
  426. }
  427. CURLcode
  428. curl_easy_getinfo_ccsid(CURL *curl, CURLINFO info, ...)
  429. {
  430. va_list arg;
  431. void *paramp;
  432. CURLcode ret;
  433. struct Curl_easy *data;
  434. /* WARNING: unlike curl_easy_getinfo(), the strings returned by this
  435. procedure have to be free'ed. */
  436. data = (struct Curl_easy *) curl;
  437. va_start(arg, info);
  438. paramp = va_arg(arg, void *);
  439. ret = Curl_getinfo(data, info, paramp);
  440. if(ret == CURLE_OK) {
  441. unsigned int ccsid;
  442. char **cpp;
  443. struct curl_slist **slp;
  444. struct curl_certinfo *cipf;
  445. struct curl_certinfo *cipt;
  446. switch((int) info & CURLINFO_TYPEMASK) {
  447. case CURLINFO_STRING:
  448. ccsid = va_arg(arg, unsigned int);
  449. cpp = (char * *) paramp;
  450. if(*cpp) {
  451. *cpp = dynconvert(ccsid, *cpp, -1, ASCII_CCSID);
  452. if(!*cpp)
  453. ret = CURLE_OUT_OF_MEMORY;
  454. }
  455. break;
  456. case CURLINFO_SLIST:
  457. ccsid = va_arg(arg, unsigned int);
  458. switch(info) {
  459. case CURLINFO_CERTINFO:
  460. cipf = *(struct curl_certinfo * *) paramp;
  461. if(cipf) {
  462. cipt = (struct curl_certinfo *) malloc(sizeof(*cipt));
  463. if(!cipt)
  464. ret = CURLE_OUT_OF_MEMORY;
  465. else {
  466. cipt->certinfo = (struct curl_slist **)
  467. calloc(cipf->num_of_certs +
  468. 1, sizeof(struct curl_slist *));
  469. if(!cipt->certinfo)
  470. ret = CURLE_OUT_OF_MEMORY;
  471. else {
  472. int i;
  473. cipt->num_of_certs = cipf->num_of_certs;
  474. for(i = 0; i < cipf->num_of_certs; i++)
  475. if(cipf->certinfo[i])
  476. if(!(cipt->certinfo[i] = slist_convert(ccsid,
  477. cipf->certinfo[i],
  478. ASCII_CCSID))) {
  479. ret = CURLE_OUT_OF_MEMORY;
  480. break;
  481. }
  482. }
  483. }
  484. if(ret != CURLE_OK) {
  485. curl_certinfo_free_all(cipt);
  486. cipt = (struct curl_certinfo *) NULL;
  487. }
  488. *(struct curl_certinfo * *) paramp = cipt;
  489. }
  490. break;
  491. case CURLINFO_TLS_SESSION:
  492. case CURLINFO_TLS_SSL_PTR:
  493. case CURLINFO_SOCKET:
  494. break;
  495. default:
  496. slp = (struct curl_slist **) paramp;
  497. if(*slp) {
  498. *slp = slist_convert(ccsid, *slp, ASCII_CCSID);
  499. if(!*slp)
  500. ret = CURLE_OUT_OF_MEMORY;
  501. }
  502. break;
  503. }
  504. }
  505. }
  506. va_end(arg);
  507. return ret;
  508. }
  509. static int
  510. Curl_is_formadd_string(CURLformoption option)
  511. {
  512. switch(option) {
  513. case CURLFORM_FILENAME:
  514. case CURLFORM_CONTENTTYPE:
  515. case CURLFORM_BUFFER:
  516. case CURLFORM_FILE:
  517. case CURLFORM_FILECONTENT:
  518. case CURLFORM_COPYCONTENTS:
  519. case CURLFORM_COPYNAME:
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. static void
  525. Curl_formadd_release_local(struct curl_forms *forms, int nargs, int skip)
  526. {
  527. while(nargs--)
  528. if(nargs != skip)
  529. if(Curl_is_formadd_string(forms[nargs].option))
  530. if(forms[nargs].value)
  531. free((char *) forms[nargs].value);
  532. free((char *) forms);
  533. }
  534. static int
  535. Curl_formadd_convert(struct curl_forms *forms,
  536. int formx, int lengthx, unsigned int ccsid)
  537. {
  538. int l;
  539. char *cp;
  540. char *cp2;
  541. if(formx < 0 || !forms[formx].value)
  542. return 0;
  543. if(lengthx >= 0)
  544. l = (int) forms[lengthx].value;
  545. else
  546. l = strlen(forms[formx].value) + 1;
  547. cp = malloc(MAX_CONV_EXPANSION * l);
  548. if(!cp)
  549. return -1;
  550. l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
  551. forms[formx].value, l, ccsid);
  552. if(l < 0) {
  553. free(cp);
  554. return -1;
  555. }
  556. cp2 = realloc(cp, l); /* Shorten buffer to the string size. */
  557. if(cp2)
  558. cp = cp2;
  559. forms[formx].value = cp;
  560. if(lengthx >= 0)
  561. forms[lengthx].value = (char *) l; /* Update length after conversion. */
  562. return l;
  563. }
  564. CURLFORMcode
  565. curl_formadd_ccsid(struct curl_httppost **httppost,
  566. struct curl_httppost **last_post, ...)
  567. {
  568. va_list arg;
  569. CURLformoption option;
  570. CURLFORMcode result;
  571. struct curl_forms *forms;
  572. struct curl_forms *lforms;
  573. struct curl_forms *tforms;
  574. unsigned int lformlen;
  575. const char *value;
  576. unsigned int ccsid;
  577. int nargs;
  578. int namex;
  579. int namelengthx;
  580. int contentx;
  581. int lengthx;
  582. unsigned int contentccsid;
  583. unsigned int nameccsid;
  584. /* A single curl_formadd() call cannot be split in several calls to deal
  585. with all parameters: the original parameters are thus copied to a local
  586. curl_forms array and converted to ASCII when needed.
  587. CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
  588. CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
  589. parameters is not defined; for this reason, the actual conversion is
  590. delayed to the end of parameter processing. The same applies to
  591. CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
  592. several times in the parameter list; the problem resides here in knowing
  593. which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
  594. when we can be sure to have both info for conversion: end of parameter
  595. list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
  596. natural separator between content data definitions; this seems to be
  597. in accordance with FormAdd() behavior. */
  598. /* Allocate the local curl_forms array. */
  599. lformlen = ALLOC_GRANULE;
  600. lforms = malloc(lformlen * sizeof(*lforms));
  601. if(!lforms)
  602. return CURL_FORMADD_MEMORY;
  603. /* Process the arguments, copying them into local array, latching conversion
  604. indexes and converting when needed. */
  605. result = CURL_FORMADD_OK;
  606. nargs = 0;
  607. contentx = -1;
  608. lengthx = -1;
  609. namex = -1;
  610. namelengthx = -1;
  611. forms = (struct curl_forms *) NULL;
  612. va_start(arg, last_post);
  613. for(;;) {
  614. /* Make sure there is still room for an item in local array. */
  615. if(nargs >= lformlen) {
  616. lformlen += ALLOC_GRANULE;
  617. tforms = realloc(lforms, lformlen * sizeof(*lforms));
  618. if(!tforms) {
  619. result = CURL_FORMADD_MEMORY;
  620. break;
  621. }
  622. lforms = tforms;
  623. }
  624. /* Get next option. */
  625. if(forms) {
  626. /* Get option from array. */
  627. option = forms->option;
  628. value = forms->value;
  629. forms++;
  630. }
  631. else {
  632. /* Get option from arguments. */
  633. option = va_arg(arg, CURLformoption);
  634. if(option == CURLFORM_END)
  635. break;
  636. }
  637. /* Dispatch by option. */
  638. switch(option) {
  639. case CURLFORM_END:
  640. forms = (struct curl_forms *) NULL; /* Leave array mode. */
  641. continue;
  642. case CURLFORM_ARRAY:
  643. if(!forms) {
  644. forms = va_arg(arg, struct curl_forms *);
  645. continue;
  646. }
  647. result = CURL_FORMADD_ILLEGAL_ARRAY;
  648. break;
  649. case CURLFORM_COPYNAME:
  650. option = CURLFORM_PTRNAME; /* Static for now. */
  651. case CURLFORM_PTRNAME:
  652. if(namex >= 0)
  653. result = CURL_FORMADD_OPTION_TWICE;
  654. namex = nargs;
  655. if(!forms) {
  656. value = va_arg(arg, char *);
  657. nameccsid = (unsigned int) va_arg(arg, long);
  658. }
  659. else {
  660. nameccsid = (unsigned int) forms->value;
  661. forms++;
  662. }
  663. break;
  664. case CURLFORM_COPYCONTENTS:
  665. if(contentx >= 0)
  666. result = CURL_FORMADD_OPTION_TWICE;
  667. contentx = nargs;
  668. if(!forms) {
  669. value = va_arg(arg, char *);
  670. contentccsid = (unsigned int) va_arg(arg, long);
  671. }
  672. else {
  673. contentccsid = (unsigned int) forms->value;
  674. forms++;
  675. }
  676. break;
  677. case CURLFORM_PTRCONTENTS:
  678. case CURLFORM_BUFFERPTR:
  679. if(!forms)
  680. value = va_arg(arg, char *); /* No conversion. */
  681. break;
  682. case CURLFORM_CONTENTSLENGTH:
  683. lengthx = nargs;
  684. if(!forms)
  685. value = (char *) va_arg(arg, long);
  686. break;
  687. case CURLFORM_CONTENTLEN:
  688. lengthx = nargs;
  689. if(!forms)
  690. value = (char *) va_arg(arg, curl_off_t);
  691. break;
  692. case CURLFORM_NAMELENGTH:
  693. namelengthx = nargs;
  694. if(!forms)
  695. value = (char *) va_arg(arg, long);
  696. break;
  697. case CURLFORM_BUFFERLENGTH:
  698. if(!forms)
  699. value = (char *) va_arg(arg, long);
  700. break;
  701. case CURLFORM_CONTENTHEADER:
  702. if(!forms)
  703. value = (char *) va_arg(arg, struct curl_slist *);
  704. break;
  705. case CURLFORM_STREAM:
  706. if(!forms)
  707. value = (char *) va_arg(arg, void *);
  708. break;
  709. case CURLFORM_CONTENTTYPE:
  710. /* If a previous content has been encountered, convert it now. */
  711. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
  712. result = CURL_FORMADD_MEMORY;
  713. break;
  714. }
  715. contentx = -1;
  716. lengthx = -1;
  717. /* Fall into default. */
  718. default:
  719. /* Must be a convertible string. */
  720. if(!Curl_is_formadd_string(option)) {
  721. result = CURL_FORMADD_UNKNOWN_OPTION;
  722. break;
  723. }
  724. if(!forms) {
  725. value = va_arg(arg, char *);
  726. ccsid = (unsigned int) va_arg(arg, long);
  727. }
  728. else {
  729. ccsid = (unsigned int) forms->value;
  730. forms++;
  731. }
  732. /* Do the conversion. */
  733. lforms[nargs].value = value;
  734. if(Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
  735. result = CURL_FORMADD_MEMORY;
  736. break;
  737. }
  738. value = lforms[nargs].value;
  739. }
  740. if(result != CURL_FORMADD_OK)
  741. break;
  742. lforms[nargs].value = value;
  743. lforms[nargs++].option = option;
  744. }
  745. va_end(arg);
  746. /* Convert the name and the last content, now that we know their lengths. */
  747. if(result == CURL_FORMADD_OK && namex >= 0) {
  748. if(Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
  749. result = CURL_FORMADD_MEMORY;
  750. else
  751. lforms[namex].option = CURLFORM_COPYNAME; /* Force copy. */
  752. }
  753. if(result == CURL_FORMADD_OK) {
  754. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
  755. result = CURL_FORMADD_MEMORY;
  756. else
  757. contentx = -1;
  758. }
  759. /* Do the formadd with our converted parameters. */
  760. if(result == CURL_FORMADD_OK) {
  761. lforms[nargs].option = CURLFORM_END;
  762. result = curl_formadd(httppost, last_post,
  763. CURLFORM_ARRAY, lforms, CURLFORM_END);
  764. }
  765. /* Terminate. */
  766. Curl_formadd_release_local(lforms, nargs, contentx);
  767. return result;
  768. }
  769. struct cfcdata {
  770. curl_formget_callback append;
  771. void * arg;
  772. unsigned int ccsid;
  773. };
  774. static size_t
  775. Curl_formget_callback_ccsid(void *arg, const char *buf, size_t len)
  776. {
  777. struct cfcdata *p;
  778. char *b;
  779. int l;
  780. size_t ret;
  781. p = (struct cfcdata *) arg;
  782. if((long) len <= 0)
  783. return (*p->append)(p->arg, buf, len);
  784. b = malloc(MAX_CONV_EXPANSION * len);
  785. if(!b)
  786. return (size_t) -1;
  787. l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
  788. if(l < 0) {
  789. free(b);
  790. return (size_t) -1;
  791. }
  792. ret = (*p->append)(p->arg, b, l);
  793. free(b);
  794. return ret == l? len: -1;
  795. }
  796. int
  797. curl_formget_ccsid(struct curl_httppost *form, void *arg,
  798. curl_formget_callback append, unsigned int ccsid)
  799. {
  800. struct cfcdata lcfc;
  801. lcfc.append = append;
  802. lcfc.arg = arg;
  803. lcfc.ccsid = ccsid;
  804. return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
  805. }
  806. CURLcode
  807. curl_easy_setopt_ccsid(CURL *easy, CURLoption tag, ...)
  808. {
  809. CURLcode result;
  810. va_list arg;
  811. char *s;
  812. char *cp = NULL;
  813. unsigned int ccsid;
  814. curl_off_t pfsize;
  815. va_start(arg, tag);
  816. switch(tag) {
  817. /* BEGIN TRANSLATABLE STRING OPTIONS */
  818. /* Keep option symbols in alphanumeric order and retain the BEGIN/END
  819. armor comments. */
  820. case CURLOPT_ABSTRACT_UNIX_SOCKET:
  821. case CURLOPT_ACCEPT_ENCODING:
  822. case CURLOPT_ALTSVC:
  823. case CURLOPT_AWS_SIGV4:
  824. case CURLOPT_CAINFO:
  825. case CURLOPT_CAPATH:
  826. case CURLOPT_COOKIE:
  827. case CURLOPT_COOKIEFILE:
  828. case CURLOPT_COOKIEJAR:
  829. case CURLOPT_COOKIELIST:
  830. case CURLOPT_CRLFILE:
  831. case CURLOPT_CUSTOMREQUEST:
  832. case CURLOPT_DEFAULT_PROTOCOL:
  833. case CURLOPT_DNS_INTERFACE:
  834. case CURLOPT_DNS_LOCAL_IP4:
  835. case CURLOPT_DNS_LOCAL_IP6:
  836. case CURLOPT_DNS_SERVERS:
  837. case CURLOPT_DOH_URL:
  838. case CURLOPT_EGDSOCKET:
  839. case CURLOPT_FTPPORT:
  840. case CURLOPT_FTP_ACCOUNT:
  841. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  842. case CURLOPT_HAPROXY_CLIENT_IP:
  843. case CURLOPT_HSTS:
  844. case CURLOPT_INTERFACE:
  845. case CURLOPT_ISSUERCERT:
  846. case CURLOPT_KEYPASSWD:
  847. case CURLOPT_KRBLEVEL:
  848. case CURLOPT_LOGIN_OPTIONS:
  849. case CURLOPT_MAIL_AUTH:
  850. case CURLOPT_MAIL_FROM:
  851. case CURLOPT_NETRC_FILE:
  852. case CURLOPT_NOPROXY:
  853. case CURLOPT_PASSWORD:
  854. case CURLOPT_PINNEDPUBLICKEY:
  855. case CURLOPT_PRE_PROXY:
  856. case CURLOPT_PROTOCOLS_STR:
  857. case CURLOPT_PROXY:
  858. case CURLOPT_PROXYPASSWORD:
  859. case CURLOPT_PROXYUSERNAME:
  860. case CURLOPT_PROXYUSERPWD:
  861. case CURLOPT_PROXY_CAINFO:
  862. case CURLOPT_PROXY_CAPATH:
  863. case CURLOPT_PROXY_CRLFILE:
  864. case CURLOPT_PROXY_ISSUERCERT:
  865. case CURLOPT_PROXY_KEYPASSWD:
  866. case CURLOPT_PROXY_PINNEDPUBLICKEY:
  867. case CURLOPT_PROXY_SERVICE_NAME:
  868. case CURLOPT_PROXY_SSLCERT:
  869. case CURLOPT_PROXY_SSLCERTTYPE:
  870. case CURLOPT_PROXY_SSLKEY:
  871. case CURLOPT_PROXY_SSLKEYTYPE:
  872. case CURLOPT_PROXY_SSL_CIPHER_LIST:
  873. case CURLOPT_PROXY_TLS13_CIPHERS:
  874. case CURLOPT_PROXY_TLSAUTH_PASSWORD:
  875. case CURLOPT_PROXY_TLSAUTH_TYPE:
  876. case CURLOPT_PROXY_TLSAUTH_USERNAME:
  877. case CURLOPT_RANDOM_FILE:
  878. case CURLOPT_RANGE:
  879. case CURLOPT_REDIR_PROTOCOLS_STR:
  880. case CURLOPT_REFERER:
  881. case CURLOPT_REQUEST_TARGET:
  882. case CURLOPT_RTSP_SESSION_ID:
  883. case CURLOPT_RTSP_STREAM_URI:
  884. case CURLOPT_RTSP_TRANSPORT:
  885. case CURLOPT_SASL_AUTHZID:
  886. case CURLOPT_SERVICE_NAME:
  887. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  888. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  889. case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
  890. case CURLOPT_SSH_KNOWNHOSTS:
  891. case CURLOPT_SSH_PRIVATE_KEYFILE:
  892. case CURLOPT_SSH_PUBLIC_KEYFILE:
  893. case CURLOPT_SSLCERT:
  894. case CURLOPT_SSLCERTTYPE:
  895. case CURLOPT_SSLENGINE:
  896. case CURLOPT_SSLKEY:
  897. case CURLOPT_SSLKEYTYPE:
  898. case CURLOPT_SSL_CIPHER_LIST:
  899. case CURLOPT_SSL_EC_CURVES:
  900. case CURLOPT_TLS13_CIPHERS:
  901. case CURLOPT_TLSAUTH_PASSWORD:
  902. case CURLOPT_TLSAUTH_TYPE:
  903. case CURLOPT_TLSAUTH_USERNAME:
  904. case CURLOPT_UNIX_SOCKET_PATH:
  905. case CURLOPT_URL:
  906. case CURLOPT_USERAGENT:
  907. case CURLOPT_USERNAME:
  908. case CURLOPT_USERPWD:
  909. case CURLOPT_XOAUTH2_BEARER:
  910. /* END TRANSLATABLE STRING OPTIONS */
  911. s = va_arg(arg, char *);
  912. ccsid = va_arg(arg, unsigned int);
  913. if(s) {
  914. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  915. if(!s) {
  916. result = CURLE_OUT_OF_MEMORY;
  917. break;
  918. }
  919. }
  920. result = curl_easy_setopt(easy, tag, s);
  921. free(s);
  922. break;
  923. case CURLOPT_COPYPOSTFIELDS:
  924. /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
  925. prior to this call. In this case, convert the given byte count and
  926. replace the length according to the conversion result. */
  927. s = va_arg(arg, char *);
  928. ccsid = va_arg(arg, unsigned int);
  929. pfsize = easy->set.postfieldsize;
  930. if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
  931. result = curl_easy_setopt(easy, CURLOPT_COPYPOSTFIELDS, s);
  932. break;
  933. }
  934. if(pfsize == -1) {
  935. /* Data is null-terminated. */
  936. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  937. if(!s) {
  938. result = CURLE_OUT_OF_MEMORY;
  939. break;
  940. }
  941. }
  942. else {
  943. /* Data length specified. */
  944. size_t len;
  945. if(pfsize < 0 || pfsize > SIZE_MAX) {
  946. result = CURLE_OUT_OF_MEMORY;
  947. break;
  948. }
  949. len = pfsize;
  950. pfsize = len * MAX_CONV_EXPANSION;
  951. if(pfsize > SIZE_MAX)
  952. pfsize = SIZE_MAX;
  953. cp = malloc(pfsize);
  954. if(!cp) {
  955. result = CURLE_OUT_OF_MEMORY;
  956. break;
  957. }
  958. pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
  959. if(pfsize < 0) {
  960. result = CURLE_OUT_OF_MEMORY;
  961. break;
  962. }
  963. easy->set.postfieldsize = pfsize; /* Replace data size. */
  964. s = cp;
  965. }
  966. result = curl_easy_setopt(easy, CURLOPT_POSTFIELDS, s);
  967. easy->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
  968. break;
  969. default:
  970. if(tag / 10000 == CURLOPTTYPE_BLOB) {
  971. struct curl_blob *bp = va_arg(arg, struct curl_blob *);
  972. struct curl_blob blob;
  973. ccsid = va_arg(arg, unsigned int);
  974. if(bp && bp->data && bp->len &&
  975. ccsid != NOCONV_CCSID && ccsid != ASCII_CCSID) {
  976. pfsize = (curl_off_t) bp->len * MAX_CONV_EXPANSION;
  977. if(pfsize > SIZE_MAX)
  978. pfsize = SIZE_MAX;
  979. cp = malloc(pfsize);
  980. if(!cp) {
  981. result = CURLE_OUT_OF_MEMORY;
  982. break;
  983. }
  984. pfsize = convert(cp, pfsize, ASCII_CCSID, bp->data, bp->len, ccsid);
  985. if(pfsize < 0) {
  986. result = CURLE_OUT_OF_MEMORY;
  987. break;
  988. }
  989. blob.data = cp;
  990. blob.len = pfsize;
  991. blob.flags = bp->flags | CURL_BLOB_COPY;
  992. bp = &blob;
  993. }
  994. result = curl_easy_setopt(easy, tag, &blob);
  995. break;
  996. }
  997. /* FALLTHROUGH */
  998. case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
  999. result = Curl_vsetopt(easy, tag, arg);
  1000. break;
  1001. }
  1002. va_end(arg);
  1003. free(cp);
  1004. return result;
  1005. }
  1006. /* ILE/RPG helper functions. */
  1007. char *
  1008. curl_form_long_value(long value)
  1009. {
  1010. /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
  1011. return (char *) value;
  1012. }
  1013. CURLcode
  1014. curl_easy_setopt_RPGnum_(CURL *easy, CURLoption tag, curl_off_t arg)
  1015. {
  1016. /* ILE/RPG procedure overloading cannot discriminate between different
  1017. size and/or signedness of format arguments. This provides a generic
  1018. wrapper that adapts size to the given tag expectation.
  1019. This procedure is not intended to be explicitly called from user code. */
  1020. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1021. return curl_easy_setopt(easy, tag, (long) arg);
  1022. return curl_easy_setopt(easy, tag, arg);
  1023. }
  1024. CURLcode
  1025. curl_multi_setopt_RPGnum_(CURLM *multi, CURLMoption tag, curl_off_t arg)
  1026. {
  1027. /* Likewise, for multi handle. */
  1028. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1029. return curl_multi_setopt(multi, tag, (long) arg);
  1030. return curl_multi_setopt(multi, tag, arg);
  1031. }
  1032. char *
  1033. curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
  1034. size_t num, unsigned int ccsid)
  1035. {
  1036. char *d = (char *) NULL;
  1037. char *s = curl_pushheader_bynum(h, num);
  1038. if(s)
  1039. d = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1040. return d;
  1041. }
  1042. char *
  1043. curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
  1044. unsigned int ccsidin, unsigned int ccsidout)
  1045. {
  1046. char *d = (char *) NULL;
  1047. if(header) {
  1048. header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
  1049. if(header) {
  1050. char *s = curl_pushheader_byname(h, header);
  1051. free((char *) header);
  1052. if(s)
  1053. d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
  1054. }
  1055. }
  1056. return d;
  1057. }
  1058. static CURLcode
  1059. mime_string_call(curl_mimepart *part, const char *string, unsigned int ccsid,
  1060. CURLcode (*mimefunc)(curl_mimepart *part, const char *string))
  1061. {
  1062. char *s = (char *) NULL;
  1063. CURLcode result;
  1064. if(!string)
  1065. return mimefunc(part, string);
  1066. s = dynconvert(ASCII_CCSID, string, -1, ccsid);
  1067. if(!s)
  1068. return CURLE_OUT_OF_MEMORY;
  1069. result = mimefunc(part, s);
  1070. free(s);
  1071. return result;
  1072. }
  1073. CURLcode
  1074. curl_mime_name_ccsid(curl_mimepart *part, const char *name, unsigned int ccsid)
  1075. {
  1076. return mime_string_call(part, name, ccsid, curl_mime_name);
  1077. }
  1078. CURLcode
  1079. curl_mime_filename_ccsid(curl_mimepart *part,
  1080. const char *filename, unsigned int ccsid)
  1081. {
  1082. return mime_string_call(part, filename, ccsid, curl_mime_filename);
  1083. }
  1084. CURLcode
  1085. curl_mime_type_ccsid(curl_mimepart *part,
  1086. const char *mimetype, unsigned int ccsid)
  1087. {
  1088. return mime_string_call(part, mimetype, ccsid, curl_mime_type);
  1089. }
  1090. CURLcode
  1091. curl_mime_encoder_ccsid(curl_mimepart *part,
  1092. const char *encoding, unsigned int ccsid)
  1093. {
  1094. return mime_string_call(part, encoding, ccsid, curl_mime_encoder);
  1095. }
  1096. CURLcode
  1097. curl_mime_filedata_ccsid(curl_mimepart *part,
  1098. const char *filename, unsigned int ccsid)
  1099. {
  1100. return mime_string_call(part, filename, ccsid, curl_mime_filedata);
  1101. }
  1102. CURLcode
  1103. curl_mime_data_ccsid(curl_mimepart *part,
  1104. const char *data, size_t datasize, unsigned int ccsid)
  1105. {
  1106. char *s = (char *) NULL;
  1107. CURLcode result;
  1108. if(!data)
  1109. return curl_mime_data(part, data, datasize);
  1110. s = dynconvert(ASCII_CCSID, data, datasize, ccsid);
  1111. if(!s)
  1112. return CURLE_OUT_OF_MEMORY;
  1113. result = curl_mime_data(part, s, datasize);
  1114. free(s);
  1115. return result;
  1116. }
  1117. CURLUcode
  1118. curl_url_get_ccsid(CURLU *handle, CURLUPart what, char **part,
  1119. unsigned int flags, unsigned int ccsid)
  1120. {
  1121. char *s = (char *)NULL;
  1122. CURLUcode result;
  1123. if(!part)
  1124. return CURLUE_BAD_PARTPOINTER;
  1125. *part = (char *)NULL;
  1126. result = curl_url_get(handle, what, &s, flags);
  1127. if(result == CURLUE_OK) {
  1128. if(s) {
  1129. *part = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1130. if(!*part)
  1131. result = CURLUE_OUT_OF_MEMORY;
  1132. }
  1133. }
  1134. if(s)
  1135. free(s);
  1136. return result;
  1137. }
  1138. CURLUcode
  1139. curl_url_set_ccsid(CURLU *handle, CURLUPart what, const char *part,
  1140. unsigned int flags, unsigned int ccsid)
  1141. {
  1142. char *s = (char *)NULL;
  1143. CURLUcode result;
  1144. if(part) {
  1145. s = dynconvert(ASCII_CCSID, part, -1, ccsid);
  1146. if(!s)
  1147. return CURLUE_OUT_OF_MEMORY;
  1148. }
  1149. result = curl_url_set(handle, what, s, flags);
  1150. if(s)
  1151. free(s);
  1152. return result;
  1153. }
  1154. const struct curl_easyoption *
  1155. curl_easy_option_by_name_ccsid(const char *name, unsigned int ccsid)
  1156. {
  1157. const struct curl_easyoption *option = NULL;
  1158. if(name) {
  1159. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1160. if(s) {
  1161. option = curl_easy_option_by_name(s);
  1162. free(s);
  1163. }
  1164. }
  1165. return option;
  1166. }
  1167. /* Return option name in the given ccsid. */
  1168. const char *
  1169. curl_easy_option_get_name_ccsid(const struct curl_easyoption *option,
  1170. unsigned int ccsid)
  1171. {
  1172. char *name = NULL;
  1173. if(option && option->name)
  1174. name = dynconvert(ccsid, option->name, -1, ASCII_CCSID);
  1175. return (const char *) name;
  1176. }
  1177. /* Header API CCSID support. */
  1178. CURLHcode
  1179. curl_easy_header_ccsid(CURL *easy, const char *name, size_t index,
  1180. unsigned int origin, int request,
  1181. struct curl_header **hout, unsigned int ccsid)
  1182. {
  1183. CURLHcode result = CURLHE_BAD_ARGUMENT;
  1184. if(name) {
  1185. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1186. result = CURLHE_OUT_OF_MEMORY;
  1187. if(s) {
  1188. result = curl_easy_header(easy, s, index, origin, request, hout);
  1189. free(s);
  1190. }
  1191. }
  1192. return result;
  1193. }