1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric
9349cc55cSDimitry Andric #include <cwchar> // mbstate_t
10349cc55cSDimitry Andric #include <limits.h> // MB_LEN_MAX
11349cc55cSDimitry Andric #include <stdlib.h> // MB_CUR_MAX, size_t
12349cc55cSDimitry Andric #include <string.h> // memcpy
13349cc55cSDimitry Andric
14349cc55cSDimitry Andric // Converts `max_source_chars` from the wide character buffer pointer to by *`src`,
15349cc55cSDimitry Andric // into the multi byte character sequence buffer stored at `dst`, which must be
16349cc55cSDimitry Andric // `dst_size_bytes` bytes in size. Returns the number of bytes in the sequence
17349cc55cSDimitry Andric // converted from *src, excluding the null terminator.
18349cc55cSDimitry Andric // Returns (size_t) -1 if an error occurs and sets errno.
19349cc55cSDimitry Andric // If `dst` is NULL, `dst_size_bytes` is ignored and no bytes are copied to `dst`.
wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t max_source_chars,size_t dst_size_bytes,mbstate_t * __restrict ps)20*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI size_t wcsnrtombs(
21*cb14a3feSDimitry Andric char* __restrict dst,
22*cb14a3feSDimitry Andric const wchar_t** __restrict src,
23*cb14a3feSDimitry Andric size_t max_source_chars,
24*cb14a3feSDimitry Andric size_t dst_size_bytes,
25349cc55cSDimitry Andric mbstate_t* __restrict ps) {
26349cc55cSDimitry Andric const size_t invalid_wchar = static_cast<size_t>(-1);
27349cc55cSDimitry Andric
28349cc55cSDimitry Andric size_t source_converted;
29349cc55cSDimitry Andric size_t dest_converted;
30349cc55cSDimitry Andric size_t result = 0;
31349cc55cSDimitry Andric
32349cc55cSDimitry Andric // If `dst` is null then `dst_size_bytes` should be ignored according to the
33349cc55cSDimitry Andric // standard. Setting dst_size_bytes to a large value has this effect.
34349cc55cSDimitry Andric if (dst == nullptr)
35349cc55cSDimitry Andric dst_size_bytes = static_cast<size_t>(-1);
36349cc55cSDimitry Andric
37349cc55cSDimitry Andric for (dest_converted = source_converted = 0;
38349cc55cSDimitry Andric source_converted < max_source_chars && (!dst || dest_converted < dst_size_bytes);
39349cc55cSDimitry Andric ++source_converted, dest_converted += result) {
40349cc55cSDimitry Andric wchar_t c = (*src)[source_converted];
41349cc55cSDimitry Andric size_t dest_remaining = dst_size_bytes - dest_converted;
42349cc55cSDimitry Andric
43349cc55cSDimitry Andric if (dst == nullptr) {
44349cc55cSDimitry Andric result = wcrtomb(NULL, c, ps);
45349cc55cSDimitry Andric } else if (dest_remaining >= static_cast<size_t>(MB_CUR_MAX)) {
46349cc55cSDimitry Andric // dst has enough space to translate in-place.
47349cc55cSDimitry Andric result = wcrtomb(dst + dest_converted, c, ps);
48349cc55cSDimitry Andric } else {
49349cc55cSDimitry Andric /*
50349cc55cSDimitry Andric * dst may not have enough space, so use a temporary buffer.
51349cc55cSDimitry Andric *
52349cc55cSDimitry Andric * We need to save a copy of the conversion state
53349cc55cSDimitry Andric * here so we can restore it if the multibyte
54349cc55cSDimitry Andric * character is too long for the buffer.
55349cc55cSDimitry Andric */
56349cc55cSDimitry Andric char buff[MB_LEN_MAX];
57349cc55cSDimitry Andric mbstate_t mbstate_tmp;
58349cc55cSDimitry Andric
59349cc55cSDimitry Andric if (ps != nullptr)
60349cc55cSDimitry Andric mbstate_tmp = *ps;
61349cc55cSDimitry Andric result = wcrtomb(buff, c, ps);
62349cc55cSDimitry Andric
63349cc55cSDimitry Andric if (result > dest_remaining) {
64349cc55cSDimitry Andric // Multi-byte sequence for character won't fit.
65349cc55cSDimitry Andric if (ps != nullptr)
66349cc55cSDimitry Andric *ps = mbstate_tmp;
67349cc55cSDimitry Andric if (result != invalid_wchar)
68349cc55cSDimitry Andric break;
69349cc55cSDimitry Andric } else {
70349cc55cSDimitry Andric // The buffer was used, so we need copy the translation to dst.
71349cc55cSDimitry Andric memcpy(dst, buff, result);
72349cc55cSDimitry Andric }
73349cc55cSDimitry Andric }
74349cc55cSDimitry Andric
75349cc55cSDimitry Andric // result (char_size) contains the size of the multi-byte-sequence converted.
76349cc55cSDimitry Andric // Otherwise, result (char_size) is (size_t) -1 and wcrtomb() sets the errno.
77349cc55cSDimitry Andric if (result == invalid_wchar) {
78349cc55cSDimitry Andric if (dst)
79349cc55cSDimitry Andric *src = *src + source_converted;
80349cc55cSDimitry Andric return invalid_wchar;
81349cc55cSDimitry Andric }
82349cc55cSDimitry Andric
83349cc55cSDimitry Andric if (c == L'\0') {
84349cc55cSDimitry Andric if (dst)
85349cc55cSDimitry Andric *src = NULL;
86349cc55cSDimitry Andric return dest_converted;
87349cc55cSDimitry Andric }
88349cc55cSDimitry Andric }
89349cc55cSDimitry Andric
90349cc55cSDimitry Andric if (dst)
91349cc55cSDimitry Andric *src = *src + source_converted;
92349cc55cSDimitry Andric
93349cc55cSDimitry Andric return dest_converted;
94349cc55cSDimitry Andric }
95