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