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