non-ascii.c 9.3 KB

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