ascii.c revision 175553
1175553Sache/*-
2175553Sache * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
3175553Sache * Copyright (c) 1993
4175553Sache *	The Regents of the University of California.  All rights reserved.
5175553Sache *
6175553Sache * This code is derived from software contributed to Berkeley by
7175553Sache * Paul Borman at Krystal Technologies.
8175553Sache *
9175553Sache * Redistribution and use in source and binary forms, with or without
10175553Sache * modification, are permitted provided that the following conditions
11175553Sache * are met:
12175553Sache * 1. Redistributions of source code must retain the above copyright
13175553Sache *    notice, this list of conditions and the following disclaimer.
14175553Sache * 2. Redistributions in binary form must reproduce the above copyright
15175553Sache *    notice, this list of conditions and the following disclaimer in the
16175553Sache *    documentation and/or other materials provided with the distribution.
17175553Sache * 4. Neither the name of the University nor the names of its contributors
18175553Sache *    may be used to endorse or promote products derived from this software
19175553Sache *    without specific prior written permission.
20175553Sache *
21175553Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22175553Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23175553Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24175553Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25175553Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26175553Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27175553Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28175553Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29175553Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30175553Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31175553Sache * SUCH DAMAGE.
32175553Sache */
33175553Sache
34175553Sache#include <sys/cdefs.h>
35175553Sache__FBSDID("$FreeBSD: head/lib/libc/locale/ascii.c 175553 2008-01-21 23:48:12Z ache $");
36175553Sache
37175553Sache#include <errno.h>
38175553Sache#include <limits.h>
39175553Sache#include <runetype.h>
40175553Sache#include <stddef.h>
41175553Sache#include <stdio.h>
42175553Sache#include <stdlib.h>
43175553Sache#include <string.h>
44175553Sache#include <wchar.h>
45175553Sache#include "mblocal.h"
46175553Sache
47175553Sachestatic size_t	_ascii_mbrtowc(wchar_t * __restrict, const char * __restrict,
48175553Sache		    size_t, mbstate_t * __restrict);
49175553Sachestatic int	_ascii_mbsinit(const mbstate_t *);
50175553Sachestatic size_t	_ascii_mbsnrtowcs(wchar_t * __restrict dst,
51175553Sache		    const char ** __restrict src, size_t nms, size_t len,
52175553Sache		    mbstate_t * __restrict ps __unused);
53175553Sachestatic size_t	_ascii_wcrtomb(char * __restrict, wchar_t,
54175553Sache		    mbstate_t * __restrict);
55175553Sachestatic size_t	_ascii_wcsnrtombs(char * __restrict, const wchar_t ** __restrict,
56175553Sache		    size_t, size_t, mbstate_t * __restrict);
57175553Sache
58175553Sacheint
59175553Sache_ascii_init(_RuneLocale *rl)
60175553Sache{
61175553Sache
62175553Sache	__mbrtowc = _ascii_mbrtowc;
63175553Sache	__mbsinit = _ascii_mbsinit;
64175553Sache	__mbsnrtowcs = _ascii_mbsnrtowcs;
65175553Sache	__wcrtomb = _ascii_wcrtomb;
66175553Sache	__wcsnrtombs = _ascii_wcsnrtombs;
67175553Sache	_CurrentRuneLocale = rl;
68175553Sache	__mb_cur_max = 1;
69175553Sache	__mb_sb_limit = 128;
70175553Sache	return(0);
71175553Sache}
72175553Sache
73175553Sachestatic int
74175553Sache_ascii_mbsinit(const mbstate_t *ps __unused)
75175553Sache{
76175553Sache
77175553Sache	/*
78175553Sache	 * Encoding is not state dependent - we are always in the
79175553Sache	 * initial state.
80175553Sache	 */
81175553Sache	return (1);
82175553Sache}
83175553Sache
84175553Sachestatic size_t
85175553Sache_ascii_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
86175553Sache    mbstate_t * __restrict ps __unused)
87175553Sache{
88175553Sache
89175553Sache	if (s == NULL)
90175553Sache		/* Reset to initial shift state (no-op) */
91175553Sache		return (0);
92175553Sache	if (n == 0)
93175553Sache		/* Incomplete multibyte sequence */
94175553Sache		return ((size_t)-2);
95175553Sache	if (*s & 0x80) {
96175553Sache		errno = EILSEQ;
97175553Sache		return ((size_t)-1);
98175553Sache	}
99175553Sache	if (pwc != NULL)
100175553Sache		*pwc = (unsigned char)*s;
101175553Sache	return (*s == '\0' ? 0 : 1);
102175553Sache}
103175553Sache
104175553Sachestatic size_t
105175553Sache_ascii_wcrtomb(char * __restrict s, wchar_t wc,
106175553Sache    mbstate_t * __restrict ps __unused)
107175553Sache{
108175553Sache
109175553Sache	if (s == NULL)
110175553Sache		/* Reset to initial shift state (no-op) */
111175553Sache		return (1);
112175553Sache	if (wc < 0 || wc > 127) {
113175553Sache		errno = EILSEQ;
114175553Sache		return ((size_t)-1);
115175553Sache	}
116175553Sache	*s = (unsigned char)wc;
117175553Sache	return (1);
118175553Sache}
119175553Sache
120175553Sachestatic size_t
121175553Sache_ascii_mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
122175553Sache    size_t nms, size_t len, mbstate_t * __restrict ps __unused)
123175553Sache{
124175553Sache	const char *s;
125175553Sache	size_t nchr;
126175553Sache
127175553Sache	if (dst == NULL) {
128175553Sache		for (s = *src; nms > 0 && *s != '\0'; s++, nms--) {
129175553Sache			if (*s & 0x80) {
130175553Sache				errno = EILSEQ;
131175553Sache				return ((size_t)-1);
132175553Sache			}
133175553Sache		}
134175553Sache		return (s - *src);
135175553Sache	}
136175553Sache
137175553Sache	s = *src;
138175553Sache	nchr = 0;
139175553Sache	while (len-- > 0 && nms-- > 0) {
140175553Sache		if (*s & 0x80) {
141175553Sache			errno = EILSEQ;
142175553Sache			return ((size_t)-1);
143175553Sache		}
144175553Sache		if ((*dst++ = (unsigned char)*s++) == L'\0') {
145175553Sache			*src = NULL;
146175553Sache			return (nchr);
147175553Sache		}
148175553Sache		nchr++;
149175553Sache	}
150175553Sache	*src = s;
151175553Sache	return (nchr);
152175553Sache}
153175553Sache
154175553Sachestatic size_t
155175553Sache_ascii_wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src,
156175553Sache    size_t nwc, size_t len, mbstate_t * __restrict ps __unused)
157175553Sache{
158175553Sache	const wchar_t *s;
159175553Sache	size_t nchr;
160175553Sache
161175553Sache	if (dst == NULL) {
162175553Sache		for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
163175553Sache			if (*s < 0 || *s > 127) {
164175553Sache				errno = EILSEQ;
165175553Sache				return ((size_t)-1);
166175553Sache			}
167175553Sache		}
168175553Sache		return (s - *src);
169175553Sache	}
170175553Sache
171175553Sache	s = *src;
172175553Sache	nchr = 0;
173175553Sache	while (len-- > 0 && nwc-- > 0) {
174175553Sache		if (*s < 0 || *s > 127) {
175175553Sache			errno = EILSEQ;
176175553Sache			return ((size_t)-1);
177175553Sache		}
178175553Sache		if ((*dst++ = *s++) == '\0') {
179175553Sache			*src = NULL;
180175553Sache			return (nchr);
181175553Sache		}
182175553Sache		nchr++;
183175553Sache	}
184175553Sache	*src = s;
185175553Sache	return (nchr);
186175553Sache}
187175553Sache
188