xref: /titanic_51/usr/src/lib/libc/port/locale/none.c (revision d8e0a9a12d4d1ec2e9410b384d25d738daf9f240)
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 <note.h>
45 #include "mblocal.h"
46 #include "lctype.h"
47 
48 /* setup defaults */
49 
50 void
51 _none_init(struct lc_ctype *lct)
52 {
53 	lct->lc_is_ascii = 1;
54 	lct->lc_mbrtowc = __mbrtowc_ascii;
55 	lct->lc_mbsinit = __mbsinit_ascii;
56 	lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
57 	lct->lc_wcrtomb = __wcrtomb_ascii;
58 	lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
59 	lct->lc_max_mblen = 1;
60 }
61 
62 int
63 __mbsinit_ascii(const mbstate_t *unused)
64 {
65 	_NOTE(ARGUNUSED(unused));
66 
67 	/*
68 	 * Encoding is not state dependent - we are always in the
69 	 * initial state.
70 	 */
71 	return (1);
72 }
73 
74 size_t
75 __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
76     size_t n, mbstate_t *_RESTRICT_KYWD unused, boolean_t zero)
77 {
78 	_NOTE(ARGUNUSED(unused));
79 
80 	if (s == NULL)
81 		/* Reset to initial shift state (no-op) */
82 		return (0);
83 	if (n == 0)
84 		/* Incomplete multibyte sequence */
85 		return ((size_t)-2);
86 	if (pwc != NULL)
87 		*pwc = (unsigned char)*s;
88 	if (zero || *s != '\0') {
89 		return (1);
90 	} else {
91 		return (0);
92 	}
93 }
94 
95 size_t
96 __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
97     mbstate_t *_RESTRICT_KYWD unused)
98 {
99 	_NOTE(ARGUNUSED(unused));
100 
101 	if (s == NULL)
102 		/* Reset to initial shift state (no-op) */
103 		return (1);
104 	if (wc < 0 || wc > UCHAR_MAX) {
105 		errno = EILSEQ;
106 		return ((size_t)-1);
107 	}
108 	*s = (unsigned char)wc;
109 	return (1);
110 }
111 
112 size_t
113 __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
114     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
115 {
116 	const char *s;
117 	size_t nchr;
118 
119 	_NOTE(ARGUNUSED(unused));
120 
121 	if (dst == NULL) {
122 		s = memchr(*src, '\0', nms);
123 		return (s != NULL ? s - *src : nms);
124 	}
125 
126 	s = *src;
127 	nchr = 0;
128 	while (len-- > 0 && nms-- > 0) {
129 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
130 			*src = NULL;
131 			return (nchr);
132 		}
133 		nchr++;
134 	}
135 	*src = s;
136 	return (nchr);
137 }
138 
139 size_t
140 __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
141     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
142 {
143 	const wchar_t *s;
144 	size_t nchr;
145 
146 	_NOTE(ARGUNUSED(unused));
147 
148 	if (dst == NULL) {
149 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
150 			if (*s < 0 || *s > UCHAR_MAX) {
151 				errno = EILSEQ;
152 				return ((size_t)-1);
153 			}
154 		}
155 		return (s - *src);
156 	}
157 
158 	s = *src;
159 	nchr = 0;
160 	while (len-- > 0 && nwc-- > 0) {
161 		if (*s < 0 || *s > UCHAR_MAX) {
162 			errno = EILSEQ;
163 			return ((size_t)-1);
164 		}
165 		if ((*dst++ = *s++) == '\0') {
166 			*src = NULL;
167 			return (nchr);
168 		}
169 		nchr++;
170 	}
171 	*src = s;
172 	return (nchr);
173 }
174