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