1 /*
2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Paul Borman at Krystal Technologies.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "lint.h"
37 #include <errno.h>
38 #include <limits.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <wchar.h>
44 #include "mblocal.h"
45 #include "lctype.h"
46
47 /* setup defaults */
48
49 void
_none_init(struct lc_ctype * lct)50 _none_init(struct lc_ctype *lct)
51 {
52 lct->lc_is_ascii = 1;
53 lct->lc_mbrtowc = __mbrtowc_ascii;
54 lct->lc_mbsinit = __mbsinit_ascii;
55 lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
56 lct->lc_wcrtomb = __wcrtomb_ascii;
57 lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
58 lct->lc_max_mblen = 1;
59 }
60
61 int
__mbsinit_ascii(const mbstate_t * unused __unused)62 __mbsinit_ascii(const mbstate_t *unused __unused)
63 {
64 /*
65 * Encoding is not state dependent - we are always in the
66 * initial state.
67 */
68 return (1);
69 }
70
71 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)72 __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
73 size_t n, mbstate_t *_RESTRICT_KYWD unused __unused, boolean_t zero)
74 {
75 if (s == NULL)
76 /* Reset to initial shift state (no-op) */
77 return (0);
78 if (n == 0)
79 /* Incomplete multibyte sequence */
80 return ((size_t)-2);
81 if (pwc != NULL)
82 *pwc = (unsigned char)*s;
83 if (zero || *s != '\0') {
84 return (1);
85 } else {
86 return (0);
87 }
88 }
89
90 size_t
__wcrtomb_ascii(char * _RESTRICT_KYWD s,wchar_t wc,mbstate_t * _RESTRICT_KYWD unused __unused)91 __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
92 mbstate_t *_RESTRICT_KYWD unused __unused)
93 {
94 if (s == NULL)
95 /* Reset to initial shift state (no-op) */
96 return (1);
97 if (wc < 0 || wc > UCHAR_MAX) {
98 errno = EILSEQ;
99 return ((size_t)-1);
100 }
101 *s = (unsigned char)wc;
102 return (1);
103 }
104
105 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)106 __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
107 size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused __unused)
108 {
109 const char *s;
110 size_t nchr;
111
112 if (dst == NULL) {
113 s = memchr(*src, '\0', nms);
114 return (s != NULL ? s - *src : nms);
115 }
116
117 s = *src;
118 nchr = 0;
119 while (len-- > 0 && nms-- > 0) {
120 if ((*dst++ = (unsigned char)*s++) == L'\0') {
121 *src = NULL;
122 return (nchr);
123 }
124 nchr++;
125 }
126 *src = s;
127 return (nchr);
128 }
129
130 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)131 __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
132 size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused __unused)
133 {
134 const wchar_t *s;
135 size_t nchr;
136
137 if (dst == NULL) {
138 for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
139 if (*s < 0 || *s > UCHAR_MAX) {
140 errno = EILSEQ;
141 return ((size_t)-1);
142 }
143 }
144 return (s - *src);
145 }
146
147 s = *src;
148 nchr = 0;
149 while (len-- > 0 && nwc-- > 0) {
150 if (*s < 0 || *s > UCHAR_MAX) {
151 errno = EILSEQ;
152 return ((size_t)-1);
153 }
154 if ((*dst++ = *s++) == '\0') {
155 *src = NULL;
156 return (nchr);
157 }
158 nchr++;
159 }
160 *src = s;
161 return (nchr);
162 }
163