none.c revision 132497
1254721Semaste/*-
2254721Semaste * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3254721Semaste * Copyright (c) 1993
4254721Semaste *	The Regents of the University of California.  All rights reserved.
5254721Semaste *
6254721Semaste * This code is derived from software contributed to Berkeley by
7254721Semaste * Paul Borman at Krystal Technologies.
8254721Semaste *
9254721Semaste * Redistribution and use in source and binary forms, with or without
10254721Semaste * modification, are permitted provided that the following conditions
11254721Semaste * are met:
12254721Semaste * 1. Redistributions of source code must retain the above copyright
13254721Semaste *    notice, this list of conditions and the following disclaimer.
14254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
15254721Semaste *    notice, this list of conditions and the following disclaimer in the
16254721Semaste *    documentation and/or other materials provided with the distribution.
17254721Semaste * 3. All advertising materials mentioning features or use of this software
18254721Semaste *    must display the following acknowledgement:
19254721Semaste *	This product includes software developed by the University of
20254721Semaste *	California, Berkeley and its contributors.
21254721Semaste * 4. Neither the name of the University nor the names of its contributors
22254721Semaste *    may be used to endorse or promote products derived from this software
23254721Semaste *    without specific prior written permission.
24254721Semaste *
25254721Semaste * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35254721Semaste * SUCH DAMAGE.
36254721Semaste */
37254721Semaste
38254721Semaste#if defined(LIBC_SCCS) && !defined(lint)
39254721Semastestatic char sccsid[] = "@(#)none.c	8.1 (Berkeley) 6/4/93";
40254721Semaste#endif /* LIBC_SCCS and not lint */
41254721Semaste#include <sys/cdefs.h>
42254721Semaste__FBSDID("$FreeBSD: head/lib/libc/locale/none.c 132497 2004-07-21 10:54:57Z tjr $");
43254721Semaste
44254721Semaste#include <errno.h>
45254721Semaste#include <limits.h>
46254721Semaste#include <runetype.h>
47254721Semaste#include <stddef.h>
48254721Semaste#include <stdio.h>
49254721Semaste#include <stdlib.h>
50254721Semaste#include <string.h>
51254721Semaste#include <wchar.h>
52254721Semaste#include "mblocal.h"
53254721Semaste
54288943Sdimint	_none_init(_RuneLocale *);
55254721Semastesize_t	_none_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
56254721Semaste	    mbstate_t * __restrict);
57254721Semasteint	_none_mbsinit(const mbstate_t *);
58254721Semastesize_t	_none_mbsnrtowcs(wchar_t * __restrict dst,
59288943Sdim	    const char ** __restrict src, size_t nms, size_t len,
60288943Sdim	    mbstate_t * __restrict ps __unused);
61288943Sdimsize_t	_none_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
62254721Semastesize_t	_none_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
63288943Sdim	    size_t, size_t, mbstate_t * __restrict);
64254721Semaste
65254721Semasteint
66254721Semaste_none_init(_RuneLocale *rl)
67254721Semaste{
68288943Sdim
69254721Semaste	__mbrtowc = _none_mbrtowc;
70254721Semaste	__mbsinit = _none_mbsinit;
71254721Semaste	__mbsnrtowcs = _none_mbsnrtowcs;
72254721Semaste	__wcrtomb = _none_wcrtomb;
73288943Sdim	__wcsnrtombs = _none_wcsnrtombs;
74254721Semaste	_CurrentRuneLocale = rl;
75254721Semaste	__mb_cur_max = 1;
76254721Semaste	return(0);
77288943Sdim}
78254721Semaste
79254721Semasteint
80254721Semaste_none_mbsinit(const mbstate_t *ps __unused)
81254721Semaste{
82254721Semaste
83254721Semaste	/*
84254721Semaste	 * Encoding is not state dependent - we are always in the
85254721Semaste	 * initial state.
86254721Semaste	 */
87254721Semaste	return (1);
88288943Sdim}
89254721Semaste
90254721Semastesize_t
91254721Semaste_none_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
92288943Sdim    mbstate_t * __restrict ps __unused)
93254721Semaste{
94254721Semaste
95254721Semaste	if (s == NULL)
96254721Semaste		/* Reset to initial shift state (no-op) */
97280031Sdim		return (0);
98288943Sdim	if (n == 0)
99254721Semaste		/* Incomplete multibyte sequence */
100280031Sdim		return ((size_t)-2);
101280031Sdim	if (pwc != NULL)
102280031Sdim		*pwc = (unsigned char)*s;
103288943Sdim	return (*s == '\0' ? 0 : 1);
104280031Sdim}
105280031Sdim
106280031Sdimsize_t
107288943Sdim_none_wcrtomb(char * __restrict s, wchar_t wc,
108254721Semaste    mbstate_t * __restrict ps __unused)
109280031Sdim{
110280031Sdim
111280031Sdim	if (s == NULL)
112280031Sdim		/* Reset to initial shift state (no-op) */
113280031Sdim		return (1);
114280031Sdim	if (wc < 0 || wc > UCHAR_MAX) {
115280031Sdim		errno = EILSEQ;
116280031Sdim		return ((size_t)-1);
117254721Semaste	}
118254721Semaste	*s = (unsigned char)wc;
119254721Semaste	return (1);
120254721Semaste}
121254721Semaste
122280031Sdimsize_t
123254721Semaste_none_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
124254721Semaste    size_t nms, size_t len, mbstate_t * __restrict ps __unused)
125254721Semaste{
126254721Semaste	const char *s;
127254721Semaste	size_t nchr;
128254721Semaste
129288943Sdim	if (dst == NULL) {
130254721Semaste		s = memchr(*src, '\0', nms);
131254721Semaste		return (s != NULL ? s - *src : nms);
132254721Semaste	}
133254721Semaste
134254721Semaste	s = *src;
135254721Semaste	nchr = 0;
136288943Sdim	while (len-- > 0 && nms-- > 0) {
137254721Semaste		if ((*dst++ = (unsigned char)*s++) == L'\0') {
138254721Semaste			*src = NULL;
139254721Semaste			return (nchr);
140288943Sdim		}
141254721Semaste		nchr++;
142280031Sdim	}
143254721Semaste	*src = s;
144254721Semaste	return (nchr);
145254721Semaste}
146254721Semaste
147254721Semastesize_t
148254721Semaste_none_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
149254721Semaste    size_t nwc, size_t len, mbstate_t * __restrict ps __unused)
150254721Semaste{
151254721Semaste	const wchar_t *s;
152254721Semaste	size_t nchr;
153254721Semaste
154254721Semaste	if (dst == NULL) {
155254721Semaste		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
156254721Semaste			if (*s < 0 || *s > UCHAR_MAX) {
157254721Semaste				errno = EILSEQ;
158254721Semaste				return ((size_t)-1);
159254721Semaste			}
160254721Semaste		}
161254721Semaste		return (s - *src);
162254721Semaste	}
163254721Semaste
164254721Semaste	s = *src;
165254721Semaste	nchr = 0;
166254721Semaste	while (len-- > 0 && nwc-- > 0) {
167254721Semaste		if (*s < 0 || *s > UCHAR_MAX) {
168254721Semaste			errno = EILSEQ;
169254721Semaste			return ((size_t)-1);
170254721Semaste		}
171254721Semaste		if ((*dst++ = *s++) == '\0') {
172254721Semaste			*src = NULL;
173254721Semaste			return (nchr);
174254721Semaste		}
175254721Semaste		nchr++;
176254721Semaste	}
177254721Semaste	*src = s;
178254721Semaste	return (nchr);
179254721Semaste}
180254721Semaste