ccsidcurl.c 32 KB

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