lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Contributed by Jiro SEKIBA <jir@yamato.ibm.com>. */ |
| 2 | #include <errno.h> |
| 3 | #include <iconv.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define UCS_STR "\x4e\x8c" /* EUC-TW 0xa2a2, EUC-JP 0x */ |
| 10 | |
| 11 | static const char *to_code; |
| 12 | |
| 13 | static bool |
| 14 | xiconv (iconv_t cd, int out_size) |
| 15 | { |
| 16 | unsigned char euc[4]; |
| 17 | char *inp = (char *) UCS_STR; |
| 18 | char *outp = (char *) euc; |
| 19 | size_t inbytesleft = strlen (UCS_STR); |
| 20 | size_t outbytesleft = out_size; |
| 21 | size_t ret; |
| 22 | bool fail = false; |
| 23 | |
| 24 | errno = 0; |
| 25 | ret = iconv (cd, &inp, &inbytesleft, &outp, &outbytesleft); |
| 26 | if (errno || ret == (size_t) -1) |
| 27 | { |
| 28 | fail = out_size == 4 || errno != E2BIG; |
| 29 | printf ("expected %d (E2BIG), got %d (%m)\n", E2BIG, errno); |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | printf ("%s: 0x%02x%02x\n", to_code, euc[0], euc[1]); |
| 34 | if (out_size == 1) |
| 35 | fail = true; |
| 36 | } |
| 37 | |
| 38 | return fail; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | static iconv_t |
| 43 | xiconv_open (const char *code) |
| 44 | { |
| 45 | iconv_t cd; |
| 46 | to_code = code; |
| 47 | errno = 0; |
| 48 | if (errno || (cd = iconv_open (to_code, "UCS-2BE")) == (iconv_t) -1) |
| 49 | { |
| 50 | puts ("Can't open converter"); |
| 51 | exit (1); |
| 52 | } |
| 53 | return cd; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | int |
| 58 | main (void) |
| 59 | { |
| 60 | iconv_t cd; |
| 61 | int result = 0; |
| 62 | |
| 63 | cd = xiconv_open ("EUC-TW"); |
| 64 | result |= xiconv (cd, 4) == true; |
| 65 | puts ("---"); |
| 66 | result |= xiconv (cd, 1) == true; |
| 67 | puts ("---"); |
| 68 | iconv_close (cd); |
| 69 | |
| 70 | cd = xiconv_open ("EUC-JP"); |
| 71 | result |= xiconv (cd, 4) == true; |
| 72 | puts ("---"); |
| 73 | result |= xiconv (cd, 1) == true; |
| 74 | puts ("---"); |
| 75 | iconv_close (cd); |
| 76 | |
| 77 | return result; |
| 78 | } |