Blame wcsmbs/mbsrtowcs_l.c

Packit 6c4009
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gnu.org>, 2002.
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 <assert.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include "wcsmbsload.h"
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <gconv.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <wcsmbsload.h>
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
#ifndef EILSEQ
Packit 6c4009
# define EILSEQ EINVAL
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
size_t
Packit 6c4009
attribute_hidden
Packit 6c4009
__mbsrtowcs_l (wchar_t *dst, const char **src, size_t len, mbstate_t *ps,
Packit 6c4009
	       locale_t l)
Packit 6c4009
{
Packit 6c4009
  struct __gconv_step_data data;
Packit 6c4009
  size_t result;
Packit 6c4009
  int status;
Packit 6c4009
  struct __gconv_step *towc;
Packit 6c4009
  size_t non_reversible;
Packit 6c4009
  const struct gconv_fcts *fcts;
Packit 6c4009
Packit 6c4009
  /* Tell where we want the result.  */
Packit 6c4009
  data.__invocation_counter = 0;
Packit 6c4009
  data.__internal_use = 1;
Packit 6c4009
  data.__flags = __GCONV_IS_LAST;
Packit 6c4009
  data.__statep = ps;
Packit 6c4009
Packit 6c4009
  /* Get the conversion functions.  */
Packit 6c4009
  fcts = get_gconv_fcts (l->__locales[LC_CTYPE]);
Packit 6c4009
Packit 6c4009
  /* Get the structure with the function pointers.  */
Packit 6c4009
  towc = fcts->towc;
Packit 6c4009
  __gconv_fct fct = towc->__fct;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  if (towc->__shlib_handle != NULL)
Packit 6c4009
    PTR_DEMANGLE (fct);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* We have to handle DST == NULL special.  */
Packit 6c4009
  if (dst == NULL)
Packit 6c4009
    {
Packit 6c4009
      mbstate_t temp_state;
Packit 6c4009
      wchar_t buf[64];		/* Just an arbitrary size.  */
Packit 6c4009
      const unsigned char *inbuf = (const unsigned char *) *src;
Packit 6c4009
      const unsigned char *srcend = inbuf + strlen (*src) + 1;
Packit 6c4009
Packit 6c4009
      temp_state = *data.__statep;
Packit 6c4009
      data.__statep = &temp_state;
Packit 6c4009
Packit 6c4009
      result = 0;
Packit 6c4009
      data.__outbufend = (unsigned char *) buf + sizeof (buf);
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  data.__outbuf = (unsigned char *) buf;
Packit 6c4009
Packit 6c4009
	  status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
Packit 6c4009
				      &non_reversible, 0, 1));
Packit 6c4009
Packit 6c4009
	  result += (wchar_t *) data.__outbuf - buf;
Packit 6c4009
	}
Packit 6c4009
      while (status == __GCONV_FULL_OUTPUT);
Packit 6c4009
Packit 6c4009
      if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
Packit 6c4009
	{
Packit 6c4009
	  /* There better should be a NUL wide char at the end.  */
Packit 6c4009
	  assert (((wchar_t *) data.__outbuf)[-1] == L'\0');
Packit 6c4009
	  /* Don't count the NUL character in.  */
Packit 6c4009
	  --result;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* This code is based on the safe assumption that all internal
Packit 6c4009
	 multi-byte encodings use the NUL byte only to mark the end
Packit 6c4009
	 of the string.  */
Packit 6c4009
      const unsigned char *srcp = (const unsigned char *) *src;
Packit 6c4009
      const unsigned char *srcend;
Packit 6c4009
Packit 6c4009
      data.__outbuf = (unsigned char *) dst;
Packit 6c4009
      data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
Packit 6c4009
Packit 6c4009
      status = __GCONV_FULL_OUTPUT;
Packit 6c4009
Packit 6c4009
      while (len > 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Pessimistic guess as to how much input we can use.  In the
Packit 6c4009
	     worst case we need one input byte for one output wchar_t.  */
Packit 6c4009
	  srcend = srcp + __strnlen ((const char *) srcp, len) + 1;
Packit 6c4009
Packit 6c4009
	  status = DL_CALL_FCT (fct, (towc, &data, &srcp, srcend, NULL,
Packit 6c4009
				      &non_reversible, 0, 1));
Packit 6c4009
	  if ((status != __GCONV_EMPTY_INPUT
Packit 6c4009
	       && status != __GCONV_INCOMPLETE_INPUT)
Packit 6c4009
	      /* Not all input read.  */
Packit 6c4009
	      || srcp != srcend
Packit 6c4009
	      /* Reached the end of the input.  */
Packit 6c4009
	      || srcend[-1] == '\0')
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  len = (wchar_t *) data.__outbufend - (wchar_t *) data.__outbuf;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Make the end if the input known to the caller.  */
Packit 6c4009
      *src = (const char *) srcp;
Packit 6c4009
Packit 6c4009
      result = (wchar_t *) data.__outbuf - dst;
Packit 6c4009
Packit 6c4009
      /* We have to determine whether the last character converted
Packit 6c4009
	 is the NUL character.  */
Packit 6c4009
      if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
Packit 6c4009
	  && ((wchar_t *) dst)[result - 1] == L'\0')
Packit 6c4009
	{
Packit 6c4009
	  assert (result > 0);
Packit 6c4009
	  assert (__mbsinit (data.__statep));
Packit 6c4009
	  *src = NULL;
Packit 6c4009
	  --result;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* There must not be any problems with the conversion but illegal input
Packit 6c4009
     characters.  */
Packit 6c4009
  assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
Packit 6c4009
	  || status == __GCONV_ILLEGAL_INPUT
Packit 6c4009
	  || status == __GCONV_INCOMPLETE_INPUT
Packit 6c4009
	  || status == __GCONV_FULL_OUTPUT);
Packit 6c4009
Packit 6c4009
  if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
Packit 6c4009
      && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
Packit 6c4009
    {
Packit 6c4009
      result = (size_t) -1;
Packit 6c4009
      __set_errno (EILSEQ);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}