/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-2022 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 | { |
57 | 0 | if ((s[2] ^ 0x80) < 0x40) |
58 | 0 | { |
59 | 0 | if ((c >= 0xe1 || s[1] >= 0xa0) |
60 | 0 | && (c != 0xed || s[1] < 0xa0)) |
61 | 0 | { |
62 | 0 | *puc = ((unsigned int) (c & 0x0f) << 12) |
63 | 0 | | ((unsigned int) (s[1] ^ 0x80) << 6) |
64 | 0 | | (unsigned int) (s[2] ^ 0x80); |
65 | 0 | return 3; |
66 | 0 | } |
67 | | /* invalid multibyte character */ |
68 | 0 | *puc = 0xfffd; |
69 | 0 | return 3; |
70 | 0 | } |
71 | | /* invalid multibyte character */ |
72 | 0 | *puc = 0xfffd; |
73 | 0 | return 2; |
74 | 0 | } |
75 | | /* invalid multibyte character */ |
76 | 0 | } |
77 | 0 | else |
78 | 0 | { |
79 | | /* incomplete multibyte character */ |
80 | 0 | *puc = 0xfffd; |
81 | 0 | if (n == 1 || (s[1] ^ 0x80) >= 0x40) |
82 | 0 | return 1; |
83 | 0 | else |
84 | 0 | return 2; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | else if (c < 0xf8) |
88 | 0 | { |
89 | 0 | if (n >= 4) |
90 | 0 | { |
91 | 0 | if ((s[1] ^ 0x80) < 0x40) |
92 | 0 | { |
93 | 0 | if ((s[2] ^ 0x80) < 0x40) |
94 | 0 | { |
95 | 0 | if ((s[3] ^ 0x80) < 0x40) |
96 | 0 | { |
97 | 0 | if ((c >= 0xf1 || s[1] >= 0x90) |
98 | 0 | && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))) |
99 | 0 | { |
100 | 0 | *puc = ((unsigned int) (c & 0x07) << 18) |
101 | 0 | | ((unsigned int) (s[1] ^ 0x80) << 12) |
102 | 0 | | ((unsigned int) (s[2] ^ 0x80) << 6) |
103 | 0 | | (unsigned int) (s[3] ^ 0x80); |
104 | 0 | return 4; |
105 | 0 | } |
106 | | /* invalid multibyte character */ |
107 | 0 | *puc = 0xfffd; |
108 | 0 | return 4; |
109 | 0 | } |
110 | | /* invalid multibyte character */ |
111 | 0 | *puc = 0xfffd; |
112 | 0 | return 3; |
113 | 0 | } |
114 | | /* invalid multibyte character */ |
115 | 0 | *puc = 0xfffd; |
116 | 0 | return 2; |
117 | 0 | } |
118 | | /* invalid multibyte character */ |
119 | 0 | } |
120 | 0 | else |
121 | 0 | { |
122 | | /* incomplete multibyte character */ |
123 | 0 | *puc = 0xfffd; |
124 | 0 | if (n == 1 || (s[1] ^ 0x80) >= 0x40) |
125 | 0 | return 1; |
126 | 0 | else if (n == 2 || (s[2] ^ 0x80) >= 0x40) |
127 | 0 | return 2; |
128 | 0 | else |
129 | 0 | return 3; |
130 | 0 | } |
131 | 0 | } |
132 | 0 | } |
133 | | /* invalid multibyte character */ |
134 | 0 | *puc = 0xfffd; |
135 | 0 | return 1; |
136 | 0 | } |
137 | | |
138 | | #endif |