xref: /openbsd-src/gnu/llvm/libcxx/src/support/win32/support.cpp (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
176d0caaeSpatrick //===----------------------------------------------------------------------===//
246035553Spatrick //
346035553Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
446035553Spatrick // See https://llvm.org/LICENSE.txt for license information.
546035553Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
646035553Spatrick //
746035553Spatrick //===----------------------------------------------------------------------===//
846035553Spatrick 
946035553Spatrick #include <cstdarg> // va_start, va_end
1046035553Spatrick #include <cstddef> // size_t
1146035553Spatrick #include <cstdlib> // malloc
1246035553Spatrick #include <cstdio>  // vsprintf, vsnprintf
1346035553Spatrick #include <cstring> // strcpy, wcsncpy
1446035553Spatrick #include <cwchar>  // mbstate_t
1546035553Spatrick 
1646035553Spatrick 
1746035553Spatrick // Like sprintf, but when return value >= 0 it returns
1846035553Spatrick // a pointer to a malloc'd string in *sptr.
1946035553Spatrick // If return >= 0, use free to delete *sptr.
__libcpp_vasprintf(char ** sptr,const char * __restrict format,va_list ap)2046035553Spatrick int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap )
2146035553Spatrick {
2246035553Spatrick     *sptr = NULL;
2346035553Spatrick     // Query the count required.
2476d0caaeSpatrick     va_list ap_copy;
2576d0caaeSpatrick     va_copy(ap_copy, ap);
26*4bdff4beSrobert     _LIBCPP_DIAGNOSTIC_PUSH
27*4bdff4beSrobert     _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
28*4bdff4beSrobert     int count = vsnprintf( NULL, 0, format, ap_copy );
29*4bdff4beSrobert     _LIBCPP_DIAGNOSTIC_POP
3076d0caaeSpatrick     va_end(ap_copy);
3146035553Spatrick     if (count < 0)
3246035553Spatrick         return count;
3346035553Spatrick     size_t buffer_size = static_cast<size_t>(count) + 1;
3446035553Spatrick     char* p = static_cast<char*>(malloc(buffer_size));
3546035553Spatrick     if ( ! p )
3646035553Spatrick         return -1;
3746035553Spatrick     // If we haven't used exactly what was required, something is wrong.
3846035553Spatrick     // Maybe bug in vsnprintf. Report the error and return.
39*4bdff4beSrobert     _LIBCPP_DIAGNOSTIC_PUSH
40*4bdff4beSrobert     _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
41*4bdff4beSrobert     if (vsnprintf(p, buffer_size, format, ap) != count) {
42*4bdff4beSrobert     _LIBCPP_DIAGNOSTIC_POP
4346035553Spatrick         free(p);
4446035553Spatrick         return -1;
4546035553Spatrick     }
4646035553Spatrick     // All good. This is returning memory to the caller not freeing it.
4746035553Spatrick     *sptr = p;
4846035553Spatrick     return count;
4946035553Spatrick }
5046035553Spatrick 
5146035553Spatrick // Returns >= 0: the number of wide characters found in the
5246035553Spatrick // multi byte sequence src (of src_size_bytes), that fit in the buffer dst
5346035553Spatrick // (of max_dest_chars elements size). The count returned excludes the
5446035553Spatrick // null terminator. When dst is NULL, no characters are copied
5546035553Spatrick // and no "out" parameters are updated.
5646035553Spatrick // Returns (size_t) -1: an incomplete sequence encountered.
5746035553Spatrick // Leaves *src pointing the next character to convert or NULL
5846035553Spatrick // if a null character was converted from *src.
mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t src_size_bytes,size_t max_dest_chars,mbstate_t * __restrict ps)5946035553Spatrick size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
6046035553Spatrick                    size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
6146035553Spatrick {
6246035553Spatrick     const size_t terminated_sequence = static_cast<size_t>(0);
6346035553Spatrick     //const size_t invalid_sequence = static_cast<size_t>(-1);
6446035553Spatrick     const size_t incomplete_sequence = static_cast< size_t>(-2);
6546035553Spatrick 
6646035553Spatrick     size_t dest_converted = 0;
6746035553Spatrick     size_t source_converted = 0;
6846035553Spatrick     size_t source_remaining = src_size_bytes;
6946035553Spatrick     size_t result = 0;
7046035553Spatrick     bool have_result = false;
7146035553Spatrick 
7246035553Spatrick     // If dst is null then max_dest_chars should be ignored according to the
7346035553Spatrick     // standard.  Setting max_dest_chars to a large value has this effect.
7446035553Spatrick     if (!dst)
7546035553Spatrick         max_dest_chars = static_cast<size_t>(-1);
7646035553Spatrick 
7746035553Spatrick     while ( source_remaining ) {
7846035553Spatrick         if ( dst && dest_converted >= max_dest_chars )
7946035553Spatrick             break;
8046035553Spatrick         // Converts one multi byte character.
8146035553Spatrick         // if result > 0, it's the size in bytes of that character.
8246035553Spatrick         // othewise if result is zero it indicates the null character has been found.
8346035553Spatrick         // otherwise it's an error and errno may be set.
8446035553Spatrick         size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
8546035553Spatrick         // Don't do anything to change errno from here on.
8646035553Spatrick         if ( char_size > 0 ) {
8746035553Spatrick             source_remaining -= char_size;
8846035553Spatrick             source_converted += char_size;
8946035553Spatrick             ++dest_converted;
9046035553Spatrick             continue;
9146035553Spatrick         }
9246035553Spatrick         result = char_size;
9346035553Spatrick         have_result = true;
9446035553Spatrick         break;
9546035553Spatrick     }
9646035553Spatrick     if ( dst ) {
9746035553Spatrick         if ( have_result && result == terminated_sequence )
9846035553Spatrick             *src = NULL;
9946035553Spatrick         else
10046035553Spatrick             *src += source_converted;
10146035553Spatrick     }
10246035553Spatrick     if ( have_result && result != terminated_sequence && result != incomplete_sequence )
10346035553Spatrick         return static_cast<size_t>(-1);
10446035553Spatrick 
10546035553Spatrick     return dest_converted;
10646035553Spatrick }
10746035553Spatrick 
10846035553Spatrick // Converts max_source_chars from the wide character buffer pointer to by *src,
10946035553Spatrick // into the multi byte character sequence buffer stored at dst which must be
11046035553Spatrick // dst_size_bytes bytes in size.
11146035553Spatrick // Returns >= 0: the number of bytes in the sequence
11246035553Spatrick // converted from *src, excluding the null terminator.
11346035553Spatrick // Returns size_t(-1) if an error occurs, also sets errno.
11446035553Spatrick // If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst
11546035553Spatrick // and no "out" parameters are updated.
wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t max_source_chars,size_t dst_size_bytes,mbstate_t * __restrict ps)11646035553Spatrick size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
11746035553Spatrick                    size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
11846035553Spatrick {
11946035553Spatrick     //const size_t invalid_sequence = static_cast<size_t>(-1);
12046035553Spatrick 
12146035553Spatrick     size_t source_converted = 0;
12246035553Spatrick     size_t dest_converted = 0;
12346035553Spatrick     size_t dest_remaining = dst_size_bytes;
12446035553Spatrick     size_t char_size = 0;
12546035553Spatrick     const errno_t no_error = ( errno_t) 0;
12646035553Spatrick     errno_t result = ( errno_t ) 0;
12746035553Spatrick     bool have_result = false;
12846035553Spatrick     bool terminator_found = false;
12946035553Spatrick 
13046035553Spatrick     // If dst is null then dst_size_bytes should be ignored according to the
13146035553Spatrick     // standard.  Setting dest_remaining to a large value has this effect.
13246035553Spatrick     if (!dst)
13346035553Spatrick         dest_remaining = static_cast<size_t>(-1);
13446035553Spatrick 
13546035553Spatrick     while ( source_converted != max_source_chars ) {
13646035553Spatrick         if ( ! dest_remaining )
13746035553Spatrick             break;
13846035553Spatrick         wchar_t c = (*src)[source_converted];
13946035553Spatrick         if ( dst )
14046035553Spatrick             result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
14146035553Spatrick         else
14246035553Spatrick             result = wcrtomb_s( &char_size, NULL, 0, c, ps);
14346035553Spatrick         // If result is zero there is no error and char_size contains the
14446035553Spatrick         // size of the multi-byte-sequence converted.
14546035553Spatrick         // Otherwise result indicates an errno type error.
14646035553Spatrick         if ( result == no_error ) {
14746035553Spatrick             if ( c == L'\0' ) {
14846035553Spatrick                 terminator_found = true;
14946035553Spatrick                 break;
15046035553Spatrick             }
15146035553Spatrick             ++source_converted;
15246035553Spatrick             if ( dst )
15346035553Spatrick                 dest_remaining -= char_size;
15446035553Spatrick             dest_converted += char_size;
15546035553Spatrick             continue;
15646035553Spatrick         }
15746035553Spatrick         have_result = true;
15846035553Spatrick         break;
15946035553Spatrick     }
16046035553Spatrick     if ( dst ) {
16146035553Spatrick         if ( terminator_found )
16246035553Spatrick             *src = NULL;
16346035553Spatrick         else
16446035553Spatrick             *src = *src + source_converted;
16546035553Spatrick     }
16646035553Spatrick     if ( have_result && result != no_error ) {
16746035553Spatrick         errno = result;
16846035553Spatrick         return static_cast<size_t>(-1);
16946035553Spatrick     }
17046035553Spatrick 
17146035553Spatrick     return dest_converted;
17246035553Spatrick }
173