This source file includes following definitions.
- new_offsets
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "uniconv.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "unistr.h"
27 #include "macros.h"
28 extern int iconv_supports_encoding (const char *encoding);
29
30
31 #define MAGIC 0x1983EFF1
32
33 static size_t *
34 new_offsets (size_t n)
35 {
36 size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
37 offsets[n] = MAGIC;
38 return offsets;
39 }
40
41 int
42 main ()
43 {
44 #if HAVE_ICONV
45 static enum iconv_ilseq_handler handlers[] =
46 { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
47 size_t h;
48 size_t o;
49 size_t i;
50
51
52
53
54
55 for (h = 0; h < SIZEOF (handlers); h++)
56 {
57 enum iconv_ilseq_handler handler = handlers[h];
58 static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
59 static const uint32_t expected[] =
60 {
61 0xC4, 'r', 'g', 'e', 'r', ' ', 'm', 'i', 't', ' ', 'b', 0xF6, 's',
62 'e', 'n', ' ', 'B', 0xFC, 'b', 'c', 'h', 'e', 'n', ' ', 'o', 'h',
63 'n', 'e', ' ', 'A', 'u', 'g', 'e', 'n', 'm', 'a', 0xDF
64 };
65 for (o = 0; o < 2; o++)
66 {
67 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
68 size_t length;
69 uint32_t *result = u32_conv_from_encoding ("ISO-8859-1", handler,
70 input, strlen (input),
71 offsets,
72 NULL, &length);
73 ASSERT (result != NULL);
74 ASSERT (length == SIZEOF (expected));
75 ASSERT (u32_cmp (result, expected, SIZEOF (expected)) == 0);
76 if (o)
77 {
78 for (i = 0; i < 37; i++)
79 ASSERT (offsets[i] == i);
80 ASSERT (offsets[37] == MAGIC);
81 free (offsets);
82 }
83 free (result);
84 }
85 }
86
87
88 for (h = 0; h < SIZEOF (handlers); h++)
89 {
90 enum iconv_ilseq_handler handler = handlers[h];
91 static const char input[] = "Rafa\263 Maszkowski";
92 static const uint32_t expected[] =
93 {
94 'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
95 's', 'k', 'i'
96 };
97 for (o = 0; o < 2; o++)
98 {
99 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
100 size_t length;
101 uint32_t *result = u32_conv_from_encoding ("ISO-8859-2", handler,
102 input, strlen (input),
103 offsets,
104 NULL, &length);
105 ASSERT (result != NULL);
106 ASSERT (length == SIZEOF (expected));
107 ASSERT (u32_cmp (result, expected, SIZEOF (expected)) == 0);
108 if (o)
109 {
110 for (i = 0; i < 16; i++)
111 ASSERT (offsets[i] == i);
112 ASSERT (offsets[16] == MAGIC);
113 free (offsets);
114 }
115 free (result);
116 }
117 }
118
119
120 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
121 if (iconv_supports_encoding ("ISO-2022-JP-2"))
122 {
123
124 for (h = 0; h < SIZEOF (handlers); h++)
125 {
126 enum iconv_ilseq_handler handler = handlers[h];
127 static const char input[] = "\244\263\244\363\244\313\244\301\244\317";
128 static const uint32_t expected[] =
129 {
130 0x3053, 0x3093, 0x306B, 0x3061, 0x306F
131 };
132 for (o = 0; o < 2; o++)
133 {
134 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
135 size_t length;
136 uint32_t *result = u32_conv_from_encoding ("autodetect_jp", handler,
137 input, strlen (input),
138 offsets,
139 NULL, &length);
140 ASSERT (result != NULL);
141 ASSERT (length == SIZEOF (expected));
142 ASSERT (u32_cmp (result, expected, SIZEOF (expected)) == 0);
143 if (o)
144 {
145 for (i = 0; i < 10; i++)
146 ASSERT (offsets[i] == ((i % 2) == 0 ? i / 2 : (size_t)(-1)));
147 ASSERT (offsets[10] == MAGIC);
148 free (offsets);
149 }
150 free (result);
151 }
152 }
153 for (h = 0; h < SIZEOF (handlers); h++)
154 {
155 enum iconv_ilseq_handler handler = handlers[h];
156 static const char input[] = "\202\261\202\361\202\311\202\277\202\315";
157 static const uint32_t expected[] =
158 {
159 0x3053, 0x3093, 0x306B, 0x3061, 0x306F
160 };
161 for (o = 0; o < 2; o++)
162 {
163 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
164 size_t length;
165 uint32_t *result = u32_conv_from_encoding ("autodetect_jp", handler,
166 input, strlen (input),
167 offsets,
168 NULL, &length);
169 ASSERT (result != NULL);
170 ASSERT (length == SIZEOF (expected));
171 ASSERT (u32_cmp (result, expected, SIZEOF (expected)) == 0);
172 if (o)
173 {
174 for (i = 0; i < 10; i++)
175 ASSERT (offsets[i] == ((i % 2) == 0 ? i / 2 : (size_t)(-1)));
176 ASSERT (offsets[10] == MAGIC);
177 free (offsets);
178 }
179 free (result);
180 }
181 }
182 for (h = 0; h < SIZEOF (handlers); h++)
183 {
184 enum iconv_ilseq_handler handler = handlers[h];
185 static const char input[] = "\033$B$3$s$K$A$O\033(B";
186 static const uint32_t expected[] =
187 {
188 0x3053, 0x3093, 0x306B, 0x3061, 0x306F
189 };
190 for (o = 0; o < 2; o++)
191 {
192 size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
193 size_t length;
194 uint32_t *result = u32_conv_from_encoding ("autodetect_jp", handler,
195 input, strlen (input),
196 offsets,
197 NULL, &length);
198 ASSERT (result != NULL);
199 ASSERT (length == SIZEOF (expected));
200 ASSERT (u32_cmp (result, expected, SIZEOF (expected)) == 0);
201 if (o)
202 {
203 for (i = 0; i < 16; i++)
204 ASSERT (offsets[i] == (i == 0 ? 0 :
205 i == 5 ? 1 :
206 i == 7 ? 2 :
207 i == 9 ? 3 :
208 i == 11 ? 4 :
209 i == 13 ? 5 :
210 (size_t)(-1)));
211 ASSERT (offsets[16] == MAGIC);
212 free (offsets);
213 }
214 free (result);
215 }
216 }
217 }
218 # endif
219
220 #endif
221
222 return 0;
223 }