xref: /freebsd/lib/libc/locale/wcsnrtombs.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
11949a347STim J. Robbins /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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.
71949a347STim J. Robbins  * All rights reserved.
81949a347STim J. Robbins  *
93c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
105b5fa75aSEd Maste  *
113c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
123c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
133c87aa1dSDavid Chisnall  *
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 <limits.h>
371949a347STim J. Robbins #include <stdlib.h>
381949a347STim J. Robbins #include <string.h>
391949a347STim J. Robbins #include <wchar.h>
401949a347STim J. Robbins #include "mblocal.h"
411949a347STim J. Robbins 
421949a347STim J. Robbins size_t
wcsnrtombs_l(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps,locale_t locale)433c87aa1dSDavid Chisnall wcsnrtombs_l(char * __restrict dst, const wchar_t ** __restrict src, size_t nwc,
443c87aa1dSDavid Chisnall     size_t len, mbstate_t * __restrict ps, locale_t locale)
453c87aa1dSDavid Chisnall {
463c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
473c87aa1dSDavid Chisnall 	if (ps == NULL)
489eb7d595SYuri Pankov 		ps = &(XLOCALE_CTYPE(locale)->wcsnrtombs);
493c87aa1dSDavid Chisnall 	return (XLOCALE_CTYPE(locale)->__wcsnrtombs(dst, src, nwc, len, ps));
503c87aa1dSDavid Chisnall }
513c87aa1dSDavid Chisnall size_t
wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps)521949a347STim J. Robbins wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src, size_t nwc,
531949a347STim J. Robbins     size_t len, mbstate_t * __restrict ps)
541949a347STim J. Robbins {
553c87aa1dSDavid Chisnall 	return wcsnrtombs_l(dst, src, nwc, len, ps, __get_locale());
561949a347STim J. Robbins }
571949a347STim J. Robbins 
583c87aa1dSDavid Chisnall 
591949a347STim J. Robbins size_t
__wcsnrtombs_std(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps,wcrtomb_pfn_t pwcrtomb)601949a347STim J. Robbins __wcsnrtombs_std(char * __restrict dst, const wchar_t ** __restrict src,
617b247341SBaptiste Daroussin     size_t nwc, size_t len, mbstate_t * __restrict ps,
627b247341SBaptiste Daroussin     wcrtomb_pfn_t pwcrtomb)
631949a347STim J. Robbins {
641949a347STim J. Robbins 	mbstate_t mbsbak;
651949a347STim J. Robbins 	char buf[MB_LEN_MAX];
661949a347STim J. Robbins 	const wchar_t *s;
671949a347STim J. Robbins 	size_t nbytes;
681949a347STim J. Robbins 	size_t nb;
691949a347STim J. Robbins 
701949a347STim J. Robbins 	s = *src;
711949a347STim J. Robbins 	nbytes = 0;
721949a347STim J. Robbins 
731949a347STim J. Robbins 	if (dst == NULL) {
741949a347STim J. Robbins 		while (nwc-- > 0) {
757b247341SBaptiste Daroussin 			if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1)
761949a347STim J. Robbins 				/* Invalid character - wcrtomb() sets errno. */
771949a347STim J. Robbins 				return ((size_t)-1);
781949a347STim J. Robbins 			else if (*s == L'\0')
796740cd83STim J. Robbins 				return (nbytes + nb - 1);
801949a347STim J. Robbins 			s++;
811949a347STim J. Robbins 			nbytes += nb;
821949a347STim J. Robbins 		}
836740cd83STim J. Robbins 		return (nbytes);
841949a347STim J. Robbins 	}
851949a347STim J. Robbins 
861949a347STim J. Robbins 	while (len > 0 && nwc-- > 0) {
871949a347STim J. Robbins 		if (len > (size_t)MB_CUR_MAX) {
881949a347STim J. Robbins 			/* Enough space to translate in-place. */
897b247341SBaptiste Daroussin 			if ((nb = pwcrtomb(dst, *s, ps)) == (size_t)-1) {
901949a347STim J. Robbins 				*src = s;
911949a347STim J. Robbins 				return ((size_t)-1);
921949a347STim J. Robbins 			}
931949a347STim J. Robbins 		} else {
941949a347STim J. Robbins 			/*
951949a347STim J. Robbins 			 * May not be enough space; use temp. buffer.
961949a347STim J. Robbins 			 *
971949a347STim J. Robbins 			 * We need to save a copy of the conversion state
981949a347STim J. Robbins 			 * here so we can restore it if the multibyte
991949a347STim J. Robbins 			 * character is too long for the buffer.
1001949a347STim J. Robbins 			 */
1011949a347STim J. Robbins 			mbsbak = *ps;
1027b247341SBaptiste Daroussin 			if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1) {
1031949a347STim J. Robbins 				*src = s;
1041949a347STim J. Robbins 				return ((size_t)-1);
1051949a347STim J. Robbins 			}
1061949a347STim J. Robbins 			if (nb > (int)len) {
1071949a347STim J. Robbins 				/* MB sequence for character won't fit. */
1081949a347STim J. Robbins 				*ps = mbsbak;
1091949a347STim J. Robbins 				break;
1101949a347STim J. Robbins 			}
1111949a347STim J. Robbins 			memcpy(dst, buf, nb);
1121949a347STim J. Robbins 		}
1131949a347STim J. Robbins 		if (*s == L'\0') {
1141949a347STim J. Robbins 			*src = NULL;
1151949a347STim J. Robbins 			return (nbytes + nb - 1);
1161949a347STim J. Robbins 		}
1171949a347STim J. Robbins 		s++;
1181949a347STim J. Robbins 		dst += nb;
1191949a347STim J. Robbins 		len -= nb;
1201949a347STim J. Robbins 		nbytes += nb;
1211949a347STim J. Robbins 	}
1221949a347STim J. Robbins 	*src = s;
1231949a347STim J. Robbins 	return (nbytes);
1241949a347STim J. Robbins }
125