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