]>
Commit | Line | Data |
---|---|---|
e7f21fa6 | 1 | /* Charset name normalization. |
d614a753 | 2 | Copyright (C) 2001-2020 Free Software Foundation, Inc. |
e7f21fa6 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2001. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
e7f21fa6 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
e7f21fa6 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
e7f21fa6 UD |
19 | |
20 | #include <ctype.h> | |
5db91571 | 21 | #include <locale.h> |
91927b7c AS |
22 | #include <stdbool.h> |
23 | #include <string.h> | |
24 | #include <sys/stat.h> | |
25 | #include <stdlib.h> | |
26 | #include "gconv_int.h" | |
e7f21fa6 UD |
27 | |
28 | ||
91927b7c AS |
29 | /* An iconv encoding is in the form of a triplet, with parts separated by |
30 | a '/' character. The first part is the standard name, the second part is | |
31 | the character set, and the third part is the error handler. If the first | |
32 | part is sufficient to identify both the standard and the character set | |
33 | then the second part can be empty e.g. UTF-8//. If the first part is not | |
34 | sufficient to identify both the standard and the character set then the | |
35 | second part is required e.g. ISO-10646/UTF8/. If neither the first or | |
36 | second parts are provided e.g. //, then the current locale is used. | |
37 | The actual values used in the first and second parts are not entirely | |
38 | relevant to the implementation. The values themselves are used in a hash | |
39 | table to lookup modules and so the naming convention of the first two parts | |
40 | is somewhat arbitrary and only helps locate the entries in the cache. | |
41 | The third part is the error handler and is comprised of a ',' or '/' | |
42 | separated list of suffixes. Currently, we support "TRANSLIT" for | |
43 | transliteration and "IGNORE" for ignoring conversion errors due to | |
44 | unrecognized input characters. */ | |
45 | #define GCONV_TRIPLE_SEPARATOR "/" | |
46 | #define GCONV_SUFFIX_SEPARATOR "," | |
47 | #define GCONV_TRANSLIT_SUFFIX "TRANSLIT" | |
48 | #define GCONV_IGNORE_ERRORS_SUFFIX "IGNORE" | |
49 | ||
50 | ||
51 | /* This function accepts the charset names of the source and destination of the | |
52 | conversion and populates *conv_spec with an equivalent conversion | |
53 | specification that may later be used by __gconv_open. The charset names | |
54 | might contain options in the form of suffixes that alter the conversion, | |
55 | e.g. "ISO-10646/UTF-8/TRANSLIT". It processes the charset names, ignoring | |
56 | and truncating any suffix options in fromcode, and processing and truncating | |
57 | any suffix options in tocode. Supported suffix options ("TRANSLIT" or | |
58 | "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec | |
59 | to be set to true. Unrecognized suffix options are silently discarded. If | |
60 | the function succeeds, it returns conv_spec back to the caller. It returns | |
61 | NULL upon failure. */ | |
62 | struct gconv_spec * | |
63 | __gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode, | |
64 | const char *tocode); | |
65 | libc_hidden_proto (__gconv_create_spec) | |
66 | ||
67 | ||
68 | /* This function frees all heap memory allocated by __gconv_create_spec. */ | |
69 | static void __attribute__ ((unused)) | |
70 | gconv_destroy_spec (struct gconv_spec *conv_spec) | |
71 | { | |
72 | free (conv_spec->fromcode); | |
73 | free (conv_spec->tocode); | |
74 | return; | |
75 | } | |
76 | ||
77 | ||
78 | /* This function copies in-order, characters from the source 's' that are | |
79 | either alpha-numeric or one in one of these: "_-.,:/" - into the destination | |
80 | 'wp' while dropping all other characters. In the process, it converts all | |
81 | alphabetical characters to upper case. It then appends up to two '/' | |
82 | characters so that the total number of '/'es in the destination is 2. */ | |
83 | static inline void __attribute__ ((unused, always_inline)) | |
e7f21fa6 UD |
84 | strip (char *wp, const char *s) |
85 | { | |
86 | int slash_count = 0; | |
87 | ||
88 | while (*s != '\0') | |
89 | { | |
4b5b009c | 90 | if (__isalnum_l (*s, _nl_C_locobj_ptr) |
82faa177 | 91 | || *s == '_' || *s == '-' || *s == '.' || *s == ',' || *s == ':') |
4b5b009c | 92 | *wp++ = __toupper_l (*s, _nl_C_locobj_ptr); |
e7f21fa6 UD |
93 | else if (*s == '/') |
94 | { | |
95 | if (++slash_count == 3) | |
96 | break; | |
97 | *wp++ = '/'; | |
98 | } | |
99 | ++s; | |
100 | } | |
101 | ||
102 | while (slash_count++ < 2) | |
103 | *wp++ = '/'; | |
104 | ||
105 | *wp = '\0'; | |
106 | } | |
107 | ||
108 | ||
dd9423a6 | 109 | static inline char * __attribute__ ((unused, always_inline)) |
e7f21fa6 UD |
110 | upstr (char *dst, const char *str) |
111 | { | |
112 | char *cp = dst; | |
4b5b009c | 113 | while ((*cp++ = __toupper_l (*str++, _nl_C_locobj_ptr)) != '\0') |
e7f21fa6 UD |
114 | /* nothing */; |
115 | return dst; | |
116 | } |