14297a3b0SGarrett D'Amore /*
2*2d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2002-2004 Tim J. Robbins.
54297a3b0SGarrett D'Amore  * All rights reserved.
64297a3b0SGarrett D'Amore  *
74297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
84297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
94297a3b0SGarrett D'Amore  * are met:
104297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
114297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
124297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
134297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
144297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
154297a3b0SGarrett D'Amore  *
164297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264297a3b0SGarrett D'Amore  * SUCH DAMAGE.
274297a3b0SGarrett D'Amore  */
284297a3b0SGarrett D'Amore 
294297a3b0SGarrett D'Amore #include "lint.h"
304297a3b0SGarrett D'Amore #include <limits.h>
314297a3b0SGarrett D'Amore #include <stdlib.h>
324297a3b0SGarrett D'Amore #include <string.h>
33*2d08521bSGarrett D'Amore #include <locale.h>
344297a3b0SGarrett D'Amore #include <wchar.h>
354297a3b0SGarrett D'Amore #include "mblocal.h"
36*2d08521bSGarrett D'Amore #include "localeimpl.h"
37*2d08521bSGarrett D'Amore #include "lctype.h"
384297a3b0SGarrett D'Amore 
394297a3b0SGarrett D'Amore size_t
wcsnrtombs_l(char * _RESTRICT_KYWD dst,const wchar_t ** _RESTRICT_KYWD src,size_t nwc,size_t len,mbstate_t * _RESTRICT_KYWD ps,locale_t loc)40*2d08521bSGarrett D'Amore wcsnrtombs_l(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
41*2d08521bSGarrett D'Amore     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps, locale_t loc)
424297a3b0SGarrett D'Amore {
434297a3b0SGarrett D'Amore 	static mbstate_t mbs;
444297a3b0SGarrett D'Amore 
454297a3b0SGarrett D'Amore 	if (ps == NULL)
464297a3b0SGarrett D'Amore 		ps = &mbs;
47*2d08521bSGarrett D'Amore 	return (loc->ctype->lc_wcsnrtombs(dst, src, nwc, len, ps));
484297a3b0SGarrett D'Amore }
494297a3b0SGarrett D'Amore 
504297a3b0SGarrett D'Amore size_t
wcsnrtombs(char * _RESTRICT_KYWD dst,const wchar_t ** _RESTRICT_KYWD src,size_t nwc,size_t len,mbstate_t * _RESTRICT_KYWD ps)51*2d08521bSGarrett D'Amore wcsnrtombs(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
524297a3b0SGarrett D'Amore     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps)
53*2d08521bSGarrett D'Amore {
54*2d08521bSGarrett D'Amore 	return (wcsnrtombs_l(dst, src, nwc, len, ps, uselocale(NULL)));
55*2d08521bSGarrett D'Amore }
56*2d08521bSGarrett D'Amore 
57*2d08521bSGarrett D'Amore size_t
__wcsnrtombs_std(char * _RESTRICT_KYWD dst,const wchar_t ** _RESTRICT_KYWD src,size_t nwc,size_t len,mbstate_t * _RESTRICT_KYWD ps,wcrtomb_pfn_t pwcrtomb)58*2d08521bSGarrett D'Amore __wcsnrtombs_std(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
59*2d08521bSGarrett D'Amore     size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD ps,
60*2d08521bSGarrett D'Amore     wcrtomb_pfn_t pwcrtomb)
614297a3b0SGarrett D'Amore {
624297a3b0SGarrett D'Amore 	mbstate_t mbsbak;
634297a3b0SGarrett D'Amore 	char buf[MB_LEN_MAX];
644297a3b0SGarrett D'Amore 	const wchar_t *s;
654297a3b0SGarrett D'Amore 	size_t nbytes;
664297a3b0SGarrett D'Amore 	size_t nb;
674297a3b0SGarrett D'Amore 
684297a3b0SGarrett D'Amore 	s = *src;
694297a3b0SGarrett D'Amore 	nbytes = 0;
704297a3b0SGarrett D'Amore 
714297a3b0SGarrett D'Amore 	if (dst == NULL) {
724297a3b0SGarrett D'Amore 		while (nwc-- > 0) {
73*2d08521bSGarrett D'Amore 			if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1)
744297a3b0SGarrett D'Amore 				/* Invalid character - wcrtomb() sets errno. */
754297a3b0SGarrett D'Amore 				return ((size_t)-1);
764297a3b0SGarrett D'Amore 			else if (*s == L'\0')
774297a3b0SGarrett D'Amore 				return (nbytes + nb - 1);
784297a3b0SGarrett D'Amore 			s++;
794297a3b0SGarrett D'Amore 			nbytes += nb;
804297a3b0SGarrett D'Amore 		}
814297a3b0SGarrett D'Amore 		return (nbytes);
824297a3b0SGarrett D'Amore 	}
834297a3b0SGarrett D'Amore 
844297a3b0SGarrett D'Amore 	while (len > 0 && nwc-- > 0) {
854297a3b0SGarrett D'Amore 		if (len > (size_t)MB_CUR_MAX) {
864297a3b0SGarrett D'Amore 			/* Enough space to translate in-place. */
87*2d08521bSGarrett D'Amore 			if ((nb = pwcrtomb(dst, *s, ps)) == (size_t)-1) {
884297a3b0SGarrett D'Amore 				*src = s;
894297a3b0SGarrett D'Amore 				return ((size_t)-1);
904297a3b0SGarrett D'Amore 			}
914297a3b0SGarrett D'Amore 		} else {
924297a3b0SGarrett D'Amore 			/*
934297a3b0SGarrett D'Amore 			 * May not be enough space; use temp. buffer.
944297a3b0SGarrett D'Amore 			 *
954297a3b0SGarrett D'Amore 			 * We need to save a copy of the conversion state
964297a3b0SGarrett D'Amore 			 * here so we can restore it if the multibyte
974297a3b0SGarrett D'Amore 			 * character is too long for the buffer.
984297a3b0SGarrett D'Amore 			 */
994297a3b0SGarrett D'Amore 			mbsbak = *ps;
100*2d08521bSGarrett D'Amore 			if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1) {
1014297a3b0SGarrett D'Amore 				*src = s;
1024297a3b0SGarrett D'Amore 				return ((size_t)-1);
1034297a3b0SGarrett D'Amore 			}
1044297a3b0SGarrett D'Amore 			if (nb > (int)len) {
1054297a3b0SGarrett D'Amore 				/* MB sequence for character won't fit. */
1064297a3b0SGarrett D'Amore 				*ps = mbsbak;
1074297a3b0SGarrett D'Amore 				break;
1084297a3b0SGarrett D'Amore 			}
1094297a3b0SGarrett D'Amore 			(void) memcpy(dst, buf, nb);
1104297a3b0SGarrett D'Amore 		}
1114297a3b0SGarrett D'Amore 		if (*s == L'\0') {
1124297a3b0SGarrett D'Amore 			*src = NULL;
1134297a3b0SGarrett D'Amore 			return (nbytes + nb - 1);
1144297a3b0SGarrett D'Amore 		}
1154297a3b0SGarrett D'Amore 		s++;
1164297a3b0SGarrett D'Amore 		dst += nb;
1174297a3b0SGarrett D'Amore 		len -= nb;
1184297a3b0SGarrett D'Amore 		nbytes += nb;
1194297a3b0SGarrett D'Amore 	}
1204297a3b0SGarrett D'Amore 	*src = s;
1214297a3b0SGarrett D'Amore 	return (nbytes);
1224297a3b0SGarrett D'Amore }
123