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 <sys/cdefs.h> 361949a347STim J. Robbins __FBSDID("$FreeBSD$"); 371949a347STim J. Robbins 381949a347STim J. Robbins #include <errno.h> 391949a347STim J. Robbins #include <limits.h> 401949a347STim J. Robbins #include <stdlib.h> 411949a347STim J. Robbins #include <wchar.h> 421949a347STim J. Robbins #include "mblocal.h" 431949a347STim J. Robbins 441949a347STim J. Robbins size_t 453c87aa1dSDavid Chisnall mbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src, 463c87aa1dSDavid Chisnall size_t nms, size_t len, mbstate_t * __restrict ps, locale_t locale) 473c87aa1dSDavid Chisnall { 483c87aa1dSDavid Chisnall FIX_LOCALE(locale); 493c87aa1dSDavid Chisnall if (ps == NULL) 509eb7d595SYuri Pankov ps = &(XLOCALE_CTYPE(locale)->mbsnrtowcs); 513c87aa1dSDavid Chisnall return (XLOCALE_CTYPE(locale)->__mbsnrtowcs(dst, src, nms, len, ps)); 523c87aa1dSDavid Chisnall } 533c87aa1dSDavid Chisnall size_t 541949a347STim J. Robbins mbsnrtowcs(wchar_t * __restrict dst, const char ** __restrict src, 551949a347STim J. Robbins size_t nms, size_t len, mbstate_t * __restrict ps) 561949a347STim J. Robbins { 573c87aa1dSDavid Chisnall return mbsnrtowcs_l(dst, src, nms, len, ps, __get_locale()); 581949a347STim J. Robbins } 591949a347STim J. Robbins 601949a347STim J. Robbins size_t 611949a347STim J. Robbins __mbsnrtowcs_std(wchar_t * __restrict dst, const char ** __restrict src, 627b247341SBaptiste Daroussin size_t nms, size_t len, mbstate_t * __restrict ps, 637b247341SBaptiste Daroussin mbrtowc_pfn_t pmbrtowc) 641949a347STim J. Robbins { 651949a347STim J. Robbins const char *s; 661949a347STim J. Robbins size_t nchr; 671949a347STim J. Robbins wchar_t wc; 681949a347STim J. Robbins size_t nb; 691949a347STim J. Robbins 701949a347STim J. Robbins s = *src; 711949a347STim J. Robbins nchr = 0; 721949a347STim J. Robbins 731949a347STim J. Robbins if (dst == NULL) { 741949a347STim J. Robbins for (;;) { 757b247341SBaptiste Daroussin if ((nb = pmbrtowc(&wc, s, nms, ps)) == (size_t)-1) 761949a347STim J. Robbins /* Invalid sequence - mbrtowc() sets errno. */ 771949a347STim J. Robbins return ((size_t)-1); 781949a347STim J. Robbins else if (nb == 0 || nb == (size_t)-2) 791949a347STim J. Robbins return (nchr); 801949a347STim J. Robbins s += nb; 811949a347STim J. Robbins nms -= nb; 821949a347STim J. Robbins nchr++; 831949a347STim J. Robbins } 841949a347STim J. Robbins /*NOTREACHED*/ 851949a347STim J. Robbins } 861949a347STim J. Robbins 871949a347STim J. Robbins while (len-- > 0) { 887b247341SBaptiste Daroussin if ((nb = pmbrtowc(dst, s, nms, ps)) == (size_t)-1) { 891949a347STim J. Robbins *src = s; 901949a347STim J. Robbins return ((size_t)-1); 911949a347STim J. Robbins } else if (nb == (size_t)-2) { 921949a347STim J. Robbins *src = s + nms; 931949a347STim J. Robbins return (nchr); 941949a347STim J. Robbins } else if (nb == 0) { 951949a347STim J. Robbins *src = NULL; 961949a347STim J. Robbins return (nchr); 971949a347STim J. Robbins } 981949a347STim J. Robbins s += nb; 991949a347STim J. Robbins nms -= nb; 1001949a347STim J. Robbins nchr++; 1011949a347STim J. Robbins dst++; 1021949a347STim J. Robbins } 1031949a347STim J. Robbins *src = s; 1041949a347STim J. Robbins return (nchr); 1051949a347STim J. Robbins } 106