xref: /openbsd-src/lib/libc/citrus/citrus_none.c (revision a28d8b979ac4c519555d4516e03a6accff77d089)
1*a28d8b97Smartijn /*	$OpenBSD: citrus_none.c,v 1.11 2021/05/08 09:38:29 martijn Exp $ */
2c9b8e388Sstsp /*	$NetBSD: citrus_none.c,v 1.18 2008/06/14 16:01:07 tnozaki Exp $	*/
3c9b8e388Sstsp 
4c9b8e388Sstsp /*-
5c9b8e388Sstsp  * Copyright (c)2002 Citrus Project,
6c9b8e388Sstsp  * All rights reserved.
7c9b8e388Sstsp  *
8c9b8e388Sstsp  * Redistribution and use in source and binary forms, with or without
9c9b8e388Sstsp  * modification, are permitted provided that the following conditions
10c9b8e388Sstsp  * are met:
11c9b8e388Sstsp  * 1. Redistributions of source code must retain the above copyright
12c9b8e388Sstsp  *    notice, this list of conditions and the following disclaimer.
13c9b8e388Sstsp  * 2. Redistributions in binary form must reproduce the above copyright
14c9b8e388Sstsp  *    notice, this list of conditions and the following disclaimer in the
15c9b8e388Sstsp  *    documentation and/or other materials provided with the distribution.
16c9b8e388Sstsp  *
17c9b8e388Sstsp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c9b8e388Sstsp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c9b8e388Sstsp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c9b8e388Sstsp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c9b8e388Sstsp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c9b8e388Sstsp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c9b8e388Sstsp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c9b8e388Sstsp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c9b8e388Sstsp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c9b8e388Sstsp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c9b8e388Sstsp  * SUCH DAMAGE.
28c9b8e388Sstsp  */
29c9b8e388Sstsp 
30c9b8e388Sstsp #include <sys/types.h>
31c9b8e388Sstsp 
32c9b8e388Sstsp #include <errno.h>
33c9b8e388Sstsp #include <string.h>
34c9b8e388Sstsp #include <wchar.h>
35c9b8e388Sstsp 
36c9b8e388Sstsp #include "citrus_ctype.h"
37c9b8e388Sstsp 
38*a28d8b97Smartijn static char	 wrapv(unsigned char);
39*a28d8b97Smartijn 
40*a28d8b97Smartijn 
41*a28d8b97Smartijn /*
42*a28d8b97Smartijn  * Convert an unsigned char value into a char value without relying on
43*a28d8b97Smartijn  * signed overflow behavior.
44*a28d8b97Smartijn  */
45*a28d8b97Smartijn static char
wrapv(unsigned char ch)46*a28d8b97Smartijn wrapv(unsigned char ch)
47*a28d8b97Smartijn {
48*a28d8b97Smartijn 	if (ch >= 0x80)
49*a28d8b97Smartijn 		return (int)ch - 0x100;
50*a28d8b97Smartijn 	else
51*a28d8b97Smartijn 		return ch;
52*a28d8b97Smartijn }
53*a28d8b97Smartijn 
54c9b8e388Sstsp size_t
_citrus_none_ctype_mbrtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n)55c9b8e388Sstsp _citrus_none_ctype_mbrtowc(wchar_t * __restrict pwc,
56754110aeSschwarze     const char * __restrict s, size_t n)
57c9b8e388Sstsp {
58c9b8e388Sstsp 	if (s == NULL)
59c9b8e388Sstsp 		return 0;
60c9b8e388Sstsp 	if (n == 0)
618558486aSschwarze 		return -2;
628558486aSschwarze 	if (pwc != NULL)
63*a28d8b97Smartijn 		*pwc = (wchar_t)(unsigned char)*s;
648558486aSschwarze 	return *s != '\0';
65c9b8e388Sstsp }
66c9b8e388Sstsp 
67c9b8e388Sstsp size_t
_citrus_none_ctype_mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nmc,size_t len)685a08728eSmatthew _citrus_none_ctype_mbsnrtowcs(wchar_t * __restrict dst,
69754110aeSschwarze     const char ** __restrict src, size_t nmc, size_t len)
70c9b8e388Sstsp {
715a08728eSmatthew 	size_t i;
72c9b8e388Sstsp 
735a08728eSmatthew 	if (dst == NULL)
745a08728eSmatthew 		return strnlen(*src, nmc);
75c9b8e388Sstsp 
765a08728eSmatthew 	for (i = 0; i < nmc && i < len; i++)
77*a28d8b97Smartijn 		if ((dst[i] = (wchar_t)(unsigned char)(*src)[i]) == L'\0') {
785a08728eSmatthew 			*src = NULL;
798558486aSschwarze 			return i;
80c9b8e388Sstsp 		}
81c9b8e388Sstsp 
825a08728eSmatthew 	*src += i;
838558486aSschwarze 	return i;
84c9b8e388Sstsp }
85c9b8e388Sstsp 
86c9b8e388Sstsp size_t
_citrus_none_ctype_wcrtomb(char * __restrict s,wchar_t wc)87754110aeSschwarze _citrus_none_ctype_wcrtomb(char * __restrict s, wchar_t wc)
88c9b8e388Sstsp {
89c9b8e388Sstsp 	if (s == NULL)
908558486aSschwarze 		return 1;
91c9b8e388Sstsp 
92*a28d8b97Smartijn 	if (wc < 0 || wc > 0xff) {
935a08728eSmatthew 		errno = EILSEQ;
948558486aSschwarze 		return -1;
955a08728eSmatthew 	}
965a08728eSmatthew 
97*a28d8b97Smartijn 	*s = wrapv(wc);
988558486aSschwarze 	return 1;
99c9b8e388Sstsp }
100c9b8e388Sstsp 
101c9b8e388Sstsp size_t
_citrus_none_ctype_wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len)1025a08728eSmatthew _citrus_none_ctype_wcsnrtombs(char * __restrict dst,
103754110aeSschwarze     const wchar_t ** __restrict src, size_t nwc, size_t len)
104c9b8e388Sstsp {
1055a08728eSmatthew 	size_t i;
106c9b8e388Sstsp 
1075a08728eSmatthew 	if (dst == NULL) {
1085a08728eSmatthew 		for (i = 0; i < nwc; i++) {
1095a08728eSmatthew 			wchar_t wc = (*src)[i];
110*a28d8b97Smartijn 			if (wc < 0 || wc > 0xff) {
1115a08728eSmatthew 				errno = EILSEQ;
1128558486aSschwarze 				return -1;
1135a08728eSmatthew 			}
1145a08728eSmatthew 			if (wc == L'\0')
1158558486aSschwarze 				return i;
1165a08728eSmatthew 		}
1178558486aSschwarze 		return i;
118c9b8e388Sstsp 	}
119c9b8e388Sstsp 
1205a08728eSmatthew 	for (i = 0; i < nwc && i < len; i++) {
1215a08728eSmatthew 		wchar_t wc = (*src)[i];
122*a28d8b97Smartijn 		if (wc < 0 || wc > 0xff) {
1235a08728eSmatthew 			*src += i;
1245a08728eSmatthew 			errno = EILSEQ;
1258558486aSschwarze 			return -1;
126c9b8e388Sstsp 		}
127*a28d8b97Smartijn 		dst[i] = wrapv(wc);
1285a08728eSmatthew 		if (wc == L'\0') {
1295a08728eSmatthew 			*src = NULL;
1308558486aSschwarze 			return i;
1315a08728eSmatthew 		}
1325a08728eSmatthew 	}
1335a08728eSmatthew 	*src += i;
1348558486aSschwarze 	return i;
135c9b8e388Sstsp }
136