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