1ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_gbk2k.c,v 1.7 2008/06/14 16:01:07 tnozaki Exp $ */
2ad30f8e7SGabor Kovesdan
3ad30f8e7SGabor Kovesdan /*-
45e53a4f9SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause
55e53a4f9SPedro F. Giffuni *
6ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project,
7ad30f8e7SGabor Kovesdan * All rights reserved.
8ad30f8e7SGabor Kovesdan *
9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan * are met:
12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan *
18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan */
30ad30f8e7SGabor Kovesdan
31ad30f8e7SGabor Kovesdan #include <sys/types.h>
32ad30f8e7SGabor Kovesdan
33ad30f8e7SGabor Kovesdan #include <assert.h>
34ad30f8e7SGabor Kovesdan #include <errno.h>
35ad30f8e7SGabor Kovesdan #include <limits.h>
36ad30f8e7SGabor Kovesdan #include <stdbool.h>
37ad30f8e7SGabor Kovesdan #include <stddef.h>
38ad30f8e7SGabor Kovesdan #include <stdio.h>
39ad30f8e7SGabor Kovesdan #include <stdlib.h>
40ad30f8e7SGabor Kovesdan #include <string.h>
41ad30f8e7SGabor Kovesdan #include <wchar.h>
42ad30f8e7SGabor Kovesdan
43ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
44ad30f8e7SGabor Kovesdan #include "citrus_types.h"
45ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
46ad30f8e7SGabor Kovesdan #include "citrus_module.h"
47ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
48ad30f8e7SGabor Kovesdan #include "citrus_gbk2k.h"
49ad30f8e7SGabor Kovesdan
50ad30f8e7SGabor Kovesdan
51ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
52ad30f8e7SGabor Kovesdan * private stuffs used by templates
53ad30f8e7SGabor Kovesdan */
54ad30f8e7SGabor Kovesdan
55ad30f8e7SGabor Kovesdan typedef struct _GBK2KState {
56ad30f8e7SGabor Kovesdan int chlen;
57ad30f8e7SGabor Kovesdan char ch[4];
58ad30f8e7SGabor Kovesdan } _GBK2KState;
59ad30f8e7SGabor Kovesdan
60ad30f8e7SGabor Kovesdan typedef struct {
61ad30f8e7SGabor Kovesdan int mb_cur_max;
62ad30f8e7SGabor Kovesdan } _GBK2KEncodingInfo;
63ad30f8e7SGabor Kovesdan
64ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
65ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
66ad30f8e7SGabor Kovesdan
67ad30f8e7SGabor Kovesdan #define _FUNCNAME(m) _citrus_GBK2K_##m
68ad30f8e7SGabor Kovesdan #define _ENCODING_INFO _GBK2KEncodingInfo
69ad30f8e7SGabor Kovesdan #define _ENCODING_STATE _GBK2KState
70ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
71ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT 0
72ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
73ad30f8e7SGabor Kovesdan
74ad30f8e7SGabor Kovesdan static __inline void
75ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei __unused,_GBK2KState * __restrict s)76ad30f8e7SGabor Kovesdan _citrus_GBK2K_init_state(_GBK2KEncodingInfo * __restrict ei __unused,
77ad30f8e7SGabor Kovesdan _GBK2KState * __restrict s)
78ad30f8e7SGabor Kovesdan {
79ad30f8e7SGabor Kovesdan
80ad30f8e7SGabor Kovesdan memset(s, 0, sizeof(*s));
81ad30f8e7SGabor Kovesdan }
82ad30f8e7SGabor Kovesdan
83b61949ddSDimitry Andric #if 0
84ad30f8e7SGabor Kovesdan static __inline void
85ad30f8e7SGabor Kovesdan /*ARGSUSED*/
86ad30f8e7SGabor Kovesdan _citrus_GBK2K_pack_state(_GBK2KEncodingInfo * __restrict ei __unused,
87ad30f8e7SGabor Kovesdan void * __restrict pspriv, const _GBK2KState * __restrict s)
88ad30f8e7SGabor Kovesdan {
89ad30f8e7SGabor Kovesdan
90ad30f8e7SGabor Kovesdan memcpy(pspriv, (const void *)s, sizeof(*s));
91ad30f8e7SGabor Kovesdan }
92ad30f8e7SGabor Kovesdan
93ad30f8e7SGabor Kovesdan static __inline void
94ad30f8e7SGabor Kovesdan /*ARGSUSED*/
95ad30f8e7SGabor Kovesdan _citrus_GBK2K_unpack_state(_GBK2KEncodingInfo * __restrict ei __unused,
96ad30f8e7SGabor Kovesdan _GBK2KState * __restrict s, const void * __restrict pspriv)
97ad30f8e7SGabor Kovesdan {
98ad30f8e7SGabor Kovesdan
99ad30f8e7SGabor Kovesdan memcpy((void *)s, pspriv, sizeof(*s));
100ad30f8e7SGabor Kovesdan }
101b61949ddSDimitry Andric #endif
102ad30f8e7SGabor Kovesdan
103ad30f8e7SGabor Kovesdan static __inline bool
_mb_singlebyte(int c)104ad30f8e7SGabor Kovesdan _mb_singlebyte(int c)
105ad30f8e7SGabor Kovesdan {
106ad30f8e7SGabor Kovesdan
107ad30f8e7SGabor Kovesdan return ((c & 0xff) <= 0x7f);
108ad30f8e7SGabor Kovesdan }
109ad30f8e7SGabor Kovesdan
110ad30f8e7SGabor Kovesdan static __inline bool
_mb_leadbyte(int c)111ad30f8e7SGabor Kovesdan _mb_leadbyte(int c)
112ad30f8e7SGabor Kovesdan {
113ad30f8e7SGabor Kovesdan
114ad30f8e7SGabor Kovesdan c &= 0xff;
115ad30f8e7SGabor Kovesdan return (0x81 <= c && c <= 0xfe);
116ad30f8e7SGabor Kovesdan }
117ad30f8e7SGabor Kovesdan
118ad30f8e7SGabor Kovesdan static __inline bool
_mb_trailbyte(int c)119ad30f8e7SGabor Kovesdan _mb_trailbyte(int c)
120ad30f8e7SGabor Kovesdan {
121ad30f8e7SGabor Kovesdan
122ad30f8e7SGabor Kovesdan c &= 0xff;
123ad30f8e7SGabor Kovesdan return ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfe));
124ad30f8e7SGabor Kovesdan }
125ad30f8e7SGabor Kovesdan
126ad30f8e7SGabor Kovesdan static __inline bool
_mb_surrogate(int c)127ad30f8e7SGabor Kovesdan _mb_surrogate(int c)
128ad30f8e7SGabor Kovesdan {
129ad30f8e7SGabor Kovesdan
130ad30f8e7SGabor Kovesdan c &= 0xff;
131ad30f8e7SGabor Kovesdan return (0x30 <= c && c <= 0x39);
132ad30f8e7SGabor Kovesdan }
133ad30f8e7SGabor Kovesdan
134ad30f8e7SGabor Kovesdan static __inline int
_mb_count(wchar_t v)135ad30f8e7SGabor Kovesdan _mb_count(wchar_t v)
136ad30f8e7SGabor Kovesdan {
137ad30f8e7SGabor Kovesdan uint32_t c;
138ad30f8e7SGabor Kovesdan
139ad30f8e7SGabor Kovesdan c = (uint32_t)v; /* XXX */
140ad30f8e7SGabor Kovesdan if (!(c & 0xffffff00))
141ad30f8e7SGabor Kovesdan return (1);
142ad30f8e7SGabor Kovesdan if (!(c & 0xffff0000))
143ad30f8e7SGabor Kovesdan return (2);
144ad30f8e7SGabor Kovesdan return (4);
145ad30f8e7SGabor Kovesdan }
146ad30f8e7SGabor Kovesdan
147ad30f8e7SGabor Kovesdan #define _PSENC (psenc->ch[psenc->chlen - 1])
148ad30f8e7SGabor Kovesdan #define _PUSH_PSENC(c) (psenc->ch[psenc->chlen++] = (c))
149ad30f8e7SGabor Kovesdan
150ad30f8e7SGabor Kovesdan static int
_citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei,wchar_t * __restrict pwc,char ** __restrict s,size_t n,_GBK2KState * __restrict psenc,size_t * __restrict nresult)151ad30f8e7SGabor Kovesdan _citrus_GBK2K_mbrtowc_priv(_GBK2KEncodingInfo * __restrict ei,
1521243a98eSTijl Coosemans wchar_t * __restrict pwc, char ** __restrict s, size_t n,
153ad30f8e7SGabor Kovesdan _GBK2KState * __restrict psenc, size_t * __restrict nresult)
154ad30f8e7SGabor Kovesdan {
1551243a98eSTijl Coosemans char *s0, *s1;
156ad30f8e7SGabor Kovesdan wchar_t wc;
157ad30f8e7SGabor Kovesdan int chlenbak, len;
158ad30f8e7SGabor Kovesdan
159ad30f8e7SGabor Kovesdan s0 = *s;
160ad30f8e7SGabor Kovesdan
161ad30f8e7SGabor Kovesdan if (s0 == NULL) {
162ad30f8e7SGabor Kovesdan /* _citrus_GBK2K_init_state(ei, psenc); */
163ad30f8e7SGabor Kovesdan psenc->chlen = 0;
164ad30f8e7SGabor Kovesdan *nresult = 0;
165ad30f8e7SGabor Kovesdan return (0);
166ad30f8e7SGabor Kovesdan }
167ad30f8e7SGabor Kovesdan
168ad30f8e7SGabor Kovesdan chlenbak = psenc->chlen;
169ad30f8e7SGabor Kovesdan
170ad30f8e7SGabor Kovesdan switch (psenc->chlen) {
171ad30f8e7SGabor Kovesdan case 3:
172ad30f8e7SGabor Kovesdan if (!_mb_leadbyte (_PSENC))
173ad30f8e7SGabor Kovesdan goto invalid;
174ad30f8e7SGabor Kovesdan /* FALLTHROUGH */
175ad30f8e7SGabor Kovesdan case 2:
176ad30f8e7SGabor Kovesdan if (!_mb_surrogate(_PSENC) || _mb_trailbyte(_PSENC))
177ad30f8e7SGabor Kovesdan goto invalid;
178ad30f8e7SGabor Kovesdan /* FALLTHROUGH */
179ad30f8e7SGabor Kovesdan case 1:
180ad30f8e7SGabor Kovesdan if (!_mb_leadbyte (_PSENC))
181ad30f8e7SGabor Kovesdan goto invalid;
182ad30f8e7SGabor Kovesdan /* FALLTHOROUGH */
183ad30f8e7SGabor Kovesdan case 0:
184ad30f8e7SGabor Kovesdan break;
185ad30f8e7SGabor Kovesdan default:
186ad30f8e7SGabor Kovesdan goto invalid;
187ad30f8e7SGabor Kovesdan }
188ad30f8e7SGabor Kovesdan
189ad30f8e7SGabor Kovesdan for (;;) {
190ad30f8e7SGabor Kovesdan if (n-- < 1)
191ad30f8e7SGabor Kovesdan goto restart;
192ad30f8e7SGabor Kovesdan
193ad30f8e7SGabor Kovesdan _PUSH_PSENC(*s0++);
194ad30f8e7SGabor Kovesdan
195ad30f8e7SGabor Kovesdan switch (psenc->chlen) {
196ad30f8e7SGabor Kovesdan case 1:
197ad30f8e7SGabor Kovesdan if (_mb_singlebyte(_PSENC))
198ad30f8e7SGabor Kovesdan goto convert;
199ad30f8e7SGabor Kovesdan if (_mb_leadbyte (_PSENC))
200ad30f8e7SGabor Kovesdan continue;
201ad30f8e7SGabor Kovesdan goto ilseq;
202ad30f8e7SGabor Kovesdan case 2:
203ad30f8e7SGabor Kovesdan if (_mb_trailbyte (_PSENC))
204ad30f8e7SGabor Kovesdan goto convert;
205ad30f8e7SGabor Kovesdan if (ei->mb_cur_max == 4 &&
206ad30f8e7SGabor Kovesdan _mb_surrogate (_PSENC))
207ad30f8e7SGabor Kovesdan continue;
208ad30f8e7SGabor Kovesdan goto ilseq;
209ad30f8e7SGabor Kovesdan case 3:
210ad30f8e7SGabor Kovesdan if (_mb_leadbyte (_PSENC))
211ad30f8e7SGabor Kovesdan continue;
212ad30f8e7SGabor Kovesdan goto ilseq;
213ad30f8e7SGabor Kovesdan case 4:
214ad30f8e7SGabor Kovesdan if (_mb_surrogate (_PSENC))
215ad30f8e7SGabor Kovesdan goto convert;
216ad30f8e7SGabor Kovesdan goto ilseq;
217ad30f8e7SGabor Kovesdan }
218ad30f8e7SGabor Kovesdan }
219ad30f8e7SGabor Kovesdan
220ad30f8e7SGabor Kovesdan convert:
221ad30f8e7SGabor Kovesdan len = psenc->chlen;
222ad30f8e7SGabor Kovesdan s1 = &psenc->ch[0];
223ad30f8e7SGabor Kovesdan wc = 0;
224ad30f8e7SGabor Kovesdan while (len-- > 0)
225ad30f8e7SGabor Kovesdan wc = (wc << 8) | (*s1++ & 0xff);
226ad30f8e7SGabor Kovesdan
227ad30f8e7SGabor Kovesdan if (pwc != NULL)
228ad30f8e7SGabor Kovesdan *pwc = wc;
229ad30f8e7SGabor Kovesdan *s = s0;
230ad30f8e7SGabor Kovesdan *nresult = (wc == 0) ? 0 : psenc->chlen - chlenbak;
231ad30f8e7SGabor Kovesdan /* _citrus_GBK2K_init_state(ei, psenc); */
232ad30f8e7SGabor Kovesdan psenc->chlen = 0;
233ad30f8e7SGabor Kovesdan
234ad30f8e7SGabor Kovesdan return (0);
235ad30f8e7SGabor Kovesdan
236ad30f8e7SGabor Kovesdan restart:
237ad30f8e7SGabor Kovesdan *s = s0;
238ad30f8e7SGabor Kovesdan *nresult = (size_t)-2;
239ad30f8e7SGabor Kovesdan
240ad30f8e7SGabor Kovesdan return (0);
241ad30f8e7SGabor Kovesdan
242ad30f8e7SGabor Kovesdan invalid:
243ad30f8e7SGabor Kovesdan return (EINVAL);
244ad30f8e7SGabor Kovesdan
245ad30f8e7SGabor Kovesdan ilseq:
246ad30f8e7SGabor Kovesdan *nresult = (size_t)-1;
247ad30f8e7SGabor Kovesdan return (EILSEQ);
248ad30f8e7SGabor Kovesdan }
249ad30f8e7SGabor Kovesdan
250ad30f8e7SGabor Kovesdan static int
_citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei,char * __restrict s,size_t n,wchar_t wc,_GBK2KState * __restrict psenc,size_t * __restrict nresult)251ad30f8e7SGabor Kovesdan _citrus_GBK2K_wcrtomb_priv(_GBK2KEncodingInfo * __restrict ei,
252ad30f8e7SGabor Kovesdan char * __restrict s, size_t n, wchar_t wc, _GBK2KState * __restrict psenc,
253ad30f8e7SGabor Kovesdan size_t * __restrict nresult)
254ad30f8e7SGabor Kovesdan {
255ad30f8e7SGabor Kovesdan size_t len;
256ad30f8e7SGabor Kovesdan int ret;
257ad30f8e7SGabor Kovesdan
258ad30f8e7SGabor Kovesdan if (psenc->chlen != 0) {
259ad30f8e7SGabor Kovesdan ret = EINVAL;
260ad30f8e7SGabor Kovesdan goto err;
261ad30f8e7SGabor Kovesdan }
262ad30f8e7SGabor Kovesdan
263ad30f8e7SGabor Kovesdan len = _mb_count(wc);
264ad30f8e7SGabor Kovesdan if (n < len) {
265ad30f8e7SGabor Kovesdan ret = E2BIG;
266ad30f8e7SGabor Kovesdan goto err;
267ad30f8e7SGabor Kovesdan }
268ad30f8e7SGabor Kovesdan
269ad30f8e7SGabor Kovesdan switch (len) {
270ad30f8e7SGabor Kovesdan case 1:
271ad30f8e7SGabor Kovesdan if (!_mb_singlebyte(_PUSH_PSENC(wc ))) {
272ad30f8e7SGabor Kovesdan ret = EILSEQ;
273ad30f8e7SGabor Kovesdan goto err;
274ad30f8e7SGabor Kovesdan }
275ad30f8e7SGabor Kovesdan break;
276ad30f8e7SGabor Kovesdan case 2:
277ad30f8e7SGabor Kovesdan if (!_mb_leadbyte (_PUSH_PSENC(wc >> 8)) ||
278ad30f8e7SGabor Kovesdan !_mb_trailbyte (_PUSH_PSENC(wc))) {
279ad30f8e7SGabor Kovesdan ret = EILSEQ;
280ad30f8e7SGabor Kovesdan goto err;
281ad30f8e7SGabor Kovesdan }
282ad30f8e7SGabor Kovesdan break;
283ad30f8e7SGabor Kovesdan case 4:
284ad30f8e7SGabor Kovesdan if (ei->mb_cur_max != 4 ||
285ad30f8e7SGabor Kovesdan !_mb_leadbyte (_PUSH_PSENC(wc >> 24)) ||
286ad30f8e7SGabor Kovesdan !_mb_surrogate (_PUSH_PSENC(wc >> 16)) ||
287ad30f8e7SGabor Kovesdan !_mb_leadbyte (_PUSH_PSENC(wc >> 8)) ||
288ad30f8e7SGabor Kovesdan !_mb_surrogate (_PUSH_PSENC(wc))) {
289ad30f8e7SGabor Kovesdan ret = EILSEQ;
290ad30f8e7SGabor Kovesdan goto err;
291ad30f8e7SGabor Kovesdan }
292ad30f8e7SGabor Kovesdan break;
293ad30f8e7SGabor Kovesdan }
294ad30f8e7SGabor Kovesdan
295ad30f8e7SGabor Kovesdan memcpy(s, psenc->ch, psenc->chlen);
296ad30f8e7SGabor Kovesdan *nresult = psenc->chlen;
297ad30f8e7SGabor Kovesdan /* _citrus_GBK2K_init_state(ei, psenc); */
298ad30f8e7SGabor Kovesdan psenc->chlen = 0;
299ad30f8e7SGabor Kovesdan
300ad30f8e7SGabor Kovesdan return (0);
301ad30f8e7SGabor Kovesdan
302ad30f8e7SGabor Kovesdan err:
303ad30f8e7SGabor Kovesdan *nresult = (size_t)-1;
304ad30f8e7SGabor Kovesdan return (ret);
305ad30f8e7SGabor Kovesdan }
306ad30f8e7SGabor Kovesdan
307ad30f8e7SGabor Kovesdan static __inline int
308ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei __unused,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)309ad30f8e7SGabor Kovesdan _citrus_GBK2K_stdenc_wctocs(_GBK2KEncodingInfo * __restrict ei __unused,
310ad30f8e7SGabor Kovesdan _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
311ad30f8e7SGabor Kovesdan {
312ad30f8e7SGabor Kovesdan uint8_t ch, cl;
313ad30f8e7SGabor Kovesdan
314ad30f8e7SGabor Kovesdan if ((uint32_t)wc < 0x80) {
315ad30f8e7SGabor Kovesdan /* ISO646 */
316ad30f8e7SGabor Kovesdan *csid = 0;
317ad30f8e7SGabor Kovesdan *idx = (_index_t)wc;
318ad30f8e7SGabor Kovesdan } else if ((uint32_t)wc >= 0x10000) {
319ad30f8e7SGabor Kovesdan /* GBKUCS : XXX */
320ad30f8e7SGabor Kovesdan *csid = 3;
321ad30f8e7SGabor Kovesdan *idx = (_index_t)wc;
322ad30f8e7SGabor Kovesdan } else {
323ad30f8e7SGabor Kovesdan ch = (uint8_t)(wc >> 8);
324ad30f8e7SGabor Kovesdan cl = (uint8_t)wc;
325ad30f8e7SGabor Kovesdan if (ch >= 0xA1 && cl >= 0xA1) {
326ad30f8e7SGabor Kovesdan /* EUC G1 */
327ad30f8e7SGabor Kovesdan *csid = 1;
328ad30f8e7SGabor Kovesdan *idx = (_index_t)wc & 0x7F7FU;
329ad30f8e7SGabor Kovesdan } else {
330ad30f8e7SGabor Kovesdan /* extended area (0x8140-) */
331ad30f8e7SGabor Kovesdan *csid = 2;
332ad30f8e7SGabor Kovesdan *idx = (_index_t)wc;
333ad30f8e7SGabor Kovesdan }
334ad30f8e7SGabor Kovesdan }
335ad30f8e7SGabor Kovesdan
336ad30f8e7SGabor Kovesdan return (0);
337ad30f8e7SGabor Kovesdan }
338ad30f8e7SGabor Kovesdan
339ad30f8e7SGabor Kovesdan static __inline int
340ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei,wchar_t * __restrict wc,_csid_t csid,_index_t idx)341ad30f8e7SGabor Kovesdan _citrus_GBK2K_stdenc_cstowc(_GBK2KEncodingInfo * __restrict ei,
342ad30f8e7SGabor Kovesdan wchar_t * __restrict wc, _csid_t csid, _index_t idx)
343ad30f8e7SGabor Kovesdan {
344ad30f8e7SGabor Kovesdan
345ad30f8e7SGabor Kovesdan switch (csid) {
346ad30f8e7SGabor Kovesdan case 0:
347ad30f8e7SGabor Kovesdan /* ISO646 */
348ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx;
349ad30f8e7SGabor Kovesdan break;
350ad30f8e7SGabor Kovesdan case 1:
351ad30f8e7SGabor Kovesdan /* EUC G1 */
352ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx | 0x8080U;
353ad30f8e7SGabor Kovesdan break;
354ad30f8e7SGabor Kovesdan case 2:
355ad30f8e7SGabor Kovesdan /* extended area */
356ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx;
357ad30f8e7SGabor Kovesdan break;
358ad30f8e7SGabor Kovesdan case 3:
359ad30f8e7SGabor Kovesdan /* GBKUCS : XXX */
360ad30f8e7SGabor Kovesdan if (ei->mb_cur_max != 4)
361ad30f8e7SGabor Kovesdan return (EINVAL);
362ad30f8e7SGabor Kovesdan *wc = (wchar_t)idx;
363ad30f8e7SGabor Kovesdan break;
364ad30f8e7SGabor Kovesdan default:
365ad30f8e7SGabor Kovesdan return (EILSEQ);
366ad30f8e7SGabor Kovesdan }
367ad30f8e7SGabor Kovesdan
368ad30f8e7SGabor Kovesdan return (0);
369ad30f8e7SGabor Kovesdan }
370ad30f8e7SGabor Kovesdan
371ad30f8e7SGabor Kovesdan static __inline int
372ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei __unused,_GBK2KState * __restrict psenc,int * __restrict rstate)373ad30f8e7SGabor Kovesdan _citrus_GBK2K_stdenc_get_state_desc_generic(_GBK2KEncodingInfo * __restrict ei __unused,
374ad30f8e7SGabor Kovesdan _GBK2KState * __restrict psenc, int * __restrict rstate)
375ad30f8e7SGabor Kovesdan {
376ad30f8e7SGabor Kovesdan
377ad30f8e7SGabor Kovesdan *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
378ad30f8e7SGabor Kovesdan _STDENC_SDGEN_INCOMPLETE_CHAR;
379ad30f8e7SGabor Kovesdan return (0);
380ad30f8e7SGabor Kovesdan }
381ad30f8e7SGabor Kovesdan
382ad30f8e7SGabor Kovesdan static int
383ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei,const void * __restrict var,size_t lenvar)384ad30f8e7SGabor Kovesdan _citrus_GBK2K_encoding_module_init(_GBK2KEncodingInfo * __restrict ei,
385ad30f8e7SGabor Kovesdan const void * __restrict var, size_t lenvar)
386ad30f8e7SGabor Kovesdan {
387ad30f8e7SGabor Kovesdan const char *p;
388ad30f8e7SGabor Kovesdan
389ad30f8e7SGabor Kovesdan p = var;
390ad30f8e7SGabor Kovesdan memset((void *)ei, 0, sizeof(*ei));
391ad30f8e7SGabor Kovesdan ei->mb_cur_max = 4;
392ad30f8e7SGabor Kovesdan while (lenvar > 0) {
393ad30f8e7SGabor Kovesdan switch (_bcs_tolower(*p)) {
394ad30f8e7SGabor Kovesdan case '2':
395ad30f8e7SGabor Kovesdan MATCH("2byte", ei->mb_cur_max = 2);
396ad30f8e7SGabor Kovesdan break;
397ad30f8e7SGabor Kovesdan }
398ad30f8e7SGabor Kovesdan p++;
399ad30f8e7SGabor Kovesdan lenvar--;
400ad30f8e7SGabor Kovesdan }
401ad30f8e7SGabor Kovesdan
402ad30f8e7SGabor Kovesdan return (0);
403ad30f8e7SGabor Kovesdan }
404ad30f8e7SGabor Kovesdan
405ad30f8e7SGabor Kovesdan static void
406ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo * ei __unused)407ad30f8e7SGabor Kovesdan _citrus_GBK2K_encoding_module_uninit(_GBK2KEncodingInfo *ei __unused)
408ad30f8e7SGabor Kovesdan {
409ad30f8e7SGabor Kovesdan
410ad30f8e7SGabor Kovesdan }
411ad30f8e7SGabor Kovesdan
412ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
413ad30f8e7SGabor Kovesdan * public interface for stdenc
414ad30f8e7SGabor Kovesdan */
415ad30f8e7SGabor Kovesdan
416ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(GBK2K);
417ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(GBK2K);
418ad30f8e7SGabor Kovesdan
419ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
420