Back to index

glibc  2.9
bug-iconv4.c
Go to the documentation of this file.
00001 /* Contributed by Jiro SEKIBA <jir@yamato.ibm.com>.  */
00002 #include <errno.h>
00003 #include <iconv.h>
00004 #include <stdbool.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 
00009 #define UCS_STR "\x4e\x8c" /* EUC-TW 0xa2a2, EUC-JP 0x */
00010 
00011 static const char *to_code;
00012 
00013 static bool
00014 xiconv (iconv_t cd, int out_size)
00015 {
00016   unsigned char euc[4];
00017   char *inp = (char *) UCS_STR;
00018   char *outp = (char *) euc;
00019   size_t inbytesleft = strlen (UCS_STR);
00020   size_t outbytesleft = out_size;
00021   size_t ret;
00022   bool fail = false;
00023 
00024   errno = 0;
00025   ret = iconv (cd, &inp, &inbytesleft, &outp, &outbytesleft);
00026   if (errno || ret == (size_t) -1)
00027     {
00028       fail = out_size == 4 || errno != E2BIG;
00029       printf ("expected %d (E2BIG), got %d (%m)\n", E2BIG, errno);
00030     }
00031   else
00032     {
00033       printf ("%s: 0x%02x%02x\n", to_code, euc[0], euc[1]);
00034       if (out_size == 1)
00035        fail = true;
00036     }
00037 
00038   return fail;
00039 }
00040 
00041 
00042 static iconv_t
00043 xiconv_open (const char *code)
00044 {
00045   iconv_t cd;
00046   to_code = code;
00047   errno = 0;
00048   if (errno || (cd = iconv_open (to_code, "UCS-2BE")) == (iconv_t) -1)
00049     {
00050       puts ("Can't open converter");
00051       exit (1);
00052     }
00053   return cd;
00054 }
00055 
00056 
00057 int
00058 main (void)
00059 {
00060   iconv_t cd;
00061   int result = 0;
00062 
00063   cd = xiconv_open ("EUC-TW");
00064   result |= xiconv (cd, 4) == true;
00065   puts ("---");
00066   result |= xiconv (cd, 1) == true;
00067   puts ("---");
00068   iconv_close (cd);
00069 
00070   cd = xiconv_open ("EUC-JP");
00071   result |= xiconv (cd, 4) == true;
00072   puts ("---");
00073   result |= xiconv (cd, 1) == true;
00074   puts ("---");
00075   iconv_close (cd);
00076 
00077   return result;
00078 }