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