non-ascii.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 "setup.h"
  23. #ifdef CURL_DOES_CONVERSIONS
  24. #include "non-ascii.h"
  25. #include "formdata.h"
  26. #include "sendf.h"
  27. #include "urldata.h"
  28. #include <curl/curl.h>
  29. #ifdef HAVE_ICONV
  30. #include <iconv.h>
  31. /* set default codesets for iconv */
  32. #ifndef CURL_ICONV_CODESET_OF_NETWORK
  33. #define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
  34. #endif
  35. #ifndef CURL_ICONV_CODESET_FOR_UTF8
  36. #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8"
  37. #endif
  38. #define ICONV_ERROR (size_t)-1
  39. #endif /* HAVE_ICONV */
  40. /*
  41. * Curl_convert_clone() returns a malloced copy of the source string (if
  42. * returning CURLE_OK), with the data converted to network format.
  43. */
  44. CURLcode Curl_convert_clone(struct SessionHandle *data,
  45. const char *indata,
  46. size_t insize,
  47. char **outbuf)
  48. {
  49. char *convbuf;
  50. CURLcode result;
  51. convbuf = malloc(insize);
  52. if(!convbuf)
  53. return CURLE_OUT_OF_MEMORY;
  54. memcpy(convbuf, indata, insize);
  55. result = Curl_convert_to_network(data, convbuf, insize);
  56. if(result) {
  57. free(convbuf);
  58. return result;
  59. }
  60. *outbuf = convbuf; /* return the converted buffer */
  61. return CURLE_OK;
  62. }
  63. /*
  64. * Curl_convert_to_network() is an internal function for performing ASCII
  65. * conversions on non-ASCII platforms. It convers the buffer _in place_.
  66. */
  67. CURLcode Curl_convert_to_network(struct SessionHandle *data,
  68. char *buffer, size_t length)
  69. {
  70. CURLcode rc;
  71. if(data->set.convtonetwork) {
  72. /* use translation callback */
  73. rc = data->set.convtonetwork(buffer, length);
  74. if(rc != CURLE_OK) {
  75. failf(data,
  76. "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
  77. (int)rc, curl_easy_strerror(rc));
  78. }
  79. return rc;
  80. }
  81. else {
  82. #ifdef HAVE_ICONV
  83. /* do the translation ourselves */
  84. char *input_ptr, *output_ptr;
  85. size_t in_bytes, out_bytes, rc;
  86. int error;
  87. /* open an iconv conversion descriptor if necessary */
  88. if(data->outbound_cd == (iconv_t)-1) {
  89. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  90. CURL_ICONV_CODESET_OF_HOST);
  91. if(data->outbound_cd == (iconv_t)-1) {
  92. error = ERRNO;
  93. failf(data,
  94. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  95. CURL_ICONV_CODESET_OF_NETWORK,
  96. CURL_ICONV_CODESET_OF_HOST,
  97. error, strerror(error));
  98. return CURLE_CONV_FAILED;
  99. }
  100. }
  101. /* call iconv */
  102. input_ptr = output_ptr = buffer;
  103. in_bytes = out_bytes = length;
  104. rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
  105. &output_ptr, &out_bytes);
  106. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  107. error = ERRNO;
  108. failf(data,
  109. "The Curl_convert_to_network iconv call failed with errno %i: %s",
  110. error, strerror(error));
  111. return CURLE_CONV_FAILED;
  112. }
  113. #else
  114. failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
  115. return CURLE_CONV_REQD;
  116. #endif /* HAVE_ICONV */
  117. }
  118. return CURLE_OK;
  119. }
  120. /*
  121. * Curl_convert_from_network() is an internal function for performing ASCII
  122. * conversions on non-ASCII platforms. It convers the buffer _in place_.
  123. */
  124. CURLcode Curl_convert_from_network(struct SessionHandle *data,
  125. char *buffer, size_t length)
  126. {
  127. CURLcode rc;
  128. if(data->set.convfromnetwork) {
  129. /* use translation callback */
  130. rc = data->set.convfromnetwork(buffer, length);
  131. if(rc != CURLE_OK) {
  132. failf(data,
  133. "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
  134. (int)rc, curl_easy_strerror(rc));
  135. }
  136. return rc;
  137. }
  138. else {
  139. #ifdef HAVE_ICONV
  140. /* do the translation ourselves */
  141. char *input_ptr, *output_ptr;
  142. size_t in_bytes, out_bytes, rc;
  143. int error;
  144. /* open an iconv conversion descriptor if necessary */
  145. if(data->inbound_cd == (iconv_t)-1) {
  146. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  147. CURL_ICONV_CODESET_OF_NETWORK);
  148. if(data->inbound_cd == (iconv_t)-1) {
  149. error = ERRNO;
  150. failf(data,
  151. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  152. CURL_ICONV_CODESET_OF_HOST,
  153. CURL_ICONV_CODESET_OF_NETWORK,
  154. error, strerror(error));
  155. return CURLE_CONV_FAILED;
  156. }
  157. }
  158. /* call iconv */
  159. input_ptr = output_ptr = buffer;
  160. in_bytes = out_bytes = length;
  161. rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
  162. &output_ptr, &out_bytes);
  163. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  164. error = ERRNO;
  165. failf(data,
  166. "Curl_convert_from_network iconv call failed with errno %i: %s",
  167. error, strerror(error));
  168. return CURLE_CONV_FAILED;
  169. }
  170. #else
  171. failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
  172. return CURLE_CONV_REQD;
  173. #endif /* HAVE_ICONV */
  174. }
  175. return CURLE_OK;
  176. }
  177. /*
  178. * Curl_convert_from_utf8() is an internal function for performing UTF-8
  179. * conversions on non-ASCII platforms.
  180. */
  181. CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
  182. char *buffer, size_t length)
  183. {
  184. CURLcode rc;
  185. if(data->set.convfromutf8) {
  186. /* use translation callback */
  187. rc = data->set.convfromutf8(buffer, length);
  188. if(rc != CURLE_OK) {
  189. failf(data,
  190. "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
  191. (int)rc, curl_easy_strerror(rc));
  192. }
  193. return rc;
  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. struct FormData *next;
  292. CURLcode rc;
  293. if(!form)
  294. return CURLE_OK;
  295. if(!data)
  296. return CURLE_BAD_FUNCTION_ARGUMENT;
  297. do {
  298. next=form->next; /* the following form line */
  299. if(form->type == FORM_DATA) {
  300. rc = Curl_convert_to_network(data, form->line, form->length);
  301. /* Curl_convert_to_network calls failf if unsuccessful */
  302. if(rc != CURLE_OK)
  303. return rc;
  304. }
  305. } while((form = next) != NULL); /* continue */
  306. return CURLE_OK;
  307. }
  308. #endif /* CURL_DOES_CONVERSIONS */