Back to index

glibc  2.9
bug-iconv1.c
Go to the documentation of this file.
00001 /* Test program by Satoru Takabayashi.  */
00002 #include <errno.h>
00003 #include <iconv.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 int
00009 main (int argc, char **argv)
00010 {
00011   const char in[] = "\x41\x42\x43\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa";
00012                   /* valid eucJP string */
00013   const char exp[] = "\x41\x42\x43\x82\xa0\x82\xa2\x82\xa4";
00014   size_t outbufsize = 10;
00015                   /* 10 is too small to store full result (intentional) */
00016   size_t inleft, outleft;
00017   char *in_p = (char *) in;
00018   char out[outbufsize];
00019   char *out_p = out;
00020   iconv_t cd;
00021   int i;
00022 
00023   inleft = strlen (in);
00024   outleft = outbufsize;
00025 
00026   cd = iconv_open ("SJIS", "eucJP");
00027   if (cd == (iconv_t) -1)
00028     {
00029       puts ("iconv_open failed");
00030       exit (1);
00031     }
00032 
00033   iconv (cd, &in_p, &inleft, &out_p, &outleft); /* this returns E2BIG */
00034   for (i = 0; i < outbufsize - outleft; ++i)
00035     printf (" %02x", (unsigned char) out[i]);
00036   puts ("");
00037   iconv_close (cd);
00038 
00039   return outbufsize - outleft != 9 || memcmp (out, exp, 9) != 0;
00040 }