]> git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/tst-iconv5.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / iconv / tst-iconv5.c
1 /* Copyright (C) 2004-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by GOTO Masanori <gotom@debian.or.jp>, 2004
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <iconv.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24
25 #define SIZE 256 /* enough room for conversion */
26 #define SAMPLESTR "abc"
27
28 struct unalign
29 {
30 char str1[1];
31 char str2[SIZE];
32 };
33
34 struct convcode
35 {
36 const char *tocode;
37 const char *fromcode;
38 };
39
40 /* test builtin transformation */
41 static const struct convcode testcode[] = {
42 {"ASCII", "ASCII"},
43 {"UTF-8", "ASCII"},
44 {"UCS-2BE", "ASCII"},
45 {"UCS-2LE", "ASCII"},
46 {"UCS-4BE", "ASCII"},
47 {"UCS-4LE", "ASCII"},
48 };
49
50 static const int number = (int) sizeof (testcode) / sizeof (struct convcode);
51
52 static int
53 convert (const char *tocode, const char *fromcode, char *inbufp,
54 size_t inbytesleft, char *outbufp, size_t outbytesleft)
55 {
56 iconv_t *ic;
57 size_t outbytes = outbytesleft;
58 int ret;
59
60 ic = iconv_open (tocode, fromcode);
61 if (ic == (iconv_t *) - 1)
62 {
63 printf ("iconv_open failed: from: %s, to: %s: %s",
64 fromcode, tocode, strerror (errno));
65 return -1;
66 }
67
68 while (inbytesleft > 0)
69 {
70 ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
71 if (ret == -1)
72 {
73 printf ("iconv failed: from: %s, to: %s: %s",
74 fromcode, tocode, strerror (errno));
75 iconv_close (ic);
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 static int
93 test_unalign (const struct convcode *codes, const char *str, int len)
94 {
95 struct unalign *inbufp, *outbufp;
96 char *inbuf, *outbuf;
97 size_t inbytesleft, outbytesleft;
98 int retlen;
99
100 /* allocating unaligned buffer for both inbuf and outbuf */
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 /* first iconv phase */
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) /* failed */
124 return 1;
125
126 /* second round trip iconv phase */
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) /* failed */
133 return 1;
134
135 free (inbufp);
136 free (outbufp);
137
138 return 0;
139 }
140
141 static int
142 do_test (void)
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 if (ret == 0)
156 printf ("Succeeded.\n");
157
158 return ret;
159 }
160
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"