Back to index

glibc  2.9
tst-iconv5.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by GOTO Masanori <gotom@debian.or.jp>, 2004
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <iconv.h>
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <errno.h>
00025 
00026 #define SIZE 256            /* enough room for conversion */
00027 #define SAMPLESTR "abc"
00028 
00029 struct unalign
00030 {
00031   char str1[1];
00032   char str2[SIZE];
00033 };
00034 
00035 struct convcode
00036 {
00037   const char *tocode;
00038   const char *fromcode;
00039 };
00040 
00041 /* test builtin transformation */
00042 struct convcode testcode[] = {
00043   {"ASCII", "ASCII"},
00044   {"UTF-8", "ASCII"},
00045   {"UCS-2BE", "ASCII"},
00046   {"UCS-2LE", "ASCII"},
00047   {"UCS-4BE", "ASCII"},
00048   {"UCS-4LE", "ASCII"},
00049 };
00050 
00051 int number = (int) sizeof (testcode) / sizeof (struct convcode);
00052 
00053 int
00054 convert (const char *tocode, const char *fromcode, char *inbufp,
00055         size_t inbytesleft, char *outbufp, size_t outbytesleft)
00056 {
00057   iconv_t *ic;
00058   size_t outbytes = outbytesleft;
00059   int ret;
00060 
00061   ic = iconv_open (tocode, fromcode);
00062   if (ic == (iconv_t *) - 1)
00063     {
00064       printf ("iconv_open failed: from: %s, to: %s: %s",
00065              fromcode, tocode, strerror (errno));
00066       return -1;
00067     }
00068 
00069   while (inbytesleft > 0)
00070     {
00071       ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
00072       if (ret == -1)
00073        {
00074          printf ("iconv failed: from: %s, to: %s: %s",
00075                 fromcode, tocode, strerror (errno));
00076          return -1;
00077        }
00078     }
00079 
00080   ret = iconv_close (ic);
00081   if (ret == -1)
00082     {
00083       printf ("iconv_close failed: from: %s, to: %s: %s",
00084              fromcode, tocode, strerror (errno));
00085       return -1;
00086     }
00087 
00088   return outbytesleft - outbytes;
00089 }
00090 
00091 
00092 int
00093 test_unalign (struct convcode *codes, char *str, int len)
00094 {
00095   struct unalign *inbufp, *outbufp;
00096   char *inbuf, *outbuf;
00097   size_t inbytesleft, outbytesleft;
00098   int retlen;
00099 
00100   /* allocating unaligned buffer for both inbuf and outbuf */
00101   inbufp = (struct unalign *) malloc (sizeof (struct unalign));
00102   if (!inbufp)
00103     {
00104       printf ("no memory available\n");
00105       exit (1);
00106     }
00107   inbuf = inbufp->str2;
00108 
00109   outbufp = (struct unalign *) malloc (sizeof (struct unalign));
00110   if (!outbufp)
00111     {
00112       printf ("no memory available\n");
00113       exit (1);
00114     }
00115   outbuf = outbufp->str2;
00116 
00117   /* first iconv phase */
00118   memcpy (inbuf, str, len);
00119   inbytesleft = len;
00120   outbytesleft = sizeof (struct unalign);
00121   retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft,
00122                   outbuf, outbytesleft);
00123   if (retlen == -1)  /* failed */
00124     return 1;
00125 
00126   /* second round trip iconv phase */
00127   memcpy (inbuf, outbuf, retlen);
00128   inbytesleft = retlen;
00129   outbytesleft = sizeof (struct unalign);
00130   retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft,
00131                   outbuf, outbytesleft);
00132   if (retlen == -1)  /* failed */
00133     return 1;
00134 
00135   free (inbufp);
00136   free (outbufp);
00137 
00138   return 0;
00139 }
00140 
00141 int
00142 main (int argc, char *argv[])
00143 {
00144   int i;
00145   int ret = 0;
00146 
00147   for (i = 0; i < number; i++)
00148     {
00149       ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR));
00150       if (ret)
00151        break;
00152       printf ("iconv: %s <-> %s: ok\n",
00153              testcode[i].fromcode, testcode[i].tocode);
00154     }
00155   printf ("Succeeded.\n");
00156 
00157   return ret;
00158 }