Branch
Hash :
9f28054d
Author :
Date :
2024-12-15T12:52:18
Implement the //NON_IDENTICAL_DISCARD suffix from POSIX:2024. * include/iconv.h.in (ICONV_GET_DISCARD_INVALID, ICONV_SET_DISCARD_INVALID, ICONV_GET_DISCARD_NON_IDENTICAL, ICONV_SET_DISCARD_NON_IDENTICAL): New macros. * lib/converters.h (struct conv_struct): Change type of discard_ilseq to 'unsigned int'. (DISCARD_INVALID, DISCARD_UNCONVERTIBLE): New macros. * lib/iconv.c (iconv_open, iconv_open_into): Change type of discard_ilseq to 'unsigned int'. (iconvctl): Implement ICONV_GET_DISCARD_INVALID, ICONV_SET_DISCARD_INVALID, ICONV_GET_DISCARD_NON_IDENTICAL, ICONV_SET_DISCARD_NON_IDENTICAL. Change the implementation of ICONV_GET_DISCARD_ILSEQ, ICONV_SET_DISCARD_ILSEQ to test/set both bits. * lib/iconv_open1.h: Update comment. Recognize //NON_IDENTICAL_DISCARD. * lib/iconv_open2.h: Update comment. * lib/loop_unicode.h (mb_to_uc_write_replacement): Test the DISCARD_UNCONVERTIBLE bit of discard_ilseq. (unicode_loop_convert): Test the respective bit of discard_ilseq. (unicode_loop_reset): Test the DISCARD_UNCONVERTIBLE bit of discard_ilseq. * lib/loop_wchar.h (wchar_from_loop_convert, wchar_to_loop_convert): Test the DISCARD_INVALID bit of discard_ilseq. * man/iconv_open.3: Mention the //NON_IDENTICAL_DISCARD suffix. Mark as conforming to POSIX:2024. * man/iconv.3: Likewise. * man/iconv_close.3: Mark as conforming to POSIX:2024. * man/iconv.1: Likewise. * man/iconvctl.3: Document ICONV_GET_DISCARD_INVALID, ICONV_SET_DISCARD_INVALID, ICONV_GET_DISCARD_NON_IDENTICAL, ICONV_SET_DISCARD_NON_IDENTICAL. Revise the description of ICONV_GET_DISCARD_ILSEQ, ICONV_SET_DISCARD_ILSEQ. * tests/test-discard.c (test_default, test_translit, test_ignore, test_ignore_translit): Test also the ICONV_GET_DISCARD_INVALID, ICONV_GET_DISCARD_NON_IDENTICAL accessors. (test_nid, test_nid_translit, test_invd, test_invd_translit): New functions. (main): Add test cases with //NON_IDENTICAL_DISCARD suffix. * NEWS: Mention the change.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/*
* Copyright (C) 1999-2024 Free Software Foundation, Inc.
* This file is part of the GNU LIBICONV Library.
*
* The GNU LIBICONV Library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* The GNU LIBICONV Library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the GNU LIBICONV Library; see the file COPYING.LIB.
* If not, see <https://www.gnu.org/licenses/>.
*/
/* Part 1 of iconv_open.
Input: const char* tocode, const char* fromcode.
Output:
unsigned int from_index;
int from_wchar;
unsigned int from_surface;
unsigned int to_index;
int to_wchar;
unsigned int to_surface;
int transliterate;
unsigned int discard_ilseq;
Jumps to 'invalid' in case of error.
*/
{
char buf[MAX_WORD_LENGTH+9+9+1];
const char* cp;
char* bp;
const struct alias * ap;
unsigned int count;
from_surface = ICONV_SURFACE_NONE;
to_surface = ICONV_SURFACE_NONE;
transliterate = 0;
discard_ilseq = 0;
/* Before calling aliases_lookup, convert the input string to upper case,
* and check whether it's entirely ASCII (we call gperf with option "-7"
* to achieve a smaller table) and non-empty. If it's not entirely ASCII,
* or if it's too long, it is not a valid encoding name.
*/
for (to_wchar = 0;;) {
/* Search tocode in the table. */
for (cp = tocode, bp = buf, count = MAX_WORD_LENGTH+9+9+1; ; cp++, bp++) {
unsigned char c = (unsigned char) *cp;
if (c >= 0x80)
goto invalid;
if (c >= 'a' && c <= 'z')
c -= 'a'-'A';
*bp = c;
if (c == '\0')
break;
if (--count == 0)
goto invalid;
}
for (;;) {
char *sp = bp;
int parsed_translit = 0;
int parsed_ignore = 0;
int parsed_non_identical_discard = 0;
for (;;) {
if (sp-buf > 9 && memcmp(sp-9,"/TRANSLIT",9)==0) {
sp = sp - 9;
parsed_translit = 1;
} else if (sp-buf > 7 && memcmp(sp-7,"/IGNORE",7)==0) {
sp = sp - 7;
parsed_ignore = 1;
} else if (sp-buf > 22 && memcmp(sp-22,"/NON_IDENTICAL_DISCARD",22)==0) {
sp = sp - 22;
parsed_non_identical_discard = 1;
} else
break;
if (sp > buf && sp[-1] == '/')
sp = sp - 1;
}
if (sp > buf && sp[-1] == '/') {
bp = sp - 1;
} else if (sp-buf >= 9 && memcmp(sp-9,"/ZOS_UNIX",9)==0) {
bp = sp - 9;
to_surface = ICONV_SURFACE_EBCDIC_ZOS_UNIX;
} else
bp = sp;
*bp = '\0';
if (parsed_translit)
transliterate = 1;
if (parsed_ignore)
discard_ilseq |= DISCARD_INVALID | DISCARD_UNCONVERTIBLE;
if (parsed_non_identical_discard)
discard_ilseq |= DISCARD_UNCONVERTIBLE;
break;
}
if (buf[0] == '\0') {
tocode = locale_charset();
/* Avoid an endless loop that could occur when using an older version
of localcharset.c. */
if (tocode[0] == '\0')
goto invalid;
continue;
}
ap = aliases_lookup(buf,bp-buf);
if (ap == NULL) {
ap = aliases2_lookup(buf);
if (ap == NULL)
goto invalid;
}
if (ap->encoding_index == ei_local_char) {
tocode = locale_charset();
/* Avoid an endless loop that could occur when using an older version
of localcharset.c. */
if (tocode[0] == '\0')
goto invalid;
continue;
}
if (ap->encoding_index == ei_local_wchar_t) {
/* On systems which define __STDC_ISO_10646__, wchar_t is Unicode.
This is also the case on native Woe32 systems and Cygwin >= 1.7, where
we know that it is UTF-16. */
#if (defined _WIN32 && !defined __CYGWIN__) || (defined __CYGWIN__ && CYGWIN_VERSION_DLL_MAJOR >= 1007)
if (sizeof(wchar_t) == 4) {
to_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
# if WORDS_LITTLEENDIAN
to_index = ei_utf16le;
# else
to_index = ei_utf16be;
# endif
break;
}
#elif __STDC_ISO_10646__
if (sizeof(wchar_t) == 4) {
to_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
to_index = ei_ucs2internal;
break;
}
if (sizeof(wchar_t) == 1) {
to_index = ei_iso8859_1;
break;
}
#endif
#if HAVE_MBRTOWC
to_wchar = 1;
tocode = locale_charset();
continue;
#endif
goto invalid;
}
to_index = ap->encoding_index;
break;
}
for (from_wchar = 0;;) {
/* Search fromcode in the table. */
for (cp = fromcode, bp = buf, count = MAX_WORD_LENGTH+9+9+1; ; cp++, bp++) {
unsigned char c = (unsigned char) *cp;
if (c >= 0x80)
goto invalid;
if (c >= 'a' && c <= 'z')
c -= 'a'-'A';
*bp = c;
if (c == '\0')
break;
if (--count == 0)
goto invalid;
}
for (;;) {
char *sp = bp;
int parsed_translit = 0;
int parsed_ignore = 0;
int parsed_non_identical_discard = 0;
for (;;) {
if (sp-buf > 9 && memcmp(sp-9,"/TRANSLIT",9)==0) {
sp = sp - 9;
parsed_translit = 1;
} else if (sp-buf > 7 && memcmp(sp-7,"/IGNORE",7)==0) {
sp = sp - 7;
parsed_ignore = 1;
} else if (sp-buf > 22 && memcmp(sp-22,"/NON_IDENTICAL_DISCARD",22)==0) {
sp = sp - 22;
parsed_non_identical_discard = 1;
} else
break;
if (sp > buf && sp[-1] == '/')
sp = sp - 1;
}
if (sp > buf && sp[-1] == '/') {
bp = sp - 1;
} else if (sp-buf >= 9 && memcmp(sp-9,"/ZOS_UNIX",9)==0) {
bp = sp - 9;
from_surface = ICONV_SURFACE_EBCDIC_ZOS_UNIX;
} else
bp = sp;
*bp = '\0';
if (parsed_translit)
transliterate = 1;
if (parsed_ignore)
discard_ilseq |= DISCARD_INVALID | DISCARD_UNCONVERTIBLE;
if (parsed_non_identical_discard)
discard_ilseq |= DISCARD_UNCONVERTIBLE;
break;
}
if (buf[0] == '\0') {
fromcode = locale_charset();
/* Avoid an endless loop that could occur when using an older version
of localcharset.c. */
if (fromcode[0] == '\0')
goto invalid;
continue;
}
ap = aliases_lookup(buf,bp-buf);
if (ap == NULL) {
ap = aliases2_lookup(buf);
if (ap == NULL)
goto invalid;
}
if (ap->encoding_index == ei_local_char) {
fromcode = locale_charset();
/* Avoid an endless loop that could occur when using an older version
of localcharset.c. */
if (fromcode[0] == '\0')
goto invalid;
continue;
}
if (ap->encoding_index == ei_local_wchar_t) {
/* On systems which define __STDC_ISO_10646__, wchar_t is Unicode.
This is also the case on native Woe32 systems and Cygwin >= 1.7, where
we know that it is UTF-16. */
#if (defined _WIN32 && !defined __CYGWIN__) || (defined __CYGWIN__ && CYGWIN_VERSION_DLL_MAJOR >= 1007)
if (sizeof(wchar_t) == 4) {
from_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
# if WORDS_LITTLEENDIAN
from_index = ei_utf16le;
# else
from_index = ei_utf16be;
# endif
break;
}
#elif __STDC_ISO_10646__
if (sizeof(wchar_t) == 4) {
from_index = ei_ucs4internal;
break;
}
if (sizeof(wchar_t) == 2) {
from_index = ei_ucs2internal;
break;
}
if (sizeof(wchar_t) == 1) {
from_index = ei_iso8859_1;
break;
}
#endif
#if HAVE_WCRTOMB
from_wchar = 1;
fromcode = locale_charset();
continue;
#endif
goto invalid;
}
from_index = ap->encoding_index;
break;
}
}