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