iconv_impl.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "stdafx.h"
  2. #include "iconv.h"
  3. #include "iconv_impl.h"
  4. #ifdef USE_STATIC_ICONV // 使用静态库;
  5. // mt
  6. #if defined(_MT) && !defined(_DLL)
  7. #ifdef _DEBUG// mtd;
  8. #pragma comment(lib,"./lib/libiconv_mtd.lib")
  9. #else // mt;
  10. #pragma comment(lib,"./lib/libiconv_mt.lib")
  11. #endif
  12. #endif
  13. // md;
  14. #if defined(_MT) && defined(_DLL)
  15. #ifdef _DEBUG // mdd;
  16. #pragma comment(lib,"./lib/libiconv_mdd.lib")
  17. #else // md;
  18. #pragma comment(lib,"./lib/libiconv_md.lib")
  19. #endif
  20. #endif
  21. #else // 使用dll的静态连接;
  22. // mt
  23. #if defined(_MT) && !defined(_DLL)
  24. #ifdef _DEBUG// mtd;
  25. #pragma comment(lib,"./dll/libiconv_mtd.lib")
  26. #else // mt;
  27. #pragma comment(lib,"./dll/libiconv_mt.lib")
  28. #endif
  29. #endif
  30. // md;
  31. #if defined(_MT) && defined(_DLL)
  32. #ifdef _DEBUG // mdd;
  33. #pragma comment(lib,"./dll/libiconv_mdd.lib")
  34. #else // md;
  35. #pragma comment(lib,"./dll/libiconv_md.lib")
  36. #endif
  37. #endif
  38. #endif
  39. /************************************************************************/
  40. /* 函数:[1/22/2018 Home];
  41. /* 描述:;
  42. /* 参数:;
  43. /* [IN] :;
  44. /* [OUT] :;
  45. /* [IN/OUT] :;
  46. /* 返回:void;
  47. /* 注意:;
  48. /* 示例:;
  49. /*
  50. /* 修改:;
  51. /* 日期:;
  52. /* 内容:;
  53. /************************************************************************/
  54. std::string convert2(const char *from, const char* to, const char* inbuf, const size_t &inbufsize)
  55. {
  56. int iconv_ret = 0;
  57. // 打开字符集转换;
  58. iconv_t hIconv = iconv_open(to, from);
  59. if (-1 == (int)hIconv)
  60. return "";//打开失败,可能不支持的字符集
  61. size_t insize = inbufsize;
  62. std::string strout;
  63. // 循环利用;
  64. size_t outsize = 512;
  65. // 513多一位用做字符结束符;
  66. char out[513] = { 0 };
  67. while (insize > 0)
  68. {
  69. outsize = 512;
  70. memset(out, 0, outsize);
  71. // out地址保存不变,以便重复使用;
  72. char *p = out;
  73. // 开始转换;
  74. iconv_ret = iconv(hIconv, (const char**)(&inbuf), &insize, &p, &outsize);
  75. if (((iconv_ret == (size_t)-1) && (E2BIG == errno)) || (iconv_ret != (size_t)-1))
  76. {
  77. // out和p地址不同, p-out指针相减得到长度;
  78. //strout.append(out, p - out);
  79. strout.insert(strout.size(), out, p - out);
  80. }
  81. }
  82. //关闭字符集转换
  83. iconv_close(hIconv);
  84. return strout;
  85. }
  86. std::string convert(const char *from, const char* to, const char* inbuf, const size_t &inbufsize)
  87. {
  88. int iconv_ret = 0;
  89. // 打开字符集转换;
  90. iconv_t hIconv = iconv_open(to, from);
  91. if (-1 == (int)hIconv)
  92. return "";//打开失败,可能不支持的字符集
  93. int status = 0;
  94. int times = (inbufsize / 512) + 1; // 预计可转换的次数;
  95. iconv(hIconv, NULL, NULL, NULL, NULL);
  96. size_t insize = inbufsize;
  97. std::string strout;
  98. // 循环利用;
  99. size_t outsize = 512;
  100. // 513多一位用做字符结束符;
  101. char out[513] = { 0 };
  102. while (insize > 0)
  103. {
  104. outsize = 512;
  105. memset(out, 0, outsize);
  106. // out地址保存不变,以便重复使用;
  107. char *p = out;
  108. // 开始转换;
  109. //times--;
  110. iconv_ret = iconv(hIconv, (const char**)(&inbuf), &insize, &p, &outsize);
  111. if (out != p)
  112. {
  113. int saved_errno = errno;
  114. strout.insert(strout.size(), out, p - out);
  115. errno = saved_errno;
  116. }
  117. if (iconv_ret == (size_t)(-1))
  118. {
  119. if (errno == EILSEQ)
  120. {// 输入中遇到无效的多字节序列;
  121. int one = 1;
  122. // 非法序列丢弃并继续;
  123. iconvctl(hIconv, ICONV_SET_DISCARD_ILSEQ, &one);
  124. status = -3;
  125. }
  126. else if (errno == EINVAL)
  127. {// 输入中遇到了一个不完整的多字节序列;
  128. if (inbufsize == 0)
  129. {
  130. status = -4;
  131. goto done;
  132. }
  133. else
  134. {
  135. break;
  136. }
  137. }
  138. else if (errno == E2BIG)
  139. {// *outbuf没有足够的空间;
  140. status = -5;
  141. //goto done;
  142. }
  143. else {
  144. status = -6;
  145. goto done;
  146. }
  147. }
  148. /*if (((iconv_ret == (size_t)-1) && (E2BIG == errno)) || (iconv_ret != (size_t)-1))
  149. {
  150. // out和p地址不同, p-out指针相减得到长度;
  151. //strout.append(out, p - out);
  152. strout.insert(strout.size(), out, p - out);
  153. }*/
  154. }
  155. done:
  156. //关闭字符集转换
  157. iconv_close(hIconv);
  158. return strout;
  159. }
  160. /************************************************************************/
  161. /* 函数:[7/26/2016 IT];
  162. /* 描述:;
  163. /* 参数:;
  164. /* [IN] :;
  165. /* [OUT] :;
  166. /* [IN/OUT] :;
  167. /* 返回:void;
  168. /* 注意:;
  169. /* 示例:;
  170. /*
  171. /* 修改:;
  172. /* 日期:;
  173. /* 内容:;
  174. /************************************************************************/
  175. TString EnCode_UTF8URL(const TCHAR* pText)
  176. {
  177. if (pText == NULL || pText[0] == '\0')
  178. return _T("");
  179. std::string tt = "";
  180. std::string dd = "";
  181. //ASCII2UTF8(pText, tt);
  182. #ifdef UNICODE
  183. tt = convert("UCS-2LE", "UTF-8", (char*)pText, _tcslen(pText) * sizeof(TCHAR));
  184. #else
  185. tt = convert("ASCII", "UTF-8", pText, strlen(pText));
  186. #endif
  187. size_t len = tt.length();
  188. for (size_t i = 0; i < len; i++)
  189. {
  190. if (isalnum((BYTE)tt.at(i)))
  191. {
  192. char tempbuff[2] = { 0 };
  193. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  194. dd.append(tempbuff);
  195. }
  196. else if (isspace((BYTE)tt.at(i)))
  197. {
  198. dd.append("+");
  199. }
  200. else
  201. {
  202. char tempbuff[4];
  203. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  204. dd.append(tempbuff);
  205. }
  206. }
  207. #ifdef UNICODE
  208. //tt = convert("ASCII", "UCS-2LE", dd.c_str(), dd.size());
  209. //TString result;
  210. //result.append((TCHAR*)tt.c_str(), dd.size());
  211. //return result;
  212. // 将以上4行代码,简化成一行;
  213. return TString().append((TCHAR*)convert("ASCII", "UCS-2LE", dd.c_str(), dd.size()).c_str(), dd.size());
  214. #else
  215. return dd;
  216. #endif
  217. }
  218. void EnCode_UTF8URL(const TCHAR* pText, TString& strResult)
  219. {
  220. std::string tt = "";
  221. //ASCII2UTF8(pText, tt);
  222. #ifdef UNICODE
  223. std::string result = "";
  224. tt = convert("UCS-2LE", "UTF-8", (char*)pText, _tcslen(pText) * sizeof(TCHAR));
  225. #else
  226. tt = convert("ASCII", "UTF-8", pText, strlen(pText));
  227. #endif
  228. size_t len = tt.length();
  229. for (size_t i = 0; i < len; i++)
  230. {
  231. if (isalnum((BYTE)tt.at(i)))
  232. {
  233. char tempbuff[2] = { 0 };
  234. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  235. #ifdef UNICODE
  236. result.append(tempbuff);
  237. #else
  238. strResult.append(tempbuff);
  239. #endif
  240. }
  241. else if (isspace((BYTE)tt.at(i)))
  242. {
  243. #ifdef UNICODE
  244. result.append("+");
  245. #else
  246. strResult.append("+");
  247. #endif
  248. }
  249. else
  250. {
  251. char tempbuff[4];
  252. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  253. #ifdef UNICODE
  254. result.append(tempbuff);
  255. #else
  256. strResult.append(tempbuff);
  257. #endif
  258. }
  259. }
  260. #ifdef UNICODE
  261. //tt = convert("ASCII", "UCS-2LE", dd.c_str(), dd.size());
  262. //TString result;
  263. //result.append((TCHAR*)tt.c_str(), dd.size());
  264. // 将以上代码,简化成一行;
  265. strResult.append((TCHAR*)convert("ASCII", "UCS-2LE", result.c_str(), result.size()).c_str(), result.size());
  266. #endif
  267. }
  268. //做为解Url使用
  269. char CharToInt(char ch)
  270. {
  271. if (ch >= '0' && ch <= '9')return (char)(ch - '0');
  272. if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
  273. if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
  274. return -1;
  275. }
  276. char StrToBin(IN char(&str)[2])
  277. {
  278. char tempWord[2];
  279. char chn;
  280. tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
  281. tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
  282. chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
  283. return chn;
  284. }
  285. /************************************************************************/
  286. /* 函数:[7/26/2016 IT];
  287. /* 描述:;
  288. /* 参数:;
  289. /* [IN] :;
  290. /* [OUT] :;
  291. /* [IN/OUT] :;
  292. /* 返回:void;
  293. /* 注意:;
  294. /* 示例:;
  295. /*
  296. /* 修改:;
  297. /* 日期:;
  298. /* 内容:;
  299. /************************************************************************/
  300. string DeCode_URLGB2312(IN const CHAR* pURLText)
  301. {
  302. string output = "";
  303. char tmp[2];
  304. int i = 0, idx = 0, ndx, len = strlen(pURLText);
  305. while (i < len) {
  306. if (pURLText[i] == '%')
  307. {
  308. tmp[0] = pURLText[i + 1];
  309. tmp[1] = pURLText[i + 2];
  310. output += StrToBin(tmp);
  311. i = i + 3;
  312. }
  313. else if (pURLText[i] == '+')
  314. {
  315. output += ' ';
  316. i++;
  317. }
  318. else {
  319. output += pURLText[i];
  320. i++;
  321. }
  322. }
  323. return output;
  324. }
  325. void DeCode_URLGB2312(IN const CHAR* pURLText, OUT string& strResult)
  326. {
  327. char tmp[2];
  328. int i = 0, idx = 0, ndx, len = strlen(pURLText);
  329. while (i < len) {
  330. if (pURLText[i] == '%')
  331. {
  332. tmp[0] = pURLText[i + 1];
  333. tmp[1] = pURLText[i + 2];
  334. strResult += StrToBin(tmp);
  335. i = i + 3;
  336. }
  337. else if (pURLText[i] == '+')
  338. {
  339. strResult += ' ';
  340. i++;
  341. }
  342. else {
  343. strResult += pURLText[i];
  344. i++;
  345. }
  346. }
  347. }