Path: blob/main/contrib/llvm-project/libcxx/src/support/ibm/mbsnrtowcs.cpp
35291 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <cstddef> // size_t9#include <cwchar> // mbstate_t10#include <limits.h> // MB_LEN_MAX11#include <string.h> // wmemcpy1213// Returns the number of wide characters found in the multi byte sequence `src`14// (of `src_size_bytes`), that fit in the buffer `dst` (of `max_dest_chars`15// elements size). The count returned excludes the null terminator.16// When `dst` is NULL, no characters are copied to `dst`.17// Returns (size_t) -1 when an invalid sequence is encountered.18// Leaves *`src` pointing to the next character to convert or NULL19// if a null character was converted from *`src`.20_LIBCPP_EXPORTED_FROM_ABI size_t mbsnrtowcs(21wchar_t* __restrict dst,22const char** __restrict src,23size_t src_size_bytes,24size_t max_dest_chars,25mbstate_t* __restrict ps) {26const size_t terminated_sequence = static_cast<size_t>(0);27const size_t invalid_sequence = static_cast<size_t>(-1);28const size_t incomplete_sequence = static_cast<size_t>(-2);2930size_t source_converted;31size_t dest_converted;32size_t result = 0;3334// If `dst` is null then `max_dest_chars` should be ignored according to the35// standard. Setting `max_dest_chars` to a large value has this effect.36if (dst == nullptr)37max_dest_chars = static_cast<size_t>(-1);3839for (dest_converted = source_converted = 0;40source_converted < src_size_bytes && (!dst || dest_converted < max_dest_chars);41++dest_converted, source_converted += result) {42// Converts one multi byte character.43// If result (char_size) is greater than 0, it's the size in bytes of that character.44// If result (char_size) is zero, it indicates that the null character has been found.45// Otherwise, it's an error and errno may be set.46size_t source_remaining = src_size_bytes - source_converted;47size_t dest_remaining = max_dest_chars - dest_converted;4849if (dst == nullptr) {50result = mbrtowc(NULL, *src + source_converted, source_remaining, ps);51} else if (dest_remaining >= source_remaining) {52// dst has enough space to translate in-place.53result = mbrtowc(dst + dest_converted, *src + source_converted, source_remaining, ps);54} else {55/*56* dst may not have enough space, so use a temporary buffer.57*58* We need to save a copy of the conversion state59* here so we can restore it if the multibyte60* character is too long for the buffer.61*/62wchar_t buff[MB_LEN_MAX];63mbstate_t mbstate_tmp;6465if (ps != nullptr)66mbstate_tmp = *ps;67result = mbrtowc(buff, *src + source_converted, source_remaining, ps);6869if (result > dest_remaining) {70// Multi-byte sequence for character won't fit.71if (ps != nullptr)72*ps = mbstate_tmp;73break;74} else {75// The buffer was used, so we need copy the translation to dst.76wmemcpy(dst, buff, result);77}78}7980// Don't do anything to change errno from here on.81if (result == invalid_sequence || result == terminated_sequence || result == incomplete_sequence) {82break;83}84}8586if (dst) {87if (result == terminated_sequence)88*src = NULL;89else90*src += source_converted;91}92if (result == invalid_sequence)93return invalid_sequence;9495return dest_converted;96}979899