1ea2741973acd73619da693548388a05527b92d57David Chisnall
2ea2741973acd73619da693548388a05527b92d57David Chisnall
3ea2741973acd73619da693548388a05527b92d57David Chisnall/*-
4ea2741973acd73619da693548388a05527b92d57David Chisnall * As noted in the source, some portions of this implementation are copied from
5ea2741973acd73619da693548388a05527b92d57David Chisnall * FreeBSD libc.  These are covered by the following copyright:
6ea2741973acd73619da693548388a05527b92d57David Chisnall *
7ea2741973acd73619da693548388a05527b92d57David Chisnall * Copyright (c) 2002-2004 Tim J. Robbins.
8ea2741973acd73619da693548388a05527b92d57David Chisnall *
9ea2741973acd73619da693548388a05527b92d57David Chisnall * Redistribution and use in source and binary forms, with or without
10ea2741973acd73619da693548388a05527b92d57David Chisnall * modification, are permitted provided that the following conditions
11ea2741973acd73619da693548388a05527b92d57David Chisnall * are met:
12ea2741973acd73619da693548388a05527b92d57David Chisnall * 1. Redistributions of source code must retain the above copyright
13ea2741973acd73619da693548388a05527b92d57David Chisnall *    notice, this list of conditions and the following disclaimer.
14ea2741973acd73619da693548388a05527b92d57David Chisnall * 2. Redistributions in binary form must reproduce the above copyright
15ea2741973acd73619da693548388a05527b92d57David Chisnall *    notice, this list of conditions and the following disclaimer in the
16ea2741973acd73619da693548388a05527b92d57David Chisnall *    documentation and/or other materials provided with the distribution.
17ea2741973acd73619da693548388a05527b92d57David Chisnall *
18ea2741973acd73619da693548388a05527b92d57David Chisnall * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ea2741973acd73619da693548388a05527b92d57David Chisnall * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ea2741973acd73619da693548388a05527b92d57David Chisnall * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ea2741973acd73619da693548388a05527b92d57David Chisnall * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ea2741973acd73619da693548388a05527b92d57David Chisnall * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ea2741973acd73619da693548388a05527b92d57David Chisnall * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ea2741973acd73619da693548388a05527b92d57David Chisnall * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ea2741973acd73619da693548388a05527b92d57David Chisnall * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ea2741973acd73619da693548388a05527b92d57David Chisnall * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ea2741973acd73619da693548388a05527b92d57David Chisnall * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ea2741973acd73619da693548388a05527b92d57David Chisnall * SUCH DAMAGE.
29ea2741973acd73619da693548388a05527b92d57David Chisnall */
30ea2741973acd73619da693548388a05527b92d57David Chisnall
31ea2741973acd73619da693548388a05527b92d57David Chisnallsize_t
32ea2741973acd73619da693548388a05527b92d57David Chisnallmbsnrtowcs_l(wchar_t * __restrict dst, const char ** __restrict src,
33ea2741973acd73619da693548388a05527b92d57David Chisnall    size_t nms, size_t len, mbstate_t * __restrict ps, locale_t loc)
34ea2741973acd73619da693548388a05527b92d57David Chisnall{
35ea2741973acd73619da693548388a05527b92d57David Chisnall  const char *s;
36ea2741973acd73619da693548388a05527b92d57David Chisnall  size_t nchr;
37ea2741973acd73619da693548388a05527b92d57David Chisnall  wchar_t wc;
38ea2741973acd73619da693548388a05527b92d57David Chisnall  size_t nb;
39ea2741973acd73619da693548388a05527b92d57David Chisnall  FIX_LOCALE(loc);
40ea2741973acd73619da693548388a05527b92d57David Chisnall
41ea2741973acd73619da693548388a05527b92d57David Chisnall  s = *src;
42ea2741973acd73619da693548388a05527b92d57David Chisnall  nchr = 0;
43ea2741973acd73619da693548388a05527b92d57David Chisnall
44ea2741973acd73619da693548388a05527b92d57David Chisnall  if (dst == NULL) {
45ea2741973acd73619da693548388a05527b92d57David Chisnall    for (;;) {
46ea2741973acd73619da693548388a05527b92d57David Chisnall      if ((nb = mbrtowc_l(&wc, s, nms, ps, loc)) == (size_t)-1)
47ea2741973acd73619da693548388a05527b92d57David Chisnall        /* Invalid sequence - mbrtowc() sets errno. */
48ea2741973acd73619da693548388a05527b92d57David Chisnall        return ((size_t)-1);
49ea2741973acd73619da693548388a05527b92d57David Chisnall      else if (nb == 0 || nb == (size_t)-2)
50ea2741973acd73619da693548388a05527b92d57David Chisnall        return (nchr);
51ea2741973acd73619da693548388a05527b92d57David Chisnall      s += nb;
52ea2741973acd73619da693548388a05527b92d57David Chisnall      nms -= nb;
53ea2741973acd73619da693548388a05527b92d57David Chisnall      nchr++;
54ea2741973acd73619da693548388a05527b92d57David Chisnall    }
55ea2741973acd73619da693548388a05527b92d57David Chisnall    /*NOTREACHED*/
56ea2741973acd73619da693548388a05527b92d57David Chisnall  }
57ea2741973acd73619da693548388a05527b92d57David Chisnall
58ea2741973acd73619da693548388a05527b92d57David Chisnall  while (len-- > 0) {
59ea2741973acd73619da693548388a05527b92d57David Chisnall    if ((nb = mbrtowc_l(dst, s, nms, ps, loc)) == (size_t)-1) {
60ea2741973acd73619da693548388a05527b92d57David Chisnall      *src = s;
61ea2741973acd73619da693548388a05527b92d57David Chisnall      return ((size_t)-1);
62ea2741973acd73619da693548388a05527b92d57David Chisnall    } else if (nb == (size_t)-2) {
63ea2741973acd73619da693548388a05527b92d57David Chisnall      *src = s + nms;
64ea2741973acd73619da693548388a05527b92d57David Chisnall      return (nchr);
65ea2741973acd73619da693548388a05527b92d57David Chisnall    } else if (nb == 0) {
66ea2741973acd73619da693548388a05527b92d57David Chisnall      *src = NULL;
67ea2741973acd73619da693548388a05527b92d57David Chisnall      return (nchr);
68ea2741973acd73619da693548388a05527b92d57David Chisnall    }
69ea2741973acd73619da693548388a05527b92d57David Chisnall    s += nb;
70ea2741973acd73619da693548388a05527b92d57David Chisnall    nms -= nb;
71ea2741973acd73619da693548388a05527b92d57David Chisnall    nchr++;
72ea2741973acd73619da693548388a05527b92d57David Chisnall    dst++;
73ea2741973acd73619da693548388a05527b92d57David Chisnall  }
74ea2741973acd73619da693548388a05527b92d57David Chisnall  *src = s;
75ea2741973acd73619da693548388a05527b92d57David Chisnall  return (nchr);
76ea2741973acd73619da693548388a05527b92d57David Chisnall}
77