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