1 | /* Charset name normalization. |
2 | Copyright (C) 2001-2023 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 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 GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ctype.h> |
20 | #include <locale.h> |
21 | #include <stdbool.h> |
22 | #include <string.h> |
23 | #include <sys/stat.h> |
24 | #include <stdlib.h> |
25 | #include "gconv_int.h" |
26 | |
27 | |
28 | /* An iconv encoding is in the form of a triplet, with parts separated by |
29 | a '/' character. The first part is the standard name, the second part is |
30 | the character set, and the third part is the error handler. If the first |
31 | part is sufficient to identify both the standard and the character set |
32 | then the second part can be empty e.g. UTF-8//. If the first part is not |
33 | sufficient to identify both the standard and the character set then the |
34 | second part is required e.g. ISO-10646/UTF8/. If neither the first or |
35 | second parts are provided e.g. //, then the current locale is used. |
36 | The actual values used in the first and second parts are not entirely |
37 | relevant to the implementation. The values themselves are used in a hash |
38 | table to lookup modules and so the naming convention of the first two parts |
39 | is somewhat arbitrary and only helps locate the entries in the cache. |
40 | The third part is the error handler and is comprised of a ',' or '/' |
41 | separated list of suffixes. Currently, we support "TRANSLIT" for |
42 | transliteration and "IGNORE" for ignoring conversion errors due to |
43 | unrecognized input characters. */ |
44 | #define GCONV_TRIPLE_SEPARATOR "/" |
45 | #define GCONV_SUFFIX_SEPARATOR "," |
46 | #define GCONV_TRANSLIT_SUFFIX "TRANSLIT" |
47 | #define GCONV_IGNORE_ERRORS_SUFFIX "IGNORE" |
48 | |
49 | |
50 | /* This function copies in-order, characters from the source 's' that are |
51 | either alphanumeric or one in one of these: "_-.,:/" - into the destination |
52 | 'wp' while dropping all other characters. In the process, it converts all |
53 | alphabetical characters to upper case. It then appends up to two '/' |
54 | characters so that the total number of '/'es in the destination is 2. */ |
55 | static inline void __attribute__ ((unused, always_inline)) |
56 | strip (char *wp, const char *s) |
57 | { |
58 | int slash_count = 0; |
59 | |
60 | while (*s != '\0') |
61 | { |
62 | if (__isalnum_l (*s, _nl_C_locobj_ptr) |
63 | || *s == '_' || *s == '-' || *s == '.' || *s == ',' || *s == ':') |
64 | *wp++ = __toupper_l (*s, _nl_C_locobj_ptr); |
65 | else if (*s == '/') |
66 | { |
67 | if (++slash_count == 3) |
68 | break; |
69 | *wp++ = '/'; |
70 | } |
71 | ++s; |
72 | } |
73 | |
74 | while (slash_count++ < 2) |
75 | *wp++ = '/'; |
76 | |
77 | *wp = '\0'; |
78 | } |
79 | |
80 | |
81 | static inline char * __attribute__ ((unused, always_inline)) |
82 | upstr (char *dst, const char *str) |
83 | { |
84 | char *cp = dst; |
85 | while ((*cp++ = __toupper_l (*str++, _nl_C_locobj_ptr)) != '\0') |
86 | /* nothing */; |
87 | return dst; |
88 | } |
89 | |