Debian Codegraph @

src/utf8.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1996-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1996.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23
24 /* Read next data byte and check its value, discard an illegal sequence.
25 This macro is meant to be used only within the `while' loop in
26 `transform_utf8_ucs[24]'. */
27 #define GET_DATA_BYTE \
28 character = recode_get_byte (subtask); \
29 if (character == EOF) \
30 { \
31 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask); \
32 break; \
33 } \
34 else if ((BIT_MASK (2) << 6 & character) != 1 << 7) \
35 { \
36 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask); \
37 continue; \
38 } \
39 else
40
41 /* Read next data byte and check its value, discard an illegal sequence.
42 Merge it into `value' at POSITION. This macro is meant to be used only
43 within the `while' loop in `transform_utf8_ucs[24]'. */
44 #define GET_DATA_BYTE_AT(Position) \
45 GET_DATA_BYTE /* ... else */ value |= (BIT_MASK (6) & character) << Position
46
47 static bool
48 transform_ucs2_utf8 (RECODE_SUBTASK subtask)
49 {
50 unsigned value;
51
52 while (recode_get_ucs2 (&value, subtask))
53 {
54 if (value & ~BIT_MASK (7))
55 if (value & ~BIT_MASK (11))
56 {
57 /* 3 bytes - more than 11 bits, but not more than 16. */
58 recode_put_byte ((BIT_MASK (3) << 5) | (BIT_MASK (6) & value >> 12), subtask);
59 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 6), subtask);
60 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
61 }
62 else
63 {
64 /* 2 bytes - more than 7 bits, but not more than 11. */
65 recode_put_byte ((BIT_MASK (2) << 6) | (BIT_MASK (6) & value >> 6), subtask);
66 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
67 }
68 else
69 /* 1 byte - not more than 7 bits (that is, ASCII). */
70 recode_put_byte (value, subtask);
71 }
72
73 SUBTASK_RETURN (subtask);
74 }
75
76 static bool
77 transform_ucs4_utf8 (RECODE_SUBTASK subtask)
78 {
79 unsigned value;
80
81 while (recode_get_ucs4 (&value, subtask))
82 if (value & ~BIT_MASK (16))
83 if (value & ~BIT_MASK (26))
84 if (value & ~BIT_MASK (31))
85 {
86 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
87 }
88 else
89 {
90 /* 6 bytes - more than 26 bits, but not more than 31. */
91 recode_put_byte ((BIT_MASK (6) << 2) | (BIT_MASK (6) & value >> 30), subtask);
92 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 24), subtask);
93 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 18), subtask);
94 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 12), subtask);
95 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 6), subtask);
96 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
97 }
98 else if (value & ~BIT_MASK (21))
99 {
100 /* 5 bytes - more than 21 bits, but not more than 26. */
101 recode_put_byte ((BIT_MASK (5) << 3) | (BIT_MASK (6) & value >> 24), subtask);
102 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 18), subtask);
103 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 12), subtask);
104 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 6), subtask);
105 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
106 }
107 else
108 {
109 /* 4 bytes - more than 16 bits, but not more than 21. */
110 recode_put_byte ((BIT_MASK (4) << 4) | (BIT_MASK (6) & value >> 18), subtask);
111 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 12), subtask);
112 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 6), subtask);
113 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
114 }
115 else if (value & ~BIT_MASK (7))
116 if (value & ~BIT_MASK (11))
117 {
118 /* 3 bytes - more than 11 bits, but not more than 16. */
119 recode_put_byte ((BIT_MASK (3) << 5) | (BIT_MASK (6) & value >> 12), subtask);
120 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value >> 6), subtask);
121 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
122 }
123 else
124 {
125 /* 2 bytes - more than 7 bits, but not more than 11. */
126 recode_put_byte ((BIT_MASK (2) << 6) | (BIT_MASK (6) & value >> 6), subtask);
127 recode_put_byte ((1 << 7) | (BIT_MASK (6) & value), subtask);
128 }
129 else
130 /* 1 byte - not more than 7 bits (that is, ASCII). */
131 recode_put_byte (value, subtask);
132
133 SUBTASK_RETURN (subtask);
134 }
135
136 /* FIXME: The UTF-8 decoding algorithms do not validate that the minimum
137 length surface was indeed used. This would be necessary for ensuring
138 that the recoding is exactly reversible. In fact, this minimum length
139 surface is also a requirement of UTF-8 specification. */
140
141 static bool
142 transform_utf8_ucs4 (RECODE_SUBTASK subtask)
143 {
144 int character = recode_get_byte (subtask);
145 unsigned value;
146
147 while (character != EOF)
148
149 /* Process one UTF-8 value. EOF is acceptable on first byte only. */
150
151 if ((character & BIT_MASK (4) << 4) == BIT_MASK (4) << 4)
152 if ((character & BIT_MASK (6) << 2) == BIT_MASK (6) << 2)
153 if ((character & BIT_MASK (7) << 1) == BIT_MASK (7) << 1)
154 {
155 /* 7 bytes - more than 31 bits (that is, exactly 32 :-). */
156 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
157 character = recode_get_byte (subtask);
158 }
159 else
160 {
161 /* 6 bytes - more than 26 bits, but not more than 31. */
162 value = (BIT_MASK (1) & character) << 30;
163 GET_DATA_BYTE_AT (24);
164 GET_DATA_BYTE_AT (18);
165 GET_DATA_BYTE_AT (12);
166 GET_DATA_BYTE_AT (6);
167 GET_DATA_BYTE_AT (0);
168 recode_put_ucs4 (value, subtask);
169 character = recode_get_byte (subtask);
170 }
171 else if ((character & BIT_MASK (5) << 3) == BIT_MASK (5) << 3)
172 {
173 /* 5 bytes - more than 21 bits, but not more than 26. */
174 value = (BIT_MASK (2) & character) << 24;
175 GET_DATA_BYTE_AT (18);
176 GET_DATA_BYTE_AT (12);
177 GET_DATA_BYTE_AT (6);
178 GET_DATA_BYTE_AT (0);
179 recode_put_ucs4 (value, subtask);
180 character = recode_get_byte (subtask);
181 }
182 else
183 {
184 /* 4 bytes - more than 16 bits, but not more than 21. */
185 value = (BIT_MASK (3) & character) << 18;
186 GET_DATA_BYTE_AT (12);
187 GET_DATA_BYTE_AT (6);
188 GET_DATA_BYTE_AT (0);
189 recode_put_ucs4 (value, subtask);
190 character = recode_get_byte (subtask);
191 }
192 else if ((character & BIT_MASK (2) << 6) == BIT_MASK (2) << 6)
193 if ((character & BIT_MASK (3) << 5) == BIT_MASK (3) << 5)
194 {
195 /* 3 bytes - more than 11 bits, but not more than 16. */
196 value = (BIT_MASK (4) & character) << 12;
197 GET_DATA_BYTE_AT (6);
198 GET_DATA_BYTE_AT (0);
199 recode_put_ucs4 (value, subtask);
200 character = recode_get_byte (subtask);
201 }
202 else
203 {
204 /* 2 bytes - more than 7 bits, but not more than 11. */
205 value = (BIT_MASK (5) & character) << 6;
206 GET_DATA_BYTE_AT (0);
207 recode_put_ucs4 (value, subtask);
208 character = recode_get_byte (subtask);
209 }
210 else if ((character & 1 << 7) == 1 << 7)
211 {
212 /* Valid only as a continuation byte. */
213 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
214 character = recode_get_byte (subtask);
215 }
216 else
217 {
218 /* 1 byte - not more than 7 bits (that is, ASCII). */
219 recode_put_ucs4 (BIT_MASK (8) & character, subtask);
220 character = recode_get_byte (subtask);
221 }
222
223 SUBTASK_RETURN (subtask);
224 }
225
226 bool
227 module_utf8 (RECODE_OUTER outer)
228 {
229 return
230 recode_declare_single (outer, "ISO-10646-UCS-4", "UTF-8",
231 outer->quality_variable_to_variable,
232 NULL, transform_ucs4_utf8)
233 && recode_declare_single (outer, "UTF-8", "ISO-10646-UCS-4",
234 outer->quality_variable_to_variable,
235 NULL, transform_utf8_ucs4)
236
237 && recode_declare_alias (outer, "UTF-2", "UTF-8")
238 && recode_declare_alias (outer, "UTF-FSS", "UTF-8")
239 && recode_declare_alias (outer, "FSS_UTF", "UTF-8")
240 && recode_declare_alias (outer, "TF-8", "UTF-8")
241 && recode_declare_alias (outer, "u8", "UTF-8")
242
243 /* Simple UCS-2 does not have to go through UTF-16. */
244 && recode_declare_single (outer, "ISO-10646-UCS-2", "UTF-8",
245 outer->quality_variable_to_variable,
246 NULL, transform_ucs2_utf8);
247 }
248
249 void
250 delmodule_utf8 (_GL_UNUSED RECODE_OUTER outer)
251 {
252 }