pkg-monitoring-plugins/gl/mbrtowc.c

158 lines
3.6 KiB
C
Raw Normal View History

/* Convert multibyte character to wide character.
2023-10-18 07:29:37 +00:00
Copyright (C) 1999-2002, 2005-2023 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2008.
2023-10-18 07:29:37 +00:00
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
2023-10-18 07:29:37 +00:00
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2023-10-18 07:29:37 +00:00
GNU Lesser General Public License for more details.
2023-10-18 07:29:37 +00:00
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <wchar.h>
#if GNULIB_defined_mbstate_t
2023-10-18 07:29:37 +00:00
/* Implement mbrtowc() on top of mbtowc() for the non-UTF-8 locales
and directly for the UTF-8 locales. */
# include <errno.h>
2023-10-18 07:29:37 +00:00
# include <stdint.h>
# include <stdlib.h>
2023-10-18 07:29:37 +00:00
# if defined _WIN32 && !defined __CYGWIN__
2023-10-18 07:29:37 +00:00
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
2023-10-18 07:29:37 +00:00
# elif HAVE_PTHREAD_API
2023-10-18 07:29:37 +00:00
# include <pthread.h>
# if HAVE_THREADS_H && HAVE_WEAK_SYMBOLS
# include <threads.h>
# pragma weak thrd_exit
# define c11_threads_in_use() (thrd_exit != NULL)
# else
# define c11_threads_in_use() 0
# endif
2023-10-18 07:29:37 +00:00
# elif HAVE_THREADS_H
2023-10-18 07:29:37 +00:00
# include <threads.h>
# endif
2013-11-26 22:57:29 +00:00
2023-10-18 07:29:37 +00:00
# include "attribute.h"
# include "lc-charset-dispatch.h"
# include "mbtowc-lock.h"
2013-11-26 22:57:29 +00:00
2023-10-18 07:29:37 +00:00
static_assert (sizeof (mbstate_t) >= 4);
static char internal_state[4];
2023-10-18 07:29:37 +00:00
size_t
mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
{
# define FITS_IN_CHAR_TYPE(wc) ((wc) <= WCHAR_MAX)
# include "mbrtowc-impl.h"
}
#else
/* Override the system's mbrtowc() function. */
2023-10-18 07:29:37 +00:00
# if MBRTOWC_IN_C_LOCALE_MAYBE_EILSEQ
# include "hard-locale.h"
# include <locale.h>
# endif
# undef mbrtowc
size_t
rpl_mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
{
2023-10-18 07:29:37 +00:00
size_t ret;
wchar_t wc;
# if MBRTOWC_NULL_ARG2_BUG || MBRTOWC_RETVAL_BUG || MBRTOWC_EMPTY_INPUT_BUG
if (s == NULL)
{
pwc = NULL;
s = "";
n = 1;
}
# endif
2023-10-18 07:29:37 +00:00
# if MBRTOWC_EMPTY_INPUT_BUG
if (n == 0)
return (size_t) -2;
# endif
if (! pwc)
pwc = &wc;
# if MBRTOWC_RETVAL_BUG
{
static mbstate_t internal_state;
/* Override mbrtowc's internal state. We cannot call mbsinit() on the
hidden internal state, but we can call it on our variable. */
if (ps == NULL)
ps = &internal_state;
if (!mbsinit (ps))
{
2013-11-26 22:57:29 +00:00
/* Parse the rest of the multibyte character byte for byte. */
size_t count = 0;
for (; n > 0; s++, n--)
{
2023-10-18 07:29:37 +00:00
ret = mbrtowc (&wc, s, 1, ps);
2013-11-26 22:57:29 +00:00
if (ret == (size_t)(-1))
return (size_t)(-1);
count++;
if (ret != (size_t)(-2))
{
/* The multibyte character has been completed. */
2023-10-18 07:29:37 +00:00
*pwc = wc;
2013-11-26 22:57:29 +00:00
return (wc == 0 ? 0 : count);
}
}
return (size_t)(-2);
}
}
# endif
2023-10-18 07:29:37 +00:00
# if MBRTOWC_STORES_INCOMPLETE_BUG
ret = mbrtowc (&wc, s, n, ps);
if (ret < (size_t) -2 && pwc != NULL)
*pwc = wc;
# else
2023-10-18 07:29:37 +00:00
ret = mbrtowc (pwc, s, n, ps);
# endif
2023-10-18 07:29:37 +00:00
# if MBRTOWC_NUL_RETVAL_BUG
if (ret < (size_t) -2 && !*pwc)
return 0;
# endif
2023-10-18 07:29:37 +00:00
# if MBRTOWC_IN_C_LOCALE_MAYBE_EILSEQ
if ((size_t) -2 <= ret && n != 0 && ! hard_locale (LC_CTYPE))
{
unsigned char uc = *s;
*pwc = uc;
return 1;
}
# endif
2023-10-18 07:29:37 +00:00
return ret;
}
#endif