lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test for a tricky E2BIG situation. |
| 2 | Copyright (C) 2002-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Bruno Haible <bruno@clisp.org>, 2002. |
| 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 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <alloca.h> |
| 21 | #include <errno.h> |
| 22 | #include <iconv.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | /* In EUC-JISX0213 and TSCII, a single input character can convert to |
| 28 | a sequence of two or more Unicode characters. When the output buffer |
| 29 | has room for less Unicode characters than would be produced with an |
| 30 | unconstrained output buffer, the conversion must give errno = E2BIG. */ |
| 31 | |
| 32 | void |
| 33 | test (const char *encoding, char *inbuf, size_t inbufsize, size_t outbufsize) |
| 34 | { |
| 35 | char *outbuf = alloca (outbufsize); |
| 36 | iconv_t cd; |
| 37 | char *inptr; |
| 38 | size_t inlen; |
| 39 | char *outptr; |
| 40 | size_t outlen; |
| 41 | int result; |
| 42 | bool empty_input; |
| 43 | bool empty_output; |
| 44 | |
| 45 | cd = iconv_open ("UTF-8", encoding); |
| 46 | if (cd == (iconv_t) -1) |
| 47 | { |
| 48 | fprintf (stderr, "cannot convert from %s\n", encoding); |
| 49 | exit (1); |
| 50 | } |
| 51 | |
| 52 | inptr = inbuf; |
| 53 | inlen = inbufsize; |
| 54 | outptr = outbuf; |
| 55 | outlen = outbufsize; |
| 56 | |
| 57 | result = iconv (cd, &inptr, &inlen, &outptr, &outlen); |
| 58 | if (!(result == -1 && errno == E2BIG)) |
| 59 | { |
| 60 | fprintf (stderr, "%s: wrong iconv result: %d/%d (%m)\n", |
| 61 | encoding, result, errno); |
| 62 | exit (1); |
| 63 | } |
| 64 | empty_input = (inptr == inbuf && inlen == inbufsize); |
| 65 | empty_output = (outptr == outbuf && outlen == outbufsize); |
| 66 | |
| 67 | if (!empty_input && empty_output) |
| 68 | { |
| 69 | fprintf (stderr, "%s: ate %td input bytes\n", encoding, inptr - inbuf); |
| 70 | exit (1); |
| 71 | } |
| 72 | if (empty_input && !empty_output) |
| 73 | { |
| 74 | fprintf (stderr, "%s: produced %td output bytes\n", |
| 75 | encoding, outptr - outbuf); |
| 76 | exit (1); |
| 77 | } |
| 78 | |
| 79 | iconv_close (cd); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | test_euc_jisx0213 (void) |
| 84 | { |
| 85 | char inbuf[2] = { 0xa4, 0xf7 }; |
| 86 | test ("EUC-JISX0213", inbuf, sizeof (inbuf), 3); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | test_tscii (void) |
| 91 | { |
| 92 | char inbuf[1] = { 0x82 }; |
| 93 | test ("TSCII", inbuf, sizeof (inbuf), 3); |
| 94 | test ("TSCII", inbuf, sizeof (inbuf), 6); |
| 95 | test ("TSCII", inbuf, sizeof (inbuf), 9); |
| 96 | } |
| 97 | |
| 98 | static int |
| 99 | do_test (void) |
| 100 | { |
| 101 | test_euc_jisx0213 (); |
| 102 | test_tscii (); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | #define TEST_FUNCTION do_test () |
| 107 | #include "../test-skeleton.c" |