2024-01-01 18:12:26 +00:00
|
|
|
/* Copyright (C) 2004-2024 Free Software Foundation, Inc.
|
2004-06-30 07:43:11 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2004-06-30 07:43:11 +00:00
|
|
|
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define SIZE 256 /* enough room for conversion */
|
|
|
|
#define SAMPLESTR "abc"
|
|
|
|
|
|
|
|
struct unalign
|
|
|
|
{
|
|
|
|
char str1[1];
|
|
|
|
char str2[SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct convcode
|
|
|
|
{
|
|
|
|
const char *tocode;
|
|
|
|
const char *fromcode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* test builtin transformation */
|
2015-02-25 23:55:08 +00:00
|
|
|
static const struct convcode testcode[] = {
|
2004-06-30 07:43:11 +00:00
|
|
|
{"ASCII", "ASCII"},
|
|
|
|
{"UTF-8", "ASCII"},
|
|
|
|
{"UCS-2BE", "ASCII"},
|
|
|
|
{"UCS-2LE", "ASCII"},
|
|
|
|
{"UCS-4BE", "ASCII"},
|
|
|
|
{"UCS-4LE", "ASCII"},
|
|
|
|
};
|
|
|
|
|
2015-02-25 23:55:08 +00:00
|
|
|
static const int number = (int) sizeof (testcode) / sizeof (struct convcode);
|
2004-06-30 07:43:11 +00:00
|
|
|
|
2015-02-25 23:55:08 +00:00
|
|
|
static int
|
2004-06-30 07:43:11 +00:00
|
|
|
convert (const char *tocode, const char *fromcode, char *inbufp,
|
|
|
|
size_t inbytesleft, char *outbufp, size_t outbytesleft)
|
|
|
|
{
|
|
|
|
iconv_t *ic;
|
|
|
|
size_t outbytes = outbytesleft;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ic = iconv_open (tocode, fromcode);
|
|
|
|
if (ic == (iconv_t *) - 1)
|
|
|
|
{
|
|
|
|
printf ("iconv_open failed: from: %s, to: %s: %s",
|
|
|
|
fromcode, tocode, strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (inbytesleft > 0)
|
|
|
|
{
|
|
|
|
ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
printf ("iconv failed: from: %s, to: %s: %s",
|
|
|
|
fromcode, tocode, strerror (errno));
|
2020-12-21 04:22:38 +00:00
|
|
|
iconv_close (ic);
|
2004-06-30 07:43:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = iconv_close (ic);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
printf ("iconv_close failed: from: %s, to: %s: %s",
|
|
|
|
fromcode, tocode, strerror (errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return outbytesleft - outbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-25 23:55:08 +00:00
|
|
|
static int
|
|
|
|
test_unalign (const struct convcode *codes, const char *str, int len)
|
2004-06-30 07:43:11 +00:00
|
|
|
{
|
|
|
|
struct unalign *inbufp, *outbufp;
|
|
|
|
char *inbuf, *outbuf;
|
|
|
|
size_t inbytesleft, outbytesleft;
|
|
|
|
int retlen;
|
|
|
|
|
|
|
|
/* allocating unaligned buffer for both inbuf and outbuf */
|
|
|
|
inbufp = (struct unalign *) malloc (sizeof (struct unalign));
|
|
|
|
if (!inbufp)
|
|
|
|
{
|
|
|
|
printf ("no memory available\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
inbuf = inbufp->str2;
|
|
|
|
|
|
|
|
outbufp = (struct unalign *) malloc (sizeof (struct unalign));
|
|
|
|
if (!outbufp)
|
|
|
|
{
|
|
|
|
printf ("no memory available\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
outbuf = outbufp->str2;
|
|
|
|
|
|
|
|
/* first iconv phase */
|
|
|
|
memcpy (inbuf, str, len);
|
|
|
|
inbytesleft = len;
|
|
|
|
outbytesleft = sizeof (struct unalign);
|
|
|
|
retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft,
|
|
|
|
outbuf, outbytesleft);
|
|
|
|
if (retlen == -1) /* failed */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* second round trip iconv phase */
|
|
|
|
memcpy (inbuf, outbuf, retlen);
|
|
|
|
inbytesleft = retlen;
|
|
|
|
outbytesleft = sizeof (struct unalign);
|
|
|
|
retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft,
|
|
|
|
outbuf, outbytesleft);
|
|
|
|
if (retlen == -1) /* failed */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
free (inbufp);
|
|
|
|
free (outbufp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-25 23:55:08 +00:00
|
|
|
static int
|
|
|
|
do_test (void)
|
2004-06-30 07:43:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < number; i++)
|
|
|
|
{
|
|
|
|
ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR));
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
printf ("iconv: %s <-> %s: ok\n",
|
|
|
|
testcode[i].fromcode, testcode[i].tocode);
|
|
|
|
}
|
2015-02-25 23:55:08 +00:00
|
|
|
if (ret == 0)
|
|
|
|
printf ("Succeeded.\n");
|
2004-06-30 07:43:11 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-02-25 23:55:08 +00:00
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|