1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
23 ***************************************************************************/
25 /* CCSID API wrappers for OS/400. */
40 #include "ccsidcurl.h"
45 #define SIZE_MAX ((size_t) ~0) /* Is unsigned on OS/400. */
49 #define ASCII_CCSID 819 /* Use ISO-8859-1 as ASCII. */
50 #define NOCONV_CCSID 65535 /* No conversion. */
51 #define ICONV_ID_SIZE 32 /* Size of iconv_open() code identifier. */
52 #define ICONV_OPEN_ERROR(t) ((t).return_value == -1)
54 #define ALLOC_GRANULE 8 /* Alloc. granule for curl_formadd_ccsid(). */
58 makeOS400IconvCode(char buf[ICONV_ID_SIZE], unsigned int ccsid)
62 *** Convert a CCSID to the corresponding IBM iconv_open() character
64 *** This code is specific to the OS400 implementation of the iconv library.
65 *** CCSID 65535 (no conversion) is replaced by the ASCII CCSID.
66 *** CCSID 0 is interpreted by the OS400 as the job's CCSID.
71 if (ccsid == NOCONV_CCSID)
74 memset(buf, 0, ICONV_ID_SIZE);
75 curl_msprintf(buf, "IBMCCSID%05u0000000", ccsid);
80 iconv_open_CCSID(unsigned int ccsidout, unsigned int ccsidin, unsigned int cstr)
83 char fromcode[ICONV_ID_SIZE];
84 char tocode[ICONV_ID_SIZE];
87 *** Like iconv_open(), but character codes are given as CCSIDs.
88 *** If `cstr' is non-zero, conversion is set up to stop whenever a
89 *** null character is encountered.
90 *** See iconv_open() IBM description in "National Language Support API".
93 makeOS400IconvCode(fromcode, ccsidin);
94 makeOS400IconvCode(tocode, ccsidout);
95 memset(tocode + 13, 0, sizeof tocode - 13); /* Dest. code id format. */
98 fromcode[18] = '1'; /* Set null-terminator flag. */
100 return iconv_open(tocode, fromcode);
105 convert(char * d, size_t dlen, int dccsid,
106 const char * s, int slen, int sccsid)
114 *** Convert `sccsid'-coded `slen'-data bytes at `s' into `dccsid'-coded
115 *** data stored in the `dlen'-byte buffer at `d'.
116 *** If `slen' < 0, source string is null-terminated.
117 *** CCSID 65535 (no conversion) is replaced by the ASCII CCSID.
118 *** Return the converted destination byte count, or -1 if error.
122 sccsid = ASCII_CCSID;
125 dccsid = ASCII_CCSID;
127 if (sccsid == dccsid) {
128 lslen = slen >= 0? slen: strlen(s) + 1;
129 i = lslen < dlen? lslen: dlen;
139 cd = iconv_open_CCSID(dccsid, sccsid, 1);
142 lslen = (size_t) slen;
143 cd = iconv_open_CCSID(dccsid, sccsid, 0);
146 if (ICONV_OPEN_ERROR(cd))
151 if ((int) iconv(cd, (char * *) &s, &lslen, &d, &dlen) < 0)
162 dynconvert(int dccsid, const char * s, int slen, int sccsid)
170 static const char nullbyte = 0;
172 /* Like convert, but the destination is allocated and returned. */
174 dlen = (size_t) (slen < 0? strlen(s): slen) + 1;
175 dlen *= MAX_CONV_EXPANSION; /* Allow some expansion. */
179 return (char *) NULL;
181 l = convert(d, dlen, dccsid, s, slen, sccsid);
185 return (char *) NULL;
189 /* Need to null-terminate even when source length is given.
190 Since destination code size is unknown, use a conversion to generate
193 l2 = convert(d + l, dlen - l, dccsid, &nullbyte, -1, ASCII_CCSID);
197 return (char *) NULL;
203 if ((size_t) l < dlen) {
204 cp = realloc(d, l); /* Shorten to minimum needed. */
215 curl_version_ccsid(unsigned int ccsid)
222 aversion = curl_version();
227 i = strlen(aversion) + 1;
228 i *= MAX_CONV_EXPANSION;
230 if (!(eversion = Curl_thread_buffer(LK_CURL_VERSION, i)))
231 return (char *) NULL;
233 if (convert(eversion, i, ccsid, aversion, -1, ASCII_CCSID) < 0)
234 return (char *) NULL;
241 curl_easy_escape_ccsid(CURL * handle, const char * string, int length,
242 unsigned int sccsid, unsigned int dccsid)
250 return (char *) NULL;
253 s = dynconvert(ASCII_CCSID, s, length? length: -1, sccsid);
256 return (char *) NULL;
258 d = curl_easy_escape(handle, s, 0);
262 return (char *) NULL;
264 s = dynconvert(dccsid, d, -1, ASCII_CCSID);
271 curl_easy_unescape_ccsid(CURL * handle, const char * string, int length,
273 unsigned int sccsid, unsigned int dccsid)
281 return (char *) NULL;
284 s = dynconvert(ASCII_CCSID, s, length? length: -1, sccsid);
287 return (char *) NULL;
289 d = curl_easy_unescape(handle, s, 0, outlength);
293 return (char *) NULL;
295 s = dynconvert(dccsid, d, -1, ASCII_CCSID);
299 *outlength = strlen(s);
306 curl_slist_append_ccsid(struct curl_slist * list,
307 const char * data, unsigned int ccsid)
315 return curl_slist_append(list, data);
317 s = dynconvert(ASCII_CCSID, data, -1, ccsid);
320 return (struct curl_slist *) NULL;
322 list = curl_slist_append(list, s);
329 curl_getdate_ccsid(const char * p, const time_t * unused, unsigned int ccsid)
336 return curl_getdate(p, unused);
338 s = dynconvert(ASCII_CCSID, p, -1, ccsid);
343 t = curl_getdate(s, unused);
350 convert_version_info_string(const char * * stringp,
351 char * * bufp, int * left, unsigned int ccsid)
356 /* Helper for curl_version_info_ccsid(): convert a string if defined.
357 Result is stored in the `*left'-byte buffer at `*bufp'.
358 `*bufp' and `*left' are updated accordingly.
359 Return 0 if ok, else -1. */
362 l = convert(*bufp, *left, ccsid, *stringp, -1, ASCII_CCSID);
376 curl_version_info_data *
377 curl_version_info_ccsid(CURLversion stamp, unsigned int ccsid)
380 curl_version_info_data * p;
385 curl_version_info_data * id;
387 /* The assertion below is possible, because although the second operand
388 is an enum member, the first is a #define. In that case, the OS/400 C
389 compiler seems to compare string values after substitution. */
391 #if CURLVERSION_NOW != CURLVERSION_FOURTH
392 #error curl_version_info_data structure has changed: upgrade this procedure too.
395 /* If caller has been compiled with a new version, error. */
397 if (stamp > CURLVERSION_NOW)
398 return (curl_version_info_data *) NULL;
400 p = curl_version_info(stamp);
405 /* Measure thread space needed. */
411 while (p->protocols[nproto])
412 n += strlen(p->protocols[nproto++]);
418 n += strlen(p->version) + 1;
421 n += strlen(p->host) + 1;
424 n += strlen(p->ssl_version) + 1;
427 n += strlen(p->libz_version) + 1;
430 n += strlen(p->ares) + 1;
433 n += strlen(p->libidn) + 1;
435 if (p->libssh_version)
436 n += strlen(p->libssh_version) + 1;
438 /* Allocate thread space. */
440 n *= MAX_CONV_EXPANSION;
443 n += nproto * sizeof(const char *);
445 cp = Curl_thread_buffer(LK_VERSION_INFO_DATA, n);
446 id = (curl_version_info_data *) Curl_thread_buffer(LK_VERSION_INFO,
450 return (curl_version_info_data *) NULL;
452 /* Copy data and convert strings. */
454 memcpy((char *) id, (char *) p, sizeof *p);
457 id->protocols = (const char * const *) cp;
458 i = nproto * sizeof id->protocols[0];
459 memcpy(cp, (char *) p->protocols, i);
463 for (i = 0; id->protocols[i]; i++)
464 if (convert_version_info_string(((const char * *) id->protocols) + i,
466 return (curl_version_info_data *) NULL;
469 if (convert_version_info_string(&id->version, &cp, &n, ccsid))
470 return (curl_version_info_data *) NULL;
472 if (convert_version_info_string(&id->host, &cp, &n, ccsid))
473 return (curl_version_info_data *) NULL;
475 if (convert_version_info_string(&id->ssl_version, &cp, &n, ccsid))
476 return (curl_version_info_data *) NULL;
478 if (convert_version_info_string(&id->libz_version, &cp, &n, ccsid))
479 return (curl_version_info_data *) NULL;
481 if (convert_version_info_string(&id->ares, &cp, &n, ccsid))
482 return (curl_version_info_data *) NULL;
484 if (convert_version_info_string(&id->libidn, &cp, &n, ccsid))
485 return (curl_version_info_data *) NULL;
487 if (convert_version_info_string(&id->libssh_version, &cp, &n, ccsid))
488 return (curl_version_info_data *) NULL;
495 curl_easy_strerror_ccsid(CURLcode error, unsigned int ccsid)
502 s = curl_easy_strerror(error);
507 i = MAX_CONV_EXPANSION * (strlen(s) + 1);
509 if (!(buf = Curl_thread_buffer(LK_EASY_STRERROR, i)))
510 return (const char *) NULL;
512 if (convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
513 return (const char *) NULL;
515 return (const char *) buf;
520 curl_share_strerror_ccsid(CURLSHcode error, unsigned int ccsid)
527 s = curl_share_strerror(error);
532 i = MAX_CONV_EXPANSION * (strlen(s) + 1);
534 if (!(buf = Curl_thread_buffer(LK_SHARE_STRERROR, i)))
535 return (const char *) NULL;
537 if (convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
538 return (const char *) NULL;
540 return (const char *) buf;
545 curl_multi_strerror_ccsid(CURLMcode error, unsigned int ccsid)
552 s = curl_multi_strerror(error);
557 i = MAX_CONV_EXPANSION * (strlen(s) + 1);
559 if (!(buf = Curl_thread_buffer(LK_MULTI_STRERROR, i)))
560 return (const char *) NULL;
562 if (convert(buf, i, ccsid, s, -1, ASCII_CCSID) < 0)
563 return (const char *) NULL;
565 return (const char *) buf;
570 curl_easy_getinfo_ccsid(CURL * curl, CURLINFO info, ...)
580 struct SessionHandle * data;
582 /* WARNING: unlike curl_easy_get_info(), the strings returned by this
583 procedure have to be free'ed. */
585 data = (struct SessionHandle *) curl;
587 paramp = va_arg(arg, void *);
588 ret = Curl_getinfo(data, info, paramp);
590 if (ret != CURLE_OK || ((int) info & CURLINFO_TYPEMASK) != CURLINFO_STRING) {
595 ccsid = va_arg(arg, unsigned int);
597 cpp = (char * *) paramp;
603 d = dynconvert(ccsid, s, -1, ASCII_CCSID);
607 return CURLE_OUT_OF_MEMORY;
614 Curl_is_formadd_string(CURLformoption option)
619 case CURLFORM_FILENAME:
620 case CURLFORM_CONTENTTYPE:
621 case CURLFORM_BUFFER:
623 case CURLFORM_FILECONTENT:
624 case CURLFORM_COPYCONTENTS:
625 case CURLFORM_COPYNAME:
634 Curl_formadd_release_local(struct curl_forms * forms, int nargs, int skip)
639 if (Curl_is_formadd_string(forms[nargs].option))
640 if (forms[nargs].value)
641 free((char *) forms[nargs].value);
643 free((char *) forms);
648 Curl_formadd_convert(struct curl_forms * forms,
649 int formx, int lengthx, unsigned int ccsid)
656 if (formx < 0 || !forms[formx].value)
660 l = (int) forms[lengthx].value;
662 l = strlen(forms[formx].value) + 1;
664 cp = malloc(MAX_CONV_EXPANSION * l);
669 l = convert(cp, MAX_CONV_EXPANSION * l, ASCII_CCSID,
670 forms[formx].value, l, ccsid);
677 cp2 = realloc(cp, l); /* Shorten buffer to the string size. */
682 forms[formx].value = cp;
685 forms[lengthx].value = (char *) l; /* Update to length after conversion. */
692 curl_formadd_ccsid(struct curl_httppost * * httppost,
693 struct curl_httppost * * last_post, ...)
697 CURLformoption option;
699 struct curl_forms * forms;
700 struct curl_forms * lforms;
701 struct curl_forms * tforms;
702 unsigned int lformlen;
710 unsigned int contentccsid;
711 unsigned int nameccsid;
713 /* A single curl_formadd() call cannot be splitted in several calls to deal
714 with all parameters: the original parameters are thus copied to a local
715 curl_forms array and converted to ASCII when needed.
716 CURLFORM_PTRNAME is processed as if it were CURLFORM_COPYNAME.
717 CURLFORM_COPYNAME and CURLFORM_NAMELENGTH occurrence order in
718 parameters is not defined; for this reason, the actual conversion is
719 delayed to the end of parameter processing. The same applies to
720 CURLFORM_COPYCONTENTS/CURLFORM_CONTENTSLENGTH, but these may appear
721 several times in the parameter list; the problem resides here in knowing
722 which CURLFORM_CONTENTSLENGTH applies to which CURLFORM_COPYCONTENTS and
723 when we can be sure to have both info for conversion: end of parameter
724 list is such a point, but CURLFORM_CONTENTTYPE is also used here as a
725 natural separator between content data definitions; this seems to be
726 in accordance with FormAdd() behavior. */
728 /* Allocate the local curl_forms array. */
730 lformlen = ALLOC_GRANULE;
731 lforms = malloc(lformlen * sizeof *lforms);
734 return CURL_FORMADD_MEMORY;
736 /* Process the arguments, copying them into local array, latching conversion
737 indexes and converting when needed. */
739 result = CURL_FORMADD_OK;
745 forms = (struct curl_forms *) NULL;
746 va_start(arg, last_post);
749 /* Make sure there is still room for an item in local array. */
751 if (nargs >= lformlen) {
752 lformlen += ALLOC_GRANULE;
753 tforms = realloc(lforms, lformlen * sizeof *lforms);
756 result = CURL_FORMADD_MEMORY;
763 /* Get next option. */
766 /* Get option from array. */
768 option = forms->option;
769 value = forms->value;
773 /* Get option from arguments. */
775 option = va_arg(arg, CURLformoption);
777 if (option == CURLFORM_END)
781 /* Dispatch by option. */
786 forms = (struct curl_forms *) NULL; /* Leave array mode. */
791 forms = va_arg(arg, struct curl_forms *);
795 result = CURL_FORMADD_ILLEGAL_ARRAY;
798 case CURLFORM_COPYNAME:
799 option = CURLFORM_PTRNAME; /* Static for now. */
801 case CURLFORM_PTRNAME:
803 result = CURL_FORMADD_OPTION_TWICE;
808 value = va_arg(arg, char *);
809 nameccsid = (unsigned int) va_arg(arg, long);
812 nameccsid = (unsigned int) forms->value;
818 case CURLFORM_COPYCONTENTS:
820 result = CURL_FORMADD_OPTION_TWICE;
825 value = va_arg(arg, char *);
826 contentccsid = (unsigned int) va_arg(arg, long);
829 contentccsid = (unsigned int) forms->value;
835 case CURLFORM_PTRCONTENTS:
836 case CURLFORM_BUFFERPTR:
838 value = va_arg(arg, char *); /* No conversion. */
842 case CURLFORM_CONTENTSLENGTH:
846 value = (char *) va_arg(arg, long);
850 case CURLFORM_NAMELENGTH:
854 value = (char *) va_arg(arg, long);
858 case CURLFORM_BUFFERLENGTH:
860 value = (char *) va_arg(arg, long);
864 case CURLFORM_CONTENTHEADER:
866 value = (char *) va_arg(arg, struct curl_slist *);
870 case CURLFORM_STREAM:
872 value = (char *) va_arg(arg, void *);
876 case CURLFORM_CONTENTTYPE:
877 /* If a previous content has been encountered, convert it now. */
879 if (Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0) {
880 result = CURL_FORMADD_MEMORY;
886 /* Fall into default. */
889 /* Must be a convertible string. */
891 if (!Curl_is_formadd_string(option)) {
892 result = CURL_FORMADD_UNKNOWN_OPTION;
897 value = va_arg(arg, char *);
898 ccsid = (unsigned int) va_arg(arg, long);
901 ccsid = (unsigned int) forms->value;
905 /* Do the conversion. */
907 lforms[nargs].value = value;
909 if (Curl_formadd_convert(lforms, nargs, -1, ccsid) < 0) {
910 result = CURL_FORMADD_MEMORY;
914 value = lforms[nargs].value;
917 if (result != CURL_FORMADD_OK)
920 lforms[nargs].value = value;
921 lforms[nargs++].option = option;
926 /* Convert the name and the last content, now that we know their lengths. */
928 if (result == CURL_FORMADD_OK && namex >= 0) {
929 if (Curl_formadd_convert(lforms, namex, namelengthx, nameccsid) < 0)
930 result = CURL_FORMADD_MEMORY;
932 lforms[namex].option = CURLFORM_COPYNAME; /* Force copy. */
935 if (result == CURL_FORMADD_OK) {
936 if (Curl_formadd_convert(lforms, contentx, lengthx, contentccsid) < 0)
937 result = CURL_FORMADD_MEMORY;
942 /* Do the formadd with our converted parameters. */
944 if (result == CURL_FORMADD_OK) {
945 lforms[nargs].option = CURLFORM_END;
946 result = curl_formadd(httppost, last_post,
947 CURLFORM_ARRAY, lforms, CURLFORM_END);
952 Curl_formadd_release_local(lforms, nargs, contentx);
958 curl_formget_callback append;
965 Curl_formget_callback_ccsid(void * arg, const char * buf, size_t len)
976 return (*p->append)(p->arg, buf, len);
978 b = malloc(MAX_CONV_EXPANSION * len);
983 l = convert(b, MAX_CONV_EXPANSION * len, p->ccsid, buf, len, ASCII_CCSID);
990 ret = (*p->append)(p->arg, b, l);
992 return ret == l? len: -1;
997 curl_formget_ccsid(struct curl_httppost * form, void * arg,
998 curl_formget_callback append, unsigned int ccsid)
1003 lcfc.append = append;
1006 return curl_formget(form, (void *) &lcfc, Curl_formget_callback_ccsid);
1011 curl_easy_setopt_ccsid(CURL * curl, CURLoption tag, ...)
1016 struct SessionHandle * data;
1022 static char testwarn = 1;
1024 /* Warns if this procedure has not been updated when the dupstring enum
1026 We (try to) do it only once: there is no need to issue several times
1027 the same message; but since threadsafeness is not handled here,
1028 this may occur (and we don't care!). */
1033 if ((int) STRING_LAST != (int) STRING_MAIL_FROM + 1)
1034 curl_mfprintf(stderr,
1035 "*** WARNING: curl_easy_setopt_ccsid() should be reworked ***\n");
1038 data = (struct SessionHandle *) curl;
1043 case CURLOPT_CAINFO:
1044 case CURLOPT_CAPATH:
1045 case CURLOPT_COOKIE:
1046 case CURLOPT_COOKIEFILE:
1047 case CURLOPT_COOKIEJAR:
1048 case CURLOPT_COOKIELIST:
1049 case CURLOPT_CUSTOMREQUEST:
1050 case CURLOPT_EGDSOCKET:
1051 case CURLOPT_ENCODING:
1052 case CURLOPT_FTPPORT:
1053 case CURLOPT_FTP_ACCOUNT:
1054 case CURLOPT_FTP_ALTERNATIVE_TO_USER:
1055 case CURLOPT_INTERFACE:
1056 case CURLOPT_KEYPASSWD:
1057 case CURLOPT_KRBLEVEL:
1058 case CURLOPT_NETRC_FILE:
1060 case CURLOPT_PROXYUSERPWD:
1061 case CURLOPT_RANDOM_FILE:
1063 case CURLOPT_REFERER:
1064 case CURLOPT_SSH_PRIVATE_KEYFILE:
1065 case CURLOPT_SSH_PUBLIC_KEYFILE:
1066 case CURLOPT_SSLCERT:
1067 case CURLOPT_SSLCERTTYPE:
1068 case CURLOPT_SSLENGINE:
1069 case CURLOPT_SSLKEY:
1070 case CURLOPT_SSLKEYTYPE:
1071 case CURLOPT_SSL_CIPHER_LIST:
1073 case CURLOPT_USERAGENT:
1074 case CURLOPT_USERPWD:
1075 case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
1076 case CURLOPT_CRLFILE:
1077 case CURLOPT_ISSUERCERT:
1078 case CURLOPT_USERNAME:
1079 case CURLOPT_PASSWORD:
1080 case CURLOPT_PROXYUSERNAME:
1081 case CURLOPT_PROXYPASSWORD:
1082 case CURLOPT_NOPROXY:
1083 case CURLOPT_RTSP_SESSION_ID:
1084 case CURLOPT_RTSP_STREAM_URI:
1085 case CURLOPT_RTSP_TRANSPORT:
1086 /* SSH2 not (yet) implemented on OS400. */
1087 /* case CURLOPT_SSH_KNOWNHOSTS: */
1088 case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1089 case CURLOPT_MAIL_FROM:
1090 s = va_arg(arg, char *);
1091 ccsid = va_arg(arg, unsigned int);
1094 s = dynconvert(ASCII_CCSID, s, -1, ccsid);
1097 result = CURLE_OUT_OF_MEMORY;
1102 result = curl_easy_setopt(curl, tag, s);
1109 case CURLOPT_COPYPOSTFIELDS:
1110 /* Special case: byte count may have been given by CURLOPT_POSTFIELDSIZE
1111 prior to this call. In this case, convert the given byte count and
1112 replace the length according to the conversion result. */
1113 s = va_arg(arg, char *);
1114 ccsid = va_arg(arg, unsigned int);
1116 pfsize = data->set.postfieldsize;
1118 if (!s || !pfsize || ccsid == NOCONV_CCSID || ccsid == ASCII_CCSID) {
1119 result = curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, s);
1124 /* Data is null-terminated. */
1125 s = dynconvert(ASCII_CCSID, s, -1, ccsid);
1128 result = CURLE_OUT_OF_MEMORY;
1133 /* Data length specified. */
1135 if (pfsize < 0 || pfsize > SIZE_MAX) {
1136 result = CURLE_OUT_OF_MEMORY;
1141 pfsize = len * MAX_CONV_EXPANSION;
1143 if (pfsize > SIZE_MAX)
1146 cp = malloc(pfsize);
1149 result = CURLE_OUT_OF_MEMORY;
1153 pfsize = convert(cp, pfsize, ASCII_CCSID, s, len, ccsid);
1157 result = CURLE_OUT_OF_MEMORY;
1161 data->set.postfieldsize = pfsize; /* Replace data size. */
1165 result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s);
1166 data->set.str[STRING_COPYPOSTFIELDS] = s; /* Give to library. */
1169 case CURLOPT_ERRORBUFFER: /* This is an output buffer. */
1171 result = Curl_setopt(data, tag, arg);
1181 curl_form_long_value(long value)
1184 /* ILE/RPG cannot cast an integer to a pointer. This procedure does it. */
1186 return (char *) value;