Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | #include "curl_setup.h" |
| 24 | |
| 25 | #ifdef CURL_DOES_CONVERSIONS |
| 26 | |
| 27 | #include <curl/curl.h> |
| 28 | |
| 29 | #include "non-ascii.h" |
| 30 | #include "formdata.h" |
| 31 | #include "sendf.h" |
| 32 | #include "urldata.h" |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 33 | #include "multiif.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 34 | |
| 35 | #include "curl_memory.h" |
| 36 | /* The last #include file should be: */ |
| 37 | #include "memdebug.h" |
| 38 | |
| 39 | #ifdef HAVE_ICONV |
| 40 | #include <iconv.h> |
| 41 | /* set default codesets for iconv */ |
| 42 | #ifndef CURL_ICONV_CODESET_OF_NETWORK |
| 43 | #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1" |
| 44 | #endif |
| 45 | #ifndef CURL_ICONV_CODESET_FOR_UTF8 |
| 46 | #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8" |
| 47 | #endif |
| 48 | #define ICONV_ERROR (size_t)-1 |
| 49 | #endif /* HAVE_ICONV */ |
| 50 | |
| 51 | /* |
| 52 | * Curl_convert_clone() returns a malloced copy of the source string (if |
| 53 | * returning CURLE_OK), with the data converted to network format. |
| 54 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 55 | CURLcode Curl_convert_clone(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 56 | const char *indata, |
| 57 | size_t insize, |
| 58 | char **outbuf) |
| 59 | { |
| 60 | char *convbuf; |
| 61 | CURLcode result; |
| 62 | |
| 63 | convbuf = malloc(insize); |
| 64 | if(!convbuf) |
| 65 | return CURLE_OUT_OF_MEMORY; |
| 66 | |
| 67 | memcpy(convbuf, indata, insize); |
| 68 | result = Curl_convert_to_network(data, convbuf, insize); |
| 69 | if(result) { |
| 70 | free(convbuf); |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | *outbuf = convbuf; /* return the converted buffer */ |
| 75 | |
| 76 | return CURLE_OK; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Curl_convert_to_network() is an internal function for performing ASCII |
| 81 | * conversions on non-ASCII platforms. It convers the buffer _in place_. |
| 82 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 83 | CURLcode Curl_convert_to_network(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 84 | char *buffer, size_t length) |
| 85 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 86 | if(data && data->set.convtonetwork) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 87 | /* use translation callback */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 88 | CURLcode result; |
| 89 | Curl_set_in_callback(data, true); |
| 90 | result = data->set.convtonetwork(buffer, length); |
| 91 | Curl_set_in_callback(data, false); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 92 | if(result) { |
| 93 | failf(data, |
| 94 | "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s", |
| 95 | (int)result, curl_easy_strerror(result)); |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | } |
| 100 | else { |
| 101 | #ifdef HAVE_ICONV |
| 102 | /* do the translation ourselves */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 103 | iconv_t tmpcd = (iconv_t) -1; |
| 104 | iconv_t *cd = &tmpcd; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 105 | char *input_ptr, *output_ptr; |
| 106 | size_t in_bytes, out_bytes, rc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 107 | |
| 108 | /* open an iconv conversion descriptor if necessary */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 109 | if(data) |
| 110 | cd = &data->outbound_cd; |
| 111 | if(*cd == (iconv_t)-1) { |
| 112 | *cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK, |
| 113 | CURL_ICONV_CODESET_OF_HOST); |
| 114 | if(*cd == (iconv_t)-1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 115 | failf(data, |
| 116 | "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s", |
| 117 | CURL_ICONV_CODESET_OF_NETWORK, |
| 118 | CURL_ICONV_CODESET_OF_HOST, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 119 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 120 | return CURLE_CONV_FAILED; |
| 121 | } |
| 122 | } |
| 123 | /* call iconv */ |
| 124 | input_ptr = output_ptr = buffer; |
| 125 | in_bytes = out_bytes = length; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 126 | rc = iconv(*cd, &input_ptr, &in_bytes, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 127 | &output_ptr, &out_bytes); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 128 | if(!data) |
| 129 | iconv_close(tmpcd); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 130 | if((rc == ICONV_ERROR) || (in_bytes != 0)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 131 | failf(data, |
| 132 | "The Curl_convert_to_network iconv call failed with errno %i: %s", |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 133 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 134 | return CURLE_CONV_FAILED; |
| 135 | } |
| 136 | #else |
| 137 | failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required"); |
| 138 | return CURLE_CONV_REQD; |
| 139 | #endif /* HAVE_ICONV */ |
| 140 | } |
| 141 | |
| 142 | return CURLE_OK; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Curl_convert_from_network() is an internal function for performing ASCII |
| 147 | * conversions on non-ASCII platforms. It convers the buffer _in place_. |
| 148 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 149 | CURLcode Curl_convert_from_network(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 150 | char *buffer, size_t length) |
| 151 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 152 | if(data && data->set.convfromnetwork) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 153 | /* use translation callback */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 154 | CURLcode result; |
| 155 | Curl_set_in_callback(data, true); |
| 156 | result = data->set.convfromnetwork(buffer, length); |
| 157 | Curl_set_in_callback(data, false); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 158 | if(result) { |
| 159 | failf(data, |
| 160 | "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s", |
| 161 | (int)result, curl_easy_strerror(result)); |
| 162 | } |
| 163 | |
| 164 | return result; |
| 165 | } |
| 166 | else { |
| 167 | #ifdef HAVE_ICONV |
| 168 | /* do the translation ourselves */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 169 | iconv_t tmpcd = (iconv_t) -1; |
| 170 | iconv_t *cd = &tmpcd; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 171 | char *input_ptr, *output_ptr; |
| 172 | size_t in_bytes, out_bytes, rc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 173 | |
| 174 | /* open an iconv conversion descriptor if necessary */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 175 | if(data) |
| 176 | cd = &data->inbound_cd; |
| 177 | if(*cd == (iconv_t)-1) { |
| 178 | *cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, |
| 179 | CURL_ICONV_CODESET_OF_NETWORK); |
| 180 | if(*cd == (iconv_t)-1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 181 | failf(data, |
| 182 | "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s", |
| 183 | CURL_ICONV_CODESET_OF_HOST, |
| 184 | CURL_ICONV_CODESET_OF_NETWORK, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 185 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 186 | return CURLE_CONV_FAILED; |
| 187 | } |
| 188 | } |
| 189 | /* call iconv */ |
| 190 | input_ptr = output_ptr = buffer; |
| 191 | in_bytes = out_bytes = length; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 192 | rc = iconv(*cd, &input_ptr, &in_bytes, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 193 | &output_ptr, &out_bytes); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 194 | if(!data) |
| 195 | iconv_close(tmpcd); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 196 | if((rc == ICONV_ERROR) || (in_bytes != 0)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 197 | failf(data, |
| 198 | "Curl_convert_from_network iconv call failed with errno %i: %s", |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 199 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 200 | return CURLE_CONV_FAILED; |
| 201 | } |
| 202 | #else |
| 203 | failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required"); |
| 204 | return CURLE_CONV_REQD; |
| 205 | #endif /* HAVE_ICONV */ |
| 206 | } |
| 207 | |
| 208 | return CURLE_OK; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Curl_convert_from_utf8() is an internal function for performing UTF-8 |
| 213 | * conversions on non-ASCII platforms. |
| 214 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 215 | CURLcode Curl_convert_from_utf8(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 216 | char *buffer, size_t length) |
| 217 | { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 218 | if(data && data->set.convfromutf8) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 219 | /* use translation callback */ |
Elliott Hughes | cac3980 | 2018-04-27 16:19:43 -0700 | [diff] [blame] | 220 | CURLcode result; |
| 221 | Curl_set_in_callback(data, true); |
| 222 | result = data->set.convfromutf8(buffer, length); |
| 223 | Curl_set_in_callback(data, false); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 224 | if(result) { |
| 225 | failf(data, |
| 226 | "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s", |
| 227 | (int)result, curl_easy_strerror(result)); |
| 228 | } |
| 229 | |
| 230 | return result; |
| 231 | } |
| 232 | else { |
| 233 | #ifdef HAVE_ICONV |
| 234 | /* do the translation ourselves */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 235 | iconv_t tmpcd = (iconv_t) -1; |
| 236 | iconv_t *cd = &tmpcd; |
| 237 | char *input_ptr; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 238 | char *output_ptr; |
| 239 | size_t in_bytes, out_bytes, rc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 240 | |
| 241 | /* open an iconv conversion descriptor if necessary */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 242 | if(data) |
| 243 | cd = &data->utf8_cd; |
| 244 | if(*cd == (iconv_t)-1) { |
| 245 | *cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, |
| 246 | CURL_ICONV_CODESET_FOR_UTF8); |
| 247 | if(*cd == (iconv_t)-1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 248 | failf(data, |
| 249 | "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s", |
| 250 | CURL_ICONV_CODESET_OF_HOST, |
| 251 | CURL_ICONV_CODESET_FOR_UTF8, |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 252 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 253 | return CURLE_CONV_FAILED; |
| 254 | } |
| 255 | } |
| 256 | /* call iconv */ |
| 257 | input_ptr = output_ptr = buffer; |
| 258 | in_bytes = out_bytes = length; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 259 | rc = iconv(*cd, &input_ptr, &in_bytes, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 260 | &output_ptr, &out_bytes); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 261 | if(!data) |
| 262 | iconv_close(tmpcd); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 263 | if((rc == ICONV_ERROR) || (in_bytes != 0)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 264 | failf(data, |
| 265 | "The Curl_convert_from_utf8 iconv call failed with errno %i: %s", |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 266 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 267 | return CURLE_CONV_FAILED; |
| 268 | } |
| 269 | if(output_ptr < input_ptr) { |
| 270 | /* null terminate the now shorter output string */ |
| 271 | *output_ptr = 0x00; |
| 272 | } |
| 273 | #else |
| 274 | failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required"); |
| 275 | return CURLE_CONV_REQD; |
| 276 | #endif /* HAVE_ICONV */ |
| 277 | } |
| 278 | |
| 279 | return CURLE_OK; |
| 280 | } |
| 281 | |
| 282 | /* |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 283 | * Init conversion stuff for a Curl_easy |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 284 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 285 | void Curl_convert_init(struct Curl_easy *data) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 286 | { |
| 287 | #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV) |
| 288 | /* conversion descriptors for iconv calls */ |
| 289 | data->outbound_cd = (iconv_t)-1; |
| 290 | data->inbound_cd = (iconv_t)-1; |
| 291 | data->utf8_cd = (iconv_t)-1; |
| 292 | #else |
| 293 | (void)data; |
| 294 | #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */ |
| 295 | } |
| 296 | |
| 297 | /* |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 298 | * Setup conversion stuff for a Curl_easy |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 299 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 300 | void Curl_convert_setup(struct Curl_easy *data) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 301 | { |
| 302 | data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, |
| 303 | CURL_ICONV_CODESET_OF_NETWORK); |
| 304 | data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK, |
| 305 | CURL_ICONV_CODESET_OF_HOST); |
| 306 | data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, |
| 307 | CURL_ICONV_CODESET_FOR_UTF8); |
| 308 | } |
| 309 | |
| 310 | /* |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 311 | * Close conversion stuff for a Curl_easy |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 312 | */ |
| 313 | |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 314 | void Curl_convert_close(struct Curl_easy *data) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 315 | { |
| 316 | #ifdef HAVE_ICONV |
| 317 | /* close iconv conversion descriptors */ |
| 318 | if(data->inbound_cd != (iconv_t)-1) { |
| 319 | iconv_close(data->inbound_cd); |
| 320 | } |
| 321 | if(data->outbound_cd != (iconv_t)-1) { |
| 322 | iconv_close(data->outbound_cd); |
| 323 | } |
| 324 | if(data->utf8_cd != (iconv_t)-1) { |
| 325 | iconv_close(data->utf8_cd); |
| 326 | } |
| 327 | #else |
| 328 | (void)data; |
| 329 | #endif /* HAVE_ICONV */ |
| 330 | } |
| 331 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 332 | #endif /* CURL_DOES_CONVERSIONS */ |