|
Packit Service |
82fcde |
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Create a table from CHARSET to Unicode.
|
|
Packit Service |
82fcde |
This is a good test for CHARSET's iconv() module, in particular the
|
|
Packit Service |
82fcde |
FROM_LOOP BODY macro. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <stddef.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <stdlib.h>
|
|
Packit Service |
82fcde |
#include <string.h>
|
|
Packit Service |
82fcde |
#include <iconv.h>
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* If nonzero, ignore conversions outside Unicode plane 0. */
|
|
Packit Service |
82fcde |
static int bmp_only;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Converts a byte buffer to a hexadecimal string. */
|
|
Packit Service |
82fcde |
static const char*
|
|
Packit Service |
82fcde |
hexbuf (unsigned char buf[], unsigned int buflen)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
static char msg[50];
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
switch (buflen)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
case 1:
|
|
Packit Service |
82fcde |
sprintf (msg, "0x%02X", buf[0]);
|
|
Packit Service |
82fcde |
break;
|
|
Packit Service |
82fcde |
case 2:
|
|
Packit Service |
82fcde |
sprintf (msg, "0x%02X%02X", buf[0], buf[1]);
|
|
Packit Service |
82fcde |
break;
|
|
Packit Service |
82fcde |
case 3:
|
|
Packit Service |
82fcde |
sprintf (msg, "0x%02X%02X%02X", buf[0], buf[1], buf[2]);
|
|
Packit Service |
82fcde |
break;
|
|
Packit Service |
82fcde |
case 4:
|
|
Packit Service |
82fcde |
sprintf (msg, "0x%02X%02X%02X%02X", buf[0], buf[1], buf[2], buf[3]);
|
|
Packit Service |
82fcde |
break;
|
|
Packit Service |
82fcde |
default:
|
|
Packit Service |
82fcde |
abort ();
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return msg;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Attempts to convert a byte buffer BUF (BUFLEN bytes) to OUT (12 bytes)
|
|
Packit Service |
82fcde |
using the conversion descriptor CD. Returns the number of written bytes,
|
|
Packit Service |
82fcde |
or 0 if ambiguous, or -1 if invalid. */
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
try (iconv_t cd, unsigned char buf[], unsigned int buflen, unsigned char *out)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *inbuf = (const char *) buf;
|
|
Packit Service |
82fcde |
size_t inbytesleft = buflen;
|
|
Packit Service |
82fcde |
char *outbuf = (char *) out;
|
|
Packit Service |
82fcde |
size_t outbytesleft = 12;
|
|
Packit Service |
82fcde |
size_t result;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
iconv (cd, NULL, NULL, NULL, NULL);
|
|
Packit Service |
82fcde |
result = iconv (cd, (char **) &inbuf, &inbytesleft, &outbuf, &outbytesleft);
|
|
Packit Service |
82fcde |
if (result != (size_t)(-1))
|
|
Packit Service |
82fcde |
result = iconv (cd, NULL, NULL, &outbuf, &outbytesleft);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (result == (size_t)(-1))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (errno == EILSEQ)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
return -1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (errno == EINVAL)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
int saved_errno = errno;
|
|
Packit Service |
82fcde |
fprintf (stderr, "%s: iconv error: ", hexbuf (buf, buflen));
|
|
Packit Service |
82fcde |
errno = saved_errno;
|
|
Packit Service |
82fcde |
perror ("");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (inbytesleft != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
fprintf (stderr, "%s: inbytes = %ld, outbytes = %ld\n",
|
|
Packit Service |
82fcde |
hexbuf (buf, buflen),
|
|
Packit Service |
82fcde |
(long) (buflen - inbytesleft),
|
|
Packit Service |
82fcde |
(long) (12 - outbytesleft));
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
return 12 - outbytesleft;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Returns the out[] buffer as a Unicode value, formatted as 0x%04X. */
|
|
Packit Service |
82fcde |
static const char *
|
|
Packit Service |
82fcde |
utf8_decode (const unsigned char *out, unsigned int outlen)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
static char hexbuf[84];
|
|
Packit Service |
82fcde |
char *p = hexbuf;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
while (outlen > 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (p > hexbuf)
|
|
Packit Service |
82fcde |
*p++ = ' ';
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (out[0] < 0x80)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", out[0]);
|
|
Packit Service |
82fcde |
out += 1; outlen -= 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (out[0] >= 0xc0 && out[0] < 0xe0 && outlen >= 2)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", ((out[0] & 0x1f) << 6) + (out[1] & 0x3f));
|
|
Packit Service |
82fcde |
out += 2; outlen -= 2;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (out[0] >= 0xe0 && out[0] < 0xf0 && outlen >= 3)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", ((out[0] & 0x0f) << 12)
|
|
Packit Service |
82fcde |
+ ((out[1] & 0x3f) << 6) + (out[2] & 0x3f));
|
|
Packit Service |
82fcde |
out += 3; outlen -= 3;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (out[0] >= 0xf0 && out[0] < 0xf8 && outlen >= 4)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", ((out[0] & 0x07) << 18)
|
|
Packit Service |
82fcde |
+ ((out[1] & 0x3f) << 12)
|
|
Packit Service |
82fcde |
+ ((out[2] & 0x3f) << 6) + (out[3] & 0x3f));
|
|
Packit Service |
82fcde |
out += 4; outlen -= 4;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (out[0] >= 0xf8 && out[0] < 0xfc && outlen >= 5)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", ((out[0] & 0x03) << 24)
|
|
Packit Service |
82fcde |
+ ((out[1] & 0x3f) << 18)
|
|
Packit Service |
82fcde |
+ ((out[2] & 0x3f) << 12)
|
|
Packit Service |
82fcde |
+ ((out[3] & 0x3f) << 6) + (out[4] & 0x3f));
|
|
Packit Service |
82fcde |
out += 5; outlen -= 5;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (out[0] >= 0xfc && out[0] < 0xfe && outlen >= 6)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x%04X", ((out[0] & 0x01) << 30)
|
|
Packit Service |
82fcde |
+ ((out[1] & 0x3f) << 24)
|
|
Packit Service |
82fcde |
+ ((out[2] & 0x3f) << 18)
|
|
Packit Service |
82fcde |
+ ((out[3] & 0x3f) << 12)
|
|
Packit Service |
82fcde |
+ ((out[4] & 0x3f) << 6) + (out[5] & 0x3f));
|
|
Packit Service |
82fcde |
out += 6; outlen -= 6;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
sprintf (p, "0x????");
|
|
Packit Service |
82fcde |
out += 1; outlen -= 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (bmp_only && strlen (p) > 6)
|
|
Packit Service |
82fcde |
/* Ignore conversions outside Unicode plane 0. */
|
|
Packit Service |
82fcde |
return NULL;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
p += strlen (p);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return hexbuf;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
int
|
|
Packit Service |
82fcde |
main (int argc, char *argv[])
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *charset;
|
|
Packit Service |
82fcde |
iconv_t cd;
|
|
Packit Service |
82fcde |
int search_depth;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (argc != 2)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
fprintf (stderr, "Usage: tst-table-from charset\n");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
charset = argv[1];
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
cd = iconv_open ("UTF-8", charset);
|
|
Packit Service |
82fcde |
if (cd == (iconv_t)(-1))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
perror ("iconv_open");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* When testing UTF-8 or GB18030, stop at 0x10000, otherwise the output
|
|
Packit Service |
82fcde |
file gets too big. */
|
|
Packit Service |
82fcde |
bmp_only = (strcmp (charset, "UTF-8") == 0
|
|
Packit Service |
82fcde |
|| strcmp (charset, "GB18030") == 0);
|
|
Packit Service |
82fcde |
search_depth = (strcmp (charset, "UTF-8") == 0 ? 3 : 4);
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
unsigned char out[12];
|
|
Packit Service |
82fcde |
unsigned char buf[4];
|
|
Packit Service |
82fcde |
unsigned int i0, i1, i2, i3;
|
|
Packit Service |
82fcde |
int result;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
for (i0 = 0; i0 < 0x100; i0++)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
buf[0] = i0;
|
|
Packit Service |
82fcde |
result = try (cd, buf, 1, out);
|
|
Packit Service |
82fcde |
if (result < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (result > 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *unicode = utf8_decode (out, result);
|
|
Packit Service |
82fcde |
if (unicode != NULL)
|
|
Packit Service |
82fcde |
printf ("0x%02X\t%s\n", i0, unicode);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
for (i1 = 0; i1 < 0x100; i1++)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
buf[1] = i1;
|
|
Packit Service |
82fcde |
result = try (cd, buf, 2, out);
|
|
Packit Service |
82fcde |
if (result < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (result > 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *unicode = utf8_decode (out, result);
|
|
Packit Service |
82fcde |
if (unicode != NULL)
|
|
Packit Service |
82fcde |
printf ("0x%02X%02X\t%s\n", i0, i1, unicode);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
for (i2 = 0; i2 < 0x100; i2++)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
buf[2] = i2;
|
|
Packit Service |
82fcde |
result = try (cd, buf, 3, out);
|
|
Packit Service |
82fcde |
if (result < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (result > 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *unicode = utf8_decode (out, result);
|
|
Packit Service |
82fcde |
if (unicode != NULL)
|
|
Packit Service |
82fcde |
printf ("0x%02X%02X%02X\t%s\n",
|
|
Packit Service |
82fcde |
i0, i1, i2, unicode);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (search_depth > 3)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
for (i3 = 0; i3 < 0x100; i3++)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
buf[3] = i3;
|
|
Packit Service |
82fcde |
result = try (cd, buf, 4, out);
|
|
Packit Service |
82fcde |
if (result < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (result > 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const char *unicode =
|
|
Packit Service |
82fcde |
utf8_decode (out, result);
|
|
Packit Service |
82fcde |
if (unicode != NULL)
|
|
Packit Service |
82fcde |
printf ("0x%02X%02X%02X%02X\t%s\n",
|
|
Packit Service |
82fcde |
i0, i1, i2, i3, unicode);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
fprintf (stderr,
|
|
Packit Service |
82fcde |
"%s: incomplete byte sequence\n",
|
|
Packit Service |
82fcde |
hexbuf (buf, 4));
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (iconv_close (cd) < 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
perror ("iconv_close");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (ferror (stdin) || fflush (stdout) || ferror (stdout))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
fprintf (stderr, "I/O error\n");
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|