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