xref: /netbsd/lib/libc/citrus/citrus_none.c (revision bf9ec67e)
1 /*	$NetBSD: citrus_none.c,v 1.5 2002/05/24 04:04:30 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c)2002 Citrus Project,
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_none.c,v 1.5 2002/05/24 04:04:30 thorpej Exp $");
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <assert.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <locale.h>
41 #include <wchar.h>
42 #include <sys/types.h>
43 #include "citrus_module.h"
44 #include "citrus_ctype.h"
45 #include "citrus_none.h"
46 
47 /* ---------------------------------------------------------------------- */
48 
49 _CITRUS_CTYPE_DECLS(NONE);
50 _CITRUS_CTYPE_DEF_OPS(NONE);
51 
52 
53 /* ---------------------------------------------------------------------- */
54 
55 static int
56 /*ARGSUSED*/
57 _citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var,
58 			size_t lenvar, size_t lenps)
59 {
60 	*cl = NULL;
61 	return (0);
62 }
63 
64 static void
65 /*ARGSUSED*/
66 _citrus_NONE_ctype_uninit(void *cl)
67 {
68 }
69 
70 static unsigned
71 /*ARGSUSED*/
72 _citrus_NONE_ctype_get_mb_cur_max(void *cl)
73 {
74 	return (1);
75 }
76 
77 static int
78 /*ARGSUSED*/
79 _citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s,
80 			 size_t n, int * __restrict nresult)
81 {
82 	if (!s) {
83 		*nresult = 0; /* state independent */
84 		return (0);
85 	}
86 	if (n==0) {
87 		*nresult = -1;
88 		return (EILSEQ);
89 	}
90 	*nresult = (*s == 0) ? 0 : 1;
91 	return (0);
92 }
93 
94 static int
95 /*ARGSUSED*/
96 _citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s,
97 			  size_t n, void * __restrict pspriv,
98 			  size_t * __restrict nresult)
99 {
100 	if (!s) {
101 		*nresult = 0;
102 		return (0);
103 	}
104 	if (n==0) {
105 		*nresult = (size_t)-2;
106 		return (0);
107 	}
108 	*nresult = (*s == 0) ? 0 : 1;
109 	return (0);
110 }
111 
112 static int
113 /*ARGSUSED*/
114 _citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc,
115 			   const char * __restrict s, size_t n,
116 			   void * __restrict pspriv,
117 			   size_t * __restrict nresult)
118 {
119 	if (s == NULL) {
120 		*nresult = 0;
121 		return (0);
122 	}
123 	if (n == 0) {
124 		*nresult = (size_t)-2;
125 		return (0);
126 	}
127 
128 	if (pwc != NULL)
129 		*pwc = (wchar_t)(unsigned char) *s;
130 
131 	*nresult = *s == '\0' ? 0 : 1;
132 	return (0);
133 }
134 
135 static int
136 /*ARGSUSED*/
137 _citrus_NONE_ctype_mbsinit(void * __restrict cl,
138 			   const void * __restrict pspriv,
139 			   int * __restrict nresult)
140 {
141 	*nresult = 1;  /* always initial state */
142 	return (0);
143 }
144 
145 static int
146 /*ARGSUSED*/
147 _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict wcs,
148 			     const char ** __restrict s, size_t n,
149 			     void * __restrict pspriv,
150 			     size_t * __restrict nresult)
151 {
152 	size_t count;
153 	const char *s0;
154 
155 	count = 0;
156 	s0 = *s;
157 	while (n>0) {
158 		if (wcs != NULL)
159 			*wcs++ = (wchar_t)(unsigned char)*s0;
160 		if (*s0 == '\0') {
161 			s0 = NULL;
162 			break;
163 		}
164 		count++;
165 		n--;
166 		s0++;
167 	}
168 	if (*wcs)
169 		*s = s0;
170 
171 	*nresult = count;
172 
173 	return (0);
174 }
175 
176 static int
177 _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs,
178 			    const char * __restrict s, size_t n,
179 			    size_t * __restrict nresult)
180 {
181 	return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, (const char **)&s, n, NULL, nresult));
182 }
183 
184 static int
185 /*ARGSUSED*/
186 _citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc,
187 			  const char * __restrict s, size_t n,
188 			  int * __restrict nresult)
189 {
190 
191 	if (s == NULL) {
192 		*nresult = 0; /* state independent */
193 		return (0);
194 	}
195 	if (n == 0) {
196 		return (EILSEQ);
197 	}
198 	if (pwc == NULL) {
199 		if (*s == '\0') {
200 			*nresult = 0;
201 		} else {
202 			*nresult = 1;
203 		}
204 		return (0);
205 	}
206 
207 	*pwc = (wchar_t)*s;
208 	*nresult = *s == '\0' ? 0 : 1;
209 
210 	return (0);
211 }
212 
213 static int
214 /*ARGSUSED*/
215 _citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s,
216 			   wchar_t wc, void * __restrict pspriv,
217 			   size_t * __restrict nresult)
218 {
219 	if ((wc&~0xFFU) != 0) {
220 		*nresult = (size_t)-1;
221 		return (EILSEQ);
222 	}
223 
224 	*nresult = 1;
225 	if (s!=NULL)
226 		*s = (char)wc;
227 
228 	return (0);
229 }
230 
231 static int
232 /*ARGSUSED*/
233 _citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s,
234 			     const wchar_t ** __restrict pwcs, size_t n,
235 			     void * __restrict pspriv,
236 			     size_t * __restrict nresult)
237 {
238 	size_t count;
239 	const wchar_t *pwcs0;
240 
241 	pwcs0 = *pwcs;
242 	count = 0;
243 
244 	if (s == NULL)
245 		n = 1;
246 
247 	while (n > 0) {
248 		if ((*pwcs0 & ~0xFFU) != 0) {
249 			*nresult = (size_t)-1;
250 			return (EILSEQ);
251 		}
252 		if (s != NULL) {
253 			*s++ = (char)*pwcs0;
254 			n--;
255 		}
256 		if (*pwcs0 == L'\0') {
257 			pwcs0 = NULL;
258 			break;
259 		}
260 		count++;
261 		pwcs0++;
262 	}
263 	if (s != NULL)
264 		*pwcs = pwcs0;
265 
266 	*nresult = count;
267 
268 	return (0);
269 }
270 
271 static int
272 _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s,
273 			    const wchar_t * __restrict pwcs, size_t n,
274 			    size_t * __restrict nresult)
275 {
276 	return (_citrus_NONE_ctype_wcsrtombs(cl, s, (const wchar_t **)&pwcs, n, NULL, nresult));
277 }
278 
279 static int
280 _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s,
281 			  wchar_t wc, int * __restrict nresult)
282 {
283 	int ret;
284 	size_t nr;
285 
286 	ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr);
287 	*nresult = (int)nr;
288 
289 	return (ret);
290 }
291