xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* bug 19432: iconv rejects redundant escape sequences in IBM903, |
| 2 | IBM905, IBM907, and IBM909 |
| 3 | |
| 4 | Copyright (C) 2016 Free Software Foundation, Inc. |
| 5 | This file is part of the GNU C Library. |
| 6 | |
| 7 | The GNU C Library 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 | The GNU C Library 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 the GNU C Library; if not, see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <iconv.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | // The longest test input sequence. |
| 28 | #define MAXINBYTES 8 |
| 29 | #define MAXOUTBYTES (MAXINBYTES * MB_LEN_MAX) |
| 30 | |
| 31 | /* Verify that a conversion of the INPUT sequence consisting of |
| 32 | INBYTESLEFT bytes in the encoding specified by the codeset |
| 33 | named by FROM_SET is successful. |
| 34 | Return 0 on success, non-zero on iconv() failure. */ |
| 35 | |
| 36 | static int |
| 37 | test_ibm93x (const char *from_set, const char *input, size_t inbytesleft) |
| 38 | { |
| 39 | const char to_set[] = "UTF-8"; |
| 40 | iconv_t cd = iconv_open (to_set, from_set); |
| 41 | if (cd == (iconv_t) -1) |
| 42 | { |
| 43 | printf ("iconv_open(\"%s\", \"%s\"): %s\n", |
| 44 | from_set, to_set, strerror (errno)); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | char output [MAXOUTBYTES]; |
| 49 | size_t outbytesleft = sizeof output; |
| 50 | |
| 51 | char *inbuf = (char*)input; |
| 52 | char *outbuf = output; |
| 53 | |
| 54 | printf ("iconv(cd, %p, %zu, %p, %zu)\n", |
| 55 | inbuf, inbytesleft, outbuf, outbytesleft); |
| 56 | |
| 57 | errno = 0; |
| 58 | size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 59 | printf (" ==> %zu: %s\n" |
| 60 | " inbuf%+td, inbytesleft=%zu, outbuf%+td, outbytesleft=%zu\n", |
| 61 | ret, strerror (errno), |
| 62 | inbuf - input, inbytesleft, outbuf - output, outbytesleft); |
| 63 | |
| 64 | // Return 0 on success, non-zero on iconv() failure. |
| 65 | return ret == (size_t)-1 || errno; |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | do_test (void) |
| 70 | { |
| 71 | // State-dependent encodings to exercise. |
| 72 | static const char* const to_code[] = { |
| 73 | "IBM930", "IBM933", "IBM935", "IBM937", "IBM939" |
| 74 | }; |
| 75 | |
| 76 | static const size_t ncodesets = sizeof to_code / sizeof *to_code; |
| 77 | |
| 78 | static const struct { |
| 79 | char txt[MAXINBYTES]; |
| 80 | size_t len; |
| 81 | } input[] = { |
| 82 | #define DATA(s) { s, sizeof s - 1 } |
| 83 | /* <SI>: denotes the shift-in 1-byte escape sequence, changing |
| 84 | the encoder from a sigle-byte encoding to multibyte |
| 85 | <SO>: denotes the shift-out 1-byte escape sequence, switching |
| 86 | the encoder from a multibyte to a single-byte state */ |
| 87 | |
| 88 | DATA ("\x0e"), // <SI> (not redundant) |
| 89 | DATA ("\x0f"), // <S0> (redundant with initial state) |
| 90 | DATA ("\x0e\x0e"), // <SI><SI> |
| 91 | DATA ("\x0e\x0f\x0f"), // <SI><SO><SO> |
| 92 | DATA ("\x0f\x0f"), // <SO><SO> |
| 93 | DATA ("\x0f\x0e\x0e"), // <SO><SI><SI> |
| 94 | DATA ("\x0e\x0f\xc7\x0f"), // <SI><SO><G><SO> |
| 95 | DATA ("\xc7\x0f") // <G><SO> (redundant with initial state) |
| 96 | }; |
| 97 | |
| 98 | static const size_t ninputs = sizeof input / sizeof *input; |
| 99 | |
| 100 | int ret = 0; |
| 101 | |
| 102 | size_t i, j; |
| 103 | |
| 104 | /* Iterate over the IBM93x codesets above and exercise each with |
| 105 | the input sequences above. */ |
| 106 | for (i = 0; i != ncodesets; ++i) |
| 107 | for (j = 0; j != ninputs; ++j) |
| 108 | ret += test_ibm93x (to_code [i], input [i].txt, input [i].len); |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | #define TEST_FUNCTION do_test () |
| 114 | #include "../test-skeleton.c" |