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