]>
Commit | Line | Data |
---|---|---|
224c7076 A |
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 | * 3. All advertising materials mentioning features or use of this software | |
18 | * must display the following acknowledgement: | |
19 | * This product includes software developed by the University of | |
20 | * California, Berkeley and its contributors. | |
21 | * 4. Neither the name of the University nor the names of its contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
35 | * SUCH DAMAGE. | |
36 | */ | |
37 | ||
38 | #if defined(LIBC_SCCS) && !defined(lint) | |
39 | static char sccsid[] = "@(#)none.c 8.1 (Berkeley) 6/4/93"; | |
40 | #endif /* LIBC_SCCS and not lint */ | |
41 | #include <sys/cdefs.h> | |
42 | __FBSDID("$FreeBSD: src/lib/libc/locale/none.c,v 1.12 2004/07/21 10:54:57 tjr Exp $"); | |
43 | ||
44 | #include "xlocale_private.h" | |
45 | ||
46 | #include <errno.h> | |
47 | #include <limits.h> | |
48 | #include <runetype.h> | |
49 | #include <stddef.h> | |
50 | #include <stdio.h> | |
51 | #include <stdlib.h> | |
52 | #include <string.h> | |
53 | #include <wchar.h> | |
54 | #include "mblocal.h" | |
55 | ||
56 | __private_extern__ int _none_init(struct __xlocale_st_runelocale *); | |
57 | __private_extern__ size_t _none_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, | |
58 | mbstate_t * __restrict, locale_t); | |
59 | __private_extern__ int _none_mbsinit(const mbstate_t *, locale_t); | |
60 | __private_extern__ size_t _none_mbsnrtowcs(wchar_t * __restrict dst, | |
61 | const char ** __restrict src, size_t nms, size_t len, | |
62 | mbstate_t * __restrict ps __unused, locale_t); | |
63 | __private_extern__ size_t _none_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict, locale_t); | |
64 | __private_extern__ size_t _none_wcsnrtombs(char * __restrict, const wchar_t ** __restrict, | |
65 | size_t, size_t, mbstate_t * __restrict, locale_t); | |
66 | ||
67 | __private_extern__ int | |
68 | _none_init(struct __xlocale_st_runelocale *xrl) | |
69 | { | |
70 | ||
71 | xrl->__mbrtowc = _none_mbrtowc; | |
72 | xrl->__mbsinit = _none_mbsinit; | |
73 | xrl->__mbsnrtowcs = _none_mbsnrtowcs; | |
74 | xrl->__wcrtomb = _none_wcrtomb; | |
75 | xrl->__wcsnrtombs = _none_wcsnrtombs; | |
76 | xrl->__mb_cur_max = 1; | |
77 | return(0); | |
78 | } | |
79 | ||
80 | __private_extern__ int | |
81 | _none_mbsinit(const mbstate_t *ps __unused, locale_t loc) | |
82 | { | |
83 | ||
84 | /* | |
85 | * Encoding is not state dependent - we are always in the | |
86 | * initial state. | |
87 | */ | |
88 | return (1); | |
89 | } | |
90 | ||
91 | __private_extern__ size_t | |
92 | _none_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, | |
93 | mbstate_t * __restrict ps __unused, locale_t loc) | |
94 | { | |
95 | ||
96 | if (s == NULL) | |
97 | /* Reset to initial shift state (no-op) */ | |
98 | return (0); | |
99 | if (n == 0) | |
100 | /* Incomplete multibyte sequence */ | |
101 | return ((size_t)-2); | |
102 | if (pwc != NULL) | |
103 | *pwc = (unsigned char)*s; | |
104 | return (*s == '\0' ? 0 : 1); | |
105 | } | |
106 | ||
107 | __private_extern__ size_t | |
108 | _none_wcrtomb(char * __restrict s, wchar_t wc, | |
109 | mbstate_t * __restrict ps __unused, locale_t loc) | |
110 | { | |
111 | ||
112 | if (s == NULL) | |
113 | /* Reset to initial shift state (no-op) */ | |
114 | return (1); | |
115 | if (wc < 0 || wc > UCHAR_MAX) { | |
116 | errno = EILSEQ; | |
117 | return ((size_t)-1); | |
118 | } | |
119 | *s = (unsigned char)wc; | |
120 | return (1); | |
121 | } | |
122 | ||
123 | __private_extern__ size_t | |
124 | _none_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src, | |
125 | size_t nms, size_t len, mbstate_t * __restrict ps __unused, locale_t loc) | |
126 | { | |
127 | const char *s; | |
128 | size_t nchr; | |
129 | ||
130 | if (dst == NULL) { | |
131 | s = memchr(*src, '\0', nms); | |
132 | return (s != NULL ? s - *src : nms); | |
133 | } | |
134 | ||
135 | s = *src; | |
136 | nchr = 0; | |
137 | while (len-- > 0 && nms-- > 0) { | |
138 | if ((*dst++ = (unsigned char)*s++) == L'\0') { | |
139 | *src = NULL; | |
140 | return (nchr); | |
141 | } | |
142 | nchr++; | |
143 | } | |
144 | *src = s; | |
145 | return (nchr); | |
146 | } | |
147 | ||
148 | __private_extern__ size_t | |
149 | _none_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src, | |
150 | size_t nwc, size_t len, mbstate_t * __restrict ps __unused, locale_t loc) | |
151 | { | |
152 | const wchar_t *s; | |
153 | size_t nchr; | |
154 | ||
155 | if (dst == NULL) { | |
156 | for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) { | |
157 | if (*s < 0 || *s > UCHAR_MAX) { | |
158 | errno = EILSEQ; | |
159 | return ((size_t)-1); | |
160 | } | |
161 | } | |
162 | return (s - *src); | |
163 | } | |
164 | ||
165 | s = *src; | |
166 | nchr = 0; | |
167 | while (len-- > 0 && nwc-- > 0) { | |
168 | if (*s < 0 || *s > UCHAR_MAX) { | |
169 | errno = EILSEQ; | |
170 | return ((size_t)-1); | |
171 | } | |
172 | if ((*dst++ = *s++) == '\0') { | |
173 | *src = NULL; | |
174 | return (nchr); | |
175 | } | |
176 | nchr++; | |
177 | } | |
178 | *src = s; | |
179 | return (nchr); | |
180 | } |