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