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 <note.h>
454297a3b0SGarrett D'Amore #include "mblocal.h"
462d08521bSGarrett D'Amore #include "lctype.h"
474297a3b0SGarrett D'Amore
484297a3b0SGarrett D'Amore /* setup defaults */
494297a3b0SGarrett D'Amore
502d08521bSGarrett D'Amore void
_none_init(struct lc_ctype * lct)512d08521bSGarrett D'Amore _none_init(struct lc_ctype *lct)
524297a3b0SGarrett D'Amore {
532d08521bSGarrett D'Amore lct->lc_is_ascii = 1;
542d08521bSGarrett D'Amore lct->lc_mbrtowc = __mbrtowc_ascii;
552d08521bSGarrett D'Amore lct->lc_mbsinit = __mbsinit_ascii;
562d08521bSGarrett D'Amore lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
572d08521bSGarrett D'Amore lct->lc_wcrtomb = __wcrtomb_ascii;
582d08521bSGarrett D'Amore lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
592d08521bSGarrett D'Amore lct->lc_max_mblen = 1;
604297a3b0SGarrett D'Amore }
614297a3b0SGarrett D'Amore
622d08521bSGarrett D'Amore int
__mbsinit_ascii(const mbstate_t * unused)632d08521bSGarrett D'Amore __mbsinit_ascii(const mbstate_t *unused)
644297a3b0SGarrett D'Amore {
654297a3b0SGarrett D'Amore _NOTE(ARGUNUSED(unused));
664297a3b0SGarrett D'Amore
674297a3b0SGarrett D'Amore /*
684297a3b0SGarrett D'Amore * Encoding is not state dependent - we are always in the
694297a3b0SGarrett D'Amore * initial state.
704297a3b0SGarrett D'Amore */
714297a3b0SGarrett D'Amore return (1);
724297a3b0SGarrett D'Amore }
734297a3b0SGarrett D'Amore
742d08521bSGarrett D'Amore size_t
__mbrtowc_ascii(wchar_t * _RESTRICT_KYWD pwc,const char * _RESTRICT_KYWD s,size_t n,mbstate_t * _RESTRICT_KYWD unused,boolean_t zero)752d08521bSGarrett D'Amore __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
76*50370af8SRobert Mustacchi size_t n, mbstate_t *_RESTRICT_KYWD unused, boolean_t zero)
774297a3b0SGarrett D'Amore {
784297a3b0SGarrett D'Amore _NOTE(ARGUNUSED(unused));
794297a3b0SGarrett D'Amore
804297a3b0SGarrett D'Amore if (s == NULL)
814297a3b0SGarrett D'Amore /* Reset to initial shift state (no-op) */
824297a3b0SGarrett D'Amore return (0);
834297a3b0SGarrett D'Amore if (n == 0)
844297a3b0SGarrett D'Amore /* Incomplete multibyte sequence */
854297a3b0SGarrett D'Amore return ((size_t)-2);
864297a3b0SGarrett D'Amore if (pwc != NULL)
874297a3b0SGarrett D'Amore *pwc = (unsigned char)*s;
88*50370af8SRobert Mustacchi if (zero || *s != '\0') {
89*50370af8SRobert Mustacchi return (1);
90*50370af8SRobert Mustacchi } else {
91*50370af8SRobert Mustacchi return (0);
92*50370af8SRobert Mustacchi }
934297a3b0SGarrett D'Amore }
944297a3b0SGarrett D'Amore
952d08521bSGarrett D'Amore size_t
__wcrtomb_ascii(char * _RESTRICT_KYWD s,wchar_t wc,mbstate_t * _RESTRICT_KYWD unused)962d08521bSGarrett D'Amore __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
974297a3b0SGarrett D'Amore mbstate_t *_RESTRICT_KYWD unused)
984297a3b0SGarrett D'Amore {
994297a3b0SGarrett D'Amore _NOTE(ARGUNUSED(unused));
1004297a3b0SGarrett D'Amore
1014297a3b0SGarrett D'Amore if (s == NULL)
1024297a3b0SGarrett D'Amore /* Reset to initial shift state (no-op) */
1034297a3b0SGarrett D'Amore return (1);
1044297a3b0SGarrett D'Amore if (wc < 0 || wc > UCHAR_MAX) {
1054297a3b0SGarrett D'Amore errno = EILSEQ;
1064297a3b0SGarrett D'Amore return ((size_t)-1);
1074297a3b0SGarrett D'Amore }
1084297a3b0SGarrett D'Amore *s = (unsigned char)wc;
1094297a3b0SGarrett D'Amore return (1);
1104297a3b0SGarrett D'Amore }
1114297a3b0SGarrett D'Amore
1122d08521bSGarrett 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)1132d08521bSGarrett D'Amore __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
1144297a3b0SGarrett D'Amore size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
1154297a3b0SGarrett D'Amore {
1164297a3b0SGarrett D'Amore const char *s;
1174297a3b0SGarrett D'Amore size_t nchr;
1184297a3b0SGarrett D'Amore
1194297a3b0SGarrett D'Amore _NOTE(ARGUNUSED(unused));
1204297a3b0SGarrett D'Amore
1214297a3b0SGarrett D'Amore if (dst == NULL) {
1224297a3b0SGarrett D'Amore s = memchr(*src, '\0', nms);
1234297a3b0SGarrett D'Amore return (s != NULL ? s - *src : nms);
1244297a3b0SGarrett D'Amore }
1254297a3b0SGarrett D'Amore
1264297a3b0SGarrett D'Amore s = *src;
1274297a3b0SGarrett D'Amore nchr = 0;
1284297a3b0SGarrett D'Amore while (len-- > 0 && nms-- > 0) {
1294297a3b0SGarrett D'Amore if ((*dst++ = (unsigned char)*s++) == L'\0') {
1304297a3b0SGarrett D'Amore *src = NULL;
1314297a3b0SGarrett D'Amore return (nchr);
1324297a3b0SGarrett D'Amore }
1334297a3b0SGarrett D'Amore nchr++;
1344297a3b0SGarrett D'Amore }
1354297a3b0SGarrett D'Amore *src = s;
1364297a3b0SGarrett D'Amore return (nchr);
1374297a3b0SGarrett D'Amore }
1384297a3b0SGarrett D'Amore
1392d08521bSGarrett 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)1402d08521bSGarrett D'Amore __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
1414297a3b0SGarrett D'Amore size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
1424297a3b0SGarrett D'Amore {
1434297a3b0SGarrett D'Amore const wchar_t *s;
1444297a3b0SGarrett D'Amore size_t nchr;
1454297a3b0SGarrett D'Amore
1464297a3b0SGarrett D'Amore _NOTE(ARGUNUSED(unused));
1474297a3b0SGarrett D'Amore
1484297a3b0SGarrett D'Amore if (dst == NULL) {
1494297a3b0SGarrett D'Amore for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
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 }
1554297a3b0SGarrett D'Amore return (s - *src);
1564297a3b0SGarrett D'Amore }
1574297a3b0SGarrett D'Amore
1584297a3b0SGarrett D'Amore s = *src;
1594297a3b0SGarrett D'Amore nchr = 0;
1604297a3b0SGarrett D'Amore while (len-- > 0 && nwc-- > 0) {
1614297a3b0SGarrett D'Amore if (*s < 0 || *s > UCHAR_MAX) {
1624297a3b0SGarrett D'Amore errno = EILSEQ;
1634297a3b0SGarrett D'Amore return ((size_t)-1);
1644297a3b0SGarrett D'Amore }
1654297a3b0SGarrett D'Amore if ((*dst++ = *s++) == '\0') {
1664297a3b0SGarrett D'Amore *src = NULL;
1674297a3b0SGarrett D'Amore return (nchr);
1684297a3b0SGarrett D'Amore }
1694297a3b0SGarrett D'Amore nchr++;
1704297a3b0SGarrett D'Amore }
1714297a3b0SGarrett D'Amore *src = s;
1724297a3b0SGarrett D'Amore return (nchr);
1734297a3b0SGarrett D'Amore }
174