utf8_to_ucs2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**************************************************************************
  2. * Copyright (C), AirM2M Tech. Co., Ltd.
  3. *
  4. * Name: utf8_to_ucs2.c
  5. * Author: liweiqiang
  6. * Version: V0.1
  7. * Date: 2013/7/19
  8. *
  9. * Description:
  10. * utf8 转换为 ucs2 le/be
  11. **************************************************************************/
  12. #include "stdio.h"
  13. #include "errno.h"
  14. #if 0
  15. /*****************************************************************************
  16. * 将一个字符的Unicode(UCS-2和UCS-4)编码转换成UTF-8编码.
  17. *
  18. * 参数:
  19. * unic 字符的Unicode编码值
  20. * pOutput 指向输出的用于存储UTF8编码值的缓冲区的指针
  21. * outsize pOutput缓冲的大小
  22. *
  23. * 返回值:
  24. * 返回转换后的字符的UTF8编码所占的字节数, 如果出错则返回 0 .
  25. *
  26. * 注意:
  27. * 1. UTF8没有字节序问题, 但是Unicode有字节序要求;
  28. * 字节序分为大端(Big Endian)和小端(Little Endian)两种;
  29. * 在Intel处理器中采用小端法表示, 在此采用小端法表示. (低地址存低位)
  30. * 2. 请保证 pOutput 缓冲区有最少有 6 字节的空间大小!
  31. ****************************************************************************/
  32. static int enc_unicode_to_utf8_one(unsigned long unic, unsigned char *pOutput,
  33. int outSize)
  34. {
  35. ASSERT(pOutput != NULL);
  36. ASSERT(outSize >= 6);
  37. if ( unic <= 0x0000007F )
  38. {
  39. // * U-00000000 - U-0000007F: 0xxxxxxx
  40. *pOutput = (unic & 0x7F);
  41. return 1;
  42. }
  43. else if ( unic >= 0x00000080 && unic <= 0x000007FF )
  44. {
  45. // * U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
  46. *(pOutput+1) = (unic & 0x3F) | 0x80;
  47. *pOutput = ((unic >> 6) & 0x1F) | 0xC0;
  48. return 2;
  49. }
  50. else if ( unic >= 0x00000800 && unic <= 0x0000FFFF )
  51. {
  52. // * U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
  53. *(pOutput+2) = (unic & 0x3F) | 0x80;
  54. *(pOutput+1) = ((unic >> 6) & 0x3F) | 0x80;
  55. *pOutput = ((unic >> 12) & 0x0F) | 0xE0;
  56. return 3;
  57. }
  58. else if ( unic >= 0x00010000 && unic <= 0x001FFFFF )
  59. {
  60. // * U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  61. *(pOutput+3) = (unic & 0x3F) | 0x80;
  62. *(pOutput+2) = ((unic >> 6) & 0x3F) | 0x80;
  63. *(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
  64. *pOutput = ((unic >> 18) & 0x07) | 0xF0;
  65. return 4;
  66. }
  67. else if ( unic >= 0x00200000 && unic <= 0x03FFFFFF )
  68. {
  69. // * U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  70. *(pOutput+4) = (unic & 0x3F) | 0x80;
  71. *(pOutput+3) = ((unic >> 6) & 0x3F) | 0x80;
  72. *(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
  73. *(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
  74. *pOutput = ((unic >> 24) & 0x03) | 0xF8;
  75. return 5;
  76. }
  77. else if ( unic >= 0x04000000 && unic <= 0x7FFFFFFF )
  78. {
  79. // * U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  80. *(pOutput+5) = (unic & 0x3F) | 0x80;
  81. *(pOutput+4) = ((unic >> 6) & 0x3F) | 0x80;
  82. *(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
  83. *(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
  84. *(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
  85. *pOutput = ((unic >> 30) & 0x01) | 0xFC;
  86. return 6;
  87. }
  88. return 0;
  89. }
  90. #endif
  91. static int enc_get_utf8_size(char c)
  92. {
  93. int count = 0;
  94. while (c & (1<<7))
  95. {
  96. c = c << 1;
  97. count++;
  98. }
  99. return count == 0 ? 1 : count;
  100. }
  101. /*****************************************************************************
  102. * 将一个字符的UTF8编码转换成Unicode(UCS-2和UCS-4)编码.
  103. *
  104. * 参数:
  105. * pInput 指向输入缓冲区, 以UTF-8编码
  106. * pOutput 指向输出缓冲区, 其保存的数据即是Unicode编码值,
  107. * endian 1 - big endian
  108. * 0 - little endian
  109. *
  110. * 返回值:
  111. * 成功则返回该字符的UTF8编码所占用的字节数; 失败则返回0.
  112. *
  113. * 注意:
  114. * 1. UTF8没有字节序问题, 但是Unicode有字节序要求;
  115. * 字节序分为大端(Big Endian)和小端(Little Endian)两种;
  116. * 在Intel处理器中采用小端法表示, 在此采用小端法表示. (低地址存低位)
  117. ****************************************************************************/
  118. static int enc_utf8_to_unicode_one(const char* pInput, char* pOutput, int endian)
  119. {
  120. char b1, b2, b3/*, b4, b5, b6*/;
  121. int utfbytes = enc_get_utf8_size(*pInput);
  122. switch ( utfbytes )
  123. {
  124. case 1:
  125. if(endian)
  126. {
  127. *pOutput++ = 0x00;
  128. *pOutput = *pInput;
  129. }
  130. else
  131. {
  132. *pOutput++ = *pInput;
  133. *pOutput = 0x00;
  134. }
  135. return 2;
  136. break;
  137. case 2:
  138. b1 = *pInput;
  139. b2 = *(pInput + 1);
  140. if ( (b2 & 0xE0) != 0x80 )
  141. return -1;
  142. if(endian)
  143. {
  144. *pOutput++ = (b1 >> 2) & 0x07;
  145. *pOutput = (b1 << 6) + (b2 & 0x3F);
  146. }
  147. else
  148. {
  149. *pOutput++ = (b1 << 6) + (b2 & 0x3F);
  150. *pOutput = (b1 >> 2) & 0x07;
  151. }
  152. return 2;
  153. break;
  154. case 3:
  155. b1 = *pInput;
  156. b2 = *(pInput + 1);
  157. b3 = *(pInput + 2);
  158. if ( ((b2 & 0xC0) != 0x80) || ((b3 & 0xC0) != 0x80) )
  159. return -1;
  160. if(endian)
  161. {
  162. *pOutput++ = (b1 << 4) + ((b2 >> 2) & 0x0F);
  163. *pOutput = (b2 << 6) + (b3 & 0x3F);
  164. }
  165. else
  166. {
  167. *pOutput++ = (b2 << 6) + (b3 & 0x3F);
  168. *pOutput = (b1 << 4) + ((b2 >> 2) & 0x0F);
  169. }
  170. return 2;
  171. break;
  172. #if 0
  173. case 4:
  174. b1 = *pInput;
  175. b2 = *(pInput + 1);
  176. b3 = *(pInput + 2);
  177. b4 = *(pInput + 3);
  178. if ( ((b2 & 0xC0) != 0x80) || ((b3 & 0xC0) != 0x80)
  179. || ((b4 & 0xC0) != 0x80) )
  180. return -1;
  181. *pOutput = (b3 << 6) + (b4 & 0x3F);
  182. *(pOutput+1) = (b2 << 4) + ((b3 >> 2) & 0x0F);
  183. *(pOutput+2) = ((b1 << 2) & 0x1C) + ((b2 >> 4) & 0x03);
  184. return 3;
  185. break;
  186. case 5:
  187. b1 = *pInput;
  188. b2 = *(pInput + 1);
  189. b3 = *(pInput + 2);
  190. b4 = *(pInput + 3);
  191. b5 = *(pInput + 4);
  192. if ( ((b2 & 0xC0) != 0x80) || ((b3 & 0xC0) != 0x80)
  193. || ((b4 & 0xC0) != 0x80) || ((b5 & 0xC0) != 0x80) )
  194. return -1;
  195. *pOutput = (b4 << 6) + (b5 & 0x3F);
  196. *(pOutput+1) = (b3 << 4) + ((b4 >> 2) & 0x0F);
  197. *(pOutput+2) = (b2 << 2) + ((b3 >> 4) & 0x03);
  198. *(pOutput+3) = (b1 << 6);
  199. return 4;
  200. break;
  201. case 6:
  202. b1 = *pInput;
  203. b2 = *(pInput + 1);
  204. b3 = *(pInput + 2);
  205. b4 = *(pInput + 3);
  206. b5 = *(pInput + 4);
  207. b6 = *(pInput + 5);
  208. if ( ((b2 & 0xC0) != 0x80) || ((b3 & 0xC0) != 0x80)
  209. || ((b4 & 0xC0) != 0x80) || ((b5 & 0xC0) != 0x80)
  210. || ((b6 & 0xC0) != 0x80) )
  211. return -1;
  212. *pOutput = (b5 << 6) + (b6 & 0x3F);
  213. *(pOutput+1) = (b5 << 4) + ((b6 >> 2) & 0x0F);
  214. *(pOutput+2) = (b3 << 2) + ((b4 >> 4) & 0x03);
  215. *(pOutput+3) = ((b1 << 6) & 0x40) + (b2 & 0x3F);
  216. return 4;
  217. break;
  218. #endif
  219. default:
  220. break;
  221. }
  222. return -1;
  223. }
  224. static size_t enc_utf8_to_unicode(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft, int endian)
  225. {
  226. size_t maxOLen = *outbytesleft;
  227. size_t iLen = *inbytesleft;
  228. char *src = *_inbuf;
  229. char *dst = *_outbuf;
  230. size_t iPos, oPos;
  231. size_t utfbytes = 0, unicodeBytes = 0;
  232. int result = 0;
  233. for(iPos = 0, oPos = 0; iPos < iLen; )
  234. {
  235. if(oPos + 2 > maxOLen)
  236. {
  237. errno = E2BIG;
  238. result = -1;
  239. goto utf8_to_ucs2_exit;
  240. }
  241. utfbytes = enc_get_utf8_size(src[iPos]);
  242. if(utfbytes == 0)
  243. utfbytes = 1;
  244. if((unicodeBytes = enc_utf8_to_unicode_one(&src[iPos], &dst[oPos], endian)) == -1)
  245. {
  246. errno = EINVAL;
  247. result = -1;
  248. break;
  249. }
  250. oPos += unicodeBytes;
  251. iPos += utfbytes;
  252. }
  253. utf8_to_ucs2_exit:
  254. *inbytesleft -= iPos;
  255. *_inbuf += iPos;
  256. *outbytesleft -= oPos;
  257. return (size_t)result;
  258. }
  259. size_t iconv_utf8_to_ucs2(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft)
  260. {
  261. return enc_utf8_to_unicode(_inbuf, inbytesleft, _outbuf, outbytesleft, 0);
  262. }
  263. size_t iconv_utf8_to_ucs2be(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft)
  264. {
  265. return enc_utf8_to_unicode(_inbuf, inbytesleft, _outbuf, outbytesleft, 1);
  266. }
  267. static size_t enc_unicode_to_utf8(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft, int endian)
  268. {
  269. size_t maxOLen = *outbytesleft;
  270. size_t iLen = *inbytesleft;
  271. char *src = *_inbuf;
  272. char *dst = *_outbuf;
  273. size_t iPos, oPos;
  274. size_t utfbytes = 0;
  275. int result = 0;
  276. size_t unicodeVal = 0;
  277. for(iPos = 0, oPos = 0; iPos+1 < iLen;)
  278. {
  279. unicodeVal = ((endian==1) ? ((unsigned char)src[iPos]*256+(unsigned char)src[iPos+1]) : ((unsigned char)src[iPos+1]*256+(unsigned char)src[iPos]));
  280. //printf("test1:%d,%X,%X,%X\n",endian,unicodeVal,src[iPos],src[iPos+1]);
  281. if(unicodeVal <= 0x7F)
  282. {
  283. utfbytes = 1;
  284. }
  285. else if(unicodeVal > 0x7F && unicodeVal <= 0x07FF)
  286. {
  287. utfbytes = 2;
  288. }
  289. else if(unicodeVal > 0x07FF)
  290. {
  291. utfbytes = 3;
  292. }
  293. else
  294. {
  295. errno = EINVAL;
  296. result = -1;
  297. goto ucs2_to_utf8_exit;
  298. }
  299. if(oPos + utfbytes > maxOLen)
  300. {
  301. errno = E2BIG;
  302. result = -1;
  303. goto ucs2_to_utf8_exit;
  304. }
  305. switch ( utfbytes )
  306. {
  307. case 1:
  308. dst[oPos] = unicodeVal;
  309. break;
  310. case 2:
  311. dst[oPos] = ((unicodeVal>>6)|0xE0)&0xDF;
  312. dst[oPos+1] = ((char)(unicodeVal&0xFF)|0xC0)&0xBF;
  313. break;
  314. case 3:
  315. dst[oPos] = (((unicodeVal>>12)&0xFF)|0xF0)&0xEF;
  316. dst[oPos+1] = (((unicodeVal>>6)&0xFF)|0xC0)&0xBF;
  317. //printf("test:%X,%X,%X,%X,%X,",unicodeVal,unicodeVal>>6,((unicodeVal>>6)&0xFF),((unicodeVal>>6)&0xFF)|0xC0,dst[oPos+1]);
  318. dst[oPos+2] =((unicodeVal&0xFF)|0xC0)&0xBF;
  319. break;
  320. default:
  321. break;
  322. }
  323. iPos += 2;
  324. oPos += utfbytes;
  325. }
  326. ucs2_to_utf8_exit:
  327. *inbytesleft -= iPos;
  328. *_inbuf += iPos;
  329. *outbytesleft -= oPos;
  330. return (size_t)result;
  331. }
  332. size_t iconv_ucs2_to_utf8(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft)
  333. {
  334. return enc_unicode_to_utf8(_inbuf, inbytesleft, _outbuf, outbytesleft, 0);
  335. }
  336. size_t iconv_ucs2be_to_utf8(char **_inbuf, size_t *inbytesleft, char **_outbuf, size_t *outbytesleft)
  337. {
  338. return enc_unicode_to_utf8(_inbuf, inbytesleft, _outbuf, outbytesleft, 1);
  339. }