Blame wcsmbs/btowc.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 <ctype.h>
Packit Service 82fcde
#include <dlfcn.h>
Packit Service 82fcde
#include <gconv.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <wchar.h>
Packit Service 82fcde
#include <wcsmbsload.h>
Packit Service 82fcde
#include <limits.h>
Packit Service 82fcde
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
wint_t
Packit Service 82fcde
__btowc (int c)
Packit Service 82fcde
{
Packit Service 82fcde
  const struct gconv_fcts *fcts;
Packit Service 82fcde
Packit Service 82fcde
  /* If the parameter does not fit into one byte or it is the EOF value
Packit Service 82fcde
     we can give the answer now.  */
Packit Service 82fcde
  if (c < SCHAR_MIN || c > UCHAR_MAX || c == EOF)
Packit Service 82fcde
    return WEOF;
Packit Service 82fcde
Packit Service 82fcde
  /* We know that only ASCII compatible encodings are used for the
Packit Service 82fcde
     locale and that the wide character encoding is ISO 10646.  */
Packit Service 82fcde
  if (isascii (c))
Packit Service 82fcde
    return (wint_t) c;
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
  __gconv_btowc_fct btowc_fct = fcts->towc->__btowc_fct;
Packit Service 82fcde
#ifdef PTR_DEMANGLE
Packit Service 82fcde
  if (fcts->towc->__shlib_handle != NULL)
Packit Service 82fcde
    PTR_DEMANGLE (btowc_fct);
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
  if (__builtin_expect (fcts->towc_nsteps == 1, 1)
Packit Service 82fcde
      && __builtin_expect (btowc_fct != NULL, 1))
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Use the shortcut function.  */
Packit Service 82fcde
      return DL_CALL_FCT (btowc_fct, (fcts->towc, (unsigned char) c));
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Fall back to the slow but generic method.  */
Packit Service 82fcde
      wchar_t result;
Packit Service 82fcde
      struct __gconv_step_data data;
Packit Service 82fcde
      unsigned char inbuf[1];
Packit Service 82fcde
      const unsigned char *inptr = inbuf;
Packit Service 82fcde
      size_t dummy;
Packit Service 82fcde
      int status;
Packit Service 82fcde
Packit Service 82fcde
      /* Tell where we want the result.  */
Packit Service 82fcde
      data.__outbuf = (unsigned char *) &result;
Packit Service 82fcde
      data.__outbufend = data.__outbuf + sizeof (wchar_t);
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 = &data.__state;
Packit Service 82fcde
Packit Service 82fcde
      /* Make sure we start in the initial state.  */
Packit Service 82fcde
      memset (&data.__state, '\0', sizeof (mbstate_t));
Packit Service 82fcde
Packit Service 82fcde
      /* Create the input string.  */
Packit Service 82fcde
      inbuf[0] = c;
Packit Service 82fcde
Packit Service 82fcde
      __gconv_fct fct = fcts->towc->__fct;
Packit Service 82fcde
#ifdef PTR_DEMANGLE
Packit Service 82fcde
      if (fcts->towc->__shlib_handle != NULL)
Packit Service 82fcde
	PTR_DEMANGLE (fct);
Packit Service 82fcde
#endif
Packit Service 82fcde
      status = DL_CALL_FCT (fct, (fcts->towc, &data, &inptr, inptr + 1,
Packit Service 82fcde
				  NULL, &dummy, 0, 1));
Packit Service 82fcde
Packit Service 82fcde
      if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
Packit Service 82fcde
	  && status != __GCONV_EMPTY_INPUT)
Packit Service 82fcde
	/* The conversion failed.  */
Packit Service 82fcde
	result = WEOF;
Packit Service 82fcde
Packit Service 82fcde
      return result;
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
weak_alias (__btowc, btowc)