xref: /dflybsd-src/lib/libc/locale/mbsnrtowcs.c (revision 4776d4e822ede882d59eb2b58d6dfb813c6148ae)
1*4776d4e8SJohn Marino /*
2*4776d4e8SJohn Marino  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3*4776d4e8SJohn Marino  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
40d5acd74SJohn Marino  * Copyright (c) 2002-2004 Tim J. Robbins.
50d5acd74SJohn Marino  *
60d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
70d5acd74SJohn Marino  * All rights reserved.
80d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
90d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
100d5acd74SJohn Marino  * All rights reserved.
110d5acd74SJohn Marino  *
120d5acd74SJohn Marino  * Redistribution and use in source and binary forms, with or without
130d5acd74SJohn Marino  * modification, are permitted provided that the following conditions
140d5acd74SJohn Marino  * are met:
150d5acd74SJohn Marino  * 1. Redistributions of source code must retain the above copyright
160d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer.
170d5acd74SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
180d5acd74SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
190d5acd74SJohn Marino  *    documentation and/or other materials provided with the distribution.
200d5acd74SJohn Marino  *
210d5acd74SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
220d5acd74SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230d5acd74SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240d5acd74SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
250d5acd74SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260d5acd74SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270d5acd74SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280d5acd74SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290d5acd74SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300d5acd74SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310d5acd74SJohn Marino  * SUCH DAMAGE.
320d5acd74SJohn Marino  */
330d5acd74SJohn Marino 
340d5acd74SJohn Marino #include <errno.h>
350d5acd74SJohn Marino #include <limits.h>
360d5acd74SJohn Marino #include <stdlib.h>
370d5acd74SJohn Marino #include <wchar.h>
380d5acd74SJohn Marino #include "mblocal.h"
390d5acd74SJohn Marino 
400d5acd74SJohn Marino size_t
mbsnrtowcs_l(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps,locale_t locale)410d5acd74SJohn Marino mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src,
420d5acd74SJohn Marino     size_t nms, size_t len, mbstate_t * __restrict ps, locale_t locale)
430d5acd74SJohn Marino {
440d5acd74SJohn Marino 	FIX_LOCALE(locale);
450d5acd74SJohn Marino 	if (ps == NULL)
460d5acd74SJohn Marino 		ps = &locale->mbsnrtowcs;
470d5acd74SJohn Marino 	return (XLOCALE_CTYPE(locale)->__mbsnrtowcs(dst, src, nms, len, ps));
480d5acd74SJohn Marino }
490d5acd74SJohn Marino size_t
mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps)500d5acd74SJohn Marino mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
510d5acd74SJohn Marino     size_t nms, size_t len, mbstate_t * __restrict ps)
520d5acd74SJohn Marino {
530d5acd74SJohn Marino 	return mbsnrtowcs_l(dst, src, nms, len, ps, __get_locale());
540d5acd74SJohn Marino }
550d5acd74SJohn Marino 
560d5acd74SJohn Marino size_t
__mbsnrtowcs_std(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps,mbrtowc_pfn_t pmbrtowc)570d5acd74SJohn Marino __mbsnrtowcs_std(wchar_t * __restrict dst, const char ** __restrict src,
58*4776d4e8SJohn Marino     size_t nms, size_t len, mbstate_t * __restrict ps,
59*4776d4e8SJohn Marino     mbrtowc_pfn_t pmbrtowc)
600d5acd74SJohn Marino {
610d5acd74SJohn Marino 	const char *s;
620d5acd74SJohn Marino 	size_t nchr;
630d5acd74SJohn Marino 	wchar_t wc;
640d5acd74SJohn Marino 	size_t nb;
650d5acd74SJohn Marino 
660d5acd74SJohn Marino 	s = *src;
670d5acd74SJohn Marino 	nchr = 0;
680d5acd74SJohn Marino 
690d5acd74SJohn Marino 	if (dst == NULL) {
700d5acd74SJohn Marino 		for (;;) {
71*4776d4e8SJohn Marino 			if ((nb = pmbrtowc(&wc, s, nms, ps)) == (size_t)-1)
720d5acd74SJohn Marino 				/* Invalid sequence - mbrtowc() sets errno. */
730d5acd74SJohn Marino 				return ((size_t)-1);
740d5acd74SJohn Marino 			else if (nb == 0 || nb == (size_t)-2)
750d5acd74SJohn Marino 				return (nchr);
760d5acd74SJohn Marino 			s += nb;
770d5acd74SJohn Marino 			nms -= nb;
780d5acd74SJohn Marino 			nchr++;
790d5acd74SJohn Marino 		}
800d5acd74SJohn Marino 		/*NOTREACHED*/
810d5acd74SJohn Marino 	}
820d5acd74SJohn Marino 
830d5acd74SJohn Marino 	while (len-- > 0) {
84*4776d4e8SJohn Marino 		if ((nb = pmbrtowc(dst, s, nms, ps)) == (size_t)-1) {
850d5acd74SJohn Marino 			*src = s;
860d5acd74SJohn Marino 			return ((size_t)-1);
870d5acd74SJohn Marino 		} else if (nb == (size_t)-2) {
880d5acd74SJohn Marino 			*src = s + nms;
890d5acd74SJohn Marino 			return (nchr);
900d5acd74SJohn Marino 		} else if (nb == 0) {
910d5acd74SJohn Marino 			*src = NULL;
920d5acd74SJohn Marino 			return (nchr);
930d5acd74SJohn Marino 		}
940d5acd74SJohn Marino 		s += nb;
950d5acd74SJohn Marino 		nms -= nb;
960d5acd74SJohn Marino 		nchr++;
970d5acd74SJohn Marino 		dst++;
980d5acd74SJohn Marino 	}
990d5acd74SJohn Marino 	*src = s;
1000d5acd74SJohn Marino 	return (nchr);
1010d5acd74SJohn Marino }
102