rune.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <stdarg.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include "third_party/utf/utf.h"
  18. enum {
  19. Bit1 = 7,
  20. Bitx = 6,
  21. Bit2 = 5,
  22. Bit3 = 4,
  23. Bit4 = 3,
  24. Bit5 = 2,
  25. T1 = ((1 << (Bit1 + 1)) - 1) ^ 0xFF, /* 0000 0000 */
  26. Tx = ((1 << (Bitx + 1)) - 1) ^ 0xFF, /* 1000 0000 */
  27. T2 = ((1 << (Bit2 + 1)) - 1) ^ 0xFF, /* 1100 0000 */
  28. T3 = ((1 << (Bit3 + 1)) - 1) ^ 0xFF, /* 1110 0000 */
  29. T4 = ((1 << (Bit4 + 1)) - 1) ^ 0xFF, /* 1111 0000 */
  30. T5 = ((1 << (Bit5 + 1)) - 1) ^ 0xFF, /* 1111 1000 */
  31. Rune1 = (1 << (Bit1 + 0 * Bitx)) - 1, /* 0000 0000 0111 1111 */
  32. Rune2 = (1 << (Bit2 + 1 * Bitx)) - 1, /* 0000 0111 1111 1111 */
  33. Rune3 = (1 << (Bit3 + 2 * Bitx)) - 1, /* 1111 1111 1111 1111 */
  34. Rune4 = (1 << (Bit4 + 3 * Bitx)) - 1,
  35. /* 0001 1111 1111 1111 1111 1111 */
  36. Maskx = (1 << Bitx) - 1, /* 0011 1111 */
  37. Testx = Maskx ^ 0xFF, /* 1100 0000 */
  38. Bad = Runeerror,
  39. };
  40. /*
  41. * Modified by Wei-Hwa Huang, Google Inc., on 2004-09-24
  42. * This is a slower but "safe" version of the old chartorune
  43. * that works on strings that are not necessarily null-terminated.
  44. *
  45. * If you know for sure that your string is null-terminated,
  46. * chartorune will be a bit faster.
  47. *
  48. * It is guaranteed not to attempt to access "length"
  49. * past the incoming pointer. This is to avoid
  50. * possible access violations. If the string appears to be
  51. * well-formed but incomplete (i.e., to get the whole Rune
  52. * we'd need to read past str+length) then we'll set the Rune
  53. * to Bad and return 0.
  54. *
  55. * Note that if we have decoding problems for other
  56. * reasons, we return 1 instead of 0.
  57. */
  58. int charntorune(Rune *rune, const char *str, int length) {
  59. int c, c1, c2, c3;
  60. long l;
  61. /* When we're not allowed to read anything */
  62. if (length <= 0) {
  63. goto badlen;
  64. }
  65. /*
  66. * one character sequence (7-bit value)
  67. * 00000-0007F => T1
  68. */
  69. c = *(uint8_t *)str;
  70. if (c < Tx) {
  71. *rune = c;
  72. return 1;
  73. }
  74. // If we can't read more than one character we must stop
  75. if (length <= 1) {
  76. goto badlen;
  77. }
  78. /*
  79. * two character sequence (11-bit value)
  80. * 0080-07FF => T2 Tx
  81. */
  82. c1 = *(uint8_t *)(str + 1) ^ Tx;
  83. if (c1 & Testx)
  84. goto bad;
  85. if (c < T3) {
  86. if (c < T2)
  87. goto bad;
  88. l = ((c << Bitx) | c1) & Rune2;
  89. if (l <= Rune1)
  90. goto bad;
  91. *rune = l;
  92. return 2;
  93. }
  94. // If we can't read more than two characters we must stop
  95. if (length <= 2) {
  96. goto badlen;
  97. }
  98. /*
  99. * three character sequence (16-bit value)
  100. * 0800-FFFF => T3 Tx Tx
  101. */
  102. c2 = *(uint8_t *)(str + 2) ^ Tx;
  103. if (c2 & Testx)
  104. goto bad;
  105. if (c < T4) {
  106. l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
  107. if (l <= Rune2)
  108. goto bad;
  109. *rune = l;
  110. return 3;
  111. }
  112. if (length <= 3)
  113. goto badlen;
  114. /*
  115. * four character sequence (21-bit value)
  116. * 10000-1FFFFF => T4 Tx Tx Tx
  117. */
  118. c3 = *(uint8_t *)(str + 3) ^ Tx;
  119. if (c3 & Testx)
  120. goto bad;
  121. if (c < T5) {
  122. l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
  123. if (l <= Rune3)
  124. goto bad;
  125. if (l > Runemax)
  126. goto bad;
  127. *rune = l;
  128. return 4;
  129. }
  130. // Support for 5-byte or longer UTF-8 would go here, but
  131. // since we don't have that, we'll just fall through to bad.
  132. /*
  133. * bad decoding
  134. */
  135. bad:
  136. *rune = Bad;
  137. return 1;
  138. badlen:
  139. *rune = Bad;
  140. return 0;
  141. }
  142. /*
  143. * This is the older "unsafe" version, which works fine on
  144. * null-terminated strings.
  145. */
  146. int chartorune(Rune *rune, const char *str) {
  147. int c, c1, c2, c3;
  148. long l;
  149. /*
  150. * one character sequence
  151. * 00000-0007F => T1
  152. */
  153. c = *(uint8_t *)str;
  154. if (c < Tx) {
  155. *rune = c;
  156. return 1;
  157. }
  158. /*
  159. * two character sequence
  160. * 0080-07FF => T2 Tx
  161. */
  162. c1 = *(uint8_t *)(str + 1) ^ Tx;
  163. if (c1 & Testx)
  164. goto bad;
  165. if (c < T3) {
  166. if (c < T2)
  167. goto bad;
  168. l = ((c << Bitx) | c1) & Rune2;
  169. if (l <= Rune1)
  170. goto bad;
  171. *rune = l;
  172. return 2;
  173. }
  174. /*
  175. * three character sequence
  176. * 0800-FFFF => T3 Tx Tx
  177. */
  178. c2 = *(uint8_t *)(str + 2) ^ Tx;
  179. if (c2 & Testx)
  180. goto bad;
  181. if (c < T4) {
  182. l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
  183. if (l <= Rune2)
  184. goto bad;
  185. *rune = l;
  186. return 3;
  187. }
  188. /*
  189. * four character sequence (21-bit value)
  190. * 10000-1FFFFF => T4 Tx Tx Tx
  191. */
  192. c3 = *(uint8_t *)(str + 3) ^ Tx;
  193. if (c3 & Testx)
  194. goto bad;
  195. if (c < T5) {
  196. l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
  197. if (l <= Rune3)
  198. goto bad;
  199. if (l > Runemax)
  200. goto bad;
  201. *rune = l;
  202. return 4;
  203. }
  204. /*
  205. * Support for 5-byte or longer UTF-8 would go here, but
  206. * since we don't have that, we'll just fall through to bad.
  207. */
  208. /*
  209. * bad decoding
  210. */
  211. bad:
  212. *rune = Bad;
  213. return 1;
  214. }
  215. int isvalidcharntorune(const char *str, int length, Rune *rune, int *consumed) {
  216. *consumed = charntorune(rune, str, length);
  217. return *rune != Runeerror || *consumed == 3;
  218. }
  219. int runetochar(char *str, const Rune *rune) {
  220. /* Runes are signed, so convert to unsigned for range check. */
  221. unsigned long c;
  222. /*
  223. * one character sequence
  224. * 00000-0007F => 00-7F
  225. */
  226. c = *rune;
  227. if (c <= Rune1) {
  228. str[0] = c;
  229. return 1;
  230. }
  231. /*
  232. * two character sequence
  233. * 0080-07FF => T2 Tx
  234. */
  235. if (c <= Rune2) {
  236. str[0] = T2 | (c >> 1 * Bitx);
  237. str[1] = Tx | (c & Maskx);
  238. return 2;
  239. }
  240. /*
  241. * If the Rune is out of range, convert it to the error rune.
  242. * Do this test here because the error rune encodes to three bytes.
  243. * Doing it earlier would duplicate work, since an out of range
  244. * Rune wouldn't have fit in one or two bytes.
  245. */
  246. if (c > Runemax)
  247. c = Runeerror;
  248. /*
  249. * three character sequence
  250. * 0800-FFFF => T3 Tx Tx
  251. */
  252. if (c <= Rune3) {
  253. str[0] = T3 | (c >> 2 * Bitx);
  254. str[1] = Tx | ((c >> 1 * Bitx) & Maskx);
  255. str[2] = Tx | (c & Maskx);
  256. return 3;
  257. }
  258. /*
  259. * four character sequence (21-bit value)
  260. * 10000-1FFFFF => T4 Tx Tx Tx
  261. */
  262. str[0] = T4 | (c >> 3 * Bitx);
  263. str[1] = Tx | ((c >> 2 * Bitx) & Maskx);
  264. str[2] = Tx | ((c >> 1 * Bitx) & Maskx);
  265. str[3] = Tx | (c & Maskx);
  266. return 4;
  267. }
  268. int runelen(Rune rune) {
  269. char str[10];
  270. return runetochar(str, &rune);
  271. }
  272. int runenlen(const Rune *r, int nrune) {
  273. int nb;
  274. unsigned long c; /* Rune is signed, so use unsigned for range check. */
  275. nb = 0;
  276. while (nrune--) {
  277. c = *r++;
  278. if (c <= Rune1)
  279. nb++;
  280. else if (c <= Rune2)
  281. nb += 2;
  282. else if (c <= Rune3)
  283. nb += 3;
  284. else if (c <= Runemax)
  285. nb += 4;
  286. else
  287. nb += 3; /* Runeerror = 0xFFFD, see runetochar */
  288. }
  289. return nb;
  290. }
  291. int fullrune(const char *str, int n) {
  292. if (n > 0) {
  293. int c = *(uint8_t *)str;
  294. if (c < Tx)
  295. return 1;
  296. if (n > 1) {
  297. if (c < T3)
  298. return 1;
  299. if (n > 2) {
  300. if (c < T4 || n > 3)
  301. return 1;
  302. }
  303. }
  304. }
  305. return 0;
  306. }