u8-mbtoucr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Look at first character in UTF-8 string, returning an error code.
  2. Copyright (C) 1999-2002, 2006-2007, 2009-2012 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2001.
  4. This program is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "unistr.h"
  17. int
  18. u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)
  19. {
  20. uint8_t c = *s;
  21. if (c < 0x80)
  22. {
  23. *puc = c;
  24. return 1;
  25. }
  26. else if (c >= 0xc2)
  27. {
  28. if (c < 0xe0)
  29. {
  30. if (n >= 2)
  31. {
  32. if ((s[1] ^ 0x80) < 0x40)
  33. {
  34. *puc = ((unsigned int) (c & 0x1f) << 6)
  35. | (unsigned int) (s[1] ^ 0x80);
  36. return 2;
  37. }
  38. /* invalid multibyte character */
  39. }
  40. else
  41. {
  42. /* incomplete multibyte character */
  43. *puc = 0xfffd;
  44. return -2;
  45. }
  46. }
  47. else if (c < 0xf0)
  48. {
  49. if (n >= 2)
  50. {
  51. if ((s[1] ^ 0x80) < 0x40
  52. && (c >= 0xe1 || s[1] >= 0xa0)
  53. && (c != 0xed || s[1] < 0xa0))
  54. {
  55. if (n >= 3)
  56. {
  57. if ((s[2] ^ 0x80) < 0x40)
  58. {
  59. *puc = ((unsigned int) (c & 0x0f) << 12)
  60. | ((unsigned int) (s[1] ^ 0x80) << 6)
  61. | (unsigned int) (s[2] ^ 0x80);
  62. return 3;
  63. }
  64. /* invalid multibyte character */
  65. }
  66. else
  67. {
  68. /* incomplete multibyte character */
  69. *puc = 0xfffd;
  70. return -2;
  71. }
  72. }
  73. /* invalid multibyte character */
  74. }
  75. else
  76. {
  77. /* incomplete multibyte character */
  78. *puc = 0xfffd;
  79. return -2;
  80. }
  81. }
  82. else if (c < 0xf8)
  83. {
  84. if (n >= 2)
  85. {
  86. if ((s[1] ^ 0x80) < 0x40
  87. && (c >= 0xf1 || s[1] >= 0x90)
  88. #if 1
  89. && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
  90. #endif
  91. )
  92. {
  93. if (n >= 3)
  94. {
  95. if ((s[2] ^ 0x80) < 0x40)
  96. {
  97. if (n >= 4)
  98. {
  99. if ((s[3] ^ 0x80) < 0x40)
  100. {
  101. *puc = ((unsigned int) (c & 0x07) << 18)
  102. | ((unsigned int) (s[1] ^ 0x80) << 12)
  103. | ((unsigned int) (s[2] ^ 0x80) << 6)
  104. | (unsigned int) (s[3] ^ 0x80);
  105. return 4;
  106. }
  107. /* invalid multibyte character */
  108. }
  109. else
  110. {
  111. /* incomplete multibyte character */
  112. *puc = 0xfffd;
  113. return -2;
  114. }
  115. }
  116. /* invalid multibyte character */
  117. }
  118. else
  119. {
  120. /* incomplete multibyte character */
  121. *puc = 0xfffd;
  122. return -2;
  123. }
  124. }
  125. /* invalid multibyte character */
  126. }
  127. else
  128. {
  129. /* incomplete multibyte character */
  130. *puc = 0xfffd;
  131. return -2;
  132. }
  133. }
  134. #if 0
  135. else if (c < 0xfc)
  136. {
  137. if (n >= 2)
  138. {
  139. if ((s[1] ^ 0x80) < 0x40
  140. && (c >= 0xf9 || s[1] >= 0x88))
  141. {
  142. if (n >= 3)
  143. {
  144. if ((s[2] ^ 0x80) < 0x40)
  145. {
  146. if (n >= 4)
  147. {
  148. if ((s[3] ^ 0x80) < 0x40)
  149. {
  150. if (n >= 5)
  151. {
  152. if ((s[4] ^ 0x80) < 0x40)
  153. {
  154. *puc = ((unsigned int) (c & 0x03) << 24)
  155. | ((unsigned int) (s[1] ^ 0x80) << 18)
  156. | ((unsigned int) (s[2] ^ 0x80) << 12)
  157. | ((unsigned int) (s[3] ^ 0x80) << 6)
  158. | (unsigned int) (s[4] ^ 0x80);
  159. return 5;
  160. }
  161. /* invalid multibyte character */
  162. }
  163. else
  164. {
  165. /* incomplete multibyte character */
  166. *puc = 0xfffd;
  167. return -2;
  168. }
  169. }
  170. /* invalid multibyte character */
  171. }
  172. else
  173. {
  174. /* incomplete multibyte character */
  175. *puc = 0xfffd;
  176. return -2;
  177. }
  178. }
  179. /* invalid multibyte character */
  180. }
  181. else
  182. {
  183. /* incomplete multibyte character */
  184. *puc = 0xfffd;
  185. return -2;
  186. }
  187. }
  188. /* invalid multibyte character */
  189. }
  190. else
  191. {
  192. /* incomplete multibyte character */
  193. *puc = 0xfffd;
  194. return -2;
  195. }
  196. }
  197. else if (c < 0xfe)
  198. {
  199. if (n >= 2)
  200. {
  201. if ((s[1] ^ 0x80) < 0x40
  202. && (c >= 0xfd || s[1] >= 0x84))
  203. {
  204. if (n >= 3)
  205. {
  206. if ((s[2] ^ 0x80) < 0x40)
  207. {
  208. if (n >= 4)
  209. {
  210. if ((s[3] ^ 0x80) < 0x40)
  211. {
  212. if (n >= 5)
  213. {
  214. if ((s[4] ^ 0x80) < 0x40)
  215. {
  216. if (n >= 6)
  217. {
  218. if ((s[5] ^ 0x80) < 0x40)
  219. {
  220. *puc = ((unsigned int) (c & 0x01) << 30)
  221. | ((unsigned int) (s[1] ^ 0x80) << 24)
  222. | ((unsigned int) (s[2] ^ 0x80) << 18)
  223. | ((unsigned int) (s[3] ^ 0x80) << 12)
  224. | ((unsigned int) (s[4] ^ 0x80) << 6)
  225. | (unsigned int) (s[5] ^ 0x80);
  226. return 6;
  227. }
  228. /* invalid multibyte character */
  229. }
  230. else
  231. {
  232. /* incomplete multibyte character */
  233. *puc = 0xfffd;
  234. return -2;
  235. }
  236. }
  237. /* invalid multibyte character */
  238. }
  239. else
  240. {
  241. /* incomplete multibyte character */
  242. *puc = 0xfffd;
  243. return -2;
  244. }
  245. }
  246. /* invalid multibyte character */
  247. }
  248. else
  249. {
  250. /* incomplete multibyte character */
  251. *puc = 0xfffd;
  252. return -2;
  253. }
  254. }
  255. /* invalid multibyte character */
  256. }
  257. else
  258. {
  259. /* incomplete multibyte character */
  260. *puc = 0xfffd;
  261. return -2;
  262. }
  263. }
  264. /* invalid multibyte character */
  265. }
  266. else
  267. {
  268. /* incomplete multibyte character */
  269. *puc = 0xfffd;
  270. return -2;
  271. }
  272. }
  273. #endif
  274. }
  275. /* invalid multibyte character */
  276. *puc = 0xfffd;
  277. return -1;
  278. }