ccsidcurl.c 35 KB

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