1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
15 #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_x(yaz_iconv_t cd, const char *buf, const char *cmpbuf,
94 WRBUF b = wrbuf_alloc();
96 size_t inbytesleft = strlen(buf);
97 const char *inp = buf;
99 for (rounds = 0; inbytesleft && rounds < sizeof(outbuf); rounds++)
101 size_t outbytesleft = sizeof(outbuf);
103 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
104 &outp, &outbytesleft);
105 wrbuf_write(b, outbuf, outp - outbuf);
106 if (r == (size_t) (-1))
108 int e = yaz_iconv_error(cd);
109 if (e != YAZ_ICONV_E2BIG)
111 if (expect_error != -1)
112 if (e != expect_error)
119 size_t outbytesleft = sizeof(outbuf);
121 r = yaz_iconv(cd, 0, 0, &outp, &outbytesleft);
122 wrbuf_write(b, outbuf, outp - outbuf);
123 if (expect_error != -1)
129 if (wrbuf_len(b) == strlen(cmpbuf)
130 && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
134 WRBUF w = wrbuf_alloc();
138 wrbuf_puts_escaped(w, buf);
139 yaz_log(YLOG_LOG, "input %s", wrbuf_cstr(w));
142 wrbuf_write_escaped(w, wrbuf_buf(b), wrbuf_len(b));
143 yaz_log(YLOG_LOG, "got %s", wrbuf_cstr(w));
146 wrbuf_puts_escaped(w, cmpbuf);
147 yaz_log(YLOG_LOG, "exp %s", wrbuf_cstr(w));
156 static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
158 return tst_convert_x(cd, buf, cmpbuf, 0);
161 /* some test strings in ISO-8859-1 format */
162 static const char *iso_8859_1_a[] = {
171 static void tst_marc8_to_ucs4b(void)
173 yaz_iconv_t cd = yaz_iconv_open("UCS4", "MARC8");
178 YAZ_CHECK(tst_convert_l(
181 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
183 "\x00\x00\xFF\x1F" "\x00\x00\x00o"));
184 YAZ_CHECK(tst_convert_l(
187 "\033$1" "\x6F\x77\x29" /* AE0E */
188 "\x6F\x52\x7C" /* c0F4 */ "\033(B",
190 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4"));
191 YAZ_CHECK(tst_convert_l(
195 "\x21\x50\x6E" /* UCS 7CFB */
196 "\x21\x51\x31" /* UCS 7D71 */
197 "\x21\x3A\x67" /* UCS 5B89 */
198 "\x21\x33\x22" /* UCS 5168 */
199 "\x21\x33\x53" /* UCS 5206 */
200 "\x21\x44\x2B" /* UCS 6790 */
208 "\x00\x00\x67\x90"));
210 YAZ_CHECK(tst_convert_l(
213 "\xB0\xB2", /* AYN and oSLASH */
215 "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"));
216 YAZ_CHECK(tst_convert_l(
219 "\xF6\x61", /* a underscore */
221 "\x00\x00\x00\x61" "\x00\x00\x03\x32"));
223 YAZ_CHECK(tst_convert_l(
226 "\x61\xC2", /* a, phonorecord mark */
228 "\x00\x00\x00\x61" "\x00\x00\x21\x17"));
231 YAZ_CHECK(tst_convert_l(
234 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
243 "\x00\x00\x00" "n"));
245 YAZ_CHECK(tst_convert_l(
250 "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"));
252 YAZ_CHECK(tst_convert_l(
257 "\x00\x00\x00\x74" "\x00\x00\x03\x61" "\x00\x00\x00\x73"));
259 YAZ_CHECK(tst_convert_l(
264 "\x00\x00\x00\x74" "\x00\x00\x03\x60" "\x00\x00\x00\x73"));
269 static void tst_ucs4b_to_utf8(void)
271 yaz_iconv_t cd = yaz_iconv_open("UTF8", "UCS4");
275 YAZ_CHECK(tst_convert_l(
278 "\x00\x00\xFF\x1F\x00\x00\x00o",
280 "\xEF\xBC\x9F\x6F"));
282 YAZ_CHECK(tst_convert_l(
285 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
287 "\xEA\xB8\x8E\xEC\x83\xB4"));
291 static void dconvert(int mandatory, const char *tmpcode)
296 for (i = 0; iso_8859_1_a[i]; i++)
299 char *inbuf = (char*) iso_8859_1_a[i];
300 size_t inbytesleft = strlen(inbuf);
303 char *outbuf = outbuf0;
304 size_t outbytesleft = sizeof(outbuf0);
306 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
307 YAZ_CHECK(cd || !mandatory);
310 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
311 YAZ_CHECK(r != (size_t) (-1));
313 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
314 YAZ_CHECK(r != (size_t) (-1));
316 if (r == (size_t) (-1))
319 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
320 YAZ_CHECK(cd || !mandatory);
324 inbytesleft = sizeof(outbuf0) - outbytesleft;
327 outbytesleft = sizeof(outbuf1);
328 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
329 YAZ_CHECK(r != (size_t) (-1));
331 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
332 if (r == (size_t)(-1))
334 fprintf(stderr, "failed\n");
336 YAZ_CHECK(r != (size_t) (-1));
338 if (r != (size_t)(-1))
340 ret = compare_buffers("dconvert", i,
341 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
342 sizeof(outbuf1) - outbytesleft, outbuf1);
349 int utf8_check(unsigned c)
358 size_t inbytesleft = 4;
359 char *outbuf = utf8buf;
360 size_t outbytesleft = sizeof(utf8buf);
362 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "UCS4LE");
365 for (i = 0; i<4; i++)
368 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
371 if (r == (size_t)(-1))
374 cd = yaz_iconv_open("UCS4LE", "UTF-8");
377 inbytesleft = sizeof(utf8buf) - outbytesleft;
383 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
384 if (r == (size_t)(-1))
389 if (memcmp(src, dst, 4))
395 static void tst_marc8_to_utf8(void)
397 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8");
403 YAZ_CHECK(tst_convert(cd, "Cours de math",
405 /* COMBINING ACUTE ACCENT */
406 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
407 "Cours de mathe\xcc\x81"));
409 YAZ_CHECK(tst_convert(cd, "\xea" "a", "a\xcc\x8a"));
410 YAZ_CHECK(tst_convert(cd, "a" "\xea" "\x1e", "a" "\x1e\xcc\x8a"));
411 YAZ_CHECK(tst_convert(cd, "a" "\xea" "p", "a" "p\xcc\x8a"));
413 YAZ_CHECK(tst_convert_x(cd, "a\xea", "a", YAZ_ICONV_EINVAL));
414 YAZ_CHECK(tst_convert(cd, "p", "\xcc\x8a")); /* note: missing p */
415 yaz_iconv(cd, 0, 0, 0, 0); /* incomplete. so we have to reset */
418 YAZ_CHECK(tst_convert(cd, ESC "(N" ESC ")Qp" ESC "(B", "\xd0\x9f"));
420 YAZ_CHECK(tst_convert_x(cd, ESC , "", YAZ_ICONV_EINVAL));
421 YAZ_CHECK(tst_convert_x(cd, ESC "(", "", YAZ_ICONV_EINVAL));
422 YAZ_CHECK(tst_convert_x(cd, ESC "(B", "", 0));
424 YAZ_CHECK(tst_convert(cd, ESC "(B" "\x31", "1")); /* ASCII in G0 */
425 YAZ_CHECK(tst_convert(cd, ESC ")B" "\xB1", "1")); /* ASCII in G1 */
430 static void tst_marc8s_to_utf8(void)
432 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8s");
438 YAZ_CHECK(tst_convert(cd, "Cours de math",
440 /* E9: LATIN SMALL LETTER E WITH ACUTE */
441 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
442 "Cours de math\xc3\xa9"));
448 static void tst_marc8_to_latin1(void)
450 yaz_iconv_t cd = yaz_iconv_open("ISO-8859-1", "MARC8");
456 YAZ_CHECK(tst_convert(cd, "ax", "ax"));
458 /* latin capital letter o with stroke */
459 YAZ_CHECK(tst_convert(cd, "\xa2", "\xd8"));
461 /* with latin small letter ae */
462 YAZ_CHECK(tst_convert(cd, "eneb\xb5r", "eneb\346r"));
464 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2", "\xe5" "\xd8"));
466 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2" "b", "\xe5" "\xd8" "b"));
468 YAZ_CHECK(tst_convert(cd, "\xea" "a" "\xea" "a", "\xe5" "\xe5"));
470 YAZ_CHECK(tst_convert(cd, "Cours de math",
472 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
474 YAZ_CHECK(tst_convert(cd, "12345678âe",
476 YAZ_CHECK(tst_convert(cd, "123456789âe",
478 YAZ_CHECK(tst_convert(cd, "1234567890âe",
480 YAZ_CHECK(tst_convert(cd, "12345678901âe",
482 YAZ_CHECK(tst_convert(cd, "Cours de mathâem",
484 YAZ_CHECK(tst_convert(cd, "Cours de mathâematiques",
485 "Cours de mathématiques"));
490 static void tst_utf8_to_marc8(const char *marc8_type)
492 yaz_iconv_t cd = yaz_iconv_open(marc8_type, "UTF-8");
498 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
500 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
501 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
503 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
504 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
506 /** Pure ASCII. 13 characters (sizeof(outbuf)+1) */
507 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
509 /** UPPERCASE SCANDINAVIAN O */
510 YAZ_CHECK(tst_convert(cd, "S\xc3\x98", "S\xa2"));
513 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x8A", "\xEA" "A"));
515 /** A MACRON + UMLAUT, DIAERESIS */
516 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x84" "\xCC\x88",
519 /* Ligature spanning two characters */
520 YAZ_CHECK(tst_convert(cd,
521 "\x74" "\xCD\xA1" "\x73", /* UTF-8 */
522 "\xEB\x74\xEC\x73")); /* MARC-8 */
524 /* Double title spanning two characters */
525 YAZ_CHECK(tst_convert(cd,
526 "\x74" "\xCD\xA0" "\x73", /* UTF-8 */
527 "\xFA\x74\xFB\x73")); /* MARC-8 */
529 /** Ideographic question mark (Unicode FF1F) */
530 YAZ_CHECK(tst_convert(cd,
531 "\xEF\xBC\x9F" "o", /* UTF-8 */
532 "\033$1" "\x21\x2B\x3B" "\033(B" "o" ));
535 /** Ideographic space per ANSI Z39.64 */
536 YAZ_CHECK(tst_convert(cd,
537 "\xe3\x80\x80" "o", /* UTF-8 */
538 "\033$1" "\x21\x23\x21" "\033(B" "o" ));
540 /** Superscript 0 . bug #642 */
541 YAZ_CHECK(tst_convert(cd,
542 "(\xe2\x81\xb0)", /* UTF-8 */
547 YAZ_CHECK(tst_convert(cd,
548 /* offset 0x530 in UTF-8 rec marccol4.u8.marc */
549 "\xE3\x83\xB3" "\xE3\x82\xBF"
550 "\xCC\x84" "\xCC\x84" "\xE3\x83\xBC" /* UTF-8 */,
551 "\x1B\x24\x31" "\x69\x25\x73"
552 "\x1B\x28\x42" "\xE5\xE5" "\x1B\x24\x31"
554 "\x69\x21\x3C" "\x1B\x28\x42"));
558 YAZ_CHECK(tst_convert(cd,
559 "\xCE\x94\xCE\xB5\xCF\x84"
560 "\xCE\xBF\xCF\x81\xCE\xB1"
561 "\xCE\xBA\xCE\xB7\xCF\x82\x2C",
563 "\x1B\x28\x53\x45\x66\x78\x72\x75"
569 char *inbuf0 = "\xe2\x81\xb0";
570 char *inbuf = inbuf0;
571 size_t inbytesleft = strlen(inbuf);
573 char *outbuf = outbuf0;
574 size_t outbytesleft = sizeof(outbuf0)-1;
579 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
580 YAZ_CHECK(r != (size_t) (-1));
583 *outbuf = '\0'; /* so we know when to stop printing */
584 for (i = 0; outbuf0[i]; i++)
586 int ch = outbuf0[i] & 0xff;
587 yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
591 r = yaz_iconv(cd, 0, 0, &outbuf, &outbytesleft);
592 YAZ_CHECK(r != (size_t) (-1));
593 *outbuf = '\0'; /* for strcmp test below and printing */
595 for (i = 0; outbuf0[i]; i++)
597 int ch = outbuf0[i] & 0xff;
598 yaz_log(YLOG_LOG, "ch%d %02X %c", i, ch, ch >= ' ' ? ch : '?');
601 YAZ_CHECK(strcmp("\033p0\x1bs", outbuf0) == 0);
603 yaz_iconv(cd, 0, 0, 0, 0);
607 static void tst_advance_to_utf8(void)
609 yaz_iconv_t cd = yaz_iconv_open("utf-8", "advancegreek");
615 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
619 static void tst_utf8_to_advance(void)
621 yaz_iconv_t cd = yaz_iconv_open("advancegreek", "utf-8");
627 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
631 static void tst_latin1_to_marc8(void)
633 yaz_iconv_t cd = yaz_iconv_open("MARC8", "ISO-8859-1");
639 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
641 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
642 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
644 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
645 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
647 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
648 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
650 /** D8: UPPERCASE SCANDINAVIAN O */
651 YAZ_CHECK(tst_convert(cd, "S\xd8", "S\xa2"));
653 /** E9: LATIN SMALL LETTER E WITH ACUTE */
654 YAZ_CHECK(tst_convert(cd, "Cours de math\xe9", "Cours de mathâe"));
655 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"
657 YAZ_CHECK(tst_convert(cd, "Cours de mathé", "Cours de mathâe" ));
658 YAZ_CHECK(tst_convert(cd, "12345678é","12345678âe"));
659 YAZ_CHECK(tst_convert(cd, "123456789é", "123456789âe"));
660 YAZ_CHECK(tst_convert(cd, "1234567890é","1234567890âe"));
661 YAZ_CHECK(tst_convert(cd, "12345678901é", "12345678901âe"));
662 YAZ_CHECK(tst_convert(cd, "Cours de mathém", "Cours de mathâem"));
663 YAZ_CHECK(tst_convert(cd, "Cours de mathématiques",
664 "Cours de mathâematiques"));
668 static void tst_utf8_codes(void)
670 YAZ_CHECK(utf8_check(3));
671 YAZ_CHECK(utf8_check(127));
672 YAZ_CHECK(utf8_check(128));
673 YAZ_CHECK(utf8_check(255));
674 YAZ_CHECK(utf8_check(256));
675 YAZ_CHECK(utf8_check(900));
676 YAZ_CHECK(utf8_check(1000));
677 YAZ_CHECK(utf8_check(10000));
678 YAZ_CHECK(utf8_check(100000));
679 YAZ_CHECK(utf8_check(1000000));
680 YAZ_CHECK(utf8_check(10000000));
681 YAZ_CHECK(utf8_check(100000000));
684 static void tst_danmarc_to_latin1(void)
686 yaz_iconv_t cd = yaz_iconv_open("iso-8859-1", "danmarc");
692 YAZ_CHECK(tst_convert(cd, "ax", "ax"));
694 YAZ_CHECK(tst_convert(cd, "a@@b", "a@b"));
695 YAZ_CHECK(tst_convert(cd, "a@@@@b", "a@@b"));
696 YAZ_CHECK(tst_convert(cd, "@000ab", "\nb"));
698 YAZ_CHECK(tst_convert(cd, "@\xe5", "aa"));
699 YAZ_CHECK(tst_convert(cd, "@\xc5.", "Aa."));
705 int main (int argc, char **argv)
707 YAZ_CHECK_INIT(argc, argv);
713 tst_marc8s_to_utf8();
715 tst_marc8_to_latin1();
717 tst_advance_to_utf8();
718 tst_utf8_to_advance();
720 tst_utf8_to_marc8("marc8");
721 tst_utf8_to_marc8("marc8lossy");
722 tst_utf8_to_marc8("marc8lossless");
724 tst_danmarc_to_latin1();
726 tst_latin1_to_marc8();
728 tst_marc8_to_ucs4b();
731 dconvert(1, "UTF-8");
732 dconvert(1, "ISO-8859-1");
734 dconvert(1, "UCS4LE");
735 dconvert(0, "CP865");
742 * c-file-style: "Stroustrup"
743 * indent-tabs-mode: nil
745 * vim: shiftwidth=4 tabstop=8 expandtab