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.
73c87aa1dSDavid Chisnall *
83c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation
95b5fa75aSEd Maste *
103c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall
113c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation.
121949a347STim J. Robbins *
131949a347STim J. Robbins * Redistribution and use in source and binary forms, with or without
141949a347STim J. Robbins * modification, are permitted provided that the following conditions
151949a347STim J. Robbins * are met:
161949a347STim J. Robbins * 1. Redistributions of source code must retain the above copyright
171949a347STim J. Robbins * notice, this list of conditions and the following disclaimer.
181949a347STim J. Robbins * 2. Redistributions in binary form must reproduce the above copyright
191949a347STim J. Robbins * notice, this list of conditions and the following disclaimer in the
201949a347STim J. Robbins * documentation and/or other materials provided with the distribution.
211949a347STim J. Robbins *
221949a347STim J. Robbins * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
231949a347STim J. Robbins * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241949a347STim J. Robbins * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251949a347STim J. Robbins * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
261949a347STim J. Robbins * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271949a347STim J. Robbins * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281949a347STim J. Robbins * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291949a347STim J. Robbins * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301949a347STim J. Robbins * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311949a347STim J. Robbins * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321949a347STim J. Robbins * SUCH DAMAGE.
331949a347STim J. Robbins */
341949a347STim J. Robbins
351949a347STim J. Robbins #include <errno.h>
361949a347STim J. Robbins #include <limits.h>
371949a347STim J. Robbins #include <stdlib.h>
381949a347STim J. Robbins #include <wchar.h>
391949a347STim J. Robbins #include "mblocal.h"
401949a347STim J. Robbins
411949a347STim J. Robbins size_t
mbsnrtowcs_l(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps,locale_t locale)423c87aa1dSDavid Chisnall mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src,
433c87aa1dSDavid Chisnall size_t nms, size_t len, mbstate_t * __restrict ps, locale_t locale)
443c87aa1dSDavid Chisnall {
453c87aa1dSDavid Chisnall FIX_LOCALE(locale);
463c87aa1dSDavid Chisnall if (ps == NULL)
479eb7d595SYuri Pankov ps = &(XLOCALE_CTYPE(locale)->mbsnrtowcs);
483c87aa1dSDavid Chisnall return (XLOCALE_CTYPE(locale)->__mbsnrtowcs(dst, src, nms, len, ps));
493c87aa1dSDavid Chisnall }
503c87aa1dSDavid Chisnall size_t
mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps)511949a347STim J. Robbins mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src,
521949a347STim J. Robbins size_t nms, size_t len, mbstate_t * __restrict ps)
531949a347STim J. Robbins {
543c87aa1dSDavid Chisnall return mbsnrtowcs_l(dst, src, nms, len, ps, __get_locale());
551949a347STim J. Robbins }
561949a347STim J. Robbins
571949a347STim J. Robbins size_t
__mbsnrtowcs_std(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps,mbrtowc_pfn_t pmbrtowc)581949a347STim J. Robbins __mbsnrtowcs_std(wchar_t * __restrict dst, const char ** __restrict src,
597b247341SBaptiste Daroussin size_t nms, size_t len, mbstate_t * __restrict ps,
607b247341SBaptiste Daroussin mbrtowc_pfn_t pmbrtowc)
611949a347STim J. Robbins {
621949a347STim J. Robbins const char *s;
631949a347STim J. Robbins size_t nchr;
641949a347STim J. Robbins wchar_t wc;
651949a347STim J. Robbins size_t nb;
661949a347STim J. Robbins
671949a347STim J. Robbins s = *src;
681949a347STim J. Robbins nchr = 0;
691949a347STim J. Robbins
701949a347STim J. Robbins if (dst == NULL) {
711949a347STim J. Robbins for (;;) {
727b247341SBaptiste Daroussin if ((nb = pmbrtowc(&wc, s, nms, ps)) == (size_t)-1)
731949a347STim J. Robbins /* Invalid sequence - mbrtowc() sets errno. */
741949a347STim J. Robbins return ((size_t)-1);
751949a347STim J. Robbins else if (nb == 0 || nb == (size_t)-2)
761949a347STim J. Robbins return (nchr);
771949a347STim J. Robbins s += nb;
781949a347STim J. Robbins nms -= nb;
791949a347STim J. Robbins nchr++;
801949a347STim J. Robbins }
811949a347STim J. Robbins /*NOTREACHED*/
821949a347STim J. Robbins }
831949a347STim J. Robbins
841949a347STim J. Robbins while (len-- > 0) {
857b247341SBaptiste Daroussin if ((nb = pmbrtowc(dst, s, nms, ps)) == (size_t)-1) {
861949a347STim J. Robbins *src = s;
871949a347STim J. Robbins return ((size_t)-1);
881949a347STim J. Robbins } else if (nb == (size_t)-2) {
891949a347STim J. Robbins *src = s + nms;
901949a347STim J. Robbins return (nchr);
911949a347STim J. Robbins } else if (nb == 0) {
921949a347STim J. Robbins *src = NULL;
931949a347STim J. Robbins return (nchr);
941949a347STim J. Robbins }
951949a347STim J. Robbins s += nb;
961949a347STim J. Robbins nms -= nb;
971949a347STim J. Robbins nchr++;
981949a347STim J. Robbins dst++;
991949a347STim J. Robbins }
1001949a347STim J. Robbins *src = s;
1011949a347STim J. Robbins return (nchr);
1021949a347STim J. Robbins }
103