/src/libunistring/lib/unistr/u8-mbtouc-unsafe-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-2023 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_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n) |
27 | 6.13M | { |
28 | 6.13M | uint8_t c = *s; |
29 | | |
30 | 6.13M | if (c >= 0xc2) |
31 | 6.10M | { |
32 | 6.10M | if (c < 0xe0) |
33 | 5.01M | { |
34 | 5.01M | if (n >= 2) |
35 | 5.01M | { |
36 | 5.01M | if ((s[1] ^ 0x80) < 0x40) |
37 | 4.82M | { |
38 | 4.82M | *puc = ((unsigned int) (c & 0x1f) << 6) |
39 | 4.82M | | (unsigned int) (s[1] ^ 0x80); |
40 | 4.82M | return 2; |
41 | 4.82M | } |
42 | | /* invalid multibyte character */ |
43 | 5.01M | } |
44 | 0 | else |
45 | 0 | { |
46 | | /* incomplete multibyte character */ |
47 | 0 | *puc = 0xfffd; |
48 | 0 | return 1; |
49 | 0 | } |
50 | 5.01M | } |
51 | 1.08M | else if (c < 0xf0) |
52 | 814k | { |
53 | 814k | if (n >= 3) |
54 | 814k | { |
55 | 814k | if ((s[1] ^ 0x80) < 0x40) |
56 | 806k | { |
57 | 806k | if ((s[2] ^ 0x80) < 0x40) |
58 | 805k | { |
59 | 805k | if ((c >= 0xe1 || s[1] >= 0xa0) |
60 | 805k | && (c != 0xed || s[1] < 0xa0)) |
61 | 804k | { |
62 | 804k | *puc = ((unsigned int) (c & 0x0f) << 12) |
63 | 804k | | ((unsigned int) (s[1] ^ 0x80) << 6) |
64 | 804k | | (unsigned int) (s[2] ^ 0x80); |
65 | 804k | return 3; |
66 | 804k | } |
67 | | /* invalid multibyte character */ |
68 | 598 | *puc = 0xfffd; |
69 | 598 | return 3; |
70 | 805k | } |
71 | | /* invalid multibyte character */ |
72 | 1.30k | *puc = 0xfffd; |
73 | 1.30k | return 2; |
74 | 806k | } |
75 | | /* invalid multibyte character */ |
76 | 814k | } |
77 | 56 | else |
78 | 56 | { |
79 | | /* incomplete multibyte character */ |
80 | 56 | *puc = 0xfffd; |
81 | 56 | if (n == 1 || (s[1] ^ 0x80) >= 0x40) |
82 | 56 | return 1; |
83 | 0 | else |
84 | 0 | return 2; |
85 | 56 | } |
86 | 814k | } |
87 | 268k | else if (c < 0xf8) |
88 | 14.0k | { |
89 | 14.0k | if (n >= 4) |
90 | 13.9k | { |
91 | 13.9k | if ((s[1] ^ 0x80) < 0x40) |
92 | 6.26k | { |
93 | 6.26k | if ((s[2] ^ 0x80) < 0x40) |
94 | 4.96k | { |
95 | 4.96k | if ((s[3] ^ 0x80) < 0x40) |
96 | 4.38k | { |
97 | 4.38k | if ((c >= 0xf1 || s[1] >= 0x90) |
98 | 4.38k | && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90)) |
99 | 4.38k | ) |
100 | 3.77k | { |
101 | 3.77k | *puc = ((unsigned int) (c & 0x07) << 18) |
102 | 3.77k | | ((unsigned int) (s[1] ^ 0x80) << 12) |
103 | 3.77k | | ((unsigned int) (s[2] ^ 0x80) << 6) |
104 | 3.77k | | (unsigned int) (s[3] ^ 0x80); |
105 | 3.77k | return 4; |
106 | 3.77k | } |
107 | | /* invalid multibyte character */ |
108 | 614 | *puc = 0xfffd; |
109 | 614 | return 4; |
110 | 4.38k | } |
111 | | /* invalid multibyte character */ |
112 | 578 | *puc = 0xfffd; |
113 | 578 | return 3; |
114 | 4.96k | } |
115 | | /* invalid multibyte character */ |
116 | 1.30k | *puc = 0xfffd; |
117 | 1.30k | return 2; |
118 | 6.26k | } |
119 | | /* invalid multibyte character */ |
120 | 13.9k | } |
121 | 68 | else |
122 | 68 | { |
123 | | /* incomplete multibyte character */ |
124 | 68 | *puc = 0xfffd; |
125 | 68 | if (n == 1 || (s[1] ^ 0x80) >= 0x40) |
126 | 57 | return 1; |
127 | 11 | else if (n == 2 || (s[2] ^ 0x80) >= 0x40) |
128 | 11 | return 2; |
129 | 0 | else |
130 | 0 | return 3; |
131 | 68 | } |
132 | 14.0k | } |
133 | 6.10M | } |
134 | | /* invalid multibyte character */ |
135 | 496k | *puc = 0xfffd; |
136 | 496k | return 1; |
137 | 6.13M | } |
138 | | |
139 | | #endif |