Blame iconvdata/tst-table-to.c

Packit 6c4009
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Create a table from Unicode to CHARSET.
Packit 6c4009
   This is a good test for CHARSET's iconv() module, in particular the
Packit 6c4009
   TO_LOOP BODY macro.  */
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  const char *charset;
Packit 6c4009
  iconv_t cd;
Packit 6c4009
  int bmp_only;
Packit 6c4009
Packit 6c4009
  if (argc != 2)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Usage: tst-table-to charset\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  charset = argv[1];
Packit 6c4009
Packit 6c4009
  cd = iconv_open (charset, "UTF-8");
Packit 6c4009
  if (cd == (iconv_t)(-1))
Packit 6c4009
    {
Packit 6c4009
      perror ("iconv_open");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
Packit 6c4009
     file gets too big.  */
Packit 6c4009
  bmp_only = (strcmp (charset, "UTF-8") == 0
Packit 6c4009
	      || strcmp (charset, "GB18030") == 0);
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    unsigned int i;
Packit 6c4009
    unsigned char buf[10];
Packit 6c4009
Packit 6c4009
    for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++)
Packit 6c4009
      {
Packit 6c4009
	unsigned char in[6];
Packit 6c4009
	unsigned int incount =
Packit 6c4009
	  (i < 0x80 ? (in[0] = i, 1)
Packit 6c4009
	   : i < 0x800 ? (in[0] = 0xc0 | (i >> 6),
Packit 6c4009
			  in[1] = 0x80 | (i & 0x3f), 2)
Packit 6c4009
	   : i < 0x10000 ? (in[0] = 0xe0 | (i >> 12),
Packit 6c4009
			    in[1] = 0x80 | ((i >> 6) & 0x3f),
Packit 6c4009
			    in[2] = 0x80 | (i & 0x3f), 3)
Packit 6c4009
	   : /* i < 0x200000 */ (in[0] = 0xf0 | (i >> 18),
Packit 6c4009
				 in[1] = 0x80 | ((i >> 12) & 0x3f),
Packit 6c4009
				 in[2] = 0x80 | ((i >> 6) & 0x3f),
Packit 6c4009
				 in[3] = 0x80 | (i & 0x3f), 4));
Packit 6c4009
	const char *inbuf = (const char *) in;
Packit 6c4009
	size_t inbytesleft = incount;
Packit 6c4009
	char *outbuf = (char *) buf;
Packit 6c4009
	size_t outbytesleft = sizeof (buf);
Packit 6c4009
	size_t result;
Packit 6c4009
	size_t result2 = 0;
Packit 6c4009
Packit 6c4009
	iconv (cd, NULL, NULL, NULL, NULL);
Packit 6c4009
	result = iconv (cd,
Packit 6c4009
			(char **) &inbuf, &inbytesleft,
Packit 6c4009
			&outbuf, &outbytesleft);
Packit 6c4009
	if (result != (size_t)(-1))
Packit 6c4009
	  result2 = iconv (cd, NULL, NULL, &outbuf, &outbytesleft);
Packit 6c4009
Packit 6c4009
	if (result == (size_t)(-1) || result2 == (size_t)(-1))
Packit 6c4009
	  {
Packit 6c4009
	    if (errno != EILSEQ)
Packit 6c4009
	      {
Packit 6c4009
		int saved_errno = errno;
Packit 6c4009
		fprintf (stderr, "0x%02X: iconv error: ", i);
Packit 6c4009
		errno = saved_errno;
Packit 6c4009
		perror ("");
Packit 6c4009
		return 1;
Packit 6c4009
	      }
Packit 6c4009
	  }
Packit 6c4009
	else if (result == 0) /* ignore conversions with transliteration */
Packit 6c4009
	  {
Packit 6c4009
	    unsigned int j, jmax;
Packit 6c4009
	    if (inbytesleft != 0 || outbytesleft == sizeof (buf))
Packit 6c4009
	      {
Packit 6c4009
		fprintf (stderr, "0x%02X: inbytes = %ld, outbytes = %ld\n", i,
Packit 6c4009
			 (long) (incount - inbytesleft),
Packit 6c4009
			 (long) (sizeof (buf) - outbytesleft));
Packit 6c4009
		return 1;
Packit 6c4009
	      }
Packit 6c4009
	    jmax = sizeof (buf) - outbytesleft;
Packit 6c4009
	    printf ("0x");
Packit 6c4009
	    for (j = 0; j < jmax; j++)
Packit 6c4009
	      printf ("%02X", buf[j]);
Packit 6c4009
	    printf ("\t0x%04X\n", i);
Packit 6c4009
	  }
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  if (iconv_close (cd) < 0)
Packit 6c4009
    {
Packit 6c4009
      perror ("iconv_close");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ferror (stdin) || fflush (stdout) || ferror (stdout))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "I/O error\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}