1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include <cstddef> // size_t
10 #include <cwchar> // mbstate_t
11 #include <limits.h> // MB_LEN_MAX
12 #include <string.h> // wmemcpy
14 // Returns the number of wide characters found in the multi byte sequence `src`
15 // (of `src_size_bytes`), that fit in the buffer `dst` (of `max_dest_chars`
16 // elements size). The count returned excludes the null terminator.
17 // When `dst` is NULL, no characters are copied to `dst`.
18 // Returns (size_t) -1 when an invalid sequence is encountered.
19 // Leaves *`src` pointing to the next character to convert or NULL
20 // if a null character was converted from *`src`.
21 _LIBCPP_EXPORTED_FROM_ABI
22 size_t mbsnrtowcs(wchar_t *__restrict dst
, const char **__restrict src
,
23 size_t src_size_bytes
, size_t max_dest_chars
,
24 mbstate_t *__restrict ps
) {
25 const size_t terminated_sequence
= static_cast<size_t>(0);
26 const size_t invalid_sequence
= static_cast<size_t>(-1);
27 const size_t incomplete_sequence
= static_cast<size_t>(-2);
29 size_t source_converted
;
30 size_t dest_converted
;
33 // If `dst` is null then `max_dest_chars` should be ignored according to the
34 // standard. Setting `max_dest_chars` to a large value has this effect.
36 max_dest_chars
= static_cast<size_t>(-1);
38 for (dest_converted
= source_converted
= 0;
39 source_converted
< src_size_bytes
&& (!dst
|| dest_converted
< max_dest_chars
);
40 ++dest_converted
, source_converted
+= result
) {
41 // Converts one multi byte character.
42 // If result (char_size) is greater than 0, it's the size in bytes of that character.
43 // If result (char_size) is zero, it indicates that the null character has been found.
44 // Otherwise, it's an error and errno may be set.
45 size_t source_remaining
= src_size_bytes
- source_converted
;
46 size_t dest_remaining
= max_dest_chars
- dest_converted
;
49 result
= mbrtowc(NULL
, *src
+ source_converted
, source_remaining
, ps
);
50 } else if (dest_remaining
>= source_remaining
) {
51 // dst has enough space to translate in-place.
52 result
= mbrtowc(dst
+ dest_converted
, *src
+ source_converted
, source_remaining
, ps
);
55 * dst may not have enough space, so use a temporary buffer.
57 * We need to save a copy of the conversion state
58 * here so we can restore it if the multibyte
59 * character is too long for the buffer.
61 wchar_t buff
[MB_LEN_MAX
];
62 mbstate_t mbstate_tmp
;
66 result
= mbrtowc(buff
, *src
+ source_converted
, source_remaining
, ps
);
68 if (result
> dest_remaining
) {
69 // Multi-byte sequence for character won't fit.
74 // The buffer was used, so we need copy the translation to dst.
75 wmemcpy(dst
, buff
, result
);
79 // Don't do anything to change errno from here on.
80 if (result
== invalid_sequence
|| result
== terminated_sequence
|| result
== incomplete_sequence
) {
86 if (result
== terminated_sequence
)
89 *src
+= source_converted
;
91 if (result
== invalid_sequence
)
92 return invalid_sequence
;
94 return dest_converted
;