ccsidcurl.c 29 KB

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