Debian Codegraph @

texk/web2c/luatexdir/unilib/gwwiconv.c

raw
1 /* Copyright (C) 2004-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <unibasics.h>
29 #include <gwwiconv.h>
30 #include <charset.h>
31 #include <chardata.h>
32 #include <string.h>
33 #include <ustring.h>
34 #include <stdio.h>
35
36 #ifndef HAVE_ICONV_H
37
38 /* I have written an limited iconv which will convert either to or from unichar_t */
39 /* (either UCS2 or UCS4) */
40 /* it will not convert latin1 to latin2, but latin1->UCS2, UCS2->latin2 */
41 /* it uses the encodings built into libgunicode for systems with no iconv */
42 /* (ie. macs before 10.3, perhaps others) */
43
44 struct gww_iconv_t {
45 enum encoding from;
46 enum encoding to;
47 };
48
49 /*enum extended_encoding { e_jisgbpk = e_encodingmax };*/
50 #define e_jisgbpk e_encodingmax
51
52 static enum endian { end_big, end_little, end_unknown } endian = end_unknown;
53
54 static void endian_detector(void) {
55 union { short s; char c[2]; } u;
56
57 u.s = 0x0102;
58 if ( u.c[0]==0x1 )
59 endian = end_big;
60 else
61 endian = end_little;
62 }
63
64 static enum encoding name_to_enc(const char *encname) {
65 struct { const char *name; enum encoding enc; } map[] = {
66 { "UCS-2-INTERNAL", e_unicode },
67 { "UCS2", e_unicode },
68 { "UCS-2", e_unicode },
69 { "UCS-2LE", e_unicode },
70 { "UCS-2BE", e_unicode },
71 { "UNICODELITTLE", e_unicode },
72 { "UNICODEBIG", e_unicode },
73 { "ISO-10646/UCS2", e_unicode },
74 { "ISO-10646/USC2", e_unicode }, /* Old typo */
75 { "UCS4", e_ucs4 },
76 { "UCS-4", e_ucs4 },
77 { "UCS-4LE", e_ucs4 },
78 { "UCS-4BE", e_ucs4 },
79 { "UCS-4-INTERNAL", e_ucs4 },
80 { "ISO-10646/UCS4", e_ucs4 },
81 { "iso8859-1", e_iso8859_1 },
82 { "iso8859-2", e_iso8859_2 },
83 { "iso8859-3", e_iso8859_3 },
84 { "iso8859-4", e_iso8859_4 },
85 { "iso8859-5", e_iso8859_5 },
86 { "iso8859-6", e_iso8859_6 },
87 { "iso8859-7", e_iso8859_7 },
88 { "iso8859-8", e_iso8859_8 },
89 { "iso8859-9", e_iso8859_9 },
90 { "iso8859-10", e_iso8859_10 },
91 { "iso8859-11", e_iso8859_11 },
92 { "iso8859-13", e_iso8859_13 },
93 { "iso8859-14", e_iso8859_14 },
94 { "iso8859-15", e_iso8859_15 },
95 { "iso-8859-1", e_iso8859_1 },
96 { "iso-8859-2", e_iso8859_2 },
97 { "iso-8859-3", e_iso8859_3 },
98 { "iso-8859-4", e_iso8859_4 },
99 { "iso-8859-5", e_iso8859_5 },
100 { "iso-8859-6", e_iso8859_6 },
101 { "iso-8859-7", e_iso8859_7 },
102 { "iso-8859-8", e_iso8859_8 },
103 { "iso-8859-9", e_iso8859_9 },
104 { "iso-8859-10", e_iso8859_10 },
105 { "iso-8859-11", e_iso8859_11 },
106 { "iso-8859-13", e_iso8859_13 },
107 { "iso-8859-14", e_iso8859_14 },
108 { "iso-8859-15", e_iso8859_15 },
109 { "koi8-r", e_koi8_r },
110 { "jis201", e_jis201 },
111 { "mac", e_mac },
112 { "Macintosh", e_mac },
113 { "MS-ANSI", e_win },
114 { "EUC-KR", e_wansung },
115 { "johab", e_johab },
116 { "ISO-2022-KR", e_jiskorean },
117 { "ISO-2022-CN", e_jisgb },
118 { "EUC-CN", e_jisgbpk },
119 { "big5", e_big5 },
120 { "big5hkscs", e_big5hkscs },
121 { "ISO-2022-JP", e_jis },
122 { "ISO-2022-JP-2", e_jis2 },
123 { "Sjis", e_sjis },
124 { "UTF-8", e_utf8 },
125 { "UTF8", e_utf8 },
126 { NULL }};
127 int i;
128
129 for ( i=0; map[i].name!=NULL; ++i )
130 if ( strmatch(map[i].name,encname)==0 )
131 return( map[i].enc );
132
133 return( -1 );
134 }
135
136 gww_iconv_t gww_iconv_open(const char *toenc,const char *fromenc) {
137 struct gww_iconv_t stuff, *ret;
138
139 if ( endian==end_unknown )
140 endian_detector();
141
142 stuff.from = name_to_enc(fromenc);
143 stuff.to = name_to_enc(toenc);
144 if ( stuff.from==(enum encoding) -1 || stuff.to==(enum encoding) -1 ) {
145 /*fprintf( stderr, "Unknown encoding\n" );*/
146 return( (iconv_t)(-1) );
147 } else if ( stuff.from!=e_ucs4 && stuff.to!=e_ucs4 ) {
148 fprintf( stderr, "Bad call to gww_iconv_open, neither arg is UCS4\n" );
149 return( (iconv_t)(-1) );
150 }
151
152 ret = xmalloc(sizeof(struct gww_iconv_t));
153 *ret = stuff;
154 return( ret );
155 }
156
157 void gww_iconv_close( gww_iconv_t cd) {
158 free(cd);
159 }
160
161 size_t gww_iconv( gww_iconv_t _cd,
162 char **inbuf, size_t *inlen,
163 char **outbuf, size_t *outlen) {
164 struct gww_iconv_t *cd = _cd;
165 int char_cnt = 0;
166 unsigned char *plane;
167 int ch;
168
169 if ( inbuf==NULL || outbuf==NULL || inlen==NULL || outlen==NULL ||
170 *inbuf==NULL || *outbuf==NULL )
171 return( 0 ); /* Legal, used to reset the state. As we don't do states, irrelevant */
172
173 if ( cd->from<0 || cd->from>e_encodingmax || cd->to<0 || cd->to>e_encodingmax ) {
174 fprintf( stderr, "Garbage encoding passed to gww_iconv()\n" );
175 return( (size_t) -1 );
176 }
177
178 if ( cd->from==e_unicode ) {
179 if ( cd->to==e_unicode ) {
180 int min = *inlen < *outlen ? *inlen : *outlen;
181 min &= ~1;
182 memcpy(*inbuf,*outbuf,min);
183 char_cnt = min/sizeof(short);
184 *inbuf += min; *outbuf += min;
185 *inlen -= min; *outlen -= min;
186 if ( *inlen==1 && *outlen>0 )
187 return( (size_t) -1 ); /* Incomplete multi-byte sequence */
188 } else if ( cd->to==e_ucs4 ) {
189 int min = *inlen/sizeof(short) < *outlen/sizeof(int32) ? *inlen/sizeof(short) : *outlen/sizeof(int32);
190 int highch, lowch;
191 if ( endian == end_little ) {
192 while ( *inlen>=sizeof(short) && *outlen>=sizeof(int32) ) {
193 highch = ((unsigned char *) *inbuf)[1], lowch = *(unsigned char *) *inbuf;
194 ((uint8 *) outbuf)[3] = 0; ((uint8 *) outbuf)[2] = 0;
195 ((uint8 *) outbuf)[1] = highch; ((uint8 *) outbuf)[0] = lowch;
196 outbuf += sizeof(int32); inbuf += sizeof(short);
197 *outlen -= sizeof(int32); *inlen -= sizeof(short);
198 }
199 } else {
200 while ( *inlen>=sizeof(short) && *outlen>=sizeof(int32) ) {
201 highch = ((unsigned char *) *inbuf)[0], lowch = ((unsigned char *) *inbuf)[1];
202 ((uint8 *) outbuf)[0] = 0; ((uint8 *) outbuf)[1] = 0;
203 ((uint8 *) outbuf)[2] = highch; ((uint8 *) outbuf)[3] = lowch;
204 outbuf += sizeof(int32); inbuf += sizeof(short);
205 *outlen -= sizeof(int32); *inlen -= sizeof(short);
206 }
207 }
208 char_cnt = min;
209 if ( *inlen==1 && *outlen>0 )
210 return( (size_t) -1 ); /* Incomplete multi-byte sequence */
211 } else if ( cd->to<e_first2byte ) {
212 struct charmap *table = NULL;
213 table = alphabets_from_unicode[cd->to];
214 while ( *inlen>1 && *outlen>0 ) {
215 int highch, lowch;
216 if ( endian == end_little ) {
217 highch = ((unsigned char *) *inbuf)[1], lowch = *(unsigned char *) *inbuf;
218 } else {
219 highch = *(unsigned char *) *inbuf, lowch = ((unsigned char *) *inbuf)[1];
220 }
221 if ( highch>=table->first && highch<=table->last &&
222 (plane = table->table[highch])!=NULL &&
223 (ch=plane[lowch])!=0 ) {
224 *((*outbuf)++) = ch;
225 -- *outlen;
226 *inlen -= 2;
227 *inbuf += 2;
228 ++char_cnt;
229 } else
230 return( (size_t) -1 );
231 }
232 } else if ( cd->to==e_utf8 ) {
233 while ( *inlen>1 && *outlen>0 ) {
234 unichar_t uch;
235 if ( endian == end_little ) {
236 uch = (((unsigned char *) *inbuf)[1]<<8) | (*((unsigned char *) *inbuf));
237 } else {
238 uch = (*((unsigned char *) *inbuf)<<8) | (((unsigned char *) *inbuf)[1]);
239 }
240 if ( uch < 0x80 ) {
241 *((*outbuf)++) = uch;
242 --*outlen;
243 } else if ( uch<0x800 ) {
244 if ( *outlen==1 )
245 return( (size_t) -1 );
246 *((*outbuf)++) = 0xc0 | (uch>>6);
247 *((*outbuf)++) = 0x80 | (uch&0x3f);
248 *outlen-=2;
249 } else { /* I'm not dealing with */
250 if ( *outlen<=2 )
251 return( (size_t) -1 );
252 *((*outbuf)++) = 0xe0 | (uch>>12);
253 *((*outbuf)++) = 0x80 | ((uch>>6)&0x3f);
254 *((*outbuf)++) = 0x80 | (uch&0x3f);
255 *outlen-=3;
256 }
257 *inbuf += 2;
258 *inlen -= 2;
259 ++char_cnt;
260 }
261 } else {
262 fprintf( stderr, "Unexpected encoding\n" );
263 return( (size_t) -1 );
264 }
265 } else if ( cd->from==e_ucs4 ) {
266 if ( cd->to==e_unicode ) {
267 int min = *inlen/sizeof(int32) < *outlen/sizeof(int16) ? *inlen/sizeof(int32) : *outlen/sizeof(int16);
268 int highch, lowch;
269 if ( endian == end_little ) {
270 while ( *inlen>=sizeof(short) && *outlen>=sizeof(int32) ) {
271 highch = ((unsigned char *) *inbuf)[1], lowch = *(unsigned char *) *inbuf;
272 ((uint8 *) outbuf)[1] = highch; ((uint8 *) outbuf)[0] = lowch;
273 outbuf += sizeof(int16); inbuf += sizeof(int32);
274 *outlen -= sizeof(int16); *inlen -= sizeof(int32);
275 }
276 } else {
277 while ( *inlen>=sizeof(short) && *outlen>=sizeof(int32) ) {
278 highch = ((unsigned char *) *inbuf)[2], lowch = ((unsigned char *) *inbuf)[3];
279 ((uint8 *) outbuf)[0] = highch; ((uint8 *) outbuf)[1] = lowch;
280 outbuf += sizeof(int16); inbuf += sizeof(int32);
281 *outlen -= sizeof(int16); *inlen -= sizeof(int32);
282 }
283 }
284 char_cnt = min;
285 if ( *inlen>0 && *outlen>0 )
286 return( (size_t) -1 ); /* Incomplete multi-byte sequence */
287 } else if ( cd->to<e_first2byte ) {
288 struct charmap *table = NULL;
289 table = alphabets_from_unicode[cd->to];
290 while ( *inlen>1 && *outlen>0 ) {
291 int highch, lowch;
292 if ( endian == end_little ) {
293 highch = ((unsigned char *) *inbuf)[1], lowch = *(unsigned char *) *inbuf;
294 } else {
295 highch = ((unsigned char *) *inbuf)[2], lowch = ((unsigned char *) *inbuf)[3];
296 }
297 if ( highch>=table->first && highch<=table->last &&
298 (plane = table->table[highch])!=NULL &&
299 (ch=plane[lowch])!=0 ) {
300 *((*outbuf)++) = ch;
301 -- *outlen;
302 *inlen -= 4;
303 *inbuf += 4;
304 ++char_cnt;
305 } else
306 return( (size_t) -1 );
307 }
308 } else if ( cd->to==e_utf8 ) {
309 while ( *inlen>1 && *outlen>0 ) {
310 int uch;
311 if ( endian == end_little ) {
312 uch = (((unsigned char *) *inbuf)[3]<<24) |
313 (((unsigned char *) *inbuf)[2]<<16) |
314 (((unsigned char *) *inbuf)[1]<<8) |
315 (*((unsigned char *) *inbuf));
316 } else {
317 uch = (*((unsigned char *) *inbuf)<<24) |
318 (((unsigned char *) *inbuf)[1]<<16) |
319 (((unsigned char *) *inbuf)[2]<<8) |
320 (((unsigned char *) *inbuf)[3]);
321 }
322 if ( uch < 0x80 ) {
323 *((*outbuf)++) = uch;
324 --*outlen;
325 } else if ( uch<0x800 ) {
326 if ( *outlen==1 )
327 return( (size_t) -1 );
328 *((*outbuf)++) = 0xc0 | (uch>>6);
329 *((*outbuf)++) = 0x80 | (uch&0x3f);
330 *outlen-=2;
331 } else if ( uch < 0x10000 ) {
332 if ( *outlen<=2 )
333 return( (size_t) -1 );
334 *((*outbuf)++) = 0xe0 | (uch>>12);
335 *((*outbuf)++) = 0x80 | ((uch>>6)&0x3f);
336 *((*outbuf)++) = 0x80 | (uch&0x3f);
337 *outlen-=3;
338 } else {
339 uint32 val = uch-0x10000;
340 int u = ((val&0xf0000)>>16)+1, z=(val&0x0f000)>>12, y=(val&0x00fc0)>>6, x=val&0x0003f;
341 if ( *outlen<=3 )
342 return( (size_t) -1 );
343 *(*outbuf)++ = 0xf0 | (u>>2);
344 *(*outbuf)++ = 0x80 | ((u&3)<<4) | z;
345 *(*outbuf)++ = 0x80 | y;
346 *(*outbuf)++ = 0x80 | x;
347 *outlen-=4;
348 }
349 *inbuf += 4;
350 *inlen -= 4;
351 ++char_cnt;
352 }
353 } else {
354 fprintf( stderr, "Unexpected encoding\n" );
355 return( (size_t) -1 );
356 }
357 } else if ( cd->to==e_unicode ) {
358 const unichar_t *table;
359 if ( cd->from<e_first2byte ) {
360 table = unicode_from_alphabets[cd->from];
361 while ( *inlen>0 && *outlen>1 ) {
362 unichar_t ch = table[ *(unsigned char *) ((*inbuf)++)];
363 --*inlen;
364 if ( endian==end_little ) {
365 *((*outbuf)++) = ch&0xff;
366 *((*outbuf)++) = ch>>8;
367 } else {
368 *((*outbuf)++) = ch>>8;
369 *((*outbuf)++) = ch&0xff;
370 }
371 *outlen -= sizeof(unichar_t);
372 ++char_cnt;
373 }
374 } else if ( cd->from==e_jis || cd->from==e_jis2 ||
375 cd->from==e_jiskorean || cd->from==e_jisgb ) {
376 table = cd->from==e_jisgb ? unicode_from_gb2312 :
377 cd->from==e_jiskorean ? unicode_from_ksc5601 :
378 cd->from==e_jis ? unicode_from_jis208 :
379 unicode_from_jis212;
380 while ( *inlen>1 && *outlen>1 ) {
381 unsigned char *ipt = (unsigned char *) *inbuf;
382 int ch;
383 if ( *ipt<0x21 || *ipt>0x7e || ipt[1]<0x21 || ipt[1]>0x7e )
384 return( (size_t) -1 );
385 ch = (*ipt-0x21)*94 + (ipt[1]-0x21);
386 ch = table[ch];
387 *inlen -= 2;
388 *inbuf = (char *) ipt+2;
389 if ( endian==end_little ) {
390 *((*outbuf)++) = ch&0xff;
391 *((*outbuf)++) = ch>>8;
392 } else {
393 *((*outbuf)++) = ch>>8;
394 *((*outbuf)++) = ch&0xff;
395 }
396 *outlen -= sizeof(unichar_t);
397 ++char_cnt;
398 }
399 if ( *inlen==1 && *outlen>0 )
400 return( (size_t) -1 ); /* Incomplete multi-byte sequence */
401 } else if ( cd->from==e_wansung || cd->from==e_jisgbpk ) {
402 table = cd->from==e_jisgbpk ? unicode_from_gb2312 :
403 unicode_from_ksc5601 ;
404 while ( *inlen>0 && *outlen>1 ) {
405 unsigned char *ipt = (unsigned char *) *inbuf;
406 int ch;
407 if ( *ipt<0x7f ) {
408 ch = *ipt;
409 --*inlen;
410 *inbuf = (char *) ipt+1;
411 } else {
412 if ( *ipt<0xa1 || *ipt>0xfe || ipt[1]<0xa1 || ipt[1]>0xfe ||
413 *inlen==1 )
414 return( (size_t) -1 );
415 ch = (*ipt-0xa1)*94 + (ipt[1]-0xa1);
416 ch = table[ch];
417 *inlen -= 2;;
418 *inbuf = (char *) ipt+2;
419 }
420 if ( endian==end_little ) {
421 *((*outbuf)++) = ch&0xff;
422 *((*outbuf)++) = ch>>8;
423 } else {
424 *((*outbuf)++) = ch>>8;
425 *((*outbuf)++) = ch&0xff;
426 }
427 *outlen -= sizeof(unichar_t);
428 ++char_cnt;
429 }
430 } else if ( cd->from==e_johab || cd->from==e_big5 || cd->from==e_big5hkscs ) {
431 int offset;
432 if ( cd->from==e_big5 ) {
433 offset = 0xa100;
434 table = unicode_from_big5;
435 } else if ( cd->from==e_big5hkscs ) {
436 offset = 0x8100;
437 table = unicode_from_big5hkscs;
438 } else {
439 offset = 0x8400;
440 table = unicode_from_johab;
441 }
442 while ( *inlen>0 && *outlen>1 ) {
443 unsigned char *ipt = (unsigned char *) *inbuf;
444 int ch;
445 if ( *ipt<0x7f ) {
446 ch = *ipt;
447 --*inlen;
448 *inbuf = (char *) ipt+1;
449 } else {
450 if ( *inlen==1 )
451 return( (size_t) -1 );
452 ch = (*ipt<<8) | ipt[1];
453 if ( ch<offset )
454 return( (size_t) -1 );
455 ch -= offset;
456 ch = table[ch];
457 *inlen -= 2;
458 *inbuf = (char *) ipt+2;
459 }
460 if ( endian==end_little ) {
461 *((*outbuf)++) = ch&0xff;
462 *((*outbuf)++) = ch>>8;
463 } else {
464 *((*outbuf)++) = ch>>8;
465 *((*outbuf)++) = ch&0xff;
466 }
467 *outlen -= sizeof(unichar_t);
468 ++char_cnt;
469 }
470 } else if ( cd->from==e_sjis ) {
471 while ( *inlen>0 && *outlen>1 ) {
472 unsigned char *ipt = (unsigned char *) *inbuf;
473 int ch1 = *ipt;
474 if ( ch1<127 || ( ch1>=161 && ch1<=223 )) {
475 ch = unicode_from_jis201[ch1];
476 *inbuf = (char *) ipt+1;
477 --*inlen;
478 } else if ( *inlen==1 )
479 return( (size_t) -1 );
480 else {
481 int ch2 = ipt[1];
482 if ( ch1 >= 129 && ch1<= 159 )
483 ch1 -= 112;
484 else
485 ch1 -= 176;
486 ch1 <<= 1;
487 if ( ch2>=159 )
488 ch2-= 126;
489 else if ( ch2>127 ) {
490 --ch1;
491 ch2 -= 32;
492 } else {
493 --ch1;
494 ch2 -= 31;
495 }
496 if ( ch1-0x21>=94 || ch2-0x21>=94 )
497 return( (size_t) -1 );
498 ch = unicode_from_jis208[(ch1-0x21)*94+(ch2-0x21)];
499 *inlen -= 2;
500 *inbuf = (char *) ipt+2;
501 }
502 if ( endian==end_little ) {
503 *((*outbuf)++) = ch&0xff;
504 *((*outbuf)++) = ch>>8;
505 } else {
506 *((*outbuf)++) = ch>>8;
507 *((*outbuf)++) = ch&0xff;
508 }
509 *outlen -= sizeof(unichar_t);
510 ++char_cnt;
511 }
512 } else if ( cd->from==e_utf8 ) {
513 while ( *inlen>0 && *outlen>sizeof(unichar_t) ) {
514 unsigned char *ipt = (unsigned char *) *inbuf;
515 int ch = *ipt;
516 if ( ch <= 127 ) {
517 *inbuf = (char *) ipt+1;
518 --*inlen;
519 } else if ( ch<=0xdf ) {
520 if ( *inlen<2 || ipt[1]<0x80 )
521 return( (size_t) -1 );
522 ch = ((ch&0x1f)<<6) | (ipt[1] &0x3f);
523 *inlen -= 2;
524 *inbuf = (char *) ipt+2;
525 } else if ( ch<=0xef ) {
526 if ( *inlen<3 || ipt[1]<0x80 || ipt[2]<0x80 )
527 return( (size_t) -1 );
528 ch = ((ch&0x1f)<<12) | ((ipt[1] &0x3f)<<6) | (ipt[2]&0x3f);
529 *inlen -= 3;
530 *inbuf = (char *) ipt+3;
531 } else {
532 int w;
533 if ( *inlen<4 || *outlen<4 || ipt[1]<0x80 || ipt[2]<0x80 || ipt[3]<0x80 )
534 return( (size_t) -1 );
535 w = ( ((ch&0x7)<<2) | ((ipt[1]&0x30)>>4) )-1;
536 ch = 0xd800 | (w<<6) | ((ipt[1]&0xf)<<2) | ((ipt[2]&0x30)>>4);
537 if ( endian==end_little ) {
538 *((*outbuf)++) = ch&0xff;
539 *((*outbuf)++) = ch>>8;
540 } else {
541 *((*outbuf)++) = ch>>8;
542 *((*outbuf)++) = ch&0xff;
543 }
544 *outlen -= 2;
545 ch = 0xdc00 | ((ipt[2]&0xf)<<6) | (ipt[3]&0x3f);
546 }
547 if ( endian==end_little ) {
548 *((*outbuf)++) = ch&0xff;
549 *((*outbuf)++) = ch>>8;
550 } else {
551 *((*outbuf)++) = ch>>8;
552 *((*outbuf)++) = ch&0xff;
553 }
554 *outlen -= sizeof(unichar_t);
555 ++char_cnt;
556 }
557 } else {
558 fprintf( stderr, "Unexpected encoding\n" );
559 return( (size_t) -1 );
560 }
561 } else if ( cd->to==e_ucs4 ) {
562 const unichar_t *table;
563 if ( cd->from<e_first2byte ) {
564 table = unicode_from_alphabets[cd->from];
565 while ( *inlen>0 && *outlen>1 ) {
566 unichar_t ch = table[ *(unsigned char *) ((*inbuf)++)];
567 --*inlen;
568 if ( endian==end_little ) {
569 *((*outbuf)++) = 0;
570 *((*outbuf)++) = 0;
571 *((*outbuf)++) = ch&0xff;
572 *((*outbuf)++) = ch>>8;
573 } else {
574 *((*outbuf)++) = ch>>8;
575 *((*outbuf)++) = ch&0xff;
576 *((*outbuf)++) = 0;
577 *((*outbuf)++) = 0;
578 }
579 *outlen -= sizeof(unichar_t);
580 ++char_cnt;
581 }
582 } else if ( cd->from==e_jis || cd->from==e_jis2 ||
583 cd->from==e_jiskorean || cd->from==e_jisgb ) {
584 table = cd->from==e_jisgb ? unicode_from_gb2312 :
585 cd->from==e_jiskorean ? unicode_from_ksc5601 :
586 cd->from==e_jis ? unicode_from_jis208 :
587 unicode_from_jis212;
588 while ( *inlen>1 && *outlen>1 ) {
589 unsigned char *ipt = (unsigned char *) *inbuf;
590 int ch;
591 if ( *ipt<0x21 || *ipt>0x7e || ipt[1]<0x21 || ipt[1]>0x7e )
592 return( (size_t) -1 );
593 ch = (*ipt-0x21)*94 + (ipt[1]-0x21);
594 ch = table[ch];
595 *inlen -= 2;
596 *inbuf = (char *) ipt+2;
597 if ( endian==end_little ) {
598 *((*outbuf)++) = 0;
599 *((*outbuf)++) = 0;
600 *((*outbuf)++) = ch&0xff;
601 *((*outbuf)++) = ch>>8;
602 } else {
603 *((*outbuf)++) = ch>>8;
604 *((*outbuf)++) = ch&0xff;
605 *((*outbuf)++) = 0;
606 *((*outbuf)++) = 0;
607 }
608 *outlen -= sizeof(unichar_t);
609 ++char_cnt;
610 }
611 if ( *inlen==1 && *outlen>0 )
612 return( (size_t) -1 ); /* Incomplete multi-byte sequence */
613 } else if ( cd->from==e_wansung || cd->from==e_jisgbpk ) {
614 table = cd->from==e_jisgbpk ? unicode_from_gb2312 :
615 unicode_from_ksc5601 ;
616 while ( *inlen>0 && *outlen>1 ) {
617 unsigned char *ipt = (unsigned char *) *inbuf;
618 int ch;
619 if ( *ipt<0x7f ) {
620 ch = *ipt;
621 --*inlen;
622 *inbuf = (char *) ipt+1;
623 } else {
624 if ( *ipt<0xa1 || *ipt>0xfe || ipt[1]<0xa1 || ipt[1]>0xfe ||
625 *inlen==1 )
626 return( (size_t) -1 );
627 ch = (*ipt-0xa1)*94 + (ipt[1]-0xa1);
628 ch = table[ch];
629 *inlen -= 2;;
630 *inbuf = (char *) ipt+2;
631 }
632 if ( endian==end_little ) {
633 *((*outbuf)++) = 0;
634 *((*outbuf)++) = 0;
635 *((*outbuf)++) = ch&0xff;
636 *((*outbuf)++) = ch>>8;
637 } else {
638 *((*outbuf)++) = ch>>8;
639 *((*outbuf)++) = ch&0xff;
640 *((*outbuf)++) = 0;
641 *((*outbuf)++) = 0;
642 }
643 *outlen -= sizeof(unichar_t);
644 ++char_cnt;
645 }
646 } else if ( cd->from==e_johab || cd->from==e_big5 || cd->from==e_big5hkscs ) {
647 int offset;
648 if ( cd->from==e_big5 ) {
649 offset = 0xa100;
650 table = unicode_from_big5;
651 } else if ( cd->from==e_big5hkscs ) {
652 offset = 0x8100;
653 table = unicode_from_big5hkscs;
654 } else {
655 offset = 0x8400;
656 table = unicode_from_johab;
657 }
658 while ( *inlen>0 && *outlen>1 ) {
659 unsigned char *ipt = (unsigned char *) *inbuf;
660 int ch;
661 if ( *ipt<0x7f ) {
662 ch = *ipt;
663 --*inlen;
664 *inbuf = (char *) ipt+1;
665 } else {
666 if ( *inlen==1 )
667 return( (size_t) -1 );
668 ch = (*ipt<<8) | ipt[1];
669 if ( ch<offset )
670 return( (size_t) -1 );
671 ch -= offset;
672 ch = table[ch];
673 *inlen -= 2;
674 *inbuf = (char *) ipt+2;
675 }
676 if ( endian==end_little ) {
677 *((*outbuf)++) = 0;
678 *((*outbuf)++) = 0;
679 *((*outbuf)++) = ch&0xff;
680 *((*outbuf)++) = ch>>8;
681 } else {
682 *((*outbuf)++) = ch>>8;
683 *((*outbuf)++) = ch&0xff;
684 *((*outbuf)++) = 0;
685 *((*outbuf)++) = 0;
686 }
687 *outlen -= sizeof(unichar_t);
688 ++char_cnt;
689 }
690 } else if ( cd->from==e_sjis ) {
691 while ( *inlen>0 && *outlen>1 ) {
692 unsigned char *ipt = (unsigned char *) *inbuf;
693 int ch1 = *ipt;
694 if ( ch1<127 || ( ch1>=161 && ch1<=223 )) {
695 ch = unicode_from_jis201[ch1];
696 *inbuf = (char *) ipt+1;
697 --*inlen;
698 } else if ( *inlen==1 )
699 return( (size_t) -1 );
700 else {
701 int ch2 = ipt[1];
702 if ( ch1 >= 129 && ch1<= 159 )
703 ch1 -= 112;
704 else
705 ch1 -= 176;
706 ch1 <<= 1;
707 if ( ch2>=159 )
708 ch2-= 126;
709 else if ( ch2>127 ) {
710 --ch1;
711 ch2 -= 32;
712 } else {
713 --ch1;
714 ch2 -= 31;
715 }
716 if ( ch1-0x21>=94 || ch2-0x21>=94 )
717 return( (size_t) -1 );
718 ch = unicode_from_jis208[(ch1-0x21)*94+(ch2-0x21)];
719 *inlen -= 2;
720 *inbuf = (char *) ipt+2;
721 }
722 if ( endian==end_little ) {
723 *((*outbuf)++) = 0;
724 *((*outbuf)++) = 0;
725 *((*outbuf)++) = ch&0xff;
726 *((*outbuf)++) = ch>>8;
727 } else {
728 *((*outbuf)++) = ch>>8;
729 *((*outbuf)++) = ch&0xff;
730 *((*outbuf)++) = 0;
731 *((*outbuf)++) = 0;
732 }
733 *outlen -= sizeof(unichar_t);
734 ++char_cnt;
735 }
736 } else if ( cd->from==e_utf8 ) {
737 while ( *inlen>0 && *outlen>sizeof(unichar_t) ) {
738 unsigned char *ipt = (unsigned char *) *inbuf;
739 int ch = *ipt;
740 if ( ch <= 127 ) {
741 *inbuf = (char *) ipt+1;
742 --*inlen;
743 } else if ( ch<=0xdf ) {
744 if ( *inlen<2 || ipt[1]<0x80 )
745 return( (size_t) -1 );
746 ch = ((ch&0x1f)<<6) | (ipt[1] &0x3f);
747 *inlen -= 2;
748 *inbuf = (char *) ipt+2;
749 } else if ( ch<=0xef ) {
750 if ( *inlen<3 || ipt[1]<0x80 || ipt[2]<0x80 )
751 return( (size_t) -1 );
752 ch = ((ch&0x1f)<<12) | ((ipt[1] &0x3f)<<6) | (ipt[2]&0x3f);
753 *inlen -= 3;
754 *inbuf = (char *) ipt+3;
755 } else {
756 int w,w2;
757 w = ( ((*ipt&0x7)<<2) | ((ipt[1]&0x30)>>4) )-1;
758 w = (w<<6) | ((ipt[1]&0xf)<<2) | ((ipt[2]&0x30)>>4);
759 w2 = ((ipt[2]&0xf)<<6) | (ipt[3]&0x3f);
760 ch = w*0x400 + w2 + 0x10000;
761 *inbuf = (char *) ipt+4;
762 }
763 if ( endian==end_little ) {
764 *((*outbuf)++) = ch&0xff;
765 *((*outbuf)++) = ch>>8;
766 *((*outbuf)++) = ch>>16;
767 *((*outbuf)++) = ch>>24;
768 } else {
769 *((*outbuf)++) = ch>>24;
770 *((*outbuf)++) = ch>>16;
771 *((*outbuf)++) = ch>>8;
772 *((*outbuf)++) = ch&0xff;
773 }
774 *outlen -= sizeof(unichar_t);
775 ++char_cnt;
776 }
777 } else {
778 fprintf( stderr, "Unexpected encoding\n" );
779 return( (size_t) -1 );
780 }
781 } else {
782 fprintf( stderr, "One of the two encodings must be UCS2 in gww_iconv()\n" );
783 return( (size_t) -1 );
784 }
785
786 if ( *outlen>=1 ) {
787 **outbuf = '\0';
788 if ( *outlen>1 )
789 (*outbuf)[1] = '\0';
790 if ( cd->to==e_ucs4 && *outlen>3 ) {
791 (*outbuf)[2] = '\0';
792 (*outbuf)[3] = '\0';
793 }
794 }
795 return( char_cnt );
796 }
797 #else
798 static const int a_file_must_define_something=1;
799 #endif /* HAVE_ICONV_H */