/src/libidn2/unistring/unistr/u8-mbtoucr.c
Line  | Count  | Source  | 
1  |  | /* Look at first character in UTF-8 string, returning an error code.  | 
2  |  |    Copyright (C) 1999-2002, 2006-2007, 2009-2025 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  |  | int  | 
24  |  | u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)  | 
25  | 528k  | { | 
26  | 528k  |   uint8_t c = *s;  | 
27  |  |  | 
28  | 528k  |   if (c < 0x80)  | 
29  | 299k  |     { | 
30  | 299k  |       *puc = c;  | 
31  | 299k  |       return 1;  | 
32  | 299k  |     }  | 
33  | 228k  |   else if (c >= 0xc2)  | 
34  | 228k  |     { | 
35  | 228k  |       if (c < 0xe0)  | 
36  | 103k  |         { | 
37  | 103k  |           if (n >= 2)  | 
38  | 103k  |             { | 
39  | 103k  |               if ((s[1] ^ 0x80) < 0x40)  | 
40  | 102k  |                 { | 
41  | 102k  |                   *puc = ((unsigned int) (c & 0x1f) << 6)  | 
42  | 102k  |                          | (unsigned int) (s[1] ^ 0x80);  | 
43  | 102k  |                   return 2;  | 
44  | 102k  |                 }  | 
45  |  |               /* invalid multibyte character */  | 
46  | 103k  |             }  | 
47  | 0  |           else  | 
48  | 0  |             { | 
49  |  |               /* incomplete multibyte character */  | 
50  | 0  |               *puc = 0xfffd;  | 
51  | 0  |               return -2;  | 
52  | 0  |             }  | 
53  | 103k  |         }  | 
54  | 125k  |       else if (c < 0xf0)  | 
55  | 100k  |         { | 
56  | 100k  |           if (n >= 2)  | 
57  | 100k  |             { | 
58  | 100k  |               if ((s[1] ^ 0x80) < 0x40  | 
59  | 100k  |                   && (c >= 0xe1 || s[1] >= 0xa0)  | 
60  | 100k  |                   && (c != 0xed || s[1] < 0xa0))  | 
61  | 100k  |                 { | 
62  | 100k  |                   if (n >= 3)  | 
63  | 100k  |                     { | 
64  | 100k  |                       if ((s[2] ^ 0x80) < 0x40)  | 
65  | 100k  |                         { | 
66  | 100k  |                           *puc = ((unsigned int) (c & 0x0f) << 12)  | 
67  | 100k  |                                  | ((unsigned int) (s[1] ^ 0x80) << 6)  | 
68  | 100k  |                                  | (unsigned int) (s[2] ^ 0x80);  | 
69  | 100k  |                           return 3;  | 
70  | 100k  |                         }  | 
71  |  |                       /* invalid multibyte character */  | 
72  | 100k  |                     }  | 
73  | 0  |                   else  | 
74  | 0  |                     { | 
75  |  |                       /* incomplete multibyte character */  | 
76  | 0  |                       *puc = 0xfffd;  | 
77  | 0  |                       return -2;  | 
78  | 0  |                     }  | 
79  | 100k  |                 }  | 
80  |  |               /* invalid multibyte character */  | 
81  | 100k  |             }  | 
82  | 0  |           else  | 
83  | 0  |             { | 
84  |  |               /* incomplete multibyte character */  | 
85  | 0  |               *puc = 0xfffd;  | 
86  | 0  |               return -2;  | 
87  | 0  |             }  | 
88  | 100k  |         }  | 
89  | 25.0k  |       else if (c <= 0xf4)  | 
90  | 24.5k  |         { | 
91  | 24.5k  |           if (n >= 2)  | 
92  | 24.5k  |             { | 
93  | 24.5k  |               if ((s[1] ^ 0x80) < 0x40  | 
94  | 24.4k  |                   && (c >= 0xf1 || s[1] >= 0x90)  | 
95  | 24.3k  |                   && (c < 0xf4 || (/* c == 0xf4 && */ s[1] < 0x90)))  | 
96  | 24.3k  |                 { | 
97  | 24.3k  |                   if (n >= 3)  | 
98  | 24.3k  |                     { | 
99  | 24.3k  |                       if ((s[2] ^ 0x80) < 0x40)  | 
100  | 24.1k  |                         { | 
101  | 24.1k  |                           if (n >= 4)  | 
102  | 24.1k  |                             { | 
103  | 24.1k  |                               if ((s[3] ^ 0x80) < 0x40)  | 
104  | 24.1k  |                                 { | 
105  | 24.1k  |                                   *puc = ((unsigned int) (c & 0x07) << 18)  | 
106  | 24.1k  |                                          | ((unsigned int) (s[1] ^ 0x80) << 12)  | 
107  | 24.1k  |                                          | ((unsigned int) (s[2] ^ 0x80) << 6)  | 
108  | 24.1k  |                                          | (unsigned int) (s[3] ^ 0x80);  | 
109  | 24.1k  |                                   return 4;  | 
110  | 24.1k  |                                 }  | 
111  |  |                               /* invalid multibyte character */  | 
112  | 24.1k  |                             }  | 
113  | 0  |                           else  | 
114  | 0  |                             { | 
115  |  |                               /* incomplete multibyte character */  | 
116  | 0  |                               *puc = 0xfffd;  | 
117  | 0  |                               return -2;  | 
118  | 0  |                             }  | 
119  | 24.1k  |                         }  | 
120  |  |                       /* invalid multibyte character */  | 
121  | 24.3k  |                     }  | 
122  | 0  |                   else  | 
123  | 0  |                     { | 
124  |  |                       /* incomplete multibyte character */  | 
125  | 0  |                       *puc = 0xfffd;  | 
126  | 0  |                       return -2;  | 
127  | 0  |                     }  | 
128  | 24.3k  |                 }  | 
129  |  |               /* invalid multibyte character */  | 
130  | 24.5k  |             }  | 
131  | 0  |           else  | 
132  | 0  |             { | 
133  |  |               /* incomplete multibyte character */  | 
134  | 0  |               *puc = 0xfffd;  | 
135  | 0  |               return -2;  | 
136  | 0  |             }  | 
137  | 24.5k  |         }  | 
138  | 228k  |     }  | 
139  |  |   /* invalid multibyte character */  | 
140  | 1.60k  |   *puc = 0xfffd;  | 
141  | 1.60k  |   return -1;  | 
142  | 528k  | }  |