xref: /freebsd/lib/libc/locale/none.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
5  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
6  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
7  * Copyright (c) 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Paul Borman at Krystal Technologies.
12  *
13  * Copyright (c) 2011 The FreeBSD Foundation
14  *
15  * Portions of this software were developed by David Chisnall
16  * under sponsorship from the FreeBSD Foundation.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid[] = "@(#)none.c	8.1 (Berkeley) 6/4/93";
45 #endif /* LIBC_SCCS and not lint */
46 #include <errno.h>
47 #include <limits.h>
48 #include <runetype.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <wchar.h>
54 #include "mblocal.h"
55 
56 static size_t	_none_mbrtowc(wchar_t * __restrict, const char * __restrict,
57 		    size_t, mbstate_t * __restrict);
58 static int	_none_mbsinit(const mbstate_t *);
59 static size_t	_none_mbsnrtowcs(wchar_t * __restrict dst,
60 		    const char ** __restrict src, size_t nms, size_t len,
61 		    mbstate_t * __restrict ps __unused);
62 static size_t	_none_wcrtomb(char * __restrict, wchar_t,
63 		    mbstate_t * __restrict);
64 static size_t	_none_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
65 		    size_t, size_t, mbstate_t * __restrict);
66 
67 /* setup defaults */
68 
69 int __mb_cur_max = 1;
70 int __mb_sb_limit = 256; /* Expected to be <= _CACHED_RUNES */
71 
72 int
73 _none_init(struct xlocale_ctype *l, _RuneLocale *rl)
74 {
75 
76 	l->__mbrtowc = _none_mbrtowc;
77 	l->__mbsinit = _none_mbsinit;
78 	l->__mbsnrtowcs = _none_mbsnrtowcs;
79 	l->__wcrtomb = _none_wcrtomb;
80 	l->__wcsnrtombs = _none_wcsnrtombs;
81 	l->runes = rl;
82 	l->__mb_cur_max = 1;
83 	l->__mb_sb_limit = 256;
84 	return(0);
85 }
86 
87 static int
88 _none_mbsinit(const mbstate_t *ps __unused)
89 {
90 
91 	/*
92 	 * Encoding is not state dependent - we are always in the
93 	 * initial state.
94 	 */
95 	return (1);
96 }
97 
98 static size_t
99 _none_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
100     mbstate_t * __restrict ps __unused)
101 {
102 
103 	if (s == NULL)
104 		/* Reset to initial shift state (no-op) */
105 		return (0);
106 	if (n == 0)
107 		/* Incomplete multibyte sequence */
108 		return ((size_t)-2);
109 	if (pwc != NULL)
110 		*pwc = (unsigned char)*s;
111 	return (*s == '\0' ? 0 : 1);
112 }
113 
114 static size_t
115 _none_wcrtomb(char * __restrict s, wchar_t wc,
116     mbstate_t * __restrict ps __unused)
117 {
118 
119 	if (s == NULL)
120 		/* Reset to initial shift state (no-op) */
121 		return (1);
122 	if (wc < 0 || wc > UCHAR_MAX) {
123 		errno = EILSEQ;
124 		return ((size_t)-1);
125 	}
126 	*s = (unsigned char)wc;
127 	return (1);
128 }
129 
130 static size_t
131 _none_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
132     size_t nms, size_t len, mbstate_t * __restrict ps __unused)
133 {
134 	const char *s;
135 	size_t nchr;
136 
137 	if (dst == NULL) {
138 		s = memchr(*src, '\0', nms);
139 		return (s != NULL ? s - *src : nms);
140 	}
141 
142 	s = *src;
143 	nchr = 0;
144 	while (len-- > 0 && nms-- > 0) {
145 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
146 			*src = NULL;
147 			return (nchr);
148 		}
149 		nchr++;
150 	}
151 	*src = s;
152 	return (nchr);
153 }
154 
155 static size_t
156 _none_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
157     size_t nwc, size_t len, mbstate_t * __restrict ps __unused)
158 {
159 	const wchar_t *s;
160 	size_t nchr;
161 
162 	if (dst == NULL) {
163 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
164 			if (*s < 0 || *s > UCHAR_MAX) {
165 				errno = EILSEQ;
166 				return ((size_t)-1);
167 			}
168 		}
169 		return (s - *src);
170 	}
171 
172 	s = *src;
173 	nchr = 0;
174 	while (len-- > 0 && nwc-- > 0) {
175 		if (*s < 0 || *s > UCHAR_MAX) {
176 			*src = s;
177 			errno = EILSEQ;
178 			return ((size_t)-1);
179 		}
180 		if ((*dst++ = *s++) == '\0') {
181 			*src = NULL;
182 			return (nchr);
183 		}
184 		nchr++;
185 	}
186 	*src = s;
187 	return (nchr);
188 }
189 
190 /* setup defaults */
191 
192 struct xlocale_ctype __xlocale_global_ctype = {
193 	{{0}, "C"},
194 	(_RuneLocale*)&_DefaultRuneLocale,
195 	_none_mbrtowc,
196 	_none_mbsinit,
197 	_none_mbsnrtowcs,
198 	_none_wcrtomb,
199 	_none_wcsnrtombs,
200 	1, /* __mb_cur_max, */
201 	256 /* __mb_sb_limit */
202 };
203 
204 struct xlocale_ctype __xlocale_C_ctype = {
205 	{{0}, "C"},
206 	(_RuneLocale*)&_DefaultRuneLocale,
207 	_none_mbrtowc,
208 	_none_mbsinit,
209 	_none_mbsnrtowcs,
210 	_none_wcrtomb,
211 	_none_wcsnrtombs,
212 	1, /* __mb_cur_max, */
213 	256 /* __mb_sb_limit */
214 };
215