1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* $FreeBSD$ */
/* $NetBSD: citrus_johab.c,v 1.4 2008/06/14 16:01:07 tnozaki Exp $ */
/*-
* Copyright (c)2006 Citrus Project,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "citrus_namespace.h"
#include "citrus_types.h"
#include "citrus_bcs.h"
#include "citrus_module.h"
#include "citrus_stdenc.h"
#include "citrus_johab.h"
/* ----------------------------------------------------------------------
* private stuffs used by templates
*/
typedef struct {
int chlen;
char ch[2];
} _JOHABState;
typedef struct {
int dummy;
} _JOHABEncodingInfo;
#define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
#define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
#define _FUNCNAME(m) _citrus_JOHAB_##m
#define _ENCODING_INFO _JOHABEncodingInfo
#define _ENCODING_STATE _JOHABState
#define _ENCODING_MB_CUR_MAX(_ei_) 2
#define _ENCODING_IS_STATE_DEPENDENT 0
#define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
static __inline void
/*ARGSUSED*/
_citrus_JOHAB_init_state(_JOHABEncodingInfo * __restrict ei __unused,
_JOHABState * __restrict psenc)
{
psenc->chlen = 0;
}
static __inline void
/*ARGSUSED*/
_citrus_JOHAB_pack_state(_JOHABEncodingInfo * __restrict ei __unused,
void * __restrict pspriv, const _JOHABState * __restrict psenc)
{
memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
}
static __inline void
/*ARGSUSED*/
_citrus_JOHAB_unpack_state(_JOHABEncodingInfo * __restrict ei __unused,
_JOHABState * __restrict psenc, const void * __restrict pspriv)
{
memcpy((void *)psenc, pspriv, sizeof(*psenc));
}
static void
/*ARGSUSED*/
_citrus_JOHAB_encoding_module_uninit(_JOHABEncodingInfo *ei __unused)
{
/* ei may be null */
}
static int
/*ARGSUSED*/
_citrus_JOHAB_encoding_module_init(_JOHABEncodingInfo * __restrict ei __unused,
const void * __restrict var __unused, size_t lenvar __unused)
{
/* ei may be null */
return (0);
}
static __inline bool
ishangul(int l, int t)
{
return ((l >= 0x84 && l <= 0xD3) &&
((t >= 0x41 && t <= 0x7E) || (t >= 0x81 && t <= 0xFE)));
}
static __inline bool
isuda(int l, int t)
{
return ((l == 0xD8) &&
((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)));
}
static __inline bool
ishanja(int l, int t)
{
return (((l >= 0xD9 && l <= 0xDE) || (l >= 0xE0 && l <= 0xF9)) &&
((t >= 0x31 && t <= 0x7E) || (t >= 0x91 && t <= 0xFE)));
}
static int
/*ARGSUSED*/
_citrus_JOHAB_mbrtowc_priv(_JOHABEncodingInfo * __restrict ei,
wchar_t * __restrict pwc, const char ** __restrict s, size_t n,
_JOHABState * __restrict psenc, size_t * __restrict nresult)
{
const char *s0;
int l, t;
if (*s == NULL) {
_citrus_JOHAB_init_state(ei, psenc);
*nresult = _ENCODING_IS_STATE_DEPENDENT;
return (0);
}
s0 = *s;
switch (psenc->chlen) {
case 0:
if (n-- < 1)
goto restart;
l = *s0++ & 0xFF;
if (l <= 0x7F) {
if (pwc != NULL)
*pwc = (wchar_t)l;
*nresult = (l == 0) ? 0 : 1;
*s = s0;
return (0);
}
psenc->ch[psenc->chlen++] = l;
break;
case 1:
l = psenc->ch[0] & 0xFF;
break;
default:
return (EINVAL);
}
if (n-- < 1) {
restart:
*nresult = (size_t)-2;
*s = s0;
return (0);
}
t = *s0++ & 0xFF;
if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t)) {
*nresult = (size_t)-1;
return (EILSEQ);
}
if (pwc != NULL)
*pwc = (wchar_t)(l << 8 | t);
*nresult = s0 - *s;
*s = s0;
psenc->chlen = 0;
return (0);
}
static int
/*ARGSUSED*/
_citrus_JOHAB_wcrtomb_priv(_JOHABEncodingInfo * __restrict ei __unused,
char * __restrict s, size_t n, wchar_t wc,
_JOHABState * __restrict psenc, size_t * __restrict nresult)
{
int l, t;
if (psenc->chlen != 0)
return (EINVAL);
/* XXX assume wchar_t as int */
if ((uint32_t)wc <= 0x7F) {
if (n < 1)
goto e2big;
*s = wc & 0xFF;
*nresult = 1;
} else if ((uint32_t)wc <= 0xFFFF) {
if (n < 2) {
e2big:
*nresult = (size_t)-1;
return (E2BIG);
}
l = (wc >> 8) & 0xFF;
t = wc & 0xFF;
if (!ishangul(l, t) && !isuda(l, t) && !ishanja(l, t))
goto ilseq;
*s++ = l;
*s = t;
*nresult = 2;
} else {
ilseq:
*nresult = (size_t)-1;
return (EILSEQ);
}
return (0);
}
static __inline int
/*ARGSUSED*/
_citrus_JOHAB_stdenc_wctocs(_JOHABEncodingInfo * __restrict ei __unused,
_csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
{
int m, l, linear, t;
/* XXX assume wchar_t as int */
if ((uint32_t)wc <= 0x7F) {
*idx = (_index_t)wc;
*csid = 0;
} else if ((uint32_t)wc <= 0xFFFF) {
l = (wc >> 8) & 0xFF;
t = wc & 0xFF;
if (ishangul(l, t) || isuda(l, t)) {
*idx = (_index_t)wc;
*csid = 1;
} else {
if (l >= 0xD9 && l <= 0xDE) {
linear = l - 0xD9;
m = 0x21;
} else if (l >= 0xE0 && l <= 0xF9) {
linear = l - 0xE0;
m = 0x4A;
} else
return (EILSEQ);
linear *= 188;
if (t >= 0x31 && t <= 0x7E)
linear += t - 0x31;
else if (t >= 0x91 && t <= 0xFE)
linear += t - 0x43;
else
return (EILSEQ);
l = (linear / 94) + m;
t = (linear % 94) + 0x21;
*idx = (_index_t)((l << 8) | t);
*csid = 2;
}
} else
return (EILSEQ);
return (0);
}
static __inline int
/*ARGSUSED*/
_citrus_JOHAB_stdenc_cstowc(_JOHABEncodingInfo * __restrict ei __unused,
wchar_t * __restrict wc, _csid_t csid, _index_t idx)
{
int m, n, l, linear, t;
switch (csid) {
case 0:
case 1:
*wc = (wchar_t)idx;
break;
case 2:
if (idx >= 0x2121 && idx <= 0x2C71) {
m = 0xD9;
n = 0x21;
} else if (idx >= 0x4A21 && idx <= 0x7D7E) {
m = 0xE0;
n = 0x4A;
} else
return (EILSEQ);
l = ((idx >> 8) & 0xFF) - n;
t = (idx & 0xFF) - 0x21;
linear = (l * 94) + t;
l = (linear / 188) + m;
t = linear % 188;
t += (t <= 0x4D) ? 0x31 : 0x43;
break;
default:
return (EILSEQ);
}
return (0);
}
static __inline int
/*ARGSUSED*/
_citrus_JOHAB_stdenc_get_state_desc_generic(_JOHABEncodingInfo * __restrict ei __unused,
_JOHABState * __restrict psenc, int * __restrict rstate)
{
*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
_STDENC_SDGEN_INCOMPLETE_CHAR;
return (0);
}
/* ----------------------------------------------------------------------
* public interface for stdenc
*/
_CITRUS_STDENC_DECLS(JOHAB);
_CITRUS_STDENC_DEF_OPS(JOHAB);
#include "citrus_stdenc_template.h"
|