(root)/
glibc-2.38/
iconvdata/
tst-e2big.c
       1  /* Test for a tricky E2BIG situation.
       2     Copyright (C) 2002-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <alloca.h>
      20  #include <errno.h>
      21  #include <iconv.h>
      22  #include <stdbool.h>
      23  #include <stdio.h>
      24  #include <stdlib.h>
      25  
      26  /* In EUC-JISX0213 and TSCII, a single input character can convert to
      27     a sequence of two or more Unicode characters.  When the output buffer
      28     has room for less Unicode characters than would be produced with an
      29     unconstrained output buffer, the conversion must give errno = E2BIG.  */
      30  
      31  void
      32  test (const char *encoding, char *inbuf, size_t inbufsize, size_t outbufsize)
      33  {
      34    char *outbuf = alloca (outbufsize);
      35    iconv_t cd;
      36    char *inptr;
      37    size_t inlen;
      38    char *outptr;
      39    size_t outlen;
      40    int result;
      41    bool empty_input;
      42    bool empty_output;
      43  
      44    cd = iconv_open ("UTF-8", encoding);
      45    if (cd == (iconv_t) -1)
      46      {
      47        fprintf (stderr, "cannot convert from %s\n", encoding);
      48        exit (1);
      49      }
      50  
      51    inptr = inbuf;
      52    inlen = inbufsize;
      53    outptr = outbuf;
      54    outlen = outbufsize;
      55  
      56    result = iconv (cd, &inptr, &inlen, &outptr, &outlen);
      57    if (!(result == -1 && errno == E2BIG))
      58      {
      59        fprintf (stderr, "%s: wrong iconv result: %d/%d (%m)\n",
      60  	       encoding, result, errno);
      61        exit (1);
      62      }
      63    empty_input = (inptr == inbuf && inlen == inbufsize);
      64    empty_output = (outptr == outbuf && outlen == outbufsize);
      65  
      66    if (!empty_input && empty_output)
      67      {
      68        fprintf (stderr, "%s: ate %td input bytes\n", encoding, inptr - inbuf);
      69        exit (1);
      70      }
      71    if (empty_input && !empty_output)
      72      {
      73        fprintf (stderr, "%s: produced %td output bytes\n",
      74  	       encoding, outptr - outbuf);
      75        exit (1);
      76      }
      77  
      78    iconv_close (cd);
      79  }
      80  
      81  void
      82  test_euc_jisx0213 (void)
      83  {
      84    char inbuf[2] = { 0xa4, 0xf7 };
      85    test ("EUC-JISX0213", inbuf, sizeof (inbuf), 3);
      86  }
      87  
      88  void
      89  test_tscii (void)
      90  {
      91    char inbuf[1] = { 0x82 };
      92    test ("TSCII", inbuf, sizeof (inbuf), 3);
      93    test ("TSCII", inbuf, sizeof (inbuf), 6);
      94    test ("TSCII", inbuf, sizeof (inbuf), 9);
      95  }
      96  
      97  static int
      98  do_test (void)
      99  {
     100    test_euc_jisx0213 ();
     101    test_tscii ();
     102    return 0;
     103  }
     104  
     105  #define TEST_FUNCTION do_test ()
     106  #include "../test-skeleton.c"