1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifdef CURL_DOES_CONVERSIONS
27 #include <curl/curl.h>
29 #include "non-ascii.h"
35 #include "curl_memory.h"
36 /* The last #include file should be: */
41 /* set default codesets for iconv */
42 #ifndef CURL_ICONV_CODESET_OF_NETWORK
43 #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
45 #ifndef CURL_ICONV_CODESET_FOR_UTF8
46 #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
48 #define ICONV_ERROR (size_t)-1
49 #endif /* HAVE_ICONV */
52 * Curl_convert_clone() returns a malloced copy of the source string (if
53 * returning CURLE_OK), with the data converted to network format.
55 CURLcode Curl_convert_clone(struct Curl_easy *data,
63 convbuf = malloc(insize);
65 return CURLE_OUT_OF_MEMORY;
67 memcpy(convbuf, indata, insize);
68 result = Curl_convert_to_network(data, convbuf, insize);
74 *outbuf = convbuf; /* return the converted buffer */
80 * Curl_convert_to_network() is an internal function for performing ASCII
81 * conversions on non-ASCII platforms. It convers the buffer _in place_.
83 CURLcode Curl_convert_to_network(struct Curl_easy *data,
84 char *buffer, size_t length)
86 if(data && data->set.convtonetwork) {
87 /* use translation callback */
89 Curl_set_in_callback(data, true);
90 result = data->set.convtonetwork(buffer, length);
91 Curl_set_in_callback(data, false);
94 "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
95 (int)result, curl_easy_strerror(result));
102 /* do the translation ourselves */
103 iconv_t tmpcd = (iconv_t) -1;
104 iconv_t *cd = &tmpcd;
105 char *input_ptr, *output_ptr;
106 size_t in_bytes, out_bytes, rc;
108 /* open an iconv conversion descriptor if necessary */
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) {
116 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
117 CURL_ICONV_CODESET_OF_NETWORK,
118 CURL_ICONV_CODESET_OF_HOST,
119 errno, strerror(errno));
120 return CURLE_CONV_FAILED;
124 input_ptr = output_ptr = buffer;
125 in_bytes = out_bytes = length;
126 rc = iconv(*cd, &input_ptr, &in_bytes,
127 &output_ptr, &out_bytes);
130 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
132 "The Curl_convert_to_network iconv call failed with errno %i: %s",
133 errno, strerror(errno));
134 return CURLE_CONV_FAILED;
137 failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
138 return CURLE_CONV_REQD;
139 #endif /* HAVE_ICONV */
146 * Curl_convert_from_network() is an internal function for performing ASCII
147 * conversions on non-ASCII platforms. It convers the buffer _in place_.
149 CURLcode Curl_convert_from_network(struct Curl_easy *data,
150 char *buffer, size_t length)
152 if(data && data->set.convfromnetwork) {
153 /* use translation callback */
155 Curl_set_in_callback(data, true);
156 result = data->set.convfromnetwork(buffer, length);
157 Curl_set_in_callback(data, false);
160 "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
161 (int)result, curl_easy_strerror(result));
168 /* do the translation ourselves */
169 iconv_t tmpcd = (iconv_t) -1;
170 iconv_t *cd = &tmpcd;
171 char *input_ptr, *output_ptr;
172 size_t in_bytes, out_bytes, rc;
174 /* open an iconv conversion descriptor if necessary */
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) {
182 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
183 CURL_ICONV_CODESET_OF_HOST,
184 CURL_ICONV_CODESET_OF_NETWORK,
185 errno, strerror(errno));
186 return CURLE_CONV_FAILED;
190 input_ptr = output_ptr = buffer;
191 in_bytes = out_bytes = length;
192 rc = iconv(*cd, &input_ptr, &in_bytes,
193 &output_ptr, &out_bytes);
196 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
198 "Curl_convert_from_network iconv call failed with errno %i: %s",
199 errno, strerror(errno));
200 return CURLE_CONV_FAILED;
203 failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
204 return CURLE_CONV_REQD;
205 #endif /* HAVE_ICONV */
212 * Curl_convert_from_utf8() is an internal function for performing UTF-8
213 * conversions on non-ASCII platforms.
215 CURLcode Curl_convert_from_utf8(struct Curl_easy *data,
216 char *buffer, size_t length)
218 if(data && data->set.convfromutf8) {
219 /* use translation callback */
221 Curl_set_in_callback(data, true);
222 result = data->set.convfromutf8(buffer, length);
223 Curl_set_in_callback(data, false);
226 "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
227 (int)result, curl_easy_strerror(result));
234 /* do the translation ourselves */
235 iconv_t tmpcd = (iconv_t) -1;
236 iconv_t *cd = &tmpcd;
239 size_t in_bytes, out_bytes, rc;
241 /* open an iconv conversion descriptor if necessary */
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) {
249 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
250 CURL_ICONV_CODESET_OF_HOST,
251 CURL_ICONV_CODESET_FOR_UTF8,
252 errno, strerror(errno));
253 return CURLE_CONV_FAILED;
257 input_ptr = output_ptr = buffer;
258 in_bytes = out_bytes = length;
259 rc = iconv(*cd, &input_ptr, &in_bytes,
260 &output_ptr, &out_bytes);
263 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
265 "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
266 errno, strerror(errno));
267 return CURLE_CONV_FAILED;
269 if(output_ptr < input_ptr) {
270 /* null terminate the now shorter output string */
274 failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
275 return CURLE_CONV_REQD;
276 #endif /* HAVE_ICONV */
283 * Init conversion stuff for a Curl_easy
285 void Curl_convert_init(struct Curl_easy *data)
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;
294 #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
298 * Setup conversion stuff for a Curl_easy
300 void Curl_convert_setup(struct Curl_easy *data)
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);
311 * Close conversion stuff for a Curl_easy
314 void Curl_convert_close(struct Curl_easy *data)
317 /* close iconv conversion descriptors */
318 if(data->inbound_cd != (iconv_t)-1) {
319 iconv_close(data->inbound_cd);
321 if(data->outbound_cd != (iconv_t)-1) {
322 iconv_close(data->outbound_cd);
324 if(data->utf8_cd != (iconv_t)-1) {
325 iconv_close(data->utf8_cd);
329 #endif /* HAVE_ICONV */
332 #endif /* CURL_DOES_CONVERSIONS */