1  /* Copyright (C) 2000-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Create a table from Unicode to CHARSET.
      19     This is a good test for CHARSET's iconv() module, in particular the
      20     TO_LOOP BODY macro.  */
      21  
      22  #include <stddef.h>
      23  #include <stdio.h>
      24  #include <stdlib.h>
      25  #include <string.h>
      26  #include <iconv.h>
      27  #include <errno.h>
      28  
      29  int
      30  main (int argc, char *argv[])
      31  {
      32    const char *charset;
      33    iconv_t cd;
      34    int bmp_only;
      35  
      36    if (argc != 2)
      37      {
      38        fprintf (stderr, "Usage: tst-table-to charset\n");
      39        return 1;
      40      }
      41    charset = argv[1];
      42  
      43    cd = iconv_open (charset, "UTF-8");
      44    if (cd == (iconv_t)(-1))
      45      {
      46        perror ("iconv_open");
      47        return 1;
      48      }
      49  
      50    /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
      51       file gets too big.  */
      52    bmp_only = (strcmp (charset, "UTF-8") == 0
      53  	      || strcmp (charset, "GB18030") == 0);
      54  
      55    {
      56      unsigned int i;
      57      unsigned char buf[10];
      58  
      59      for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++)
      60        {
      61  	unsigned char in[6];
      62  	unsigned int incount =
      63  	  (i < 0x80 ? (in[0] = i, 1)
      64  	   : i < 0x800 ? (in[0] = 0xc0 | (i >> 6),
      65  			  in[1] = 0x80 | (i & 0x3f), 2)
      66  	   : i < 0x10000 ? (in[0] = 0xe0 | (i >> 12),
      67  			    in[1] = 0x80 | ((i >> 6) & 0x3f),
      68  			    in[2] = 0x80 | (i & 0x3f), 3)
      69  	   : /* i < 0x200000 */ (in[0] = 0xf0 | (i >> 18),
      70  				 in[1] = 0x80 | ((i >> 12) & 0x3f),
      71  				 in[2] = 0x80 | ((i >> 6) & 0x3f),
      72  				 in[3] = 0x80 | (i & 0x3f), 4));
      73  	const char *inbuf = (const char *) in;
      74  	size_t inbytesleft = incount;
      75  	char *outbuf = (char *) buf;
      76  	size_t outbytesleft = sizeof (buf);
      77  	size_t result;
      78  	size_t result2 = 0;
      79  
      80  	iconv (cd, NULL, NULL, NULL, NULL);
      81  	result = iconv (cd,
      82  			(char **) &inbuf, &inbytesleft,
      83  			&outbuf, &outbytesleft);
      84  	if (result != (size_t)(-1))
      85  	  result2 = iconv (cd, NULL, NULL, &outbuf, &outbytesleft);
      86  
      87  	if (result == (size_t)(-1) || result2 == (size_t)(-1))
      88  	  {
      89  	    if (errno != EILSEQ)
      90  	      {
      91  		int saved_errno = errno;
      92  		fprintf (stderr, "0x%02X: iconv error: ", i);
      93  		errno = saved_errno;
      94  		perror ("");
      95  		return 1;
      96  	      }
      97  	  }
      98  	else if (result == 0) /* ignore conversions with transliteration */
      99  	  {
     100  	    unsigned int j, jmax;
     101  	    if (inbytesleft != 0 || outbytesleft == sizeof (buf))
     102  	      {
     103  		fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i,
     104  			 (long) (incount - inbytesleft),
     105  			 (long) (sizeof (buf) - outbytesleft));
     106  		return 1;
     107  	      }
     108  	    jmax = sizeof (buf) - outbytesleft;
     109  	    printf ("0x");
     110  	    for (j = 0; j < jmax; j++)
     111  	      printf ("%02X", buf[j]);
     112  	    printf ("\t0x%04X\n", i);
     113  	  }
     114        }
     115    }
     116  
     117    if (iconv_close (cd) < 0)
     118      {
     119        perror ("iconv_close");
     120        return 1;
     121      }
     122  
     123    if (ferror (stdin) || fflush (stdout) || ferror (stdout))
     124      {
     125        fprintf (stderr, "I/O error\n");
     126        return 1;
     127      }
     128  
     129    return 0;
     130  }