xref: /haiku/src/libs/iconv/hz.h (revision aef5731f38da6f7b913e0f64acd8a40555491ce5)
1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe  * Copyright (C) 1999-2001 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, Fifth Floor, Boston, MA 02110-1301, USA.
18*aef5731fSOliver Tappe  */
19*aef5731fSOliver Tappe 
20*aef5731fSOliver Tappe /*
21*aef5731fSOliver Tappe  * HZ
22*aef5731fSOliver Tappe  */
23*aef5731fSOliver Tappe 
24*aef5731fSOliver Tappe /* Specification: RFC 1842, RFC 1843 */
25*aef5731fSOliver Tappe 
26*aef5731fSOliver Tappe /*
27*aef5731fSOliver Tappe  * The state is 1 in GB mode, 0 in ASCII mode.
28*aef5731fSOliver Tappe  */
29*aef5731fSOliver Tappe 
30*aef5731fSOliver Tappe static int
hz_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)31*aef5731fSOliver Tappe hz_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
32*aef5731fSOliver Tappe {
33*aef5731fSOliver Tappe   state_t state = conv->istate;
34*aef5731fSOliver Tappe   unsigned int count = 0;
35*aef5731fSOliver Tappe   unsigned char c;
36*aef5731fSOliver Tappe   for (;;) {
37*aef5731fSOliver Tappe     c = *s;
38*aef5731fSOliver Tappe     if (c == '~') {
39*aef5731fSOliver Tappe       if (n < count+2)
40*aef5731fSOliver Tappe         goto none;
41*aef5731fSOliver Tappe       c = s[1];
42*aef5731fSOliver Tappe       if (state == 0) {
43*aef5731fSOliver Tappe         if (c == '~') {
44*aef5731fSOliver Tappe           *pwc = (ucs4_t) '~';
45*aef5731fSOliver Tappe           conv->istate = state;
46*aef5731fSOliver Tappe           return count+2;
47*aef5731fSOliver Tappe         }
48*aef5731fSOliver Tappe         if (c == '{') {
49*aef5731fSOliver Tappe           state = 1;
50*aef5731fSOliver Tappe           s += 2; count += 2;
51*aef5731fSOliver Tappe           if (n < count+1)
52*aef5731fSOliver Tappe             goto none;
53*aef5731fSOliver Tappe           continue;
54*aef5731fSOliver Tappe         }
55*aef5731fSOliver Tappe         if (c == '\n') {
56*aef5731fSOliver Tappe           s += 2; count += 2;
57*aef5731fSOliver Tappe           if (n < count+1)
58*aef5731fSOliver Tappe             goto none;
59*aef5731fSOliver Tappe           continue;
60*aef5731fSOliver Tappe         }
61*aef5731fSOliver Tappe       } else {
62*aef5731fSOliver Tappe         if (c == '}') {
63*aef5731fSOliver Tappe           state = 0;
64*aef5731fSOliver Tappe           s += 2; count += 2;
65*aef5731fSOliver Tappe           if (n < count+1)
66*aef5731fSOliver Tappe             goto none;
67*aef5731fSOliver Tappe           continue;
68*aef5731fSOliver Tappe         }
69*aef5731fSOliver Tappe       }
70*aef5731fSOliver Tappe       return RET_ILSEQ;
71*aef5731fSOliver Tappe     }
72*aef5731fSOliver Tappe     break;
73*aef5731fSOliver Tappe   }
74*aef5731fSOliver Tappe   if (state == 0) {
75*aef5731fSOliver Tappe     *pwc = (ucs4_t) c;
76*aef5731fSOliver Tappe     conv->istate = state;
77*aef5731fSOliver Tappe     return count+1;
78*aef5731fSOliver Tappe   } else {
79*aef5731fSOliver Tappe     int ret;
80*aef5731fSOliver Tappe     if (n < count+2)
81*aef5731fSOliver Tappe       goto none;
82*aef5731fSOliver Tappe     ret = gb2312_mbtowc(conv,pwc,s,2);
83*aef5731fSOliver Tappe     if (ret == RET_ILSEQ)
84*aef5731fSOliver Tappe       return RET_ILSEQ;
85*aef5731fSOliver Tappe     if (ret != 2) abort();
86*aef5731fSOliver Tappe     conv->istate = state;
87*aef5731fSOliver Tappe     return count+2;
88*aef5731fSOliver Tappe   }
89*aef5731fSOliver Tappe 
90*aef5731fSOliver Tappe none:
91*aef5731fSOliver Tappe   conv->istate = state;
92*aef5731fSOliver Tappe   return RET_TOOFEW(count);
93*aef5731fSOliver Tappe }
94*aef5731fSOliver Tappe 
95*aef5731fSOliver Tappe static int
hz_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)96*aef5731fSOliver Tappe hz_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
97*aef5731fSOliver Tappe {
98*aef5731fSOliver Tappe   state_t state = conv->ostate;
99*aef5731fSOliver Tappe   unsigned char buf[2];
100*aef5731fSOliver Tappe   int ret;
101*aef5731fSOliver Tappe 
102*aef5731fSOliver Tappe   /* Code set 0 (ASCII or GB 1988-89) */
103*aef5731fSOliver Tappe   ret = ascii_wctomb(conv,buf,wc,1);
104*aef5731fSOliver Tappe   if (ret != RET_ILUNI) {
105*aef5731fSOliver Tappe     if (ret != 1) abort();
106*aef5731fSOliver Tappe     if (buf[0] < 0x80) {
107*aef5731fSOliver Tappe       int count = (state ? 3 : 1);
108*aef5731fSOliver Tappe       if (n < count)
109*aef5731fSOliver Tappe         return RET_TOOSMALL;
110*aef5731fSOliver Tappe       if (state) {
111*aef5731fSOliver Tappe         r[0] = '~';
112*aef5731fSOliver Tappe         r[1] = '}';
113*aef5731fSOliver Tappe         r += 2;
114*aef5731fSOliver Tappe         state = 0;
115*aef5731fSOliver Tappe       }
116*aef5731fSOliver Tappe       r[0] = buf[0];
117*aef5731fSOliver Tappe       conv->ostate = state;
118*aef5731fSOliver Tappe       return count;
119*aef5731fSOliver Tappe     }
120*aef5731fSOliver Tappe   }
121*aef5731fSOliver Tappe 
122*aef5731fSOliver Tappe   /* Code set 1 (GB 2312-1980) */
123*aef5731fSOliver Tappe   ret = gb2312_wctomb(conv,buf,wc,2);
124*aef5731fSOliver Tappe   if (ret != RET_ILUNI) {
125*aef5731fSOliver Tappe     if (ret != 2) abort();
126*aef5731fSOliver Tappe     if (buf[0] < 0x80 && buf[1] < 0x80) {
127*aef5731fSOliver Tappe       int count = (state ? 2 : 4);
128*aef5731fSOliver Tappe       if (n < count)
129*aef5731fSOliver Tappe         return RET_TOOSMALL;
130*aef5731fSOliver Tappe       if (!state) {
131*aef5731fSOliver Tappe         r[0] = '~';
132*aef5731fSOliver Tappe         r[1] = '{';
133*aef5731fSOliver Tappe         r += 2;
134*aef5731fSOliver Tappe         state = 1;
135*aef5731fSOliver Tappe       }
136*aef5731fSOliver Tappe       r[0] = buf[0];
137*aef5731fSOliver Tappe       r[1] = buf[1];
138*aef5731fSOliver Tappe       conv->ostate = state;
139*aef5731fSOliver Tappe       return count;
140*aef5731fSOliver Tappe     }
141*aef5731fSOliver Tappe   }
142*aef5731fSOliver Tappe 
143*aef5731fSOliver Tappe   return RET_ILUNI;
144*aef5731fSOliver Tappe }
145*aef5731fSOliver Tappe 
146*aef5731fSOliver Tappe static int
hz_reset(conv_t conv,unsigned char * r,int n)147*aef5731fSOliver Tappe hz_reset (conv_t conv, unsigned char *r, int n)
148*aef5731fSOliver Tappe {
149*aef5731fSOliver Tappe   state_t state = conv->ostate;
150*aef5731fSOliver Tappe   if (state) {
151*aef5731fSOliver Tappe     if (n < 2)
152*aef5731fSOliver Tappe       return RET_TOOSMALL;
153*aef5731fSOliver Tappe     r[0] = '~';
154*aef5731fSOliver Tappe     r[1] = '}';
155*aef5731fSOliver Tappe     /* conv->ostate = 0; will be done by the caller */
156*aef5731fSOliver Tappe     return 2;
157*aef5731fSOliver Tappe   } else
158*aef5731fSOliver Tappe     return 0;
159*aef5731fSOliver Tappe }
160