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