lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | // BZ 12814 |
| 2 | #include <errno.h> |
| 3 | #include <iconv.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | static int |
| 8 | do_test (void) |
| 9 | { |
| 10 | iconv_t h = iconv_open ("ISO-2022-JP-2", "UTF-8"); |
| 11 | if (h == (iconv_t) -1) |
| 12 | { |
| 13 | printf ("cannot load iconv module: %m\n"); |
| 14 | return 1; |
| 15 | } |
| 16 | |
| 17 | // Euro sign |
| 18 | static const char inbuf[] = "\xe2\x82\xac"; |
| 19 | char *in = (char *) inbuf; |
| 20 | size_t inlen = sizeof (inbuf) - 1; |
| 21 | |
| 22 | char outbuf[100]; |
| 23 | char *out = outbuf; |
| 24 | size_t outlen = sizeof (outbuf); |
| 25 | |
| 26 | int res = 0; |
| 27 | size_t n = iconv (h, &in, &inlen, &out, &outlen); |
| 28 | if (n == (size_t) -1) |
| 29 | { |
| 30 | printf ("iconv failed with %d: %m\n", errno); |
| 31 | return 1; |
| 32 | } |
| 33 | if (n != 0) |
| 34 | { |
| 35 | printf ("iconv returned %zu, expected zero\n", n); |
| 36 | res = 1; |
| 37 | } |
| 38 | if (in != inbuf + sizeof (inbuf) - 1) |
| 39 | { |
| 40 | printf ("in advanced by %td, expected %zu\n", |
| 41 | in - inbuf, sizeof (inbuf) - 1); |
| 42 | res = 1; |
| 43 | } |
| 44 | static const char expected[] = "\x1b\x2e\x46\x1b\x4e\x24"; |
| 45 | if (out - outbuf != sizeof (expected) - 1 |
| 46 | || memcmp (outbuf, expected, sizeof (expected) - 1) != 0) |
| 47 | { |
| 48 | fputs ("generated sequence is: \"", stdout); |
| 49 | for (size_t i = 0; i < out - outbuf; ++i) |
| 50 | printf ("\\x%02hhx", outbuf[i]); |
| 51 | fputs ("\", expected \"", stdout); |
| 52 | for (size_t i = 0; i < sizeof (expected) - 1; ++i) |
| 53 | printf ("\\x%02hhx", expected[i]); |
| 54 | puts ("\""); |
| 55 | res = 1; |
| 56 | } |
| 57 | |
| 58 | if (iconv_close (h) != 0) |
| 59 | { |
| 60 | puts ("failed closing iconv module"); |
| 61 | res = 1; |
| 62 | } |
| 63 | |
| 64 | return res; |
| 65 | } |
| 66 | |
| 67 | #define TEST_FUNCTION do_test () |
| 68 | #include "../test-skeleton.c" |