Blame wcsmbs/wcrtomb.c

Packit 6c4009
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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 <dlfcn.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <gconv.h>
Packit 6c4009
#include <stdlib.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
/* This is the private state used if PS is NULL.  */
Packit 6c4009
static mbstate_t state;
Packit 6c4009
Packit 6c4009
size_t
Packit 6c4009
__wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
Packit 6c4009
{
Packit 6c4009
  char buf[MB_LEN_MAX];
Packit 6c4009
  struct __gconv_step_data data;
Packit 6c4009
  int status;
Packit 6c4009
  size_t result;
Packit 6c4009
  size_t dummy;
Packit 6c4009
  const struct gconv_fcts *fcts;
Packit 6c4009
Packit 6c4009
  /* Set information for this step.  */
Packit 6c4009
  data.__invocation_counter = 0;
Packit 6c4009
  data.__internal_use = 1;
Packit 6c4009
  data.__flags = __GCONV_IS_LAST;
Packit 6c4009
  data.__statep = ps ?: &stat;;
Packit 6c4009
Packit 6c4009
  /* A first special case is if S is NULL.  This means put PS in the
Packit 6c4009
     initial state.  */
Packit 6c4009
  if (s == NULL)
Packit 6c4009
    {
Packit 6c4009
      s = buf;
Packit 6c4009
      wc = L'\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Tell where we want to have the result.  */
Packit 6c4009
  data.__outbuf = (unsigned char *) s;
Packit 6c4009
  data.__outbufend = (unsigned char *) s + MB_CUR_MAX;
Packit 6c4009
Packit 6c4009
  /* Get the conversion functions.  */
Packit 6c4009
  fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
Packit 6c4009
  __gconv_fct fct = fcts->tomb->__fct;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  if (fcts->tomb->__shlib_handle != NULL)
Packit 6c4009
    PTR_DEMANGLE (fct);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* If WC is the NUL character we write into the output buffer the byte
Packit 6c4009
     sequence necessary for PS to get into the initial state, followed
Packit 6c4009
     by a NUL byte.  */
Packit 6c4009
  if (wc == L'\0')
Packit 6c4009
    {
Packit 6c4009
      status = DL_CALL_FCT (fct, (fcts->tomb, &data, NULL, NULL,
Packit 6c4009
				  NULL, &dummy, 1, 1));
Packit 6c4009
Packit 6c4009
      if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
Packit 6c4009
	*data.__outbuf++ = '\0';
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Do a normal conversion.  */
Packit 6c4009
      const unsigned char *inbuf = (const unsigned char *) &wc;
Packit 6c4009
Packit 6c4009
      status = DL_CALL_FCT (fct,
Packit 6c4009
			    (fcts->tomb, &data, &inbuf,
Packit 6c4009
			     inbuf + sizeof (wchar_t), NULL, &dummy, 0, 1));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* There must not be any problems with the conversion but illegal input
Packit 6c4009
     characters.  The output buffer must be large enough, otherwise the
Packit 6c4009
     definition of MB_CUR_MAX is not correct.  All the other possible
Packit 6c4009
     errors also must not happen.  */
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_EMPTY_INPUT
Packit 6c4009
      || status == __GCONV_FULL_OUTPUT)
Packit 6c4009
    result = data.__outbuf - (unsigned char *) s;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      result = (size_t) -1;
Packit 6c4009
      __set_errno (EILSEQ);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
weak_alias (__wcrtomb, wcrtomb)
Packit 6c4009
libc_hidden_weak (wcrtomb)
Packit 6c4009
Packit 6c4009
/* There should be no difference between the UTF-32 handling required
Packit 6c4009
   by c32rtomb and the wchar_t handling which has long since been
Packit 6c4009
   implemented in wcrtomb.  */
Packit 6c4009
weak_alias (__wcrtomb, c32rtomb)