1998-04-13 14:52  Ulrich Drepper  <drepper@cygnus.com>

	* iconvdata/cns11643.c: New file.
	* iconvdata/cns11643.h: New file.
	* iconvdata/cns11643l1.c: New file.
	* iconvdata/cns11643l1.h: New file.
	* iconvdata/euctw.c: New file.
	* iconvdata/Makefile: Add rules for EUC-TW and CNS.
	* iconvdata/gconv-modules: Likewise.

	* iconvdata/eucjp.c: Several bug fixes and improvements.
	* iconvdata/gb2312.h: Likewise.
	* iconvdata/jis0201.h: Likewise.
	* iconvdata/jis0208.h: Likewise.
	* iconvdata/jis0212.h: Likewise.
This commit is contained in:
Ulrich Drepper 1998-04-13 15:01:09 +00:00
parent 40b4c81d4e
commit 2b47435322
13 changed files with 16611 additions and 28 deletions

View File

@ -1,3 +1,19 @@
1998-04-13 14:52 Ulrich Drepper <drepper@cygnus.com>
* iconvdata/cns11643.c: New file.
* iconvdata/cns11643.h: New file.
* iconvdata/cns11643l1.c: New file.
* iconvdata/cns11643l1.h: New file.
* iconvdata/euctw.c: New file.
* iconvdata/Makefile: Add rules for EUC-TW and CNS.
* iconvdata/gconv-modules: Likewise.
* iconvdata/eucjp.c: Several bug fixes and improvements.
* iconvdata/gb2312.h: Likewise.
* iconvdata/jis0201.h: Likewise.
* iconvdata/jis0208.h: Likewise.
* iconvdata/jis0212.h: Likewise.
1998-04-13 09:11 Ulrich Drepper <drepper@cygnus.com>
* iconvdata/eucjp.c: New file.

View File

@ -27,7 +27,7 @@ modules := ISO8859-1 ISO8859-2 ISO8859-3 ISO8859-4 ISO8859-5 \
T.61 ISO_6937 SJIS KOI-8 KOI8-R LATIN-GREEK LATIN-GREEK-1 \
HP-ROMAN8 EBCDIC-AT-DE EBCDIC-AT-DE-A EBCDIC-CA-FR \
EUC-KR UHC JOHAB libJIS libKSC ISO646 BIG5 EUC-JP libGB \
EUC-CN
EUC-CN libCNS EUC-TW
modules.so := $(addsuffix .so, $(modules))
@ -61,9 +61,11 @@ UHC-routines := uhc
BIG5-routines := big5
EUC-JP-routines := eucjp
EUC-CN-routines := euccn
EUC-TW-routines := euctw
libJIS-routines := jis0201 jis0208 jis0212
libKSC-routines := ksc5601
libGB-routines := gb2312
libCNS-routines := cns11643l1 cns11643
LDFLAGS-EUC-KR.so = -Wl,-rpath,$(gconvdir)
$(objpfx)EUC-KR.so: $(objpfx)libKSC.so
@ -75,10 +77,13 @@ LDFLAGS-EUC-JP.so = -Wl,-rpath,$(gconvdir)
$(objpfx)EUC-JP.so: $(objpfx)libJIS.so
LDFLAGS-EUC-CN.so = -Wl,-rpath,$(gconvdir)
$(objpfx)EUC-CN.so: $(objpfx)libGB.so
LDFLAGS-EUC-TW.so = -Wl,-rpath,$(gconvdir)
$(objpfx)EUC-TW.so: $(objpfx)libCNS.so
LDFLAGS-libJIS.so = -Wl,-soname,$(@F)
LDFLAGS-libKSC.so = -Wl,-soname,$(@F)
LDFLAGS-libGB.so = -Wl,-soname,$(@F)
LDFLAGS-libCNS.so = -Wl,-soname,$(@F)
distribute := 8bit-generic.c 8bit-gap.c gap.pl gaptab.pl gconv-modules \
iso8859-1.c iso8859-2.c iso8859-3.c iso8859-4.c iso8859-5.c \
@ -88,7 +93,8 @@ distribute := 8bit-generic.c 8bit-gap.c gap.pl gaptab.pl gconv-modules \
latin-greek.h latin-greek-1.c latin-greek-1.h ebcdic-at-de.c \
ebcdic-at-de-a.c ebcdic-ca-fr.c jis0201.c jis0208.c jis0212.c \
extra-module.mk euckr.c johab.c uhc.c ksc5601.c ksc5601.h \
iso646.c big5.c eucjp.c gb2312.c gb2312.h euccn.c
iso646.c big5.c eucjp.c gb2312.c gb2312.h euccn.c euctw.c \
cns11643l1.c cns11643l1.h cns11643.h cns11643.c
# We build the transformation modules only when we build shared libs.
ifeq (yes,$(build-shared))

11698
iconvdata/cns11643.c Normal file

File diff suppressed because it is too large Load Diff

233
iconvdata/cns11643.h Normal file
View File

@ -0,0 +1,233 @@
/* Access functions for CNS 11643, plane 2 handling.
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <stdint.h>
/* Table for CNS 11643, plane 2 to UCS4 conversion. */
extern const uint16_t cns11643l2_to_ucs4_tab[];
extern const uint16_t cns11643l14_to_ucs4_tab[];
static inline wchar_t
cns11643_to_ucs4 (const char **s, size_t avail, unsigned char offset)
{
unsigned char ch = *(*s);
unsigned char ch2;
unsigned char ch3;
wchar_t result;
int idx;
if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30)
return UNKNOWN_10646_CHAR;
if (avail < 3)
return 0;
ch2 = (*s)[1];
if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
return UNKNOWN_10646_CHAR;
ch3 = (*s)[2];
if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f)
return UNKNOWN_10646_CHAR;
idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset);
if ((ch - 0x21 - offset) == 1)
{
if (idx > 0x2196)
return UNKNOWN_10646_CHAR;
result = cns11643l1_to_ucs4_tab[idx];
}
else if ((ch - 0x21 - offset) == 2)
{
if (idx > 0x1de1)
return UNKNOWN_10646_CHAR;
result = cns11643l2_to_ucs4_tab[idx];
}
else if ((ch - 0x21 - offset) == 0xe)
{
if (idx > 0x19bd)
return UNKNOWN_10646_CHAR;
result = cns11643l14_to_ucs4_tab[idx];
}
else
return UNKNOWN_10646_CHAR;
if (result != L'\0')
(*s) += 3;
else
result = UNKNOWN_10646_CHAR;
return result;
}
/* Tables for the UCS4 -> CNS conversion. */
extern const char cns11643l1_from_ucs4_tab1[][2];
extern const char cns11643l1_from_ucs4_tab2[][2];
extern const char cns11643l1_from_ucs4_tab3[][2];
extern const char cns11643l1_from_ucs4_tab4[][2];
extern const char cns11643l1_from_ucs4_tab5[][2];
extern const char cns11643l1_from_ucs4_tab6[][2];
extern const char cns11643l1_from_ucs4_tab7[][2];
extern const char cns11643l1_from_ucs4_tab8[][2];
extern const char cns11643l1_from_ucs4_tab9[][2];
extern const char cns11643l1_from_ucs4_tab10[][2];
extern const char cns11643l1_from_ucs4_tab11[][2];
extern const char cns11643l1_from_ucs4_tab12[][2];
extern const char cns11643l1_from_ucs4_tab13[][2];
extern const char cns11643l1_from_ucs4_tab14[][2];
extern const char cns11643_from_ucs4_tab[][3];
static inline size_t
ucs4_to_cns11643 (wchar_t wch, char *s, size_t avail)
{
unsigned int ch = (unsigned int) wch;
char buf[2];
const char *cp = NULL;
int needed = 2;
if (ch < 0xa7)
cp = "";
else if (ch < 0xf7)
cp = cns11643l1_from_ucs4_tab1[ch - 0xa7];
else if (ch < 0x2c7)
cp = "";
else if (ch <= 0x2d9)
cp = cns11643l1_from_ucs4_tab2[ch - 0x2c7];
else if (ch < 0x391)
cp = "";
else if (ch <= 0x3c9)
cp = cns11643l1_from_ucs4_tab3[ch - 0x391];
else if (ch < 0x2013)
cp = "";
else if (ch <= 0x203e)
cp = cns11643l1_from_ucs4_tab4[ch - 0x2013];
else if (ch == 0x2103)
cp = "\x22\x6a";
else if (ch == 0x2105)
cp = "\x22\x22";
else if (ch == 0x2109)
cp = "\x22\x6b";
else if (ch < 0x2160)
cp = "";
else if (ch <= 0x2169)
{
buf[0] = '\x24';
buf[1] = '\x2b' + (ch - 0x2160);
cp = buf;
}
else if (ch < 0x2170)
cp = "";
else if (ch <= 0x2179)
{
buf[0] = '\x26';
buf[1] = '\x35' + (ch - 0x2170);
cp = buf;
}
else if (ch < 0x2190)
cp = "";
else if (ch <= 0x2199)
cp = cns11643l1_from_ucs4_tab5[ch - 0x2190];
else if (ch < 0x2215)
cp = "";
else if (ch <= 0x2267)
cp = cns11643l1_from_ucs4_tab6[ch - 0x2215];
else if (ch == 0x22a5)
cp = "\x22\x47";
else if (ch == 0x22bf)
cp = "\x22\x4a";
else if (ch < 0x2400)
cp = "";
else if (ch <= 0x2421)
cp = cns11643l1_from_ucs4_tab7[ch - 0x2400];
else if (ch < 0x2460)
cp = "";
else if (ch <= 0x247d)
cp = cns11643l1_from_ucs4_tab8[ch - 0x2460];
else if (ch < 0x2500)
cp = "";
else if (ch <= 0x2642)
cp = cns11643l1_from_ucs4_tab9[ch - 0x2500];
else if (ch < 0x3000)
cp = "";
else if (ch <= 0x3029)
cp = cns11643l1_from_ucs4_tab10[ch - 0x3000];
else if (ch == 0x30fb)
cp = "\x21\x26";
else if (ch < 0x3105)
cp = "";
else if (ch <= 0x3129)
{
buf[0] = '\x25';
buf[1] = '\x26' + (ch - 0x3105);
cp = buf;
}
else if (ch == 0x32a3)
cp = "\x22\x21";
else if (ch < 0x338e)
cp = "";
else if (ch <= 0x33d5)
cp = cns11643l1_from_ucs4_tab11[ch - 0x338e];
else if (ch < 0x4e00)
cp = "";
else if (ch <= 0x9f9c)
{
cp = cns11643l1_from_ucs4_tab12[ch - 0x4e00];
if (cp[0] == '\0')
{
/* Let's try the other planes. */
needed = 3;
cp = cns11643_from_ucs4_tab[ch - 0x4e00];
}
}
else if (ch < 0xfe30)
cp = "";
else if (ch <= 0xfe6b)
cp = cns11643l1_from_ucs4_tab13[ch - 0xfe30];
else if (ch < 0xff01)
cp = "";
else if (ch <= 0xff5d)
cp = cns11643l1_from_ucs4_tab14[ch - 0xff01];
else if (ch == 0xffe0)
cp = "\x22\x66";
else if (ch == 0xffe1)
cp = "\x22\x67";
else if (ch == 0xffe5)
cp = "\x22\x64";
else
cp = "";
if (cp[0] == '\0')
return UNKNOWN_10646_CHAR;
if (avail < needed)
return 0;
s[0] = cp[0];
s[1] = cp[1];
if (needed == 3)
s[2] = cp[2];
return needed;
}

3743
iconvdata/cns11643l1.c Normal file

File diff suppressed because it is too large Load Diff

191
iconvdata/cns11643l1.h Normal file
View File

@ -0,0 +1,191 @@
/* Access functions for CNS 11643, plane 1 handling.
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <stdint.h>
/* Table for CNS 11643, plane 1 to UCS4 conversion. */
extern const uint16_t cns11643l1_to_ucs4_tab[];
static inline wchar_t
cns11643l1_to_ucs4 (const char **s, size_t avail, unsigned char offset)
{
unsigned char ch = *(*s);
unsigned char ch2;
int idx;
if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d)
return UNKNOWN_10646_CHAR;
if (avail < 2)
return 0;
ch2 = (*s)[1];
if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
return UNKNOWN_10646_CHAR;
idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
if (idx > 0x2196)
return UNKNOWN_10646_CHAR;
(*s) += 2;
return cns11643l1_to_ucs4_tab[idx] ?: ((*s) -= 2, UNKNOWN_10646_CHAR);
}
/* Tables for the UCS4 -> CNS conversion. */
extern const char cns11643l1_from_ucs4_tab1[][2];
extern const char cns11643l1_from_ucs4_tab2[][2];
extern const char cns11643l1_from_ucs4_tab3[][2];
extern const char cns11643l1_from_ucs4_tab4[][2];
extern const char cns11643l1_from_ucs4_tab5[][2];
extern const char cns11643l1_from_ucs4_tab6[][2];
extern const char cns11643l1_from_ucs4_tab7[][2];
extern const char cns11643l1_from_ucs4_tab8[][2];
extern const char cns11643l1_from_ucs4_tab9[][2];
extern const char cns11643l1_from_ucs4_tab10[][2];
extern const char cns11643l1_from_ucs4_tab11[][2];
extern const char cns11643l1_from_ucs4_tab12[][2];
extern const char cns11643l1_from_ucs4_tab13[][2];
extern const char cns11643l1_from_ucs4_tab14[][2];
static inline size_t
ucs4_to_cns11643l1 (wchar_t wch, char *s, size_t avail)
{
unsigned int ch = (unsigned int) wch;
char buf[2];
const char *cp = NULL;
if (ch < 0xa7)
cp = "";
else if (ch < 0xf7)
cp = cns11643l1_from_ucs4_tab1[ch - 0xa7];
else if (ch < 0x2c7)
cp = "";
else if (ch <= 0x2d9)
cp = cns11643l1_from_ucs4_tab2[ch - 0x2c7];
else if (ch < 0x391)
cp = "";
else if (ch <= 0x3c9)
cp = cns11643l1_from_ucs4_tab3[ch - 0x391];
else if (ch < 0x2013)
cp = "";
else if (ch <= 0x203e)
cp = cns11643l1_from_ucs4_tab4[ch - 0x2013];
else if (ch == 0x2103)
cp = "\x22\x6a";
else if (ch == 0x2105)
cp = "\x22\x22";
else if (ch == 0x2109)
cp = "\x22\x6b";
else if (ch < 0x2160)
cp = "";
else if (ch <= 0x2169)
{
buf[0] = '\x24';
buf[1] = '\x2b' + (ch - 0x2160);
cp = buf;
}
else if (ch < 0x2170)
cp = "";
else if (ch <= 0x2179)
{
buf[0] = '\x26';
buf[1] = '\x35' + (ch - 0x2170);
cp = buf;
}
else if (ch < 0x2190)
cp = "";
else if (ch <= 0x2199)
cp = cns11643l1_from_ucs4_tab5[ch - 0x2190];
else if (ch < 0x2215)
cp = "";
else if (ch <= 0x2267)
cp = cns11643l1_from_ucs4_tab6[ch - 0x2215];
else if (ch == 0x22a5)
cp = "\x22\x47";
else if (ch == 0x22bf)
cp = "\x22\x4a";
else if (ch < 0x2400)
cp = "";
else if (ch <= 0x2421)
cp = cns11643l1_from_ucs4_tab7[ch - 0x2400];
else if (ch < 0x2460)
cp = "";
else if (ch <= 0x247d)
cp = cns11643l1_from_ucs4_tab8[ch - 0x2460];
else if (ch < 0x2500)
cp = "";
else if (ch <= 0x2642)
cp = cns11643l1_from_ucs4_tab9[ch - 0x2500];
else if (ch < 0x3000)
cp = "";
else if (ch <= 0x3029)
cp = cns11643l1_from_ucs4_tab10[ch - 0x3000];
else if (ch == 0x30fb)
cp = "\x21\x26";
else if (ch < 0x3105)
cp = "";
else if (ch <= 0x3129)
{
buf[0] = '\x25';
buf[1] = '\x26' + (ch - 0x3105);
cp = buf;
}
else if (ch == 0x32a3)
cp = "\x22\x21";
else if (ch < 0x338e)
cp = "";
else if (ch <= 0x33d5)
cp = cns11643l1_from_ucs4_tab11[ch - 0x338e];
else if (ch < 0x4e00)
cp = "";
else if (ch <= 0x9f9c)
cp = cns11643l1_from_ucs4_tab12[ch - 0x4e00];
else if (ch < 0xfe30)
cp = "";
else if (ch <= 0xfe6b)
cp = cns11643l1_from_ucs4_tab13[ch - 0xfe30];
else if (ch < 0xff01)
cp = "";
else if (ch <= 0xff5d)
cp = cns11643l1_from_ucs4_tab14[ch - 0xff01];
else if (ch == 0xffe0)
cp = "\x22\x66";
else if (ch == 0xffe1)
cp = "\x22\x67";
else if (ch == 0xffe5)
cp = "\x22\x64";
else
cp = "";
if (cp[0] == '\0')
return UNKNOWN_10646_CHAR;
if (avail < 2)
return 0;
s[0] = cp[0];
s[1] = cp[1];
return 2;
}

348
iconvdata/eucjp.c Normal file
View File

@ -0,0 +1,348 @@
/* Mapping tables for EUC-JP handling.
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <gconv.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <jis0201.h>
#include <jis0208.h>
#include <jis0212.h>
/* Direction of the transformation. */
enum direction
{
illegal,
to_eucjp,
from_eucjp
};
struct eucjp_data
{
enum direction dir;
};
int
gconv_init (struct gconv_step *step)
{
/* Determine which direction. */
struct eucjp_data *new_data;
enum direction dir;
int result;
if (strcasestr (step->from_name, "EUC-JP") != NULL)
dir = from_eucjp;
else if (strcasestr (step->to_name, "EUC-JP") != NULL)
dir = to_eucjp;
else
dir = illegal;
result = GCONV_NOCONV;
if (dir != illegal
&& ((new_data
= (struct eucjp_data *) malloc (sizeof (struct eucjp_data)))
!= NULL))
{
new_data->dir = dir;
step->data = new_data;
result = GCONV_OK;
}
return result;
}
void
gconv_end (struct gconv_step *data)
{
free (data->data);
}
int
gconv (struct gconv_step *step, struct gconv_step_data *data,
const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
{
struct gconv_step *next_step = step + 1;
struct gconv_step_data *next_data = data + 1;
gconv_fct fct = next_step->fct;
size_t do_write;
int result;
/* If the function is called with no input this means we have to reset
to the initial state. The possibly partly converted input is
dropped. */
if (do_flush)
{
do_write = 0;
/* Call the steps down the chain if there are any. */
if (data->is_last)
result = GCONV_OK;
else
{
struct gconv_step *next_step = step + 1;
struct gconv_step_data *next_data = data + 1;
result = (*fct) (next_step, next_data, NULL, 0, written, 1);
/* Clear output buffer. */
data->outbufavail = 0;
}
}
else
{
enum direction dir = ((struct eucjp_data *) step->data)->dir;
do_write = 0;
do
{
result = GCONV_OK;
if (dir == from_eucjp)
{
size_t inchars = *inbufsize;
size_t outwchars = data->outbufavail;
char *outbuf = data->outbuf;
size_t cnt = 0;
while (cnt < inchars
&& (outwchars + sizeof (wchar_t) <= data->outbufsize))
{
int inchar = (unsigned char) inbuf[cnt];
wchar_t ch;
if (inchar <= 0x7f)
ch = (wchar_t) inchar;
else if ((inchar <= 0xa0 || inchar > 0xfe)
&& inchar != 0x8e && inchar != 0x8f)
/* This is illegal. */
ch = L'\0';
else
{
/* Two or more byte character. First test whether the
next character is also available. */
int inchar2;
if (cnt + 1 >= inchars)
{
/* The second character is not available. Store
the intermediate result. */
result = GCONV_INCOMPLETE_INPUT;
break;
}
inchar2 = (unsigned char) inbuf[++cnt];
/* All second bytes of a multibyte character must be
>= 0xa1. */
if (inchar2 < 0xa1)
{
/* This is an illegal character. */
--cnt;
result = GCONV_ILLEGAL_INPUT;
break;
}
if (inchar == '\x8e')
/* This is code set 2: half-width katakana. */
ch = jisx0201_to_ucs4 (inchar2);
else if (inchar == '\x8f')
{
/* This is code set 3: JIS X 0212-1990. */
const char *endp = &inbuf[cnt];
ch = jisx0212_to_ucs4 (&endp, 1 + inchars - cnt,
0x80);
cnt = endp - inbuf;
}
else
{
/* This is code set 1: JIS X 0208. */
const char *endp = &inbuf[cnt - 1];
ch = jisx0208_to_ucs4 (&endp, 2 + inchars - cnt,
0x80);
if (ch != L'\0')
++cnt;
}
if (ch == UNKNOWN_10646_CHAR)
ch = L'\0';
if (ch == L'\0')
--cnt;
}
if (ch == L'\0' && inbuf[cnt] != '\0')
{
/* This is an illegal character. */
result = GCONV_ILLEGAL_INPUT;
break;
}
*((wchar_t *) (outbuf + outwchars)) = ch;
++do_write;
outwchars += sizeof (wchar_t);
++cnt;
}
*inbufsize -= cnt;
data->outbufavail = outwchars;
}
else
{
size_t inwchars = *inbufsize;
size_t outchars = data->outbufavail;
char *outbuf = data->outbuf;
size_t cnt = 0;
int extra = 0;
while (inwchars >= cnt + sizeof (wchar_t)
&& outchars < data->outbufsize)
{
wchar_t ch = *((wchar_t *) (inbuf + cnt));
if (ch <= L'\x7f')
/* It's plain ASCII. */
outbuf[outchars] = ch;
else
{
/* Try the JIS character sets. */
size_t found;
found = ucs4_to_jisx0201 (ch, &outbuf[outchars]);
if (found == UNKNOWN_10646_CHAR)
{
/* No JIS 0201 character. */
found = ucs4_to_jisx0208 (ch, &outbuf[outchars],
(data->outbufsize
- outchars));
if (found == 0)
{
/* We ran out of space. */
extra = 2;
break;
}
else if (found != UNKNOWN_10646_CHAR)
{
/* It's a JIS 0208 character, adjust it for
EUC-JP. */
outbuf[outchars++] += 0x80;
outbuf[outchars] += 0x80;
}
else
{
/* No JIS 0208 character. */
found = ucs4_to_jisx0212 (ch, &outbuf[outchars],
(data->outbufsize
- outchars));
if (found == 0)
{
/* We ran out of space. */
extra = 2;
break;
}
else if (found != UNKNOWN_10646_CHAR)
{
/* It's a JIS 0212 character, adjust it for
EUC-JP. */
outbuf[outchars++] += 0x80;
outbuf[outchars] += 0x80;
}
else
/* Illegal character. */
break;
}
}
}
++do_write;
++outchars;
cnt += sizeof (wchar_t);
}
*inbufsize -= cnt;
data->outbufavail = outchars;
if (outchars + extra < data->outbufsize)
{
/* If there is still room in the output buffer something
is wrong with the input. */
if (inwchars >= cnt + sizeof (wchar_t))
{
/* An error occurred. */
result = GCONV_ILLEGAL_INPUT;
break;
}
if (inwchars != cnt)
{
/* There are some unprocessed bytes at the end of the
input buffer. */
result = GCONV_INCOMPLETE_INPUT;
break;
}
}
}
if (result != GCONV_OK)
break;
if (data->is_last)
{
/* This is the last step. */
result = (*inbufsize > (dir == from_eucjp
? 0 : sizeof (wchar_t) - 1)
? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
break;
}
/* Status so far. */
result = GCONV_EMPTY_INPUT;
if (data->outbufavail > 0)
{
/* Call the functions below in the chain. */
size_t newavail = data->outbufavail;
result = (*fct) (next_step, next_data, data->outbuf, &newavail,
written, 0);
/* Correct the output buffer. */
if (newavail != data->outbufavail && newavail > 0)
{
memmove (data->outbuf,
&data->outbuf[data->outbufavail - newavail],
newavail);
data->outbufavail = newavail;
}
}
}
while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
}
if (written != NULL && data->is_last)
*written = do_write;
return result;
}

344
iconvdata/euctw.c Normal file
View File

@ -0,0 +1,344 @@
/* Mapping tables for EUC-TW handling.
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <gconv.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <cns11643l1.h>
#include <cns11643.h>
/* Direction of the transformation. */
enum direction
{
illegal,
to_euctw,
from_euctw
};
struct euctw_data
{
enum direction dir;
};
int
gconv_init (struct gconv_step *step)
{
/* Determine which direction. */
struct euctw_data *new_data;
enum direction dir;
int result;
if (strcasestr (step->from_name, "EUC-TW") != NULL)
dir = from_euctw;
else if (strcasestr (step->to_name, "EUC-TW") != NULL)
dir = to_euctw;
else
dir = illegal;
result = GCONV_NOCONV;
if (dir != illegal
&& ((new_data
= (struct euctw_data *) malloc (sizeof (struct euctw_data)))
!= NULL))
{
new_data->dir = dir;
step->data = new_data;
result = GCONV_OK;
}
return result;
}
void
gconv_end (struct gconv_step *data)
{
free (data->data);
}
int
gconv (struct gconv_step *step, struct gconv_step_data *data,
const char *inbuf, size_t *inbufsize, size_t *written, int do_flush)
{
struct gconv_step *next_step = step + 1;
struct gconv_step_data *next_data = data + 1;
gconv_fct fct = next_step->fct;
size_t do_write;
int result;
/* If the function is called with no input this means we have to reset
to the initial state. The possibly partly converted input is
dropped. */
if (do_flush)
{
do_write = 0;
/* Call the steps down the chain if there are any. */
if (data->is_last)
result = GCONV_OK;
else
{
struct gconv_step *next_step = step + 1;
struct gconv_step_data *next_data = data + 1;
result = (*fct) (next_step, next_data, NULL, 0, written, 1);
/* Clear output buffer. */
data->outbufavail = 0;
}
}
else
{
enum direction dir = ((struct euctw_data *) step->data)->dir;
do_write = 0;
do
{
result = GCONV_OK;
if (dir == from_euctw)
{
size_t inchars = *inbufsize;
size_t outwchars = data->outbufavail;
char *outbuf = data->outbuf;
size_t cnt = 0;
while (cnt < inchars
&& (outwchars + sizeof (wchar_t) <= data->outbufsize))
{
int inchar = (unsigned char) inbuf[cnt];
wchar_t ch;
if (inchar <= 0x7f)
ch = (wchar_t) inchar;
else if ((inchar <= 0xa0 || inchar > 0xfe)
&& inchar != 0x8e)
/* This is illegal. */
ch = L'\0';
else
{
/* Two or more byte character. First test whether the
next character is also available. */
int inchar2;
if (cnt + 1 + (inchar == 0x8e ? 2 : 0) >= inchars)
{
/* The second character is not available. Store
the intermediate result. */
result = GCONV_INCOMPLETE_INPUT;
break;
}
inchar2 = (unsigned char) inbuf[++cnt];
/* All second bytes of a multibyte character must be
>= 0xa1. */
if (inchar2 < 0xa1 && inchar2 == 0xff)
{
/* This is an illegal character. */
--cnt;
result = GCONV_ILLEGAL_INPUT;
break;
}
if (inchar == '\x8e')
{
/* This is code set 2: CNS 11643, planes 1 to 16. */
const char *endp = &inbuf[cnt];
ch = cns11643_to_ucs4 (&endp, 2 + inchars - cnt,
0x80);
if (ch == UNKNOWN_10646_CHAR)
ch = L'\0';
if (ch != L'\0')
cnt += 2;
}
else
{
/* This is code set 1: CNS 11643, plane 1. */
const char *endp = &inbuf[cnt - 1];
ch = cns11643l1_to_ucs4 (&endp, 2 + inchars - cnt,
0x80);
if (ch == UNKNOWN_10646_CHAR)
ch = L'\0';
if (ch != L'\0')
++cnt;
}
if (ch == L'\0')
--cnt;
}
if (ch == L'\0' && inbuf[cnt] != '\0')
{
/* This is an illegal character. */
result = GCONV_ILLEGAL_INPUT;
break;
}
*((wchar_t *) (outbuf + outwchars)) = ch;
++do_write;
outwchars += sizeof (wchar_t);
++cnt;
}
*inbufsize -= cnt;
data->outbufavail = outwchars;
}
else
{
size_t inwchars = *inbufsize;
size_t outchars = data->outbufavail;
char *outbuf = data->outbuf;
size_t cnt = 0;
int extra = 0;
while (inwchars >= cnt + sizeof (wchar_t)
&& outchars < data->outbufsize)
{
wchar_t ch = *((wchar_t *) (inbuf + cnt));
if (ch <= L'\x7f')
/* It's plain ASCII. */
outbuf[outchars] = ch;
else
{
/* Try the JIS character sets. */
size_t found;
found = ucs4_to_cns11643l1 (ch, &outbuf[outchars],
(data->outbufsize
- outchars));
if (found == 0)
{
/* We ran out of space. */
extra = 2;
break;
}
else if (found != UNKNOWN_10646_CHAR)
{
/* It's a CNS 11643, plane 1 character, adjust it
for EUC-TW. */
outbuf[outchars++] += 0x80;
outbuf[outchars] += 0x80;
}
else
{
/* No CNS 11643, plane 1 character. */
outbuf[outchars] = '\x8e';
found = ucs4_to_cns11643 (ch, &outbuf[outchars + 1],
(data->outbufsize
- outchars - 1));
if (found > 0)
{
/* It's a CNS 11643 character, adjust it for
EUC-TW. */
outbuf[++outchars] += 0xa0;
outbuf[++outchars] += 0x80;
outbuf[outchars] += 0x80;
}
else if (found == 0)
{
/* We ran out of space. */
extra = 4;
break;
}
else
/* Illegal character. */
break;
}
}
++do_write;
++outchars;
cnt += sizeof (wchar_t);
}
*inbufsize -= cnt;
data->outbufavail = outchars;
if (outchars + extra < data->outbufsize)
{
/* If there is still room in the output buffer something
is wrong with the input. */
if (inwchars >= cnt + sizeof (wchar_t))
{
/* An error occurred. */
result = GCONV_ILLEGAL_INPUT;
break;
}
if (inwchars != cnt)
{
/* There are some unprocessed bytes at the end of the
input buffer. */
result = GCONV_INCOMPLETE_INPUT;
break;
}
}
}
if (result != GCONV_OK)
break;
if (data->is_last)
{
/* This is the last step. */
result = (*inbufsize > (dir == from_euctw
? 0 : sizeof (wchar_t) - 1)
? GCONV_FULL_OUTPUT : GCONV_EMPTY_INPUT);
break;
}
/* Status so far. */
result = GCONV_EMPTY_INPUT;
if (data->outbufavail > 0)
{
/* Call the functions below in the chain. */
size_t newavail = data->outbufavail;
result = (*fct) (next_step, next_data, data->outbuf, &newavail,
written, 0);
/* Correct the output buffer. */
if (newavail != data->outbufavail && newavail > 0)
{
memmove (data->outbuf,
&data->outbuf[data->outbufavail - newavail],
newavail);
data->outbufavail = newavail;
}
}
}
while (*inbufsize > 0 && result == GCONV_EMPTY_INPUT);
}
if (written != NULL && data->is_last)
*written = do_write;
return result;
}

View File

@ -46,7 +46,7 @@ gb2312_to_ucs4 (const char **s, size_t avail, unsigned char offset)
return UNKNOWN_10646_CHAR;
idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
if (idx >= 0x1ff1)
if (idx > 0x1ff1)
return UNKNOWN_10646_CHAR;
(*s) += 2;
@ -73,7 +73,7 @@ ucs4_to_gb2312 (wchar_t wch, char *s, size_t avail)
const char *cp = NULL;
if (ch < 0xa4)
cp = NULL;
return UNKNOWN_10646_CHAR;
else if (ch < 0x101)
cp = gb2312_from_ucs4_tab1[ch - 0xa4];
else if (ch == 0x113)

View File

@ -236,3 +236,8 @@ module ISO-10646/UCS4/ EUC-JP// EUC-JP 1
alias EUCCN// EUC-CN//
module EUC-CN// ISO-10646/UCS4/ EUC-CN 1
module ISO-10646/UCS4/ EUC-CN// EUC-CN 1
# from to module cost
alias EUCTW// EUC-TW//
module EUC-TW// ISO-10646/UCS4/ EUC-TW 1
module ISO-10646/UCS4/ EUC-TW// EUC-TW 1

View File

@ -1,5 +1,5 @@
/* Access functions for JISX0201 conversion.
Copyright (C) 1997 Free Software Foundation, Inc.
Copyright (C) 1997, 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@ -26,11 +26,11 @@ extern const wchar_t jis0201_to_ucs4[];
static inline wchar_t
jisx0201_to_ucs4 (char **s, size_t avail __attribute__ ((unused)))
jisx0201_to_ucs4 (char ch)
{
wchar_t val = jis0201_to_ucs4[*(unsigned char *) (*s)];
wchar_t val = jis0201_to_ucs4[(unsigned char) ch];
if (val == 0 && **s != '\0')
if (val == 0 && ch != '\0')
val = UNKNOWN_10646_CHAR;
return val;
@ -38,7 +38,7 @@ jisx0201_to_ucs4 (char **s, size_t avail __attribute__ ((unused)))
static inline size_t
ucs4_to_jisx0201 (wchar_t wch, char **s, size_t avail __attribute__ ((unused)))
ucs4_to_jisx0201 (wchar_t wch, char *s)
{
char ch;
@ -51,9 +51,9 @@ ucs4_to_jisx0201 (wchar_t wch, char **s, size_t avail __attribute__ ((unused)))
else if (wch >= 0xff61 && wch <= 0xff9f)
ch = wch - 0xfec0;
else
return 0;
return UNKNOWN_10646_CHAR;
*(*s)++ = ch;
s[0] = ch;
return 1;
}

View File

@ -1,5 +1,5 @@
/* Access functions for JISX0208 conversion.
Copyright (C) 1997 Free Software Foundation, Inc.
Copyright (C) 1997, 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@ -43,23 +43,23 @@ struct jisx0208_ucs_idx
static inline wchar_t
jisx0208_to_ucs4 (char **s, size_t avail)
jisx0208_to_ucs4 (const char **s, size_t avail, unsigned char offset)
{
unsigned char ch = *(*s);
unsigned char ch2;
int idx;
if (ch <= 0x20 || ch > 0xea)
if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0xea)
return UNKNOWN_10646_CHAR;
if (avail < 2)
return 0;
ch2 = (*s)[1];
if (ch2 <= 0x20 || ch2 >= 0x7f)
if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
return UNKNOWN_10646_CHAR;
idx = (ch - 0x21) * 94 + (ch2 - 0x21);
idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
if (idx >= 0x1e80)
return UNKNOWN_10646_CHAR;
@ -70,7 +70,7 @@ jisx0208_to_ucs4 (char **s, size_t avail)
static inline size_t
ucs4_to_jisx0208 (wchar_t wch, char **s, size_t avail)
ucs4_to_jisx0208 (wchar_t wch, char *s, size_t avail)
{
unsigned int ch = (unsigned int) wch;
const char *cp = NULL;
@ -95,8 +95,8 @@ ucs4_to_jisx0208 (wchar_t wch, char **s, size_t avail)
if (cp == NULL || cp[0] == '\0')
return UNKNOWN_10646_CHAR;
*(*s)++ = cp[0];
*(*s)++ = cp[1];
s[0] = cp[0];
s[1] = cp[1];
return 2;
}

View File

@ -1,5 +1,5 @@
/* Access functions for JISX0212 conversion.
Copyright (C) 1997 Free Software Foundation, Inc.
Copyright (C) 1997, 1998 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
@ -42,7 +42,7 @@ extern const char jisx0212_from_ucs[][2];
static inline wchar_t
jisx0212_to_ucs4 (char **s, size_t avail)
jisx0212_to_ucs4 (const char **s, size_t avail, unsigned char offset)
{
const struct jisx0212_idx *rp = jisx0212_to_ucs_idx;
unsigned char ch = *(*s);
@ -50,17 +50,17 @@ jisx0212_to_ucs4 (char **s, size_t avail)
wchar_t wch = L'\0';
int idx;
if (ch <= 0x6d || ch > 0xea)
if (ch < offset || (ch - offset) <= 0x6d || (ch - offset) > 0xea)
return UNKNOWN_10646_CHAR;
if (avail < 2)
return 0;
ch2 = (*s)[1];
if (ch2 <= 0x20 || ch2 >= 0x7f)
if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
return UNKNOWN_10646_CHAR;
idx = (ch - 0x21) * 94 + (ch2 - 0x21);
idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset);
while (idx < rp->start)
++rp;
@ -77,7 +77,7 @@ jisx0212_to_ucs4 (char **s, size_t avail)
static inline size_t
ucs4_to_jisx0212 (wchar_t wch, char **s, size_t avail)
ucs4_to_jisx0212 (wchar_t wch, char *s, size_t avail)
{
const struct jisx0212_idx *rp = jisx0212_from_ucs_idx;
unsigned int ch = (unsigned int) wch;
@ -91,15 +91,14 @@ ucs4_to_jisx0212 (wchar_t wch, char **s, size_t avail)
if (cp == NULL || cp[0] == '\0')
return UNKNOWN_10646_CHAR;
**s = cp[0];
s[0] = cp[0];
if (cp[1] != '\0')
{
if (avail < 2)
return 0;
*++(*s) = cp[1];
s[1] = cp[1];
}
++(*s);
return 2;
}