1 /* Test of character set conversion with error handling and autodetection.
2 Copyright (C) 2007-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
21 #include "striconveha.h"
32 extern int iconv_supports_encoding (const char *encoding
);
34 /* Magic number for detecting bounds violations. */
35 #define MAGIC 0x1983EFF1
38 new_offsets (size_t n
)
40 size_t *offsets
= (size_t *) malloc ((n
+ 1) * sizeof (size_t));
49 static enum iconv_ilseq_handler handlers
[] =
50 { iconveh_error
, iconveh_question_mark
, iconveh_escape_sequence
};
55 /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
56 ISO-8859-2, and UTF-8. */
58 /* ------------------------- Test mem_iconveha() ------------------------- */
60 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
61 for (h
= 0; h
< SIZEOF (handlers
); h
++)
63 enum iconv_ilseq_handler handler
= handlers
[h
];
64 static const char input
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
65 static const char expected
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
66 for (o
= 0; o
< 2; o
++)
68 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
71 int retval
= mem_iconveha (input
, strlen (input
),
72 "ISO-8859-2", "ISO-8859-1",
77 ASSERT (length
== strlen (expected
));
78 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
81 for (i
= 0; i
< 37; i
++)
82 ASSERT (offsets
[i
] == i
);
83 ASSERT (offsets
[37] == MAGIC
);
90 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
91 for (h
= 0; h
< SIZEOF (handlers
); h
++)
93 enum iconv_ilseq_handler handler
= handlers
[h
];
94 static const char input
[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
95 for (o
= 0; o
< 2; o
++)
97 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
100 int retval
= mem_iconveha (input
, strlen (input
),
101 "ISO-8859-2", "ISO-8859-1",
108 ASSERT (retval
== -1 && errno
== EILSEQ
);
109 ASSERT (result
== NULL
);
113 case iconveh_question_mark
:
115 static const char expected
[] = "Rafa? Maszkowski";
116 ASSERT (retval
== 0);
117 ASSERT (length
== strlen (expected
));
118 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
121 for (i
= 0; i
< 16; i
++)
122 ASSERT (offsets
[i
] == i
);
123 ASSERT (offsets
[16] == MAGIC
);
129 case iconveh_escape_sequence
:
131 static const char expected
[] = "Rafa\\u0142 Maszkowski";
132 ASSERT (retval
== 0);
133 ASSERT (length
== strlen (expected
));
134 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
137 for (i
= 0; i
< 16; i
++)
138 ASSERT (offsets
[i
] == (i
< 5 ? i
:
140 ASSERT (offsets
[16] == MAGIC
);
150 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
151 for (h
= 0; h
< SIZEOF (handlers
); h
++)
153 enum iconv_ilseq_handler handler
= handlers
[h
];
154 static const char input
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
155 static const char expected
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
156 for (o
= 0; o
< 2; o
++)
158 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
161 int retval
= mem_iconveha (input
, strlen (input
),
162 "ISO-8859-1", "UTF-8",
166 ASSERT (retval
== 0);
167 ASSERT (length
== strlen (expected
));
168 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
171 for (i
= 0; i
< 37; i
++)
172 ASSERT (offsets
[i
] == (i
< 1 ? i
:
176 ASSERT (offsets
[37] == MAGIC
);
183 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
184 for (h
= 0; h
< SIZEOF (handlers
); h
++)
186 enum iconv_ilseq_handler handler
= handlers
[h
];
187 static const char input
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
188 static const char expected
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
189 for (o
= 0; o
< 2; o
++)
191 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
194 int retval
= mem_iconveha (input
, strlen (input
),
195 "UTF-8", "ISO-8859-1",
199 ASSERT (retval
== 0);
200 ASSERT (length
== strlen (expected
));
201 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
204 for (i
= 0; i
< 41; i
++)
205 ASSERT (offsets
[i
] == (i
< 1 ? i
:
206 i
== 1 ? (size_t)(-1) :
208 i
== 13 ? (size_t)(-1) :
210 i
== 20 ? (size_t)(-1) :
213 ASSERT (offsets
[41] == MAGIC
);
220 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
221 for (h
= 0; h
< SIZEOF (handlers
); h
++)
223 enum iconv_ilseq_handler handler
= handlers
[h
];
224 static const char input
[] = "Rafa\305\202 Maszkowski"; /* Rafał Maszkowski */
225 for (o
= 0; o
< 2; o
++)
227 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
230 int retval
= mem_iconveha (input
, strlen (input
),
231 "UTF-8", "ISO-8859-1",
238 ASSERT (retval
== -1 && errno
== EILSEQ
);
239 ASSERT (result
== NULL
);
243 case iconveh_question_mark
:
245 static const char expected
[] = "Rafa? Maszkowski";
246 ASSERT (retval
== 0);
247 ASSERT (length
== strlen (expected
));
248 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
251 for (i
= 0; i
< 17; i
++)
252 ASSERT (offsets
[i
] == (i
< 5 ? i
:
253 i
== 5 ? (size_t)(-1) :
255 ASSERT (offsets
[17] == MAGIC
);
261 case iconveh_escape_sequence
:
263 static const char expected
[] = "Rafa\\u0142 Maszkowski";
264 ASSERT (retval
== 0);
265 ASSERT (length
== strlen (expected
));
266 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
269 for (i
= 0; i
< 17; i
++)
270 ASSERT (offsets
[i
] == (i
< 5 ? i
:
271 i
== 5 ? (size_t)(-1) :
273 ASSERT (offsets
[17] == MAGIC
);
283 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
284 for (h
= 0; h
< SIZEOF (handlers
); h
++)
286 enum iconv_ilseq_handler handler
= handlers
[h
];
287 static const char input
[] = "\342";
288 for (o
= 0; o
< 2; o
++)
290 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
293 int retval
= mem_iconveha (input
, strlen (input
),
294 "UTF-8", "ISO-8859-1",
298 ASSERT (retval
== 0);
299 ASSERT (length
== 0);
302 ASSERT (offsets
[0] == 0);
303 ASSERT (offsets
[1] == MAGIC
);
310 /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2. */
311 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
312 if (iconv_supports_encoding ("ISO-2022-JP-2"))
314 /* Test conversions from autodetect_jp to UTF-8. */
315 for (h
= 0; h
< SIZEOF (handlers
); h
++)
317 enum iconv_ilseq_handler handler
= handlers
[h
];
318 static const char input
[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
319 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
320 for (o
= 0; o
< 2; o
++)
322 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
325 int retval
= mem_iconveha (input
, strlen (input
),
326 "autodetect_jp", "UTF-8",
330 ASSERT (retval
== 0);
331 ASSERT (length
== strlen (expected
));
332 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
335 for (i
= 0; i
< 10; i
++)
336 ASSERT (offsets
[i
] == ((i
% 2) == 0 ? (i
/ 2) * 3 : (size_t)(-1)));
337 ASSERT (offsets
[10] == MAGIC
);
343 for (h
= 0; h
< SIZEOF (handlers
); h
++)
345 enum iconv_ilseq_handler handler
= handlers
[h
];
346 static const char input
[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
347 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
348 for (o
= 0; o
< 2; o
++)
350 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
353 int retval
= mem_iconveha (input
, strlen (input
),
354 "autodetect_jp", "UTF-8",
358 ASSERT (retval
== 0);
359 ASSERT (length
== strlen (expected
));
360 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
363 for (i
= 0; i
< 10; i
++)
364 ASSERT (offsets
[i
] == ((i
% 2) == 0 ? (i
/ 2) * 3 : (size_t)(-1)));
365 ASSERT (offsets
[10] == MAGIC
);
371 for (h
= 0; h
< SIZEOF (handlers
); h
++)
373 enum iconv_ilseq_handler handler
= handlers
[h
];
374 static const char input
[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
375 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
376 for (o
= 0; o
< 2; o
++)
378 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
381 int retval
= mem_iconveha (input
, strlen (input
),
382 "autodetect_jp", "UTF-8",
386 ASSERT (retval
== 0);
387 ASSERT (length
== strlen (expected
));
388 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
391 for (i
= 0; i
< 16; i
++)
392 ASSERT (offsets
[i
] == (i
== 0 ? 0 :
399 ASSERT (offsets
[16] == MAGIC
);
408 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) && !defined __UCLIBC__) || _LIBICONV_VERSION >= 0x0105
409 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
410 for (h
= 0; h
< SIZEOF (handlers
); h
++)
412 enum iconv_ilseq_handler handler
= handlers
[h
];
413 static const char input
[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
414 static const char expected
[] = "Costs: 27 EUR";
415 for (o
= 0; o
< 2; o
++)
417 size_t *offsets
= (o
? new_offsets (strlen (input
)) : NULL
);
420 int retval
= mem_iconveha (input
, strlen (input
),
421 "UTF-8", "ISO-8859-1",
425 ASSERT (retval
== 0);
426 ASSERT (length
== strlen (expected
));
427 ASSERT (result
!= NULL
&& memcmp (result
, expected
, strlen (expected
)) == 0);
430 for (i
= 0; i
< 13; i
++)
431 ASSERT (offsets
[i
] == (i
< 11 ? i
: (size_t)(-1)));
432 ASSERT (offsets
[13] == MAGIC
);
440 /* ------------------------- Test str_iconveha() ------------------------- */
442 /* Test conversion from ISO-8859-2 to ISO-8859-1 with no errors. */
443 for (h
= 0; h
< SIZEOF (handlers
); h
++)
445 enum iconv_ilseq_handler handler
= handlers
[h
];
446 static const char input
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
447 static const char expected
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
448 char *result
= str_iconveha (input
, "ISO-8859-2", "ISO-8859-1", false, handler
);
449 ASSERT (result
!= NULL
);
450 ASSERT (strcmp (result
, expected
) == 0);
454 /* Test conversion from ISO-8859-2 to ISO-8859-1 with EILSEQ. */
455 for (h
= 0; h
< SIZEOF (handlers
); h
++)
457 enum iconv_ilseq_handler handler
= handlers
[h
];
458 static const char input
[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
459 char *result
= str_iconveha (input
, "ISO-8859-2", "ISO-8859-1", false, handler
);
463 ASSERT (result
== NULL
&& errno
== EILSEQ
);
465 case iconveh_question_mark
:
467 static const char expected
[] = "Rafa? Maszkowski";
468 ASSERT (result
!= NULL
);
469 ASSERT (strcmp (result
, expected
) == 0);
473 case iconveh_escape_sequence
:
475 static const char expected
[] = "Rafa\\u0142 Maszkowski";
476 ASSERT (result
!= NULL
);
477 ASSERT (strcmp (result
, expected
) == 0);
484 /* Test conversion from ISO-8859-1 to UTF-8 with no errors. */
485 for (h
= 0; h
< SIZEOF (handlers
); h
++)
487 enum iconv_ilseq_handler handler
= handlers
[h
];
488 static const char input
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
489 static const char expected
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
490 char *result
= str_iconveha (input
, "ISO-8859-1", "UTF-8", false, handler
);
491 ASSERT (result
!= NULL
);
492 ASSERT (strcmp (result
, expected
) == 0);
496 /* Test conversion from UTF-8 to ISO-8859-1 with no errors. */
497 for (h
= 0; h
< SIZEOF (handlers
); h
++)
499 enum iconv_ilseq_handler handler
= handlers
[h
];
500 static const char input
[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
501 static const char expected
[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
502 char *result
= str_iconveha (input
, "UTF-8", "ISO-8859-1", false, handler
);
503 ASSERT (result
!= NULL
);
504 ASSERT (strcmp (result
, expected
) == 0);
508 /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ. */
509 for (h
= 0; h
< SIZEOF (handlers
); h
++)
511 enum iconv_ilseq_handler handler
= handlers
[h
];
512 static const char input
[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
513 char *result
= str_iconveha (input
, "UTF-8", "ISO-8859-1", false, handler
);
517 ASSERT (result
== NULL
&& errno
== EILSEQ
);
519 case iconveh_question_mark
:
521 static const char expected
[] = "Costs: 27 ?";
522 ASSERT (result
!= NULL
);
523 ASSERT (strcmp (result
, expected
) == 0);
527 case iconveh_escape_sequence
:
529 static const char expected
[] = "Costs: 27 \\u20AC";
530 ASSERT (result
!= NULL
);
531 ASSERT (strcmp (result
, expected
) == 0);
538 /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL. */
539 for (h
= 0; h
< SIZEOF (handlers
); h
++)
541 enum iconv_ilseq_handler handler
= handlers
[h
];
542 static const char input
[] = "\342";
543 char *result
= str_iconveha (input
, "UTF-8", "ISO-8859-1", false, handler
);
544 ASSERT (result
!= NULL
);
545 ASSERT (strcmp (result
, "") == 0);
549 /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2. */
550 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
551 if (iconv_supports_encoding ("ISO-2022-JP-2"))
553 /* Test conversions from autodetect_jp to UTF-8. */
554 for (h
= 0; h
< SIZEOF (handlers
); h
++)
556 enum iconv_ilseq_handler handler
= handlers
[h
];
557 static const char input
[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
558 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
559 char *result
= str_iconveha (input
, "autodetect_jp", "UTF-8", false, handler
);
560 ASSERT (result
!= NULL
);
561 ASSERT (strcmp (result
, expected
) == 0);
564 for (h
= 0; h
< SIZEOF (handlers
); h
++)
566 enum iconv_ilseq_handler handler
= handlers
[h
];
567 static const char input
[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
568 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
569 char *result
= str_iconveha (input
, "autodetect_jp", "UTF-8", false, handler
);
570 ASSERT (result
!= NULL
);
571 ASSERT (strcmp (result
, expected
) == 0);
574 for (h
= 0; h
< SIZEOF (handlers
); h
++)
576 enum iconv_ilseq_handler handler
= handlers
[h
];
577 static const char input
[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
578 static const char expected
[] = "\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257"; /* こんにちは */
579 char *result
= str_iconveha (input
, "autodetect_jp", "UTF-8", false, handler
);
580 ASSERT (result
!= NULL
);
581 ASSERT (strcmp (result
, expected
) == 0);
587 # if (((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2) && !defined __UCLIBC__) || _LIBICONV_VERSION >= 0x0105
588 /* Test conversion from UTF-8 to ISO-8859-1 with transliteration. */
589 for (h
= 0; h
< SIZEOF (handlers
); h
++)
591 enum iconv_ilseq_handler handler
= handlers
[h
];
592 static const char input
[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
593 static const char expected
[] = "Costs: 27 EUR";
594 char *result
= str_iconveha (input
, "UTF-8", "ISO-8859-1", true, handler
);
595 ASSERT (result
!= NULL
);
596 ASSERT (strcmp (result
, expected
) == 0);