1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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"
34 #include "curl_memory.h"
35 /* The last #include file should be: */
40 /* set default codesets for iconv */
41 #ifndef CURL_ICONV_CODESET_OF_NETWORK
42 #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
44 #ifndef CURL_ICONV_CODESET_FOR_UTF8
45 #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
47 #define ICONV_ERROR (size_t)-1
48 #endif /* HAVE_ICONV */
51 * Curl_convert_clone() returns a malloced copy of the source string (if
52 * returning CURLE_OK), with the data converted to network format.
54 CURLcode Curl_convert_clone(struct SessionHandle *data,
62 convbuf = malloc(insize);
64 return CURLE_OUT_OF_MEMORY;
66 memcpy(convbuf, indata, insize);
67 result = Curl_convert_to_network(data, convbuf, insize);
73 *outbuf = convbuf; /* return the converted buffer */
79 * Curl_convert_to_network() is an internal function for performing ASCII
80 * conversions on non-ASCII platforms. It convers the buffer _in place_.
82 CURLcode Curl_convert_to_network(struct SessionHandle *data,
83 char *buffer, size_t length)
85 if(data->set.convtonetwork) {
86 /* use translation callback */
87 CURLcode result = data->set.convtonetwork(buffer, length);
90 "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
91 (int)result, curl_easy_strerror(result));
98 /* do the translation ourselves */
99 char *input_ptr, *output_ptr;
100 size_t in_bytes, out_bytes, rc;
103 /* open an iconv conversion descriptor if necessary */
104 if(data->outbound_cd == (iconv_t)-1) {
105 data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
106 CURL_ICONV_CODESET_OF_HOST);
107 if(data->outbound_cd == (iconv_t)-1) {
110 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
111 CURL_ICONV_CODESET_OF_NETWORK,
112 CURL_ICONV_CODESET_OF_HOST,
113 error, strerror(error));
114 return CURLE_CONV_FAILED;
118 input_ptr = output_ptr = buffer;
119 in_bytes = out_bytes = length;
120 rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
121 &output_ptr, &out_bytes);
122 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
125 "The Curl_convert_to_network iconv call failed with errno %i: %s",
126 error, strerror(error));
127 return CURLE_CONV_FAILED;
130 failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
131 return CURLE_CONV_REQD;
132 #endif /* HAVE_ICONV */
139 * Curl_convert_from_network() is an internal function for performing ASCII
140 * conversions on non-ASCII platforms. It convers the buffer _in place_.
142 CURLcode Curl_convert_from_network(struct SessionHandle *data,
143 char *buffer, size_t length)
145 if(data->set.convfromnetwork) {
146 /* use translation callback */
147 CURLcode result = data->set.convfromnetwork(buffer, length);
150 "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
151 (int)result, curl_easy_strerror(result));
158 /* do the translation ourselves */
159 char *input_ptr, *output_ptr;
160 size_t in_bytes, out_bytes, rc;
163 /* open an iconv conversion descriptor if necessary */
164 if(data->inbound_cd == (iconv_t)-1) {
165 data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
166 CURL_ICONV_CODESET_OF_NETWORK);
167 if(data->inbound_cd == (iconv_t)-1) {
170 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
171 CURL_ICONV_CODESET_OF_HOST,
172 CURL_ICONV_CODESET_OF_NETWORK,
173 error, strerror(error));
174 return CURLE_CONV_FAILED;
178 input_ptr = output_ptr = buffer;
179 in_bytes = out_bytes = length;
180 rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
181 &output_ptr, &out_bytes);
182 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
185 "Curl_convert_from_network iconv call failed with errno %i: %s",
186 error, strerror(error));
187 return CURLE_CONV_FAILED;
190 failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
191 return CURLE_CONV_REQD;
192 #endif /* HAVE_ICONV */
199 * Curl_convert_from_utf8() is an internal function for performing UTF-8
200 * conversions on non-ASCII platforms.
202 CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
203 char *buffer, size_t length)
205 if(data->set.convfromutf8) {
206 /* use translation callback */
207 CURLcode result = data->set.convfromutf8(buffer, length);
210 "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
211 (int)result, curl_easy_strerror(result));
218 /* do the translation ourselves */
219 const char *input_ptr;
221 size_t in_bytes, out_bytes, rc;
224 /* open an iconv conversion descriptor if necessary */
225 if(data->utf8_cd == (iconv_t)-1) {
226 data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
227 CURL_ICONV_CODESET_FOR_UTF8);
228 if(data->utf8_cd == (iconv_t)-1) {
231 "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
232 CURL_ICONV_CODESET_OF_HOST,
233 CURL_ICONV_CODESET_FOR_UTF8,
234 error, strerror(error));
235 return CURLE_CONV_FAILED;
239 input_ptr = output_ptr = buffer;
240 in_bytes = out_bytes = length;
241 rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
242 &output_ptr, &out_bytes);
243 if((rc == ICONV_ERROR) || (in_bytes != 0)) {
246 "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
247 error, strerror(error));
248 return CURLE_CONV_FAILED;
250 if(output_ptr < input_ptr) {
251 /* null terminate the now shorter output string */
255 failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
256 return CURLE_CONV_REQD;
257 #endif /* HAVE_ICONV */
264 * Init conversion stuff for a SessionHandle
266 void Curl_convert_init(struct SessionHandle *data)
268 #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
269 /* conversion descriptors for iconv calls */
270 data->outbound_cd = (iconv_t)-1;
271 data->inbound_cd = (iconv_t)-1;
272 data->utf8_cd = (iconv_t)-1;
275 #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
279 * Setup conversion stuff for a SessionHandle
281 void Curl_convert_setup(struct SessionHandle *data)
283 data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
284 CURL_ICONV_CODESET_OF_NETWORK);
285 data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
286 CURL_ICONV_CODESET_OF_HOST);
287 data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
288 CURL_ICONV_CODESET_FOR_UTF8);
292 * Close conversion stuff for a SessionHandle
295 void Curl_convert_close(struct SessionHandle *data)
298 /* close iconv conversion descriptors */
299 if(data->inbound_cd != (iconv_t)-1) {
300 iconv_close(data->inbound_cd);
302 if(data->outbound_cd != (iconv_t)-1) {
303 iconv_close(data->outbound_cd);
305 if(data->utf8_cd != (iconv_t)-1) {
306 iconv_close(data->utf8_cd);
310 #endif /* HAVE_ICONV */
314 * Curl_convert_form() is used from http.c, this converts any form items that
315 need to be sent in the network encoding. Returns CURLE_OK on success.
317 CURLcode Curl_convert_form(struct SessionHandle *data, struct FormData *form)
322 return CURLE_BAD_FUNCTION_ARGUMENT;
325 if(form->type == FORM_DATA) {
326 result = Curl_convert_to_network(data, form->line, form->length);
327 /* Curl_convert_to_network calls failf if unsuccessful */
338 #endif /* CURL_DOES_CONVERSIONS */