Back to index

glibc  2.9
tst-e2big.c
Go to the documentation of this file.
00001 /* Test for a tricky E2BIG situation.
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Bruno Haible <bruno@clisp.org>, 2002.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <alloca.h>
00022 #include <errno.h>
00023 #include <iconv.h>
00024 #include <stdbool.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 
00028 /* In EUC-JISX0213 and TSCII, a single input character can convert to
00029    a sequence of two or more Unicode characters.  When the output buffer
00030    has room for less Unicode characters than would be produced with an
00031    unconstrained output buffer, the conversion must give errno = E2BIG.  */
00032 
00033 void
00034 test (const char *encoding, char *inbuf, size_t inbufsize, size_t outbufsize)
00035 {
00036   char *outbuf = alloca (outbufsize);
00037   iconv_t cd;
00038   char *inptr;
00039   size_t inlen;
00040   char *outptr;
00041   size_t outlen;
00042   int result;
00043   bool empty_input;
00044   bool empty_output;
00045 
00046   cd = iconv_open ("UTF-8", encoding);
00047   if (cd == (iconv_t) -1)
00048     {
00049       fprintf (stderr, "cannot convert from %s\n", encoding);
00050       exit (1);
00051     }
00052 
00053   inptr = inbuf;
00054   inlen = inbufsize;
00055   outptr = outbuf;
00056   outlen = outbufsize;
00057 
00058   result = iconv (cd, &inptr, &inlen, &outptr, &outlen);
00059   if (!(result == -1 && errno == E2BIG))
00060     {
00061       fprintf (stderr, "%s: wrong iconv result: %d/%d (%m)\n",
00062               encoding, result, errno);
00063       exit (1);
00064     }
00065   empty_input = (inptr == inbuf && inlen == inbufsize);
00066   empty_output = (outptr == outbuf && outlen == outbufsize);
00067 
00068   if (!empty_input && empty_output)
00069     {
00070       fprintf (stderr, "%s: ate %td input bytes\n", encoding, inptr - inbuf);
00071       exit (1);
00072     }
00073   if (empty_input && !empty_output)
00074     {
00075       fprintf (stderr, "%s: produced %td output bytes\n",
00076               encoding, outptr - outbuf);
00077       exit (1);
00078     }
00079 
00080   iconv_close (cd);
00081 }
00082 
00083 void
00084 test_euc_jisx0213 (void)
00085 {
00086   char inbuf[2] = { 0xa4, 0xf7 };
00087   test ("EUC-JISX0213", inbuf, sizeof (inbuf), 3);
00088 }
00089 
00090 void
00091 test_tscii (void)
00092 {
00093   char inbuf[1] = { 0x82 };
00094   test ("TSCII", inbuf, sizeof (inbuf), 3);
00095   test ("TSCII", inbuf, sizeof (inbuf), 6);
00096   test ("TSCII", inbuf, sizeof (inbuf), 9);
00097 }
00098 
00099 int
00100 main (void)
00101 {
00102   test_euc_jisx0213 ();
00103   test_tscii ();
00104   return 0;
00105 }