1/* Copyright (C) 2000-2002, 2004-2005 Free Software Foundation, Inc.
2   This file is part of the GNU LIBICONV Library.
3
4   The GNU LIBICONV Library is free software; you can redistribute it
5   and/or modify it under the terms of the GNU Library General Public
6   License as published by the Free Software Foundation; either version 2
7   of the License, or (at your option) any later version.
8
9   The GNU LIBICONV Library is distributed in the hope that it will be
10   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Library General Public License for more details.
13
14   You should have received a copy of the GNU Library General Public
15   License along with the GNU LIBICONV Library; see the file COPYING.LIB.
16   If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
17   Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19/* Create a table from Unicode to CHARSET. */
20
21#include "config.h"
22
23#include <stddef.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <iconv.h>
28#include <errno.h>
29
30#include "binary-io.h"
31
32int main (int argc, char* argv[])
33{
34  const char* charset;
35  iconv_t cd;
36  int bmp_only;
37
38  if (argc != 2) {
39    fprintf(stderr,"Usage: table-to charset\n");
40    exit(1);
41  }
42  charset = argv[1];
43
44#if O_BINARY
45  SET_BINARY(fileno(stdout));
46#endif
47
48  cd = iconv_open(charset,"UCS-4-INTERNAL");
49  if (cd == (iconv_t)(-1)) {
50    perror("iconv_open");
51    exit(1);
52  }
53
54  /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
55     file gets too big. */
56  bmp_only = (strcmp(charset,"UTF-8") == 0 || strcmp(charset,"GB18030") == 0);
57
58  {
59    unsigned int i;
60    unsigned char buf[10];
61    for (i = 0; i < (bmp_only ? 0x10000 : 0x30000); i++) {
62      unsigned int in = i;
63      const char* inbuf = (const char*) &in;
64      size_t inbytesleft = sizeof(unsigned int);
65      char* outbuf = (char*)buf;
66      size_t outbytesleft = sizeof(buf);
67      size_t result;
68      size_t result2 = 0;
69      iconv(cd,NULL,NULL,NULL,NULL);
70      result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft);
71      if (result != (size_t)(-1))
72        result2 = iconv(cd,NULL,NULL,&outbuf,&outbytesleft);
73      if (result == (size_t)(-1) || result2 == (size_t)(-1)) {
74        if (errno != EILSEQ) {
75          int saved_errno = errno;
76          fprintf(stderr,"0x%02X: iconv error: ",i);
77          errno = saved_errno;
78          perror("");
79          exit(1);
80        }
81      } else if (result == 0) /* ignore conversions with transliteration */ {
82        unsigned int j, jmax;
83        if (inbytesleft != 0 || outbytesleft == sizeof(buf)) {
84          fprintf(stderr,"0x%02X: inbytes = %ld, outbytes = %ld\n",i,(long)(sizeof(unsigned int)-inbytesleft),(long)(sizeof(buf)-outbytesleft));
85          exit(1);
86        }
87        jmax = sizeof(buf) - outbytesleft;
88        printf("0x");
89        for (j = 0; j < jmax; j++)
90          printf("%02X",buf[j]);
91        printf("\t0x%04X\n",i);
92      }
93    }
94  }
95
96  if (iconv_close(cd) < 0) {
97    perror("iconv_close");
98    exit(1);
99  }
100
101  if (ferror(stdin) || ferror(stdout) || fclose(stdout)) {
102    fprintf(stderr,"I/O error\n");
103    exit(1);
104  }
105
106  exit(0);
107}
108