Blame wcsmbs/wcsrtombs.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 <stdlib.h>
Packit Service 82fcde
#include <gconv.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
size_t
Packit Service 82fcde
__wcsrtombs (char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
Packit Service 82fcde
{
Packit Service 82fcde
  struct __gconv_step_data data;
Packit Service 82fcde
  int status;
Packit Service 82fcde
  size_t result;
Packit Service 82fcde
  struct __gconv_step *tomb;
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
  /* 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
  tomb = fcts->tomb;
Packit Service 82fcde
  __gconv_fct fct = tomb->__fct;
Packit Service 82fcde
#ifdef PTR_DEMANGLE
Packit Service 82fcde
  if (tomb->__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
      unsigned char buf[256];		/* Just an arbitrary value.  */
Packit Service 82fcde
      const wchar_t *srcend = *src + __wcslen (*src) + 1;
Packit Service 82fcde
      const unsigned char *inbuf = (const unsigned char *) *src;
Packit Service 82fcde
      size_t dummy;
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 = buf + sizeof (buf);
Packit Service 82fcde
Packit Service 82fcde
      do
Packit Service 82fcde
	{
Packit Service 82fcde
	  data.__outbuf = buf;
Packit Service 82fcde
Packit Service 82fcde
	  status = DL_CALL_FCT (fct, (tomb, &data, &inbuf,
Packit Service 82fcde
				      (const unsigned char *) srcend, NULL,
Packit Service 82fcde
				      &dummy, 0, 1));
Packit Service 82fcde
Packit Service 82fcde
	  /* Count the number of bytes.  */
Packit Service 82fcde
	  result += 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
	{
Packit Service 82fcde
	  /* There better should be a NUL byte at the end.  */
Packit Service 82fcde
	  assert (data.__outbuf[-1] == '\0');
Packit Service 82fcde
	  /* Don't count the NUL character in.  */
Packit Service 82fcde
	  --result;
Packit Service 82fcde
	}
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
      const wchar_t *srcend = *src + __wcsnlen (*src, len) + 1;
Packit Service 82fcde
      size_t dummy;
Packit Service 82fcde
Packit Service 82fcde
      data.__outbuf = (unsigned char *) dst;
Packit Service 82fcde
      data.__outbufend = (unsigned char *) dst + len;
Packit Service 82fcde
Packit Service 82fcde
      status = DL_CALL_FCT (fct, (tomb, &data, (const unsigned char **) src,
Packit Service 82fcde
				  (const unsigned char *) srcend, NULL,
Packit Service 82fcde
				  &dummy, 0, 1));
Packit Service 82fcde
Packit Service 82fcde
      /* Count the number of bytes.  */
Packit Service 82fcde
      result = data.__outbuf - (unsigned char *) 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
	  && data.__outbuf[-1] == '\0')
Packit Service 82fcde
	{
Packit Service 82fcde
	  assert (data.__outbuf != (unsigned char *) dst);
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)
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 (__wcsrtombs, wcsrtombs)