aa88aecccb31444e17bfdb416987784222f88850
[gnulib.git] / lib / unistr / u8-mbtouc-unsafe.c
1 /* Look at first character in UTF-8 string.
2    Copyright (C) 1999-2002, 2006-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2001.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU Library General Public License as published
7    by the Free Software Foundation; either version 2, or (at your option)
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18    USA.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "unistr.h"
24
25 #if !HAVE_INLINE
26
27 int
28 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
29 {
30   uint8_t c = *s;
31
32   if (c < 0x80)
33     {
34       *puc = c;
35       return 1;
36     }
37   else if (c >= 0xc2)
38     {
39       if (c < 0xe0)
40         {
41           if (n >= 2)
42             {
43 #if CONFIG_UNICODE_SAFETY
44               if ((s[1] ^ 0x80) < 0x40)
45 #endif
46                 {
47                   *puc = ((unsigned int) (c & 0x1f) << 6)
48                          | (unsigned int) (s[1] ^ 0x80);
49                   return 2;
50                 }
51               /* invalid multibyte character */
52             }
53           else
54             {
55               /* incomplete multibyte character */
56               *puc = 0xfffd;
57               return n;
58             }
59         }
60       else if (c < 0xf0)
61         {
62           if (n >= 3)
63             {
64 #if CONFIG_UNICODE_SAFETY
65               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
66                   && (c >= 0xe1 || s[1] >= 0xa0)
67                   && (c != 0xed || s[1] < 0xa0))
68 #endif
69                 {
70                   *puc = ((unsigned int) (c & 0x0f) << 12)
71                          | ((unsigned int) (s[1] ^ 0x80) << 6)
72                          | (unsigned int) (s[2] ^ 0x80);
73                   return 3;
74                 }
75               /* invalid multibyte character */
76             }
77           else
78             {
79               /* incomplete multibyte character */
80               *puc = 0xfffd;
81               return n;
82             }
83         }
84       else if (c < 0xf8)
85         {
86           if (n >= 4)
87             {
88 #if CONFIG_UNICODE_SAFETY
89               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
90                   && (s[3] ^ 0x80) < 0x40
91                   && (c >= 0xf1 || s[1] >= 0x90)
92 #if 1
93                   && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
94 #endif
95                  )
96 #endif
97                 {
98                   *puc = ((unsigned int) (c & 0x07) << 18)
99                          | ((unsigned int) (s[1] ^ 0x80) << 12)
100                          | ((unsigned int) (s[2] ^ 0x80) << 6)
101                          | (unsigned int) (s[3] ^ 0x80);
102                   return 4;
103                 }
104               /* invalid multibyte character */
105             }
106           else
107             {
108               /* incomplete multibyte character */
109               *puc = 0xfffd;
110               return n;
111             }
112         }
113 #if 0
114       else if (c < 0xfc)
115         {
116           if (n >= 5)
117             {
118 #if CONFIG_UNICODE_SAFETY
119               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
120                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
121                   && (c >= 0xf9 || s[1] >= 0x88))
122 #endif
123                 {
124                   *puc = ((unsigned int) (c & 0x03) << 24)
125                          | ((unsigned int) (s[1] ^ 0x80) << 18)
126                          | ((unsigned int) (s[2] ^ 0x80) << 12)
127                          | ((unsigned int) (s[3] ^ 0x80) << 6)
128                          | (unsigned int) (s[4] ^ 0x80);
129                   return 5;
130                 }
131               /* invalid multibyte character */
132             }
133           else
134             {
135               /* incomplete multibyte character */
136               *puc = 0xfffd;
137               return n;
138             }
139         }
140       else if (c < 0xfe)
141         {
142           if (n >= 6)
143             {
144 #if CONFIG_UNICODE_SAFETY
145               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
146                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
147                   && (s[5] ^ 0x80) < 0x40
148                   && (c >= 0xfd || s[1] >= 0x84))
149 #endif
150                 {
151                   *puc = ((unsigned int) (c & 0x01) << 30)
152                          | ((unsigned int) (s[1] ^ 0x80) << 24)
153                          | ((unsigned int) (s[2] ^ 0x80) << 18)
154                          | ((unsigned int) (s[3] ^ 0x80) << 12)
155                          | ((unsigned int) (s[4] ^ 0x80) << 6)
156                          | (unsigned int) (s[5] ^ 0x80);
157                   return 6;
158                 }
159               /* invalid multibyte character */
160             }
161           else
162             {
163               /* incomplete multibyte character */
164               *puc = 0xfffd;
165               return n;
166             }
167         }
168 #endif
169     }
170   /* invalid multibyte character */
171   *puc = 0xfffd;
172   return 1;
173 }
174
175 #endif