2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.9 2005-05-08 07:35:23 adam Exp $
17 #include <yaz/yaz-util.h>
19 static int compare_buffers(char *msg, int no,
20 int expect_len, const unsigned char *expect_buf,
21 int got_len, const unsigned char *got_buf)
24 if (expect_len == got_len
25 && !memcmp(expect_buf, got_buf, expect_len))
27 printf("tsticonv test=%s i=%d failed\n", msg, no);
28 printf("off got exp\n");
29 for (i = 0; i<got_len || i<expect_len; i++)
35 sprintf(got_char, "%02X", got_buf[i]);
37 sprintf(got_char, "? ");
40 sprintf(expect_char, "%02X", expect_buf[i]);
42 sprintf(expect_char, "? ");
44 printf("%02d %s %s %c\n",
45 i, got_char, expect_char, got_buf[i] == expect_buf[i] ?
52 /* some test strings in ISO-8859-1 format */
53 static const char *iso_8859_1_a[] = {
62 /* same test strings in MARC-8 format */
63 static const char *marc8_a[] = {
65 "\xa2", /* latin capital letter o with stroke */
66 "eneb\xb5r", /* latin small letter ae */
69 "\xea" "a" "\xea" "a",
73 static void tst_marc8_to_iso_8859_1()
78 cd = yaz_iconv_open("ISO-8859-1", "MARC8");
81 printf("tsticonv 10 yaz_iconv_open failed\n");
84 for (i = 0; iso_8859_1_a[i]; i++)
87 char *inbuf= (char*) marc8_a[i];
88 size_t inbytesleft = strlen(inbuf);
90 char *outbuf = outbuf0;
91 size_t outbytesleft = sizeof(outbuf0);
93 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
94 if (r == (size_t) (-1))
96 int e = yaz_iconv_error(cd);
98 printf ("tsticonv 11 i=%d e=%d\n", i, e);
101 compare_buffers("tsticonv 11", i,
102 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
103 outbuf - outbuf0, outbuf0);
108 static void tst_marc8_to_ucs4b()
111 const unsigned char *marc8_b;
113 const unsigned char *ucs4_b;
116 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
117 8, "\x00\x00\xFF\x1F" "\x00\x00\x00o"
119 "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
120 8, "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
123 "\x21\x50\x6E" /* UCS 7CFB */
124 "\x21\x51\x31" /* UCS 7D71 */
125 "\x21\x3A\x67" /* UCS 5B89 */
126 "\x21\x33\x22" /* UCS 5168 */
127 "\x21\x33\x53" /* UCS 5206 */
128 "\x21\x44\x2B" /* UCS 6790 */
130 24, "\x00\x00\x7C\xFB"
137 "\xB0\xB2", /* AYN and oSLASH */
138 8, "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"
140 "\xF6\x61", /* a underscore */
141 8, "\x00\x00\x00\x61" "\x00\x00\x03\x32"
143 "\x61\xC2", /* a, phonorecord mark */
144 8, "\x00\x00\x00\x61" "\x00\x00\x21\x17"
147 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
160 12, "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"
169 cd = yaz_iconv_open("UCS4", "MARC8");
172 printf ("tsticonv 20 yaz_iconv_open failed\n");
175 for (i = 0; ar[i].len; i++)
178 size_t expect_len = ar[i].len;
179 char *inbuf= (char*) ar[i].marc8_b;
180 size_t inbytesleft = strlen(inbuf);
182 char *outbuf = outbuf0;
186 size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
187 if (outbytesleft > 12)
189 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
190 if (r == (size_t) (-1))
192 int e = yaz_iconv_error(cd);
193 if (e != YAZ_ICONV_E2BIG)
195 printf ("tsticonv 21 i=%d e=%d\n", i, e);
202 compare_buffers("tsticonv 22", i,
203 expect_len, ar[i].ucs4_b,
204 outbuf - outbuf0, outbuf0);
209 static void tst_ucs4b_to_utf8()
211 static const char *ucs4_c[] = {
212 "\x00\x00\xFF\x1F\x00\x00\x00o",
213 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
216 static const char *utf8_c[] = {
218 "\xEA\xB8\x8E\xEC\x83\xB4",
225 cd = yaz_iconv_open("UTF8", "UCS4");
228 printf ("tsticonv 30 yaz_iconv_open failed\n");
231 for (i = 0; ucs4_c[i]; i++)
234 char *inbuf= (char*) ucs4_c[i];
235 size_t inbytesleft = 8;
237 char *outbuf = outbuf0;
238 size_t outbytesleft = sizeof(outbuf0);
240 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
241 if (r == (size_t) (-1))
243 int e = yaz_iconv_error(cd);
245 printf ("tsticonv 31 i=%d e=%d\n", i, e);
248 compare_buffers("tsticonv 32", i,
249 strlen(utf8_c[i]), utf8_c[i],
250 outbuf - outbuf0, outbuf0);
255 static void dconvert(int mandatory, const char *tmpcode)
259 for (i = 0; iso_8859_1_a[i]; i++)
262 char *inbuf = (char*) iso_8859_1_a[i];
263 size_t inbytesleft = strlen(inbuf);
266 char *outbuf = outbuf0;
267 size_t outbytesleft = sizeof(outbuf0);
269 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
274 printf ("tsticonv code=%s i=%d 1\n", tmpcode, i);
277 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
278 if (r == (size_t)(-1))
280 int e = yaz_iconv_error(cd);
282 printf ("tsticonv code=%s i=%d 2 e=%d\n", tmpcode, i, e);
287 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
292 printf ("tsticonv code=%s i=%d 3\n", tmpcode, i);
296 inbytesleft = sizeof(outbuf0) - outbytesleft;
299 outbytesleft = sizeof(outbuf1);
300 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
301 if (r == (size_t)(-1)) {
302 int e = yaz_iconv_error(cd);
304 printf ("tsticonv code=%s i=%d 4 e=%d\n", tmpcode, i, e);
307 compare_buffers("dconvert", i,
308 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
309 sizeof(outbuf1) - outbytesleft, outbuf1);
314 int main (int argc, char **argv)
316 dconvert(1, "UTF-8");
317 dconvert(1, "ISO-8859-1");
319 dconvert(1, "UCS4LE");
320 dconvert(0, "CP865");
321 tst_marc8_to_iso_8859_1();
322 tst_marc8_to_ucs4b();