2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.29 2007-03-20 21:37:32 adam Exp $
17 #include <yaz/yaz-util.h>
20 static int compare_buffers(char *msg, int no,
21 int expect_len, const char *expect_buf,
22 int got_len, const char *got_buf)
24 if (expect_len == got_len
25 && !memcmp(expect_buf, got_buf, expect_len))
28 if (0) /* use 1 see how the buffers differ (for debug purposes) */
31 printf("tsticonv test=%s i=%d failed\n", msg, no);
32 printf("off got exp\n");
33 for (i = 0; i<got_len || i<expect_len; i++)
39 sprintf(got_char, "%02X", got_buf[i]);
41 sprintf(got_char, "? ");
44 sprintf(expect_char, "%02X", expect_buf[i]);
46 sprintf(expect_char, "? ");
48 printf("%02d %s %s %c\n",
49 i, got_char, expect_char, got_buf[i] == expect_buf[i] ?
57 static int tst_convert_l(yaz_iconv_t cd, size_t in_len, const char *in_buf,
58 size_t expect_len, const char *expect_buf)
61 char *inbuf= (char*) in_buf;
62 size_t inbytesleft = in_len > 0 ? in_len : strlen(in_buf);
64 char *outbuf = outbuf0;
68 size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
69 if (outbytesleft > 12)
71 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
72 if (r == (size_t) (-1))
74 int e = yaz_iconv_error(cd);
75 if (e != YAZ_ICONV_E2BIG)
80 yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
85 return compare_buffers("tsticonv 22", 0,
86 expect_len, expect_buf,
87 outbuf - outbuf0, outbuf0);
90 static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
93 WRBUF b = wrbuf_alloc();
95 size_t inbytesleft = strlen(buf);
96 const char *inp = buf;
98 for (rounds = 0; inbytesleft && rounds < sizeof(outbuf); rounds++)
100 size_t outbytesleft = sizeof(outbuf);
102 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
103 &outp, &outbytesleft);
104 wrbuf_write(b, outbuf, outp - outbuf);
105 if (r == (size_t) (-1))
107 int e = yaz_iconv_error(cd);
108 if (e != YAZ_ICONV_E2BIG)
113 size_t outbytesleft = sizeof(outbuf);
115 r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
116 wrbuf_write(b, outbuf, outp - outbuf);
120 if (wrbuf_len(b) == strlen(cmpbuf)
121 && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
124 yaz_log(YLOG_LOG, "GOT (%.*s)", wrbuf_len(b), wrbuf_buf(b));
130 /* some test strings in ISO-8859-1 format */
131 static const char *iso_8859_1_a[] = {
140 static void tst_marc8_to_ucs4b(void)
142 yaz_iconv_t cd = yaz_iconv_open("UCS4", "MARC8");
147 YAZ_CHECK(tst_convert_l(
150 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
152 "\x00\x00\xFF\x1F" "\x00\x00\x00o"));
153 YAZ_CHECK(tst_convert_l(
156 "\033$1" "\x6F\x77\x29" /* AE0E */
157 "\x6F\x52\x7C" /* c0F4 */ "\033(B",
159 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4"));
160 YAZ_CHECK(tst_convert_l(
164 "\x21\x50\x6E" /* UCS 7CFB */
165 "\x21\x51\x31" /* UCS 7D71 */
166 "\x21\x3A\x67" /* UCS 5B89 */
167 "\x21\x33\x22" /* UCS 5168 */
168 "\x21\x33\x53" /* UCS 5206 */
169 "\x21\x44\x2B" /* UCS 6790 */
177 "\x00\x00\x67\x90"));
179 YAZ_CHECK(tst_convert_l(
182 "\xB0\xB2", /* AYN and oSLASH */
184 "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"));
185 YAZ_CHECK(tst_convert_l(
188 "\xF6\x61", /* a underscore */
190 "\x00\x00\x00\x61" "\x00\x00\x03\x32"));
192 YAZ_CHECK(tst_convert_l(
195 "\x61\xC2", /* a, phonorecord mark */
197 "\x00\x00\x00\x61" "\x00\x00\x21\x17"));
200 YAZ_CHECK(tst_convert_l(
203 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
212 "\x00\x00\x00" "n"));
214 YAZ_CHECK(tst_convert_l(
219 "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"));
221 YAZ_CHECK(tst_convert_l(
226 "\x00\x00\x00\x74" "\x00\x00\x03\x61" "\x00\x00\x00\x73"));
228 YAZ_CHECK(tst_convert_l(
233 "\x00\x00\x00\x74" "\x00\x00\x03\x60" "\x00\x00\x00\x73"));
238 static void tst_ucs4b_to_utf8(void)
240 yaz_iconv_t cd = yaz_iconv_open("UTF8", "UCS4");
244 YAZ_CHECK(tst_convert_l(
247 "\x00\x00\xFF\x1F\x00\x00\x00o",
249 "\xEF\xBC\x9F\x6F"));
251 YAZ_CHECK(tst_convert_l(
254 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
256 "\xEA\xB8\x8E\xEC\x83\xB4"));
260 static void dconvert(int mandatory, const char *tmpcode)
265 for (i = 0; iso_8859_1_a[i]; i++)
268 char *inbuf = (char*) iso_8859_1_a[i];
269 size_t inbytesleft = strlen(inbuf);
272 char *outbuf = outbuf0;
273 size_t outbytesleft = sizeof(outbuf0);
275 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
276 YAZ_CHECK(cd || !mandatory);
279 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
280 YAZ_CHECK(r != (size_t) (-1));
282 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
283 YAZ_CHECK(r != (size_t) (-1));
285 if (r == (size_t) (-1))
288 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
289 YAZ_CHECK(cd || !mandatory);
293 inbytesleft = sizeof(outbuf0) - outbytesleft;
296 outbytesleft = sizeof(outbuf1);
297 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
298 YAZ_CHECK(r != (size_t) (-1));
300 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
301 if (r == (size_t)(-1))
303 fprintf(stderr, "failed\n");
305 YAZ_CHECK(r != (size_t) (-1));
307 if (r != (size_t)(-1))
309 ret = compare_buffers("dconvert", i,
310 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
311 sizeof(outbuf1) - outbytesleft, outbuf1);
318 int utf8_check(unsigned c)
327 size_t inbytesleft = 4;
328 char *outbuf = utf8buf;
329 size_t outbytesleft = sizeof(utf8buf);
331 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "UCS4LE");
334 for (i = 0; i<4; i++)
337 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
340 if (r == (size_t)(-1))
343 cd = yaz_iconv_open("UCS4LE", "UTF-8");
346 inbytesleft = sizeof(utf8buf) - outbytesleft;
352 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
353 if (r == (size_t)(-1))
358 if (memcmp(src, dst, 4))
364 static void tst_marc8_to_utf8(void)
366 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8");
372 YAZ_CHECK(tst_convert(cd, "Cours de math",
374 /* COMBINING ACUTE ACCENT */
375 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
376 "Cours de mathe\xcc\x81"));
379 YAZ_CHECK(tst_convert(cd, "a\xea\x1e", "a\x1e\xcc\x8a"));
381 YAZ_CHECK(tst_convert(cd, "a\xea", "a"));
385 static void tst_marc8s_to_utf8(void)
387 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8s");
393 YAZ_CHECK(tst_convert(cd, "Cours de math",
395 /* E9: LATIN SMALL LETTER E WITH ACUTE */
396 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
397 "Cours de math\xc3\xa9"));
403 static void tst_marc8_to_latin1(void)
405 yaz_iconv_t cd = yaz_iconv_open("ISO-8859-1", "MARC8");
411 YAZ_CHECK(tst_convert(cd, "ax", "ax"));
413 /* latin capital letter o with stroke */
414 YAZ_CHECK(tst_convert(cd, "\xa2", "\xd8"));
416 /* with latin small letter ae */
417 YAZ_CHECK(tst_convert(cd, "eneb\xb5r", "eneb\346r"));
419 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2", "\xe5" "\xd8"));
421 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2" "b", "\xe5" "\xd8" "b"));
423 YAZ_CHECK(tst_convert(cd, "\xea" "a" "\xea" "a", "\xe5" "\xe5"));
425 YAZ_CHECK(tst_convert(cd, "Cours de math",
427 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
429 YAZ_CHECK(tst_convert(cd, "12345678âe",
431 YAZ_CHECK(tst_convert(cd, "123456789âe",
433 YAZ_CHECK(tst_convert(cd, "1234567890âe",
435 YAZ_CHECK(tst_convert(cd, "12345678901âe",
437 YAZ_CHECK(tst_convert(cd, "Cours de mathâem",
439 YAZ_CHECK(tst_convert(cd, "Cours de mathâematiques",
440 "Cours de mathématiques"));
445 static void tst_utf8_to_marc8(void)
447 yaz_iconv_t cd = yaz_iconv_open("MARC8", "UTF-8");
453 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
455 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
456 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
458 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
459 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
461 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
462 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
464 /** UPPERCASE SCANDINAVIAN O */
465 YAZ_CHECK(tst_convert(cd, "S\xc3\x98", "S\xa2"));
468 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x8A", "\xEA" "A"));
470 /** A MACRON + UMLAUT, DIAERESIS */
471 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x84" "\xCC\x88",
474 /* Ligature spanning two characters */
475 YAZ_CHECK(tst_convert(cd,
476 "\x74" "\xCD\xA1" "\x73", /* UTF-8 */
477 "\xEB\x74\xEC\x73")); /* MARC-8 */
479 /* Double title spanning two characters */
480 YAZ_CHECK(tst_convert(cd,
481 "\x74" "\xCD\xA0" "\x73", /* UTF-8 */
482 "\xFA\x74\xFB\x73")); /* MARC-8 */
484 /** Ideographic question mark (Unicode FF1F) */
485 YAZ_CHECK(tst_convert(cd,
486 "\xEF\xBC\x9F" "o", /* UTF-8 */
487 "\033$1" "\x21\x2B\x3B" "\033(B" "o" ));
490 /** Superscript 0 . bug #642 */
491 YAZ_CHECK(tst_convert(cd,
492 "(\xe2\x81\xb0)", /* UTF-8 */
497 char *inbuf0 = "\xe2\x81\xb0";
498 char *inbuf = inbuf0;
499 size_t inbytesleft = strlen(inbuf);
501 char *outbuf = outbuf0;
502 size_t outbytesleft = sizeof(outbuf0)-1;
507 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
508 YAZ_CHECK(r != (size_t) (-1));
511 *outbuf = '\0'; /* so we know when to stop printing */
512 for (i = 0; outbuf0[i]; i++)
514 int ch = outbuf0[i] & 0xff;
515 yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
519 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
520 YAZ_CHECK(r != (size_t) (-1));
521 *outbuf = '\0'; /* for strcmp test below and printing */
523 for (i = 0; outbuf0[i]; i++)
525 int ch = outbuf0[i] & 0xff;
526 yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
529 YAZ_CHECK(strcmp("\033p0\x1bs", outbuf0) == 0);
534 static void tst_advance_to_utf8(void)
536 yaz_iconv_t cd = yaz_iconv_open("utf-8", "advancegreek");
542 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
546 static void tst_utf8_to_advance(void)
548 yaz_iconv_t cd = yaz_iconv_open("advancegreek", "utf-8");
554 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
558 static void tst_latin1_to_marc8(void)
560 yaz_iconv_t cd = yaz_iconv_open("MARC8", "ISO-8859-1");
566 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
568 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
569 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
571 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
572 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
574 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
575 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
577 /** D8: UPPERCASE SCANDINAVIAN O */
578 YAZ_CHECK(tst_convert(cd, "S\xd8", "S\xa2"));
580 /** E9: LATIN SMALL LETTER E WITH ACUTE */
581 YAZ_CHECK(tst_convert(cd, "Cours de math\xe9", "Cours de mathâe"));
582 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"
584 YAZ_CHECK(tst_convert(cd, "Cours de mathé", "Cours de mathâe" ));
585 YAZ_CHECK(tst_convert(cd, "12345678é","12345678âe"));
586 YAZ_CHECK(tst_convert(cd, "123456789é", "123456789âe"));
587 YAZ_CHECK(tst_convert(cd, "1234567890é","1234567890âe"));
588 YAZ_CHECK(tst_convert(cd, "12345678901é", "12345678901âe"));
589 YAZ_CHECK(tst_convert(cd, "Cours de mathém", "Cours de mathâem"));
590 YAZ_CHECK(tst_convert(cd, "Cours de mathématiques",
591 "Cours de mathâematiques"));
595 static void tst_utf8_codes(void)
597 YAZ_CHECK(utf8_check(3));
598 YAZ_CHECK(utf8_check(127));
599 YAZ_CHECK(utf8_check(128));
600 YAZ_CHECK(utf8_check(255));
601 YAZ_CHECK(utf8_check(256));
602 YAZ_CHECK(utf8_check(900));
603 YAZ_CHECK(utf8_check(1000));
604 YAZ_CHECK(utf8_check(10000));
605 YAZ_CHECK(utf8_check(100000));
606 YAZ_CHECK(utf8_check(1000000));
607 YAZ_CHECK(utf8_check(10000000));
608 YAZ_CHECK(utf8_check(100000000));
611 int main (int argc, char **argv)
613 YAZ_CHECK_INIT(argc, argv);
619 tst_marc8s_to_utf8();
621 tst_marc8_to_latin1();
623 tst_advance_to_utf8();
624 tst_utf8_to_advance();
628 tst_latin1_to_marc8();
630 tst_marc8_to_ucs4b();
633 dconvert(1, "UTF-8");
634 dconvert(1, "ISO-8859-1");
636 dconvert(1, "UCS4LE");
637 dconvert(0, "CP865");
644 * indent-tabs-mode: nil
646 * vim: shiftwidth=4 tabstop=8 expandtab