Blame iconvdata/bug-iconv11.c

Packit 6c4009
/* bug 19432: iconv rejects redundant escape sequences in IBM903,
Packit 6c4009
              IBM905, IBM907, and IBM909
Packit 6c4009
Packit 6c4009
   Copyright (C) 2016-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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
Packit 6c4009
// The longest test input sequence.
Packit 6c4009
#define MAXINBYTES    8
Packit 6c4009
#define MAXOUTBYTES   (MAXINBYTES * MB_LEN_MAX)
Packit 6c4009
Packit 6c4009
/* Verify that a conversion of the INPUT sequence consisting of
Packit 6c4009
   INBYTESLEFT bytes in the encoding specified by the codeset
Packit 6c4009
   named by FROM_SET is successful.
Packit 6c4009
   Return 0 on success, non-zero on iconv() failure.  */
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
test_ibm93x (const char *from_set, const char *input, size_t inbytesleft)
Packit 6c4009
{
Packit 6c4009
  const char to_set[] = "UTF-8";
Packit 6c4009
  iconv_t cd = iconv_open (to_set, from_set);
Packit 6c4009
  if (cd == (iconv_t) -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("iconv_open(\"%s\", \"%s\"): %s\n",
Packit 6c4009
              from_set, to_set, strerror (errno));
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  char output [MAXOUTBYTES];
Packit 6c4009
  size_t outbytesleft = sizeof output;
Packit 6c4009
Packit 6c4009
  char *inbuf = (char*)input;
Packit 6c4009
  char *outbuf = output;
Packit 6c4009
Packit 6c4009
  printf ("iconv(cd, %p, %zu, %p, %zu)\n",
Packit 6c4009
          inbuf, inbytesleft, outbuf, outbytesleft);
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
Packit 6c4009
  printf ("  ==> %zu: %s\n"
Packit 6c4009
          "  inbuf%+td, inbytesleft=%zu, outbuf%+td, outbytesleft=%zu\n",
Packit 6c4009
          ret, strerror (errno),
Packit 6c4009
          inbuf - input, inbytesleft, outbuf - output, outbytesleft);
Packit 6c4009
Packit 6c4009
  // Return 0 on success, non-zero on iconv() failure.
Packit 6c4009
  return ret == (size_t)-1 || errno;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  // State-dependent encodings to exercise.
Packit 6c4009
  static const char* const to_code[] = {
Packit 6c4009
    "IBM930", "IBM933", "IBM935", "IBM937", "IBM939"
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
  static const size_t ncodesets = sizeof to_code / sizeof *to_code;
Packit 6c4009
Packit 6c4009
  static const struct {
Packit 6c4009
    char txt[MAXINBYTES];
Packit 6c4009
    size_t len;
Packit 6c4009
  } input[] = {
Packit 6c4009
#define DATA(s) { s, sizeof s - 1 }
Packit 6c4009
    /* <SI>: denotes the shift-in 1-byte escape sequence, changing
Packit 6c4009
             the encoder from a sigle-byte encoding to multibyte
Packit 6c4009
       <SO>: denotes the shift-out 1-byte escape sequence, switching
Packit 6c4009
             the encoder from a multibyte to a single-byte state  */
Packit 6c4009
Packit 6c4009
    DATA ("\x0e"),               // <SI> (not redundant)
Packit 6c4009
    DATA ("\x0f"),               // <S0> (redundant with initial state)
Packit 6c4009
    DATA ("\x0e\x0e"),           // <SI><SI>
Packit 6c4009
    DATA ("\x0e\x0f\x0f"),       // <SI><SO><SO>
Packit 6c4009
    DATA ("\x0f\x0f"),           // <SO><SO>
Packit 6c4009
    DATA ("\x0f\x0e\x0e"),       // <SO><SI><SI>
Packit 6c4009
    DATA ("\x0e\x0f\xc7\x0f"),   // <SI><SO><G><SO>
Packit 6c4009
    DATA ("\xc7\x0f")            // <G><SO> (redundant with initial state)
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
  static const size_t ninputs = sizeof input / sizeof *input;
Packit 6c4009
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  size_t i, j;
Packit 6c4009
Packit 6c4009
  /* Iterate over the IBM93x codesets above and exercise each with
Packit 6c4009
     the input sequences above.  */
Packit 6c4009
  for (i = 0; i != ncodesets; ++i)
Packit 6c4009
    for (j = 0; j != ninputs; ++j)
Packit 6c4009
      ret += test_ibm93x (to_code [i], input [i].txt, input [i].len);
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"