2
0

ccsidcurl.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * 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. char *curl_version_ccsid(unsigned int ccsid)
  186. {
  187. int i;
  188. char *aversion;
  189. char *eversion;
  190. aversion = curl_version();
  191. if(!aversion)
  192. return aversion;
  193. i = strlen(aversion) + 1;
  194. i *= MAX_CONV_EXPANSION;
  195. eversion = Curl_thread_buffer(LK_CURL_VERSION, i);
  196. if(!eversion)
  197. return (char *) NULL;
  198. if(convert(eversion, i, ccsid, aversion, -1, ASCII_CCSID) < 0)
  199. return (char *) NULL;
  200. return eversion;
  201. }
  202. char *
  203. curl_easy_escape_ccsid(CURL *handle, const char *string, int length,
  204. unsigned int sccsid, unsigned int dccsid)
  205. {
  206. char *s;
  207. char *d;
  208. if(!string) {
  209. errno = EINVAL;
  210. return (char *) NULL;
  211. }
  212. s = dynconvert(ASCII_CCSID, string, length? length: -1, sccsid);
  213. if(!s)
  214. return (char *) NULL;
  215. d = curl_easy_escape(handle, s, 0);
  216. free(s);
  217. if(!d)
  218. return (char *) NULL;
  219. s = dynconvert(dccsid, d, -1, ASCII_CCSID);
  220. free(d);
  221. return s;
  222. }
  223. char *
  224. curl_easy_unescape_ccsid(CURL *handle, const char *string, int length,
  225. int *outlength,
  226. unsigned int sccsid, unsigned int dccsid)
  227. {
  228. char *s;
  229. char *d;
  230. if(!string) {
  231. errno = EINVAL;
  232. return (char *) NULL;
  233. }
  234. s = dynconvert(ASCII_CCSID, string, length? length: -1, sccsid);
  235. if(!s)
  236. return (char *) NULL;
  237. d = curl_easy_unescape(handle, s, 0, outlength);
  238. free(s);
  239. if(!d)
  240. return (char *) NULL;
  241. s = dynconvert(dccsid, d, -1, ASCII_CCSID);
  242. free(d);
  243. if(s && outlength)
  244. *outlength = strlen(s);
  245. return s;
  246. }
  247. struct curl_slist *
  248. curl_slist_append_ccsid(struct curl_slist *list,
  249. const char *data, unsigned int ccsid)
  250. {
  251. char *s;
  252. s = (char *) NULL;
  253. if(!data)
  254. return curl_slist_append(list, data);
  255. s = dynconvert(ASCII_CCSID, data, -1, ccsid);
  256. if(!s)
  257. return (struct curl_slist *) NULL;
  258. list = curl_slist_append(list, s);
  259. free(s);
  260. return list;
  261. }
  262. time_t
  263. curl_getdate_ccsid(const char *p, const time_t *unused, unsigned int ccsid)
  264. {
  265. char *s;
  266. time_t t;
  267. if(!p)
  268. return curl_getdate(p, unused);
  269. s = dynconvert(ASCII_CCSID, p, -1, ccsid);
  270. if(!s)
  271. return (time_t) -1;
  272. t = curl_getdate(s, unused);
  273. free(s);
  274. return t;
  275. }
  276. static int
  277. convert_version_info_string(const char **stringp,
  278. char **bufp, int *left, unsigned int ccsid)
  279. {
  280. /* Helper for curl_version_info_ccsid(): convert a string if defined.
  281. Result is stored in the `*left'-byte buffer at `*bufp'.
  282. `*bufp' and `*left' are updated accordingly.
  283. Return 0 if ok, else -1. */
  284. if(*stringp) {
  285. int l = convert(*bufp, *left, ccsid, *stringp, -1, ASCII_CCSID);
  286. if(l <= 0)
  287. return -1;
  288. *stringp = *bufp;
  289. *bufp += l;
  290. *left -= l;
  291. }
  292. return 0;
  293. }
  294. curl_version_info_data *
  295. curl_version_info_ccsid(CURLversion stamp, unsigned int ccsid)
  296. {
  297. curl_version_info_data *p;
  298. char *cp;
  299. int n;
  300. int nproto;
  301. curl_version_info_data *id;
  302. int i;
  303. const char **cpp;
  304. static const size_t charfields[] = {
  305. offsetof(curl_version_info_data, version),
  306. offsetof(curl_version_info_data, host),
  307. offsetof(curl_version_info_data, ssl_version),
  308. offsetof(curl_version_info_data, libz_version),
  309. offsetof(curl_version_info_data, ares),
  310. offsetof(curl_version_info_data, libidn),
  311. offsetof(curl_version_info_data, libssh_version),
  312. offsetof(curl_version_info_data, brotli_version),
  313. offsetof(curl_version_info_data, nghttp2_version),
  314. offsetof(curl_version_info_data, quic_version),
  315. offsetof(curl_version_info_data, cainfo),
  316. offsetof(curl_version_info_data, capath),
  317. offsetof(curl_version_info_data, zstd_version),
  318. offsetof(curl_version_info_data, hyper_version),
  319. offsetof(curl_version_info_data, gsasl_version)
  320. };
  321. /* The assertion below is possible, because although the second operand
  322. is an enum member, the first is a #define. In that case, the OS/400 C
  323. compiler seems to compare string values after substitution. */
  324. #if CURLVERSION_NOW != CURLVERSION_TENTH
  325. #error curl_version_info_data structure has changed: upgrade this procedure.
  326. #endif
  327. /* If caller has been compiled with a new version, error. */
  328. if(stamp > CURLVERSION_NOW)
  329. return (curl_version_info_data *) NULL;
  330. p = curl_version_info(stamp);
  331. if(!p)
  332. return p;
  333. /* Measure thread space needed. */
  334. n = 0;
  335. nproto = 0;
  336. if(p->protocols) {
  337. while(p->protocols[nproto])
  338. n += strlen(p->protocols[nproto++]);
  339. n += nproto++;
  340. }
  341. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  342. cpp = (const char **) ((char *) p + charfields[i]);
  343. if(*cpp)
  344. n += strlen(*cpp) + 1;
  345. }
  346. /* Allocate thread space. */
  347. n *= MAX_CONV_EXPANSION;
  348. if(nproto)
  349. n += nproto * sizeof(const char *);
  350. cp = Curl_thread_buffer(LK_VERSION_INFO_DATA, n);
  351. id = (curl_version_info_data *) Curl_thread_buffer(LK_VERSION_INFO,
  352. sizeof(*id));
  353. if(!id || !cp)
  354. return (curl_version_info_data *) NULL;
  355. /* Copy data and convert strings. */
  356. memcpy((char *) id, (char *) p, sizeof(*p));
  357. if(id->protocols) {
  358. int i = nproto * sizeof(id->protocols[0]);
  359. id->protocols = (const char * const *) cp;
  360. memcpy(cp, (char *) p->protocols, i);
  361. cp += i;
  362. n -= i;
  363. for(i = 0; id->protocols[i]; i++)
  364. if(convert_version_info_string(((const char * *) id->protocols) + i,
  365. &cp, &n, ccsid))
  366. return (curl_version_info_data *) NULL;
  367. }
  368. for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
  369. cpp = (const char **) ((char *) p + charfields[i]);
  370. if (*cpp && convert_version_info_string(cpp, &cp, &n, ccsid))
  371. return (curl_version_info_data *) NULL;
  372. }
  373. return id;
  374. }
  375. const char *
  376. curl_easy_strerror_ccsid(CURLcode error, unsigned int ccsid)
  377. {
  378. int i;
  379. const char *s;
  380. char *buf;
  381. s = curl_easy_strerror(error);
  382. if(!s)
  383. return s;
  384. i = MAX_CONV_EXPANSION * (strlen(s) + 1);
  385. buf = Curl_thread_buffer(LK_EASY_STRERROR, i);
  386. if(!buf)
  387. return (const char *) NULL;
  388. if(convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
  389. return (const char *) NULL;
  390. return (const char *) buf;
  391. }
  392. const char *
  393. curl_share_strerror_ccsid(CURLSHcode error, unsigned int ccsid)
  394. {
  395. int i;
  396. const char *s;
  397. char *buf;
  398. s = curl_share_strerror(error);
  399. if(!s)
  400. return s;
  401. i = MAX_CONV_EXPANSION * (strlen(s) + 1);
  402. buf = Curl_thread_buffer(LK_SHARE_STRERROR, i);
  403. if(!buf)
  404. return (const char *) NULL;
  405. if(convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
  406. return (const char *) NULL;
  407. return (const char *) buf;
  408. }
  409. const char *
  410. curl_multi_strerror_ccsid(CURLMcode error, unsigned int ccsid)
  411. {
  412. int i;
  413. const char *s;
  414. char *buf;
  415. s = curl_multi_strerror(error);
  416. if(!s)
  417. return s;
  418. i = MAX_CONV_EXPANSION * (strlen(s) + 1);
  419. buf = Curl_thread_buffer(LK_MULTI_STRERROR, i);
  420. if(!buf)
  421. return (const char *) NULL;
  422. if(convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
  423. return (const char *) NULL;
  424. return (const char *) buf;
  425. }
  426. void
  427. curl_certinfo_free_all(struct curl_certinfo *info)
  428. {
  429. /* Free all memory used by certificate info. */
  430. if(info) {
  431. if(info->certinfo) {
  432. int i;
  433. for(i = 0; i < info->num_of_certs; i++)
  434. curl_slist_free_all(info->certinfo[i]);
  435. free((char *) info->certinfo);
  436. }
  437. free((char *) info);
  438. }
  439. }
  440. CURLcode
  441. curl_easy_getinfo_ccsid(CURL *curl, CURLINFO info, ...)
  442. {
  443. va_list arg;
  444. void *paramp;
  445. CURLcode ret;
  446. struct Curl_easy *data;
  447. /* WARNING: unlike curl_easy_getinfo(), the strings returned by this
  448. procedure have to be free'ed. */
  449. data = (struct Curl_easy *) curl;
  450. va_start(arg, info);
  451. paramp = va_arg(arg, void *);
  452. ret = Curl_getinfo(data, info, paramp);
  453. if(ret == CURLE_OK) {
  454. unsigned int ccsid;
  455. char **cpp;
  456. struct curl_slist **slp;
  457. struct curl_certinfo *cipf;
  458. struct curl_certinfo *cipt;
  459. switch((int) info & CURLINFO_TYPEMASK) {
  460. case CURLINFO_STRING:
  461. ccsid = va_arg(arg, unsigned int);
  462. cpp = (char * *) paramp;
  463. if(*cpp) {
  464. *cpp = dynconvert(ccsid, *cpp, -1, ASCII_CCSID);
  465. if(!*cpp)
  466. ret = CURLE_OUT_OF_MEMORY;
  467. }
  468. break;
  469. case CURLINFO_SLIST:
  470. ccsid = va_arg(arg, unsigned int);
  471. switch(info) {
  472. case CURLINFO_CERTINFO:
  473. cipf = *(struct curl_certinfo * *) paramp;
  474. if(cipf) {
  475. cipt = (struct curl_certinfo *) malloc(sizeof(*cipt));
  476. if(!cipt)
  477. ret = CURLE_OUT_OF_MEMORY;
  478. else {
  479. cipt->certinfo = (struct curl_slist **)
  480. calloc(cipf->num_of_certs +
  481. 1, sizeof(struct curl_slist *));
  482. if(!cipt->certinfo)
  483. ret = CURLE_OUT_OF_MEMORY;
  484. else {
  485. int i;
  486. cipt->num_of_certs = cipf->num_of_certs;
  487. for(i = 0; i < cipf->num_of_certs; i++)
  488. if(cipf->certinfo[i])
  489. if(!(cipt->certinfo[i] = slist_convert(ccsid,
  490. cipf->certinfo[i],
  491. ASCII_CCSID))) {
  492. ret = CURLE_OUT_OF_MEMORY;
  493. break;
  494. }
  495. }
  496. }
  497. if(ret != CURLE_OK) {
  498. curl_certinfo_free_all(cipt);
  499. cipt = (struct curl_certinfo *) NULL;
  500. }
  501. *(struct curl_certinfo * *) paramp = cipt;
  502. }
  503. break;
  504. case CURLINFO_TLS_SESSION:
  505. case CURLINFO_TLS_SSL_PTR:
  506. case CURLINFO_SOCKET:
  507. break;
  508. default:
  509. slp = (struct curl_slist **) paramp;
  510. if(*slp) {
  511. *slp = slist_convert(ccsid, *slp, ASCII_CCSID);
  512. if(!*slp)
  513. ret = CURLE_OUT_OF_MEMORY;
  514. }
  515. break;
  516. }
  517. }
  518. }
  519. va_end(arg);
  520. return ret;
  521. }
  522. static int
  523. Curl_is_formadd_string(CURLformoption option)
  524. {
  525. switch(option) {
  526. case CURLFORM_FILENAME:
  527. case CURLFORM_CONTENTTYPE:
  528. case CURLFORM_BUFFER:
  529. case CURLFORM_FILE:
  530. case CURLFORM_FILECONTENT:
  531. case CURLFORM_COPYCONTENTS:
  532. case CURLFORM_COPYNAME:
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. static void
  538. Curl_formadd_release_local(struct curl_forms *forms, int nargs, int skip)
  539. {
  540. while(nargs--)
  541. if(nargs != skip)
  542. if(Curl_is_formadd_string(forms[nargs].option))
  543. if(forms[nargs].value)
  544. free((char *) forms[nargs].value);
  545. free((char *) forms);
  546. }
  547. static int
  548. Curl_formadd_convert(struct curl_forms *forms,
  549. int formx, int lengthx, unsigned int ccsid)
  550. {
  551. int l;
  552. char *cp;
  553. char *cp2;
  554. if(formx < 0 || !forms[formx].value)
  555. return 0;
  556. if(lengthx >= 0)
  557. l = (int) forms[lengthx].value;
  558. else
  559. l = strlen(forms[formx].value) + 1;
  560. cp = malloc(MAX_CONV_EXPANSION * l);
  561. if(!cp)
  562. return -1;
  563. l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
  564. forms[formx].value, l, ccsid);
  565. if(l < 0) {
  566. free(cp);
  567. return -1;
  568. }
  569. cp2 = realloc(cp, l); /* Shorten buffer to the string size. */
  570. if(cp2)
  571. cp = cp2;
  572. forms[formx].value = cp;
  573. if(lengthx >= 0)
  574. forms[lengthx].value = (char *) l; /* Update length after conversion. */
  575. return l;
  576. }
  577. CURLFORMcode
  578. curl_formadd_ccsid(struct curl_httppost **httppost,
  579. struct curl_httppost **last_post, ...)
  580. {
  581. va_list arg;
  582. CURLformoption option;
  583. CURLFORMcode result;
  584. struct curl_forms *forms;
  585. struct curl_forms *lforms;
  586. struct curl_forms *tforms;
  587. unsigned int lformlen;
  588. const char *value;
  589. unsigned int ccsid;
  590. int nargs;
  591. int namex;
  592. int namelengthx;
  593. int contentx;
  594. int lengthx;
  595. unsigned int contentccsid;
  596. unsigned int nameccsid;
  597. /* A single curl_formadd() call cannot be split in several calls to deal
  598. with all parameters: the original parameters are thus copied to a local
  599. curl_forms array and converted to ASCII when needed.
  600. CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
  601. CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
  602. parameters is not defined; for this reason, the actual conversion is
  603. delayed to the end of parameter processing. The same applies to
  604. CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
  605. several times in the parameter list; the problem resides here in knowing
  606. which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
  607. when we can be sure to have both info for conversion: end of parameter
  608. list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
  609. natural separator between content data definitions; this seems to be
  610. in accordance with FormAdd() behavior. */
  611. /* Allocate the local curl_forms array. */
  612. lformlen = ALLOC_GRANULE;
  613. lforms = malloc(lformlen * sizeof(*lforms));
  614. if(!lforms)
  615. return CURL_FORMADD_MEMORY;
  616. /* Process the arguments, copying them into local array, latching conversion
  617. indexes and converting when needed. */
  618. result = CURL_FORMADD_OK;
  619. nargs = 0;
  620. contentx = -1;
  621. lengthx = -1;
  622. namex = -1;
  623. namelengthx = -1;
  624. forms = (struct curl_forms *) NULL;
  625. va_start(arg, last_post);
  626. for(;;) {
  627. /* Make sure there is still room for an item in local array. */
  628. if(nargs >= lformlen) {
  629. lformlen += ALLOC_GRANULE;
  630. tforms = realloc(lforms, lformlen * sizeof(*lforms));
  631. if(!tforms) {
  632. result = CURL_FORMADD_MEMORY;
  633. break;
  634. }
  635. lforms = tforms;
  636. }
  637. /* Get next option. */
  638. if(forms) {
  639. /* Get option from array. */
  640. option = forms->option;
  641. value = forms->value;
  642. forms++;
  643. }
  644. else {
  645. /* Get option from arguments. */
  646. option = va_arg(arg, CURLformoption);
  647. if(option == CURLFORM_END)
  648. break;
  649. }
  650. /* Dispatch by option. */
  651. switch(option) {
  652. case CURLFORM_END:
  653. forms = (struct curl_forms *) NULL; /* Leave array mode. */
  654. continue;
  655. case CURLFORM_ARRAY:
  656. if(!forms) {
  657. forms = va_arg(arg, struct curl_forms *);
  658. continue;
  659. }
  660. result = CURL_FORMADD_ILLEGAL_ARRAY;
  661. break;
  662. case CURLFORM_COPYNAME:
  663. option = CURLFORM_PTRNAME; /* Static for now. */
  664. case CURLFORM_PTRNAME:
  665. if(namex >= 0)
  666. result = CURL_FORMADD_OPTION_TWICE;
  667. namex = nargs;
  668. if(!forms) {
  669. value = va_arg(arg, char *);
  670. nameccsid = (unsigned int) va_arg(arg, long);
  671. }
  672. else {
  673. nameccsid = (unsigned int) forms->value;
  674. forms++;
  675. }
  676. break;
  677. case CURLFORM_COPYCONTENTS:
  678. if(contentx >= 0)
  679. result = CURL_FORMADD_OPTION_TWICE;
  680. contentx = nargs;
  681. if(!forms) {
  682. value = va_arg(arg, char *);
  683. contentccsid = (unsigned int) va_arg(arg, long);
  684. }
  685. else {
  686. contentccsid = (unsigned int) forms->value;
  687. forms++;
  688. }
  689. break;
  690. case CURLFORM_PTRCONTENTS:
  691. case CURLFORM_BUFFERPTR:
  692. if(!forms)
  693. value = va_arg(arg, char *); /* No conversion. */
  694. break;
  695. case CURLFORM_CONTENTSLENGTH:
  696. lengthx = nargs;
  697. if(!forms)
  698. value = (char *) va_arg(arg, long);
  699. break;
  700. case CURLFORM_CONTENTLEN:
  701. lengthx = nargs;
  702. if(!forms)
  703. value = (char *) va_arg(arg, curl_off_t);
  704. break;
  705. case CURLFORM_NAMELENGTH:
  706. namelengthx = nargs;
  707. if(!forms)
  708. value = (char *) va_arg(arg, long);
  709. break;
  710. case CURLFORM_BUFFERLENGTH:
  711. if(!forms)
  712. value = (char *) va_arg(arg, long);
  713. break;
  714. case CURLFORM_CONTENTHEADER:
  715. if(!forms)
  716. value = (char *) va_arg(arg, struct curl_slist *);
  717. break;
  718. case CURLFORM_STREAM:
  719. if(!forms)
  720. value = (char *) va_arg(arg, void *);
  721. break;
  722. case CURLFORM_CONTENTTYPE:
  723. /* If a previous content has been encountered, convert it now. */
  724. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
  725. result = CURL_FORMADD_MEMORY;
  726. break;
  727. }
  728. contentx = -1;
  729. lengthx = -1;
  730. /* Fall into default. */
  731. default:
  732. /* Must be a convertible string. */
  733. if(!Curl_is_formadd_string(option)) {
  734. result = CURL_FORMADD_UNKNOWN_OPTION;
  735. break;
  736. }
  737. if(!forms) {
  738. value = va_arg(arg, char *);
  739. ccsid = (unsigned int) va_arg(arg, long);
  740. }
  741. else {
  742. ccsid = (unsigned int) forms->value;
  743. forms++;
  744. }
  745. /* Do the conversion. */
  746. lforms[nargs].value = value;
  747. if(Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
  748. result = CURL_FORMADD_MEMORY;
  749. break;
  750. }
  751. value = lforms[nargs].value;
  752. }
  753. if(result != CURL_FORMADD_OK)
  754. break;
  755. lforms[nargs].value = value;
  756. lforms[nargs++].option = option;
  757. }
  758. va_end(arg);
  759. /* Convert the name and the last content, now that we know their lengths. */
  760. if(result == CURL_FORMADD_OK && namex >= 0) {
  761. if(Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
  762. result = CURL_FORMADD_MEMORY;
  763. else
  764. lforms[namex].option = CURLFORM_COPYNAME; /* Force copy. */
  765. }
  766. if(result == CURL_FORMADD_OK) {
  767. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
  768. result = CURL_FORMADD_MEMORY;
  769. else
  770. contentx = -1;
  771. }
  772. /* Do the formadd with our converted parameters. */
  773. if(result == CURL_FORMADD_OK) {
  774. lforms[nargs].option = CURLFORM_END;
  775. result = curl_formadd(httppost, last_post,
  776. CURLFORM_ARRAY, lforms, CURLFORM_END);
  777. }
  778. /* Terminate. */
  779. Curl_formadd_release_local(lforms, nargs, contentx);
  780. return result;
  781. }
  782. struct cfcdata {
  783. curl_formget_callback append;
  784. void * arg;
  785. unsigned int ccsid;
  786. };
  787. static size_t
  788. Curl_formget_callback_ccsid(void *arg, const char *buf, size_t len)
  789. {
  790. struct cfcdata *p;
  791. char *b;
  792. int l;
  793. size_t ret;
  794. p = (struct cfcdata *) arg;
  795. if((long) len <= 0)
  796. return (*p->append)(p->arg, buf, len);
  797. b = malloc(MAX_CONV_EXPANSION * len);
  798. if(!b)
  799. return (size_t) -1;
  800. l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
  801. if(l < 0) {
  802. free(b);
  803. return (size_t) -1;
  804. }
  805. ret = (*p->append)(p->arg, b, l);
  806. free(b);
  807. return ret == l? len: -1;
  808. }
  809. int
  810. curl_formget_ccsid(struct curl_httppost *form, void *arg,
  811. curl_formget_callback append, unsigned int ccsid)
  812. {
  813. struct cfcdata lcfc;
  814. lcfc.append = append;
  815. lcfc.arg = arg;
  816. lcfc.ccsid = ccsid;
  817. return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
  818. }
  819. CURLcode
  820. curl_easy_setopt_ccsid(CURL *curl, CURLoption tag, ...)
  821. {
  822. CURLcode result;
  823. va_list arg;
  824. struct Curl_easy *data;
  825. char *s;
  826. char *cp;
  827. unsigned int ccsid;
  828. curl_off_t pfsize;
  829. data = (struct Curl_easy *) curl;
  830. va_start(arg, tag);
  831. switch(tag) {
  832. case CURLOPT_ABSTRACT_UNIX_SOCKET:
  833. case CURLOPT_ALTSVC:
  834. case CURLOPT_AWS_SIGV4:
  835. case CURLOPT_CAINFO:
  836. case CURLOPT_CAPATH:
  837. case CURLOPT_COOKIE:
  838. case CURLOPT_COOKIEFILE:
  839. case CURLOPT_COOKIEJAR:
  840. case CURLOPT_COOKIELIST:
  841. case CURLOPT_CRLFILE:
  842. case CURLOPT_CUSTOMREQUEST:
  843. case CURLOPT_DEFAULT_PROTOCOL:
  844. case CURLOPT_DNS_SERVERS:
  845. case CURLOPT_DNS_INTERFACE:
  846. case CURLOPT_DNS_LOCAL_IP4:
  847. case CURLOPT_DNS_LOCAL_IP6:
  848. case CURLOPT_DOH_URL:
  849. case CURLOPT_EGDSOCKET:
  850. case CURLOPT_ENCODING:
  851. case CURLOPT_FTPPORT:
  852. case CURLOPT_FTP_ACCOUNT:
  853. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  854. case CURLOPT_HSTS:
  855. case CURLOPT_INTERFACE:
  856. case CURLOPT_ISSUERCERT:
  857. case CURLOPT_KEYPASSWD:
  858. case CURLOPT_KRBLEVEL:
  859. case CURLOPT_LOGIN_OPTIONS:
  860. case CURLOPT_MAIL_AUTH:
  861. case CURLOPT_MAIL_FROM:
  862. case CURLOPT_NETRC_FILE:
  863. case CURLOPT_NOPROXY:
  864. case CURLOPT_PASSWORD:
  865. case CURLOPT_PINNEDPUBLICKEY:
  866. case CURLOPT_PRE_PROXY:
  867. case CURLOPT_PROXY:
  868. case CURLOPT_PROXYPASSWORD:
  869. case CURLOPT_PROXYUSERNAME:
  870. case CURLOPT_PROXYUSERPWD:
  871. case CURLOPT_PROXY_CAINFO:
  872. case CURLOPT_PROXY_CAPATH:
  873. case CURLOPT_PROXY_CRLFILE:
  874. case CURLOPT_PROXY_KEYPASSWD:
  875. case CURLOPT_PROXY_PINNEDPUBLICKEY:
  876. case CURLOPT_PROXY_SERVICE_NAME:
  877. case CURLOPT_PROXY_SSLCERT:
  878. case CURLOPT_PROXY_SSLCERTTYPE:
  879. case CURLOPT_PROXY_SSLKEY:
  880. case CURLOPT_PROXY_SSLKEYTYPE:
  881. case CURLOPT_PROXY_SSL_CIPHER_LIST:
  882. case CURLOPT_PROXY_TLS13_CIPHERS:
  883. case CURLOPT_PROXY_TLSAUTH_PASSWORD:
  884. case CURLOPT_PROXY_TLSAUTH_TYPE:
  885. case CURLOPT_PROXY_TLSAUTH_USERNAME:
  886. case CURLOPT_RANDOM_FILE:
  887. case CURLOPT_RANGE:
  888. case CURLOPT_REFERER:
  889. case CURLOPT_REQUEST_TARGET:
  890. case CURLOPT_RTSP_SESSION_ID:
  891. case CURLOPT_RTSP_STREAM_URI:
  892. case CURLOPT_RTSP_TRANSPORT:
  893. case CURLOPT_SASL_AUTHZID:
  894. case CURLOPT_SERVICE_NAME:
  895. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  896. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  897. case CURLOPT_SSH_KNOWNHOSTS:
  898. case CURLOPT_SSH_PRIVATE_KEYFILE:
  899. case CURLOPT_SSH_PUBLIC_KEYFILE:
  900. case CURLOPT_SSLCERT:
  901. case CURLOPT_SSLCERTTYPE:
  902. case CURLOPT_SSLENGINE:
  903. case CURLOPT_SSLKEY:
  904. case CURLOPT_SSLKEYTYPE:
  905. case CURLOPT_SSL_CIPHER_LIST:
  906. case CURLOPT_SSL_EC_CURVES:
  907. case CURLOPT_TLS13_CIPHERS:
  908. case CURLOPT_TLSAUTH_PASSWORD:
  909. case CURLOPT_TLSAUTH_TYPE:
  910. case CURLOPT_TLSAUTH_USERNAME:
  911. case CURLOPT_UNIX_SOCKET_PATH:
  912. case CURLOPT_URL:
  913. case CURLOPT_USERAGENT:
  914. case CURLOPT_USERNAME:
  915. case CURLOPT_USERPWD:
  916. case CURLOPT_XOAUTH2_BEARER:
  917. s = va_arg(arg, char *);
  918. ccsid = va_arg(arg, unsigned int);
  919. if(s) {
  920. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  921. if(!s) {
  922. result = CURLE_OUT_OF_MEMORY;
  923. break;
  924. }
  925. }
  926. result = curl_easy_setopt(curl, tag, s);
  927. free(s);
  928. break;
  929. case CURLOPT_COPYPOSTFIELDS:
  930. /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
  931. prior to this call. In this case, convert the given byte count and
  932. replace the length according to the conversion result. */
  933. s = va_arg(arg, char *);
  934. ccsid = va_arg(arg, unsigned int);
  935. pfsize = data->set.postfieldsize;
  936. if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
  937. result = curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, s);
  938. break;
  939. }
  940. if(pfsize == -1) {
  941. /* Data is null-terminated. */
  942. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  943. if(!s) {
  944. result = CURLE_OUT_OF_MEMORY;
  945. break;
  946. }
  947. }
  948. else {
  949. /* Data length specified. */
  950. size_t len;
  951. if(pfsize < 0 || pfsize > SIZE_MAX) {
  952. result = CURLE_OUT_OF_MEMORY;
  953. break;
  954. }
  955. len = pfsize;
  956. pfsize = len * MAX_CONV_EXPANSION;
  957. if(pfsize > SIZE_MAX)
  958. pfsize = SIZE_MAX;
  959. cp = malloc(pfsize);
  960. if(!cp) {
  961. result = CURLE_OUT_OF_MEMORY;
  962. break;
  963. }
  964. pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
  965. if(pfsize < 0) {
  966. free(cp);
  967. result = CURLE_OUT_OF_MEMORY;
  968. break;
  969. }
  970. data->set.postfieldsize = pfsize; /* Replace data size. */
  971. s = cp;
  972. }
  973. result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s);
  974. data->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
  975. break;
  976. case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
  977. default:
  978. result = Curl_vsetopt(curl, tag, arg);
  979. break;
  980. }
  981. va_end(arg);
  982. return result;
  983. }
  984. char *
  985. curl_form_long_value(long value)
  986. {
  987. /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
  988. return (char *) value;
  989. }
  990. char *
  991. curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
  992. size_t num, unsigned int ccsid)
  993. {
  994. char *d = (char *) NULL;
  995. char *s = curl_pushheader_bynum(h, num);
  996. if(s)
  997. d = dynconvert(ccsid, s, -1, ASCII_CCSID);
  998. return d;
  999. }
  1000. char *
  1001. curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
  1002. unsigned int ccsidin, unsigned int ccsidout)
  1003. {
  1004. char *d = (char *) NULL;
  1005. if(header) {
  1006. header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
  1007. if(header) {
  1008. char *s = curl_pushheader_byname(h, header);
  1009. free((char *) header);
  1010. if(s)
  1011. d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
  1012. }
  1013. }
  1014. return d;
  1015. }
  1016. static CURLcode
  1017. mime_string_call(curl_mimepart *part, const char *string, unsigned int ccsid,
  1018. CURLcode (*mimefunc)(curl_mimepart *part, const char *string))
  1019. {
  1020. char *s = (char *) NULL;
  1021. CURLcode result;
  1022. if(!string)
  1023. return mimefunc(part, string);
  1024. s = dynconvert(ASCII_CCSID, string, -1, ccsid);
  1025. if(!s)
  1026. return CURLE_OUT_OF_MEMORY;
  1027. result = mimefunc(part, s);
  1028. free(s);
  1029. return result;
  1030. }
  1031. CURLcode
  1032. curl_mime_name_ccsid(curl_mimepart *part, const char *name, unsigned int ccsid)
  1033. {
  1034. return mime_string_call(part, name, ccsid, curl_mime_name);
  1035. }
  1036. CURLcode
  1037. curl_mime_filename_ccsid(curl_mimepart *part,
  1038. const char *filename, unsigned int ccsid)
  1039. {
  1040. return mime_string_call(part, filename, ccsid, curl_mime_filename);
  1041. }
  1042. CURLcode
  1043. curl_mime_type_ccsid(curl_mimepart *part,
  1044. const char *mimetype, unsigned int ccsid)
  1045. {
  1046. return mime_string_call(part, mimetype, ccsid, curl_mime_type);
  1047. }
  1048. CURLcode
  1049. curl_mime_encoder_ccsid(curl_mimepart *part,
  1050. const char *encoding, unsigned int ccsid)
  1051. {
  1052. return mime_string_call(part, encoding, ccsid, curl_mime_encoder);
  1053. }
  1054. CURLcode
  1055. curl_mime_filedata_ccsid(curl_mimepart *part,
  1056. const char *filename, unsigned int ccsid)
  1057. {
  1058. return mime_string_call(part, filename, ccsid, curl_mime_filedata);
  1059. }
  1060. CURLcode
  1061. curl_mime_data_ccsid(curl_mimepart *part,
  1062. const char *data, size_t datasize, unsigned int ccsid)
  1063. {
  1064. char *s = (char *) NULL;
  1065. CURLcode result;
  1066. if(!data)
  1067. return curl_mime_data(part, data, datasize);
  1068. s = dynconvert(ASCII_CCSID, data, datasize, ccsid);
  1069. if(!s)
  1070. return CURLE_OUT_OF_MEMORY;
  1071. result = curl_mime_data(part, s, datasize);
  1072. free(s);
  1073. return result;
  1074. }
  1075. CURLUcode
  1076. curl_url_get_ccsid(CURLU *handle, CURLUPart what, char **part,
  1077. unsigned int flags, unsigned int ccsid)
  1078. {
  1079. char *s = (char *)NULL;
  1080. CURLUcode result;
  1081. if(!part)
  1082. return CURLUE_BAD_PARTPOINTER;
  1083. *part = (char *)NULL;
  1084. result = curl_url_get(handle, what, &s, flags);
  1085. if(result == CURLUE_OK) {
  1086. if(s) {
  1087. *part = dynconvert(ccsid, s, -1, ASCII_CCSID);
  1088. if(!*part)
  1089. result = CURLUE_OUT_OF_MEMORY;
  1090. }
  1091. }
  1092. if(s)
  1093. free(s);
  1094. return result;
  1095. }
  1096. CURLUcode
  1097. curl_url_set_ccsid(CURLU *handle, CURLUPart what, const char *part,
  1098. unsigned int flags, unsigned int ccsid)
  1099. {
  1100. char *s = (char *)NULL;
  1101. CURLUcode result;
  1102. if(part) {
  1103. s = dynconvert(ASCII_CCSID, part, -1, ccsid);
  1104. if(!s)
  1105. return CURLUE_OUT_OF_MEMORY;
  1106. }
  1107. result = curl_url_set(handle, what, s, flags);
  1108. if(s)
  1109. free(s);
  1110. return result;
  1111. }
  1112. const struct curl_easyoption *
  1113. curl_easy_option_by_name_ccsid(const char *name, unsigned int ccsid)
  1114. {
  1115. const struct curl_easyoption *option = NULL;
  1116. if(name) {
  1117. char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
  1118. if(s) {
  1119. option = curl_easy_option_by_name(s);
  1120. free(s);
  1121. }
  1122. }
  1123. return option;
  1124. }
  1125. /* Return option name in the given ccsid. */
  1126. const char *
  1127. curl_easy_option_get_name_ccsid(const struct curl_easyoption *option,
  1128. unsigned int ccsid)
  1129. {
  1130. char *name = NULL;
  1131. if(option && option->name)
  1132. name = dynconvert(ccsid, option->name, -1, ASCII_CCSID);
  1133. return (const char *) name;
  1134. }