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 * All rights reserved.
60d5acd74SJohn Marino *
70d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
80d5acd74SJohn Marino * All rights reserved.
90d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
100d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
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 <limits.h>
350d5acd74SJohn Marino #include <stdlib.h>
360d5acd74SJohn Marino #include <string.h>
370d5acd74SJohn Marino #include <wchar.h>
380d5acd74SJohn Marino #include "mblocal.h"
390d5acd74SJohn Marino
400d5acd74SJohn Marino 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)410d5acd74SJohn Marino wcsnrtombs_l(char * __restrict dst, const wchar_t ** __restrict src, size_t nwc,
420d5acd74SJohn Marino 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->wcsnrtombs;
470d5acd74SJohn Marino return (XLOCALE_CTYPE(locale)->__wcsnrtombs(dst, src, nwc, len, ps));
480d5acd74SJohn Marino }
490d5acd74SJohn Marino size_t
wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps)500d5acd74SJohn Marino wcsnrtombs(char * __restrict dst, const wchar_t ** __restrict src, size_t nwc,
510d5acd74SJohn Marino size_t len, mbstate_t * __restrict ps)
520d5acd74SJohn Marino {
530d5acd74SJohn Marino return wcsnrtombs_l(dst, src, nwc, len, ps, __get_locale());
540d5acd74SJohn Marino }
550d5acd74SJohn Marino
560d5acd74SJohn Marino
570d5acd74SJohn Marino 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)580d5acd74SJohn Marino __wcsnrtombs_std(char * __restrict dst, const wchar_t ** __restrict src,
59*4776d4e8SJohn Marino size_t nwc, size_t len, mbstate_t * __restrict ps,
60*4776d4e8SJohn Marino wcrtomb_pfn_t pwcrtomb)
610d5acd74SJohn Marino {
620d5acd74SJohn Marino mbstate_t mbsbak;
630d5acd74SJohn Marino char buf[MB_LEN_MAX];
640d5acd74SJohn Marino const wchar_t *s;
650d5acd74SJohn Marino size_t nbytes;
660d5acd74SJohn Marino size_t nb;
670d5acd74SJohn Marino
680d5acd74SJohn Marino s = *src;
690d5acd74SJohn Marino nbytes = 0;
700d5acd74SJohn Marino
710d5acd74SJohn Marino if (dst == NULL) {
720d5acd74SJohn Marino while (nwc-- > 0) {
73*4776d4e8SJohn Marino if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1)
740d5acd74SJohn Marino /* Invalid character - wcrtomb() sets errno. */
750d5acd74SJohn Marino return ((size_t)-1);
760d5acd74SJohn Marino else if (*s == L'\0')
770d5acd74SJohn Marino return (nbytes + nb - 1);
780d5acd74SJohn Marino s++;
790d5acd74SJohn Marino nbytes += nb;
800d5acd74SJohn Marino }
810d5acd74SJohn Marino return (nbytes);
820d5acd74SJohn Marino }
830d5acd74SJohn Marino
840d5acd74SJohn Marino while (len > 0 && nwc-- > 0) {
850d5acd74SJohn Marino if (len > (size_t)MB_CUR_MAX) {
860d5acd74SJohn Marino /* Enough space to translate in-place. */
87*4776d4e8SJohn Marino if ((nb = pwcrtomb(dst, *s, ps)) == (size_t)-1) {
880d5acd74SJohn Marino *src = s;
890d5acd74SJohn Marino return ((size_t)-1);
900d5acd74SJohn Marino }
910d5acd74SJohn Marino } else {
920d5acd74SJohn Marino /*
930d5acd74SJohn Marino * May not be enough space; use temp. buffer.
940d5acd74SJohn Marino *
950d5acd74SJohn Marino * We need to save a copy of the conversion state
960d5acd74SJohn Marino * here so we can restore it if the multibyte
970d5acd74SJohn Marino * character is too long for the buffer.
980d5acd74SJohn Marino */
990d5acd74SJohn Marino mbsbak = *ps;
100*4776d4e8SJohn Marino if ((nb = pwcrtomb(buf, *s, ps)) == (size_t)-1) {
1010d5acd74SJohn Marino *src = s;
1020d5acd74SJohn Marino return ((size_t)-1);
1030d5acd74SJohn Marino }
1040d5acd74SJohn Marino if (nb > (int)len) {
1050d5acd74SJohn Marino /* MB sequence for character won't fit. */
1060d5acd74SJohn Marino *ps = mbsbak;
1070d5acd74SJohn Marino break;
1080d5acd74SJohn Marino }
109*4776d4e8SJohn Marino (void) memcpy(dst, buf, nb);
1100d5acd74SJohn Marino }
1110d5acd74SJohn Marino if (*s == L'\0') {
1120d5acd74SJohn Marino *src = NULL;
1130d5acd74SJohn Marino return (nbytes + nb - 1);
1140d5acd74SJohn Marino }
1150d5acd74SJohn Marino s++;
1160d5acd74SJohn Marino dst += nb;
1170d5acd74SJohn Marino len -= nb;
1180d5acd74SJohn Marino nbytes += nb;
1190d5acd74SJohn Marino }
1200d5acd74SJohn Marino *src = s;
1210d5acd74SJohn Marino return (nbytes);
1220d5acd74SJohn Marino }
123