2 * Copyright (c) 1997-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: siconv.c,v 1.7 2004-10-15 00:19:00 adam Exp $
9 * \brief Implements simple ICONV
11 * This implements an interface similar to that of iconv and
12 * is used by YAZ to interface with iconv (if present).
13 * For systems where iconv is not present, this layer
14 * provides a few important conversion: UTF-8, MARC-8, Latin-1.
32 #include <yaz/yaz-util.h>
34 unsigned long yaz_marc8_1_conv (unsigned char *inp, size_t inbytesleft,
35 size_t *no_read, int *combining);
36 unsigned long yaz_marc8_2_conv (unsigned char *inp, size_t inbytesleft,
37 size_t *no_read, int *combining);
38 unsigned long yaz_marc8_3_conv (unsigned char *inp, size_t inbytesleft,
39 size_t *no_read, int *combining);
40 unsigned long yaz_marc8_4_conv (unsigned char *inp, size_t inbytesleft,
41 size_t *no_read, int *combining);
42 unsigned long yaz_marc8_5_conv (unsigned char *inp, size_t inbytesleft,
43 size_t *no_read, int *combining);
44 unsigned long yaz_marc8_6_conv (unsigned char *inp, size_t inbytesleft,
45 size_t *no_read, int *combining);
46 unsigned long yaz_marc8_7_conv (unsigned char *inp, size_t inbytesleft,
47 size_t *no_read, int *combining);
48 unsigned long yaz_marc8_8_conv (unsigned char *inp, size_t inbytesleft,
49 size_t *no_read, int *combining);
50 unsigned long yaz_marc8_9_conv (unsigned char *inp, size_t inbytesleft,
51 size_t *no_read, int *combining);
53 struct yaz_iconv_struct {
56 size_t (*init_handle)(yaz_iconv_t cd, unsigned char *inbuf,
57 size_t inbytesleft, size_t *no_read);
58 unsigned long (*read_handle)(yaz_iconv_t cd, unsigned char *inbuf,
59 size_t inbytesleft, size_t *no_read);
60 size_t (*write_handle)(yaz_iconv_t cd, unsigned long x,
61 char **outbuf, size_t *outbytesleft);
64 int marc8_comb_no_read;
70 static unsigned long yaz_read_ISO8859_1 (yaz_iconv_t cd, unsigned char *inp,
71 size_t inbytesleft, size_t *no_read)
73 unsigned long x = inp[0];
78 static size_t yaz_init_UTF8 (yaz_iconv_t cd, unsigned char *inp,
79 size_t inbytesleft, size_t *no_read)
88 cd->my_errno = YAZ_ICONV_EINVAL;
91 if (inp[1] != 0xbb || inp[2] != 0xbf)
93 cd->my_errno = YAZ_ICONV_EILSEQ;
100 static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
101 size_t inbytesleft, size_t *no_read)
110 else if (inp[0] <= 0xbf || inp[0] >= 0xfe)
113 cd->my_errno = YAZ_ICONV_EILSEQ;
115 else if (inp[0] <= 0xdf && inbytesleft >= 2)
117 x = ((inp[0] & 0x1f) << 6) | (inp[1] & 0x3f);
123 cd->my_errno = YAZ_ICONV_EILSEQ;
126 else if (inp[0] <= 0xef && inbytesleft >= 3)
128 x = ((inp[0] & 0x0f) << 12) | ((inp[1] & 0x3f) << 6) |
135 cd->my_errno = YAZ_ICONV_EILSEQ;
138 else if (inp[0] <= 0xf7 && inbytesleft >= 4)
140 x = ((inp[0] & 0x07) << 18) | ((inp[1] & 0x3f) << 12) |
141 ((inp[2] & 0x3f) << 6) | (inp[3] & 0x3f);
147 cd->my_errno = YAZ_ICONV_EILSEQ;
150 else if (inp[0] <= 0xfb && inbytesleft >= 5)
152 x = ((inp[0] & 0x03) << 24) | ((inp[1] & 0x3f) << 18) |
153 ((inp[2] & 0x3f) << 12) | ((inp[3] & 0x3f) << 6) |
160 cd->my_errno = YAZ_ICONV_EILSEQ;
163 else if (inp[0] <= 0xfd && inbytesleft >= 6)
165 x = ((inp[0] & 0x01) << 30) | ((inp[1] & 0x3f) << 24) |
166 ((inp[2] & 0x3f) << 18) | ((inp[3] & 0x3f) << 12) |
167 ((inp[4] & 0x3f) << 6) | (inp[5] & 0x3f);
173 cd->my_errno = YAZ_ICONV_EILSEQ;
179 cd->my_errno = YAZ_ICONV_EINVAL;
184 static unsigned long yaz_read_UCS4 (yaz_iconv_t cd, unsigned char *inp,
185 size_t inbytesleft, size_t *no_read)
191 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
196 x = (inp[0]<<24) | (inp[1]<<16) | (inp[2]<<8) | inp[3];
202 static unsigned long yaz_read_UCS4LE (yaz_iconv_t cd, unsigned char *inp,
203 size_t inbytesleft, size_t *no_read)
209 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
214 x = (inp[3]<<24) | (inp[2]<<16) | (inp[1]<<8) | inp[0];
221 static unsigned long yaz_read_wchar_t (yaz_iconv_t cd, unsigned char *inp,
222 size_t inbytesleft, size_t *no_read)
226 if (inbytesleft < sizeof(wchar_t))
228 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
234 memcpy (&wch, inp, sizeof(wch));
236 *no_read = sizeof(wch);
242 static unsigned long yaz_read_marc8 (yaz_iconv_t cd, unsigned char *inp,
243 size_t inbytesleft, size_t *no_read)
245 if (cd->marc8_comb_x)
247 unsigned long x = cd->marc8_comb_x;
248 *no_read = cd->marc8_comb_no_read;
249 cd->marc8_comb_x = 0;
253 while(inbytesleft >= 1 && inp[0] == 27)
255 size_t inbytesleft0 = inbytesleft;
258 while(inbytesleft > 0 && strchr("(,$!", *inp))
263 if (inbytesleft <= 0)
266 cd->my_errno = YAZ_ICONV_EINVAL;
269 cd->marc8_esc_mode = *inp++;
271 (*no_read) += inbytesleft0 - inbytesleft;
273 if (inbytesleft <= 0)
279 size_t no_read_sub = 0;
281 switch(cd->marc8_esc_mode)
283 case 'B': /* Basic ASCII */
284 case 'E': /* ANSEL */
285 case 's': /* ASCII */
286 x = yaz_marc8_1_conv(inp, inbytesleft, &no_read_sub, &comb);
288 case 'g': /* Greek */
289 x = yaz_marc8_2_conv(inp, inbytesleft, &no_read_sub, &comb);
291 case 'b': /* Subscripts */
292 x = yaz_marc8_3_conv(inp, inbytesleft, &no_read_sub, &comb);
294 case 'p': /* Superscripts */
295 x = yaz_marc8_4_conv(inp, inbytesleft, &no_read_sub, &comb);
297 case '2': /* Basic Hebrew */
298 x = yaz_marc8_5_conv(inp, inbytesleft, &no_read_sub, &comb);
300 case 'N': /* Basic Cyrillic */
301 case 'Q': /* Extended Cyrillic */
302 x = yaz_marc8_6_conv(inp, inbytesleft, &no_read_sub, &comb);
304 case '3': /* Basic Arabic */
305 case '4': /* Extended Arabic */
306 x = yaz_marc8_7_conv(inp, inbytesleft, &no_read_sub, &comb);
308 case 'S': /* Greek */
309 x = yaz_marc8_8_conv(inp, inbytesleft, &no_read_sub, &comb);
311 case '1': /* Chinese, Japanese, Korean (EACC) */
312 x = yaz_marc8_9_conv(inp, inbytesleft, &no_read_sub, &comb);
316 cd->my_errno = YAZ_ICONV_EILSEQ;
320 printf ("esc mode=%c x=%04lX comb=%d\n", cd->marc8_esc_mode, x, comb);
322 *no_read += no_read_sub;
324 if (comb && cd->marc8_comb_x == 0)
327 unsigned long next_x;
329 /* read next char .. */
330 next_x = yaz_read_marc8(cd, inp + *no_read,
331 inbytesleft - *no_read, &tmp_read);
332 /* save this x for later .. */
333 cd->marc8_comb_x = x;
334 /* save next read for later .. */
335 cd->marc8_comb_no_read = tmp_read;
336 /* return next x - thereby swap */
343 static size_t yaz_write_UTF8 (yaz_iconv_t cd, unsigned long x,
344 char **outbuf, size_t *outbytesleft)
346 unsigned char *outp = (unsigned char *) *outbuf;
347 if (x <= 0x7f && *outbytesleft >= 1)
349 *outp++ = (unsigned char) x;
352 else if (x <= 0x7ff && *outbytesleft >= 2)
354 *outp++ = (unsigned char) ((x >> 6) | 0xc0);
355 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
356 (*outbytesleft) -= 2;
358 else if (x <= 0xffff && *outbytesleft >= 3)
360 *outp++ = (unsigned char) ((x >> 12) | 0xe0);
361 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
362 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
363 (*outbytesleft) -= 3;
365 else if (x <= 0x1fffff && *outbytesleft >= 4)
367 *outp++ = (unsigned char) ((x >> 18) | 0xf0);
368 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
369 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
370 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
371 (*outbytesleft) -= 4;
373 else if (x <= 0x3ffffff && *outbytesleft >= 5)
375 *outp++ = (unsigned char) ((x >> 24) | 0xf8);
376 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
377 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
378 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
379 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
380 (*outbytesleft) -= 5;
382 else if (*outbytesleft >= 6)
384 *outp++ = (unsigned char) ((x >> 30) | 0xfc);
385 *outp++ = (unsigned char) (((x >> 24) & 0x3f) | 0x80);
386 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
387 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
388 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
389 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
390 (*outbytesleft) -= 6;
394 cd->my_errno = YAZ_ICONV_E2BIG; /* not room for output */
397 *outbuf = (char *) outp;
401 static size_t yaz_write_ISO8859_1 (yaz_iconv_t cd, unsigned long x,
402 char **outbuf, size_t *outbytesleft)
404 unsigned char *outp = (unsigned char *) *outbuf;
405 if (x > 255 || x < 1)
407 cd->my_errno = YAZ_ICONV_EILSEQ;
410 else if (*outbytesleft >= 1)
412 *outp++ = (unsigned char) x;
417 cd->my_errno = YAZ_ICONV_E2BIG;
420 *outbuf = (char *) outp;
425 static size_t yaz_write_UCS4 (yaz_iconv_t cd, unsigned long x,
426 char **outbuf, size_t *outbytesleft)
428 unsigned char *outp = (unsigned char *) *outbuf;
429 if (*outbytesleft >= 4)
431 *outp++ = (unsigned char) (x>>24);
432 *outp++ = (unsigned char) (x>>16);
433 *outp++ = (unsigned char) (x>>8);
434 *outp++ = (unsigned char) x;
435 (*outbytesleft) -= 4;
439 cd->my_errno = YAZ_ICONV_E2BIG;
442 *outbuf = (char *) outp;
446 static size_t yaz_write_UCS4LE (yaz_iconv_t cd, unsigned long x,
447 char **outbuf, size_t *outbytesleft)
449 unsigned char *outp = (unsigned char *) *outbuf;
450 if (*outbytesleft >= 4)
452 *outp++ = (unsigned char) x;
453 *outp++ = (unsigned char) (x>>8);
454 *outp++ = (unsigned char) (x>>16);
455 *outp++ = (unsigned char) (x>>24);
456 (*outbytesleft) -= 4;
460 cd->my_errno = YAZ_ICONV_E2BIG;
463 *outbuf = (char *) outp;
468 static size_t yaz_write_wchar_t (yaz_iconv_t cd, unsigned long x,
469 char **outbuf, size_t *outbytesleft)
471 unsigned char *outp = (unsigned char *) *outbuf;
473 if (*outbytesleft >= sizeof(wchar_t))
476 memcpy(outp, &wch, sizeof(wch));
478 (*outbytesleft) -= sizeof(wch);
482 cd->my_errno = YAZ_ICONV_E2BIG;
485 *outbuf = (char *) outp;
490 int yaz_iconv_isbuiltin(yaz_iconv_t cd)
492 return cd->read_handle && cd->write_handle;
495 yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode)
497 yaz_iconv_t cd = (yaz_iconv_t) xmalloc (sizeof(*cd));
499 cd->write_handle = 0;
502 cd->my_errno = YAZ_ICONV_UNKNOWN;
503 cd->marc8_esc_mode = 'B';
504 cd->marc8_comb_x = 0;
506 /* a useful hack: if fromcode has leading @,
507 the library not use YAZ's own conversions .. */
508 if (fromcode[0] == '@')
512 if (!yaz_matchstr(fromcode, "UTF8"))
514 cd->read_handle = yaz_read_UTF8;
515 cd->init_handle = yaz_init_UTF8;
517 else if (!yaz_matchstr(fromcode, "ISO88591"))
518 cd->read_handle = yaz_read_ISO8859_1;
519 else if (!yaz_matchstr(fromcode, "UCS4"))
520 cd->read_handle = yaz_read_UCS4;
521 else if (!yaz_matchstr(fromcode, "UCS4LE"))
522 cd->read_handle = yaz_read_UCS4LE;
523 else if (!yaz_matchstr(fromcode, "MARC8"))
524 cd->read_handle = yaz_read_marc8;
526 else if (!yaz_matchstr(fromcode, "WCHAR_T"))
527 cd->read_handle = yaz_read_wchar_t;
530 if (!yaz_matchstr(tocode, "UTF8"))
531 cd->write_handle = yaz_write_UTF8;
532 else if (!yaz_matchstr(tocode, "ISO88591"))
533 cd->write_handle = yaz_write_ISO8859_1;
534 else if (!yaz_matchstr (tocode, "UCS4"))
535 cd->write_handle = yaz_write_UCS4;
536 else if (!yaz_matchstr(tocode, "UCS4LE"))
537 cd->write_handle = yaz_write_UCS4LE;
539 else if (!yaz_matchstr(tocode, "WCHAR_T"))
540 cd->write_handle = yaz_write_wchar_t;
545 if (!cd->read_handle || !cd->write_handle)
547 cd->iconv_cd = iconv_open (tocode, fromcode);
548 if (cd->iconv_cd == (iconv_t) (-1))
555 if (!cd->read_handle || !cd->write_handle)
565 size_t yaz_iconv (yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
566 char **outbuf, size_t *outbytesleft)
574 iconv(cd->iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
575 if (r == (size_t)(-1))
580 cd->my_errno = YAZ_ICONV_E2BIG;
583 cd->my_errno = YAZ_ICONV_EINVAL;
586 cd->my_errno = YAZ_ICONV_EILSEQ;
589 cd->my_errno = YAZ_ICONV_UNKNOWN;
595 if (inbuf == 0 || *inbuf == 0)
598 cd->my_errno = YAZ_ICONV_UNKNOWN;
608 size_t r = (cd->init_handle)(cd, (unsigned char *) *inbuf,
609 *inbytesleft, &no_read);
612 if (cd->my_errno == YAZ_ICONV_EINVAL)
617 *inbytesleft -= no_read;
627 if (*inbytesleft == 0)
633 x = (cd->read_handle)(cd, (unsigned char *) *inbuf, *inbytesleft,
642 r = (cd->write_handle)(cd, x, outbuf, outbytesleft);
646 *inbytesleft -= no_read;
652 int yaz_iconv_error (yaz_iconv_t cd)
657 int yaz_iconv_close (yaz_iconv_t cd)
661 iconv_close (cd->iconv_cd);