citrus_none.c revision 1.14 1 /* $NetBSD: citrus_none.c,v 1.14 2005/10/29 18:02:04 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.14 2005/10/29 18:02:04 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
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_module.h"
47 #include "citrus_ctype.h"
48 #include "citrus_none.h"
49 #include "citrus_stdenc.h"
50
51 /* ---------------------------------------------------------------------- */
52
53 _CITRUS_CTYPE_DECLS(NONE);
54 _CITRUS_CTYPE_DEF_OPS(NONE);
55
56
57 /* ---------------------------------------------------------------------- */
58
59 static int
60 /*ARGSUSED*/
61 _citrus_NONE_ctype_init(void ** __restrict cl, void * __restrict var,
62 size_t lenvar, size_t lenps)
63 {
64 *cl = NULL;
65 return (0);
66 }
67
68 static void
69 /*ARGSUSED*/
70 _citrus_NONE_ctype_uninit(void *cl)
71 {
72 }
73
74 static unsigned
75 /*ARGSUSED*/
76 _citrus_NONE_ctype_get_mb_cur_max(void *cl)
77 {
78 return (1);
79 }
80
81 static int
82 /*ARGSUSED*/
83 _citrus_NONE_ctype_mblen(void * __restrict cl, const char * __restrict s,
84 size_t n, int * __restrict nresult)
85 {
86 if (!s) {
87 *nresult = 0; /* state independent */
88 return (0);
89 }
90 if (n==0) {
91 *nresult = -1;
92 return (EILSEQ);
93 }
94 *nresult = (*s == 0) ? 0 : 1;
95 return (0);
96 }
97
98 static int
99 /*ARGSUSED*/
100 _citrus_NONE_ctype_mbrlen(void * __restrict cl, const char * __restrict s,
101 size_t n, void * __restrict pspriv,
102 size_t * __restrict nresult)
103 {
104 if (!s) {
105 *nresult = 0;
106 return (0);
107 }
108 if (n==0) {
109 *nresult = (size_t)-2;
110 return (0);
111 }
112 *nresult = (*s == 0) ? 0 : 1;
113 return (0);
114 }
115
116 static int
117 /*ARGSUSED*/
118 _citrus_NONE_ctype_mbrtowc(void * __restrict cl, wchar_t * __restrict pwc,
119 const char * __restrict s, size_t n,
120 void * __restrict pspriv,
121 size_t * __restrict nresult)
122 {
123 if (s == NULL) {
124 *nresult = 0;
125 return (0);
126 }
127 if (n == 0) {
128 *nresult = (size_t)-2;
129 return (0);
130 }
131
132 if (pwc != NULL)
133 *pwc = (wchar_t)(unsigned char) *s;
134
135 *nresult = *s == '\0' ? 0 : 1;
136 return (0);
137 }
138
139 static int
140 /*ARGSUSED*/
141 _citrus_NONE_ctype_mbsinit(void * __restrict cl,
142 const void * __restrict pspriv,
143 int * __restrict nresult)
144 {
145 *nresult = 1; /* always initial state */
146 return (0);
147 }
148
149 static int
150 /*ARGSUSED*/
151 _citrus_NONE_ctype_mbsrtowcs(void * __restrict cl, wchar_t * __restrict pwcs,
152 const char ** __restrict s, size_t n,
153 void * __restrict pspriv,
154 size_t * __restrict nresult)
155 {
156 int cnt;
157 const char *s0;
158
159 /* if pwcs is NULL, ignore n */
160 if (pwcs == NULL)
161 n = 1; /* arbitrary >0 value */
162
163 cnt = 0;
164 s0 = *s; /* to keep *s unchanged for now, use copy instead. */
165 while (n > 0) {
166 if (pwcs != NULL) {
167 *pwcs = (wchar_t)(unsigned char)*s0;
168 }
169 if (*s0 == '\0') {
170 s0 = NULL;
171 break;
172 }
173 s0++;
174 if (pwcs != NULL) {
175 pwcs++;
176 n--;
177 }
178 cnt++;
179 }
180 if (pwcs)
181 *s = s0;
182
183 *nresult = (size_t)cnt;
184
185 return (0);
186 }
187
188 static int
189 _citrus_NONE_ctype_mbstowcs(void * __restrict cl, wchar_t * __restrict wcs,
190 const char * __restrict s, size_t n,
191 size_t * __restrict nresult)
192 {
193 return (_citrus_NONE_ctype_mbsrtowcs(cl, wcs, (const char **)&s, n, NULL, nresult));
194 }
195
196 static int
197 /*ARGSUSED*/
198 _citrus_NONE_ctype_mbtowc(void * __restrict cl, wchar_t * __restrict pwc,
199 const char * __restrict s, size_t n,
200 int * __restrict nresult)
201 {
202
203 if (s == NULL) {
204 *nresult = 0; /* state independent */
205 return (0);
206 }
207 if (n == 0) {
208 return (EILSEQ);
209 }
210 if (pwc == NULL) {
211 if (*s == '\0') {
212 *nresult = 0;
213 } else {
214 *nresult = 1;
215 }
216 return (0);
217 }
218
219 *pwc = (wchar_t)(unsigned char)*s;
220 *nresult = *s == '\0' ? 0 : 1;
221
222 return (0);
223 }
224
225 static int
226 /*ARGSUSED*/
227 _citrus_NONE_ctype_wcrtomb(void * __restrict cl, char * __restrict s,
228 wchar_t wc, void * __restrict pspriv,
229 size_t * __restrict nresult)
230 {
231 if ((wc&~0xFFU) != 0) {
232 *nresult = (size_t)-1;
233 return (EILSEQ);
234 }
235
236 *nresult = 1;
237 if (s!=NULL)
238 *s = (char)wc;
239
240 return (0);
241 }
242
243 static int
244 /*ARGSUSED*/
245 _citrus_NONE_ctype_wcsrtombs(void * __restrict cl, char * __restrict s,
246 const wchar_t ** __restrict pwcs, size_t n,
247 void * __restrict pspriv,
248 size_t * __restrict nresult)
249 {
250 size_t count;
251 const wchar_t *pwcs0;
252
253 pwcs0 = *pwcs;
254 count = 0;
255
256 if (s == NULL)
257 n = 1;
258
259 while (n > 0) {
260 if ((*pwcs0 & ~0xFFU) != 0) {
261 *nresult = (size_t)-1;
262 return (EILSEQ);
263 }
264 if (s != NULL) {
265 *s++ = (char)*pwcs0;
266 n--;
267 }
268 if (*pwcs0 == L'\0') {
269 pwcs0 = NULL;
270 break;
271 }
272 count++;
273 pwcs0++;
274 }
275 if (s != NULL)
276 *pwcs = pwcs0;
277
278 *nresult = count;
279
280 return (0);
281 }
282
283 static int
284 _citrus_NONE_ctype_wcstombs(void * __restrict cl, char * __restrict s,
285 const wchar_t * __restrict pwcs, size_t n,
286 size_t * __restrict nresult)
287 {
288 return (_citrus_NONE_ctype_wcsrtombs(cl, s, (const wchar_t **)&pwcs, n, NULL, nresult));
289 }
290
291 static int
292 _citrus_NONE_ctype_wctomb(void * __restrict cl, char * __restrict s,
293 wchar_t wc, int * __restrict nresult)
294 {
295 int ret;
296 size_t nr;
297
298 if (s == 0) {
299 /*
300 * initialize state here.
301 * (nothing to do for us.)
302 */
303 *nresult = 0; /* we're state independent */
304 return (0);
305 }
306
307 ret = _citrus_NONE_ctype_wcrtomb(cl, s, wc, NULL, &nr);
308 *nresult = (int)nr;
309
310 return (ret);
311 }
312
313 static int
314 /*ARGSUSED*/
315 _citrus_NONE_ctype_btowc(_citrus_ctype_rec_t * __restrict cc,
316 int c, wint_t * __restrict wcresult)
317 {
318 if (c == EOF || c & ~0xFF)
319 *wcresult = WEOF;
320 else
321 *wcresult = (wint_t)c;
322 return (0);
323 }
324
325 static int
326 /*ARGSUSED*/
327 _citrus_NONE_ctype_wctob(_citrus_ctype_rec_t * __restrict cc,
328 wint_t wc, int * __restrict cresult)
329 {
330 if (wc == WEOF || wc & ~0xFF)
331 *cresult = EOF;
332 else
333 *cresult = (int)wc;
334 return (0);
335 }
336
337 /* ---------------------------------------------------------------------- */
338
339 _CITRUS_STDENC_DECLS(NONE);
340 _CITRUS_STDENC_DEF_OPS(NONE);
341 struct _citrus_stdenc_traits _citrus_NONE_stdenc_traits = {
342 0, /* et_state_size */
343 1, /* mb_cur_max */
344 };
345
346 static int
347 /*ARGSUSED*/
348 _citrus_NONE_stdenc_init(struct _citrus_stdenc * __restrict ce,
349 const void *var, size_t lenvar,
350 struct _citrus_stdenc_traits * __restrict et)
351 {
352
353 et->et_state_size = 0;
354 et->et_mb_cur_max = 1;
355
356 ce->ce_closure = NULL;
357
358 return (0);
359 }
360
361 static void
362 /*ARGSUSED*/
363 _citrus_NONE_stdenc_uninit(struct _citrus_stdenc *ce)
364 {
365 }
366
367 static int
368 /*ARGSUSED*/
369 _citrus_NONE_stdenc_init_state(struct _citrus_stdenc * __restrict ce,
370 void * __restrict ps)
371 {
372 return (0);
373 }
374
375 static int
376 /*ARGSUSED*/
377 _citrus_NONE_stdenc_mbtocs(struct _citrus_stdenc * __restrict ce,
378 _csid_t *csid, _index_t *idx,
379 const char **s, size_t n,
380 void *ps, size_t *nresult)
381 {
382
383 _DIAGASSERT(csid != NULL && idx != NULL);
384
385 if (n<1) {
386 *nresult = (size_t)-2;
387 return (0);
388 }
389
390 *csid = 0;
391 *idx = (_index_t)(unsigned char)*(*s)++;
392 *nresult = *idx == 0 ? 0 : 1;
393
394 return (0);
395 }
396
397 static int
398 /*ARGSUSED*/
399 _citrus_NONE_stdenc_cstomb(struct _citrus_stdenc * __restrict ce,
400 char *s, size_t n,
401 _csid_t csid, _index_t idx,
402 void *ps, size_t *nresult)
403 {
404
405 if (csid == _CITRUS_CSID_INVALID) {
406 *nresult = 0;
407 return (0);
408 }
409 if (n<1) {
410 *nresult = (size_t)-1;
411 return (E2BIG);
412 }
413 if (csid != 0 || (idx&0xFF) != idx)
414 return (EILSEQ);
415
416 *s = (char)idx;
417 *nresult = 1;
418
419 return (0);
420 }
421
422 static int
423 /*ARGSUSED*/
424 _citrus_NONE_stdenc_mbtowc(struct _citrus_stdenc * __restrict ce,
425 _wc_t * __restrict pwc,
426 const char ** __restrict s, size_t n,
427 void * __restrict pspriv,
428 size_t * __restrict nresult)
429 {
430 if (s == NULL) {
431 *nresult = 0;
432 return (0);
433 }
434 if (n == 0) {
435 *nresult = (size_t)-2;
436 return (0);
437 }
438
439 if (pwc != NULL)
440 *pwc = (_wc_t)(unsigned char) **s;
441
442 *nresult = *s == '\0' ? 0 : 1;
443 return (0);
444 }
445
446 static int
447 /*ARGSUSED*/
448 _citrus_NONE_stdenc_wctomb(struct _citrus_stdenc * __restrict ce,
449 char * __restrict s, size_t n,
450 _wc_t wc, void * __restrict pspriv,
451 size_t * __restrict nresult)
452 {
453 if ((wc&~0xFFU) != 0) {
454 *nresult = (size_t)-1;
455 return (EILSEQ);
456 }
457 if (n==0) {
458 *nresult = (size_t)-1;
459 return (E2BIG);
460 }
461
462 *nresult = 1;
463 if (s!=NULL && n>0)
464 *s = (char)wc;
465
466 return (0);
467 }
468
469 static int
470 /*ARGSUSED*/
471 _citrus_NONE_stdenc_put_state_reset(struct _citrus_stdenc * __restrict ce,
472 char * __restrict s, size_t n,
473 void * __restrict pspriv,
474 size_t * __restrict nresult)
475 {
476
477 *nresult = 0;
478
479 return (0);
480 }
481
482 static int
483 /*ARGSUSED*/
484 _citrus_NONE_stdenc_get_state_desc(struct _stdenc * __restrict ce,
485 void * __restrict ps,
486 int id,
487 struct _stdenc_state_desc * __restrict d)
488 {
489 int ret;
490
491 switch (id) {
492 case _STDENC_SDID_GENERIC:
493 d->u.generic.state = _STDENC_SDGEN_INITIAL;
494 break;
495 default:
496 ret = EOPNOTSUPP;
497 }
498
499 return ret;
500 }
501