36562d132699a43b74549f3a5f29dfac410def54
[gnulib.git] / lib / unistr / u8-strmbtouc.c
1 /* Look at first character in UTF-8 string.
2    Copyright (C) 1999-2000, 2002, 2006-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2002.
4
5    This program is free software: you can redistribute it and/or modify it
6    under the terms of the GNU Lesser General Public License as published
7    by the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "unistr.h"
22
23 int
24 u8_strmbtouc (ucs4_t *puc, const uint8_t *s)
25 {
26   /* Keep in sync with unistr.h and utf8-ucs4.c.  */
27   uint8_t c = *s;
28
29   if (c < 0x80)
30     {
31       *puc = c;
32       return (c != 0 ? 1 : 0);
33     }
34   if (c >= 0xc2)
35     {
36       if (c < 0xe0)
37         {
38 #if CONFIG_UNICODE_SAFETY
39           if ((s[1] ^ 0x80) < 0x40)
40 #else
41           if (s[1] != 0)
42 #endif
43             {
44               *puc = ((unsigned int) (c & 0x1f) << 6)
45                      | (unsigned int) (s[1] ^ 0x80);
46               return 2;
47             }
48         }
49       else if (c < 0xf0)
50         {
51 #if CONFIG_UNICODE_SAFETY
52           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
53               && (c >= 0xe1 || s[1] >= 0xa0)
54               && (c != 0xed || s[1] < 0xa0))
55 #else
56           if (s[1] != 0 && s[2] != 0)
57 #endif
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         }
65       else if (c < 0xf8)
66         {
67 #if CONFIG_UNICODE_SAFETY
68           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
69               && (s[3] ^ 0x80) < 0x40
70               && (c >= 0xf1 || s[1] >= 0x90)
71 #if 1
72               && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
73 #endif
74              )
75 #else
76           if (s[1] != 0 && s[2] != 0 && s[3] != 0)
77 #endif
78             {
79               *puc = ((unsigned int) (c & 0x07) << 18)
80                      | ((unsigned int) (s[1] ^ 0x80) << 12)
81                      | ((unsigned int) (s[2] ^ 0x80) << 6)
82                      | (unsigned int) (s[3] ^ 0x80);
83               return 4;
84             }
85         }
86 #if 0
87       else if (c < 0xfc)
88         {
89 #if CONFIG_UNICODE_SAFETY
90           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
91               && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
92               && (c >= 0xf9 || s[1] >= 0x88))
93 #else
94           if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0)
95 #endif
96             {
97               *puc = ((unsigned int) (c & 0x03) << 24)
98                      | ((unsigned int) (s[1] ^ 0x80) << 18)
99                      | ((unsigned int) (s[2] ^ 0x80) << 12)
100                      | ((unsigned int) (s[3] ^ 0x80) << 6)
101                      | (unsigned int) (s[4] ^ 0x80);
102               return 5;
103             }
104         }
105       else if (c < 0xfe)
106         {
107 #if CONFIG_UNICODE_SAFETY
108           if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
109               && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
110               && (s[5] ^ 0x80) < 0x40
111               && (c >= 0xfd || s[1] >= 0x84))
112 #else
113           if (s[1] != 0 && s[2] != 0 && s[3] != 0 && s[4] != 0 && s[5] != 0)
114 #endif
115             {
116               *puc = ((unsigned int) (c & 0x01) << 30)
117                      | ((unsigned int) (s[1] ^ 0x80) << 24)
118                      | ((unsigned int) (s[2] ^ 0x80) << 18)
119                      | ((unsigned int) (s[3] ^ 0x80) << 12)
120                      | ((unsigned int) (s[4] ^ 0x80) << 6)
121                      | (unsigned int) (s[5] ^ 0x80);
122               return 6;
123             }
124         }
125 #endif
126     }
127   /* invalid or incomplete multibyte character */
128   return -1;
129 }