ccsidcurl.c 33 KB

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