xref: /dflybsd-src/lib/libc/citrus/citrus_none.c (revision 23c32883e759b0ea42fdaff39e661bd1a12e3b9f)
1 /*	$NetBSD: src/lib/libc/citrus/citrus_none.c,v 1.12 2004/01/18 03:57:30 yamt Exp $	*/
2 /*	$DragonFly: src/lib/libc/citrus/citrus_none.c,v 1.2 2005/03/16 06:13:24 joerg Exp $ */
3 
4 /*-
5  * Copyright (c)2002 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <assert.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stddef.h>
36 #include <locale.h>
37 #include <wchar.h>
38 #include <sys/types.h>
39 
40 #include "citrus_namespace.h"
41 #include "citrus_types.h"
42 #include "citrus_module.h"
43 #include "citrus_ctype.h"
44 #include "citrus_none.h"
45 #include "citrus_stdenc.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 __unused,
58 			size_t lenvar __unused, size_t lenps __unused)
59 {
60 	*cl = NULL;
61 	return (0);
62 }
63 
64 static void
65 /*ARGSUSED*/
66 _citrus_NONE_ctype_uninit(void *cl __unused)
67 {
68 }
69 
70 static unsigned
71 /*ARGSUSED*/
72 _citrus_NONE_ctype_get_mb_cur_max(void *cl __unused)
73 {
74 	return (1);
75 }
76 
77 static int
78 /*ARGSUSED*/
79 _citrus_NONE_ctype_mblen(void * __restrict cl __unused,
80 			 const char * __restrict s, size_t n,
81 			 int * __restrict nresult)
82 {
83 	if (!s) {
84 		*nresult = 0; /* state independent */
85 		return (0);
86 	}
87 	if (n==0) {
88 		*nresult = -1;
89 		return (EILSEQ);
90 	}
91 	*nresult = (*s == 0) ? 0 : 1;
92 	return (0);
93 }
94 
95 static int
96 /*ARGSUSED*/
97 _citrus_NONE_ctype_mbrlen(void * __restrict cl __unused,
98 			  const char * __restrict s, size_t n,
99 			  void * __restrict pspriv __unused,
100 			  size_t * __restrict nresult)
101 {
102 	if (!s) {
103 		*nresult = 0;
104 		return (0);
105 	}
106 	if (n==0) {
107 		*nresult = (size_t)-2;
108 		return (0);
109 	}
110 	*nresult = (*s == 0) ? 0 : 1;
111 	return (0);
112 }
113 
114 static int
115 /*ARGSUSED*/
116 _citrus_NONE_ctype_mbrtowc(void * __restrict cl __unused,
117 			   wchar_t * __restrict pwc,
118 			   const char * __restrict s, size_t n,
119 			   void * __restrict pspriv __unused,
120 			   size_t * __restrict nresult)
121 {
122 	if (s == NULL) {
123 		*nresult = 0;
124 		return (0);
125 	}
126 	if (n == 0) {
127 		*nresult = (size_t)-2;
128 		return (0);
129 	}
130 
131 	if (pwc != NULL)
132 		*pwc = (wchar_t)(unsigned char) *s;
133 
134 	*nresult = *s == '\0' ? 0 : 1;
135 	return (0);
136 }
137 
138 static int
139 /*ARGSUSED*/
140 _citrus_NONE_ctype_mbsinit(void * __restrict cl __unused,
141 			   const void * __restrict pspriv __unused,
142 			   int * __restrict nresult)
143 {
144 	*nresult = 1;  /* always initial state */
145 	return (0);
146 }
147 
148 static int
149 /*ARGSUSED*/
150 _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl __unused,
151 			     wchar_t * __restrict pwcs,
152 			     const char ** __restrict s, size_t n,
153 			     void * __restrict pspriv __unused,
154 			     size_t * __restrict nresult)
155 {
156 	int cnt;
157 	const char *s0;
158 
159 	/* if pwcs is NULL, ignore n */
160 	if (pwcs == NULL)
161 		n = 1; /* arbitrary >0 value */
162 
163 	cnt = 0;
164 	s0 = *s; /* to keep *s unchanged for now, use copy instead. */
165 	while (n > 0) {
166 		if (pwcs != NULL) {
167 			*pwcs = (wchar_t)(unsigned char)*s0;
168 		}
169 		if (*s0 == '\0') {
170 			s0 = NULL;
171 			break;
172 		}
173 		s0++;
174 		if (pwcs != NULL) {
175 			pwcs++;
176 			n--;
177 		}
178 		cnt++;
179 	}
180 	if (pwcs)
181 		*s = s0;
182 
183 	*nresult = (size_t)cnt;
184 
185 	return (0);
186 }
187 
188 static int
189 _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs,
190 			    const char * __restrict s, size_t n,
191 			    size_t * __restrict nresult)
192 {
193 	return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, &s, n, NULL, nresult));
194 }
195 
196 static int
197 /*ARGSUSED*/
198 _citrus_NONE_ctype_mbtowc(void * __restrict cl __unused,
199 			  wchar_t * __restrict pwc,
200 			  const char * __restrict s, size_t n,
201 			  int * __restrict nresult)
202 {
203 
204 	if (s == NULL) {
205 		*nresult = 0; /* state independent */
206 		return (0);
207 	}
208 	if (n == 0) {
209 		return (EILSEQ);
210 	}
211 	if (pwc == NULL) {
212 		if (*s == '\0') {
213 			*nresult = 0;
214 		} else {
215 			*nresult = 1;
216 		}
217 		return (0);
218 	}
219 
220 	*pwc = (wchar_t)*s;
221 	*nresult = *s == '\0' ? 0 : 1;
222 
223 	return (0);
224 }
225 
226 static int
227 /*ARGSUSED*/
228 _citrus_NONE_ctype_wcrtomb(void * __restrict cl __unused,
229 			   char * __restrict s, wchar_t wc,
230 			   void * __restrict pspriv __unused,
231 			   size_t * __restrict nresult)
232 {
233 	if ((wc&~0xFFU) != 0) {
234 		*nresult = (size_t)-1;
235 		return (EILSEQ);
236 	}
237 
238 	*nresult = 1;
239 	if (s!=NULL)
240 		*s = (char)wc;
241 
242 	return (0);
243 }
244 
245 static int
246 /*ARGSUSED*/
247 _citrus_NONE_ctype_wcsrtombs(void * __restrict cl __unused,
248 			     char * __restrict s,
249 			     const wchar_t ** __restrict pwcs, size_t n,
250 			     void * __restrict pspriv __unused,
251 			     size_t * __restrict nresult)
252 {
253 	size_t count;
254 	const wchar_t *pwcs0;
255 
256 	pwcs0 = *pwcs;
257 	count = 0;
258 
259 	if (s == NULL)
260 		n = 1;
261 
262 	while (n > 0) {
263 		if ((*pwcs0 & ~0xFFU) != 0) {
264 			*nresult = (size_t)-1;
265 			return (EILSEQ);
266 		}
267 		if (s != NULL) {
268 			*s++ = (char)*pwcs0;
269 			n--;
270 		}
271 		if (*pwcs0 == L'\0') {
272 			pwcs0 = NULL;
273 			break;
274 		}
275 		count++;
276 		pwcs0++;
277 	}
278 	if (s != NULL)
279 		*pwcs = pwcs0;
280 
281 	*nresult = count;
282 
283 	return (0);
284 }
285 
286 static int
287 _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s,
288 			    const wchar_t * __restrict pwcs, size_t n,
289 			    size_t * __restrict nresult)
290 {
291 	return (_citrus_NONE_ctype_wcsrtombs(cl, s, &pwcs, n, NULL, nresult));
292 }
293 
294 static int
295 _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s,
296 			  wchar_t wc, int * __restrict nresult)
297 {
298 	int ret;
299 	size_t nr;
300 
301 	if (s == 0) {
302 		/*
303 		 * initialize state here.
304 		 * (nothing to do for us.)
305 		 */
306 		*nresult = 0; /* we're state independent */
307 		return (0);
308 	}
309 
310 	ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr);
311 	*nresult = (int)nr;
312 
313 	return (ret);
314 }
315 
316 static int
317 /*ARGSUSED*/
318 _citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc __unused,
319 			 int c, wint_t * __restrict wcresult)
320 {
321 	if (c == EOF || c & ~0xFF)
322 		*wcresult = WEOF;
323 	else
324 		*wcresult = (wint_t)c;
325 	return (0);
326 }
327 
328 static int
329 /*ARGSUSED*/
330 _citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc __unused,
331 			 wint_t wc, int * __restrict cresult)
332 {
333 	if (wc == WEOF || wc & ~0xFF)
334 		*cresult = EOF;
335 	else
336 		*cresult = (int)wc;
337 	return (0);
338 }
339 
340 /* ---------------------------------------------------------------------- */
341 
342 _CITRUS_STDENC_DECLS(NONE);
343 _CITRUS_STDENC_DEF_OPS(NONE);
344 struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = {
345 	0,	/* et_state_size */
346 	1,	/* mb_cur_max */
347 };
348 
349 static int
350 /*ARGSUSED*/
351 _citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce,
352 			 const void *var __unused, size_t lenvar __unused,
353 			 struct _citrus_stdenc_traits * __restrict et)
354 {
355 
356 	et->et_state_size = 0;
357 	et->et_mb_cur_max = 1;
358 
359 	ce->ce_closure = NULL;
360 
361 	return (0);
362 }
363 
364 static void
365 /*ARGSUSED*/
366 _citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce __unused)
367 {
368 }
369 
370 static int
371 /*ARGSUSED*/
372 _citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce __unused,
373 			       void * __restrict ps __unused)
374 {
375 	return (0);
376 }
377 
378 static int
379 /*ARGSUSED*/
380 _citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce __unused,
381 			   _csid_t *csid, _index_t *idx,
382 			   const char **s, size_t n,
383 			   void *ps __unused, size_t *nresult)
384 {
385 
386 	_DIAGASSERT(csid != NULL && idx != NULL);
387 
388 	if (n<1) {
389 		*nresult = (size_t)-2;
390 		return (0);
391 	}
392 
393 	*csid = 0;
394 	*idx = (_index_t)(unsigned char)*(*s)++;
395 	*nresult = *idx == 0 ? 0 : 1;
396 
397 	return (0);
398 }
399 
400 static int
401 /*ARGSUSED*/
402 _citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce __unused,
403 			   char *s, size_t n,
404 			   _csid_t csid, _index_t idx,
405 			   void *ps __unused, size_t *nresult)
406 {
407 
408 	if (csid == _CITRUS_CSID_INVALID) {
409 		*nresult = 0;
410 		return (0);
411 	}
412 	if (n<1) {
413 		*nresult = (size_t)-1;
414 		return (E2BIG);
415 	}
416 	if (csid != 0 || (idx&0xFF) != idx)
417 		return (EILSEQ);
418 
419 	*s = (char)idx;
420 	*nresult = 1;
421 
422 	return (0);
423 }
424 
425 static int
426 /*ARGSUSED*/
427 _citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce __unused,
428 			   _wc_t * __restrict pwc,
429 			   const char ** __restrict s, size_t n,
430 			   void * __restrict pspriv __unused,
431 			   size_t * __restrict nresult)
432 {
433 	if (s == NULL) {
434 		*nresult = 0;
435 		return (0);
436 	}
437 	if (n == 0) {
438 		*nresult = (size_t)-2;
439 		return (0);
440 	}
441 
442 	if (pwc != NULL)
443 		*pwc = (_wc_t)(unsigned char) **s;
444 
445 	*nresult = *s == '\0' ? 0 : 1;
446 	return (0);
447 }
448 
449 static int
450 /*ARGSUSED*/
451 _citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce __unused,
452 			   char * __restrict s, size_t n, _wc_t wc,
453 			   void * __restrict pspriv __unused,
454 			   size_t * __restrict nresult)
455 {
456 	if ((wc&~0xFFU) != 0) {
457 		*nresult = (size_t)-1;
458 		return (EILSEQ);
459 	}
460 	if (n==0) {
461 		*nresult = (size_t)-1;
462 		return (E2BIG);
463 	}
464 
465 	*nresult = 1;
466 	if (s!=NULL && n>0)
467 		*s = (char)wc;
468 
469 	return (0);
470 }
471 
472 static int
473 /*ARGSUSED*/
474 _citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce __unused,
475 				    char * __restrict s __unused,
476 				    size_t n __unused,
477 				    void * __restrict pspriv __unused,
478 				    size_t * __restrict nresult)
479 {
480 
481 	*nresult = 0;
482 
483 	return (0);
484 }
485