|
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 <assert.h>
|
|
Packit |
6c4009 |
#include <dlfcn.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <gconv.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 |
/* This is the private state used if PS is NULL. */
|
|
Packit |
6c4009 |
static mbstate_t state;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
size_t
|
|
Packit |
6c4009 |
__mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
wchar_t buf[1];
|
|
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 *) (pwc ?: buf);
|
|
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 |
outbuf = (unsigned char *) buf;
|
|
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 |
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 |
if (data.__outbuf != (unsigned char *) outbuf
|
|
Packit |
6c4009 |
&& *(wchar_t *) outbuf == 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 |
else
|
|
Packit |
6c4009 |
result = inbuf - (const unsigned char *) s;
|
|
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 |
}
|
|
Packit |
6c4009 |
libc_hidden_def (__mbrtowc)
|
|
Packit |
6c4009 |
weak_alias (__mbrtowc, mbrtowc)
|
|
Packit |
6c4009 |
libc_hidden_weak (mbrtowc)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* There should be no difference between the UTF-32 handling required
|
|
Packit |
6c4009 |
by mbrtoc32 and the wchar_t handling which has long since been
|
|
Packit |
6c4009 |
implemented in mbrtowc. */
|
|
Packit |
6c4009 |
weak_alias (__mbrtowc, mbrtoc32)
|