xref: /freebsd/lib/libc/locale/mbsnrtowcs.c (revision 7b2473410fcb28134729c5781ccb21f47a250a09)
11949a347STim J. Robbins /*-
2*7b247341SBaptiste Daroussin  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3*7b247341SBaptiste Daroussin  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
41949a347STim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
53c87aa1dSDavid Chisnall  *
63c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
73c87aa1dSDavid Chisnall  * All rights reserved.
83c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
93c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
101949a347STim J. Robbins  * All rights reserved.
111949a347STim J. Robbins  *
121949a347STim J. Robbins  * Redistribution and use in source and binary forms, with or without
131949a347STim J. Robbins  * modification, are permitted provided that the following conditions
141949a347STim J. Robbins  * are met:
151949a347STim J. Robbins  * 1. Redistributions of source code must retain the above copyright
161949a347STim J. Robbins  *    notice, this list of conditions and the following disclaimer.
171949a347STim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
181949a347STim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
191949a347STim J. Robbins  *    documentation and/or other materials provided with the distribution.
201949a347STim J. Robbins  *
211949a347STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
221949a347STim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231949a347STim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241949a347STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
251949a347STim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261949a347STim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271949a347STim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281949a347STim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291949a347STim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301949a347STim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311949a347STim J. Robbins  * SUCH DAMAGE.
321949a347STim J. Robbins  */
331949a347STim J. Robbins 
341949a347STim J. Robbins #include <sys/cdefs.h>
351949a347STim J. Robbins __FBSDID("$FreeBSD$");
361949a347STim J. Robbins 
371949a347STim J. Robbins #include <errno.h>
381949a347STim J. Robbins #include <limits.h>
391949a347STim J. Robbins #include <stdlib.h>
401949a347STim J. Robbins #include <wchar.h>
411949a347STim J. Robbins #include "mblocal.h"
421949a347STim J. Robbins 
431949a347STim J. Robbins size_t
443c87aa1dSDavid Chisnall mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src,
453c87aa1dSDavid Chisnall     size_t nms, size_t len, mbstate_t * __restrict ps, locale_t locale)
463c87aa1dSDavid Chisnall {
473c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
483c87aa1dSDavid Chisnall 	if (ps == NULL)
493c87aa1dSDavid Chisnall 		ps = &locale->mbsnrtowcs;
503c87aa1dSDavid Chisnall 	return (XLOCALE_CTYPE(locale)->__mbsnrtowcs(dst, src, nms, len, ps));
513c87aa1dSDavid Chisnall }
523c87aa1dSDavid Chisnall size_t
531949a347STim J. Robbins mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
541949a347STim J. Robbins     size_t nms, size_t len, mbstate_t * __restrict ps)
551949a347STim J. Robbins {
563c87aa1dSDavid Chisnall 	return mbsnrtowcs_l(dst, src, nms, len, ps, __get_locale());
571949a347STim J. Robbins }
581949a347STim J. Robbins 
591949a347STim J. Robbins size_t
601949a347STim J. Robbins __mbsnrtowcs_std(wchar_t * __restrict dst, const char ** __restrict src,
61*7b247341SBaptiste Daroussin     size_t nms, size_t len, mbstate_t * __restrict ps,
62*7b247341SBaptiste Daroussin     mbrtowc_pfn_t pmbrtowc)
631949a347STim J. Robbins {
641949a347STim J. Robbins 	const char *s;
651949a347STim J. Robbins 	size_t nchr;
661949a347STim J. Robbins 	wchar_t wc;
671949a347STim J. Robbins 	size_t nb;
681949a347STim J. Robbins 
691949a347STim J. Robbins 	s = *src;
701949a347STim J. Robbins 	nchr = 0;
711949a347STim J. Robbins 
721949a347STim J. Robbins 	if (dst == NULL) {
731949a347STim J. Robbins 		for (;;) {
74*7b247341SBaptiste Daroussin 			if ((nb = pmbrtowc(&wc, s, nms, ps)) == (size_t)-1)
751949a347STim J. Robbins 				/* Invalid sequence - mbrtowc() sets errno. */
761949a347STim J. Robbins 				return ((size_t)-1);
771949a347STim J. Robbins 			else if (nb == 0 || nb == (size_t)-2)
781949a347STim J. Robbins 				return (nchr);
791949a347STim J. Robbins 			s += nb;
801949a347STim J. Robbins 			nms -= nb;
811949a347STim J. Robbins 			nchr++;
821949a347STim J. Robbins 		}
831949a347STim J. Robbins 		/*NOTREACHED*/
841949a347STim J. Robbins 	}
851949a347STim J. Robbins 
861949a347STim J. Robbins 	while (len-- > 0) {
87*7b247341SBaptiste Daroussin 		if ((nb = pmbrtowc(dst, s, nms, ps)) == (size_t)-1) {
881949a347STim J. Robbins 			*src = s;
891949a347STim J. Robbins 			return ((size_t)-1);
901949a347STim J. Robbins 		} else if (nb == (size_t)-2) {
911949a347STim J. Robbins 			*src = s + nms;
921949a347STim J. Robbins 			return (nchr);
931949a347STim J. Robbins 		} else if (nb == 0) {
941949a347STim J. Robbins 			*src = NULL;
951949a347STim J. Robbins 			return (nchr);
961949a347STim J. Robbins 		}
971949a347STim J. Robbins 		s += nb;
981949a347STim J. Robbins 		nms -= nb;
991949a347STim J. Robbins 		nchr++;
1001949a347STim J. Robbins 		dst++;
1011949a347STim J. Robbins 	}
1021949a347STim J. Robbins 	*src = s;
1031949a347STim J. Robbins 	return (nchr);
1041949a347STim J. Robbins }
105