xref: /haiku/src/libs/iconv/dec_hanyu.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 2001, 2005 Free Software Foundation, Inc.
3*aef5731fSOliver Tappe  * This file is part of the GNU LIBICONV Library.
4*aef5731fSOliver Tappe  *
5*aef5731fSOliver Tappe  * The GNU LIBICONV Library is free software; you can redistribute it
6*aef5731fSOliver Tappe  * and/or modify it under the terms of the GNU Library General Public
7*aef5731fSOliver Tappe  * License as published by the Free Software Foundation; either version 2
8*aef5731fSOliver Tappe  * of the License, or (at your option) any later version.
9*aef5731fSOliver Tappe  *
10*aef5731fSOliver Tappe  * The GNU LIBICONV Library is distributed in the hope that it will be
11*aef5731fSOliver Tappe  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12*aef5731fSOliver Tappe  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*aef5731fSOliver Tappe  * Library General Public License for more details.
14*aef5731fSOliver Tappe  *
15*aef5731fSOliver Tappe  * You should have received a copy of the GNU Library General Public
16*aef5731fSOliver Tappe  * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17*aef5731fSOliver Tappe  * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18*aef5731fSOliver Tappe  * Fifth Floor, Boston, MA 02110-1301, USA.
19*aef5731fSOliver Tappe  */
20*aef5731fSOliver Tappe 
21*aef5731fSOliver Tappe /*
22*aef5731fSOliver Tappe  * DEC-HANYU
23*aef5731fSOliver Tappe  */
24*aef5731fSOliver Tappe 
25*aef5731fSOliver Tappe static int
dec_hanyu_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)26*aef5731fSOliver Tappe dec_hanyu_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
27*aef5731fSOliver Tappe {
28*aef5731fSOliver Tappe   unsigned char c = *s;
29*aef5731fSOliver Tappe   /* Code set 0 (ASCII) */
30*aef5731fSOliver Tappe   if (c < 0x80)
31*aef5731fSOliver Tappe     return ascii_mbtowc(conv,pwc,s,n);
32*aef5731fSOliver Tappe   /* Code set 1 (CNS 11643-1992 Plane 1),
33*aef5731fSOliver Tappe      Code set 2 (CNS 11643-1992 Plane 2),
34*aef5731fSOliver Tappe      Code set 3 (CNS 11643-1992 Plane 3) */
35*aef5731fSOliver Tappe   if (c >= 0xa1 && c < 0xff) {
36*aef5731fSOliver Tappe     if (n < 2)
37*aef5731fSOliver Tappe       return RET_TOOFEW(0);
38*aef5731fSOliver Tappe     {
39*aef5731fSOliver Tappe       unsigned char c2 = s[1];
40*aef5731fSOliver Tappe       if (c == 0xc2 && c2 == 0xcb) {
41*aef5731fSOliver Tappe         if (n < 4)
42*aef5731fSOliver Tappe           return RET_TOOFEW(0);
43*aef5731fSOliver Tappe         if (s[2] >= 0xa1 && s[2] < 0xff && s[3] >= 0xa1 && s[3] < 0xff) {
44*aef5731fSOliver Tappe           unsigned char buf[2];
45*aef5731fSOliver Tappe           int ret;
46*aef5731fSOliver Tappe           buf[0] = s[2]-0x80; buf[1] = s[3]-0x80;
47*aef5731fSOliver Tappe           ret = cns11643_3_mbtowc(conv,pwc,buf,2);
48*aef5731fSOliver Tappe           if (ret != RET_ILSEQ) {
49*aef5731fSOliver Tappe             if (ret != 2) abort();
50*aef5731fSOliver Tappe             return 4;
51*aef5731fSOliver Tappe           }
52*aef5731fSOliver Tappe         }
53*aef5731fSOliver Tappe       } else if (c2 >= 0xa1 && c2 < 0xff) {
54*aef5731fSOliver Tappe         if (c != 0xc2 || c2 < 0xc2) {
55*aef5731fSOliver Tappe           unsigned char buf[2];
56*aef5731fSOliver Tappe           buf[0] = c-0x80; buf[1] = c2-0x80;
57*aef5731fSOliver Tappe           return cns11643_1_mbtowc(conv,pwc,buf,2);
58*aef5731fSOliver Tappe         }
59*aef5731fSOliver Tappe       } else if (c2 >= 0x21 && c2 < 0x7f) {
60*aef5731fSOliver Tappe         unsigned char buf[2];
61*aef5731fSOliver Tappe         buf[0] = c-0x80; buf[1] = c2;
62*aef5731fSOliver Tappe         return cns11643_2_mbtowc(conv,pwc,buf,2);
63*aef5731fSOliver Tappe       }
64*aef5731fSOliver Tappe     }
65*aef5731fSOliver Tappe   }
66*aef5731fSOliver Tappe   return RET_ILSEQ;
67*aef5731fSOliver Tappe }
68*aef5731fSOliver Tappe 
69*aef5731fSOliver Tappe static int
dec_hanyu_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)70*aef5731fSOliver Tappe dec_hanyu_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
71*aef5731fSOliver Tappe {
72*aef5731fSOliver Tappe   unsigned char buf[3];
73*aef5731fSOliver Tappe   int ret;
74*aef5731fSOliver Tappe 
75*aef5731fSOliver Tappe   /* Code set 0 (ASCII) */
76*aef5731fSOliver Tappe   ret = ascii_wctomb(conv,r,wc,n);
77*aef5731fSOliver Tappe   if (ret != RET_ILUNI)
78*aef5731fSOliver Tappe     return ret;
79*aef5731fSOliver Tappe 
80*aef5731fSOliver Tappe   ret = cns11643_wctomb(conv,buf,wc,3);
81*aef5731fSOliver Tappe   if (ret != RET_ILUNI) {
82*aef5731fSOliver Tappe     if (ret != 3) abort();
83*aef5731fSOliver Tappe 
84*aef5731fSOliver Tappe     /* Code set 1 (CNS 11643-1992 Plane 1) */
85*aef5731fSOliver Tappe     if (buf[0] == 1 && (buf[1] != 0x42 || buf[2] < 0x42)) {
86*aef5731fSOliver Tappe       if (n < 2)
87*aef5731fSOliver Tappe         return RET_TOOSMALL;
88*aef5731fSOliver Tappe       r[0] = buf[1]+0x80;
89*aef5731fSOliver Tappe       r[1] = buf[2]+0x80;
90*aef5731fSOliver Tappe       return 2;
91*aef5731fSOliver Tappe     }
92*aef5731fSOliver Tappe 
93*aef5731fSOliver Tappe     /* Code set 2 (CNS 11643-1992 Plane 2) */
94*aef5731fSOliver Tappe     if (buf[0] == 2) {
95*aef5731fSOliver Tappe       if (n < 2)
96*aef5731fSOliver Tappe         return RET_TOOSMALL;
97*aef5731fSOliver Tappe       r[0] = buf[1]+0x80;
98*aef5731fSOliver Tappe       r[1] = buf[2];
99*aef5731fSOliver Tappe       return 2;
100*aef5731fSOliver Tappe     }
101*aef5731fSOliver Tappe 
102*aef5731fSOliver Tappe     /* Code set 3 (CNS 11643-1992 Plane 3) */
103*aef5731fSOliver Tappe     if (buf[0] == 3) {
104*aef5731fSOliver Tappe       if (n < 4)
105*aef5731fSOliver Tappe         return RET_TOOSMALL;
106*aef5731fSOliver Tappe       r[0] = 0xc2;
107*aef5731fSOliver Tappe       r[1] = 0xcb;
108*aef5731fSOliver Tappe       r[2] = buf[1]+0x80;
109*aef5731fSOliver Tappe       r[3] = buf[2]+0x80;
110*aef5731fSOliver Tappe       return 4;
111*aef5731fSOliver Tappe     }
112*aef5731fSOliver Tappe   }
113*aef5731fSOliver Tappe 
114*aef5731fSOliver Tappe   return RET_ILUNI;
115*aef5731fSOliver Tappe }
116