xref: /titanic_41/usr/src/lib/libc/port/locale/none.c (revision e521049d0fdbd76727f036cb77c99c98d7ee1125)
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)
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 	return (*s == '\0' ? 0 : 1);
89 }
90 
91 size_t
92 __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
93     mbstate_t *_RESTRICT_KYWD unused)
94 {
95 	_NOTE(ARGUNUSED(unused));
96 
97 	if (s == NULL)
98 		/* Reset to initial shift state (no-op) */
99 		return (1);
100 	if (wc < 0 || wc > UCHAR_MAX) {
101 		errno = EILSEQ;
102 		return ((size_t)-1);
103 	}
104 	*s = (unsigned char)wc;
105 	return (1);
106 }
107 
108 size_t
109 __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
110     size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
111 {
112 	const char *s;
113 	size_t nchr;
114 
115 	_NOTE(ARGUNUSED(unused));
116 
117 	if (dst == NULL) {
118 		s = memchr(*src, '\0', nms);
119 		return (s != NULL ? s - *src : nms);
120 	}
121 
122 	s = *src;
123 	nchr = 0;
124 	while (len-- > 0 && nms-- > 0) {
125 		if ((*dst++ = (unsigned char)*s++) == L'\0') {
126 			*src = NULL;
127 			return (nchr);
128 		}
129 		nchr++;
130 	}
131 	*src = s;
132 	return (nchr);
133 }
134 
135 size_t
136 __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
137     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
138 {
139 	const wchar_t *s;
140 	size_t nchr;
141 
142 	_NOTE(ARGUNUSED(unused));
143 
144 	if (dst == NULL) {
145 		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
146 			if (*s < 0 || *s > UCHAR_MAX) {
147 				errno = EILSEQ;
148 				return ((size_t)-1);
149 			}
150 		}
151 		return (s - *src);
152 	}
153 
154 	s = *src;
155 	nchr = 0;
156 	while (len-- > 0 && nwc-- > 0) {
157 		if (*s < 0 || *s > UCHAR_MAX) {
158 			errno = EILSEQ;
159 			return ((size_t)-1);
160 		}
161 		if ((*dst++ = *s++) == '\0') {
162 			*src = NULL;
163 			return (nchr);
164 		}
165 		nchr++;
166 	}
167 	*src = s;
168 	return (nchr);
169 }
170