Enable the function only if HAVE_INLINE.
[gnulib.git] / lib / unistr / u8-mbtouc-unsafe-aux.c
1 /* Conversion UTF-8 to UCS-4.
2    Copyright (C) 2001-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_aux (ucs4_t *puc, const uint8_t *s, size_t n)
29 {
30   uint8_t c = *s;
31
32   if (c >= 0xc2)
33     {
34       if (c < 0xe0)
35         {
36           if (n >= 2)
37             {
38 #if CONFIG_UNICODE_SAFETY
39               if ((s[1] ^ 0x80) < 0x40)
40 #endif
41                 {
42                   *puc = ((unsigned int) (c & 0x1f) << 6)
43                          | (unsigned int) (s[1] ^ 0x80);
44                   return 2;
45                 }
46               /* invalid multibyte character */
47             }
48           else
49             {
50               /* incomplete multibyte character */
51               *puc = 0xfffd;
52               return n;
53             }
54         }
55       else if (c < 0xf0)
56         {
57           if (n >= 3)
58             {
59 #if CONFIG_UNICODE_SAFETY
60               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
61                   && (c >= 0xe1 || s[1] >= 0xa0)
62                   && (c != 0xed || s[1] < 0xa0))
63 #endif
64                 {
65                   *puc = ((unsigned int) (c & 0x0f) << 12)
66                          | ((unsigned int) (s[1] ^ 0x80) << 6)
67                          | (unsigned int) (s[2] ^ 0x80);
68                   return 3;
69                 }
70               /* invalid multibyte character */
71             }
72           else
73             {
74               /* incomplete multibyte character */
75               *puc = 0xfffd;
76               return n;
77             }
78         }
79       else if (c < 0xf8)
80         {
81           if (n >= 4)
82             {
83 #if CONFIG_UNICODE_SAFETY
84               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
85                   && (s[3] ^ 0x80) < 0x40
86                   && (c >= 0xf1 || s[1] >= 0x90)
87 #if 1
88                   && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))
89 #endif
90                  )
91 #endif
92                 {
93                   *puc = ((unsigned int) (c & 0x07) << 18)
94                          | ((unsigned int) (s[1] ^ 0x80) << 12)
95                          | ((unsigned int) (s[2] ^ 0x80) << 6)
96                          | (unsigned int) (s[3] ^ 0x80);
97                   return 4;
98                 }
99               /* invalid multibyte character */
100             }
101           else
102             {
103               /* incomplete multibyte character */
104               *puc = 0xfffd;
105               return n;
106             }
107         }
108 #if 0
109       else if (c < 0xfc)
110         {
111           if (n >= 5)
112             {
113 #if CONFIG_UNICODE_SAFETY
114               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
115                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
116                   && (c >= 0xf9 || s[1] >= 0x88))
117 #endif
118                 {
119                   *puc = ((unsigned int) (c & 0x03) << 24)
120                          | ((unsigned int) (s[1] ^ 0x80) << 18)
121                          | ((unsigned int) (s[2] ^ 0x80) << 12)
122                          | ((unsigned int) (s[3] ^ 0x80) << 6)
123                          | (unsigned int) (s[4] ^ 0x80);
124                   return 5;
125                 }
126               /* invalid multibyte character */
127             }
128           else
129             {
130               /* incomplete multibyte character */
131               *puc = 0xfffd;
132               return n;
133             }
134         }
135       else if (c < 0xfe)
136         {
137           if (n >= 6)
138             {
139 #if CONFIG_UNICODE_SAFETY
140               if ((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
141                   && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
142                   && (s[5] ^ 0x80) < 0x40
143                   && (c >= 0xfd || s[1] >= 0x84))
144 #endif
145                 {
146                   *puc = ((unsigned int) (c & 0x01) << 30)
147                          | ((unsigned int) (s[1] ^ 0x80) << 24)
148                          | ((unsigned int) (s[2] ^ 0x80) << 18)
149                          | ((unsigned int) (s[3] ^ 0x80) << 12)
150                          | ((unsigned int) (s[4] ^ 0x80) << 6)
151                          | (unsigned int) (s[5] ^ 0x80);
152                   return 6;
153                 }
154               /* invalid multibyte character */
155             }
156           else
157             {
158               /* incomplete multibyte character */
159               *puc = 0xfffd;
160               return n;
161             }
162         }
163 #endif
164     }
165   /* invalid multibyte character */
166   *puc = 0xfffd;
167   return 1;
168 }
169
170 #endif