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, see <https://www.gnu.org/licenses/>. */
17:
18: /* Create a table from Unicode to CHARSET. */
19:
20: #include "config.h"
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: #include "binary-io.h"
30:
31: int main (int argc, char* argv[])
32: {
33: const char* charset;
34: iconv_t cd;
35: int bmp_only;
36:
37: if (argc != 2) {
38: fprintf(stderr,"Usage: table-to charset\n");
39: exit(1);
40: }
41: charset = argv[1];
42:
43: #if O_BINARY
44: SET_BINARY(fileno(stdout));
45: #endif
46:
47: cd = iconv_open(charset,"UCS-4-INTERNAL");
48: if (cd == (iconv_t)(-1)) {
49: perror("iconv_open");
50: exit(1);
51: }
52:
53: /* When testing UTF-8, stop at 0x10000, otherwise the output file gets too
54: big. */
55: bmp_only = (strcmp(charset,"UTF-8") == 0);
56:
57: {
58: unsigned int i;
59: unsigned char buf[10];
60: for (i = 0; i < (bmp_only ? 0x10000 : 0x110000); i++) {
61: unsigned int in = i;
62: const char* inbuf = (const char*) ∈
63: size_t inbytesleft = sizeof(unsigned int);
64: char* outbuf = (char*)buf;
65: size_t outbytesleft = sizeof(buf);
66: size_t result;
67: size_t result2 = 0;
68: iconv(cd,NULL,NULL,NULL,NULL);
69: result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft);
70: if (result != (size_t)(-1))
71: result2 = iconv(cd,NULL,NULL,&outbuf,&outbytesleft);
72: if (result == (size_t)(-1) || result2 == (size_t)(-1)) {
73: if (errno != EILSEQ) {
74: int saved_errno = errno;
75: fprintf(stderr,"0x%02X: iconv error: ",i);
76: errno = saved_errno;
77: perror("");
78: exit(1);
79: }
80: } else if (result == 0) /* ignore conversions with transliteration */ {
81: if (inbytesleft == 0 && outbytesleft < sizeof(buf)) {
82: unsigned int jmax = sizeof(buf) - outbytesleft;
83: unsigned int j;
84: printf("0x");
85: for (j = 0; j < jmax; j++)
86: printf("%02X",buf[j]);
87: printf("\t0x%04X\n",i);
88: } else if (inbytesleft == 0 && i >= 0xe0000 && i < 0xe0080) {
89: /* Language tags may silently be dropped. */
90: } else {
91: fprintf(stderr,"0x%02X: inbytes = %ld, outbytes = %ld\n",i,(long)(sizeof(unsigned int)-inbytesleft),(long)(sizeof(buf)-outbytesleft));
92: exit(1);
93: }
94: }
95: }
96: }
97:
98: if (iconv_close(cd) < 0) {
99: perror("iconv_close");
100: exit(1);
101: }
102:
103: if (ferror(stdin) || ferror(stdout) || fclose(stdout)) {
104: fprintf(stderr,"I/O error\n");
105: exit(1);
106: }
107:
108: exit(0);
109: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>