Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / unicode / unicodetest.c
1 /*
2 ** Copyright 2011 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 **
5 */
6
7 #include "unicode_config.h"
8 #include "unicode.h"
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <errno.h>
13
14 struct collect_buf {
15 char *ptr;
16 size_t cnt;
17 size_t size;
18 };
19
20 static int save_output(const char *p, size_t n, void *ptr)
21 {
22 struct collect_buf *cb=(struct collect_buf *)ptr;
23
24 while (n)
25 {
26 if (cb->cnt < cb->size)
27 cb->ptr[cb->cnt++]=*p;
28 ++p;
29 --n;
30 }
31 return 0;
32 }
33
34 static void test1()
35 {
36 static const char teststr[]= {
37 0x00, 0x00, 0x00, 0x41,
38 0x00, 0x00, 0x04, 0x14,
39 0x00, 0x00, 0x04, 0x30,
40 0x00, 0x00, 0x00, 0x42};
41 char outputbuf[12];
42 struct collect_buf cb;
43 libmail_u_convert_handle_t h;
44 int checkflag;
45
46 cb.ptr=outputbuf;
47 cb.cnt=0;
48 cb.size=sizeof(outputbuf);
49
50 if ((h=libmail_u_convert_init("UCS-4BE", "ISO-8859-1",
51 save_output, &cb)) == NULL)
52 {
53 perror("libmail_u_convert_init");
54 exit(1);
55 }
56
57 libmail_u_convert(h, teststr, sizeof(teststr));
58
59 if (libmail_u_convert_deinit(h, &checkflag))
60 {
61 perror("libmail_u_convert_deinit");
62 exit(1);
63 }
64 if (cb.cnt != 2 || memcmp(cb.ptr, "AB", 2) || !checkflag)
65 {
66 fprintf(stderr, "Unexpected result from convert()\n");
67 exit(1);
68 }
69 }
70
71 static void test2()
72 {
73 unicode_char *ucptr;
74 size_t ucsize;
75 libmail_u_convert_handle_t h=
76 libmail_u_convert_tou_init("utf-8", &ucptr, &ucsize, 1);
77 char *cptr;
78 size_t csize;
79
80 if (h)
81 {
82 size_t i;
83
84 for (i=0; i<1024/32; ++i)
85 libmail_u_convert(h, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
86 32);
87
88 if (libmail_u_convert_deinit(h, NULL) == 0 &&
89 ucsize == 1024+1)
90 {
91 for (i=0; i<1024; i++)
92 if (ucptr[i] != 'A')
93 break;
94
95 if (i == 1024)
96 {
97 h=libmail_u_convert_fromu_init("utf-8",
98 &cptr, &csize,
99 1);
100
101 if (h)
102 {
103 libmail_u_convert_uc(h, ucptr, 1024);
104 if (libmail_u_convert_deinit(h, NULL)
105 == 0 && csize == 1024+1)
106 {
107 for (i=0; i<1024; i++)
108 if (cptr[i] != 'A')
109 break;
110
111 free(ucptr);
112 free(cptr);
113 if (i == 1024)
114 return;
115 }
116 }
117 }
118 }
119 fprintf(stderr, "test2: failed");
120 errno=EINVAL;
121 }
122 perror("test2");
123 exit(1);
124 }
125
126 int main(int argc, char **argv)
127 {
128 const char *chset=unicode_x_imap_modutf7;
129 int argn=1;
130
131 if (argn < argc && strcmp(argv[argn], "--smap") == 0)
132 {
133 chset=unicode_x_imap_modutf7 " ./~:";
134 ++argn;
135 }
136
137 if (argn < argc && strcmp(argv[argn], "--totitle") == 0)
138 {
139 ++argn;
140
141 if (argn < argc)
142 {
143 char *p=libmail_u_convert_tocase(argv[argn],
144 "utf-8",
145 unicode_tc,
146 unicode_lc);
147
148 if (p)
149 {
150 printf("%s\n", p);
151 free(p);
152 exit(0);
153 }
154 }
155 exit(1);
156 }
157
158 if (argn < argc)
159 {
160 int errflag;
161 char *p=libmail_u_convert_tobuf(argv[argn],
162 "utf-8",
163 chset,
164 &errflag);
165 char *q;
166
167 if (!p)
168 {
169 perror("libmail_u_convert");
170 exit(1);
171 }
172
173 if (errflag)
174 {
175 fprintf(stderr, "Conversion error?\n");
176 exit(1);
177 }
178
179 q=libmail_u_convert_tobuf(p, chset, "utf-8", &errflag);
180 if (!q)
181 {
182 perror("libmail_u_convert");
183 exit(1);
184 }
185
186 if (errflag)
187 {
188 fprintf(stderr, "Conversion error?\n");
189 exit(1);
190 }
191 if (strcmp(q, argv[argn]))
192 {
193 fprintf(stderr, "Round trip error\n");
194 exit(1);
195 }
196 printf("%s\n", p);
197 free(p);
198 free(q);
199 }
200 else
201 {
202 test1();
203 test2();
204 }
205 return 0;
206 }