| 1 | /* Charset name normalization. |
| 2 | Copyright (C) 2001-2021 Free Software Foundation, Inc. |
| 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 |
| 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. |
| 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 |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <ctype.h> |
| 21 | #include <locale.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <stdlib.h> |
| 26 | #include "gconv_int.h" |
| 27 | |
| 28 | |
| 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 copies in-order, characters from the source 's' that are |
| 52 | either alpha-numeric or one in one of these: "_-.,:/" - into the destination |
| 53 | 'wp' while dropping all other characters. In the process, it converts all |
| 54 | alphabetical characters to upper case. It then appends up to two '/' |
| 55 | characters so that the total number of '/'es in the destination is 2. */ |
| 56 | static inline void __attribute__ ((unused, always_inline)) |
| 57 | strip (char *wp, const char *s) |
| 58 | { |
| 59 | int slash_count = 0; |
| 60 | |
| 61 | while (*s != '\0') |
| 62 | { |
| 63 | if (__isalnum_l (*s, _nl_C_locobj_ptr) |
| 64 | || *s == '_' || *s == '-' || *s == '.' || *s == ',' || *s == ':') |
| 65 | *wp++ = __toupper_l (*s, _nl_C_locobj_ptr); |
| 66 | else if (*s == '/') |
| 67 | { |
| 68 | if (++slash_count == 3) |
| 69 | break; |
| 70 | *wp++ = '/'; |
| 71 | } |
| 72 | ++s; |
| 73 | } |
| 74 | |
| 75 | while (slash_count++ < 2) |
| 76 | *wp++ = '/'; |
| 77 | |
| 78 | *wp = '\0'; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static inline char * __attribute__ ((unused, always_inline)) |
| 83 | upstr (char *dst, const char *str) |
| 84 | { |
| 85 | char *cp = dst; |
| 86 | while ((*cp++ = __toupper_l (*str++, _nl_C_locobj_ptr)) != '\0') |
| 87 | /* nothing */; |
| 88 | return dst; |
| 89 | } |
| 90 | |