Update the testsuite for changed GB18030 converter.
[libiconv.git] / lib / hz.h
blob3242ea971505aa3432f770d8adad8ac6719418be
1 /*
2 * Copyright (C) 1999-2000 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
5 * The GNU LIBICONV Library is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * The GNU LIBICONV Library is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, write to the Free Software Foundation, Inc., 59 Temple Place -
18 * Suite 330, Boston, MA 02111-1307, USA.
22 * HZ
25 /* Specification: RFC 1842, RFC 1843 */
28 * The state is 1 in GB mode, 0 in ASCII mode.
31 static int
32 hz_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
34 state_t state = conv->istate;
35 unsigned int count = 0;
36 unsigned char c;
37 for (;;) {
38 c = *s;
39 if (c == '~') {
40 if (n < count+2)
41 goto none;
42 c = s[1];
43 if (state == 0) {
44 if (c == '~') {
45 *pwc = (ucs4_t) '~';
46 conv->istate = state;
47 return count+2;
49 if (c == '{') {
50 state = 1;
51 s += 2; count += 2;
52 if (n < count+1)
53 goto none;
54 continue;
56 if (c == '\n') {
57 s += 2; count += 2;
58 if (n < count+1)
59 goto none;
60 continue;
62 } else {
63 if (c == '}') {
64 state = 0;
65 s += 2; count += 2;
66 if (n < count+1)
67 goto none;
68 continue;
71 return RET_ILSEQ;
73 break;
75 if (state == 0) {
76 *pwc = (ucs4_t) c;
77 conv->istate = state;
78 return count+1;
79 } else {
80 int ret;
81 if (n < count+2)
82 goto none;
83 ret = gb2312_mbtowc(conv,pwc,s,2);
84 if (ret == RET_ILSEQ)
85 return RET_ILSEQ;
86 if (ret != 2) abort();
87 conv->istate = state;
88 return count+2;
91 none:
92 conv->istate = state;
93 return RET_TOOFEW(count);
96 static int
97 hz_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
99 state_t state = conv->ostate;
100 unsigned char buf[2];
101 int ret;
103 /* Code set 0 (ASCII or GB 1988-89) */
104 ret = ascii_wctomb(conv,buf,wc,1);
105 if (ret != RET_ILSEQ) {
106 if (ret != 1) abort();
107 if (buf[0] < 0x80) {
108 int count = (state ? 3 : 1);
109 if (n < count)
110 return RET_TOOSMALL;
111 if (state) {
112 r[0] = '~';
113 r[1] = '}';
114 r += 2;
115 state = 0;
117 r[0] = buf[0];
118 conv->ostate = state;
119 return count;
123 /* Code set 1 (GB 2312-1980) */
124 ret = gb2312_wctomb(conv,buf,wc,2);
125 if (ret != RET_ILSEQ) {
126 if (ret != 2) abort();
127 if (buf[0] < 0x80 && buf[1] < 0x80) {
128 int count = (state ? 2 : 4);
129 if (n < count)
130 return RET_TOOSMALL;
131 if (!state) {
132 r[0] = '~';
133 r[1] = '{';
134 r += 2;
135 state = 1;
137 r[0] = buf[0];
138 r[1] = buf[1];
139 conv->ostate = state;
140 return count;
144 return RET_ILSEQ;
147 static int
148 hz_reset (conv_t conv, unsigned char *r, int n)
150 state_t state = conv->ostate;
151 if (state) {
152 if (n < 2)
153 return RET_TOOSMALL;
154 r[0] = '~';
155 r[1] = '}';
156 /* conv->ostate = 0; will be done by the caller */
157 return 2;
158 } else
159 return 0;