1 /* $NetBSD: citrus_none.c,v 1.8 2003/03/05 20:18:16 tshiozak Exp $ */ 2 3 /*- 4 * Copyright (c)2002 Citrus Project, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #if defined(LIBC_SCCS) && !defined(lint) 31 __RCSID("$NetBSD: citrus_none.c,v 1.8 2003/03/05 20:18:16 tshiozak Exp $"); 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include <assert.h> 35 #include <errno.h> 36 #include <string.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <stddef.h> 40 #include <locale.h> 41 #include <wchar.h> 42 #include <sys/types.h> 43 #include "citrus_module.h" 44 #include "citrus_ctype.h" 45 #include "citrus_none.h" 46 47 /* ---------------------------------------------------------------------- */ 48 49 _CITRUS_CTYPE_DECLS(NONE); 50 _CITRUS_CTYPE_DEF_OPS(NONE); 51 52 53 /* ---------------------------------------------------------------------- */ 54 55 static int 56 /*ARGSUSED*/ 57 _citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var, 58 size_t lenvar, size_t lenps) 59 { 60 *cl = NULL; 61 return (0); 62 } 63 64 static void 65 /*ARGSUSED*/ 66 _citrus_NONE_ctype_uninit(void *cl) 67 { 68 } 69 70 static unsigned 71 /*ARGSUSED*/ 72 _citrus_NONE_ctype_get_mb_cur_max(void *cl) 73 { 74 return (1); 75 } 76 77 static int 78 /*ARGSUSED*/ 79 _citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s, 80 size_t n, int * __restrict nresult) 81 { 82 if (!s) { 83 *nresult = 0; /* state independent */ 84 return (0); 85 } 86 if (n==0) { 87 *nresult = -1; 88 return (EILSEQ); 89 } 90 *nresult = (*s == 0) ? 0 : 1; 91 return (0); 92 } 93 94 static int 95 /*ARGSUSED*/ 96 _citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s, 97 size_t n, void * __restrict pspriv, 98 size_t * __restrict nresult) 99 { 100 if (!s) { 101 *nresult = 0; 102 return (0); 103 } 104 if (n==0) { 105 *nresult = (size_t)-2; 106 return (0); 107 } 108 *nresult = (*s == 0) ? 0 : 1; 109 return (0); 110 } 111 112 static int 113 /*ARGSUSED*/ 114 _citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc, 115 const char * __restrict s, size_t n, 116 void * __restrict pspriv, 117 size_t * __restrict nresult) 118 { 119 if (s == NULL) { 120 *nresult = 0; 121 return (0); 122 } 123 if (n == 0) { 124 *nresult = (size_t)-2; 125 return (0); 126 } 127 128 if (pwc != NULL) 129 *pwc = (wchar_t)(unsigned char) *s; 130 131 *nresult = *s == '\0' ? 0 : 1; 132 return (0); 133 } 134 135 static int 136 /*ARGSUSED*/ 137 _citrus_NONE_ctype_mbsinit(void * __restrict cl, 138 const void * __restrict pspriv, 139 int * __restrict nresult) 140 { 141 *nresult = 1; /* always initial state */ 142 return (0); 143 } 144 145 static int 146 /*ARGSUSED*/ 147 _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs, 148 const char ** __restrict s, size_t n, 149 void * __restrict pspriv, 150 size_t * __restrict nresult) 151 { 152 int cnt; 153 const char *s0; 154 155 /* if pwcs is NULL, ignore n */ 156 if (pwcs == NULL) 157 n = 1; /* arbitrary >0 value */ 158 159 cnt = 0; 160 s0 = *s; /* to keep *s unchanged for now, use copy instead. */ 161 while (n > 0) { 162 if (pwcs != NULL) { 163 *pwcs = (wchar_t)(unsigned char)*s0; 164 } 165 if (*s0 == '\0') { 166 s0 = NULL; 167 break; 168 } 169 s0++; 170 if (pwcs != NULL) { 171 pwcs++; 172 n--; 173 } 174 cnt++; 175 } 176 if (pwcs) 177 *s = s0; 178 179 *nresult = (size_t)cnt; 180 181 return (0); 182 } 183 184 static int 185 _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs, 186 const char * __restrict s, size_t n, 187 size_t * __restrict nresult) 188 { 189 return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, (const char **)&s, n, NULL, nresult)); 190 } 191 192 static int 193 /*ARGSUSED*/ 194 _citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc, 195 const char * __restrict s, size_t n, 196 int * __restrict nresult) 197 { 198 199 if (s == NULL) { 200 *nresult = 0; /* state independent */ 201 return (0); 202 } 203 if (n == 0) { 204 return (EILSEQ); 205 } 206 if (pwc == NULL) { 207 if (*s == '\0') { 208 *nresult = 0; 209 } else { 210 *nresult = 1; 211 } 212 return (0); 213 } 214 215 *pwc = (wchar_t)*s; 216 *nresult = *s == '\0' ? 0 : 1; 217 218 return (0); 219 } 220 221 static int 222 /*ARGSUSED*/ 223 _citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s, 224 wchar_t wc, void * __restrict pspriv, 225 size_t * __restrict nresult) 226 { 227 if ((wc&~0xFFU) != 0) { 228 *nresult = (size_t)-1; 229 return (EILSEQ); 230 } 231 232 *nresult = 1; 233 if (s!=NULL) 234 *s = (char)wc; 235 236 return (0); 237 } 238 239 static int 240 /*ARGSUSED*/ 241 _citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s, 242 const wchar_t ** __restrict pwcs, size_t n, 243 void * __restrict pspriv, 244 size_t * __restrict nresult) 245 { 246 size_t count; 247 const wchar_t *pwcs0; 248 249 pwcs0 = *pwcs; 250 count = 0; 251 252 if (s == NULL) 253 n = 1; 254 255 while (n > 0) { 256 if ((*pwcs0 & ~0xFFU) != 0) { 257 *nresult = (size_t)-1; 258 return (EILSEQ); 259 } 260 if (s != NULL) { 261 *s++ = (char)*pwcs0; 262 n--; 263 } 264 if (*pwcs0 == L'\0') { 265 pwcs0 = NULL; 266 break; 267 } 268 count++; 269 pwcs0++; 270 } 271 if (s != NULL) 272 *pwcs = pwcs0; 273 274 *nresult = count; 275 276 return (0); 277 } 278 279 static int 280 _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s, 281 const wchar_t * __restrict pwcs, size_t n, 282 size_t * __restrict nresult) 283 { 284 return (_citrus_NONE_ctype_wcsrtombs(cl, s, (const wchar_t **)&pwcs, n, NULL, nresult)); 285 } 286 287 static int 288 _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s, 289 wchar_t wc, int * __restrict nresult) 290 { 291 int ret; 292 size_t nr; 293 294 if (s == 0) { 295 /* 296 * initialize state here. 297 * (nothing to do for us.) 298 */ 299 *nresult = 0; /* we're state independent */ 300 return (0); 301 } 302 303 ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr); 304 *nresult = (int)nr; 305 306 return (ret); 307 } 308 309 static int 310 /*ARGSUSED*/ 311 _citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc, 312 int c, wint_t * __restrict wcresult) 313 { 314 if (c == EOF || c & ~0xFF) 315 *wcresult = WEOF; 316 else 317 *wcresult = (wint_t)c; 318 return (0); 319 } 320 321 static int 322 /*ARGSUSED*/ 323 _citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc, 324 wint_t wc, int * __restrict cresult) 325 { 326 if (wc == WEOF || wc & ~0xFF) 327 *cresult = EOF; 328 else 329 *cresult = (int)wc; 330 return (0); 331 } 332