1
0

utf8.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2007 Alexey Vatchenko <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <wchar.h>
  17. #include "utf8.h"
  18. #ifdef _WIN32
  19. #include <windows.h>
  20. #include "c99defs.h"
  21. static inline bool has_utf8_bom(const char *in_char)
  22. {
  23. uint8_t *in = (uint8_t *)in_char;
  24. return (in && in[0] == 0xef && in[1] == 0xbb && in[2] == 0xbf);
  25. }
  26. size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out, size_t outsize, int flags)
  27. {
  28. int i_insize = (int)insize;
  29. int ret;
  30. if (i_insize == 0)
  31. i_insize = (int)strlen(in);
  32. /* prevent bom from being used in the string */
  33. if (has_utf8_bom(in)) {
  34. if (i_insize >= 3) {
  35. in += 3;
  36. i_insize -= 3;
  37. }
  38. }
  39. ret = MultiByteToWideChar(CP_UTF8, 0, in, i_insize, out, (int)outsize);
  40. UNUSED_PARAMETER(flags);
  41. return (ret > 0) ? (size_t)ret : 0;
  42. }
  43. size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags)
  44. {
  45. int i_insize = (int)insize;
  46. int ret;
  47. if (i_insize == 0)
  48. i_insize = (int)wcslen(in);
  49. ret = WideCharToMultiByte(CP_UTF8, 0, in, i_insize, out, (int)outsize, NULL, NULL);
  50. UNUSED_PARAMETER(flags);
  51. return (ret > 0) ? (size_t)ret : 0;
  52. }
  53. #else
  54. #define _NXT 0x80
  55. #define _SEQ2 0xc0
  56. #define _SEQ3 0xe0
  57. #define _SEQ4 0xf0
  58. #define _SEQ5 0xf8
  59. #define _SEQ6 0xfc
  60. #define _BOM 0xfeff
  61. static int wchar_forbidden(wchar_t sym);
  62. static int utf8_forbidden(unsigned char octet);
  63. static int wchar_forbidden(wchar_t sym)
  64. {
  65. /* Surrogate pairs */
  66. if (sym >= 0xd800 && sym <= 0xdfff)
  67. return -1;
  68. return 0;
  69. }
  70. static int utf8_forbidden(unsigned char octet)
  71. {
  72. switch (octet) {
  73. case 0xc0:
  74. case 0xc1:
  75. case 0xf5:
  76. case 0xff:
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. /*
  82. * DESCRIPTION
  83. * This function translates UTF-8 string into UCS-4 string (all symbols
  84. * will be in local machine byte order).
  85. *
  86. * It takes the following arguments:
  87. * in - input UTF-8 string. It can be null-terminated.
  88. * insize - size of input string in bytes. If insize is 0,
  89. * function continues until a null terminator is reached.
  90. * out - result buffer for UCS-4 string. If out is NULL,
  91. * function returns size of result buffer.
  92. * outsize - size of out buffer in wide characters.
  93. *
  94. * RETURN VALUES
  95. * The function returns size of result buffer (in wide characters).
  96. * Zero is returned in case of error.
  97. *
  98. * CAVEATS
  99. * 1. If UTF-8 string contains zero symbols, they will be translated
  100. * as regular symbols.
  101. * 2. If UTF8_IGNORE_ERROR or UTF8_SKIP_BOM flag is set, sizes may vary
  102. * when `out' is NULL and not NULL. It's because of special UTF-8
  103. * sequences which may result in forbidden (by RFC3629) UNICODE
  104. * characters. So, the caller must check return value every time and
  105. * not prepare buffer in advance (\0 terminate) but after calling this
  106. * function.
  107. */
  108. size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out, size_t outsize, int flags)
  109. {
  110. unsigned char *p, *lim;
  111. wchar_t *wlim, high;
  112. size_t n, total, i, n_bits;
  113. if (in == NULL || (outsize == 0 && out != NULL))
  114. return 0;
  115. total = 0;
  116. p = (unsigned char *)in;
  117. lim = (insize != 0) ? (p + insize) : (unsigned char *)-1;
  118. wlim = out == NULL ? NULL : out + outsize;
  119. for (; p < lim; p += n) {
  120. if (!*p && insize == 0)
  121. break;
  122. if (utf8_forbidden(*p) != 0 && (flags & UTF8_IGNORE_ERROR) == 0)
  123. return 0;
  124. /*
  125. * Get number of bytes for one wide character.
  126. */
  127. n = 1; /* default: 1 byte. Used when skipping bytes. */
  128. if ((*p & 0x80) == 0)
  129. high = (wchar_t)*p;
  130. else if ((*p & 0xe0) == _SEQ2) {
  131. n = 2;
  132. high = (wchar_t)(*p & 0x1f);
  133. } else if ((*p & 0xf0) == _SEQ3) {
  134. n = 3;
  135. high = (wchar_t)(*p & 0x0f);
  136. } else if ((*p & 0xf8) == _SEQ4) {
  137. n = 4;
  138. high = (wchar_t)(*p & 0x07);
  139. } else if ((*p & 0xfc) == _SEQ5) {
  140. n = 5;
  141. high = (wchar_t)(*p & 0x03);
  142. } else if ((*p & 0xfe) == _SEQ6) {
  143. n = 6;
  144. high = (wchar_t)(*p & 0x01);
  145. } else {
  146. if ((flags & UTF8_IGNORE_ERROR) == 0)
  147. return 0;
  148. continue;
  149. }
  150. /* does the sequence header tell us truth about length? */
  151. if ((size_t)(lim - p) <= n - 1) {
  152. if ((flags & UTF8_IGNORE_ERROR) == 0)
  153. return 0;
  154. n = 1;
  155. continue; /* skip */
  156. }
  157. /*
  158. * Validate sequence.
  159. * All symbols must have higher bits set to 10xxxxxx
  160. */
  161. if (n > 1) {
  162. for (i = 1; i < n; i++) {
  163. if ((p[i] & 0xc0) != _NXT)
  164. break;
  165. }
  166. if (i != n) {
  167. if ((flags & UTF8_IGNORE_ERROR) == 0)
  168. return 0;
  169. n = 1;
  170. continue; /* skip */
  171. }
  172. }
  173. total++;
  174. if (out == NULL)
  175. continue;
  176. if (out >= wlim)
  177. return 0; /* no space left */
  178. *out = 0;
  179. n_bits = 0;
  180. for (i = 1; i < n; i++) {
  181. *out |= (wchar_t)(p[n - i] & 0x3f) << n_bits;
  182. n_bits += 6; /* 6 low bits in every byte */
  183. }
  184. *out |= high << n_bits;
  185. if (wchar_forbidden(*out) != 0) {
  186. if ((flags & UTF8_IGNORE_ERROR) == 0)
  187. return 0; /* forbidden character */
  188. else {
  189. total--;
  190. out--;
  191. }
  192. } else if (*out == _BOM && (flags & UTF8_SKIP_BOM) != 0) {
  193. total--;
  194. out--;
  195. }
  196. out++;
  197. }
  198. return total;
  199. }
  200. /*
  201. * DESCRIPTION
  202. * This function translates UCS-4 symbols (given in local machine
  203. * byte order) into UTF-8 string.
  204. *
  205. * It takes the following arguments:
  206. * in - input unicode string. It can be null-terminated.
  207. * insize - size of input string in wide characters. If insize is 0,
  208. * function continues until a null terminator is reaches.
  209. * out - result buffer for utf8 string. If out is NULL,
  210. * function returns size of result buffer.
  211. * outsize - size of result buffer.
  212. *
  213. * RETURN VALUES
  214. * The function returns size of result buffer (in bytes). Zero is returned
  215. * in case of error.
  216. *
  217. * CAVEATS
  218. * If UCS-4 string contains zero symbols, they will be translated
  219. * as regular symbols.
  220. */
  221. size_t wchar_to_utf8(const wchar_t *in, size_t insize, char *out, size_t outsize, int flags)
  222. {
  223. wchar_t *w, *wlim, ch = 0;
  224. unsigned char *p, *lim, *oc;
  225. size_t total, n;
  226. if (in == NULL || (outsize == 0 && out != NULL))
  227. return 0;
  228. w = (wchar_t *)in;
  229. wlim = (insize != 0) ? (w + insize) : (wchar_t *)-1;
  230. p = (unsigned char *)out;
  231. lim = out == NULL ? NULL : p + outsize;
  232. total = 0;
  233. for (; w < wlim; w++) {
  234. if (!*w && insize == 0)
  235. break;
  236. if (wchar_forbidden(*w) != 0) {
  237. if ((flags & UTF8_IGNORE_ERROR) == 0)
  238. return 0;
  239. else
  240. continue;
  241. }
  242. if (*w == _BOM && (flags & UTF8_SKIP_BOM) != 0)
  243. continue;
  244. if (*w < 0) {
  245. if ((flags & UTF8_IGNORE_ERROR) == 0)
  246. return 0;
  247. continue;
  248. } else if (*w <= 0x0000007f)
  249. n = 1;
  250. else if (*w <= 0x000007ff)
  251. n = 2;
  252. else if (*w <= 0x0000ffff)
  253. n = 3;
  254. else if (*w <= 0x001fffff)
  255. n = 4;
  256. else if (*w <= 0x03ffffff)
  257. n = 5;
  258. else /* if (*w <= 0x7fffffff) */
  259. n = 6;
  260. total += n;
  261. if (out == NULL)
  262. continue;
  263. if ((size_t)(lim - p) <= n - 1)
  264. return 0; /* no space left */
  265. ch = *w;
  266. oc = (unsigned char *)&ch;
  267. switch (n) {
  268. case 1:
  269. *p = oc[0];
  270. break;
  271. case 2:
  272. p[1] = _NXT | (oc[0] & 0x3f);
  273. p[0] = _SEQ2 | (oc[0] >> 6) | ((oc[1] & 0x07) << 2);
  274. break;
  275. case 3:
  276. p[2] = _NXT | (oc[0] & 0x3f);
  277. p[1] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
  278. p[0] = _SEQ3 | ((oc[1] & 0xf0) >> 4);
  279. break;
  280. case 4:
  281. p[3] = _NXT | (oc[0] & 0x3f);
  282. p[2] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
  283. p[1] = _NXT | ((oc[1] & 0xf0) >> 4) | ((oc[2] & 0x03) << 4);
  284. p[0] = _SEQ4 | ((oc[2] & 0x1f) >> 2);
  285. break;
  286. case 5:
  287. p[4] = _NXT | (oc[0] & 0x3f);
  288. p[3] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
  289. p[2] = _NXT | ((oc[1] & 0xf0) >> 4) | ((oc[2] & 0x03) << 4);
  290. p[1] = _NXT | (oc[2] >> 2);
  291. p[0] = _SEQ5 | (oc[3] & 0x03);
  292. break;
  293. case 6:
  294. p[5] = _NXT | (oc[0] & 0x3f);
  295. p[4] = _NXT | (oc[0] >> 6) | ((oc[1] & 0x0f) << 2);
  296. p[3] = _NXT | (oc[1] >> 4) | ((oc[2] & 0x03) << 4);
  297. p[2] = _NXT | (oc[2] >> 2);
  298. p[1] = _NXT | (oc[3] & 0x3f);
  299. p[0] = _SEQ6 | ((oc[3] & 0x40) >> 6);
  300. break;
  301. }
  302. /*
  303. * NOTE: do not check here for forbidden UTF-8 characters.
  304. * They cannot appear here because we do proper conversion.
  305. */
  306. p += n;
  307. }
  308. return total;
  309. }
  310. #endif