1 /* Copyright (C) 2000-2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Create a table from CHARSET to Unicode.
21 This is a good test for CHARSET's iconv() module, in particular the
22 FROM_LOOP BODY macro. */
31 /* If nonzero, ignore conversions outside Unicode plane 0. */
34 /* Converts a byte buffer to a hexadecimal string. */
36 hexbuf (unsigned char buf
[], unsigned int buflen
)
43 sprintf (msg
, "0x%02X", buf
[0]);
46 sprintf (msg
, "0x%02X%02X", buf
[0], buf
[1]);
49 sprintf (msg
, "0x%02X%02X%02X", buf
[0], buf
[1], buf
[2]);
52 sprintf (msg
, "0x%02X%02X%02X%02X", buf
[0], buf
[1], buf
[2], buf
[3]);
60 /* Attempts to convert a byte buffer BUF (BUFLEN bytes) to OUT (12 bytes)
61 using the conversion descriptor CD. Returns the number of written bytes,
62 or 0 if ambiguous, or -1 if invalid. */
64 try (iconv_t cd
, unsigned char buf
[], unsigned int buflen
, unsigned char *out
)
66 const char *inbuf
= (const char *) buf
;
67 size_t inbytesleft
= buflen
;
68 char *outbuf
= (char *) out
;
69 size_t outbytesleft
= 12;
72 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
73 result
= iconv (cd
, (char **) &inbuf
, &inbytesleft
, &outbuf
, &outbytesleft
);
74 if (result
!= (size_t)(-1))
75 result
= iconv (cd
, NULL
, NULL
, &outbuf
, &outbytesleft
);
77 if (result
== (size_t)(-1))
83 else if (errno
== EINVAL
)
89 int saved_errno
= errno
;
90 fprintf (stderr
, "%s: iconv error: ", hexbuf (buf
, buflen
));
100 fprintf (stderr
, "%s: inbytes = %ld, outbytes = %ld\n",
101 hexbuf (buf
, buflen
),
102 (long) (buflen
- inbytesleft
),
103 (long) (12 - outbytesleft
));
106 return 12 - outbytesleft
;
110 /* Returns the out[] buffer as a Unicode value, formatted as 0x%04X. */
112 utf8_decode (const unsigned char *out
, unsigned int outlen
)
114 static char hexbuf
[84];
124 sprintf (p
, "0x%04X", out
[0]);
125 out
+= 1; outlen
-= 1;
127 else if (out
[0] >= 0xc0 && out
[0] < 0xe0 && outlen
>= 2)
129 sprintf (p
, "0x%04X", ((out
[0] & 0x1f) << 6) + (out
[1] & 0x3f));
130 out
+= 2; outlen
-= 2;
132 else if (out
[0] >= 0xe0 && out
[0] < 0xf0 && outlen
>= 3)
134 sprintf (p
, "0x%04X", ((out
[0] & 0x0f) << 12)
135 + ((out
[1] & 0x3f) << 6) + (out
[2] & 0x3f));
136 out
+= 3; outlen
-= 3;
138 else if (out
[0] >= 0xf0 && out
[0] < 0xf8 && outlen
>= 4)
140 sprintf (p
, "0x%04X", ((out
[0] & 0x07) << 18)
141 + ((out
[1] & 0x3f) << 12)
142 + ((out
[2] & 0x3f) << 6) + (out
[3] & 0x3f));
143 out
+= 4; outlen
-= 4;
145 else if (out
[0] >= 0xf8 && out
[0] < 0xfc && outlen
>= 5)
147 sprintf (p
, "0x%04X", ((out
[0] & 0x03) << 24)
148 + ((out
[1] & 0x3f) << 18)
149 + ((out
[2] & 0x3f) << 12)
150 + ((out
[3] & 0x3f) << 6) + (out
[4] & 0x3f));
151 out
+= 5; outlen
-= 5;
153 else if (out
[0] >= 0xfc && out
[0] < 0xfe && outlen
>= 6)
155 sprintf (p
, "0x%04X", ((out
[0] & 0x01) << 30)
156 + ((out
[1] & 0x3f) << 24)
157 + ((out
[2] & 0x3f) << 18)
158 + ((out
[3] & 0x3f) << 12)
159 + ((out
[4] & 0x3f) << 6) + (out
[5] & 0x3f));
160 out
+= 6; outlen
-= 6;
164 sprintf (p
, "0x????");
165 out
+= 1; outlen
-= 1;
168 if (bmp_only
&& strlen (p
) > 6)
169 /* Ignore conversions outside Unicode plane 0. */
179 main (int argc
, char *argv
[])
187 fprintf (stderr
, "Usage: tst-table-from charset\n");
192 cd
= iconv_open ("UTF-8", charset
);
193 if (cd
== (iconv_t
)(-1))
195 perror ("iconv_open");
199 /* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
200 file gets too big. */
201 bmp_only
= (strcmp (charset
, "UTF-8") == 0
202 || strcmp (charset
, "GB18030") == 0);
203 search_depth
= (strcmp (charset
, "UTF-8") == 0 ? 3 : 4);
206 unsigned char out
[12];
207 unsigned char buf
[4];
208 unsigned int i0
, i1
, i2
, i3
;
211 for (i0
= 0; i0
< 0x100; i0
++)
214 result
= try (cd
, buf
, 1, out
);
220 const char *unicode
= utf8_decode (out
, result
);
222 printf ("0x%02X\t%s\n", i0
, unicode
);
226 for (i1
= 0; i1
< 0x100; i1
++)
229 result
= try (cd
, buf
, 2, out
);
235 const char *unicode
= utf8_decode (out
, result
);
237 printf ("0x%02X%02X\t%s\n", i0
, i1
, unicode
);
241 for (i2
= 0; i2
< 0x100; i2
++)
244 result
= try (cd
, buf
, 3, out
);
250 const char *unicode
= utf8_decode (out
, result
);
252 printf ("0x%02X%02X%02X\t%s\n",
253 i0
, i1
, i2
, unicode
);
255 else if (search_depth
> 3)
257 for (i3
= 0; i3
< 0x100; i3
++)
260 result
= try (cd
, buf
, 4, out
);
266 const char *unicode
=
267 utf8_decode (out
, result
);
269 printf ("0x%02X%02X%02X%02X\t%s\n",
270 i0
, i1
, i2
, i3
, unicode
);
275 "%s: incomplete byte sequence\n",
288 if (iconv_close (cd
) < 0)
290 perror ("iconv_close");
294 if (ferror (stdin
) || fflush (stdout
) || ferror (stdout
))
296 fprintf (stderr
, "I/O error\n");