1 /* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
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.
8 
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
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <assert.h>
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <gconv.h>
22 #include <wchar.h>
23 #include <wcsmbsload.h>
24 
25 #include <sysdep.h>
26 
27 #ifndef EILSEQ
28 # define EILSEQ EINVAL
29 #endif
30 
31 
32 /* This is the private state used if PS is NULL.  */
33 static mbstate_t state;
34 
35 /* This is a non-standard function but it is very useful in the
36    implementation of stdio because we have to deal with unterminated
37    buffers.  At most NWC wide character will be converted.  */
38 size_t
__wcsnrtombs(char * dst,const wchar_t ** src,size_t nwc,size_t len,mbstate_t * ps)39 __wcsnrtombs (char *dst, const wchar_t **src, size_t nwc, size_t len,
40 	      mbstate_t *ps)
41 {
42   struct __gconv_step_data data;
43   const wchar_t *srcend;
44   int status;
45   size_t result;
46   struct __gconv_step *tomb;
47   const struct gconv_fcts *fcts;
48 
49   /* Tell where we want the result.  */
50   data.__invocation_counter = 0;
51   data.__internal_use = 1;
52   data.__flags = __GCONV_IS_LAST;
53   data.__statep = ps ?: &state;
54 
55   if (nwc == 0)
56     return 0;
57   srcend = *src + __wcsnlen (*src, nwc - 1) + 1;
58 
59   /* Get the conversion functions.  */
60   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
61 
62   /* Get the structure with the function pointers.  */
63   tomb = fcts->tomb;
64   __gconv_fct fct = tomb->__fct;
65 #ifdef PTR_DEMANGLE
66   if (tomb->__shlib_handle != NULL)
67     PTR_DEMANGLE (fct);
68 #endif
69 
70   /* We have to handle DST == NULL special.  */
71   if (dst == NULL)
72     {
73       mbstate_t temp_state;
74       unsigned char buf[256];		/* Just an arbitrary value.  */
75       const unsigned char *inbuf = (const unsigned char *) *src;
76       size_t dummy;
77 
78       temp_state = *data.__statep;
79       data.__statep = &temp_state;
80 
81       result = 0;
82       data.__outbufend = buf + sizeof (buf);
83 
84       do
85 	{
86 	  data.__outbuf = buf;
87 
88 	  status = DL_CALL_FCT (fct, (tomb, &data, &inbuf,
89 				      (const unsigned char *) srcend, NULL,
90 				      &dummy, 0, 1));
91 
92 	  /* Count the number of bytes.  */
93 	  result += data.__outbuf - buf;
94 	}
95       while (status == __GCONV_FULL_OUTPUT);
96 
97       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
98 	  && data.__outbuf[-1] == '\0')
99 	/* Don't count the NUL character in.  */
100 	--result;
101     }
102   else
103     {
104       /* This code is based on the safe assumption that all internal
105 	 multi-byte encodings use the NUL byte only to mark the end
106 	 of the string.  */
107       size_t dummy;
108 
109       data.__outbuf = (unsigned char *) dst;
110       data.__outbufend = (unsigned char *) dst + len;
111 
112       status = DL_CALL_FCT (fct, (tomb, &data, (const unsigned char **) src,
113 				  (const unsigned char *) srcend, NULL,
114 				  &dummy, 0, 1));
115 
116       /* Count the number of bytes.  */
117       result = data.__outbuf - (unsigned char *) dst;
118 
119       /* We have to determine whether the last character converted
120 	 is the NUL character.  */
121       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
122 	  && data.__outbuf[-1] == '\0')
123 	{
124 	  assert (data.__outbuf != (unsigned char *) dst);
125 	  assert (__mbsinit (data.__statep));
126 	  *src = NULL;
127 	  --result;
128 	}
129     }
130 
131   /* There must not be any problems with the conversion but illegal input
132      characters.  */
133   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
134 	  || status == __GCONV_ILLEGAL_INPUT
135 	  || status == __GCONV_INCOMPLETE_INPUT
136 	  || status == __GCONV_FULL_OUTPUT);
137 
138   if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
139       && status != __GCONV_EMPTY_INPUT)
140     {
141       result = (size_t) -1;
142       __set_errno (EILSEQ);
143     }
144 
145   return result;
146 }
147 weak_alias (__wcsnrtombs, wcsnrtombs)
148