/src/libunistring/lib/unistr/u8-mbtouc-aux.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* Conversion UTF-8 to UCS-4.  | 
2  |  |    Copyright (C) 2001-2002, 2006-2007, 2009-2024 Free Software Foundation, Inc.  | 
3  |  |    Written by Bruno Haible <bruno@clisp.org>, 2001.  | 
4  |  |  | 
5  |  |    This file is free software: you can redistribute it and/or modify  | 
6  |  |    it under the terms of the GNU Lesser General Public License as  | 
7  |  |    published by the Free Software Foundation; either version 2.1 of the  | 
8  |  |    License, or (at your option) any later version.  | 
9  |  |  | 
10  |  |    This file 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  | 
13  |  |    GNU 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 <https://www.gnu.org/licenses/>.  */  | 
17  |  |  | 
18  |  | #include <config.h>  | 
19  |  |  | 
20  |  | /* Specification.  */  | 
21  |  | #include "unistr.h"  | 
22  |  |  | 
23  |  | #if defined IN_LIBUNISTRING || HAVE_INLINE  | 
24  |  |  | 
25  |  | int  | 
26  |  | u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n)  | 
27  | 0  | { | 
28  | 0  |   uint8_t c = *s;  | 
29  |  | 
  | 
30  | 0  |   if (c >= 0xc2)  | 
31  | 0  |     { | 
32  | 0  |       if (c < 0xe0)  | 
33  | 0  |         { | 
34  | 0  |           if (n >= 2)  | 
35  | 0  |             { | 
36  | 0  |               if ((s[1] ^ 0x80) < 0x40)  | 
37  | 0  |                 { | 
38  | 0  |                   *puc = ((unsigned int) (c & 0x1f) << 6)  | 
39  | 0  |                          | (unsigned int) (s[1] ^ 0x80);  | 
40  | 0  |                   return 2;  | 
41  | 0  |                 }  | 
42  |  |               /* invalid multibyte character */  | 
43  | 0  |             }  | 
44  | 0  |           else  | 
45  | 0  |             { | 
46  |  |               /* incomplete multibyte character */  | 
47  | 0  |               *puc = 0xfffd;  | 
48  | 0  |               return 1;  | 
49  | 0  |             }  | 
50  | 0  |         }  | 
51  | 0  |       else if (c < 0xf0)  | 
52  | 0  |         { | 
53  | 0  |           if (n >= 3)  | 
54  | 0  |             { | 
55  | 0  |               if ((s[1] ^ 0x80) < 0x40  | 
56  | 0  |                   && (c >= 0xe1 || s[1] >= 0xa0)  | 
57  | 0  |                   && (c != 0xed || s[1] < 0xa0))  | 
58  | 0  |                 { | 
59  | 0  |                   if ((s[2] ^ 0x80) < 0x40)  | 
60  | 0  |                     { | 
61  | 0  |                       *puc = ((unsigned int) (c & 0x0f) << 12)  | 
62  | 0  |                              | ((unsigned int) (s[1] ^ 0x80) << 6)  | 
63  | 0  |                              | (unsigned int) (s[2] ^ 0x80);  | 
64  | 0  |                       return 3;  | 
65  | 0  |                     }  | 
66  |  |                   /* invalid multibyte character */  | 
67  | 0  |                   *puc = 0xfffd;  | 
68  | 0  |                   return 2;  | 
69  | 0  |                 }  | 
70  |  |               /* invalid multibyte character */  | 
71  | 0  |               *puc = 0xfffd;  | 
72  | 0  |               return 1;  | 
73  | 0  |             }  | 
74  | 0  |           else  | 
75  | 0  |             { | 
76  | 0  |               *puc = 0xfffd;  | 
77  | 0  |               if (n == 1)  | 
78  | 0  |                 { | 
79  |  |                   /* incomplete multibyte character */  | 
80  | 0  |                   return 1;  | 
81  | 0  |                 }  | 
82  | 0  |               else  | 
83  | 0  |                 { | 
84  | 0  |                   if ((s[1] ^ 0x80) < 0x40  | 
85  | 0  |                       && (c >= 0xe1 || s[1] >= 0xa0)  | 
86  | 0  |                       && (c != 0xed || s[1] < 0xa0))  | 
87  | 0  |                     { | 
88  |  |                       /* incomplete multibyte character */  | 
89  | 0  |                       return 2;  | 
90  | 0  |                     }  | 
91  | 0  |                   else  | 
92  | 0  |                     { | 
93  |  |                       /* invalid multibyte character */  | 
94  | 0  |                       return 1;  | 
95  | 0  |                     }  | 
96  | 0  |                 }  | 
97  | 0  |             }  | 
98  | 0  |         }  | 
99  | 0  |       else if (c <= 0xf4)  | 
100  | 0  |         { | 
101  | 0  |           if (n >= 4)  | 
102  | 0  |             { | 
103  | 0  |               if ((s[1] ^ 0x80) < 0x40  | 
104  | 0  |                   && (c >= 0xf1 || s[1] >= 0x90)  | 
105  | 0  |                   && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))  | 
106  | 0  |                 { | 
107  | 0  |                   if ((s[2] ^ 0x80) < 0x40)  | 
108  | 0  |                     { | 
109  | 0  |                       if ((s[3] ^ 0x80) < 0x40)  | 
110  | 0  |                         { | 
111  | 0  |                           *puc = ((unsigned int) (c & 0x07) << 18)  | 
112  | 0  |                                  | ((unsigned int) (s[1] ^ 0x80) << 12)  | 
113  | 0  |                                  | ((unsigned int) (s[2] ^ 0x80) << 6)  | 
114  | 0  |                                  | (unsigned int) (s[3] ^ 0x80);  | 
115  | 0  |                           return 4;  | 
116  | 0  |                         }  | 
117  |  |                       /* invalid multibyte character */  | 
118  | 0  |                       *puc = 0xfffd;  | 
119  | 0  |                       return 3;  | 
120  | 0  |                     }  | 
121  |  |                   /* invalid multibyte character */  | 
122  | 0  |                   *puc = 0xfffd;  | 
123  | 0  |                   return 2;  | 
124  | 0  |                 }  | 
125  |  |               /* invalid multibyte character */  | 
126  | 0  |               *puc = 0xfffd;  | 
127  | 0  |               return 1;  | 
128  | 0  |             }  | 
129  | 0  |           else  | 
130  | 0  |             { | 
131  | 0  |               *puc = 0xfffd;  | 
132  | 0  |               if (n == 1)  | 
133  | 0  |                 { | 
134  |  |                   /* incomplete multibyte character */  | 
135  | 0  |                   return 1;  | 
136  | 0  |                 }  | 
137  | 0  |               else  | 
138  | 0  |                 { | 
139  | 0  |                   if ((s[1] ^ 0x80) < 0x40  | 
140  | 0  |                       && (c >= 0xf1 || s[1] >= 0x90)  | 
141  | 0  |                       && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))  | 
142  | 0  |                     { | 
143  | 0  |                       if (n == 2)  | 
144  | 0  |                         { | 
145  |  |                           /* incomplete multibyte character */  | 
146  | 0  |                           return 2;  | 
147  | 0  |                         }  | 
148  | 0  |                       else  | 
149  | 0  |                         { | 
150  | 0  |                           if ((s[2] ^ 0x80) < 0x40)  | 
151  | 0  |                             { | 
152  |  |                               /* incomplete multibyte character */  | 
153  | 0  |                               return 3;  | 
154  | 0  |                             }  | 
155  | 0  |                           else  | 
156  | 0  |                             { | 
157  |  |                               /* invalid multibyte character */  | 
158  | 0  |                               return 2;  | 
159  | 0  |                             }  | 
160  | 0  |                         }  | 
161  | 0  |                     }  | 
162  | 0  |                   else  | 
163  | 0  |                     { | 
164  |  |                       /* invalid multibyte character */  | 
165  | 0  |                       return 1;  | 
166  | 0  |                     }  | 
167  | 0  |                 }  | 
168  | 0  |             }  | 
169  | 0  |         }  | 
170  | 0  |     }  | 
171  |  |   /* invalid multibyte character */  | 
172  | 0  |   *puc = 0xfffd;  | 
173  | 0  |   return 1;  | 
174  | 0  | }  | 
175  |  |  | 
176  |  | #endif  |