Blame wcsmbs/mbrtoc16.c

Packit 6c4009
/* Copyright (C) 2011-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
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 <uchar.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
mbrtoc16 (char16_t *pc16, const char *s, size_t n, mbstate_t *ps)
Packit 6c4009
{
Packit 6c4009
  if (ps == NULL)
Packit 6c4009
    ps = &stat;;
Packit 6c4009
Packit 6c4009
  /* The standard text does not say that S being NULL means the state
Packit 6c4009
     is reset even if the second half of a surrogate still have to be
Packit 6c4009
     returned.  In fact, the error code description indicates
Packit 6c4009
     otherwise.  Therefore always first try to return a second
Packit 6c4009
     half.  */
Packit 6c4009
  if (ps->__count & 0x80000000)
Packit 6c4009
    {
Packit 6c4009
      /* We have to return the second word for a surrogate.  */
Packit 6c4009
      ps->__count &= 0x7fffffff;
Packit 6c4009
      *pc16 = ps->__value.__wch;
Packit 6c4009
      ps->__value.__wch = L'\0';
Packit 6c4009
      return (size_t) -3;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  wchar_t wc;
Packit 6c4009
  struct __gconv_step_data data;
Packit 6c4009
  int status;
Packit 6c4009
  size_t result;
Packit 6c4009
  size_t dummy;
Packit 6c4009
  const unsigned char *inbuf, *endbuf;
Packit 6c4009
  unsigned char *outbuf = (unsigned char *) &wc;
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;
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
      pc16 = NULL;
Packit 6c4009
      s = "";
Packit 6c4009
      n = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (n == 0)
Packit 6c4009
    return (size_t) -2;
Packit 6c4009
Packit 6c4009
  /* Tell where we want the result.  */
Packit 6c4009
  data.__outbuf = outbuf;
Packit 6c4009
  data.__outbufend = outbuf + sizeof (wchar_t);
Packit 6c4009
Packit 6c4009
  /* Get the conversion functions.  */
Packit 6c4009
  fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
Packit 6c4009
Packit 6c4009
  /* Do a normal conversion.  */
Packit 6c4009
  inbuf = (const unsigned char *) s;
Packit 6c4009
  endbuf = inbuf + n;
Packit 6c4009
  if (__glibc_unlikely (endbuf < inbuf))
Packit 6c4009
    {
Packit 6c4009
      endbuf = (const unsigned char *) ~(uintptr_t) 0;
Packit 6c4009
      if (endbuf == inbuf)
Packit 6c4009
	goto ilseq;
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
Packit 6c4009
  status = DL_CALL_FCT (fct, (fcts->towc, &data, &inbuf, endbuf,
Packit 6c4009
			      NULL, &dummy, 0, 1));
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
    {
Packit 6c4009
      result = inbuf - (const unsigned char *) s;
Packit 6c4009
Packit 6c4009
      if (wc < 0x10000)
Packit 6c4009
	{
Packit 6c4009
	  if (pc16 != NULL)
Packit 6c4009
	    *pc16 = wc;
Packit 6c4009
Packit 6c4009
	  if (data.__outbuf != outbuf && wc == L'\0')
Packit 6c4009
	    {
Packit 6c4009
	      /* The converted character is the NUL character.  */
Packit 6c4009
	      assert (__mbsinit (data.__statep));
Packit 6c4009
	      result = 0;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* This is a surrogate.  */
Packit 6c4009
	  if (pc16 != NULL)
Packit 6c4009
	    *pc16 = 0xd7c0 + (wc >> 10);
Packit 6c4009
Packit 6c4009
	  ps->__count |= 0x80000000;
Packit 6c4009
	  ps->__value.__wch = 0xdc00 + (wc & 0x3ff);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (status == __GCONV_INCOMPLETE_INPUT)
Packit 6c4009
    result = (size_t) -2;
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
    ilseq:
Packit 6c4009
      result = (size_t) -1;
Packit 6c4009
      __set_errno (EILSEQ);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}