14297a3b0SGarrett D'Amore /*
22d08521bSGarrett 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 <errno.h>
314297a3b0SGarrett D'Amore #include <limits.h>
324297a3b0SGarrett D'Amore #include <stdlib.h>
334297a3b0SGarrett D'Amore #include <wchar.h>
344297a3b0SGarrett D'Amore #include "mblocal.h"
352d08521bSGarrett D'Amore #include "localeimpl.h"
362d08521bSGarrett D'Amore #include "lctype.h"
374297a3b0SGarrett D'Amore
384297a3b0SGarrett D'Amore size_t
mbsnrtowcs_l(wchar_t * _RESTRICT_KYWD dst,const char ** _RESTRICT_KYWD src,size_t nms,size_t len,mbstate_t * _RESTRICT_KYWD ps,locale_t loc)392d08521bSGarrett D'Amore mbsnrtowcs_l(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
402d08521bSGarrett D'Amore size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD ps, locale_t loc)
414297a3b0SGarrett D'Amore {
424297a3b0SGarrett D'Amore static mbstate_t mbs;
434297a3b0SGarrett D'Amore
444297a3b0SGarrett D'Amore if (ps == NULL)
454297a3b0SGarrett D'Amore ps = &mbs;
462d08521bSGarrett D'Amore return (loc->ctype->lc_mbsnrtowcs(dst, src, nms, len, ps));
472d08521bSGarrett D'Amore }
482d08521bSGarrett D'Amore
492d08521bSGarrett D'Amore size_t
mbsnrtowcs(wchar_t * _RESTRICT_KYWD dst,const char ** _RESTRICT_KYWD src,size_t nms,size_t len,mbstate_t * _RESTRICT_KYWD ps)502d08521bSGarrett D'Amore mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
512d08521bSGarrett D'Amore size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD ps)
522d08521bSGarrett D'Amore {
532d08521bSGarrett D'Amore return (mbsnrtowcs_l(dst, src, nms, len, ps, uselocale(NULL)));
544297a3b0SGarrett D'Amore }
554297a3b0SGarrett D'Amore
564297a3b0SGarrett D'Amore size_t
__mbsnrtowcs_std(wchar_t * _RESTRICT_KYWD dst,const char ** _RESTRICT_KYWD src,size_t nms,size_t len,mbstate_t * _RESTRICT_KYWD ps,mbrtowc_pfn_t pmbrtowc)574297a3b0SGarrett D'Amore __mbsnrtowcs_std(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
582d08521bSGarrett D'Amore size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD ps,
592d08521bSGarrett D'Amore mbrtowc_pfn_t pmbrtowc)
604297a3b0SGarrett D'Amore {
614297a3b0SGarrett D'Amore const char *s;
624297a3b0SGarrett D'Amore size_t nchr;
634297a3b0SGarrett D'Amore wchar_t wc;
644297a3b0SGarrett D'Amore size_t nb;
654297a3b0SGarrett D'Amore
664297a3b0SGarrett D'Amore s = *src;
674297a3b0SGarrett D'Amore nchr = 0;
684297a3b0SGarrett D'Amore
694297a3b0SGarrett D'Amore if (dst == NULL) {
704297a3b0SGarrett D'Amore for (;;) {
71*d8e0a9a1SRobert Mustacchi if ((nb = pmbrtowc(&wc, s, nms, ps, B_FALSE)) ==
72*d8e0a9a1SRobert Mustacchi (size_t)-1) {
734297a3b0SGarrett D'Amore /* Invalid sequence - mbrtowc() sets errno. */
744297a3b0SGarrett D'Amore return ((size_t)-1);
75*d8e0a9a1SRobert Mustacchi } else if (nb == 0 || nb == (size_t)-2) {
764297a3b0SGarrett D'Amore return (nchr);
77*d8e0a9a1SRobert Mustacchi }
784297a3b0SGarrett D'Amore s += nb;
794297a3b0SGarrett D'Amore nms -= nb;
804297a3b0SGarrett D'Amore nchr++;
814297a3b0SGarrett D'Amore }
824297a3b0SGarrett D'Amore /*NOTREACHED*/
834297a3b0SGarrett D'Amore }
844297a3b0SGarrett D'Amore
854297a3b0SGarrett D'Amore while (len-- > 0) {
86*d8e0a9a1SRobert Mustacchi if ((nb = pmbrtowc(dst, s, nms, ps, B_FALSE)) == (size_t)-1) {
874297a3b0SGarrett D'Amore *src = s;
884297a3b0SGarrett D'Amore return ((size_t)-1);
894297a3b0SGarrett D'Amore } else if (nb == (size_t)-2) {
904297a3b0SGarrett D'Amore *src = s + nms;
914297a3b0SGarrett D'Amore return (nchr);
924297a3b0SGarrett D'Amore } else if (nb == 0) {
934297a3b0SGarrett D'Amore *src = NULL;
944297a3b0SGarrett D'Amore return (nchr);
954297a3b0SGarrett D'Amore }
964297a3b0SGarrett D'Amore s += nb;
974297a3b0SGarrett D'Amore nms -= nb;
984297a3b0SGarrett D'Amore nchr++;
994297a3b0SGarrett D'Amore dst++;
1004297a3b0SGarrett D'Amore }
1014297a3b0SGarrett D'Amore *src = s;
1024297a3b0SGarrett D'Amore return (nchr);
1034297a3b0SGarrett D'Amore }
104