xref: /illumos-gate/usr/src/lib/libc/port/locale/none.c (revision 8d0c3d29bb99f6521f2dc5058a7e4debebad7899)
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 	__ctype_mask = rl->__runetype;
84 	__trans_upper = rl->__mapupper;
85 	__trans_lower = rl->__maplower;
86 
87 	__mbrtowc = _none_mbrtowc;
88 	__mbsinit = _none_mbsinit;
89 	__mbsnrtowcs = _none_mbsnrtowcs;
90 	__wcrtomb = _none_wcrtomb;
91 	__wcsnrtombs = _none_wcsnrtombs;
92 	_CurrentRuneLocale = rl;
93 	return (0);
94 }
95 
96 static int
97 _none_mbsinit(const mbstate_t *unused)
98 {
99 	_NOTE(ARGUNUSED(unused));
100 
101 	/*
102 	 * Encoding is not state dependent - we are always in the
103 	 * initial state.
104 	 */
105 	return (1);
106 }
107 
108 static size_t
109 _none_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
110     size_t n, mbstate_t *_RESTRICT_KYWD unused)
111 {
112 	_NOTE(ARGUNUSED(unused));
113 
114 	if (s == NULL)
115 		/* Reset to initial shift state (no-op) */
116 		return (0);
117 	if (n == 0)
118 		/* Incomplete multibyte sequence */
119 		return ((size_t)-2);
120 	if (pwc != NULL)
121 		*pwc = (unsigned char)*s;
122 	return (*s == '\0' ? 0 : 1);
123 }
124 
125 static size_t
126 _none_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc,
127     mbstate_t *_RESTRICT_KYWD unused)
128 {
129 	_NOTE(ARGUNUSED(unused));
130 
131 	if (s == NULL)
132 		/* Reset to initial shift state (no-op) */
133 		return (1);
134 	if (wc < 0 || wc > UCHAR_MAX) {
135 		errno = EILSEQ;
136 		return ((size_t)-1);
137 	}
138 	*s = (unsigned char)wc;
139 	return (1);
140 }
141 
142 static size_t
143 _none_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
144     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
145 {
146 	const char *s;
147 	size_t nchr;
148 
149 	_NOTE(ARGUNUSED(unused));
150 
151 	if (dst == NULL) {
152 		s = memchr(*src, '\0', nms);
153 		return (s != NULL ? s - *src : nms);
154 	}
155 
156 	s = *src;
157 	nchr = 0;
158 	while (len-- > 0 && nms-- > 0) {
159 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
160 			*src = NULL;
161 			return (nchr);
162 		}
163 		nchr++;
164 	}
165 	*src = s;
166 	return (nchr);
167 }
168 
169 static size_t
170 _none_wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
171     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
172 {
173 	const wchar_t *s;
174 	size_t nchr;
175 
176 	_NOTE(ARGUNUSED(unused));
177 
178 	if (dst == NULL) {
179 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
180 			if (*s < 0 || *s > UCHAR_MAX) {
181 				errno = EILSEQ;
182 				return ((size_t)-1);
183 			}
184 		}
185 		return (s - *src);
186 	}
187 
188 	s = *src;
189 	nchr = 0;
190 	while (len-- > 0 && nwc-- > 0) {
191 		if (*s < 0 || *s > UCHAR_MAX) {
192 			errno = EILSEQ;
193 			return ((size_t)-1);
194 		}
195 		if ((*dst++ = *s++) == '\0') {
196 			*src = NULL;
197 			return (nchr);
198 		}
199 		nchr++;
200 	}
201 	*src = s;
202 	return (nchr);
203 }
204 
205 /* setup defaults */
206 
207 size_t (*__mbrtowc)(wchar_t *_RESTRICT_KYWD, const char *_RESTRICT_KYWD,
208     size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbrtowc;
209 
210 int (*__mbsinit)(const mbstate_t *) = _none_mbsinit;
211 
212 size_t (*__mbsnrtowcs)(wchar_t *_RESTRICT_KYWD, const char **_RESTRICT_KYWD,
213     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_mbsnrtowcs;
214 
215 size_t (*__wcrtomb)(char *_RESTRICT_KYWD, wchar_t, mbstate_t *_RESTRICT_KYWD) =
216     _none_wcrtomb;
217 
218 size_t (*__wcsnrtombs)(char *_RESTRICT_KYWD, const wchar_t **_RESTRICT_KYWD,
219     size_t, size_t, mbstate_t *_RESTRICT_KYWD) = _none_wcsnrtombs;
220