xref: /dragonfly/lib/libc/locale/none.c (revision 5dcdf778)
14776d4e8SJohn Marino /*
24776d4e8SJohn Marino  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
34776d4e8SJohn Marino  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
40d5acd74SJohn Marino  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
50d5acd74SJohn Marino  * Copyright (c) 1993
60d5acd74SJohn Marino  *	The Regents of the University of California.  All rights reserved.
70d5acd74SJohn Marino  *
80d5acd74SJohn Marino  * This code is derived from software contributed to Berkeley by
90d5acd74SJohn Marino  * Paul Borman at Krystal Technologies.
100d5acd74SJohn Marino  *
110d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
120d5acd74SJohn Marino  * All rights reserved.
130d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
140d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
150d5acd74SJohn Marino  *
160d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
170d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
180d5acd74SJohn Marino  * are met:
190d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
200d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
210d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
220d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
230d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
24c66c7e2fSzrj  * 3. Neither the name of the University nor the names of its contributors
250d5acd74SJohn Marino  *    may be used to endorse or promote products derived from this software
260d5acd74SJohn Marino  *    without specific prior written permission.
270d5acd74SJohn Marino  *
280d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
290d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
300d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
310d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
320d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
330d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
340d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
350d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
360d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
370d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
380d5acd74SJohn Marino  * SUCH DAMAGE.
390d5acd74SJohn Marino  *
400d5acd74SJohn Marino  * @(#)none.c	8.1 (Berkeley) 6/4/93
410d5acd74SJohn Marino  */
420d5acd74SJohn Marino 
430d5acd74SJohn Marino #include <errno.h>
440d5acd74SJohn Marino #include <limits.h>
450d5acd74SJohn Marino #include <runetype.h>
460d5acd74SJohn Marino #include <stddef.h>
470d5acd74SJohn Marino #include <stdio.h>
480d5acd74SJohn Marino #include <stdlib.h>
490d5acd74SJohn Marino #include <string.h>
500d5acd74SJohn Marino #include <wchar.h>
510d5acd74SJohn Marino #include "mblocal.h"
520d5acd74SJohn Marino 
530d5acd74SJohn Marino static size_t	_none_mbrtowc(wchar_t * __restrict, const char * __restrict,
540d5acd74SJohn Marino 		    size_t, mbstate_t * __restrict);
550d5acd74SJohn Marino static int	_none_mbsinit(const mbstate_t *);
560d5acd74SJohn Marino static size_t	_none_mbsnrtowcs(wchar_t * __restrict dst,
570d5acd74SJohn Marino 		    const char ** __restrict src, size_t nms, size_t len,
580d5acd74SJohn Marino 		    mbstate_t * __restrict ps __unused);
590d5acd74SJohn Marino static size_t	_none_wcrtomb(char * __restrict, wchar_t,
600d5acd74SJohn Marino 		    mbstate_t * __restrict);
610d5acd74SJohn Marino static size_t	_none_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
620d5acd74SJohn Marino 		    size_t, size_t, mbstate_t * __restrict);
630d5acd74SJohn Marino 
640d5acd74SJohn Marino /* setup defaults */
650d5acd74SJohn Marino 
660d5acd74SJohn Marino int __mb_cur_max = 1;
670d5acd74SJohn Marino int __mb_sb_limit = 256; /* Expected to be <= _CACHED_RUNES */
680d5acd74SJohn Marino 
690d5acd74SJohn Marino int
_none_init(struct xlocale_ctype * l,_RuneLocale * rl)700d5acd74SJohn Marino _none_init(struct xlocale_ctype *l, _RuneLocale *rl)
710d5acd74SJohn Marino {
720d5acd74SJohn Marino 
730d5acd74SJohn Marino 	l->__mbrtowc = _none_mbrtowc;
740d5acd74SJohn Marino 	l->__mbsinit = _none_mbsinit;
750d5acd74SJohn Marino 	l->__mbsnrtowcs = _none_mbsnrtowcs;
760d5acd74SJohn Marino 	l->__wcrtomb = _none_wcrtomb;
770d5acd74SJohn Marino 	l->__wcsnrtombs = _none_wcsnrtombs;
780d5acd74SJohn Marino 	l->runes = rl;
790d5acd74SJohn Marino 	l->__mb_cur_max = 1;
800d5acd74SJohn Marino 	l->__mb_sb_limit = 256;
810d5acd74SJohn Marino 	return(0);
820d5acd74SJohn Marino }
830d5acd74SJohn Marino 
840d5acd74SJohn Marino static int
_none_mbsinit(const mbstate_t * ps __unused)850d5acd74SJohn Marino _none_mbsinit(const mbstate_t *ps __unused)
860d5acd74SJohn Marino {
870d5acd74SJohn Marino 
880d5acd74SJohn Marino 	/*
890d5acd74SJohn Marino 	 * Encoding is not state dependent - we are always in the
900d5acd74SJohn Marino 	 * initial state.
910d5acd74SJohn Marino 	 */
920d5acd74SJohn Marino 	return (1);
930d5acd74SJohn Marino }
940d5acd74SJohn Marino 
950d5acd74SJohn Marino static size_t
_none_mbrtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n,mbstate_t * __restrict ps __unused)960d5acd74SJohn Marino _none_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
970d5acd74SJohn Marino     mbstate_t * __restrict ps __unused)
980d5acd74SJohn Marino {
990d5acd74SJohn Marino 
1000d5acd74SJohn Marino 	if (s == NULL)
1010d5acd74SJohn Marino 		/* Reset to initial shift state (no-op) */
1020d5acd74SJohn Marino 		return (0);
1030d5acd74SJohn Marino 	if (n == 0)
1040d5acd74SJohn Marino 		/* Incomplete multibyte sequence */
1050d5acd74SJohn Marino 		return ((size_t)-2);
1060d5acd74SJohn Marino 	if (pwc != NULL)
1070d5acd74SJohn Marino 		*pwc = (unsigned char)*s;
1080d5acd74SJohn Marino 	return (*s == '\0' ? 0 : 1);
1090d5acd74SJohn Marino }
1100d5acd74SJohn Marino 
1110d5acd74SJohn Marino static size_t
_none_wcrtomb(char * __restrict s,wchar_t wc,mbstate_t * __restrict ps __unused)1120d5acd74SJohn Marino _none_wcrtomb(char * __restrict s, wchar_t wc,
1130d5acd74SJohn Marino     mbstate_t * __restrict ps __unused)
1140d5acd74SJohn Marino {
1150d5acd74SJohn Marino 
1160d5acd74SJohn Marino 	if (s == NULL)
1170d5acd74SJohn Marino 		/* Reset to initial shift state (no-op) */
1180d5acd74SJohn Marino 		return (1);
1190d5acd74SJohn Marino 	if (wc < 0 || wc > UCHAR_MAX) {
1200d5acd74SJohn Marino 		errno = EILSEQ;
1210d5acd74SJohn Marino 		return ((size_t)-1);
1220d5acd74SJohn Marino 	}
1230d5acd74SJohn Marino 	*s = (unsigned char)wc;
1240d5acd74SJohn Marino 	return (1);
1250d5acd74SJohn Marino }
1260d5acd74SJohn Marino 
1270d5acd74SJohn Marino static size_t
_none_mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps __unused)1280d5acd74SJohn Marino _none_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
1290d5acd74SJohn Marino     size_t nms, size_t len, mbstate_t * __restrict ps __unused)
1300d5acd74SJohn Marino {
1310d5acd74SJohn Marino 	const char *s;
1320d5acd74SJohn Marino 	size_t nchr;
1330d5acd74SJohn Marino 
1340d5acd74SJohn Marino 	if (dst == NULL) {
1350d5acd74SJohn Marino 		s = memchr(*src, '\0', nms);
1360d5acd74SJohn Marino 		return (s != NULL ? s - *src : nms);
1370d5acd74SJohn Marino 	}
1380d5acd74SJohn Marino 
1390d5acd74SJohn Marino 	s = *src;
1400d5acd74SJohn Marino 	nchr = 0;
1410d5acd74SJohn Marino 	while (len-- > 0 && nms-- > 0) {
1420d5acd74SJohn Marino 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
1430d5acd74SJohn Marino 			*src = NULL;
1440d5acd74SJohn Marino 			return (nchr);
1450d5acd74SJohn Marino 		}
1460d5acd74SJohn Marino 		nchr++;
1470d5acd74SJohn Marino 	}
1480d5acd74SJohn Marino 	*src = s;
1490d5acd74SJohn Marino 	return (nchr);
1500d5acd74SJohn Marino }
1510d5acd74SJohn Marino 
1520d5acd74SJohn Marino static size_t
_none_wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps __unused)1530d5acd74SJohn Marino _none_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
1540d5acd74SJohn Marino     size_t nwc, size_t len, mbstate_t * __restrict ps __unused)
1550d5acd74SJohn Marino {
1560d5acd74SJohn Marino 	const wchar_t *s;
1570d5acd74SJohn Marino 	size_t nchr;
1580d5acd74SJohn Marino 
1590d5acd74SJohn Marino 	if (dst == NULL) {
1600d5acd74SJohn Marino 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
1610d5acd74SJohn Marino 			if (*s < 0 || *s > UCHAR_MAX) {
1620d5acd74SJohn Marino 				errno = EILSEQ;
1630d5acd74SJohn Marino 				return ((size_t)-1);
1640d5acd74SJohn Marino 			}
1650d5acd74SJohn Marino 		}
1660d5acd74SJohn Marino 		return (s - *src);
1670d5acd74SJohn Marino 	}
1680d5acd74SJohn Marino 
1690d5acd74SJohn Marino 	s = *src;
1700d5acd74SJohn Marino 	nchr = 0;
1710d5acd74SJohn Marino 	while (len-- > 0 && nwc-- > 0) {
1720d5acd74SJohn Marino 		if (*s < 0 || *s > UCHAR_MAX) {
1739915e371SJohn Marino 			*src = s;
1740d5acd74SJohn Marino 			errno = EILSEQ;
1750d5acd74SJohn Marino 			return ((size_t)-1);
1760d5acd74SJohn Marino 		}
1770d5acd74SJohn Marino 		if ((*dst++ = *s++) == '\0') {
1780d5acd74SJohn Marino 			*src = NULL;
1790d5acd74SJohn Marino 			return (nchr);
1800d5acd74SJohn Marino 		}
1810d5acd74SJohn Marino 		nchr++;
1820d5acd74SJohn Marino 	}
1830d5acd74SJohn Marino 	*src = s;
1840d5acd74SJohn Marino 	return (nchr);
1850d5acd74SJohn Marino }
1860d5acd74SJohn Marino 
1878a84c799SMatthew Dillon /*
1888a84c799SMatthew Dillon  * Multibyte binary data to escaped wchar.
1898a84c799SMatthew Dillon  * Round-trip match guaranteed, including 0x00 bytes.
1908a84c799SMatthew Dillon  *
1918a84c799SMatthew Dillon  * Cannot return an error.  *slen bytes is converted to the destination
1928a84c799SMatthew Dillon  * buffer until one or the other is exhausted.  Destination elements returned
1938a84c799SMatthew Dillon  * and *slen modified with source elements processed.
1948a84c799SMatthew Dillon  *
1958a84c799SMatthew Dillon  * Incomplete sequences or partial re-encodings that would overflow the
1968a84c799SMatthew Dillon  * destination buffer are not processed and will also leave excess *slen.
1978a84c799SMatthew Dillon  *
1988a84c799SMatthew Dillon  * Never returns an error.  Instead, incomplete sequences are not processed
1998a84c799SMatthew Dillon  * and *slen will index to the beginning of the incomplete sequence.  It is
2008a84c799SMatthew Dillon  * possible for 0 to be returned and for *slen to be set to 0 due to an
2018a84c799SMatthew Dillon  * incomplete whole-buffer sequence, unless termination is specified.
2028a84c799SMatthew Dillon  *
2038a84c799SMatthew Dillon  * If termination is specified any trailing incomplete sequences are escaped
2048a84c799SMatthew Dillon  * and *slen will index to the end of the source buffer, unless insufficient
2058a84c799SMatthew Dillon  * room exists in the destination.  If there is insufficient room, *slen may
2068a84c799SMatthew Dillon  * not be able to index to the end of the source buffer.
2078a84c799SMatthew Dillon  *
2088a84c799SMatthew Dillon  * Does not support a NULL dst on purpose - caller is expected to loop
2098a84c799SMatthew Dillon  * in parts.
2108a84c799SMatthew Dillon  */
2118a84c799SMatthew Dillon static size_t
_none_mbintowcr(wchar_t * __restrict dst,const char * __restrict src,size_t dlen,size_t * slen,int flags __unused)2128a84c799SMatthew Dillon _none_mbintowcr(wchar_t * __restrict dst, const char * __restrict src,
213*5dcdf778SSascha Wildner 		size_t dlen, size_t *slen, int flags __unused)
2148a84c799SMatthew Dillon {
2158a84c799SMatthew Dillon 	size_t i;
2168a84c799SMatthew Dillon 	size_t j;
2178a84c799SMatthew Dillon 	size_t n = *slen;
2188a84c799SMatthew Dillon 
2198a84c799SMatthew Dillon 	for (i = j = 0; i < n; ++i) {
2208a84c799SMatthew Dillon 		if (j == dlen)
2218a84c799SMatthew Dillon 			break;
2228a84c799SMatthew Dillon 		if (dst)
2238a84c799SMatthew Dillon 			dst[j] = (unsigned char)src[i];
2248a84c799SMatthew Dillon 		++j;
2258a84c799SMatthew Dillon 	}
2268a84c799SMatthew Dillon 	/* no partial sequences so we can ignore flags */
2278a84c799SMatthew Dillon 	*slen = i;
2288a84c799SMatthew Dillon 
2298a84c799SMatthew Dillon 	return j;
2308a84c799SMatthew Dillon }
2318a84c799SMatthew Dillon 
2328a84c799SMatthew Dillon /*
2338a84c799SMatthew Dillon  * Escaped wchar to multibyte binary data.
2348a84c799SMatthew Dillon  * Round-trip match guaranteed, including 0x00 bytes.
2358a84c799SMatthew Dillon  *
2368a84c799SMatthew Dillon  * *slen bytes is converted to the destination buffer until one or the other
2378a84c799SMatthew Dillon  * is exhausted.  Destination elements returned and *slen modified with
2388a84c799SMatthew Dillon  * source elements processed.
2398a84c799SMatthew Dillon  *
2408a84c799SMatthew Dillon  * Can return an error only if the first wchar src[] element is illegal,
2418a84c799SMatthew Dillon  * otherwise will process up to the illegal wchar and return an error on
2428a84c799SMatthew Dillon  * the next call (if called with the remainder).
2438a84c799SMatthew Dillon  *
2448a84c799SMatthew Dillon  * Never returns -2.  Instead, incomplete sequences are not processed and
2458a84c799SMatthew Dillon  * *slen will index to the beginning of the incomplete sequence.  If
2468a84c799SMatthew Dillon  * termination is specified, incomplete sequences are discarded and *slen
2478a84c799SMatthew Dillon  * indexes to the end of the input array.
2488a84c799SMatthew Dillon  *
2498a84c799SMatthew Dillon  * Does not support a NULL dst on purpose - caller is expected to loop
2508a84c799SMatthew Dillon  * in parts.
2518a84c799SMatthew Dillon  */
2528a84c799SMatthew Dillon static size_t
_none_wcrtombin(char * __restrict dst,const wchar_t * __restrict src,size_t dlen,size_t * slen,int flags __unused)2538a84c799SMatthew Dillon _none_wcrtombin(char * __restrict dst, const wchar_t * __restrict src,
254*5dcdf778SSascha Wildner 		size_t dlen, size_t *slen, int flags __unused)
2558a84c799SMatthew Dillon {
2568a84c799SMatthew Dillon 	size_t i;
2578a84c799SMatthew Dillon 	size_t j;
2588a84c799SMatthew Dillon 	size_t n = *slen;
2598a84c799SMatthew Dillon 
2608a84c799SMatthew Dillon 	for (i = j = 0; i < n; ++i) {
2618a84c799SMatthew Dillon 		if (j == dlen)
2628a84c799SMatthew Dillon 			break;
2638a84c799SMatthew Dillon 		if (src[i] >= 0x100) {
2648a84c799SMatthew Dillon 			if (i == 0) {
2658a84c799SMatthew Dillon 				errno = EILSEQ;
2668a84c799SMatthew Dillon 				return(-1);
2678a84c799SMatthew Dillon 			}
2688a84c799SMatthew Dillon 			break;
2698a84c799SMatthew Dillon 		}
2708a84c799SMatthew Dillon 		if (dst)
2718a84c799SMatthew Dillon 			dst[j] = (unsigned char)src[i];
2728a84c799SMatthew Dillon 		++j;
2738a84c799SMatthew Dillon 	}
2748a84c799SMatthew Dillon 	/* no partial sequences so we can ignore flags */
2758a84c799SMatthew Dillon 	*slen = i;
2768a84c799SMatthew Dillon 
2778a84c799SMatthew Dillon 	return j;
2788a84c799SMatthew Dillon }
2798a84c799SMatthew Dillon 
2800d5acd74SJohn Marino /* setup defaults */
2810d5acd74SJohn Marino 
2820d5acd74SJohn Marino struct xlocale_ctype __xlocale_global_ctype = {
2830d5acd74SJohn Marino 	{{0}, "C"},
2840d5acd74SJohn Marino 	(_RuneLocale*)&_DefaultRuneLocale,
2850d5acd74SJohn Marino 	_none_mbrtowc,
2860d5acd74SJohn Marino 	_none_mbsinit,
2870d5acd74SJohn Marino 	_none_mbsnrtowcs,
2880d5acd74SJohn Marino 	_none_wcrtomb,
2890d5acd74SJohn Marino 	_none_wcsnrtombs,
2908a84c799SMatthew Dillon 	_none_mbintowcr,
2918a84c799SMatthew Dillon 	_none_wcrtombin,
2920d5acd74SJohn Marino 	1, /* __mb_cur_max, */
2930d5acd74SJohn Marino 	256 /* __mb_sb_limit */
2940d5acd74SJohn Marino };
2950d5acd74SJohn Marino 
29652dc78afSJohn Marino struct xlocale_ctype __xlocale_C_ctype = {
2970d5acd74SJohn Marino 	{{0}, "C"},
2980d5acd74SJohn Marino 	(_RuneLocale*)&_DefaultRuneLocale,
2990d5acd74SJohn Marino 	_none_mbrtowc,
3000d5acd74SJohn Marino 	_none_mbsinit,
3010d5acd74SJohn Marino 	_none_mbsnrtowcs,
3020d5acd74SJohn Marino 	_none_wcrtomb,
3030d5acd74SJohn Marino 	_none_wcsnrtombs,
3048a84c799SMatthew Dillon 	_none_mbintowcr,
3058a84c799SMatthew Dillon 	_none_wcrtombin,
3060d5acd74SJohn Marino 	1, /* __mb_cur_max, */
3070d5acd74SJohn Marino 	256 /* __mb_sb_limit */
3080d5acd74SJohn Marino };
309