Blame wcsmbs/mbsnrtowcs.c

Packit Service 82fcde
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <assert.h>
Packit Service 82fcde
#include <dlfcn.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <gconv.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <wchar.h>
Packit Service 82fcde
#include <wcsmbsload.h>
Packit Service 82fcde
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
Packit Service 82fcde
#ifndef EILSEQ
Packit Service 82fcde
# define EILSEQ EINVAL
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* This is the private state used if PS is NULL.  */
Packit Service 82fcde
static mbstate_t state;
Packit Service 82fcde
Packit Service 82fcde
/* This is a non-standard function but it is very useful in the
Packit Service 82fcde
   implementation of stdio because we have to deal with unterminated
Packit Service 82fcde
   buffers.  At most NMC bytes will be converted.  */
Packit Service 82fcde
size_t
Packit Service 82fcde
__mbsnrtowcs (wchar_t *dst, const char **src, size_t nmc, size_t len,
Packit Service 82fcde
	      mbstate_t *ps)
Packit Service 82fcde
{
Packit Service 82fcde
  const unsigned char *srcend;
Packit Service 82fcde
  struct __gconv_step_data data;
Packit Service 82fcde
  size_t result;
Packit Service 82fcde
  int status;
Packit Service 82fcde
  struct __gconv_step *towc;
Packit Service 82fcde
  size_t dummy;
Packit Service 82fcde
  const struct gconv_fcts *fcts;
Packit Service 82fcde
Packit Service 82fcde
  /* Tell where we want the result.  */
Packit Service 82fcde
  data.__invocation_counter = 0;
Packit Service 82fcde
  data.__internal_use = 1;
Packit Service 82fcde
  data.__flags = __GCONV_IS_LAST;
Packit Service 82fcde
  data.__statep = ps ?: &stat;;
Packit Service 82fcde
Packit Service 82fcde
  if (nmc == 0)
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  srcend = (const unsigned char *) *src + __strnlen (*src, nmc - 1) + 1;
Packit Service 82fcde
Packit Service 82fcde
  /* Get the conversion functions.  */
Packit Service 82fcde
  fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
Packit Service 82fcde
Packit Service 82fcde
  /* Get the structure with the function pointers.  */
Packit Service 82fcde
  towc = fcts->towc;
Packit Service 82fcde
  __gconv_fct fct = towc->__fct;
Packit Service 82fcde
#ifdef PTR_DEMANGLE
Packit Service 82fcde
  if (towc->__shlib_handle != NULL)
Packit Service 82fcde
    PTR_DEMANGLE (fct);
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
  /* We have to handle DST == NULL special.  */
Packit Service 82fcde
  if (dst == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      mbstate_t temp_state;
Packit Service 82fcde
      wchar_t buf[64];		/* Just an arbitrary size.  */
Packit Service 82fcde
      const unsigned char *inbuf = (const unsigned char *) *src;
Packit Service 82fcde
Packit Service 82fcde
      temp_state = *data.__statep;
Packit Service 82fcde
      data.__statep = &temp_state;
Packit Service 82fcde
Packit Service 82fcde
      result = 0;
Packit Service 82fcde
      data.__outbufend = (unsigned char *) buf + sizeof (buf);
Packit Service 82fcde
      do
Packit Service 82fcde
	{
Packit Service 82fcde
	  data.__outbuf = (unsigned char *) buf;
Packit Service 82fcde
Packit Service 82fcde
	  status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
Packit Service 82fcde
				      &dummy, 0, 1));
Packit Service 82fcde
Packit Service 82fcde
	  result += (wchar_t *) data.__outbuf - buf;
Packit Service 82fcde
	}
Packit Service 82fcde
      while (status == __GCONV_FULL_OUTPUT);
Packit Service 82fcde
Packit Service 82fcde
      if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
Packit Service 82fcde
	  && ((wchar_t *) data.__outbuf)[-1] == L'\0')
Packit Service 82fcde
	/* Don't count the NUL character in.  */
Packit Service 82fcde
	--result;
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      /* This code is based on the safe assumption that all internal
Packit Service 82fcde
	 multi-byte encodings use the NUL byte only to mark the end
Packit Service 82fcde
	 of the string.  */
Packit Service 82fcde
      data.__outbuf = (unsigned char *) dst;
Packit Service 82fcde
      data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
Packit Service 82fcde
Packit Service 82fcde
      status = DL_CALL_FCT (fct,
Packit Service 82fcde
			    (towc, &data, (const unsigned char **) src, srcend,
Packit Service 82fcde
			     NULL, &dummy, 0, 1));
Packit Service 82fcde
Packit Service 82fcde
      result = (wchar_t *) data.__outbuf - dst;
Packit Service 82fcde
Packit Service 82fcde
      /* We have to determine whether the last character converted
Packit Service 82fcde
	 is the NUL character.  */
Packit Service 82fcde
      if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
Packit Service 82fcde
	  && (assert (result > 0),
Packit Service 82fcde
	      ((wchar_t *) dst)[result - 1] == L'\0'))
Packit Service 82fcde
	{
Packit Service 82fcde
	  assert (__mbsinit (data.__statep));
Packit Service 82fcde
	  *src = NULL;
Packit Service 82fcde
	  --result;
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* There must not be any problems with the conversion but illegal input
Packit Service 82fcde
     characters.  */
Packit Service 82fcde
  assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
Packit Service 82fcde
	  || status == __GCONV_ILLEGAL_INPUT
Packit Service 82fcde
	  || status == __GCONV_INCOMPLETE_INPUT
Packit Service 82fcde
	  || status == __GCONV_FULL_OUTPUT);
Packit Service 82fcde
Packit Service 82fcde
  if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
Packit Service 82fcde
      && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
Packit Service 82fcde
    {
Packit Service 82fcde
      result = (size_t) -1;
Packit Service 82fcde
      __set_errno (EILSEQ);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return result;
Packit Service 82fcde
}
Packit Service 82fcde
weak_alias (__mbsnrtowcs, mbsnrtowcs)