Blame iconvdata/bug-iconv10.c

Packit 6c4009
/* bug 17197: check that iconv doesn't emit invalid extra shift character
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  static const char *charsets[] =
Packit 6c4009
    { "IBM930", "IBM933", "IBM935", "IBM937", "IBM939" };
Packit 6c4009
  static const char *expects[] =
Packit 6c4009
    { "\016\x44\x4d\017", "\016\x41\x63\017", "\016\x44\x4d\017",
Packit 6c4009
      "\016\x44\x4d\017", "\016\x44\x4d\017" };
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < sizeof (charsets) / sizeof (*charsets); i++)
Packit 6c4009
    {
Packit 6c4009
      const char *charset = charsets[i];
Packit 6c4009
      iconv_t cd = iconv_open (charset, "UTF-8");
Packit 6c4009
      if (cd == (iconv_t) -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("iconv_open failed (%s)\n", charset);
Packit 6c4009
	  ret = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      char input[] = "\xe2\x88\x9e.";
Packit 6c4009
      const char *expect1 = expects[i];
Packit 6c4009
      const char expect2[] = "\x4b";
Packit 6c4009
      size_t input_len = sizeof (input);
Packit 6c4009
      char output[4];
Packit 6c4009
      size_t inlen = input_len;
Packit 6c4009
      size_t outlen = sizeof (output);
Packit 6c4009
      char *inptr = input;
Packit 6c4009
      char *outptr = output;
Packit 6c4009
      /* First round: expect conversion to stop before ".".  */
Packit 6c4009
      size_t r = iconv (cd, &inptr, &inlen, &outptr, &outlen);
Packit 6c4009
      if (r != -1
Packit 6c4009
	  || errno != E2BIG
Packit 6c4009
	  || inlen != 2
Packit 6c4009
	  || inptr != input + input_len - 2
Packit 6c4009
	  || outlen != 0
Packit 6c4009
	  || memcmp (output, expect1, sizeof (output)) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("wrong first conversion (%s)", charset);
Packit 6c4009
	  ret = 1;
Packit 6c4009
	  goto do_close;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      outlen = sizeof (output);
Packit 6c4009
      outptr = output;
Packit 6c4009
      r = iconv (cd, &inptr, &inlen, &outptr, &outlen);
Packit 6c4009
      if (r != 0
Packit 6c4009
	  || inlen != 0
Packit 6c4009
	  || outlen != sizeof (output) - sizeof (expect2)
Packit 6c4009
	  || memcmp (output, expect2, sizeof (expect2)) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("wrong second conversion (%s)\n", charset);
Packit 6c4009
	  ret = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    do_close:
Packit 6c4009
      if (iconv_close (cd) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("iconv_close failed (%s)\n", charset);
Packit 6c4009
	  ret = 1;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"