11573Srgrimes/*-
2128004Stjr * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
31573Srgrimes * Copyright (c) 1993
41573Srgrimes *	The Regents of the University of California.  All rights reserved.
51573Srgrimes *
61573Srgrimes * This code is derived from software contributed to Berkeley by
71573Srgrimes * Paul Borman at Krystal Technologies.
81573Srgrimes *
9227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
10227753Stheraven * All rights reserved.
11227753Stheraven * Portions of this software were developed by David Chisnall
12227753Stheraven * under sponsorship from the FreeBSD Foundation.
13227753Stheraven *
141573Srgrimes * Redistribution and use in source and binary forms, with or without
151573Srgrimes * modification, are permitted provided that the following conditions
161573Srgrimes * are met:
171573Srgrimes * 1. Redistributions of source code must retain the above copyright
181573Srgrimes *    notice, this list of conditions and the following disclaimer.
191573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
201573Srgrimes *    notice, this list of conditions and the following disclaimer in the
211573Srgrimes *    documentation and/or other materials provided with the distribution.
221573Srgrimes * 4. Neither the name of the University nor the names of its contributors
231573Srgrimes *    may be used to endorse or promote products derived from this software
241573Srgrimes *    without specific prior written permission.
251573Srgrimes *
261573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361573Srgrimes * SUCH DAMAGE.
371573Srgrimes */
381573Srgrimes
391573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
401573Srgrimesstatic char sccsid[] = "@(#)none.c	8.1 (Berkeley) 6/4/93";
411573Srgrimes#endif /* LIBC_SCCS and not lint */
4292986Sobrien#include <sys/cdefs.h>
4392986Sobrien__FBSDID("$FreeBSD: releng/10.2/lib/libc/locale/none.c 227753 2011-11-20 14:45:42Z theraven $");
441573Srgrimes
45121845Stjr#include <errno.h>
46105233Stjr#include <limits.h>
47121845Stjr#include <runetype.h>
481573Srgrimes#include <stddef.h>
491573Srgrimes#include <stdio.h>
501573Srgrimes#include <stdlib.h>
51129179Stjr#include <string.h>
52121845Stjr#include <wchar.h>
53129153Stjr#include "mblocal.h"
541573Srgrimes
55142654Sphantomstatic size_t	_none_mbrtowc(wchar_t * __restrict, const char * __restrict,
56142654Sphantom		    size_t, mbstate_t * __restrict);
57142654Sphantomstatic int	_none_mbsinit(const mbstate_t *);
58142654Sphantomstatic size_t	_none_mbsnrtowcs(wchar_t * __restrict dst,
59142654Sphantom		    const char ** __restrict src, size_t nms, size_t len,
60142654Sphantom		    mbstate_t * __restrict ps __unused);
61142654Sphantomstatic size_t	_none_wcrtomb(char * __restrict, wchar_t,
62142654Sphantom		    mbstate_t * __restrict);
63142654Sphantomstatic size_t	_none_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
64142654Sphantom		    size_t, size_t, mbstate_t * __restrict);
65121845Stjr
66172619Sache/* setup defaults */
67172619Sache
68172619Sacheint __mb_cur_max = 1;
69172619Sacheint __mb_sb_limit = 256; /* Expected to be <= _CACHED_RUNES */
70172619Sache
711573Srgrimesint
72227753Stheraven_none_init(struct xlocale_ctype *l, _RuneLocale *rl)
731573Srgrimes{
74121845Stjr
75227753Stheraven	l->__mbrtowc = _none_mbrtowc;
76227753Stheraven	l->__mbsinit = _none_mbsinit;
77227753Stheraven	l->__mbsnrtowcs = _none_mbsnrtowcs;
78227753Stheraven	l->__wcrtomb = _none_wcrtomb;
79227753Stheraven	l->__wcsnrtombs = _none_wcsnrtombs;
80227753Stheraven	l->runes = rl;
81227753Stheraven	l->__mb_cur_max = 1;
82227753Stheraven	l->__mb_sb_limit = 256;
831573Srgrimes	return(0);
841573Srgrimes}
851573Srgrimes
86142654Sphantomstatic int
87128004Stjr_none_mbsinit(const mbstate_t *ps __unused)
88128004Stjr{
89128004Stjr
90128004Stjr	/*
91128004Stjr	 * Encoding is not state dependent - we are always in the
92128004Stjr	 * initial state.
93128004Stjr	 */
94128004Stjr	return (1);
95128004Stjr}
96128004Stjr
97142654Sphantomstatic size_t
98121845Stjr_none_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
99121845Stjr    mbstate_t * __restrict ps __unused)
1001573Srgrimes{
101121845Stjr
102121845Stjr	if (s == NULL)
103121845Stjr		/* Reset to initial shift state (no-op) */
104121845Stjr		return (0);
105121845Stjr	if (n == 0)
106121845Stjr		/* Incomplete multibyte sequence */
107121845Stjr		return ((size_t)-2);
108121845Stjr	if (pwc != NULL)
109121845Stjr		*pwc = (unsigned char)*s;
110121845Stjr	return (*s == '\0' ? 0 : 1);
1111573Srgrimes}
1121573Srgrimes
113142654Sphantomstatic size_t
114121845Stjr_none_wcrtomb(char * __restrict s, wchar_t wc,
115121845Stjr    mbstate_t * __restrict ps __unused)
1161573Srgrimes{
117121845Stjr
118121845Stjr	if (s == NULL)
119121845Stjr		/* Reset to initial shift state (no-op) */
120121845Stjr		return (1);
121121845Stjr	if (wc < 0 || wc > UCHAR_MAX) {
122121845Stjr		errno = EILSEQ;
123121845Stjr		return ((size_t)-1);
124121845Stjr	}
125121845Stjr	*s = (unsigned char)wc;
126121845Stjr	return (1);
1271573Srgrimes}
128129179Stjr
129142654Sphantomstatic size_t
130132497Stjr_none_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
131132497Stjr    size_t nms, size_t len, mbstate_t * __restrict ps __unused)
132129179Stjr{
133129179Stjr	const char *s;
134129179Stjr	size_t nchr;
135129179Stjr
136132497Stjr	if (dst == NULL) {
137132497Stjr		s = memchr(*src, '\0', nms);
138132497Stjr		return (s != NULL ? s - *src : nms);
139132497Stjr	}
140129179Stjr
141129179Stjr	s = *src;
142129179Stjr	nchr = 0;
143132497Stjr	while (len-- > 0 && nms-- > 0) {
144129179Stjr		if ((*dst++ = (unsigned char)*s++) == L'\0') {
145129179Stjr			*src = NULL;
146129179Stjr			return (nchr);
147129179Stjr		}
148129179Stjr		nchr++;
149129179Stjr	}
150129179Stjr	*src = s;
151129179Stjr	return (nchr);
152129179Stjr}
153129179Stjr
154142654Sphantomstatic size_t
155132497Stjr_none_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
156132497Stjr    size_t nwc, size_t len, mbstate_t * __restrict ps __unused)
157129179Stjr{
158129179Stjr	const wchar_t *s;
159129179Stjr	size_t nchr;
160129179Stjr
161129707Stjr	if (dst == NULL) {
162132497Stjr		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
163129707Stjr			if (*s < 0 || *s > UCHAR_MAX) {
164129707Stjr				errno = EILSEQ;
165129707Stjr				return ((size_t)-1);
166129707Stjr			}
167129707Stjr		}
168129707Stjr		return (s - *src);
169129707Stjr	}
170129179Stjr
171129179Stjr	s = *src;
172129179Stjr	nchr = 0;
173132497Stjr	while (len-- > 0 && nwc-- > 0) {
174129179Stjr		if (*s < 0 || *s > UCHAR_MAX) {
175129179Stjr			errno = EILSEQ;
176129179Stjr			return ((size_t)-1);
177129179Stjr		}
178129179Stjr		if ((*dst++ = *s++) == '\0') {
179129179Stjr			*src = NULL;
180129179Stjr			return (nchr);
181129179Stjr		}
182129179Stjr		nchr++;
183129179Stjr	}
184129179Stjr	*src = s;
185129179Stjr	return (nchr);
186129179Stjr}
187142654Sphantom
188142654Sphantom/* setup defaults */
189142654Sphantom
190142654Sphantomsize_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, size_t,
191142654Sphantom    mbstate_t * __restrict) = _none_mbrtowc;
192142654Sphantomint (*__mbsinit)(const mbstate_t *) = _none_mbsinit;
193142654Sphantomsize_t (*__mbsnrtowcs)(wchar_t * __restrict, const char ** __restrict,
194142654Sphantom    size_t, size_t, mbstate_t * __restrict) = _none_mbsnrtowcs;
195142654Sphantomsize_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict) =
196142654Sphantom    _none_wcrtomb;
197142654Sphantomsize_t (*__wcsnrtombs)(char * __restrict, const wchar_t ** __restrict,
198142654Sphantom    size_t, size_t, mbstate_t * __restrict) = _none_wcsnrtombs;
199142654Sphantom
200227753Stheravenstruct xlocale_ctype __xlocale_global_ctype = {
201227753Stheraven	{{0}, "C"},
202227753Stheraven	(_RuneLocale*)&_DefaultRuneLocale,
203227753Stheraven	_none_mbrtowc,
204227753Stheraven	_none_mbsinit,
205227753Stheraven	_none_mbsnrtowcs,
206227753Stheraven	_none_wcrtomb,
207227753Stheraven	_none_wcsnrtombs,
208227753Stheraven	1, /* __mb_cur_max, */
209227753Stheraven	256 /* __mb_sb_limit */
210227753Stheraven};
211227753Stheraven
212227753Stheravenconst struct xlocale_ctype __xlocale_C_ctype = {
213227753Stheraven	{{0}, "C"},
214227753Stheraven	(_RuneLocale*)&_DefaultRuneLocale,
215227753Stheraven	_none_mbrtowc,
216227753Stheraven	_none_mbsinit,
217227753Stheraven	_none_mbsnrtowcs,
218227753Stheraven	_none_wcrtomb,
219227753Stheraven	_none_wcsnrtombs,
220227753Stheraven	1, /* __mb_cur_max, */
221227753Stheraven	256 /* __mb_sb_limit */
222227753Stheraven};
223