2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.20 2006-05-09 21:37:03 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)
81 return compare_buffers("tsticonv 22", 0,
82 expect_len, expect_buf,
83 outbuf - outbuf0, outbuf0);
86 static int tst_convert(yaz_iconv_t cd, const char *buf, const char *cmpbuf)
89 WRBUF b = wrbuf_alloc();
91 size_t inbytesleft = strlen(buf);
92 const char *inp = buf;
95 size_t outbytesleft = sizeof(outbuf);
97 size_t r = yaz_iconv(cd, (char**) &inp, &inbytesleft,
98 &outp, &outbytesleft);
99 if (r == (size_t) (-1))
101 int e = yaz_iconv_error(cd);
102 if (e != YAZ_ICONV_E2BIG)
105 wrbuf_write(b, outbuf, outp - outbuf);
107 if (wrbuf_len(b) == strlen(cmpbuf)
108 && !memcmp(cmpbuf, wrbuf_buf(b), wrbuf_len(b)))
111 yaz_log(YLOG_LOG, "GOT (%.*s)", wrbuf_len(b), wrbuf_buf(b));
117 /* some test strings in ISO-8859-1 format */
118 static const char *iso_8859_1_a[] = {
127 static void tst_marc8_to_ucs4b()
129 yaz_iconv_t cd = yaz_iconv_open("UCS4", "MARC8");
134 YAZ_CHECK(tst_convert_l(
137 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
139 "\x00\x00\xFF\x1F" "\x00\x00\x00o"));
140 YAZ_CHECK(tst_convert_l(
143 "\033$1" "\x6F\x77\x29" /* AE0E */
144 "\x6F\x52\x7C" /* c0F4 */ "\033(B",
146 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4"));
147 YAZ_CHECK(tst_convert_l(
151 "\x21\x50\x6E" /* UCS 7CFB */
152 "\x21\x51\x31" /* UCS 7D71 */
153 "\x21\x3A\x67" /* UCS 5B89 */
154 "\x21\x33\x22" /* UCS 5168 */
155 "\x21\x33\x53" /* UCS 5206 */
156 "\x21\x44\x2B" /* UCS 6790 */
164 "\x00\x00\x67\x90"));
166 YAZ_CHECK(tst_convert_l(
169 "\xB0\xB2", /* AYN and oSLASH */
171 "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"));
172 YAZ_CHECK(tst_convert_l(
175 "\xF6\x61", /* a underscore */
177 "\x00\x00\x00\x61" "\x00\x00\x03\x32"));
179 YAZ_CHECK(tst_convert_l(
182 "\x61\xC2", /* a, phonorecord mark */
184 "\x00\x00\x00\x61" "\x00\x00\x21\x17"));
187 YAZ_CHECK(tst_convert_l(
190 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
199 "\x00\x00\x00" "n"));
201 YAZ_CHECK(tst_convert_l(
206 "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"));
208 YAZ_CHECK(tst_convert_l(
213 "\x00\x00\x00\x74" "\x00\x00\x03\x61" "\x00\x00\x00\x73"));
215 YAZ_CHECK(tst_convert_l(
220 "\x00\x00\x00\x74" "\x00\x00\x03\x60" "\x00\x00\x00\x73"));
225 static void tst_ucs4b_to_utf8()
227 yaz_iconv_t cd = yaz_iconv_open("UTF8", "UCS4");
231 YAZ_CHECK(tst_convert_l(
234 "\x00\x00\xFF\x1F\x00\x00\x00o",
236 "\xEF\xBC\x9F\x6F"));
238 YAZ_CHECK(tst_convert_l(
241 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
243 "\xEA\xB8\x8E\xEC\x83\xB4"));
247 static void dconvert(int mandatory, const char *tmpcode)
252 for (i = 0; iso_8859_1_a[i]; i++)
255 char *inbuf = (char*) iso_8859_1_a[i];
256 size_t inbytesleft = strlen(inbuf);
259 char *outbuf = outbuf0;
260 size_t outbytesleft = sizeof(outbuf0);
262 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
263 YAZ_CHECK(cd || !mandatory);
266 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
267 YAZ_CHECK(r != (size_t) (-1));
269 if (r == (size_t) (-1))
272 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
273 YAZ_CHECK(cd || !mandatory);
277 inbytesleft = sizeof(outbuf0) - outbytesleft;
280 outbytesleft = sizeof(outbuf1);
281 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
282 YAZ_CHECK(r != (size_t) (-1));
283 if (r != (size_t)(-1))
285 ret = compare_buffers("dconvert", i,
286 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
287 sizeof(outbuf1) - outbytesleft, outbuf1);
294 int utf8_check(unsigned c)
303 size_t inbytesleft = 4;
304 char *outbuf = utf8buf;
305 size_t outbytesleft = sizeof(utf8buf);
307 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "UCS4LE");
310 for (i = 0; i<4; i++)
313 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
316 if (r == (size_t)(-1))
319 cd = yaz_iconv_open("UCS4LE", "UTF-8");
322 inbytesleft = sizeof(utf8buf) - outbytesleft;
328 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
329 if (r == (size_t)(-1))
334 if (memcmp(src, dst, 4))
340 static void tst_marc8_to_utf8()
342 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8");
348 YAZ_CHECK(tst_convert(cd, "Cours de math",
350 /* COMBINING ACUTE ACCENT */
351 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
352 "Cours de mathe\xcc\x81"));
356 static void tst_marc8s_to_utf8()
358 yaz_iconv_t cd = yaz_iconv_open("UTF-8", "MARC8s");
364 YAZ_CHECK(tst_convert(cd, "Cours de math",
366 /* E9: LATIN SMALL LETTER E WITH ACUTE */
367 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
368 "Cours de math\xc3\xa9"));
374 static void tst_marc8_to_latin1()
376 yaz_iconv_t cd = yaz_iconv_open("ISO-8859-1", "MARC8");
382 YAZ_CHECK(tst_convert(cd, "ax", "ax"));
384 /* latin capital letter o with stroke */
385 YAZ_CHECK(tst_convert(cd, "\xa2", "\xd8"));
387 /* with latin small letter ae */
388 YAZ_CHECK(tst_convert(cd, "eneb\xb5r", "eneb\346r"));
390 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2", "\xe5" "\xd8"));
392 YAZ_CHECK(tst_convert(cd, "\xea" "a\xa2" "b", "\xe5" "\xd8" "b"));
394 YAZ_CHECK(tst_convert(cd, "\xea" "a" "\xea" "a", "\xe5" "\xe5"));
396 YAZ_CHECK(tst_convert(cd, "Cours de math",
398 YAZ_CHECK(tst_convert(cd, "Cours de mathâe",
400 YAZ_CHECK(tst_convert(cd, "12345678âe",
402 YAZ_CHECK(tst_convert(cd, "123456789âe",
404 YAZ_CHECK(tst_convert(cd, "1234567890âe",
406 YAZ_CHECK(tst_convert(cd, "12345678901âe",
408 YAZ_CHECK(tst_convert(cd, "Cours de mathâem",
410 YAZ_CHECK(tst_convert(cd, "Cours de mathâematiques",
411 "Cours de mathématiques"));
416 static void tst_utf8_to_marc8()
418 yaz_iconv_t cd = yaz_iconv_open("MARC8", "UTF-8");
424 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
426 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
427 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
429 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
430 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
432 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
433 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
435 /** UPPERCASE SCANDINAVIAN O */
436 YAZ_CHECK(tst_convert(cd, "S\xc3\x98", "S\xa2"));
439 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x8A", "\xEA" "A"));
441 /** A MACRON + UMLAUT, DIAERESIS */
442 YAZ_CHECK(tst_convert(cd, "A" "\xCC\x84" "\xCC\x88",
445 /* Ligature spanning two characters */
446 YAZ_CHECK(tst_convert(cd,
447 "\x74" "\xCD\xA1" "\x73", /* UTF-8 */
448 "\xEB\x74\xEC\x73")); /* MARC-8 */
450 /* Double title spanning two characters */
451 YAZ_CHECK(tst_convert(cd,
452 "\x74" "\xCD\xA0" "\x73", /* UTF-8 */
453 "\xFA\x74\xFB\x73")); /* MARC-8 */
455 /** Ideographic question mark (Unicode FF1F) */
456 YAZ_CHECK(tst_convert(cd,
457 "\xEF\xBC\x9F" "o", /* UTF-8 */
458 "\033(1" "\x21\x2B\x3B" "\033(B" "o" ));
464 static void tst_latin1_to_marc8()
466 yaz_iconv_t cd = yaz_iconv_open("MARC8", "ISO-8859-1");
472 YAZ_CHECK(tst_convert(cd, "Cours ", "Cours "));
474 /** Pure ASCII. 11 characters (sizeof(outbuf)-1) */
475 YAZ_CHECK(tst_convert(cd, "Cours de mat", "Cours de mat"));
477 /** Pure ASCII. 12 characters (sizeof(outbuf)) */
478 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"));
480 /** Pure ASCII. 13 characters (sizeof(outbuf)) */
481 YAZ_CHECK(tst_convert(cd, "Cours de math.", "Cours de math."));
483 /** D8: UPPERCASE SCANDINAVIAN O */
484 YAZ_CHECK(tst_convert(cd, "S\xd8", "S\xa2"));
486 /** E9: LATIN SMALL LETTER E WITH ACUTE */
487 YAZ_CHECK(tst_convert(cd, "Cours de math\xe9", "Cours de mathâe"));
488 YAZ_CHECK(tst_convert(cd, "Cours de math", "Cours de math"
490 YAZ_CHECK(tst_convert(cd, "Cours de mathé", "Cours de mathâe" ));
491 YAZ_CHECK(tst_convert(cd, "12345678é","12345678âe"));
492 YAZ_CHECK(tst_convert(cd, "123456789é", "123456789âe"));
493 YAZ_CHECK(tst_convert(cd, "1234567890é","1234567890âe"));
494 YAZ_CHECK(tst_convert(cd, "12345678901é", "12345678901âe"));
495 YAZ_CHECK(tst_convert(cd, "Cours de mathém", "Cours de mathâem"));
496 YAZ_CHECK(tst_convert(cd, "Cours de mathématiques",
497 "Cours de mathâematiques"));
501 static void tst_utf8_codes()
503 YAZ_CHECK(utf8_check(3));
504 YAZ_CHECK(utf8_check(127));
505 YAZ_CHECK(utf8_check(128));
506 YAZ_CHECK(utf8_check(255));
507 YAZ_CHECK(utf8_check(256));
508 YAZ_CHECK(utf8_check(900));
509 YAZ_CHECK(utf8_check(1000));
510 YAZ_CHECK(utf8_check(10000));
511 YAZ_CHECK(utf8_check(100000));
512 YAZ_CHECK(utf8_check(1000000));
513 YAZ_CHECK(utf8_check(10000000));
514 YAZ_CHECK(utf8_check(100000000));
517 int main (int argc, char **argv)
519 YAZ_CHECK_INIT(argc, argv);
525 tst_marc8s_to_utf8();
527 tst_marc8_to_latin1();
531 tst_latin1_to_marc8();
533 tst_marc8_to_ucs4b();
536 dconvert(1, "UTF-8");
537 dconvert(1, "ISO-8859-1");
539 dconvert(1, "UCS4LE");
540 dconvert(0, "CP865");
547 * indent-tabs-mode: nil
549 * vim: shiftwidth=4 tabstop=8 expandtab