1*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 2*349cc55cSDimitry Andric // 3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*349cc55cSDimitry Andric // 7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8*349cc55cSDimitry Andric 9*349cc55cSDimitry Andric #include <cwchar> // mbstate_t 10*349cc55cSDimitry Andric #include <limits.h> // MB_LEN_MAX 11*349cc55cSDimitry Andric #include <stdlib.h> // MB_CUR_MAX, size_t 12*349cc55cSDimitry Andric #include <string.h> // memcpy 13*349cc55cSDimitry Andric 14*349cc55cSDimitry Andric // Converts `max_source_chars` from the wide character buffer pointer to by *`src`, 15*349cc55cSDimitry Andric // into the multi byte character sequence buffer stored at `dst`, which must be 16*349cc55cSDimitry Andric // `dst_size_bytes` bytes in size. Returns the number of bytes in the sequence 17*349cc55cSDimitry Andric // converted from *src, excluding the null terminator. 18*349cc55cSDimitry Andric // Returns (size_t) -1 if an error occurs and sets errno. 19*349cc55cSDimitry Andric // If `dst` is NULL, `dst_size_bytes` is ignored and no bytes are copied to `dst`. 20*349cc55cSDimitry Andric _LIBCPP_FUNC_VIS 21*349cc55cSDimitry Andric size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src, 22*349cc55cSDimitry Andric size_t max_source_chars, size_t dst_size_bytes, 23*349cc55cSDimitry Andric mbstate_t *__restrict ps) { 24*349cc55cSDimitry Andric 25*349cc55cSDimitry Andric const size_t invalid_wchar = static_cast<size_t>(-1); 26*349cc55cSDimitry Andric 27*349cc55cSDimitry Andric size_t source_converted; 28*349cc55cSDimitry Andric size_t dest_converted; 29*349cc55cSDimitry Andric size_t result = 0; 30*349cc55cSDimitry Andric 31*349cc55cSDimitry Andric // If `dst` is null then `dst_size_bytes` should be ignored according to the 32*349cc55cSDimitry Andric // standard. Setting dst_size_bytes to a large value has this effect. 33*349cc55cSDimitry Andric if (dst == nullptr) 34*349cc55cSDimitry Andric dst_size_bytes = static_cast<size_t>(-1); 35*349cc55cSDimitry Andric 36*349cc55cSDimitry Andric for (dest_converted = source_converted = 0; 37*349cc55cSDimitry Andric source_converted < max_source_chars && (!dst || dest_converted < dst_size_bytes); 38*349cc55cSDimitry Andric ++source_converted, dest_converted += result) { 39*349cc55cSDimitry Andric wchar_t c = (*src)[source_converted]; 40*349cc55cSDimitry Andric size_t dest_remaining = dst_size_bytes - dest_converted; 41*349cc55cSDimitry Andric 42*349cc55cSDimitry Andric if (dst == nullptr) { 43*349cc55cSDimitry Andric result = wcrtomb(NULL, c, ps); 44*349cc55cSDimitry Andric } else if (dest_remaining >= static_cast<size_t>(MB_CUR_MAX)) { 45*349cc55cSDimitry Andric // dst has enough space to translate in-place. 46*349cc55cSDimitry Andric result = wcrtomb(dst + dest_converted, c, ps); 47*349cc55cSDimitry Andric } else { 48*349cc55cSDimitry Andric /* 49*349cc55cSDimitry Andric * dst may not have enough space, so use a temporary buffer. 50*349cc55cSDimitry Andric * 51*349cc55cSDimitry Andric * We need to save a copy of the conversion state 52*349cc55cSDimitry Andric * here so we can restore it if the multibyte 53*349cc55cSDimitry Andric * character is too long for the buffer. 54*349cc55cSDimitry Andric */ 55*349cc55cSDimitry Andric char buff[MB_LEN_MAX]; 56*349cc55cSDimitry Andric mbstate_t mbstate_tmp; 57*349cc55cSDimitry Andric 58*349cc55cSDimitry Andric if (ps != nullptr) 59*349cc55cSDimitry Andric mbstate_tmp = *ps; 60*349cc55cSDimitry Andric result = wcrtomb(buff, c, ps); 61*349cc55cSDimitry Andric 62*349cc55cSDimitry Andric if (result > dest_remaining) { 63*349cc55cSDimitry Andric // Multi-byte sequence for character won't fit. 64*349cc55cSDimitry Andric if (ps != nullptr) 65*349cc55cSDimitry Andric *ps = mbstate_tmp; 66*349cc55cSDimitry Andric if (result != invalid_wchar) 67*349cc55cSDimitry Andric break; 68*349cc55cSDimitry Andric } else { 69*349cc55cSDimitry Andric // The buffer was used, so we need copy the translation to dst. 70*349cc55cSDimitry Andric memcpy(dst, buff, result); 71*349cc55cSDimitry Andric } 72*349cc55cSDimitry Andric } 73*349cc55cSDimitry Andric 74*349cc55cSDimitry Andric // result (char_size) contains the size of the multi-byte-sequence converted. 75*349cc55cSDimitry Andric // Otherwise, result (char_size) is (size_t) -1 and wcrtomb() sets the errno. 76*349cc55cSDimitry Andric if (result == invalid_wchar) { 77*349cc55cSDimitry Andric if (dst) 78*349cc55cSDimitry Andric *src = *src + source_converted; 79*349cc55cSDimitry Andric return invalid_wchar; 80*349cc55cSDimitry Andric } 81*349cc55cSDimitry Andric 82*349cc55cSDimitry Andric if (c == L'\0') { 83*349cc55cSDimitry Andric if (dst) 84*349cc55cSDimitry Andric *src = NULL; 85*349cc55cSDimitry Andric return dest_converted; 86*349cc55cSDimitry Andric } 87*349cc55cSDimitry Andric } 88*349cc55cSDimitry Andric 89*349cc55cSDimitry Andric if (dst) 90*349cc55cSDimitry Andric *src = *src + source_converted; 91*349cc55cSDimitry Andric 92*349cc55cSDimitry Andric return dest_converted; 93*349cc55cSDimitry Andric } 94