xref: /illumos-gate/usr/src/lib/libc/port/locale/none.c (revision e77c795bcbe51aebd7579fe13cbf2a6d56eca47f)
1 /*
2  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Paul Borman at Krystal Technologies.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
36  * Use is subject to license terms.
37  */
38 
39 #include "lint.h"
40 #include <errno.h>
41 #include <limits.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <wchar.h>
47 #include <note.h>
48 #include "runetype.h"
49 #include "mblocal.h"
50 #include "../i18n/_locale.h"
51 
52 static size_t	_none_mbrtowc(wchar_t *_RESTRICT_KYWD,
53     const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD);
54 
55 static int	_none_mbsinit(const mbstate_t *);
56 static size_t	_none_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst,
57     const char **_RESTRICT_KYWD src, size_t nms, size_t len,
58     mbstate_t *_RESTRICT_KYWD);
59 static size_t	_none_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
60     mbstate_t *_RESTRICT_KYWD);
61 static size_t	_none_wcsnrtombs(char *_RESTRICT_KYWD,
62     const wchar_t **_RESTRICT_KYWD,
63     size_t, size_t, mbstate_t *_RESTRICT_KYWD);
64 
65 /* setup defaults */
66 
67 extern unsigned char __ctype_C[];
68 
69 int
70 _none_init(_RuneLocale *rl)
71 {
72 	/*
73 	 * We need to populate the ctype stuff.  This means the
74 	 * tolower table, the type masks, etc.
75 	 * There are 257 entries for the type array, 257 entries for the
76 	 * tolower/toupper array, and 7 bytes for CSWIDTH array.
77 	 *
78 	 * We have to set this stuff up because for POSIX/C we short
79 	 * circuit most of the logic in setrunelocale that would handle it.
80 	 */
81 	(void) memcpy(__ctype, __ctype_C, SZ_TOTAL);
82 
83 	charset_is_ascii = 1;
84 
85 	__ctype_mask = rl->__runetype;
86 	__trans_upper = rl->__mapupper;
87 	__trans_lower = rl->__maplower;
88 
89 	__mbrtowc = _none_mbrtowc;
90 	__mbsinit = _none_mbsinit;
91 	__mbsnrtowcs = _none_mbsnrtowcs;
92 	__wcrtomb = _none_wcrtomb;
93 	__wcsnrtombs = _none_wcsnrtombs;
94 	_CurrentRuneLocale = rl;
95 	return (0);
96 }
97 
98 static int
99 _none_mbsinit(const mbstate_t *unused)
100 {
101 	_NOTE(ARGUNUSED(unused));
102 
103 	/*
104 	 * Encoding is not state dependent - we are always in the
105 	 * initial state.
106 	 */
107 	return (1);
108 }
109 
110 static size_t
111 _none_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
112     size_t n, mbstate_t *_RESTRICT_KYWD unused)
113 {
114 	_NOTE(ARGUNUSED(unused));
115 
116 	if (s == NULL)
117 		/* Reset to initial shift state (no-op) */
118 		return (0);
119 	if (n == 0)
120 		/* Incomplete multibyte sequence */
121 		return ((size_t)-2);
122 	if (pwc != NULL)
123 		*pwc = (unsigned char)*s;
124 	return (*s == '\0' ? 0 : 1);
125 }
126 
127 static size_t
128 _none_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc,
129     mbstate_t *_RESTRICT_KYWD unused)
130 {
131 	_NOTE(ARGUNUSED(unused));
132 
133 	if (s == NULL)
134 		/* Reset to initial shift state (no-op) */
135 		return (1);
136 	if (wc < 0 || wc > UCHAR_MAX) {
137 		errno = EILSEQ;
138 		return ((size_t)-1);
139 	}
140 	*s = (unsigned char)wc;
141 	return (1);
142 }
143 
144 static size_t
145 _none_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
146     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
147 {
148 	const char *s;
149 	size_t nchr;
150 
151 	_NOTE(ARGUNUSED(unused));
152 
153 	if (dst == NULL) {
154 		s = memchr(*src, '\0', nms);
155 		return (s != NULL ? s - *src : nms);
156 	}
157 
158 	s = *src;
159 	nchr = 0;
160 	while (len-- > 0 && nms-- > 0) {
161 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
162 			*src = NULL;
163 			return (nchr);
164 		}
165 		nchr++;
166 	}
167 	*src = s;
168 	return (nchr);
169 }
170 
171 static size_t
172 _none_wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
173     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
174 {
175 	const wchar_t *s;
176 	size_t nchr;
177 
178 	_NOTE(ARGUNUSED(unused));
179 
180 	if (dst == NULL) {
181 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
182 			if (*s < 0 || *s > UCHAR_MAX) {
183 				errno = EILSEQ;
184 				return ((size_t)-1);
185 			}
186 		}
187 		return (s - *src);
188 	}
189 
190 	s = *src;
191 	nchr = 0;
192 	while (len-- > 0 && nwc-- > 0) {
193 		if (*s < 0 || *s > UCHAR_MAX) {
194 			errno = EILSEQ;
195 			return ((size_t)-1);
196 		}
197 		if ((*dst++ = *s++) == '\0') {
198 			*src = NULL;
199 			return (nchr);
200 		}
201 		nchr++;
202 	}
203 	*src = s;
204 	return (nchr);
205 }
206 
207 /* setup defaults */
208 
209 size_t (*__mbrtowc)(wchar_t *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
210     size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbrtowc;
211 
212 int (*__mbsinit)(const mbstate_t *) = _none_mbsinit;
213 
214 size_t (*__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, const char **_RESTRICT_KYWD,
215     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbsnrtowcs;
216 
217 size_t (*__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, mbstate_t *_RESTRICT_KYWD) =
218     _none_wcrtomb;
219 
220 size_t (*__wcsnrtombs)(char *_RESTRICT_KYWD, const wchar_t **_RESTRICT_KYWD,
221     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_wcsnrtombs;
222