2 * Copyright (c) 1997-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: siconv.c,v 1.4 2004-03-15 22:51:10 adam Exp $
8 /* mini iconv and wrapper for system iconv library (if present) */
25 #include <yaz/yaz-util.h>
27 unsigned long yaz_marc8_conv (unsigned char *inp, size_t inbytesleft,
30 unsigned long yaz_marc8_cjk_conv (unsigned char *inp, size_t inbytesleft,
33 struct yaz_iconv_struct {
36 size_t (*init_handle)(yaz_iconv_t cd, unsigned char *inbuf,
37 size_t inbytesleft, size_t *no_read);
38 unsigned long (*read_handle)(yaz_iconv_t cd, unsigned char *inbuf,
39 size_t inbytesleft, size_t *no_read);
40 size_t (*write_handle)(yaz_iconv_t cd, unsigned long x,
41 char **outbuf, size_t *outbytesleft);
48 static unsigned long yaz_read_ISO8859_1 (yaz_iconv_t cd, unsigned char *inp,
49 size_t inbytesleft, size_t *no_read)
51 unsigned long x = inp[0];
56 static size_t yaz_init_UTF8 (yaz_iconv_t cd, unsigned char *inp,
57 size_t inbytesleft, size_t *no_read)
66 cd->my_errno = YAZ_ICONV_EINVAL;
69 if (inp[1] != 0xbb || inp[2] != 0xbf)
71 cd->my_errno = YAZ_ICONV_EILSEQ;
78 static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
79 size_t inbytesleft, size_t *no_read)
88 else if (inp[0] <= 0xbf || inp[0] >= 0xfe)
91 cd->my_errno = YAZ_ICONV_EILSEQ;
93 else if (inp[0] <= 0xdf && inbytesleft >= 2)
95 x = ((inp[0] & 0x1f) << 6) | (inp[1] & 0x3f);
101 cd->my_errno = YAZ_ICONV_EILSEQ;
104 else if (inp[0] <= 0xef && inbytesleft >= 3)
106 x = ((inp[0] & 0x0f) << 12) | ((inp[1] & 0x3f) << 6) |
113 cd->my_errno = YAZ_ICONV_EILSEQ;
116 else if (inp[0] <= 0xf7 && inbytesleft >= 4)
118 x = ((inp[0] & 0x07) << 18) | ((inp[1] & 0x3f) << 12) |
119 ((inp[2] & 0x3f) << 6) | (inp[3] & 0x3f);
125 cd->my_errno = YAZ_ICONV_EILSEQ;
128 else if (inp[0] <= 0xfb && inbytesleft >= 5)
130 x = ((inp[0] & 0x03) << 24) | ((inp[1] & 0x3f) << 18) |
131 ((inp[2] & 0x3f) << 12) | ((inp[3] & 0x3f) << 6) |
138 cd->my_errno = YAZ_ICONV_EILSEQ;
141 else if (inp[0] <= 0xfd && inbytesleft >= 6)
143 x = ((inp[0] & 0x01) << 30) | ((inp[1] & 0x3f) << 24) |
144 ((inp[2] & 0x3f) << 18) | ((inp[3] & 0x3f) << 12) |
145 ((inp[4] & 0x3f) << 6) | (inp[5] & 0x3f);
151 cd->my_errno = YAZ_ICONV_EILSEQ;
157 cd->my_errno = YAZ_ICONV_EINVAL;
162 static unsigned long yaz_read_UCS4 (yaz_iconv_t cd, unsigned char *inp,
163 size_t inbytesleft, size_t *no_read)
169 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
174 x = (inp[0]<<24) | (inp[1]<<16) | (inp[2]<<8) | inp[3];
180 static unsigned long yaz_read_UCS4LE (yaz_iconv_t cd, unsigned char *inp,
181 size_t inbytesleft, size_t *no_read)
187 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
192 x = (inp[3]<<24) | (inp[2]<<16) | (inp[1]<<8) | inp[0];
199 static unsigned long yaz_read_wchar_t (yaz_iconv_t cd, unsigned char *inp,
200 size_t inbytesleft, size_t *no_read)
204 if (inbytesleft < sizeof(wchar_t))
206 cd->my_errno = YAZ_ICONV_EINVAL; /* incomplete input */
212 memcpy (&wch, inp, sizeof(wch));
214 *no_read = sizeof(wch);
220 static unsigned long yaz_read_marc8 (yaz_iconv_t cd, unsigned char *inp,
221 size_t inbytesleft, size_t *no_read)
224 while(inbytesleft >= 1 && inp[0] == 27)
226 size_t inbytesleft0 = inbytesleft;
229 while(inbytesleft > 0 && strchr("(,$", *inp))
234 if (inbytesleft <= 0)
237 cd->my_errno = YAZ_ICONV_EINVAL;
242 if (inbytesleft <= 1)
245 cd->my_errno = YAZ_ICONV_EINVAL;
251 cd->marc8_esc_mode = *inp++;
253 (*no_read) += inbytesleft0 - inbytesleft;
255 if (inbytesleft <= 0)
260 size_t no_read_sub = 0;
262 switch(cd->marc8_esc_mode)
266 x = yaz_marc8_conv(inp, inbytesleft, &no_read_sub);
267 *no_read += no_read_sub;
270 x = yaz_marc8_cjk_conv(inp, inbytesleft, &no_read_sub);
271 *no_read += no_read_sub;
275 cd->my_errno = YAZ_ICONV_EILSEQ;
281 static size_t yaz_write_UTF8 (yaz_iconv_t cd, unsigned long x,
282 char **outbuf, size_t *outbytesleft)
284 unsigned char *outp = (unsigned char *) *outbuf;
285 if (x <= 0x7f && *outbytesleft >= 1)
287 *outp++ = (unsigned char) x;
290 else if (x <= 0x7ff && *outbytesleft >= 2)
292 *outp++ = (unsigned char) ((x >> 6) | 0xc0);
293 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
294 (*outbytesleft) -= 2;
296 else if (x <= 0xffff && *outbytesleft >= 3)
298 *outp++ = (unsigned char) ((x >> 12) | 0xe0);
299 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
300 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
301 (*outbytesleft) -= 3;
303 else if (x <= 0x1fffff && *outbytesleft >= 4)
305 *outp++ = (unsigned char) ((x >> 18) | 0xf0);
306 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
307 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
308 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
309 (*outbytesleft) -= 4;
311 else if (x <= 0x3ffffff && *outbytesleft >= 5)
313 *outp++ = (unsigned char) ((x >> 24) | 0xf8);
314 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
315 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
316 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
317 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
318 (*outbytesleft) -= 5;
320 else if (*outbytesleft >= 6)
322 *outp++ = (unsigned char) ((x >> 30) | 0xfc);
323 *outp++ = (unsigned char) (((x >> 24) & 0x3f) | 0x80);
324 *outp++ = (unsigned char) (((x >> 18) & 0x3f) | 0x80);
325 *outp++ = (unsigned char) (((x >> 12) & 0x3f) | 0x80);
326 *outp++ = (unsigned char) (((x >> 6) & 0x3f) | 0x80);
327 *outp++ = (unsigned char) ((x & 0x3f) | 0x80);
328 (*outbytesleft) -= 6;
332 cd->my_errno = YAZ_ICONV_E2BIG; /* not room for output */
335 *outbuf = (char *) outp;
339 static size_t yaz_write_ISO8859_1 (yaz_iconv_t cd, unsigned long x,
340 char **outbuf, size_t *outbytesleft)
342 unsigned char *outp = (unsigned char *) *outbuf;
343 if (x > 255 || x < 1)
345 cd->my_errno = YAZ_ICONV_EILSEQ;
348 else if (*outbytesleft >= 1)
350 *outp++ = (unsigned char) x;
355 cd->my_errno = YAZ_ICONV_E2BIG;
358 *outbuf = (char *) outp;
363 static size_t yaz_write_UCS4 (yaz_iconv_t cd, unsigned long x,
364 char **outbuf, size_t *outbytesleft)
366 unsigned char *outp = (unsigned char *) *outbuf;
367 if (*outbytesleft >= 4)
369 *outp++ = (unsigned char) (x>>24);
370 *outp++ = (unsigned char) (x>>16);
371 *outp++ = (unsigned char) (x>>8);
372 *outp++ = (unsigned char) x;
373 (*outbytesleft) -= 4;
377 cd->my_errno = YAZ_ICONV_E2BIG;
380 *outbuf = (char *) outp;
384 static size_t yaz_write_UCS4LE (yaz_iconv_t cd, unsigned long x,
385 char **outbuf, size_t *outbytesleft)
387 unsigned char *outp = (unsigned char *) *outbuf;
388 if (*outbytesleft >= 4)
390 *outp++ = (unsigned char) x;
391 *outp++ = (unsigned char) (x>>8);
392 *outp++ = (unsigned char) (x>>16);
393 *outp++ = (unsigned char) (x>>24);
394 (*outbytesleft) -= 4;
398 cd->my_errno = YAZ_ICONV_E2BIG;
401 *outbuf = (char *) outp;
406 static size_t yaz_write_wchar_t (yaz_iconv_t cd, unsigned long x,
407 char **outbuf, size_t *outbytesleft)
409 unsigned char *outp = (unsigned char *) *outbuf;
411 if (*outbytesleft >= sizeof(wchar_t))
414 memcpy(outp, &wch, sizeof(wch));
416 (*outbytesleft) -= sizeof(wch);
420 cd->my_errno = YAZ_ICONV_E2BIG;
423 *outbuf = (char *) outp;
428 int yaz_iconv_isbuiltin(yaz_iconv_t cd)
430 return cd->read_handle && cd->write_handle;
433 yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode)
435 yaz_iconv_t cd = (yaz_iconv_t) xmalloc (sizeof(*cd));
437 cd->write_handle = 0;
440 cd->my_errno = YAZ_ICONV_UNKNOWN;
441 cd->marc8_esc_mode = 'B';
443 /* a useful hack: if fromcode has leading @,
444 the library not use YAZ's own conversions .. */
445 if (fromcode[0] == '@')
449 if (!yaz_matchstr(fromcode, "UTF8"))
451 cd->read_handle = yaz_read_UTF8;
452 cd->init_handle = yaz_init_UTF8;
454 else if (!yaz_matchstr(fromcode, "ISO88591"))
455 cd->read_handle = yaz_read_ISO8859_1;
456 else if (!yaz_matchstr(fromcode, "UCS4"))
457 cd->read_handle = yaz_read_UCS4;
458 else if (!yaz_matchstr(fromcode, "UCS4LE"))
459 cd->read_handle = yaz_read_UCS4LE;
460 else if (!yaz_matchstr(fromcode, "MARC8"))
461 cd->read_handle = yaz_read_marc8;
463 else if (!yaz_matchstr(fromcode, "WCHAR_T"))
464 cd->read_handle = yaz_read_wchar_t;
467 if (!yaz_matchstr(tocode, "UTF8"))
468 cd->write_handle = yaz_write_UTF8;
469 else if (!yaz_matchstr(tocode, "ISO88591"))
470 cd->write_handle = yaz_write_ISO8859_1;
471 else if (!yaz_matchstr (tocode, "UCS4"))
472 cd->write_handle = yaz_write_UCS4;
473 else if (!yaz_matchstr(tocode, "UCS4LE"))
474 cd->write_handle = yaz_write_UCS4LE;
476 else if (!yaz_matchstr(tocode, "WCHAR_T"))
477 cd->write_handle = yaz_write_wchar_t;
482 if (!cd->read_handle || !cd->write_handle)
484 cd->iconv_cd = iconv_open (tocode, fromcode);
485 if (cd->iconv_cd == (iconv_t) (-1))
492 if (!cd->read_handle || !cd->write_handle)
502 size_t yaz_iconv (yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
503 char **outbuf, size_t *outbytesleft)
511 iconv(cd->iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
512 if (r == (size_t)(-1))
517 cd->my_errno = YAZ_ICONV_E2BIG;
520 cd->my_errno = YAZ_ICONV_EINVAL;
523 cd->my_errno = YAZ_ICONV_EILSEQ;
526 cd->my_errno = YAZ_ICONV_UNKNOWN;
532 if (inbuf == 0 || *inbuf == 0)
535 cd->my_errno = YAZ_ICONV_UNKNOWN;
545 size_t r = (cd->init_handle)(cd, (unsigned char *) *inbuf,
546 *inbytesleft, &no_read);
549 if (cd->my_errno == YAZ_ICONV_EINVAL)
554 *inbytesleft -= no_read;
564 if (*inbytesleft == 0)
570 x = (cd->read_handle)(cd, (unsigned char *) *inbuf, *inbytesleft,
579 r = (cd->write_handle)(cd, x, outbuf, outbytesleft);
583 *inbytesleft -= no_read;
589 int yaz_iconv_error (yaz_iconv_t cd)
594 int yaz_iconv_close (yaz_iconv_t cd)
598 iconv_close (cd->iconv_cd);