Debian Codegraph @

src/utf16.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 static bool
25 transform_ucs4_utf16 (RECODE_SUBTASK subtask)
26 {
27 unsigned value;
28
29 if (recode_get_ucs4 (&value, subtask))
30 {
31 if (subtask->task->byte_order_mark)
32 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
33
34 while (true)
35 {
36 if (value & ~BIT_MASK (16))
37 if (value < (1 << 16 | 1 << 20))
38 {
39 /* Double UCS-2 character. */
40
41 value -= 1 << 16;
42 recode_put_ucs2 (0xD800 | (BIT_MASK (10) & value >> 10), subtask);
43 recode_put_ucs2 (0xDC00 | (BIT_MASK (10) & value), subtask);
44 }
45 else
46 {
47 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
48 recode_put_ucs2 (REPLACEMENT_CHARACTER, subtask);
49 }
50 else
51 {
52 /* Single UCS-2 character. */
53
54 if (value >= 0xD800 && value < 0xE000)
55 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
56 recode_put_ucs2 (value, subtask);
57 }
58
59 if (!recode_get_ucs4 (&value, subtask))
60 break;
61 }
62 }
63
64 SUBTASK_RETURN (subtask);
65 }
66
67 static bool
68 transform_utf16_ucs4 (RECODE_SUBTASK subtask)
69 {
70 unsigned value;
71
72 if (recode_get_ucs2 (&value, subtask))
73 {
74 while (true)
75 if (value >= 0xD800 && value < 0xE000)
76 if (value < 0xDC00)
77 {
78 unsigned chunk;
79
80 if (!recode_get_ucs2 (&chunk, subtask))
81 break;
82
83 if (chunk >= 0xDC00 && chunk < 0xE000)
84 {
85 recode_put_ucs4 ((((1 << 16) + ((value - 0xD800) << 10))
86 | (chunk - 0xDC00)),
87 subtask);
88 if (!recode_get_ucs2 (&value, subtask))
89 break;
90 }
91 else
92 {
93 /* Discard the first chunk if the pair is invalid. */
94
95 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
96 value = chunk;
97 }
98 }
99 else
100 {
101 /* Discard a second chunk when presented first. */
102
103 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
104 if (!recode_get_ucs2 (&value, subtask))
105 break;
106 }
107 else
108 {
109 recode_put_ucs4 (value, subtask);
110 if (!recode_get_ucs2 (&value, subtask))
111 break;
112 }
113 }
114
115 SUBTASK_RETURN (subtask);
116 }
117
118 static bool
119 transform_ucs2_utf16 (RECODE_SUBTASK subtask)
120 {
121 unsigned value;
122
123 /* This function does nothing, besides checking that the number of input
124 bytes is even, and that special UTF-16 values do not appear. */
125
126 while (recode_get_ucs2 (&value, subtask))
127 {
128 if (value >= 0xD800 && value < 0xE000)
129 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
130 recode_put_ucs2 (value, subtask);
131 }
132
133 SUBTASK_RETURN (subtask);
134 }
135
136 static bool
137 transform_utf16_ucs2 (RECODE_SUBTASK subtask)
138 {
139 unsigned value;
140
141 /* This function does almost nothing, besides checking that the input is
142 valid UTF-16, and replacing UTF-16 extended values with the replacement
143 character. */
144
145 if (recode_get_ucs2 (&value, subtask))
146 {
147 if (subtask->task->byte_order_mark)
148 recode_put_ucs2 (BYTE_ORDER_MARK, subtask);
149
150 while (true)
151 if (value >= 0xD800 && value < 0xE000)
152 if (value < 0xDC00)
153 {
154 unsigned chunk;
155
156 if (!recode_get_ucs2 (&chunk, subtask))
157 break;
158
159 if (chunk >= 0xDC00 && chunk < 0xE000)
160 {
161 RETURN_IF_NOGO (RECODE_UNTRANSLATABLE, subtask);
162 recode_put_ucs2 (REPLACEMENT_CHARACTER, subtask);
163 if (!recode_get_ucs2 (&value, subtask))
164 break;
165 }
166 else
167 {
168 /* Discard the first chunk if the pair is invalid. */
169
170 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
171 value = chunk;
172 }
173 }
174 else
175 {
176 /* Discard a second chunk when presented first. */
177
178 RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask);
179 if (!recode_get_ucs2 (&value, subtask))
180 break;
181 }
182 else
183 {
184 recode_put_ucs2 (value, subtask);
185 if (!recode_get_ucs2 (&value, subtask))
186 break;
187 }
188 }
189
190 SUBTASK_RETURN (subtask);
191 }
192
193 bool
194 module_utf16 (RECODE_OUTER outer)
195 {
196 return
197 recode_declare_single (outer, "ISO-10646-UCS-4", "UTF-16",
198 outer->quality_variable_to_variable,
199 NULL, transform_ucs4_utf16)
200 && recode_declare_single (outer, "UTF-16", "ISO-10646-UCS-4",
201 outer->quality_variable_to_variable,
202 NULL, transform_utf16_ucs4)
203 && recode_declare_single (outer, "ISO-10646-UCS-2", "UTF-16",
204 outer->quality_variable_to_variable,
205 NULL, transform_ucs2_utf16)
206 && recode_declare_single (outer, "UTF-16", "ISO-10646-UCS-2",
207 outer->quality_variable_to_variable,
208 NULL, transform_utf16_ucs2)
209
210 && recode_declare_alias (outer, "Unicode", "UTF-16")
211 && recode_declare_alias (outer, "TF-16", "UTF-16")
212 && recode_declare_alias (outer, "u6", "UTF-16");
213 }
214
215 void
216 delmodule_utf16 (_GL_UNUSED RECODE_OUTER outer)
217 {
218 }