idnfkc.c

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 147 SCAR 0
CC
0
Cov
0%
CRAP 0
« Punycode.xs ↑ Summary pcodes.c »
1 /* nfkc.c --- Unicode normalization utilities.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Simon
3 * Josefsson
4 *
5 * This file is part of GNU Libidn.
6 *
7 * GNU Libidn is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * GNU Libidn is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with GNU Libidn; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 */
22 #include <stdlib.h>
23 #include <string.h>
24  
25 #include "spreps.h"
26  
27 /* This file contains functions from GLIB, including gutf8.c and
28 * gunidecomp.c, all licensed under LGPL and copyright hold by:
29 *
30 * Copyright (C) 1999, 2000 Tom Tromey
31 * Copyright 2000 Red Hat, Inc.
32 */
33  
34 /* Hacks to make syncing with GLIB code easier. */
35 #define gboolean int
36 #define gchar char
37 #define guchar unsigned char
38 #define glong long
39 #define gint int
40 #define guint unsigned int
41 #define gushort unsigned short
42 #define gint16 int16_t
43 #define guint16 uint16_t
44 #define gunichar uint32_t
45 #define gsize size_t
46 #define gssize ssize_t
47 #define g_malloc malloc
48 #define g_free free
49 #define g_return_val_if_fail(expr,val) { \
50 if (!(expr)) \
51 return (val); \
52 }
53  
54 /* Code from GLIB gmacros.h starts here. */
55  
56 /* GLIB - Library of useful routines for C programming
57 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
58 *
59 * This library is free software; you can redistribute it and/or
60 * modify it under the terms of the GNU Lesser General Public
61 * License as published by the Free Software Foundation; either
62 * version 2 of the License, or (at your option) any later version.
63 *
64 * This library is distributed in the hope that it will be useful,
65 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
67 * Lesser General Public License for more details.
68 *
69 * You should have received a copy of the GNU Lesser General Public
70 * License along with this library; if not, write to the
71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
72 * Boston, MA 02111-1307, USA.
73 */
74  
75 #define G_UNLIKELY(expr) (expr)
76  
77 /* Code from GLIB gunicode.h starts here. */
78  
79 /* gunicode.h - Unicode manipulation functions
80 *
81 * Copyright (C) 1999, 2000 Tom Tromey
82 * Copyright 2000, 2005 Red Hat, Inc.
83 *
84 * The Gnome Library is free software; you can redistribute it and/or
85 * modify it under the terms of the GNU Lesser General Public License as
86 * published by the Free Software Foundation; either version 2 of the
87 * License, or (at your option) any later version.
88 *
89 * The Gnome Library is distributed in the hope that it will be useful,
90 * but WITHOUT ANY WARRANTY; without even the implied warranty of
91 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
92 * Lesser General Public License for more details.
93 *
94 * You should have received a copy of the GNU Lesser General Public
95 * License along with the Gnome Library; see the file COPYING.LIB. If not,
96 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
97 * Boston, MA 02111-1307, USA.
98 */
99  
100 #define UTF8_LENGTH(Char) \
101 ((Char) < 0x80 ? 1 : \
102 ((Char) < 0x800 ? 2 : \
103 ((Char) < 0x10000 ? 3 : \
104 ((Char) < 0x200000 ? 4 : \
105 ((Char) < 0x4000000 ? 5 : 6)))))
106  
107 static const gchar utf8_skip_data[256] = {
108 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109 1, 1, 1, 1, 1, 1, 1,
110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
111 1, 1, 1, 1, 1, 1, 1,
112 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
113 1, 1, 1, 1, 1, 1, 1,
114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115 1, 1, 1, 1, 1, 1, 1,
116 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
117 1, 1, 1, 1, 1, 1, 1,
118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
119 1, 1, 1, 1, 1, 1, 1,
120 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
121 2, 2, 2, 2, 2, 2, 2,
122 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5,
123 5, 5, 5, 6, 6, 1, 1
124 };
125  
126 const gchar *const g_utf8_skip = utf8_skip_data;
127  
128 #define g_utf8_next_char(p) ((p) + g_utf8_skip[*(const guchar *)(p)])
129  
130 /*
131 * g_unichar_to_utf8:
132 * @c: a Unicode character code
133 * @outbuf: output buffer, must have at least 6 bytes of space.
134 * If %NULL, the length will be computed and returned
135 * and nothing will be written to @outbuf.
136 *
137 * Converts a single character to UTF-8.
138 *
139 * Return value: number of bytes written
140 **/
141 static int
142 0 g_unichar_to_utf8 (gunichar c, gchar * outbuf)
143 {
144 /* If this gets modified, also update the copy in g_string_insert_unichar() */
145 0 guint len = 0;
146 int first;
147 int i;
148  
149 0 ▶ if (c < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) 0 0 0
150 {
151 0 first = 0;
152 0 len = 1;
153 }
154 0 ▶ else if (c < 0x800)
Branch: 1 branch
BranchTrueFalseTotal
else if (c < 0x800) 0 0 0
155 {
156 0 first = 0xc0;
157 0 len = 2;
158 }
159 0 ▶ else if (c < 0x10000)
Branch: 1 branch
BranchTrueFalseTotal
else if (c < 0x10000) 0 0 0
160 {
161 0 first = 0xe0;
162 0 len = 3;
163 }
164 0 ▶ else if (c < 0x200000)
Branch: 1 branch
BranchTrueFalseTotal
else if (c < 0x200000) 0 0 0
165 {
166 0 first = 0xf0;
167 0 len = 4;
168 }
169 0 ▶ else if (c < 0x4000000)
Branch: 1 branch
BranchTrueFalseTotal
else if (c < 0x4000000) 0 0 0
170 {
171 0 first = 0xf8;
172 0 len = 5;
173 }
174 else
175 {
176 0 first = 0xfc;
177 0 len = 6;
178 }
179  
180 0 ▶ if (outbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (outbuf) 0 0 0
181 {
182 0 ▶ for (i = len - 1; i > 0; --i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = len - 1; i > 0; --i) 0 0 0
183 {
184 0 outbuf[i] = (c & 0x3f) | 0x80;
185 0 c >>= 6;
186 }
187 0 outbuf[0] = c | first;
188 }
189  
190 0 return len;
191 }
192  
193 /*
194 * g_utf8_to_ucs4_fast:
195 * @str: a UTF-8 encoded string
196 * @len: the maximum length of @str to use, in bytes. If @len < 0,
197 * then the string is nul-terminated.
198 * @items_written: location to store the number of characters in the
199 * result, or %NULL.
200 *
201 * Convert a string from UTF-8 to a 32-bit fixed width
202 * representation as UCS-4, assuming valid UTF-8 input.
203 * This function is roughly twice as fast as g_utf8_to_ucs4()
204 * but does no error checking on the input. A trailing 0 character
205 * will be added to the string after the converted text.
206 *
207 * Return value: a pointer to a newly allocated UCS-4 string.
208 * This value must be freed with g_free().
209 **/
210 static gunichar *
211 0 g_utf8_to_ucs4_fast (const gchar * str, glong len, glong * items_written)
212 {
213 gunichar *result;
214 gsize n_chars, i;
215 const gchar *p;
216  
217 0 ▶ g_return_val_if_fail (str != NULL, NULL);
Branch: 1 branch
BranchTrueFalseTotal
g_return_val_if_fail (str != NULL, NULL); 0 0 0
218  
219 0 p = str;
220 0 n_chars = 0;
221 0 ▶ if (len < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) 0 0 0
222 {
223 0 ▶ while (*p)
Branch: 1 branch
BranchTrueFalseTotal
while (*p) 0 0 0
224 {
225 0 p = g_utf8_next_char (p);
226 0 ++n_chars;
227 }
228 }
229 else
230 {
231 0 ▶ while (p < str + len && *p)
Branch: 2 branches
BranchTrueFalseTotal
while (p < str + len && *p) 0 0 0
while (p < str + len && *p) 0 0 0
232 {
233 0 p = g_utf8_next_char (p);
234 0 ++n_chars;
235 }
236 }
237  
238 0 result = g_malloc (sizeof (gunichar) * (n_chars + 1));
239 0 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 0 0
240 0 return NULL;
241  
242 0 p = str;
243 0 ▶ for (i = 0; i < n_chars; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_chars; i++) 0 0 0
244 {
245 0 gunichar wc = (guchar) * p++;
246  
247 0 ▶ if (wc < 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (wc < 0x80) 0 0 0
248 {
249 0 result[i] = wc;
250 }
251 else
252 {
253 0 gunichar mask = 0x40;
254  
255 0 ▶ if (G_UNLIKELY ((wc & mask) == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (G_UNLIKELY ((wc & mask) == 0)) 0 0 0
256 {
257 /* It's an out-of-sequence 10xxxxxxx byte.
258 * Rather than making an ugly hash of this and the next byte
259 * and overrunning the buffer, it's more useful to treat it
260 * with a replacement character */
261 0 result[i] = 0xfffd;
262 0 continue;
263 }
264  
265 do
266 {
267 0 wc <<= 6;
268 0 wc |= (guchar) (*p++) & 0x3f;
269 0 mask <<= 5;
270 }
271 0 ▶ while ((wc & mask) != 0);
Branch: 1 branch
BranchTrueFalseTotal
while ((wc & mask) != 0); 0 0 0
272  
273 0 wc &= mask - 1;
274  
275 0 result[i] = wc;
276 }
277 }
278 0 result[i] = 0;
279  
280 0 ▶ if (items_written)
Branch: 1 branch
BranchTrueFalseTotal
if (items_written) 0 0 0
281 0 *items_written = i;
282  
283 0 return result;
284 }
285  
286 /*
287 * g_ucs4_to_utf8:
288 * @str: a UCS-4 encoded string
289 * @len: the maximum length (number of characters) of @str to use.
290 * If @len < 0, then the string is nul-terminated.
291 * @items_read: location to store number of characters read, or %NULL.
292 * @items_written: location to store number of bytes written or %NULL.
293 * The value here stored does not include the trailing 0
294 * byte.
295 * @error: location to store the error occurring, or %NULL to ignore
296 * errors. Any of the errors in #GConvertError other than
297 * %G_CONVERT_ERROR_NO_CONVERSION may occur.
298 *
299 * Convert a string from a 32-bit fixed width representation as UCS-4.
300 * to UTF-8. The result will be terminated with a 0 byte.
301 *
302 * Return value: a pointer to a newly allocated UTF-8 string.
303 * This value must be freed with g_free(). If an
304 * error occurs, %NULL will be returned and
305 * @error set. In that case, @items_read will be
306 * set to the position of the first invalid input
307 * character.
308 **/
309 static gchar *
310 0 g_ucs4_to_utf8 (const gunichar * str,
311 glong len,
312 glong * items_read, glong * items_written)
313 {
314 gint result_length;
315 0 gchar *result = NULL;
316 gchar *p;
317 gint i;
318  
319 0 result_length = 0;
320 0 ▶ for (i = 0; len < 0 || i < len; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; len < 0 || i < len; i++) 0 0 0
for (i = 0; len < 0 || i < len; i++) 0 0 0
321 {
322 0 ▶ if (!str[i])
Branch: 1 branch
BranchTrueFalseTotal
if (!str[i]) 0 0 0
323 0 break;
324  
325 0 ▶ if (str[i] >= 0x80000000)
Branch: 1 branch
BranchTrueFalseTotal
if (str[i] >= 0x80000000) 0 0 0
326 0 goto err_out;
327  
328 0 ▶ result_length += UTF8_LENGTH (str[i]);
Branch: 5 branches
BranchTrueFalseTotal
result_length += UTF8_LENGTH (str[i]); 0 0 0
result_length += UTF8_LENGTH (str[i]); 0 0 0
result_length += UTF8_LENGTH (str[i]); 0 0 0
result_length += UTF8_LENGTH (str[i]); 0 0 0
result_length += UTF8_LENGTH (str[i]); 0 0 0
329 }
330  
331 0 result = g_malloc (result_length + 1);
332 0 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 0 0
333 0 return NULL;
334 0 p = result;
335  
336 0 i = 0;
337 0 ▶ while (p < result + result_length)
Branch: 1 branch
BranchTrueFalseTotal
while (p < result + result_length) 0 0 0
338 0 p += g_unichar_to_utf8 (str[i++], p);
339  
340 0 *p = '\0';
341  
342 0 ▶ if (items_written)
Branch: 1 branch
BranchTrueFalseTotal
if (items_written) 0 0 0
343 0 *items_written = p - result;
344  
345 0 err_out:
346 0 ▶ if (items_read)
Branch: 1 branch
BranchTrueFalseTotal
if (items_read) 0 0 0
347 0 *items_read = i;
348  
349 0 return result;
350 }
351  
352 /**
353 * stringprep_utf8_to_ucs4:
354 * @str: a UTF-8 encoded string
355 * @len: the maximum length of @str to use. If @len < 0, then
356 * the string is nul-terminated.
357 * @items_written: location to store the number of characters in the
358 * result, or %NULL.
359 *
360 * Convert a string from UTF-8 to a 32-bit fixed width
361 * representation as UCS-4, assuming valid UTF-8 input.
362 * This function does no error checking on the input.
363 *
364 * Return value: a pointer to a newly allocated UCS-4 string.
365 * This value must be deallocated by the caller.
366 **/
367 uint32_t *
368 0 stringprep_utf8_to_ucs4 (const char *str, ssize_t len, size_t * items_written)
369 {
370 0 return g_utf8_to_ucs4_fast (str, (glong) len, (glong *) items_written);
371 }
372  
373 /**
374 * stringprep_ucs4_to_utf8:
375 * @str: a UCS-4 encoded string
376 * @len: the maximum length of @str to use. If @len < 0, then
377 * the string is terminated with a 0 character.
378 * @items_read: location to store number of characters read read, or %NULL.
379 * @items_written: location to store number of bytes written or %NULL.
380 * The value here stored does not include the trailing 0
381 * byte.
382 *
383 * Convert a string from a 32-bit fixed width representation as UCS-4.
384 * to UTF-8. The result will be terminated with a 0 byte.
385 *
386 * Return value: a pointer to a newly allocated UTF-8 string.
387 * This value must be deallocated by the caller.
388 * If an error occurs, %NULL will be returned.
389 **/
390 char *
391 0 stringprep_ucs4_to_utf8 (const uint32_t * str, ssize_t len,
392 size_t * items_read, size_t * items_written)
393 {
394 0 return g_ucs4_to_utf8 (str, len, (glong *) items_read,
395 (glong *) items_written);
396 }
« Punycode.xs ↑ Summary pcodes.c »