xref: /curl/packages/OS400/ccsidcurl.c (revision b70e8f4b)
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     offsetof(curl_version_info_data, rtmp_version)
442   };
443 
444   /* The assertion below is possible, because although the second operand
445      is an enum member, the first is a #define. In that case, the OS/400 C
446      compiler seems to compare string values after substitution. */
447 
448 #if CURLVERSION_NOW != CURLVERSION_ELEVENTH
449 #error curl_version_info_data structure has changed: upgrade this procedure.
450 #endif
451 
452   /* If caller has been compiled with a newer version, error. */
453 
454   if(stamp > CURLVERSION_NOW)
455     return (curl_version_info_data *) NULL;
456 
457   p = curl_version_info(stamp);
458 
459   if(!p)
460     return p;
461 
462   /* Measure thread space needed. */
463 
464   n = 0;
465   nproto = 0;
466 
467   if(p->protocols) {
468     while(p->protocols[nproto])
469       n += strlen(p->protocols[nproto++]);
470 
471     n += nproto++;
472     }
473 
474   for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
475     cpp = (const char **) ((char *) p + charfields[i]);
476     if(*cpp)
477       n += strlen(*cpp) + 1;
478   }
479 
480   /* Allocate thread space. */
481 
482   n *= MAX_CONV_EXPANSION;
483 
484   if(nproto)
485     n += nproto * sizeof(const char *);
486 
487   cp = Curl_thread_buffer(LK_VERSION_INFO_DATA, n);
488   id = (curl_version_info_data *) Curl_thread_buffer(LK_VERSION_INFO,
489                                                      sizeof(*id));
490 
491   if(!id || !cp)
492     return (curl_version_info_data *) NULL;
493 
494   /* Copy data and convert strings. */
495 
496   memcpy((char *) id, (char *) p, sizeof(*p));
497 
498   if(id->protocols) {
499     i = nproto * sizeof(id->protocols[0]);
500 
501     id->protocols = (const char * const *) cp;
502     memcpy(cp, (char *) p->protocols, i);
503     cp += i;
504     n -= i;
505 
506     for(i = 0; id->protocols[i]; i++)
507       if(convert_version_info_string(((const char * *) id->protocols) + i,
508                                       &cp, &n, ccsid))
509         return (curl_version_info_data *) NULL;
510   }
511 
512   for(i = 0; i < sizeof(charfields) / sizeof(charfields[0]); i++) {
513     cpp = (const char **) ((char *) p + charfields[i]);
514     if(*cpp && convert_version_info_string(cpp, &cp, &n, ccsid))
515       return (curl_version_info_data *) NULL;
516   }
517 
518   return id;
519 }
520 
521 
522 const char *
curl_easy_strerror_ccsid(CURLcode error,unsigned int ccsid)523 curl_easy_strerror_ccsid(CURLcode error, unsigned int ccsid)
524 {
525   return keyed_string(LK_EASY_STRERROR, curl_easy_strerror(error), ccsid);
526 }
527 
528 
529 const char *
curl_share_strerror_ccsid(CURLSHcode error,unsigned int ccsid)530 curl_share_strerror_ccsid(CURLSHcode error, unsigned int ccsid)
531 {
532   return keyed_string(LK_SHARE_STRERROR, curl_share_strerror(error), ccsid);
533 }
534 
535 
536 const char *
curl_multi_strerror_ccsid(CURLMcode error,unsigned int ccsid)537 curl_multi_strerror_ccsid(CURLMcode error, unsigned int ccsid)
538 {
539   return keyed_string(LK_MULTI_STRERROR, curl_multi_strerror(error), ccsid);
540 }
541 
542 
543 const char *
curl_url_strerror_ccsid(CURLUcode error,unsigned int ccsid)544 curl_url_strerror_ccsid(CURLUcode error, unsigned int ccsid)
545 {
546   return keyed_string(LK_URL_STRERROR, curl_url_strerror(error), ccsid);
547 }
548 
549 
550 void
curl_certinfo_free_all(struct curl_certinfo * info)551 curl_certinfo_free_all(struct curl_certinfo *info)
552 {
553   /* Free all memory used by certificate info. */
554   if(info) {
555     if(info->certinfo) {
556       int i;
557 
558       for(i = 0; i < info->num_of_certs; i++)
559         curl_slist_free_all(info->certinfo[i]);
560       free((char *) info->certinfo);
561     }
562     free((char *) info);
563   }
564 }
565 
566 
567 CURLcode
curl_easy_getinfo_ccsid(CURL * curl,CURLINFO info,...)568 curl_easy_getinfo_ccsid(CURL *curl, CURLINFO info, ...)
569 {
570   va_list arg;
571   void *paramp;
572   CURLcode ret;
573   struct Curl_easy *data;
574 
575   /* WARNING: unlike curl_easy_getinfo(), the strings returned by this
576      procedure have to be free'ed. */
577 
578   data = (struct Curl_easy *) curl;
579   va_start(arg, info);
580   paramp = va_arg(arg, void *);
581   ret = Curl_getinfo(data, info, paramp);
582 
583   if(ret == CURLE_OK) {
584     unsigned int ccsid;
585     char **cpp;
586     struct curl_slist **slp;
587     struct curl_certinfo *cipf;
588     struct curl_certinfo *cipt;
589 
590     switch((int) info & CURLINFO_TYPEMASK) {
591 
592     case CURLINFO_STRING:
593       ccsid = va_arg(arg, unsigned int);
594       cpp = (char * *) paramp;
595 
596       if(*cpp) {
597         *cpp = dynconvert(ccsid, *cpp, -1, ASCII_CCSID);
598 
599         if(!*cpp)
600           ret = CURLE_OUT_OF_MEMORY;
601       }
602 
603       break;
604 
605     case CURLINFO_SLIST:
606       ccsid = va_arg(arg, unsigned int);
607       switch(info) {
608       case CURLINFO_CERTINFO:
609         cipf = *(struct curl_certinfo * *) paramp;
610         if(cipf) {
611           cipt = (struct curl_certinfo *) malloc(sizeof(*cipt));
612           if(!cipt)
613             ret = CURLE_OUT_OF_MEMORY;
614           else {
615             cipt->certinfo = (struct curl_slist **)
616               calloc(cipf->num_of_certs +
617                      1, sizeof(struct curl_slist *));
618             if(!cipt->certinfo)
619               ret = CURLE_OUT_OF_MEMORY;
620             else {
621               int i;
622 
623               cipt->num_of_certs = cipf->num_of_certs;
624               for(i = 0; i < cipf->num_of_certs; i++)
625                 if(cipf->certinfo[i])
626                   if(!(cipt->certinfo[i] = slist_convert(ccsid,
627                                                           cipf->certinfo[i],
628                                                           ASCII_CCSID))) {
629                     ret = CURLE_OUT_OF_MEMORY;
630                     break;
631                   }
632               }
633             }
634 
635           if(ret != CURLE_OK) {
636             curl_certinfo_free_all(cipt);
637             cipt = (struct curl_certinfo *) NULL;
638           }
639 
640           *(struct curl_certinfo * *) paramp = cipt;
641         }
642 
643         break;
644 
645       case CURLINFO_TLS_SESSION:
646       case CURLINFO_TLS_SSL_PTR:
647       case CURLINFO_SOCKET:
648         break;
649 
650       default:
651         slp = (struct curl_slist **) paramp;
652         if(*slp) {
653           *slp = slist_convert(ccsid, *slp, ASCII_CCSID);
654           if(!*slp)
655             ret = CURLE_OUT_OF_MEMORY;
656         }
657         break;
658       }
659     }
660   }
661 
662   va_end(arg);
663   return ret;
664 }
665 
666 
667 static int
Curl_is_formadd_string(CURLformoption option)668 Curl_is_formadd_string(CURLformoption option)
669 {
670   switch(option) {
671 
672   case CURLFORM_FILENAME:
673   case CURLFORM_CONTENTTYPE:
674   case CURLFORM_BUFFER:
675   case CURLFORM_FILE:
676   case CURLFORM_FILECONTENT:
677   case CURLFORM_COPYCONTENTS:
678   case CURLFORM_COPYNAME:
679     return 1;
680   }
681 
682   return 0;
683 }
684 
685 
686 static void
Curl_formadd_release_local(struct curl_forms * forms,int nargs,int skip)687 Curl_formadd_release_local(struct curl_forms *forms, int nargs, int skip)
688 {
689   while(nargs--)
690     if(nargs != skip)
691       if(Curl_is_formadd_string(forms[nargs].option))
692         if(forms[nargs].value)
693           free((char *) forms[nargs].value);
694 
695   free((char *) forms);
696 }
697 
698 
699 static int
Curl_formadd_convert(struct curl_forms * forms,int formx,int lengthx,unsigned int ccsid)700 Curl_formadd_convert(struct curl_forms *forms,
701                      int formx, int lengthx, unsigned int ccsid)
702 {
703   int l;
704   char *cp;
705   char *cp2;
706 
707   if(formx < 0 || !forms[formx].value)
708     return 0;
709 
710   if(lengthx >= 0)
711     l = (int) forms[lengthx].value;
712   else
713     l = strlen(forms[formx].value) + 1;
714 
715   cp = malloc(MAX_CONV_EXPANSION * l);
716 
717   if(!cp)
718     return -1;
719 
720   l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
721               forms[formx].value, l, ccsid);
722 
723   if(l < 0) {
724     free(cp);
725     return -1;
726     }
727 
728   cp2 = realloc(cp, l);         /* Shorten buffer to the string size. */
729 
730   if(cp2)
731     cp = cp2;
732 
733   forms[formx].value = cp;
734 
735   if(lengthx >= 0)
736     forms[lengthx].value = (char *) l;  /* Update length after conversion. */
737 
738   return l;
739 }
740 
741 
742 CURLFORMcode
curl_formadd_ccsid(struct curl_httppost ** httppost,struct curl_httppost ** last_post,...)743 curl_formadd_ccsid(struct curl_httppost **httppost,
744                    struct curl_httppost **last_post, ...)
745 {
746   va_list arg;
747   CURLformoption option;
748   CURLFORMcode result;
749   struct curl_forms *forms;
750   struct curl_forms *lforms;
751   struct curl_forms *tforms;
752   unsigned int lformlen;
753   const char *value;
754   unsigned int ccsid;
755   int nargs;
756   int namex;
757   int namelengthx;
758   int contentx;
759   int lengthx;
760   unsigned int contentccsid;
761   unsigned int nameccsid;
762 
763   /* A single curl_formadd() call cannot be split in several calls to deal
764      with all parameters: the original parameters are thus copied to a local
765      curl_forms array and converted to ASCII when needed.
766      CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
767      CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
768      parameters is not defined; for this reason, the actual conversion is
769      delayed to the end of parameter processing. The same applies to
770      CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
771      several times in the parameter list; the problem resides here in knowing
772      which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
773      when we can be sure to have both info for conversion: end of parameter
774      list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
775      natural separator between content data definitions; this seems to be
776      in accordance with FormAdd() behavior. */
777 
778   /* Allocate the local curl_forms array. */
779 
780   lformlen = ALLOC_GRANULE;
781   lforms = malloc(lformlen * sizeof(*lforms));
782 
783   if(!lforms)
784     return CURL_FORMADD_MEMORY;
785 
786   /* Process the arguments, copying them into local array, latching conversion
787      indexes and converting when needed. */
788 
789   result = CURL_FORMADD_OK;
790   nargs = 0;
791   contentx = -1;
792   lengthx = -1;
793   namex = -1;
794   namelengthx = -1;
795   forms = (struct curl_forms *) NULL;
796   va_start(arg, last_post);
797 
798   for(;;) {
799     /* Make sure there is still room for an item in local array. */
800 
801     if(nargs >= lformlen) {
802       lformlen += ALLOC_GRANULE;
803       tforms = realloc(lforms, lformlen * sizeof(*lforms));
804 
805       if(!tforms) {
806         result = CURL_FORMADD_MEMORY;
807         break;
808         }
809 
810       lforms = tforms;
811       }
812 
813     /* Get next option. */
814 
815     if(forms) {
816       /* Get option from array. */
817 
818       option = forms->option;
819       value = forms->value;
820       forms++;
821       }
822     else {
823       /* Get option from arguments. */
824 
825       option = va_arg(arg, CURLformoption);
826 
827       if(option == CURLFORM_END)
828         break;
829       }
830 
831     /* Dispatch by option. */
832 
833     switch(option) {
834 
835     case CURLFORM_END:
836       forms = (struct curl_forms *) NULL;       /* Leave array mode. */
837       continue;
838 
839     case CURLFORM_ARRAY:
840       if(!forms) {
841         forms = va_arg(arg, struct curl_forms *);
842         continue;
843         }
844 
845       result = CURL_FORMADD_ILLEGAL_ARRAY;
846       break;
847 
848     case CURLFORM_COPYNAME:
849       option = CURLFORM_PTRNAME;                /* Static for now. */
850 
851     case CURLFORM_PTRNAME:
852       if(namex >= 0)
853         result = CURL_FORMADD_OPTION_TWICE;
854 
855       namex = nargs;
856 
857       if(!forms) {
858         value = va_arg(arg, char *);
859         nameccsid = (unsigned int) va_arg(arg, long);
860         }
861       else {
862         nameccsid = (unsigned int) forms->value;
863         forms++;
864         }
865 
866       break;
867 
868     case CURLFORM_COPYCONTENTS:
869       if(contentx >= 0)
870         result = CURL_FORMADD_OPTION_TWICE;
871 
872       contentx = nargs;
873 
874       if(!forms) {
875         value = va_arg(arg, char *);
876         contentccsid = (unsigned int) va_arg(arg, long);
877         }
878       else {
879         contentccsid = (unsigned int) forms->value;
880         forms++;
881         }
882 
883       break;
884 
885     case CURLFORM_PTRCONTENTS:
886     case CURLFORM_BUFFERPTR:
887       if(!forms)
888         value = va_arg(arg, char *);            /* No conversion. */
889 
890       break;
891 
892     case CURLFORM_CONTENTSLENGTH:
893       lengthx = nargs;
894 
895       if(!forms)
896         value = (char *) va_arg(arg, long);
897 
898       break;
899 
900     case CURLFORM_CONTENTLEN:
901       lengthx = nargs;
902 
903       if(!forms)
904         value = (char *) va_arg(arg, curl_off_t);
905 
906       break;
907 
908     case CURLFORM_NAMELENGTH:
909       namelengthx = nargs;
910 
911       if(!forms)
912         value = (char *) va_arg(arg, long);
913 
914       break;
915 
916     case CURLFORM_BUFFERLENGTH:
917       if(!forms)
918         value = (char *) va_arg(arg, long);
919 
920       break;
921 
922     case CURLFORM_CONTENTHEADER:
923       if(!forms)
924         value = (char *) va_arg(arg, struct curl_slist *);
925 
926       break;
927 
928     case CURLFORM_STREAM:
929       if(!forms)
930         value = (char *) va_arg(arg, void *);
931 
932       break;
933 
934     case CURLFORM_CONTENTTYPE:
935       /* If a previous content has been encountered, convert it now. */
936 
937       if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
938         result = CURL_FORMADD_MEMORY;
939         break;
940         }
941 
942       contentx = -1;
943       lengthx = -1;
944       /* Fall into default. */
945 
946     default:
947       /* Must be a convertible string. */
948 
949       if(!Curl_is_formadd_string(option)) {
950         result = CURL_FORMADD_UNKNOWN_OPTION;
951         break;
952         }
953 
954       if(!forms) {
955         value = va_arg(arg, char *);
956         ccsid = (unsigned int) va_arg(arg, long);
957         }
958       else {
959         ccsid = (unsigned int) forms->value;
960         forms++;
961         }
962 
963       /* Do the conversion. */
964 
965       lforms[nargs].value = value;
966 
967       if(Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
968         result = CURL_FORMADD_MEMORY;
969         break;
970         }
971 
972       value = lforms[nargs].value;
973       }
974 
975     if(result != CURL_FORMADD_OK)
976       break;
977 
978     lforms[nargs].value = value;
979     lforms[nargs++].option = option;
980     }
981 
982   va_end(arg);
983 
984   /* Convert the name and the last content, now that we know their lengths. */
985 
986   if(result == CURL_FORMADD_OK && namex >= 0) {
987     if(Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
988       result = CURL_FORMADD_MEMORY;
989     else
990       lforms[namex].option = CURLFORM_COPYNAME;         /* Force copy. */
991     }
992 
993   if(result == CURL_FORMADD_OK) {
994     if(Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
995       result = CURL_FORMADD_MEMORY;
996     else
997       contentx = -1;
998     }
999 
1000   /* Do the formadd with our converted parameters. */
1001 
1002   if(result == CURL_FORMADD_OK) {
1003     lforms[nargs].option = CURLFORM_END;
1004     result = curl_formadd(httppost, last_post,
1005                           CURLFORM_ARRAY, lforms, CURLFORM_END);
1006     }
1007 
1008   /* Terminate. */
1009 
1010   Curl_formadd_release_local(lforms, nargs, contentx);
1011   return result;
1012 }
1013 
1014 
1015 struct cfcdata {
1016   curl_formget_callback append;
1017   void *                arg;
1018   unsigned int          ccsid;
1019 };
1020 
1021 
1022 static size_t
Curl_formget_callback_ccsid(void * arg,const char * buf,size_t len)1023 Curl_formget_callback_ccsid(void *arg, const char *buf, size_t len)
1024 {
1025   struct cfcdata *p;
1026   char *b;
1027   int l;
1028   size_t ret;
1029 
1030   p = (struct cfcdata *) arg;
1031 
1032   if((long) len <= 0)
1033     return (*p->append)(p->arg, buf, len);
1034 
1035   b = malloc(MAX_CONV_EXPANSION * len);
1036 
1037   if(!b)
1038     return (size_t) -1;
1039 
1040   l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
1041 
1042   if(l < 0) {
1043     free(b);
1044     return (size_t) -1;
1045     }
1046 
1047   ret = (*p->append)(p->arg, b, l);
1048   free(b);
1049   return ret == l ? len : -1;
1050 }
1051 
1052 
1053 int
curl_formget_ccsid(struct curl_httppost * form,void * arg,curl_formget_callback append,unsigned int ccsid)1054 curl_formget_ccsid(struct curl_httppost *form, void *arg,
1055                    curl_formget_callback append, unsigned int ccsid)
1056 {
1057   struct cfcdata lcfc;
1058 
1059   lcfc.append = append;
1060   lcfc.arg = arg;
1061   lcfc.ccsid = ccsid;
1062   return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
1063 }
1064 
1065 
1066 CURLcode
curl_easy_setopt_ccsid(CURL * easy,CURLoption tag,...)1067 curl_easy_setopt_ccsid(CURL *easy, CURLoption tag, ...)
1068 {
1069   CURLcode result;
1070   va_list arg;
1071   char *s;
1072   char *cp = NULL;
1073   unsigned int ccsid;
1074   curl_off_t pfsize;
1075 
1076   va_start(arg, tag);
1077 
1078   switch(tag) {
1079 
1080   /* BEGIN TRANSLATABLE STRING OPTIONS */
1081   /* Keep option symbols in alphanumeric order and retain the BEGIN/END
1082      armor comments. */
1083   case CURLOPT_ABSTRACT_UNIX_SOCKET:
1084   case CURLOPT_ACCEPT_ENCODING:
1085   case CURLOPT_ALTSVC:
1086   case CURLOPT_AWS_SIGV4:
1087   case CURLOPT_CAINFO:
1088   case CURLOPT_CAPATH:
1089   case CURLOPT_COOKIE:
1090   case CURLOPT_COOKIEFILE:
1091   case CURLOPT_COOKIEJAR:
1092   case CURLOPT_COOKIELIST:
1093   case CURLOPT_CRLFILE:
1094   case CURLOPT_CUSTOMREQUEST:
1095   case CURLOPT_DEFAULT_PROTOCOL:
1096   case CURLOPT_DNS_INTERFACE:
1097   case CURLOPT_DNS_LOCAL_IP4:
1098   case CURLOPT_DNS_LOCAL_IP6:
1099   case CURLOPT_DNS_SERVERS:
1100   case CURLOPT_DOH_URL:
1101   case CURLOPT_ECH:
1102   case CURLOPT_EGDSOCKET:
1103   case CURLOPT_FTPPORT:
1104   case CURLOPT_FTP_ACCOUNT:
1105   case CURLOPT_FTP_ALTERNATIVE_TO_USER:
1106   case CURLOPT_HAPROXY_CLIENT_IP:
1107   case CURLOPT_HSTS:
1108   case CURLOPT_INTERFACE:
1109   case CURLOPT_ISSUERCERT:
1110   case CURLOPT_KEYPASSWD:
1111   case CURLOPT_KRBLEVEL:
1112   case CURLOPT_LOGIN_OPTIONS:
1113   case CURLOPT_MAIL_AUTH:
1114   case CURLOPT_MAIL_FROM:
1115   case CURLOPT_NETRC_FILE:
1116   case CURLOPT_NOPROXY:
1117   case CURLOPT_PASSWORD:
1118   case CURLOPT_PINNEDPUBLICKEY:
1119   case CURLOPT_PRE_PROXY:
1120   case CURLOPT_PROTOCOLS_STR:
1121   case CURLOPT_PROXY:
1122   case CURLOPT_PROXYPASSWORD:
1123   case CURLOPT_PROXYUSERNAME:
1124   case CURLOPT_PROXYUSERPWD:
1125   case CURLOPT_PROXY_CAINFO:
1126   case CURLOPT_PROXY_CAPATH:
1127   case CURLOPT_PROXY_CRLFILE:
1128   case CURLOPT_PROXY_ISSUERCERT:
1129   case CURLOPT_PROXY_KEYPASSWD:
1130   case CURLOPT_PROXY_PINNEDPUBLICKEY:
1131   case CURLOPT_PROXY_SERVICE_NAME:
1132   case CURLOPT_PROXY_SSLCERT:
1133   case CURLOPT_PROXY_SSLCERTTYPE:
1134   case CURLOPT_PROXY_SSLKEY:
1135   case CURLOPT_PROXY_SSLKEYTYPE:
1136   case CURLOPT_PROXY_SSL_CIPHER_LIST:
1137   case CURLOPT_PROXY_TLS13_CIPHERS:
1138   case CURLOPT_PROXY_TLSAUTH_PASSWORD:
1139   case CURLOPT_PROXY_TLSAUTH_TYPE:
1140   case CURLOPT_PROXY_TLSAUTH_USERNAME:
1141   case CURLOPT_RANDOM_FILE:
1142   case CURLOPT_RANGE:
1143   case CURLOPT_REDIR_PROTOCOLS_STR:
1144   case CURLOPT_REFERER:
1145   case CURLOPT_REQUEST_TARGET:
1146   case CURLOPT_RTSP_SESSION_ID:
1147   case CURLOPT_RTSP_STREAM_URI:
1148   case CURLOPT_RTSP_TRANSPORT:
1149   case CURLOPT_SASL_AUTHZID:
1150   case CURLOPT_SERVICE_NAME:
1151   case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1152   case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
1153   case CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256:
1154   case CURLOPT_SSH_KNOWNHOSTS:
1155   case CURLOPT_SSH_PRIVATE_KEYFILE:
1156   case CURLOPT_SSH_PUBLIC_KEYFILE:
1157   case CURLOPT_SSLCERT:
1158   case CURLOPT_SSLCERTTYPE:
1159   case CURLOPT_SSLENGINE:
1160   case CURLOPT_SSLKEY:
1161   case CURLOPT_SSLKEYTYPE:
1162   case CURLOPT_SSL_CIPHER_LIST:
1163   case CURLOPT_SSL_EC_CURVES:
1164   case CURLOPT_TLS13_CIPHERS:
1165   case CURLOPT_TLSAUTH_PASSWORD:
1166   case CURLOPT_TLSAUTH_TYPE:
1167   case CURLOPT_TLSAUTH_USERNAME:
1168   case CURLOPT_UNIX_SOCKET_PATH:
1169   case CURLOPT_URL:
1170   case CURLOPT_USERAGENT:
1171   case CURLOPT_USERNAME:
1172   case CURLOPT_USERPWD:
1173   case CURLOPT_XOAUTH2_BEARER:
1174   /* END TRANSLATABLE STRING OPTIONS */
1175     s = va_arg(arg, char *);
1176     ccsid = va_arg(arg, unsigned int);
1177 
1178     if(s) {
1179       s = dynconvert(ASCII_CCSID, s, -1, ccsid);
1180 
1181       if(!s) {
1182         result = CURLE_OUT_OF_MEMORY;
1183         break;
1184       }
1185     }
1186 
1187     result = curl_easy_setopt(easy, tag, s);
1188     free(s);
1189     break;
1190 
1191   case CURLOPT_COPYPOSTFIELDS:
1192     /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
1193        prior to this call. In this case, convert the given byte count and
1194        replace the length according to the conversion result. */
1195     s = va_arg(arg, char *);
1196     ccsid = va_arg(arg, unsigned int);
1197 
1198     pfsize = easy->set.postfieldsize;
1199 
1200     if(!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
1201       result = curl_easy_setopt(easy, CURLOPT_COPYPOSTFIELDS, s);
1202       break;
1203     }
1204 
1205     if(pfsize == -1) {
1206       /* Data is null-terminated. */
1207       s = dynconvert(ASCII_CCSID, s, -1, ccsid);
1208 
1209       if(!s) {
1210         result = CURLE_OUT_OF_MEMORY;
1211         break;
1212         }
1213       }
1214     else {
1215       /* Data length specified. */
1216       size_t len;
1217 
1218       if(pfsize < 0 || pfsize > SIZE_MAX) {
1219         result = CURLE_OUT_OF_MEMORY;
1220         break;
1221       }
1222 
1223       len = pfsize;
1224       pfsize = len * MAX_CONV_EXPANSION;
1225 
1226       if(pfsize > SIZE_MAX)
1227         pfsize = SIZE_MAX;
1228 
1229       cp = malloc(pfsize);
1230 
1231       if(!cp) {
1232         result = CURLE_OUT_OF_MEMORY;
1233         break;
1234       }
1235 
1236       pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
1237 
1238       if(pfsize < 0) {
1239         result = CURLE_OUT_OF_MEMORY;
1240         break;
1241       }
1242 
1243       easy->set.postfieldsize = pfsize;         /* Replace data size. */
1244       s = cp;
1245     }
1246 
1247     result = curl_easy_setopt(easy, CURLOPT_POSTFIELDS, s);
1248     easy->set.str[STRING_COPYPOSTFIELDS] = s;   /* Give to library. */
1249     break;
1250 
1251   default:
1252     if(tag / 10000 == CURLOPTTYPE_BLOB) {
1253       struct curl_blob *bp = va_arg(arg, struct curl_blob *);
1254       struct curl_blob blob;
1255 
1256       ccsid = va_arg(arg, unsigned int);
1257 
1258       if(bp && bp->data && bp->len &&
1259          ccsid != NOCONV_CCSID && ccsid != ASCII_CCSID) {
1260         pfsize = (curl_off_t) bp->len * MAX_CONV_EXPANSION;
1261 
1262         if(pfsize > SIZE_MAX)
1263           pfsize = SIZE_MAX;
1264 
1265         cp = malloc(pfsize);
1266 
1267         if(!cp) {
1268           result = CURLE_OUT_OF_MEMORY;
1269           break;
1270         }
1271 
1272         pfsize = convert(cp, pfsize, ASCII_CCSID, bp->data, bp->len, ccsid);
1273 
1274         if(pfsize < 0) {
1275           result = CURLE_OUT_OF_MEMORY;
1276           break;
1277         }
1278 
1279         blob.data = cp;
1280         blob.len = pfsize;
1281         blob.flags = bp->flags | CURL_BLOB_COPY;
1282         bp = &blob;
1283       }
1284       result = curl_easy_setopt(easy, tag, &blob);
1285       break;
1286     }
1287     FALLTHROUGH();
1288   case CURLOPT_ERRORBUFFER:                     /* This is an output buffer. */
1289     result = Curl_vsetopt(easy, tag, arg);
1290     break;
1291   }
1292 
1293   va_end(arg);
1294   free(cp);
1295   return result;
1296 }
1297 
1298 
1299 /* ILE/RPG helper functions. */
1300 
1301 char *
curl_form_long_value(long value)1302 curl_form_long_value(long value)
1303 {
1304   /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
1305 
1306   return (char *) value;
1307 }
1308 
1309 
1310 CURLcode
curl_easy_setopt_RPGnum_(CURL * easy,CURLoption tag,curl_off_t arg)1311 curl_easy_setopt_RPGnum_(CURL *easy, CURLoption tag, curl_off_t arg)
1312 {
1313   /* ILE/RPG procedure overloading cannot discriminate between different
1314      size and/or signedness of format arguments. This provides a generic
1315      wrapper that adapts size to the given tag expectation.
1316      This procedure is not intended to be explicitly called from user code. */
1317   if(tag / 10000 != CURLOPTTYPE_OFF_T)
1318     return curl_easy_setopt(easy, tag, (long) arg);
1319   return curl_easy_setopt(easy, tag, arg);
1320 }
1321 
1322 
1323 CURLcode
curl_multi_setopt_RPGnum_(CURLM * multi,CURLMoption tag,curl_off_t arg)1324 curl_multi_setopt_RPGnum_(CURLM *multi, CURLMoption tag, curl_off_t arg)
1325 {
1326   /* Likewise, for multi handle. */
1327   if(tag / 10000 != CURLOPTTYPE_OFF_T)
1328     return curl_multi_setopt(multi, tag, (long) arg);
1329   return curl_multi_setopt(multi, tag, arg);
1330 }
1331 
1332 
1333 char *
curl_pushheader_bynum_cssid(struct curl_pushheaders * h,size_t num,unsigned int ccsid)1334 curl_pushheader_bynum_cssid(struct curl_pushheaders *h,
1335                             size_t num, unsigned int ccsid)
1336 {
1337   char *d = (char *) NULL;
1338   char *s = curl_pushheader_bynum(h, num);
1339 
1340   if(s)
1341     d = dynconvert(ccsid, s, -1, ASCII_CCSID);
1342 
1343   return d;
1344 }
1345 
1346 
1347 char *
curl_pushheader_byname_ccsid(struct curl_pushheaders * h,const char * header,unsigned int ccsidin,unsigned int ccsidout)1348 curl_pushheader_byname_ccsid(struct curl_pushheaders *h, const char *header,
1349                              unsigned int ccsidin, unsigned int ccsidout)
1350 {
1351   char *d = (char *) NULL;
1352 
1353   if(header) {
1354     header = dynconvert(ASCII_CCSID, header, -1, ccsidin);
1355 
1356     if(header) {
1357       char *s = curl_pushheader_byname(h, header);
1358       free((char *) header);
1359 
1360       if(s)
1361         d = dynconvert(ccsidout, s, -1, ASCII_CCSID);
1362     }
1363   }
1364 
1365   return d;
1366 }
1367 
1368 static CURLcode
mime_string_call(curl_mimepart * part,const char * string,unsigned int ccsid,CURLcode (* mimefunc)(curl_mimepart * part,const char * string))1369 mime_string_call(curl_mimepart *part, const char *string, unsigned int ccsid,
1370                  CURLcode (*mimefunc)(curl_mimepart *part, const char *string))
1371 {
1372   char *s = (char *) NULL;
1373   CURLcode result;
1374 
1375   if(!string)
1376     return mimefunc(part, string);
1377   s = dynconvert(ASCII_CCSID, string, -1, ccsid);
1378   if(!s)
1379     return CURLE_OUT_OF_MEMORY;
1380 
1381   result = mimefunc(part, s);
1382   free(s);
1383   return result;
1384 }
1385 
1386 CURLcode
curl_mime_name_ccsid(curl_mimepart * part,const char * name,unsigned int ccsid)1387 curl_mime_name_ccsid(curl_mimepart *part, const char *name, unsigned int ccsid)
1388 {
1389   return mime_string_call(part, name, ccsid, curl_mime_name);
1390 }
1391 
1392 CURLcode
curl_mime_filename_ccsid(curl_mimepart * part,const char * filename,unsigned int ccsid)1393 curl_mime_filename_ccsid(curl_mimepart *part,
1394                          const char *filename, unsigned int ccsid)
1395 {
1396   return mime_string_call(part, filename, ccsid, curl_mime_filename);
1397 }
1398 
1399 CURLcode
curl_mime_type_ccsid(curl_mimepart * part,const char * mimetype,unsigned int ccsid)1400 curl_mime_type_ccsid(curl_mimepart *part,
1401                      const char *mimetype, unsigned int ccsid)
1402 {
1403   return mime_string_call(part, mimetype, ccsid, curl_mime_type);
1404 }
1405 
1406 CURLcode
curl_mime_encoder_ccsid(curl_mimepart * part,const char * encoding,unsigned int ccsid)1407 curl_mime_encoder_ccsid(curl_mimepart *part,
1408                        const char *encoding, unsigned int ccsid)
1409 {
1410   return mime_string_call(part, encoding, ccsid, curl_mime_encoder);
1411 }
1412 
1413 CURLcode
curl_mime_filedata_ccsid(curl_mimepart * part,const char * filename,unsigned int ccsid)1414 curl_mime_filedata_ccsid(curl_mimepart *part,
1415                          const char *filename, unsigned int ccsid)
1416 {
1417   return mime_string_call(part, filename, ccsid, curl_mime_filedata);
1418 }
1419 
1420 CURLcode
curl_mime_data_ccsid(curl_mimepart * part,const char * data,size_t datasize,unsigned int ccsid)1421 curl_mime_data_ccsid(curl_mimepart *part,
1422                      const char *data, size_t datasize, unsigned int ccsid)
1423 {
1424   char *s = (char *) NULL;
1425   CURLcode result;
1426 
1427   if(!data)
1428     return curl_mime_data(part, data, datasize);
1429   s = dynconvert(ASCII_CCSID, data, datasize, ccsid);
1430   if(!s)
1431     return CURLE_OUT_OF_MEMORY;
1432 
1433   result = curl_mime_data(part, s, datasize);
1434   free(s);
1435   return result;
1436 }
1437 
1438 CURLUcode
curl_url_get_ccsid(CURLU * handle,CURLUPart what,char ** part,unsigned int flags,unsigned int ccsid)1439 curl_url_get_ccsid(CURLU *handle, CURLUPart what, char **part,
1440                    unsigned int flags, unsigned int ccsid)
1441 {
1442   char *s = (char *)NULL;
1443   CURLUcode result;
1444 
1445   if(!part)
1446     return CURLUE_BAD_PARTPOINTER;
1447 
1448   *part = (char *)NULL;
1449   result = curl_url_get(handle, what, &s, flags);
1450   if(result == CURLUE_OK) {
1451     if(s) {
1452       *part = dynconvert(ccsid, s, -1, ASCII_CCSID);
1453       if(!*part)
1454         result = CURLUE_OUT_OF_MEMORY;
1455     }
1456   }
1457   if(s)
1458     free(s);
1459   return result;
1460 }
1461 
1462 CURLUcode
curl_url_set_ccsid(CURLU * handle,CURLUPart what,const char * part,unsigned int flags,unsigned int ccsid)1463 curl_url_set_ccsid(CURLU *handle, CURLUPart what, const char *part,
1464                    unsigned int flags, unsigned int ccsid)
1465 {
1466   char *s = (char *)NULL;
1467   CURLUcode result;
1468 
1469   if(part) {
1470     s = dynconvert(ASCII_CCSID, part, -1, ccsid);
1471     if(!s)
1472       return CURLUE_OUT_OF_MEMORY;
1473   }
1474   result = curl_url_set(handle, what, s, flags);
1475   if(s)
1476     free(s);
1477   return result;
1478 }
1479 
1480 const struct curl_easyoption *
curl_easy_option_by_name_ccsid(const char * name,unsigned int ccsid)1481 curl_easy_option_by_name_ccsid(const char *name, unsigned int ccsid)
1482 {
1483   const struct curl_easyoption *option = NULL;
1484 
1485   if(name) {
1486     char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
1487 
1488     if(s) {
1489       option = curl_easy_option_by_name(s);
1490       free(s);
1491     }
1492   }
1493 
1494   return option;
1495 }
1496 
1497 /* Return option name in the given ccsid. */
1498 const char *
curl_easy_option_get_name_ccsid(const struct curl_easyoption * option,unsigned int ccsid)1499 curl_easy_option_get_name_ccsid(const struct curl_easyoption *option,
1500                                 unsigned int ccsid)
1501 {
1502   char *name = NULL;
1503 
1504   if(option && option->name)
1505     name = dynconvert(ccsid, option->name, -1, ASCII_CCSID);
1506 
1507   return (const char *) name;
1508 }
1509 
1510 /* Header API CCSID support. */
1511 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)1512 curl_easy_header_ccsid(CURL *easy, const char *name, size_t index,
1513                        unsigned int origin, int request,
1514                        struct curl_header **hout, unsigned int ccsid)
1515 {
1516   CURLHcode result = CURLHE_BAD_ARGUMENT;
1517 
1518   if(name) {
1519     char *s = dynconvert(ASCII_CCSID, name, -1, ccsid);
1520 
1521     result = CURLHE_OUT_OF_MEMORY;
1522     if(s) {
1523       result = curl_easy_header(easy, s, index, origin, request, hout);
1524       free(s);
1525     }
1526   }
1527 
1528   return result;
1529 }
1530