non-ascii.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 https://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 Curl_easy *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 Curl_easy *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. /* open an iconv conversion descriptor if necessary */
  89. if(data->outbound_cd == (iconv_t)-1) {
  90. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  91. CURL_ICONV_CODESET_OF_HOST);
  92. if(data->outbound_cd == (iconv_t)-1) {
  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. errno, strerror(errno));
  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. failf(data,
  108. "The Curl_convert_to_network iconv call failed with errno %i: %s",
  109. errno, strerror(errno));
  110. return CURLE_CONV_FAILED;
  111. }
  112. #else
  113. failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
  114. return CURLE_CONV_REQD;
  115. #endif /* HAVE_ICONV */
  116. }
  117. return CURLE_OK;
  118. }
  119. /*
  120. * Curl_convert_from_network() is an internal function for performing ASCII
  121. * conversions on non-ASCII platforms. It convers the buffer _in place_.
  122. */
  123. CURLcode Curl_convert_from_network(struct Curl_easy *data,
  124. char *buffer, size_t length)
  125. {
  126. if(data->set.convfromnetwork) {
  127. /* use translation callback */
  128. CURLcode result = data->set.convfromnetwork(buffer, length);
  129. if(result) {
  130. failf(data,
  131. "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
  132. (int)result, curl_easy_strerror(result));
  133. }
  134. return result;
  135. }
  136. else {
  137. #ifdef HAVE_ICONV
  138. /* do the translation ourselves */
  139. char *input_ptr, *output_ptr;
  140. size_t in_bytes, out_bytes, rc;
  141. /* open an iconv conversion descriptor if necessary */
  142. if(data->inbound_cd == (iconv_t)-1) {
  143. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  144. CURL_ICONV_CODESET_OF_NETWORK);
  145. if(data->inbound_cd == (iconv_t)-1) {
  146. failf(data,
  147. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  148. CURL_ICONV_CODESET_OF_HOST,
  149. CURL_ICONV_CODESET_OF_NETWORK,
  150. errno, strerror(errno));
  151. return CURLE_CONV_FAILED;
  152. }
  153. }
  154. /* call iconv */
  155. input_ptr = output_ptr = buffer;
  156. in_bytes = out_bytes = length;
  157. rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
  158. &output_ptr, &out_bytes);
  159. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  160. failf(data,
  161. "Curl_convert_from_network iconv call failed with errno %i: %s",
  162. errno, strerror(errno));
  163. return CURLE_CONV_FAILED;
  164. }
  165. #else
  166. failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
  167. return CURLE_CONV_REQD;
  168. #endif /* HAVE_ICONV */
  169. }
  170. return CURLE_OK;
  171. }
  172. /*
  173. * Curl_convert_from_utf8() is an internal function for performing UTF-8
  174. * conversions on non-ASCII platforms.
  175. */
  176. CURLcode Curl_convert_from_utf8(struct Curl_easy *data,
  177. char *buffer, size_t length)
  178. {
  179. if(data->set.convfromutf8) {
  180. /* use translation callback */
  181. CURLcode result = data->set.convfromutf8(buffer, length);
  182. if(result) {
  183. failf(data,
  184. "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
  185. (int)result, curl_easy_strerror(result));
  186. }
  187. return result;
  188. }
  189. else {
  190. #ifdef HAVE_ICONV
  191. /* do the translation ourselves */
  192. const char *input_ptr;
  193. char *output_ptr;
  194. size_t in_bytes, out_bytes, rc;
  195. /* open an iconv conversion descriptor if necessary */
  196. if(data->utf8_cd == (iconv_t)-1) {
  197. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  198. CURL_ICONV_CODESET_FOR_UTF8);
  199. if(data->utf8_cd == (iconv_t)-1) {
  200. failf(data,
  201. "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
  202. CURL_ICONV_CODESET_OF_HOST,
  203. CURL_ICONV_CODESET_FOR_UTF8,
  204. errno, strerror(errno));
  205. return CURLE_CONV_FAILED;
  206. }
  207. }
  208. /* call iconv */
  209. input_ptr = output_ptr = buffer;
  210. in_bytes = out_bytes = length;
  211. rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
  212. &output_ptr, &out_bytes);
  213. if((rc == ICONV_ERROR) || (in_bytes != 0)) {
  214. failf(data,
  215. "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
  216. errno, strerror(errno));
  217. return CURLE_CONV_FAILED;
  218. }
  219. if(output_ptr < input_ptr) {
  220. /* null terminate the now shorter output string */
  221. *output_ptr = 0x00;
  222. }
  223. #else
  224. failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
  225. return CURLE_CONV_REQD;
  226. #endif /* HAVE_ICONV */
  227. }
  228. return CURLE_OK;
  229. }
  230. /*
  231. * Init conversion stuff for a Curl_easy
  232. */
  233. void Curl_convert_init(struct Curl_easy *data)
  234. {
  235. #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  236. /* conversion descriptors for iconv calls */
  237. data->outbound_cd = (iconv_t)-1;
  238. data->inbound_cd = (iconv_t)-1;
  239. data->utf8_cd = (iconv_t)-1;
  240. #else
  241. (void)data;
  242. #endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
  243. }
  244. /*
  245. * Setup conversion stuff for a Curl_easy
  246. */
  247. void Curl_convert_setup(struct Curl_easy *data)
  248. {
  249. data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  250. CURL_ICONV_CODESET_OF_NETWORK);
  251. data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
  252. CURL_ICONV_CODESET_OF_HOST);
  253. data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
  254. CURL_ICONV_CODESET_FOR_UTF8);
  255. }
  256. /*
  257. * Close conversion stuff for a Curl_easy
  258. */
  259. void Curl_convert_close(struct Curl_easy *data)
  260. {
  261. #ifdef HAVE_ICONV
  262. /* close iconv conversion descriptors */
  263. if(data->inbound_cd != (iconv_t)-1) {
  264. iconv_close(data->inbound_cd);
  265. }
  266. if(data->outbound_cd != (iconv_t)-1) {
  267. iconv_close(data->outbound_cd);
  268. }
  269. if(data->utf8_cd != (iconv_t)-1) {
  270. iconv_close(data->utf8_cd);
  271. }
  272. #else
  273. (void)data;
  274. #endif /* HAVE_ICONV */
  275. }
  276. /*
  277. * Curl_convert_form() is used from http.c, this converts any form items that
  278. need to be sent in the network encoding. Returns CURLE_OK on success.
  279. */
  280. CURLcode Curl_convert_form(struct Curl_easy *data, struct FormData *form)
  281. {
  282. CURLcode result;
  283. if(!data)
  284. return CURLE_BAD_FUNCTION_ARGUMENT;
  285. while(form) {
  286. if(form->type == FORM_DATA) {
  287. result = Curl_convert_to_network(data, form->line, form->length);
  288. /* Curl_convert_to_network calls failf if unsuccessful */
  289. if(result)
  290. return result;
  291. }
  292. form = form->next;
  293. }
  294. return CURLE_OK;
  295. }
  296. #endif /* CURL_DOES_CONVERSIONS */