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