rune.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <string.h>
  16. #include "utf.h"
  17. #include "utfdef.h"
  18. enum
  19. {
  20. Bit1 = 7,
  21. Bitx = 6,
  22. Bit2 = 5,
  23. Bit3 = 4,
  24. Bit4 = 3,
  25. Bit5 = 2,
  26. T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */
  27. Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */
  28. T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */
  29. T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */
  30. T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */
  31. T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */
  32. Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */
  33. Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0000 0000 0111 1111 1111 */
  34. Rune3 = (1<<(Bit3+2*Bitx))-1, /* 0000 0000 1111 1111 1111 1111 */
  35. Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0001 1111 1111 1111 1111 1111 */
  36. Maskx = (1<<Bitx)-1, /* 0011 1111 */
  37. Testx = Maskx ^ 0xFF, /* 1100 0000 */
  38. SurrogateMin = 0xD800,
  39. SurrogateMax = 0xDFFF,
  40. Bad = Runeerror,
  41. };
  42. int
  43. chartorune(Rune *rune, char *str)
  44. {
  45. int c, c1, c2, c3;
  46. long l;
  47. /*
  48. * one character sequence
  49. * 00000-0007F => T1
  50. */
  51. c = *(uchar*)str;
  52. if(c < Tx) {
  53. *rune = c;
  54. return 1;
  55. }
  56. /*
  57. * two character sequence
  58. * 00080-007FF => T2 Tx
  59. */
  60. c1 = *(uchar*)(str+1) ^ Tx;
  61. if(c1 & Testx)
  62. goto bad;
  63. if(c < T3) {
  64. if(c < T2)
  65. goto bad;
  66. l = ((c << Bitx) | c1) & Rune2;
  67. if(l <= Rune1)
  68. goto bad;
  69. *rune = l;
  70. return 2;
  71. }
  72. /*
  73. * three character sequence
  74. * 00800-0FFFF => T3 Tx Tx
  75. */
  76. c2 = *(uchar*)(str+2) ^ Tx;
  77. if(c2 & Testx)
  78. goto bad;
  79. if(c < T4) {
  80. l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;
  81. if(l <= Rune2)
  82. goto bad;
  83. if (SurrogateMin <= l && l <= SurrogateMax)
  84. goto bad;
  85. *rune = l;
  86. return 3;
  87. }
  88. /*
  89. * four character sequence
  90. * 10000-10FFFF => T4 Tx Tx Tx
  91. */
  92. if(UTFmax >= 4) {
  93. c3 = *(uchar*)(str+3) ^ Tx;
  94. if(c3 & Testx)
  95. goto bad;
  96. if(c < T5) {
  97. l = ((((((c << Bitx) | c1) << Bitx) | c2) << Bitx) | c3) & Rune4;
  98. if(l <= Rune3)
  99. goto bad;
  100. if(l > Runemax)
  101. goto bad;
  102. *rune = l;
  103. return 4;
  104. }
  105. }
  106. /*
  107. * bad decoding
  108. */
  109. bad:
  110. *rune = Bad;
  111. return 1;
  112. }
  113. int
  114. runetochar(char *str, Rune *rune)
  115. {
  116. long c;
  117. /*
  118. * one character sequence
  119. * 00000-0007F => 00-7F
  120. */
  121. c = *rune;
  122. if(c <= Rune1) {
  123. str[0] = c;
  124. return 1;
  125. }
  126. /*
  127. * two character sequence
  128. * 0080-07FF => T2 Tx
  129. */
  130. if(c <= Rune2) {
  131. str[0] = T2 | (c >> 1*Bitx);
  132. str[1] = Tx | (c & Maskx);
  133. return 2;
  134. }
  135. /*
  136. * If the Rune is out of range or a surrogate half, convert it to the error rune.
  137. * Do this test here because the error rune encodes to three bytes.
  138. * Doing it earlier would duplicate work, since an out of range
  139. * Rune wouldn't have fit in one or two bytes.
  140. */
  141. if (c > Runemax)
  142. c = Runeerror;
  143. if (SurrogateMin <= c && c <= SurrogateMax)
  144. c = Runeerror;
  145. /*
  146. * three character sequence
  147. * 0800-FFFF => T3 Tx Tx
  148. */
  149. if (c <= Rune3) {
  150. str[0] = T3 | (c >> 2*Bitx);
  151. str[1] = Tx | ((c >> 1*Bitx) & Maskx);
  152. str[2] = Tx | (c & Maskx);
  153. return 3;
  154. }
  155. /*
  156. * four character sequence (21-bit value)
  157. * 10000-1FFFFF => T4 Tx Tx Tx
  158. */
  159. str[0] = T4 | (c >> 3*Bitx);
  160. str[1] = Tx | ((c >> 2*Bitx) & Maskx);
  161. str[2] = Tx | ((c >> 1*Bitx) & Maskx);
  162. str[3] = Tx | (c & Maskx);
  163. return 4;
  164. }
  165. int
  166. runelen(long c)
  167. {
  168. Rune rune;
  169. char str[10];
  170. rune = c;
  171. return runetochar(str, &rune);
  172. }
  173. int
  174. runenlen(Rune *r, int nrune)
  175. {
  176. int nb, c;
  177. nb = 0;
  178. while(nrune--) {
  179. c = *r++;
  180. if(c <= Rune1)
  181. nb++;
  182. else if(c <= Rune2)
  183. nb += 2;
  184. else if(c <= Rune3)
  185. nb += 3;
  186. else
  187. nb += 4;
  188. }
  189. return nb;
  190. }
  191. int
  192. fullrune(char *str, int n)
  193. {
  194. int c;
  195. if(n <= 0)
  196. return 0;
  197. c = *(uchar*)str;
  198. if(c < Tx)
  199. return 1;
  200. if(c < T3)
  201. return n >= 2;
  202. if(c < T4)
  203. return n >= 3;
  204. return n >= 4;
  205. }