ccsidcurl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. case CURLOPT_ABSTRACT_UNIX_SOCKET:
  818. case CURLOPT_ALTSVC:
  819. case CURLOPT_AWS_SIGV4:
  820. case CURLOPT_CAINFO:
  821. case CURLOPT_CAPATH:
  822. case CURLOPT_COOKIE:
  823. case CURLOPT_COOKIEFILE:
  824. case CURLOPT_COOKIEJAR:
  825. case CURLOPT_COOKIELIST:
  826. case CURLOPT_CRLFILE:
  827. case CURLOPT_CUSTOMREQUEST:
  828. case CURLOPT_DEFAULT_PROTOCOL:
  829. case CURLOPT_DNS_SERVERS:
  830. case CURLOPT_DNS_INTERFACE:
  831. case CURLOPT_DNS_LOCAL_IP4:
  832. case CURLOPT_DNS_LOCAL_IP6:
  833. case CURLOPT_DOH_URL:
  834. case CURLOPT_EGDSOCKET:
  835. case CURLOPT_ENCODING:
  836. case CURLOPT_FTPPORT:
  837. case CURLOPT_FTP_ACCOUNT:
  838. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  839. case CURLOPT_HSTS:
  840. case CURLOPT_INTERFACE:
  841. case CURLOPT_ISSUERCERT:
  842. case CURLOPT_KEYPASSWD:
  843. case CURLOPT_KRBLEVEL:
  844. case CURLOPT_LOGIN_OPTIONS:
  845. case CURLOPT_MAIL_AUTH:
  846. case CURLOPT_MAIL_FROM:
  847. case CURLOPT_NETRC_FILE:
  848. case CURLOPT_NOPROXY:
  849. case CURLOPT_PASSWORD:
  850. case CURLOPT_PINNEDPUBLICKEY:
  851. case CURLOPT_PRE_PROXY:
  852. case CURLOPT_PROTOCOLS_STR:
  853. case CURLOPT_PROXY:
  854. case CURLOPT_PROXYPASSWORD:
  855. case CURLOPT_PROXYUSERNAME:
  856. case CURLOPT_PROXYUSERPWD:
  857. case CURLOPT_PROXY_CAINFO:
  858. case CURLOPT_PROXY_CAPATH:
  859. case CURLOPT_PROXY_CRLFILE:
  860. case CURLOPT_PROXY_KEYPASSWD:
  861. case CURLOPT_PROXY_PINNEDPUBLICKEY:
  862. case CURLOPT_PROXY_SERVICE_NAME:
  863. case CURLOPT_PROXY_SSLCERT:
  864. case CURLOPT_PROXY_SSLCERTTYPE:
  865. case CURLOPT_PROXY_SSLKEY:
  866. case CURLOPT_PROXY_SSLKEYTYPE:
  867. case CURLOPT_PROXY_SSL_CIPHER_LIST:
  868. case CURLOPT_PROXY_TLS13_CIPHERS:
  869. case CURLOPT_PROXY_TLSAUTH_PASSWORD:
  870. case CURLOPT_PROXY_TLSAUTH_TYPE:
  871. case CURLOPT_PROXY_TLSAUTH_USERNAME:
  872. case CURLOPT_RANDOM_FILE:
  873. case CURLOPT_RANGE:
  874. case CURLOPT_REDIR_PROTOCOLS_STR:
  875. case CURLOPT_REFERER:
  876. case CURLOPT_REQUEST_TARGET:
  877. case CURLOPT_RTSP_SESSION_ID:
  878. case CURLOPT_RTSP_STREAM_URI:
  879. case CURLOPT_RTSP_TRANSPORT:
  880. case CURLOPT_SASL_AUTHZID:
  881. case CURLOPT_SERVICE_NAME:
  882. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  883. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  884. case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
  885. case CURLOPT_SSH_KNOWNHOSTS:
  886. case CURLOPT_SSH_PRIVATE_KEYFILE:
  887. case CURLOPT_SSH_PUBLIC_KEYFILE:
  888. case CURLOPT_SSLCERT:
  889. case CURLOPT_SSLCERTTYPE:
  890. case CURLOPT_SSLENGINE:
  891. case CURLOPT_SSLKEY:
  892. case CURLOPT_SSLKEYTYPE:
  893. case CURLOPT_SSL_CIPHER_LIST:
  894. case CURLOPT_SSL_EC_CURVES:
  895. case CURLOPT_TLS13_CIPHERS:
  896. case CURLOPT_TLSAUTH_PASSWORD:
  897. case CURLOPT_TLSAUTH_TYPE:
  898. case CURLOPT_TLSAUTH_USERNAME:
  899. case CURLOPT_UNIX_SOCKET_PATH:
  900. case CURLOPT_URL:
  901. case CURLOPT_USERAGENT:
  902. case CURLOPT_USERNAME:
  903. case CURLOPT_USERPWD:
  904. case CURLOPT_XOAUTH2_BEARER:
  905. case CURLOPT_HAPROXY_CLIENT_IP:
  906. s = va_arg(arg, char *);
  907. ccsid = va_arg(arg, unsigned int);
  908. if(s) {
  909. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  910. if(!s) {
  911. result = CURLE_OUT_OF_MEMORY;
  912. break;
  913. }
  914. }
  915. result = curl_easy_setopt(easy, tag, s);
  916. free(s);
  917. break;
  918. case CURLOPT_COPYPOSTFIELDS:
  919. /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
  920. prior to this call. In this case, convert the given byte count and
  921. replace the length according to the conversion result. */
  922. s = va_arg(arg, char *);
  923. ccsid = va_arg(arg, unsigned int);
  924. pfsize = easy->set.postfieldsize;
  925. if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
  926. result = curl_easy_setopt(easy, CURLOPT_COPYPOSTFIELDS, s);
  927. break;
  928. }
  929. if(pfsize == -1) {
  930. /* Data is null-terminated. */
  931. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  932. if(!s) {
  933. result = CURLE_OUT_OF_MEMORY;
  934. break;
  935. }
  936. }
  937. else {
  938. /* Data length specified. */
  939. size_t len;
  940. if(pfsize < 0 || pfsize > SIZE_MAX) {
  941. result = CURLE_OUT_OF_MEMORY;
  942. break;
  943. }
  944. len = pfsize;
  945. pfsize = len * MAX_CONV_EXPANSION;
  946. if(pfsize > SIZE_MAX)
  947. pfsize = SIZE_MAX;
  948. cp = malloc(pfsize);
  949. if(!cp) {
  950. result = CURLE_OUT_OF_MEMORY;
  951. break;
  952. }
  953. pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
  954. if(pfsize < 0) {
  955. result = CURLE_OUT_OF_MEMORY;
  956. break;
  957. }
  958. easy->set.postfieldsize = pfsize; /* Replace data size. */
  959. s = cp;
  960. }
  961. result = curl_easy_setopt(easy, CURLOPT_POSTFIELDS, s);
  962. easy->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
  963. break;
  964. default:
  965. if(tag / 10000 == CURLOPTTYPE_BLOB) {
  966. struct curl_blob *bp = va_arg(arg, struct curl_blob *);
  967. struct curl_blob blob;
  968. ccsid = va_arg(arg, unsigned int);
  969. if(bp && bp->data && bp->len &&
  970. ccsid != NOCONV_CCSID && ccsid != ASCII_CCSID) {
  971. pfsize = (curl_off_t) bp->len * MAX_CONV_EXPANSION;
  972. if(pfsize > SIZE_MAX)
  973. pfsize = SIZE_MAX;
  974. cp = malloc(pfsize);
  975. if(!cp) {
  976. result = CURLE_OUT_OF_MEMORY;
  977. break;
  978. }
  979. pfsize = convert(cp, pfsize, ASCII_CCSID, bp->data, bp->len, ccsid);
  980. if(pfsize < 0) {
  981. result = CURLE_OUT_OF_MEMORY;
  982. break;
  983. }
  984. blob.data = cp;
  985. blob.len = pfsize;
  986. blob.flags = bp->flags | CURL_BLOB_COPY;
  987. bp = &blob;
  988. }
  989. result = curl_easy_setopt(easy, tag, &blob);
  990. break;
  991. }
  992. /* FALLTHROUGH */
  993. case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
  994. result = Curl_vsetopt(easy, tag, arg);
  995. break;
  996. }
  997. va_end(arg);
  998. free(cp);
  999. return result;
  1000. }
  1001. /* ILE/RPG helper functions. */
  1002. char *
  1003. curl_form_long_value(long value)
  1004. {
  1005. /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
  1006. return (char *) value;
  1007. }
  1008. CURLcode
  1009. curl_easy_setopt_RPGnum_(CURL *easy, CURLoption tag, curl_off_t arg)
  1010. {
  1011. /* ILE/RPG procedure overloading cannot discriminate between different
  1012. size and/or signedness of format arguments. This provides a generic
  1013. wrapper that adapts size to the given tag expectation.
  1014. This procedure is not intended to be explicitly called from user code. */
  1015. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1016. return curl_easy_setopt(easy, tag, (long) arg);
  1017. return curl_easy_setopt(easy, tag, arg);
  1018. }
  1019. CURLcode
  1020. curl_multi_setopt_RPGnum_(CURLM *multi, CURLMoption tag, curl_off_t arg)
  1021. {
  1022. /* Likewise, for multi handle. */
  1023. if(tag / 10000 != CURLOPTTYPE_OFF_T)
  1024. return curl_multi_setopt(multi, tag, (long) arg);
  1025. return curl_multi_setopt(multi, tag, arg);
  1026. }
  1027. char *
  1028. curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
  1029. size_t num, unsigned int ccsid)
  1030. {
  1031. char *d = (char *) NULL;
  1032. char *s = curl_pushheader_bynum(h, num);
  1033. if(s)
  1034. d = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1035. return d;
  1036. }
  1037. char *
  1038. curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
  1039. unsigned int ccsidin, unsigned int ccsidout)
  1040. {
  1041. char *d = (char *) NULL;
  1042. if(header) {
  1043. header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
  1044. if(header) {
  1045. char *s = curl_pushheader_byname(h, header);
  1046. free((char *) header);
  1047. if(s)
  1048. d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
  1049. }
  1050. }
  1051. return d;
  1052. }
  1053. static CURLcode
  1054. mime_string_call(curl_mimepart *part, const char *string, unsigned int ccsid,
  1055. CURLcode (*mimefunc)(curl_mimepart *part, const char *string))
  1056. {
  1057. char *s = (char *) NULL;
  1058. CURLcode result;
  1059. if(!string)
  1060. return mimefunc(part, string);
  1061. s = dynconvert(ASCII_CCSID, string, -1, ccsid);
  1062. if(!s)
  1063. return CURLE_OUT_OF_MEMORY;
  1064. result = mimefunc(part, s);
  1065. free(s);
  1066. return result;
  1067. }
  1068. CURLcode
  1069. curl_mime_name_ccsid(curl_mimepart *part, const char *name, unsigned int ccsid)
  1070. {
  1071. return mime_string_call(part, name, ccsid, curl_mime_name);
  1072. }
  1073. CURLcode
  1074. curl_mime_filename_ccsid(curl_mimepart *part,
  1075. const char *filename, unsigned int ccsid)
  1076. {
  1077. return mime_string_call(part, filename, ccsid, curl_mime_filename);
  1078. }
  1079. CURLcode
  1080. curl_mime_type_ccsid(curl_mimepart *part,
  1081. const char *mimetype, unsigned int ccsid)
  1082. {
  1083. return mime_string_call(part, mimetype, ccsid, curl_mime_type);
  1084. }
  1085. CURLcode
  1086. curl_mime_encoder_ccsid(curl_mimepart *part,
  1087. const char *encoding, unsigned int ccsid)
  1088. {
  1089. return mime_string_call(part, encoding, ccsid, curl_mime_encoder);
  1090. }
  1091. CURLcode
  1092. curl_mime_filedata_ccsid(curl_mimepart *part,
  1093. const char *filename, unsigned int ccsid)
  1094. {
  1095. return mime_string_call(part, filename, ccsid, curl_mime_filedata);
  1096. }
  1097. CURLcode
  1098. curl_mime_data_ccsid(curl_mimepart *part,
  1099. const char *data, size_t datasize, unsigned int ccsid)
  1100. {
  1101. char *s = (char *) NULL;
  1102. CURLcode result;
  1103. if(!data)
  1104. return curl_mime_data(part, data, datasize);
  1105. s = dynconvert(ASCII_CCSID, data, datasize, ccsid);
  1106. if(!s)
  1107. return CURLE_OUT_OF_MEMORY;
  1108. result = curl_mime_data(part, s, datasize);
  1109. free(s);
  1110. return result;
  1111. }
  1112. CURLUcode
  1113. curl_url_get_ccsid(CURLU *handle, CURLUPart what, char **part,
  1114. unsigned int flags, unsigned int ccsid)
  1115. {
  1116. char *s = (char *)NULL;
  1117. CURLUcode result;
  1118. if(!part)
  1119. return CURLUE_BAD_PARTPOINTER;
  1120. *part = (char *)NULL;
  1121. result = curl_url_get(handle, what, &s, flags);
  1122. if(result == CURLUE_OK) {
  1123. if(s) {
  1124. *part = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1125. if(!*part)
  1126. result = CURLUE_OUT_OF_MEMORY;
  1127. }
  1128. }
  1129. if(s)
  1130. free(s);
  1131. return result;
  1132. }
  1133. CURLUcode
  1134. curl_url_set_ccsid(CURLU *handle, CURLUPart what, const char *part,
  1135. unsigned int flags, unsigned int ccsid)
  1136. {
  1137. char *s = (char *)NULL;
  1138. CURLUcode result;
  1139. if(part) {
  1140. s = dynconvert(ASCII_CCSID, part, -1, ccsid);
  1141. if(!s)
  1142. return CURLUE_OUT_OF_MEMORY;
  1143. }
  1144. result = curl_url_set(handle, what, s, flags);
  1145. if(s)
  1146. free(s);
  1147. return result;
  1148. }
  1149. const struct curl_easyoption *
  1150. curl_easy_option_by_name_ccsid(const char *name, unsigned int ccsid)
  1151. {
  1152. const struct curl_easyoption *option = NULL;
  1153. if(name) {
  1154. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1155. if(s) {
  1156. option = curl_easy_option_by_name(s);
  1157. free(s);
  1158. }
  1159. }
  1160. return option;
  1161. }
  1162. /* Return option name in the given ccsid. */
  1163. const char *
  1164. curl_easy_option_get_name_ccsid(const struct curl_easyoption *option,
  1165. unsigned int ccsid)
  1166. {
  1167. char *name = NULL;
  1168. if(option && option->name)
  1169. name = dynconvert(ccsid, option->name, -1, ASCII_CCSID);
  1170. return (const char *) name;
  1171. }
  1172. /* Header API CCSID support. */
  1173. CURLHcode
  1174. curl_easy_header_ccsid(CURL *easy, const char *name, size_t index,
  1175. unsigned int origin, int request,
  1176. struct curl_header **hout, unsigned int ccsid)
  1177. {
  1178. CURLHcode result = CURLHE_BAD_ARGUMENT;
  1179. if(name) {
  1180. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1181. result = CURLHE_OUT_OF_MEMORY;
  1182. if(s) {
  1183. result = curl_easy_header(easy, s, index, origin, request, hout);
  1184. free(s);
  1185. }
  1186. }
  1187. return result;
  1188. }