1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #include <iconv.h>
21: #include <string.h>
22: #include <stdlib.h>
23: #include <stdio.h>
24: #include <errno.h>
25:
26: #define SIZE 256
27: #define SAMPLESTR "abc"
28:
29: struct unalign
30: {
31: char str1[1];
32: char str2[SIZE];
33: };
34:
35: struct convcode
36: {
37: const char *tocode;
38: const char *fromcode;
39: };
40:
41:
42: struct convcode testcode[] = {
43: {"ASCII", "ASCII"},
44: {"UTF-8", "ASCII"},
45: {"UCS-2BE", "ASCII"},
46: {"UCS-2LE", "ASCII"},
47: {"UCS-4BE", "ASCII"},
48: {"UCS-4LE", "ASCII"},
49: };
50:
51: int number = (int) sizeof (testcode) / sizeof (struct convcode);
52:
53: int
54: convert (const char *tocode, const char *fromcode, char *inbufp,
55: size_t inbytesleft, char *outbufp, size_t outbytesleft)
56: {
57: iconv_t *ic;
58: size_t outbytes = outbytesleft;
59: int ret;
60:
61: ic = iconv_open (tocode, fromcode);
62: if (ic == (iconv_t *) - 1)
63: {
64: printf ("iconv_open failed: from: %s, to: %s: %s",
65: fromcode, tocode, strerror (errno));
66: return -1;
67: }
68:
69: while (inbytesleft > 0)
70: {
71: ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
72: if (ret == -1)
73: {
74: printf ("iconv failed: from: %s, to: %s: %s",
75: fromcode, tocode, strerror (errno));
76: return -1;
77: }
78: }
79:
80: ret = iconv_close (ic);
81: if (ret == -1)
82: {
83: printf ("iconv_close failed: from: %s, to: %s: %s",
84: fromcode, tocode, strerror (errno));
85: return -1;
86: }
87:
88: return outbytesleft - outbytes;
89: }
90:
91:
92: int
93: test_unalign (struct convcode *codes, char *str, int len)
94: {
95: struct unalign *inbufp, *outbufp;
96: char *inbuf, *outbuf;
97: size_t inbytesleft, outbytesleft;
98: int retlen;
99:
100:
101: inbufp = (struct unalign *) malloc (sizeof (struct unalign));
102: if (!inbufp)
103: {
104: printf ("no memory available\n");
105: exit (1);
106: }
107: inbuf = inbufp->str2;
108:
109: outbufp = (struct unalign *) malloc (sizeof (struct unalign));
110: if (!outbufp)
111: {
112: printf ("no memory available\n");
113: exit (1);
114: }
115: outbuf = outbufp->str2;
116:
117:
118: memcpy (inbuf, str, len);
119: inbytesleft = len;
120: outbytesleft = sizeof (struct unalign);
121: retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft,
122: outbuf, outbytesleft);
123: if (retlen == -1)
124: return 1;
125:
126:
127: memcpy (inbuf, outbuf, retlen);
128: inbytesleft = retlen;
129: outbytesleft = sizeof (struct unalign);
130: retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft,
131: outbuf, outbytesleft);
132: if (retlen == -1)
133: return 1;
134:
135: free (inbufp);
136: free (outbufp);
137:
138: return 0;
139: }
140:
141: int
142: main (int argc, char *argv[])
143: {
144: int i;
145: int ret = 0;
146:
147: for (i = 0; i < number; i++)
148: {
149: ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR));
150: if (ret)
151: break;
152: printf ("iconv: %s <-> %s: ok\n",
153: testcode[i].fromcode, testcode[i].tocode);
154: }
155: printf ("Succeeded.\n");
156:
157: return ret;
158: }