xref: /minix3/lib/libc/citrus/citrus_none.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1 /*	$NetBSD: citrus_none.c,v 1.19 2013/05/28 16:57:56 joerg 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.19 2013/05/28 16:57:56 joerg 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 <wchar.h>
41 #include <sys/types.h>
42 
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_module.h"
46 #include "citrus_ctype.h"
47 #include "citrus_none.h"
48 #include "citrus_stdenc.h"
49 
50 /* ---------------------------------------------------------------------- */
51 
52 _CITRUS_CTYPE_DECLS(NONE);
53 _CITRUS_CTYPE_DEF_OPS(NONE);
54 
55 
56 /* ---------------------------------------------------------------------- */
57 
58 static int
59 /*ARGSUSED*/
_citrus_NONE_ctype_init(void ** __restrict cl,void * __restrict var,size_t lenvar,size_t lenps)60 _citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var,
61 			size_t lenvar, size_t lenps)
62 {
63 	*cl = NULL;
64 	return (0);
65 }
66 
67 static void
68 /*ARGSUSED*/
_citrus_NONE_ctype_uninit(void * cl)69 _citrus_NONE_ctype_uninit(void *cl)
70 {
71 }
72 
73 static unsigned
74 /*ARGSUSED*/
_citrus_NONE_ctype_get_mb_cur_max(void * cl)75 _citrus_NONE_ctype_get_mb_cur_max(void *cl)
76 {
77 	return (1);
78 }
79 
80 static int
81 /*ARGSUSED*/
_citrus_NONE_ctype_mblen(void * __restrict cl,const char * __restrict s,size_t n,int * __restrict nresult)82 _citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s,
83 			 size_t n, int * __restrict nresult)
84 {
85 	if (!s) {
86 		*nresult = 0; /* state independent */
87 		return (0);
88 	}
89 	if (n==0) {
90 		*nresult = -1;
91 		return (EILSEQ);
92 	}
93 	*nresult = (*s == 0) ? 0 : 1;
94 	return (0);
95 }
96 
97 static int
98 /*ARGSUSED*/
_citrus_NONE_ctype_mbrlen(void * __restrict cl,const char * __restrict s,size_t n,void * __restrict pspriv,size_t * __restrict nresult)99 _citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s,
100 			  size_t n, void * __restrict pspriv,
101 			  size_t * __restrict nresult)
102 {
103 	if (!s) {
104 		*nresult = 0;
105 		return (0);
106 	}
107 	if (n==0) {
108 		*nresult = (size_t)-2;
109 		return (0);
110 	}
111 	*nresult = (*s == 0) ? 0 : 1;
112 	return (0);
113 }
114 
115 static int
116 /*ARGSUSED*/
_citrus_NONE_ctype_mbrtowc(void * __restrict cl,wchar_t * __restrict pwc,const char * __restrict s,size_t n,void * __restrict pspriv,size_t * __restrict nresult)117 _citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc,
118 			   const char * __restrict s, size_t n,
119 			   void * __restrict pspriv,
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*/
_citrus_NONE_ctype_mbsinit(void * __restrict cl,const void * __restrict pspriv,int * __restrict nresult)140 _citrus_NONE_ctype_mbsinit(void * __restrict cl,
141 			   const void * __restrict pspriv,
142 			   int * __restrict nresult)
143 {
144 	*nresult = 1;  /* always initial state */
145 	return (0);
146 }
147 
148 static int
149 /*ARGSUSED*/
_citrus_NONE_ctype_mbsrtowcs(void * __restrict cl,wchar_t * __restrict pwcs,const char ** __restrict s,size_t n,void * __restrict pspriv,size_t * __restrict nresult)150 _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs,
151 			     const char ** __restrict s, size_t n,
152 			     void * __restrict pspriv,
153 			     size_t * __restrict nresult)
154 {
155 	int cnt;
156 	const char *s0;
157 
158 	/* if pwcs is NULL, ignore n */
159 	if (pwcs == NULL)
160 		n = 1; /* arbitrary >0 value */
161 
162 	cnt = 0;
163 	s0 = *s; /* to keep *s unchanged for now, use copy instead. */
164 	while (n > 0) {
165 		if (pwcs != NULL) {
166 			*pwcs = (wchar_t)(unsigned char)*s0;
167 		}
168 		if (*s0 == '\0') {
169 			s0 = NULL;
170 			break;
171 		}
172 		s0++;
173 		if (pwcs != NULL) {
174 			pwcs++;
175 			n--;
176 		}
177 		cnt++;
178 	}
179 	if (pwcs)
180 		*s = s0;
181 
182 	*nresult = (size_t)cnt;
183 
184 	return (0);
185 }
186 
187 static int
188 /*ARGSUSED*/
_citrus_NONE_ctype_mbsnrtowcs(_citrus_ctype_rec_t * __restrict cc,wchar_t * __restrict pwcs,const char ** __restrict s,size_t in,size_t n,void * __restrict pspriv,size_t * __restrict nresult)189 _citrus_NONE_ctype_mbsnrtowcs(_citrus_ctype_rec_t * __restrict cc,
190 			     wchar_t * __restrict pwcs,
191 			     const char ** __restrict s, size_t in, size_t n,
192 			     void * __restrict pspriv,
193 			     size_t * __restrict nresult)
194 {
195 	int cnt;
196 	const char *s0;
197 
198 	/* if pwcs is NULL, ignore n */
199 	if (pwcs == NULL)
200 		n = 1; /* arbitrary >0 value */
201 
202 	cnt = 0;
203 	s0 = *s; /* to keep *s unchanged for now, use copy instead. */
204 	while (in > 0 && n > 0) {
205 		if (pwcs != NULL) {
206 			*pwcs = (wchar_t)(unsigned char)*s0;
207 		}
208 		if (*s0 == '\0') {
209 			s0 = NULL;
210 			break;
211 		}
212 		s0++;
213 		--in;
214 		if (pwcs != NULL) {
215 			pwcs++;
216 			n--;
217 		}
218 		cnt++;
219 	}
220 	if (pwcs)
221 		*s = s0;
222 
223 	*nresult = (size_t)cnt;
224 
225 	return (0);
226 }
227 
228 static int
_citrus_NONE_ctype_mbstowcs(void * __restrict cl,wchar_t * __restrict wcs,const char * __restrict s,size_t n,size_t * __restrict nresult)229 _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs,
230 			    const char * __restrict s, size_t n,
231 			    size_t * __restrict nresult)
232 {
233 	const char *rs = s;
234 
235 	return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, &rs, n, NULL, nresult));
236 }
237 
238 static int
239 /*ARGSUSED*/
_citrus_NONE_ctype_mbtowc(void * __restrict cl,wchar_t * __restrict pwc,const char * __restrict s,size_t n,int * __restrict nresult)240 _citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc,
241 			  const char * __restrict s, size_t n,
242 			  int * __restrict nresult)
243 {
244 
245 	if (s == NULL) {
246 		*nresult = 0; /* state independent */
247 		return (0);
248 	}
249 	if (n == 0) {
250 		return (EILSEQ);
251 	}
252 	if (pwc == NULL) {
253 		if (*s == '\0') {
254 			*nresult = 0;
255 		} else {
256 			*nresult = 1;
257 		}
258 		return (0);
259 	}
260 
261 	*pwc = (wchar_t)(unsigned char)*s;
262 	*nresult = *s == '\0' ? 0 : 1;
263 
264 	return (0);
265 }
266 
267 static int
268 /*ARGSUSED*/
_citrus_NONE_ctype_wcrtomb(void * __restrict cl,char * __restrict s,wchar_t wc,void * __restrict pspriv,size_t * __restrict nresult)269 _citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s,
270 			   wchar_t wc, void * __restrict pspriv,
271 			   size_t * __restrict nresult)
272 {
273 	if ((wc&~0xFFU) != 0) {
274 		*nresult = (size_t)-1;
275 		return (EILSEQ);
276 	}
277 
278 	*nresult = 1;
279 	if (s!=NULL)
280 		*s = (char)wc;
281 
282 	return (0);
283 }
284 
285 static int
286 /*ARGSUSED*/
_citrus_NONE_ctype_wcsrtombs(void * __restrict cl,char * __restrict s,const wchar_t ** __restrict pwcs,size_t n,void * __restrict pspriv,size_t * __restrict nresult)287 _citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s,
288 			     const wchar_t ** __restrict pwcs, size_t n,
289 			     void * __restrict pspriv,
290 			     size_t * __restrict nresult)
291 {
292 	size_t count;
293 	const wchar_t *pwcs0;
294 
295 	pwcs0 = *pwcs;
296 	count = 0;
297 
298 	if (s == NULL)
299 		n = 1;
300 
301 	while (n > 0) {
302 		if ((*pwcs0 & ~0xFFU) != 0) {
303 			*nresult = (size_t)-1;
304 			return (EILSEQ);
305 		}
306 		if (s != NULL) {
307 			*s++ = (char)*pwcs0;
308 			n--;
309 		}
310 		if (*pwcs0 == L'\0') {
311 			pwcs0 = NULL;
312 			break;
313 		}
314 		count++;
315 		pwcs0++;
316 	}
317 	if (s != NULL)
318 		*pwcs = pwcs0;
319 
320 	*nresult = count;
321 
322 	return (0);
323 }
324 
325 static int
326 /*ARGSUSED*/
_citrus_NONE_ctype_wcsnrtombs(_citrus_ctype_rec_t * __restrict cc,char * __restrict s,const wchar_t ** __restrict pwcs,size_t in,size_t n,void * __restrict pspriv,size_t * __restrict nresult)327 _citrus_NONE_ctype_wcsnrtombs(_citrus_ctype_rec_t * __restrict cc,
328 			     char * __restrict s,
329 			     const wchar_t ** __restrict pwcs, size_t in,
330 			     size_t n, void * __restrict pspriv,
331 			     size_t * __restrict nresult)
332 {
333 	size_t count;
334 	const wchar_t *pwcs0;
335 
336 	pwcs0 = *pwcs;
337 	count = 0;
338 
339 	if (s == NULL)
340 		n = 1;
341 
342 	while (in > 0 && n > 0) {
343 		if ((*pwcs0 & ~0xFFU) != 0) {
344 			*nresult = (size_t)-1;
345 			return (EILSEQ);
346 		}
347 		if (s != NULL) {
348 			*s++ = (char)*pwcs0;
349 			n--;
350 		}
351 		if (*pwcs0 == L'\0') {
352 			pwcs0 = NULL;
353 			break;
354 		}
355 		count++;
356 		pwcs0++;
357 		--in;
358 	}
359 	if (s != NULL)
360 		*pwcs = pwcs0;
361 
362 	*nresult = count;
363 
364 	return (0);
365 }
366 
367 static int
_citrus_NONE_ctype_wcstombs(void * __restrict cl,char * __restrict s,const wchar_t * __restrict pwcs,size_t n,size_t * __restrict nresult)368 _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s,
369 			    const wchar_t * __restrict pwcs, size_t n,
370 			    size_t * __restrict nresult)
371 {
372 	const wchar_t *rpwcs = pwcs;
373 
374 	return (_citrus_NONE_ctype_wcsrtombs(cl, s, &rpwcs, n, NULL, nresult));
375 }
376 
377 static int
_citrus_NONE_ctype_wctomb(void * __restrict cl,char * __restrict s,wchar_t wc,int * __restrict nresult)378 _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s,
379 			  wchar_t wc, int * __restrict nresult)
380 {
381 	int ret;
382 	size_t nr;
383 
384 	if (s == 0) {
385 		/*
386 		 * initialize state here.
387 		 * (nothing to do for us.)
388 		 */
389 		*nresult = 0; /* we're state independent */
390 		return (0);
391 	}
392 
393 	ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr);
394 	*nresult = (int)nr;
395 
396 	return (ret);
397 }
398 
399 static int
400 /*ARGSUSED*/
_citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc,int c,wint_t * __restrict wcresult)401 _citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc,
402 			 int c, wint_t * __restrict wcresult)
403 {
404 	if (c == EOF || c & ~0xFF)
405 		*wcresult = WEOF;
406 	else
407 		*wcresult = (wint_t)c;
408 	return (0);
409 }
410 
411 static int
412 /*ARGSUSED*/
_citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc,wint_t wc,int * __restrict cresult)413 _citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc,
414 			 wint_t wc, int * __restrict cresult)
415 {
416 	if (wc == WEOF || wc & ~0xFF)
417 		*cresult = EOF;
418 	else
419 		*cresult = (int)wc;
420 	return (0);
421 }
422 
423 /* ---------------------------------------------------------------------- */
424 
425 _CITRUS_STDENC_DECLS(NONE);
426 _CITRUS_STDENC_DEF_OPS(NONE);
427 struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = {
428 	0,	/* et_state_size */
429 	1,	/* mb_cur_max */
430 };
431 
432 static int
433 /*ARGSUSED*/
_citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce,const void * var,size_t lenvar,struct _citrus_stdenc_traits * __restrict et)434 _citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce,
435 			 const void *var, size_t lenvar,
436 			 struct _citrus_stdenc_traits * __restrict et)
437 {
438 
439 	et->et_state_size = 0;
440 	et->et_mb_cur_max = 1;
441 
442 	ce->ce_closure = NULL;
443 
444 	return (0);
445 }
446 
447 static void
448 /*ARGSUSED*/
_citrus_NONE_stdenc_uninit(struct _citrus_stdenc * ce)449 _citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce)
450 {
451 }
452 
453 static int
454 /*ARGSUSED*/
_citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce,void * __restrict ps)455 _citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce,
456 			       void * __restrict ps)
457 {
458 	return (0);
459 }
460 
461 static int
462 /*ARGSUSED*/
_citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce,_csid_t * csid,_index_t * idx,const char ** s,size_t n,void * ps,size_t * nresult)463 _citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce,
464 			   _csid_t *csid, _index_t *idx,
465 			   const char **s, size_t n,
466 			   void *ps, size_t *nresult)
467 {
468 
469 	_DIAGASSERT(csid != NULL && idx != NULL);
470 
471 	if (n<1) {
472 		*nresult = (size_t)-2;
473 		return (0);
474 	}
475 
476 	*csid = 0;
477 	*idx = (_index_t)(unsigned char)*(*s)++;
478 	*nresult = *idx == 0 ? 0 : 1;
479 
480 	return (0);
481 }
482 
483 static int
484 /*ARGSUSED*/
_citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce,char * s,size_t n,_csid_t csid,_index_t idx,void * ps,size_t * nresult)485 _citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce,
486 			   char *s, size_t n,
487 			   _csid_t csid, _index_t idx,
488 			   void *ps, size_t *nresult)
489 {
490 
491 	if (csid == _CITRUS_CSID_INVALID) {
492 		*nresult = 0;
493 		return (0);
494 	}
495 	if (n<1) {
496 		*nresult = (size_t)-1;
497 		return (E2BIG);
498 	}
499 	if (csid != 0 || (idx&0xFF) != idx)
500 		return (EILSEQ);
501 
502 	*s = (char)idx;
503 	*nresult = 1;
504 
505 	return (0);
506 }
507 
508 static int
509 /*ARGSUSED*/
_citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce,_wc_t * __restrict pwc,const char ** __restrict s,size_t n,void * __restrict pspriv,size_t * __restrict nresult)510 _citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce,
511 			   _wc_t * __restrict pwc,
512 			   const char ** __restrict s, size_t n,
513 			   void * __restrict pspriv,
514 			   size_t * __restrict nresult)
515 {
516 	if (s == NULL) {
517 		*nresult = 0;
518 		return (0);
519 	}
520 	if (n == 0) {
521 		*nresult = (size_t)-2;
522 		return (0);
523 	}
524 
525 	if (pwc != NULL)
526 		*pwc = (_wc_t)(unsigned char) **s;
527 
528 	*nresult = *s == '\0' ? 0 : 1;
529 	return (0);
530 }
531 
532 static int
533 /*ARGSUSED*/
_citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce,char * __restrict s,size_t n,_wc_t wc,void * __restrict pspriv,size_t * __restrict nresult)534 _citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce,
535 			   char * __restrict s, size_t n,
536 			   _wc_t wc, void * __restrict pspriv,
537 			   size_t * __restrict nresult)
538 {
539 	if ((wc&~0xFFU) != 0) {
540 		*nresult = (size_t)-1;
541 		return (EILSEQ);
542 	}
543 	if (n==0) {
544 		*nresult = (size_t)-1;
545 		return (E2BIG);
546 	}
547 
548 	*nresult = 1;
549 	if (s!=NULL && n>0)
550 		*s = (char)wc;
551 
552 	return (0);
553 }
554 
555 static int
556 /*ARGSUSED*/
_citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce,char * __restrict s,size_t n,void * __restrict pspriv,size_t * __restrict nresult)557 _citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce,
558 				    char * __restrict s, size_t n,
559 				    void * __restrict pspriv,
560 				    size_t * __restrict nresult)
561 {
562 
563 	*nresult = 0;
564 
565 	return (0);
566 }
567 
568 static int
569 /*ARGSUSED*/
_citrus_NONE_stdenc_get_state_desc(struct _stdenc * __restrict ce,void * __restrict ps,int id,struct _stdenc_state_desc * __restrict d)570 _citrus_NONE_stdenc_get_state_desc(struct _stdenc * __restrict ce,
571 				   void * __restrict ps,
572 				   int id,
573 				   struct _stdenc_state_desc * __restrict d)
574 {
575 	int ret = 0;
576 
577 	switch (id) {
578 	case _STDENC_SDID_GENERIC:
579 		d->u.generic.state = _STDENC_SDGEN_INITIAL;
580 		break;
581 	default:
582 		ret = EOPNOTSUPP;
583 	}
584 
585 	return ret;
586 }
587