xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2004-2016 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 | <http://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 | return -1; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | ret = iconv_close (ic); |
| 80 | if (ret == -1) |
| 81 | { |
| 82 | printf ("iconv_close failed: from: %s, to: %s: %s", |
| 83 | fromcode, tocode, strerror (errno)); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | return outbytesleft - outbytes; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static int |
| 92 | test_unalign (const struct convcode *codes, const char *str, int len) |
| 93 | { |
| 94 | struct unalign *inbufp, *outbufp; |
| 95 | char *inbuf, *outbuf; |
| 96 | size_t inbytesleft, outbytesleft; |
| 97 | int retlen; |
| 98 | |
| 99 | /* allocating unaligned buffer for both inbuf and outbuf */ |
| 100 | inbufp = (struct unalign *) malloc (sizeof (struct unalign)); |
| 101 | if (!inbufp) |
| 102 | { |
| 103 | printf ("no memory available\n"); |
| 104 | exit (1); |
| 105 | } |
| 106 | inbuf = inbufp->str2; |
| 107 | |
| 108 | outbufp = (struct unalign *) malloc (sizeof (struct unalign)); |
| 109 | if (!outbufp) |
| 110 | { |
| 111 | printf ("no memory available\n"); |
| 112 | exit (1); |
| 113 | } |
| 114 | outbuf = outbufp->str2; |
| 115 | |
| 116 | /* first iconv phase */ |
| 117 | memcpy (inbuf, str, len); |
| 118 | inbytesleft = len; |
| 119 | outbytesleft = sizeof (struct unalign); |
| 120 | retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft, |
| 121 | outbuf, outbytesleft); |
| 122 | if (retlen == -1) /* failed */ |
| 123 | return 1; |
| 124 | |
| 125 | /* second round trip iconv phase */ |
| 126 | memcpy (inbuf, outbuf, retlen); |
| 127 | inbytesleft = retlen; |
| 128 | outbytesleft = sizeof (struct unalign); |
| 129 | retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft, |
| 130 | outbuf, outbytesleft); |
| 131 | if (retlen == -1) /* failed */ |
| 132 | return 1; |
| 133 | |
| 134 | free (inbufp); |
| 135 | free (outbufp); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | do_test (void) |
| 142 | { |
| 143 | int i; |
| 144 | int ret = 0; |
| 145 | |
| 146 | for (i = 0; i < number; i++) |
| 147 | { |
| 148 | ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR)); |
| 149 | if (ret) |
| 150 | break; |
| 151 | printf ("iconv: %s <-> %s: ok\n", |
| 152 | testcode[i].fromcode, testcode[i].tocode); |
| 153 | } |
| 154 | if (ret == 0) |
| 155 | printf ("Succeeded.\n"); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | #define TEST_FUNCTION do_test () |
| 161 | #include "../test-skeleton.c" |