2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.8 2005-02-02 23:27:05 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[] = {
59 /* same test strings in MARC-8 format */
60 static const char *marc8_a[] = {
62 "\xa2", /* latin capital letter o with stroke */
63 "eneb\xb5r", /* latin small letter ae */
67 static void tst_marc8_to_iso_8859_1()
72 cd = yaz_iconv_open("ISO-8859-1", "MARC8");
75 printf("tsticonv 10 yaz_iconv_open failed\n");
78 for (i = 0; iso_8859_1_a[i]; i++)
81 char *inbuf= (char*) marc8_a[i];
82 size_t inbytesleft = strlen(inbuf);
84 char *outbuf = outbuf0;
85 size_t outbytesleft = sizeof(outbuf0);
87 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
88 if (r == (size_t) (-1))
90 int e = yaz_iconv_error(cd);
92 printf ("tsticonv 11 i=%d e=%d\n", i, e);
95 compare_buffers("tsticonv 11", i,
96 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
97 outbuf - outbuf0, outbuf0);
102 static void tst_marc8_to_ucs4b()
105 const unsigned char *marc8_b;
107 const unsigned char *ucs4_b;
110 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
111 8, "\x00\x00\xFF\x1F" "\x00\x00\x00o"
113 "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
114 8, "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
117 "\x21\x50\x6E" /* UCS 7CFB */
118 "\x21\x51\x31" /* UCS 7D71 */
119 "\x21\x3A\x67" /* UCS 5B89 */
120 "\x21\x33\x22" /* UCS 5168 */
121 "\x21\x33\x53" /* UCS 5206 */
122 "\x21\x44\x2B" /* UCS 6790 */
124 24, "\x00\x00\x7C\xFB"
131 "\xB0\xB2", /* AYN and oSLASH */
132 8, "\x00\x00\x02\xBB" "\x00\x00\x00\xF8"
134 "\xF6\x61", /* a underscore */
135 8, "\x00\x00\x00\x61" "\x00\x00\x03\x32"
137 "\x61\xC2", /* a, phonorecord mark */
138 8, "\x00\x00\x00\x61" "\x00\x00\x21\x17"
141 "el" "\xe8" "am\xe8" "an", /* elaman where a is a" */
154 12, "\x00\x00\x00\x41" "\x00\x00\x03\x04" "\x00\x00\x03\x08"
163 cd = yaz_iconv_open("UCS4", "MARC8");
166 printf ("tsticonv 20 yaz_iconv_open failed\n");
169 for (i = 0; ar[i].len; i++)
172 size_t expect_len = ar[i].len;
173 char *inbuf= (char*) ar[i].marc8_b;
174 size_t inbytesleft = strlen(inbuf);
176 char *outbuf = outbuf0;
180 size_t outbytesleft = outbuf0 + sizeof(outbuf0) - outbuf;
181 if (outbytesleft > 12)
183 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
184 if (r == (size_t) (-1))
186 int e = yaz_iconv_error(cd);
187 if (e != YAZ_ICONV_E2BIG)
189 printf ("tsticonv 21 i=%d e=%d\n", i, e);
196 compare_buffers("tsticonv 22", i,
197 expect_len, ar[i].ucs4_b,
198 outbuf - outbuf0, outbuf0);
203 static void tst_ucs4b_to_utf8()
205 static const char *ucs4_c[] = {
206 "\x00\x00\xFF\x1F\x00\x00\x00o",
207 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
210 static const char *utf8_c[] = {
212 "\xEA\xB8\x8E\xEC\x83\xB4",
219 cd = yaz_iconv_open("UTF8", "UCS4");
222 printf ("tsticonv 30 yaz_iconv_open failed\n");
225 for (i = 0; ucs4_c[i]; i++)
228 char *inbuf= (char*) ucs4_c[i];
229 size_t inbytesleft = 8;
231 char *outbuf = outbuf0;
232 size_t outbytesleft = sizeof(outbuf0);
234 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
235 if (r == (size_t) (-1))
237 int e = yaz_iconv_error(cd);
239 printf ("tsticonv 31 i=%d e=%d\n", i, e);
242 compare_buffers("tsticonv 32", i,
243 strlen(utf8_c[i]), utf8_c[i],
244 outbuf - outbuf0, outbuf0);
249 static void dconvert(int mandatory, const char *tmpcode)
253 for (i = 0; iso_8859_1_a[i]; i++)
256 char *inbuf = (char*) iso_8859_1_a[i];
257 size_t inbytesleft = strlen(inbuf);
260 char *outbuf = outbuf0;
261 size_t outbytesleft = sizeof(outbuf0);
263 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
268 printf ("tsticonv code=%s 1\n", tmpcode);
271 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
272 if (r == (size_t)(-1))
274 int e = yaz_iconv_error(cd);
276 printf ("tsticonv code=%s 2 e=%d\n", tmpcode, e);
281 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
286 printf ("tsticonv code=%s 3\n", tmpcode);
290 inbytesleft = sizeof(outbuf0) - outbytesleft;
293 outbytesleft = sizeof(outbuf1);
294 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
295 if (r == (size_t)(-1)) {
296 int e = yaz_iconv_error(cd);
298 printf ("tsticonv code=%s 4 e=%d\n", tmpcode, e);
301 compare_buffers("dconvert", i,
302 strlen(iso_8859_1_a[i]), iso_8859_1_a[i],
303 sizeof(outbuf1) - outbytesleft, outbuf1);
308 int main (int argc, char **argv)
310 dconvert(1, "UTF-8");
311 dconvert(1, "ISO-8859-1");
313 dconvert(1, "UCS4LE");
314 dconvert(0, "CP865");
315 tst_marc8_to_iso_8859_1();
316 tst_marc8_to_ucs4b();