]> git.ipfire.org Git - thirdparty/glibc.git/blame - wcsmbs/wctob.c
x86: Add seperate non-temporal tunable for memset
[thirdparty/glibc.git] / wcsmbs / wctob.c
CommitLineData
dff8da6b 1/* Copyright (C) 1996-2024 Free Software Foundation, Inc.
33a934a3 2 This file is part of the GNU C Library.
30de3b18 3
33a934a3 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
30de3b18 8
33a934a3
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
30de3b18 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
30de3b18 17
55985355 18#include <dlfcn.h>
4bca4c17 19#include <gconv.h>
30de3b18 20#include <stdio.h>
4bca4c17 21#include <string.h>
30de3b18 22#include <wchar.h>
4bca4c17 23#include <wcsmbsload.h>
30de3b18 24
88f4b692 25#include <pointer_guard.h>
915a6c51 26
30de3b18
RM
27
28int
9d46370c 29wctob (wint_t c)
30de3b18 30{
3cc4a097 31 unsigned char buf[MB_LEN_MAX];
d64b6ad0 32 struct __gconv_step_data data;
4bca4c17 33 wchar_t inbuf[1];
8619129f 34 wchar_t *inptr = inbuf;
5e7a22c9 35 size_t dummy;
4bca4c17 36 int status;
96310297 37 const struct gconv_fcts *fcts;
4bca4c17 38
0e16ecfa
UD
39 if (c == WEOF)
40 return EOF;
41
91682d70
UD
42 /* We know that only ASCII compatible encodings are used for the
43 locale and that the wide character encoding is ISO 10646. */
44 if (c >= L'\0' && c <= L'\x7f')
45 return (int) c;
46
4bca4c17 47 /* Tell where we want the result. */
d64b6ad0
UD
48 data.__outbuf = buf;
49 data.__outbufend = buf + MB_LEN_MAX;
50 data.__invocation_counter = 0;
51 data.__internal_use = 1;
85830c4c 52 data.__flags = __GCONV_IS_LAST;
d64b6ad0 53 data.__statep = &data.__state;
4bca4c17
UD
54
55 /* Make sure we start in the initial state. */
56 memset (&data.__state, '\0', sizeof (mbstate_t));
57
96310297
RM
58 /* Get the conversion functions. */
59 fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
4bca4c17
UD
60
61 /* Create the input string. */
62 inbuf[0] = c;
4bca4c17 63
9c7ff11a 64 const unsigned char *argptr = (const unsigned char *) inptr;
915a6c51 65 __gconv_fct fct = fcts->tomb->__fct;
915a6c51
UD
66 if (fcts->tomb->__shlib_handle != NULL)
67 PTR_DEMANGLE (fct);
915a6c51 68 status = DL_CALL_FCT (fct,
9c7ff11a
UD
69 (fcts->tomb, &data, &argptr,
70 argptr + sizeof (inbuf[0]), NULL, &dummy, 0, 1));
55985355 71
4bca4c17 72 /* The conversion failed or the output is too long. */
d64b6ad0
UD
73 if ((status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
74 && status != __GCONV_EMPTY_INPUT)
75 || data.__outbuf != (unsigned char *) (buf + 1))
3c720987 76 return EOF;
4bca4c17 77
91682d70 78 return buf[0];
30de3b18 79}