ccsidcurl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 SessionHandle * data;
  435. struct curl_slist * * slp;
  436. struct curl_certinfo * cipf;
  437. struct curl_certinfo * cipt;
  438. /* WARNING: unlike curl_easy_get_info(), the strings returned by this
  439. procedure have to be free'ed. */
  440. data = (struct SessionHandle *) 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_SOCKET:
  491. break;
  492. default:
  493. slp = (struct curl_slist * *) paramp;
  494. if(*slp)
  495. if(!(*slp = slist_convert(ccsid, *slp, ASCII_CCSID)))
  496. ret = CURLE_OUT_OF_MEMORY;
  497. break;
  498. }
  499. }
  500. va_end(arg);
  501. return ret;
  502. }
  503. static int
  504. Curl_is_formadd_string(CURLformoption option)
  505. {
  506. switch (option) {
  507. case CURLFORM_FILENAME:
  508. case CURLFORM_CONTENTTYPE:
  509. case CURLFORM_BUFFER:
  510. case CURLFORM_FILE:
  511. case CURLFORM_FILECONTENT:
  512. case CURLFORM_COPYCONTENTS:
  513. case CURLFORM_COPYNAME:
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. static void
  519. Curl_formadd_release_local(struct curl_forms * forms, int nargs, int skip)
  520. {
  521. while(nargs--)
  522. if(nargs != skip)
  523. if(Curl_is_formadd_string(forms[nargs].option))
  524. if(forms[nargs].value)
  525. free((char *) forms[nargs].value);
  526. free((char *) forms);
  527. }
  528. static int
  529. Curl_formadd_convert(struct curl_forms * forms,
  530. int formx, int lengthx, unsigned int ccsid)
  531. {
  532. int l;
  533. char * cp;
  534. char * cp2;
  535. if(formx < 0 || !forms[formx].value)
  536. return 0;
  537. if(lengthx >= 0)
  538. l = (int) forms[lengthx].value;
  539. else
  540. l = strlen(forms[formx].value) + 1;
  541. cp = malloc(MAX_CONV_EXPANSION * l);
  542. if(!cp)
  543. return -1;
  544. l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
  545. forms[formx].value, l, ccsid);
  546. if(l < 0) {
  547. free(cp);
  548. return -1;
  549. }
  550. cp2 = realloc(cp, l); /* Shorten buffer to the string size. */
  551. if(cp2)
  552. cp = cp2;
  553. forms[formx].value = cp;
  554. if(lengthx >= 0)
  555. forms[lengthx].value = (char *) l; /* Update length after conversion. */
  556. return l;
  557. }
  558. CURLFORMcode
  559. curl_formadd_ccsid(struct curl_httppost * * httppost,
  560. struct curl_httppost * * last_post, ...)
  561. {
  562. va_list arg;
  563. CURLformoption option;
  564. CURLFORMcode result;
  565. struct curl_forms * forms;
  566. struct curl_forms * lforms;
  567. struct curl_forms * tforms;
  568. unsigned int lformlen;
  569. const char * value;
  570. unsigned int ccsid;
  571. int nargs;
  572. int namex;
  573. int namelengthx;
  574. int contentx;
  575. int lengthx;
  576. unsigned int contentccsid;
  577. unsigned int nameccsid;
  578. /* A single curl_formadd() call cannot be splitted in several calls to deal
  579. with all parameters: the original parameters are thus copied to a local
  580. curl_forms array and converted to ASCII when needed.
  581. CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
  582. CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
  583. parameters is not defined; for this reason, the actual conversion is
  584. delayed to the end of parameter processing. The same applies to
  585. CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
  586. several times in the parameter list; the problem resides here in knowing
  587. which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
  588. when we can be sure to have both info for conversion: end of parameter
  589. list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
  590. natural separator between content data definitions; this seems to be
  591. in accordance with FormAdd() behavior. */
  592. /* Allocate the local curl_forms array. */
  593. lformlen = ALLOC_GRANULE;
  594. lforms = malloc(lformlen * sizeof *lforms);
  595. if(!lforms)
  596. return CURL_FORMADD_MEMORY;
  597. /* Process the arguments, copying them into local array, latching conversion
  598. indexes and converting when needed. */
  599. result = CURL_FORMADD_OK;
  600. nargs = 0;
  601. contentx = -1;
  602. lengthx = -1;
  603. namex = -1;
  604. namelengthx = -1;
  605. forms = (struct curl_forms *) NULL;
  606. va_start(arg, last_post);
  607. for(;;) {
  608. /* Make sure there is still room for an item in local array. */
  609. if(nargs >= lformlen) {
  610. lformlen += ALLOC_GRANULE;
  611. tforms = realloc(lforms, lformlen * sizeof *lforms);
  612. if(!tforms) {
  613. result = CURL_FORMADD_MEMORY;
  614. break;
  615. }
  616. lforms = tforms;
  617. }
  618. /* Get next option. */
  619. if(forms) {
  620. /* Get option from array. */
  621. option = forms->option;
  622. value = forms->value;
  623. forms++;
  624. }
  625. else {
  626. /* Get option from arguments. */
  627. option = va_arg(arg, CURLformoption);
  628. if(option == CURLFORM_END)
  629. break;
  630. }
  631. /* Dispatch by option. */
  632. switch (option) {
  633. case CURLFORM_END:
  634. forms = (struct curl_forms *) NULL; /* Leave array mode. */
  635. continue;
  636. case CURLFORM_ARRAY:
  637. if(!forms) {
  638. forms = va_arg(arg, struct curl_forms *);
  639. continue;
  640. }
  641. result = CURL_FORMADD_ILLEGAL_ARRAY;
  642. break;
  643. case CURLFORM_COPYNAME:
  644. option = CURLFORM_PTRNAME; /* Static for now. */
  645. case CURLFORM_PTRNAME:
  646. if(namex >= 0)
  647. result = CURL_FORMADD_OPTION_TWICE;
  648. namex = nargs;
  649. if(!forms) {
  650. value = va_arg(arg, char *);
  651. nameccsid = (unsigned int) va_arg(arg, long);
  652. }
  653. else {
  654. nameccsid = (unsigned int) forms->value;
  655. forms++;
  656. }
  657. break;
  658. case CURLFORM_COPYCONTENTS:
  659. if(contentx >= 0)
  660. result = CURL_FORMADD_OPTION_TWICE;
  661. contentx = nargs;
  662. if(!forms) {
  663. value = va_arg(arg, char *);
  664. contentccsid = (unsigned int) va_arg(arg, long);
  665. }
  666. else {
  667. contentccsid = (unsigned int) forms->value;
  668. forms++;
  669. }
  670. break;
  671. case CURLFORM_PTRCONTENTS:
  672. case CURLFORM_BUFFERPTR:
  673. if(!forms)
  674. value = va_arg(arg, char *); /* No conversion. */
  675. break;
  676. case CURLFORM_CONTENTSLENGTH:
  677. lengthx = nargs;
  678. if(!forms)
  679. value = (char *) va_arg(arg, long);
  680. break;
  681. case CURLFORM_CONTENTLEN:
  682. lengthx = nargs;
  683. if(!forms)
  684. value = (char *) va_arg(arg, curl_off_t);
  685. break;
  686. case CURLFORM_NAMELENGTH:
  687. namelengthx = nargs;
  688. if(!forms)
  689. value = (char *) va_arg(arg, long);
  690. break;
  691. case CURLFORM_BUFFERLENGTH:
  692. if(!forms)
  693. value = (char *) va_arg(arg, long);
  694. break;
  695. case CURLFORM_CONTENTHEADER:
  696. if(!forms)
  697. value = (char *) va_arg(arg, struct curl_slist *);
  698. break;
  699. case CURLFORM_STREAM:
  700. if(!forms)
  701. value = (char *) va_arg(arg, void *);
  702. break;
  703. case CURLFORM_CONTENTTYPE:
  704. /* If a previous content has been encountered, convert it now. */
  705. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
  706. result = CURL_FORMADD_MEMORY;
  707. break;
  708. }
  709. contentx = -1;
  710. lengthx = -1;
  711. /* Fall into default. */
  712. default:
  713. /* Must be a convertible string. */
  714. if(!Curl_is_formadd_string(option)) {
  715. result = CURL_FORMADD_UNKNOWN_OPTION;
  716. break;
  717. }
  718. if(!forms) {
  719. value = va_arg(arg, char *);
  720. ccsid = (unsigned int) va_arg(arg, long);
  721. }
  722. else {
  723. ccsid = (unsigned int) forms->value;
  724. forms++;
  725. }
  726. /* Do the conversion. */
  727. lforms[nargs].value = value;
  728. if(Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
  729. result = CURL_FORMADD_MEMORY;
  730. break;
  731. }
  732. value = lforms[nargs].value;
  733. }
  734. if(result != CURL_FORMADD_OK)
  735. break;
  736. lforms[nargs].value = value;
  737. lforms[nargs++].option = option;
  738. }
  739. va_end(arg);
  740. /* Convert the name and the last content, now that we know their lengths. */
  741. if(result == CURL_FORMADD_OK && namex >= 0) {
  742. if(Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
  743. result = CURL_FORMADD_MEMORY;
  744. else
  745. lforms[namex].option = CURLFORM_COPYNAME; /* Force copy. */
  746. }
  747. if(result == CURL_FORMADD_OK) {
  748. if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
  749. result = CURL_FORMADD_MEMORY;
  750. else
  751. contentx = -1;
  752. }
  753. /* Do the formadd with our converted parameters. */
  754. if(result == CURL_FORMADD_OK) {
  755. lforms[nargs].option = CURLFORM_END;
  756. result = curl_formadd(httppost, last_post,
  757. CURLFORM_ARRAY, lforms, CURLFORM_END);
  758. }
  759. /* Terminate. */
  760. Curl_formadd_release_local(lforms, nargs, contentx);
  761. return result;
  762. }
  763. typedef struct {
  764. curl_formget_callback append;
  765. void * arg;
  766. unsigned int ccsid;
  767. } cfcdata;
  768. static size_t
  769. Curl_formget_callback_ccsid(void * arg, const char * buf, size_t len)
  770. {
  771. cfcdata * p;
  772. char * b;
  773. int l;
  774. size_t ret;
  775. p = (cfcdata *) arg;
  776. if((long) len <= 0)
  777. return (*p->append)(p->arg, buf, len);
  778. b = malloc(MAX_CONV_EXPANSION * len);
  779. if(!b)
  780. return (size_t) -1;
  781. l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
  782. if(l < 0) {
  783. free(b);
  784. return (size_t) -1;
  785. }
  786. ret = (*p->append)(p->arg, b, l);
  787. free(b);
  788. return ret == l? len: -1;
  789. }
  790. int
  791. curl_formget_ccsid(struct curl_httppost * form, void * arg,
  792. curl_formget_callback append, unsigned int ccsid)
  793. {
  794. cfcdata lcfc;
  795. lcfc.append = append;
  796. lcfc.arg = arg;
  797. lcfc.ccsid = ccsid;
  798. return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
  799. }
  800. CURLcode
  801. curl_easy_setopt_ccsid(CURL * curl, CURLoption tag, ...)
  802. {
  803. CURLcode result;
  804. va_list arg;
  805. struct SessionHandle * data;
  806. char * s;
  807. char * cp;
  808. unsigned int ccsid;
  809. curl_off_t pfsize;
  810. static char testwarn = 1;
  811. /* Warns if this procedure has not been updated when the dupstring enum
  812. changes.
  813. We (try to) do it only once: there is no need to issue several times
  814. the same message; but since threadsafeness is not handled here,
  815. this may occur (and we don't care!). */
  816. if(testwarn) {
  817. testwarn = 0;
  818. if((int) STRING_LASTZEROTERMINATED != (int) STRING_UNIX_SOCKET_PATH + 1 ||
  819. (int) STRING_LAST != (int) STRING_COPYPOSTFIELDS + 1)
  820. curl_mfprintf(stderr,
  821. "*** WARNING: curl_easy_setopt_ccsid() should be reworked ***\n");
  822. }
  823. data = (struct SessionHandle *) curl;
  824. va_start(arg, tag);
  825. switch (tag) {
  826. case CURLOPT_CAINFO:
  827. case CURLOPT_CAPATH:
  828. case CURLOPT_COOKIE:
  829. case CURLOPT_COOKIEFILE:
  830. case CURLOPT_COOKIEJAR:
  831. case CURLOPT_COOKIELIST:
  832. case CURLOPT_CRLFILE:
  833. case CURLOPT_CUSTOMREQUEST:
  834. case CURLOPT_DEFAULT_PROTOCOL:
  835. case CURLOPT_DNS_SERVERS:
  836. case CURLOPT_EGDSOCKET:
  837. case CURLOPT_ENCODING:
  838. case CURLOPT_FTP_ACCOUNT:
  839. case CURLOPT_FTP_ALTERNATIVE_TO_USER:
  840. case CURLOPT_FTPPORT:
  841. case CURLOPT_INTERFACE:
  842. case CURLOPT_ISSUERCERT:
  843. case CURLOPT_KEYPASSWD:
  844. case CURLOPT_KRBLEVEL:
  845. case CURLOPT_LOGIN_OPTIONS:
  846. case CURLOPT_MAIL_FROM:
  847. case CURLOPT_MAIL_AUTH:
  848. case CURLOPT_NETRC_FILE:
  849. case CURLOPT_NOPROXY:
  850. case CURLOPT_PASSWORD:
  851. case CURLOPT_PINNEDPUBLICKEY:
  852. case CURLOPT_PROXY:
  853. case CURLOPT_PROXYPASSWORD:
  854. case CURLOPT_PROXYUSERNAME:
  855. case CURLOPT_PROXYUSERPWD:
  856. case CURLOPT_PROXY_SERVICE_NAME:
  857. case CURLOPT_RANDOM_FILE:
  858. case CURLOPT_RANGE:
  859. case CURLOPT_REFERER:
  860. case CURLOPT_RTSP_SESSION_ID:
  861. case CURLOPT_RTSP_STREAM_URI:
  862. case CURLOPT_RTSP_TRANSPORT:
  863. case CURLOPT_SERVICE_NAME:
  864. case CURLOPT_SOCKS5_GSSAPI_SERVICE:
  865. case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
  866. case CURLOPT_SSH_KNOWNHOSTS:
  867. case CURLOPT_SSH_PRIVATE_KEYFILE:
  868. case CURLOPT_SSH_PUBLIC_KEYFILE:
  869. case CURLOPT_SSLCERT:
  870. case CURLOPT_SSLCERTTYPE:
  871. case CURLOPT_SSL_CIPHER_LIST:
  872. case CURLOPT_SSLENGINE:
  873. case CURLOPT_SSLKEY:
  874. case CURLOPT_SSLKEYTYPE:
  875. case CURLOPT_TLSAUTH_PASSWORD:
  876. case CURLOPT_TLSAUTH_TYPE:
  877. case CURLOPT_TLSAUTH_USERNAME:
  878. case CURLOPT_UNIX_SOCKET_PATH:
  879. case CURLOPT_URL:
  880. case CURLOPT_USERAGENT:
  881. case CURLOPT_USERNAME:
  882. case CURLOPT_USERPWD:
  883. case CURLOPT_XOAUTH2_BEARER:
  884. s = va_arg(arg, char *);
  885. ccsid = va_arg(arg, unsigned int);
  886. if(s) {
  887. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  888. if(!s) {
  889. result = CURLE_OUT_OF_MEMORY;
  890. break;
  891. }
  892. }
  893. result = curl_easy_setopt(curl, tag, s);
  894. free(s);
  895. break;
  896. case CURLOPT_COPYPOSTFIELDS:
  897. /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
  898. prior to this call. In this case, convert the given byte count and
  899. replace the length according to the conversion result. */
  900. s = va_arg(arg, char *);
  901. ccsid = va_arg(arg, unsigned int);
  902. pfsize = data->set.postfieldsize;
  903. if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
  904. result = curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, s);
  905. break;
  906. }
  907. if(pfsize == -1) {
  908. /* Data is null-terminated. */
  909. s = dynconvert(ASCII_CCSID, s, -1, ccsid);
  910. if(!s) {
  911. result = CURLE_OUT_OF_MEMORY;
  912. break;
  913. }
  914. }
  915. else {
  916. /* Data length specified. */
  917. size_t len;
  918. if(pfsize < 0 || pfsize > SIZE_MAX) {
  919. result = CURLE_OUT_OF_MEMORY;
  920. break;
  921. }
  922. len = pfsize;
  923. pfsize = len * MAX_CONV_EXPANSION;
  924. if(pfsize > SIZE_MAX)
  925. pfsize = SIZE_MAX;
  926. cp = malloc(pfsize);
  927. if(!cp) {
  928. result = CURLE_OUT_OF_MEMORY;
  929. break;
  930. }
  931. pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
  932. if(pfsize < 0) {
  933. free(cp);
  934. result = CURLE_OUT_OF_MEMORY;
  935. break;
  936. }
  937. data->set.postfieldsize = pfsize; /* Replace data size. */
  938. s = cp;
  939. }
  940. result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s);
  941. data->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
  942. break;
  943. case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
  944. default:
  945. result = Curl_setopt(data, tag, arg);
  946. break;
  947. }
  948. va_end(arg);
  949. return result;
  950. }
  951. char *
  952. curl_form_long_value(long value)
  953. {
  954. /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
  955. return (char *) value;
  956. }
  957. char *
  958. curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
  959. size_t num, unsigned int ccsid)
  960. {
  961. char *d = (char *) NULL;
  962. char *s = curl_pushheader_bynum(h, num);
  963. if(s)
  964. d = dynconvert(ccsid, s, -1, ASCII_CCSID);
  965. return d;
  966. }
  967. char *
  968. curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
  969. unsigned int ccsidin, unsigned int ccsidout)
  970. {
  971. char *d = (char *) NULL;
  972. char *s;
  973. if(header) {
  974. header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
  975. if(header) {
  976. s = curl_pushheader_byname(h, header);
  977. free((char *) header);
  978. if(s)
  979. d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
  980. }
  981. }
  982. return d;
  983. }