]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Locale/multibyte_sb.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Locale / multibyte_sb.c
1 /* $NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $ */
2
3 /*
4 * Copyright (c) 1991 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31 #include <LibConfig.h>
32 #include <sys/EfiCdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";
36 #else
37 __RCSID("$NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <assert.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <wchar.h>
45
46 /*
47 * Stub multibyte character functions.
48 * This cheezy implementation is fixed to the native single-byte
49 * character set.
50 */
51
52 /*ARGSUSED*/
53 int
54 mbsinit(const mbstate_t *ps)
55 {
56
57 return 1;
58 }
59
60 /*ARGSUSED*/
61 size_t
62 mbrlen(
63 const char *s,
64 size_t n,
65 mbstate_t *ps
66 )
67 {
68
69 /* ps appears to be unused */
70
71 if (s == NULL || *s == '\0')
72 return 0;
73 if (n == 0)
74 return (size_t)-1;
75 return 1;
76 }
77
78 int
79 mblen(
80 const char *s,
81 size_t n
82 )
83 {
84
85 /* s may be NULL */
86
87 return (int)mbrlen(s, n, NULL);
88 }
89
90 /*ARGSUSED*/
91 size_t
92 mbrtowc(
93 wchar_t *pwc,
94 const char *s,
95 size_t n,
96 mbstate_t *ps
97 )
98 {
99
100 /* pwc may be NULL */
101 /* s may be NULL */
102 /* ps appears to be unused */
103
104 if (s == NULL)
105 return 0;
106 if (n == 0)
107 return (size_t)-1;
108 if (pwc)
109 *pwc = (wchar_t) *s;
110 return (*s != '\0');
111 }
112
113 int
114 mbtowc(
115 wchar_t *pwc,
116 const char *s,
117 size_t n
118 )
119 {
120
121 /* pwc may be NULL */
122 /* s may be NULL */
123
124 return (int)mbrtowc(pwc, s, n, NULL);
125 }
126
127 /*ARGSUSED*/
128 size_t
129 wcrtomb(
130 char *s,
131 wchar_t wchar,
132 mbstate_t *ps
133 )
134 {
135
136 /* s may be NULL */
137 /* ps appears to be unused */
138
139 if (s == NULL)
140 return 1; /* Spec. says this should be 1. */
141
142 *s = (char) wchar;
143 return 1;
144 }
145
146 int
147 wctomb(
148 char *s,
149 wchar_t wchar
150 )
151 {
152
153 /*
154 If s is NULL just return if MB Characters have state
155 dependent encodings.
156 */
157 if (s == NULL)
158 return 0;
159
160 return (int)wcrtomb(s, wchar, NULL);
161 }
162
163 /*ARGSUSED*/
164 size_t
165 mbsrtowcs(
166 wchar_t *pwcs,
167 const char **s,
168 size_t n,
169 mbstate_t *ps
170 )
171 {
172 int count = 0;
173
174 /* pwcs may be NULL */
175 /* s may be NULL */
176 /* ps appears to be unused */
177
178 if (!s || !*s)
179 return 0;
180
181 if (n != 0) {
182 if (pwcs != NULL) {
183 do {
184 if ((*pwcs++ = (wchar_t) *(*s)++) == 0) {
185 *s = NULL;
186 break;
187 }
188 count++;
189 } while (--n != 0);
190 } else {
191 do {
192 if (((wchar_t)*(*s)++) == 0)
193 break;
194 count++;
195 } while (--n != 0);
196 }
197 }
198
199 return count;
200 }
201
202 size_t
203 mbstowcs(
204 wchar_t *pwcs,
205 const char *s,
206 size_t n
207 )
208 {
209
210 /* pwcs may be NULL */
211 /* s may be NULL */
212
213 return mbsrtowcs(pwcs, &s, n, NULL);
214 }
215
216 /*ARGSUSED*/
217 size_t
218 wcsrtombs(
219 char *s,
220 const wchar_t **pwcs,
221 size_t n,
222 mbstate_t *ps
223 )
224 {
225 int count = 0;
226
227 /* s may be NULL */
228 /* pwcs may be NULL */
229 /* ps appears to be unused */
230
231 if (pwcs == NULL || *pwcs == NULL)
232 return (0);
233
234 if (s == NULL) {
235 while (*(*pwcs)++ != 0)
236 count++;
237 return(count);
238 }
239
240 if (n != 0) {
241 do {
242 if ((*s++ = (char) *(*pwcs)++) == 0) {
243 *pwcs = NULL;
244 break;
245 }
246 count++;
247 } while (--n != 0);
248 }
249
250 return count;
251 }
252
253 size_t
254 wcstombs(
255 char *s,
256 const wchar_t *pwcs,
257 size_t n
258 )
259 {
260
261 /* s may be NULL */
262 /* pwcs may be NULL */
263
264 return wcsrtombs(s, &pwcs, n, NULL);
265 }
266
267 wint_t
268 btowc(int c)
269 {
270 if (c == EOF || c & ~0xFF)
271 return WEOF;
272 return (wint_t)c;
273 }
274
275 int
276 wctob(wint_t c)
277 {
278 /* wctob needs to be consistent with wcrtomb.
279 if wcrtomb says that a character is representable in 1 byte,
280 which this implementation always says, then wctob needs to
281 also represent the character as 1 byte.
282 */
283 if (c == WEOF) {
284 return EOF;
285 }
286 return (int)(c & 0xFF);
287 }