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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright(c) 2001 Sun Microsystems, Inc.
* All rights reserved.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include "iscii.h"
#define MSB 0x80 /* most significant bit */
#define ONEBYTE 0xff /* right most byte */
#define REPLACE_CHAR1 0xEF /* invalid conversion character */
#define REPLACE_CHAR2 0xBF
#define REPLACE_CHAR3 0xBD
#define UTF8_SET1B(b,v) \
(b[0]=(v&0x7f))
#define UTF8_SET2B(b,v) \
(b[0]=(0xc0|((v>>6)&0x1f))); \
(b[1]=(0x80|((v&0x3f))))
#define UTF8_SET3B(b,v) \
(b[0]=(0xe0|((v>>12)&0xf))); \
(b[1]=(0x80|((v>>6)&0x3f))); \
(b[2]=(0x80|((v&0x3f))))
typedef struct _icv_state {
char keepc[3]; /* keepc[0] is attr, keepc[1] and keepc[2] are lookup-ed */
short pState; /* Previous State */
int _errno;
} _iconv_st;
enum _CSTATE { S_BASIC, S_ATR, S_EXT, S_NONE };
#define have_nukta(isc_type) ( nukta_type[isc_type] != NULL )
#define have_EXT(isc_type) ( EXT_type[isc_type] != NULL )
#define FIRST_CHAR 0xA0
static int copy_to_outbuf(ucs_t uniid, char *buf, size_t buflen);
static ucs_t
get_nukta(uchar iscii, int type)
{
int indx = iscii - FIRST_CHAR;
int *iscii_nukta = nukta_type[type];
return ((indx >= 0) ? iscii_nukta[indx] : 0 );
}
static ucs_t
get_EXT(uchar iscii, int type)
{
int indx = iscii - FIRST_CHAR;
int *iscii_EXT = EXT_type[type];
return ((indx >= 0) ? iscii_EXT[indx] : 0 );
}
static ucs_t
traverse_table(Entry *entry, int num, uchar iscii)
{
int i=0;
ucs_t retucs=0;
for ( ; i < num; ++i ) {
Entry en = entry[i];
if ( iscii < en.iscii ) break;
if ( iscii >= en.iscii && iscii < en.iscii + en.count ) {
retucs = en.ucs + ( iscii - en.iscii );
break;
}
}
return retucs;
}
/*
* the copy_to_outbuf has to be called before the st->keepc needs to changed.
* if E2BIG error, keep st->keepc. Will flush it at the beginning of next
* _icv_iconv() invocation
*/
int
iscii_to_utf8(_iconv_st *st, char *buf, size_t buflen)
{
#define DEV_ATR 0x42
ucs_t uniid;
int nBytes=0;
ISCII isc_type = isc_TYPE[st->keepc[0] - DEV_ATR];
Entries en = iscii_table[isc_type];
/* unsigned int keepc0 = (unsigned int) (st->keepc[0] & ONEBYTE); */
unsigned int keepc1 = (unsigned int) (st->keepc[1] & ONEBYTE);
unsigned int keepc2 = (unsigned int) (st->keepc[2] & ONEBYTE);
if (keepc1 == 0xFF) { /* FFFD */
if ( buflen < 3 ) {
errno = E2BIG;
return 0;
}
*buf = (char)REPLACE_CHAR1;
*(buf+1) = (char)REPLACE_CHAR2;
*(buf+2) = (char)REPLACE_CHAR3;
return (3);
}
if (keepc2 == 0) { /* Flush Single Character */
if (keepc1 & MSB) { /* ISCII - Non-Ascii Codepoints */
uniid = traverse_table(en.entry, en.items, keepc1);
} else /* ASCII */
uniid = keepc1;
if ( (nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = 0;
} else {
/* keepc[1] and keepc[2] != 0 */
if (keepc1 & MSB) {
switch (keepc1)
{
case ISC_ext:
if ( have_EXT(isc_type) && is_valid_ext_code(keepc2) )
{ /* EXT only supported in Devanagari script */
uniid = get_EXT(keepc2, isc_type);
if ((nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
}
else
errno = EILSEQ;
st->keepc[1] = st->keepc[2] = 0;
break;
case ISC_halant:
/* test whether there has enough space to hold the converted bytes */
if ((keepc2 == ISC_halant || keepc2 == ISC_nukta) && buflen < 6 )
goto E2big;
uniid = traverse_table(en.entry, en.items, keepc1);
if ((nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2];
if ( keepc2 == ISC_halant || keepc2 == ISC_nukta )
{
int nbytes_2 = 0;
if (keepc2 == ISC_halant) uniid = UNI_ZWNJ; /* explicit Halant */
if (keepc2 == ISC_nukta) uniid = UNI_ZWJ; /* soft Halant */
if ((nbytes_2 = copy_to_outbuf(uniid, buf+nBytes, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2] = 0;
nBytes += nbytes_2;
}
break;
case ISC_danda:
if ( isc_type == DEV && keepc2 == ISC_danda )
{ /* only in Devanagari script, it works */
uniid = UNI_DOUBLE_DANDA;
if ((nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2] = 0;
break;
}
/* fall into default case, convert the DANDA if it isn't DOUBLE_DANDA */
/* FALLTHRU */
default:
uniid = traverse_table(en.entry, en.items, keepc1);
if ( have_nukta(isc_type) && keepc2 == ISC_nukta) {
/* then try to test whether it is Nukta Cases */
int ucs;
if (( ucs = get_nukta(keepc1, isc_type)) != 0 ) {
uniid = ucs;
if ( (nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2] = 0;
} else {
if ( (nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2];
}
} else {
if ( (nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2];
}
break;
} /* end of switch */
} else { /* ASCII */
uniid = keepc1;
if ( (nBytes = copy_to_outbuf(uniid, buf, buflen)) == 0) goto E2big;
st->keepc[1] = st->keepc[2];
}
st->keepc[2] = 0;
}
E2big:
return nBytes;
}
static int
copy_to_outbuf(ucs_t uniid, char *buf, size_t buflen)
{
if (uniid > 0) {
if (uniid <= 0x7f) {
if (buflen < 1) {
errno = E2BIG;
return(0);
}
UTF8_SET1B(buf, uniid);
return (1);
}
if (uniid >= 0x80 && uniid <= 0x7ff) {
if (buflen < 2) {
errno = E2BIG;
return(0);
}
UTF8_SET2B(buf, uniid);
return (2);
}
if (uniid >= 0x800 && uniid <= 0xffff) {
if (buflen < 3) {
errno = E2BIG;
return(0);
}
UTF8_SET3B(buf, uniid);
return (3);
}
} else { /* Replacement Character */
if ( buflen < 3 ) {
errno = E2BIG;
return 0;
}
*buf = (char)REPLACE_CHAR1;
*(buf+1) = (char)REPLACE_CHAR2;
*(buf+2) = (char)REPLACE_CHAR3;
return (3);
}
/* This code shouldn't be reached */
return (0);
}
/*
* Open; called from iconv_open()
*/
void *
_icv_open()
{
_iconv_st *st;
if ((st = (_iconv_st*)malloc(sizeof(_iconv_st))) == NULL) {
errno = ENOMEM;
return ((void*)-1);
}
bzero(st, sizeof(_iconv_st));
st->keepc[0] = DEV_ATR;
st->pState = S_BASIC;
return ((void*)st);
}
/*
* Close; called from iconv_close()
*/
void
_icv_close(_iconv_st *st)
{
if (!st)
errno = EBADF;
else
free(st);
}
/*
* Conversion routine; called from iconv()
*/
size_t
_icv_iconv(_iconv_st *st, char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
int n;
short curState;
if (st == NULL) {
errno = EBADF;
return ((size_t) -1);
}
if (inbuf == NULL || *inbuf == NULL) { /* Reset request */
st->keepc[0] = DEV_ATR;
st->pState = S_BASIC;
st->_errno = 0;
return ((size_t)0);
}
/* flush if possible */
if ( st->_errno == E2BIG ) {
n = iscii_to_utf8(st, *outbuf, *outbytesleft);
(*outbuf) += n;
(*outbytesleft) -= n;
}
st->_errno = errno = 0; /* reset internal and external errno */
/* a state machine for interpreting ISCII code */
while (*inbytesleft > 0 && *outbytesleft > 0) {
unsigned int curChar = (unsigned int)(**inbuf & ONEBYTE);
unsigned int prevChar = (unsigned int)(st->keepc[1] & ONEBYTE);
if (curChar == ISC_ext)
curState = S_EXT;
else if (curChar == ISC_atr)
curState = S_ATR;
else
curState = S_BASIC;
switch (curState) {
case S_BASIC:
if (prevChar == 0)
st->keepc[1] = curChar;
else
st->keepc[2] = curChar;
if (st->pState == S_ATR) {
/* clear the keepc[1], which is part of attribute */
st->keepc[1] = 0;
/* change the attribute for Indian Script Fonts */
if ((curChar >= 0x42) && (curChar <= 0x4b) && curChar != 0x46) {
st->keepc[0] = curChar;
}
/* other attributes such as display attributes would be ignored */
} else { /* Handle Cases and Flush */
if ((curChar > 0 && curChar <= 0x7f) || prevChar != 0) {
n=iscii_to_utf8(st, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n;
(*outbytesleft) -= n;
} else /* don't return immediately, need advance the *inbuf */
st->_errno = errno;
}
}
break;
case S_ATR:
case S_EXT: /* Do nothing */
if (st->pState == S_BASIC) { /* Flush */
if ( st->keepc[1] == 0 )
{
if (curState == S_EXT) st->keepc[1] = ISC_ext;
break;
}
n = iscii_to_utf8(st, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n;
(*outbytesleft) -= n;
} else /* don't return immediately */
st->_errno = errno;
if (curState == S_EXT) st->keepc[1] = ISC_ext;
} else {
errno = EILSEQ;
return (size_t)-1;
}
break;
default: /* should never come here */
st->_errno = errno = EILSEQ;
st->pState = S_BASIC; /* reset state */
break;
}
st->pState = curState;
(*inbuf)++;
(*inbytesleft)--;
if (errno)
return(size_t)-1;
}
if (*inbytesleft > 0 && *outbytesleft == 0) {
/* in this case, the st->_errno is zero */
errno = E2BIG;
return(size_t)-1;
}
return (size_t)(*inbytesleft);
}
|