xref: /illumos-gate/usr/src/lib/libc/port/locale/none.c (revision 4a38094c)
14297a3b0SGarrett D'Amore /*
22d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
54297a3b0SGarrett D'Amore  * Copyright (c) 1993
64297a3b0SGarrett D'Amore  *	The Regents of the University of California.  All rights reserved.
74297a3b0SGarrett D'Amore  *
84297a3b0SGarrett D'Amore  * This code is derived from software contributed to Berkeley by
94297a3b0SGarrett D'Amore  * Paul Borman at Krystal Technologies.
104297a3b0SGarrett D'Amore  *
114297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
124297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
134297a3b0SGarrett D'Amore  * are met:
144297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
154297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
164297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
174297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
184297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
194297a3b0SGarrett D'Amore  * 4. Neither the name of the University nor the names of its contributors
204297a3b0SGarrett D'Amore  *    may be used to endorse or promote products derived from this software
214297a3b0SGarrett D'Amore  *    without specific prior written permission.
224297a3b0SGarrett D'Amore  *
234297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334297a3b0SGarrett D'Amore  * SUCH DAMAGE.
344297a3b0SGarrett D'Amore  */
354297a3b0SGarrett D'Amore 
364297a3b0SGarrett D'Amore #include "lint.h"
374297a3b0SGarrett D'Amore #include <errno.h>
384297a3b0SGarrett D'Amore #include <limits.h>
394297a3b0SGarrett D'Amore #include <stddef.h>
404297a3b0SGarrett D'Amore #include <stdio.h>
414297a3b0SGarrett D'Amore #include <stdlib.h>
424297a3b0SGarrett D'Amore #include <string.h>
434297a3b0SGarrett D'Amore #include <wchar.h>
444297a3b0SGarrett D'Amore #include "mblocal.h"
452d08521bSGarrett D'Amore #include "lctype.h"
464297a3b0SGarrett D'Amore 
474297a3b0SGarrett D'Amore /* setup defaults */
484297a3b0SGarrett D'Amore 
492d08521bSGarrett D'Amore void
_none_init(struct lc_ctype * lct)502d08521bSGarrett D'Amore _none_init(struct lc_ctype *lct)
514297a3b0SGarrett D'Amore {
522d08521bSGarrett D'Amore 	lct->lc_is_ascii = 1;
532d08521bSGarrett D'Amore 	lct->lc_mbrtowc = __mbrtowc_ascii;
542d08521bSGarrett D'Amore 	lct->lc_mbsinit = __mbsinit_ascii;
552d08521bSGarrett D'Amore 	lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
562d08521bSGarrett D'Amore 	lct->lc_wcrtomb = __wcrtomb_ascii;
572d08521bSGarrett D'Amore 	lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
582d08521bSGarrett D'Amore 	lct->lc_max_mblen = 1;
594297a3b0SGarrett D'Amore }
604297a3b0SGarrett D'Amore 
612d08521bSGarrett D'Amore int
__mbsinit_ascii(const mbstate_t * unused __unused)62*4a38094cSToomas Soome __mbsinit_ascii(const mbstate_t *unused __unused)
634297a3b0SGarrett D'Amore {
644297a3b0SGarrett D'Amore 	/*
654297a3b0SGarrett D'Amore 	 * Encoding is not state dependent - we are always in the
664297a3b0SGarrett D'Amore 	 * initial state.
674297a3b0SGarrett D'Amore 	 */
684297a3b0SGarrett D'Amore 	return (1);
694297a3b0SGarrett D'Amore }
704297a3b0SGarrett D'Amore 
712d08521bSGarrett D'Amore size_t
__mbrtowc_ascii(wchar_t * _RESTRICT_KYWD pwc,const char * _RESTRICT_KYWD s,size_t n,mbstate_t * _RESTRICT_KYWD unused __unused,boolean_t zero)722d08521bSGarrett D'Amore __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
73*4a38094cSToomas Soome     size_t n, mbstate_t *_RESTRICT_KYWD unused __unused, boolean_t zero)
744297a3b0SGarrett D'Amore {
754297a3b0SGarrett D'Amore 	if (s == NULL)
764297a3b0SGarrett D'Amore 		/* Reset to initial shift state (no-op) */
774297a3b0SGarrett D'Amore 		return (0);
784297a3b0SGarrett D'Amore 	if (n == 0)
794297a3b0SGarrett D'Amore 		/* Incomplete multibyte sequence */
804297a3b0SGarrett D'Amore 		return ((size_t)-2);
814297a3b0SGarrett D'Amore 	if (pwc != NULL)
824297a3b0SGarrett D'Amore 		*pwc = (unsigned char)*s;
830ac311baSRobert Mustacchi 	if (zero || *s != '\0') {
840ac311baSRobert Mustacchi 		return (1);
850ac311baSRobert Mustacchi 	} else {
860ac311baSRobert Mustacchi 		return (0);
870ac311baSRobert Mustacchi 	}
884297a3b0SGarrett D'Amore }
894297a3b0SGarrett D'Amore 
902d08521bSGarrett D'Amore size_t
__wcrtomb_ascii(char * _RESTRICT_KYWD s,wchar_t wc,mbstate_t * _RESTRICT_KYWD unused __unused)912d08521bSGarrett D'Amore __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
92*4a38094cSToomas Soome     mbstate_t *_RESTRICT_KYWD unused __unused)
934297a3b0SGarrett D'Amore {
944297a3b0SGarrett D'Amore 	if (s == NULL)
954297a3b0SGarrett D'Amore 		/* Reset to initial shift state (no-op) */
964297a3b0SGarrett D'Amore 		return (1);
974297a3b0SGarrett D'Amore 	if (wc < 0 || wc > UCHAR_MAX) {
984297a3b0SGarrett D'Amore 		errno = EILSEQ;
994297a3b0SGarrett D'Amore 		return ((size_t)-1);
1004297a3b0SGarrett D'Amore 	}
1014297a3b0SGarrett D'Amore 	*s = (unsigned char)wc;
1024297a3b0SGarrett D'Amore 	return (1);
1034297a3b0SGarrett D'Amore }
1044297a3b0SGarrett D'Amore 
1052d08521bSGarrett D'Amore size_t
__mbsnrtowcs_ascii(wchar_t * _RESTRICT_KYWD dst,const char ** _RESTRICT_KYWD src,size_t nms,size_t len,mbstate_t * _RESTRICT_KYWD unused __unused)1062d08521bSGarrett D'Amore __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
107*4a38094cSToomas Soome     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused __unused)
1084297a3b0SGarrett D'Amore {
1094297a3b0SGarrett D'Amore 	const char *s;
1104297a3b0SGarrett D'Amore 	size_t nchr;
1114297a3b0SGarrett D'Amore 
1124297a3b0SGarrett D'Amore 	if (dst == NULL) {
1134297a3b0SGarrett D'Amore 		s = memchr(*src, '\0', nms);
1144297a3b0SGarrett D'Amore 		return (s != NULL ? s - *src : nms);
1154297a3b0SGarrett D'Amore 	}
1164297a3b0SGarrett D'Amore 
1174297a3b0SGarrett D'Amore 	s = *src;
1184297a3b0SGarrett D'Amore 	nchr = 0;
1194297a3b0SGarrett D'Amore 	while (len-- > 0 && nms-- > 0) {
1204297a3b0SGarrett D'Amore 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
1214297a3b0SGarrett D'Amore 			*src = NULL;
1224297a3b0SGarrett D'Amore 			return (nchr);
1234297a3b0SGarrett D'Amore 		}
1244297a3b0SGarrett D'Amore 		nchr++;
1254297a3b0SGarrett D'Amore 	}
1264297a3b0SGarrett D'Amore 	*src = s;
1274297a3b0SGarrett D'Amore 	return (nchr);
1284297a3b0SGarrett D'Amore }
1294297a3b0SGarrett D'Amore 
1302d08521bSGarrett D'Amore size_t
__wcsnrtombs_ascii(char * _RESTRICT_KYWD dst,const wchar_t ** _RESTRICT_KYWD src,size_t nwc,size_t len,mbstate_t * _RESTRICT_KYWD unused __unused)1312d08521bSGarrett D'Amore __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
132*4a38094cSToomas Soome     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused __unused)
1334297a3b0SGarrett D'Amore {
1344297a3b0SGarrett D'Amore 	const wchar_t *s;
1354297a3b0SGarrett D'Amore 	size_t nchr;
1364297a3b0SGarrett D'Amore 
1374297a3b0SGarrett D'Amore 	if (dst == NULL) {
1384297a3b0SGarrett D'Amore 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
1394297a3b0SGarrett D'Amore 			if (*s < 0 || *s > UCHAR_MAX) {
1404297a3b0SGarrett D'Amore 				errno = EILSEQ;
1414297a3b0SGarrett D'Amore 				return ((size_t)-1);
1424297a3b0SGarrett D'Amore 			}
1434297a3b0SGarrett D'Amore 		}
1444297a3b0SGarrett D'Amore 		return (s - *src);
1454297a3b0SGarrett D'Amore 	}
1464297a3b0SGarrett D'Amore 
1474297a3b0SGarrett D'Amore 	s = *src;
1484297a3b0SGarrett D'Amore 	nchr = 0;
1494297a3b0SGarrett D'Amore 	while (len-- > 0 && nwc-- > 0) {
1504297a3b0SGarrett D'Amore 		if (*s < 0 || *s > UCHAR_MAX) {
1514297a3b0SGarrett D'Amore 			errno = EILSEQ;
1524297a3b0SGarrett D'Amore 			return ((size_t)-1);
1534297a3b0SGarrett D'Amore 		}
1544297a3b0SGarrett D'Amore 		if ((*dst++ = *s++) == '\0') {
1554297a3b0SGarrett D'Amore 			*src = NULL;
1564297a3b0SGarrett D'Amore 			return (nchr);
1574297a3b0SGarrett D'Amore 		}
1584297a3b0SGarrett D'Amore 		nchr++;
1594297a3b0SGarrett D'Amore 	}
1604297a3b0SGarrett D'Amore 	*src = s;
1614297a3b0SGarrett D'Amore 	return (nchr);
1624297a3b0SGarrett D'Amore }
163