non-ascii.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://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. #include "curl_setup.h"
  23. #ifdef CURL_DOES_CONVERSIONS
  24. #include <curl/curl.h>
  25. #include "non-ascii.h"
  26. #include "formdata.h"
  27. #include "sendf.h"
  28. #include "urldata.h"
  29. #include "curl_memory.h"
  30. /* The last #include file should be: */
  31. #include "memdebug.h"
  32. #ifdef HAVE_ICONV
  33. #include <iconv.h>
  34. /* set default codesets for iconv */
  35. #ifndef CURL_ICONV_CODESET_OF_NETWORK
  36. #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
  37. #endif
  38. #ifndef CURL_ICONV_CODESET_FOR_UTF8
  39. #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
  40. #endif
  41. #define ICONV_ERROR (size_t)-1
  42. #endif /* HAVE_ICONV */
  43. /*
  44. * Curl_convert_clone() returns a malloced copy of the source string (if
  45. * returning CURLE_OK), with the data converted to network format.
  46. */
  47. CURLcode Curl_convert_clone(struct SessionHandle *data,
  48. const char *indata,
  49. size_t insize,
  50. char **outbuf)
  51. {
  52. char *convbuf;
  53. CURLcode result;
  54. convbuf = malloc(insize);
  55. if(!convbuf)
  56. return CURLE_OUT_OF_MEMORY;
  57. memcpy(convbuf, indata, insize);
  58. result = Curl_convert_to_network(data, convbuf, insize);
  59. if(result) {
  60. free(convbuf);
  61. return result;
  62. }
  63. *outbuf = convbuf; /* return the converted buffer */
  64. return CURLE_OK;
  65. }
  66. /*
  67. * Curl_convert_to_network() is an internal function for performing ASCII
  68. * conversions on non-ASCII platforms. It convers the buffer _in place_.
  69. */
  70. CURLcode Curl_convert_to_network(struct SessionHandle *data,
  71. char *buffer, size_t length)
  72. {
  73. if(data->set.convtonetwork) {
  74. /* use translation callback */
  75. CURLcode result = data->set.convtonetwork(buffer, length);
  76. if(result) {
  77. failf(data,
  78. "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
  79. (int)result, curl_easy_strerror(result));
  80. }
  81. return result;
  82. }
  83. else {
  84. #ifdef HAVE_ICONV
  85. /* do the translation ourselves */
  86. char *input_ptr, *output_ptr;
  87. size_t in_bytes, out_bytes, rc;
  88. int error;
  89. /* open an iconv conversion descriptor if necessary */
  90. if(data->outbound_cd == (iconv_t)-1) {
  91. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  92. CURL_ICONV_CODESET_OF_HOST);
  93. if(data->outbound_cd == (iconv_t)-1) {
  94. error = ERRNO;
  95. failf(data,
  96. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  97. CURL_ICONV_CODESET_OF_NETWORK,
  98. CURL_ICONV_CODESET_OF_HOST,
  99. error, strerror(error));
  100. return CURLE_CONV_FAILED;
  101. }
  102. }
  103. /* call iconv */
  104. input_ptr = output_ptr = buffer;
  105. in_bytes = out_bytes = length;
  106. rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
  107. &output_ptr, &out_bytes);
  108. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  109. error = ERRNO;
  110. failf(data,
  111. "The Curl_convert_to_network iconv call failed with errno %i: %s",
  112. error, strerror(error));
  113. return CURLE_CONV_FAILED;
  114. }
  115. #else
  116. failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
  117. return CURLE_CONV_REQD;
  118. #endif /* HAVE_ICONV */
  119. }
  120. return CURLE_OK;
  121. }
  122. /*
  123. * Curl_convert_from_network() is an internal function for performing ASCII
  124. * conversions on non-ASCII platforms. It convers the buffer _in place_.
  125. */
  126. CURLcode Curl_convert_from_network(struct SessionHandle *data,
  127. char *buffer, size_t length)
  128. {
  129. if(data->set.convfromnetwork) {
  130. /* use translation callback */
  131. CURLcode result = data->set.convfromnetwork(buffer, length);
  132. if(result) {
  133. failf(data,
  134. "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
  135. (int)result, curl_easy_strerror(result));
  136. }
  137. return result;
  138. }
  139. else {
  140. #ifdef HAVE_ICONV
  141. /* do the translation ourselves */
  142. char *input_ptr, *output_ptr;
  143. size_t in_bytes, out_bytes, rc;
  144. int error;
  145. /* open an iconv conversion descriptor if necessary */
  146. if(data->inbound_cd == (iconv_t)-1) {
  147. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  148. CURL_ICONV_CODESET_OF_NETWORK);
  149. if(data->inbound_cd == (iconv_t)-1) {
  150. error = ERRNO;
  151. failf(data,
  152. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  153. CURL_ICONV_CODESET_OF_HOST,
  154. CURL_ICONV_CODESET_OF_NETWORK,
  155. error, strerror(error));
  156. return CURLE_CONV_FAILED;
  157. }
  158. }
  159. /* call iconv */
  160. input_ptr = output_ptr = buffer;
  161. in_bytes = out_bytes = length;
  162. rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
  163. &output_ptr, &out_bytes);
  164. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  165. error = ERRNO;
  166. failf(data,
  167. "Curl_convert_from_network iconv call failed with errno %i: %s",
  168. error, strerror(error));
  169. return CURLE_CONV_FAILED;
  170. }
  171. #else
  172. failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
  173. return CURLE_CONV_REQD;
  174. #endif /* HAVE_ICONV */
  175. }
  176. return CURLE_OK;
  177. }
  178. /*
  179. * Curl_convert_from_utf8() is an internal function for performing UTF-8
  180. * conversions on non-ASCII platforms.
  181. */
  182. CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
  183. char *buffer, size_t length)
  184. {
  185. if(data->set.convfromutf8) {
  186. /* use translation callback */
  187. CURLcode result = data->set.convfromutf8(buffer, length);
  188. if(result) {
  189. failf(data,
  190. "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
  191. (int)result, curl_easy_strerror(result));
  192. }
  193. return result;
  194. }
  195. else {
  196. #ifdef HAVE_ICONV
  197. /* do the translation ourselves */
  198. const char *input_ptr;
  199. char *output_ptr;
  200. size_t in_bytes, out_bytes, rc;
  201. int error;
  202. /* open an iconv conversion descriptor if necessary */
  203. if(data->utf8_cd == (iconv_t)-1) {
  204. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  205. CURL_ICONV_CODESET_FOR_UTF8);
  206. if(data->utf8_cd == (iconv_t)-1) {
  207. error = ERRNO;
  208. failf(data,
  209. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  210. CURL_ICONV_CODESET_OF_HOST,
  211. CURL_ICONV_CODESET_FOR_UTF8,
  212. error, strerror(error));
  213. return CURLE_CONV_FAILED;
  214. }
  215. }
  216. /* call iconv */
  217. input_ptr = output_ptr = buffer;
  218. in_bytes = out_bytes = length;
  219. rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
  220. &output_ptr, &out_bytes);
  221. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  222. error = ERRNO;
  223. failf(data,
  224. "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
  225. error, strerror(error));
  226. return CURLE_CONV_FAILED;
  227. }
  228. if(output_ptr < input_ptr) {
  229. /* null terminate the now shorter output string */
  230. *output_ptr = 0x00;
  231. }
  232. #else
  233. failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
  234. return CURLE_CONV_REQD;
  235. #endif /* HAVE_ICONV */
  236. }
  237. return CURLE_OK;
  238. }
  239. /*
  240. * Init conversion stuff for a SessionHandle
  241. */
  242. void Curl_convert_init(struct SessionHandle *data)
  243. {
  244. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  245. /* conversion descriptors for iconv calls */
  246. data->outbound_cd = (iconv_t)-1;
  247. data->inbound_cd = (iconv_t)-1;
  248. data->utf8_cd = (iconv_t)-1;
  249. #else
  250. (void)data;
  251. #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
  252. }
  253. /*
  254. * Setup conversion stuff for a SessionHandle
  255. */
  256. void Curl_convert_setup(struct SessionHandle *data)
  257. {
  258. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  259. CURL_ICONV_CODESET_OF_NETWORK);
  260. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  261. CURL_ICONV_CODESET_OF_HOST);
  262. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  263. CURL_ICONV_CODESET_FOR_UTF8);
  264. }
  265. /*
  266. * Close conversion stuff for a SessionHandle
  267. */
  268. void Curl_convert_close(struct SessionHandle *data)
  269. {
  270. #ifdef HAVE_ICONV
  271. /* close iconv conversion descriptors */
  272. if(data->inbound_cd != (iconv_t)-1) {
  273. iconv_close(data->inbound_cd);
  274. }
  275. if(data->outbound_cd != (iconv_t)-1) {
  276. iconv_close(data->outbound_cd);
  277. }
  278. if(data->utf8_cd != (iconv_t)-1) {
  279. iconv_close(data->utf8_cd);
  280. }
  281. #else
  282. (void)data;
  283. #endif /* HAVE_ICONV */
  284. }
  285. /*
  286. * Curl_convert_form() is used from http.c, this converts any form items that
  287. need to be sent in the network encoding. Returns CURLE_OK on success.
  288. */
  289. CURLcode Curl_convert_form(struct SessionHandle *data, struct FormData *form)
  290. {
  291. CURLcode result;
  292. if(!data)
  293. return CURLE_BAD_FUNCTION_ARGUMENT;
  294. while(form) {
  295. if(form->type == FORM_DATA) {
  296. result = Curl_convert_to_network(data, form->line, form->length);
  297. /* Curl_convert_to_network calls failf if unsuccessful */
  298. if(result)
  299. return result;
  300. }
  301. form = form->next;
  302. }
  303. return CURLE_OK;
  304. }
  305. #endif /* CURL_DOES_CONVERSIONS */