Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Howard Hinnant | 3438889 | 2011-09-28 21:39:20 +0000 | [diff] [blame] | 2 | //===----------------------- support/win32/support.h ----------------------===// |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 3 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 10 | #include <cstdarg> // va_start, va_end |
| 11 | #include <cstddef> // size_t |
| 12 | #include <cstdlib> // malloc |
| 13 | #include <cstdio> // vsprintf, vsnprintf |
| 14 | #include <cstring> // strcpy, wcsncpy |
| 15 | #include <cwchar> // mbstate_t |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 16 | |
Howard Hinnant | 39e9506 | 2013-04-02 15:46:31 +0000 | [diff] [blame] | 17 | |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 18 | // Like sprintf, but when return value >= 0 it returns |
| 19 | // a pointer to a malloc'd string in *sptr. |
Howard Hinnant | 39e9506 | 2013-04-02 15:46:31 +0000 | [diff] [blame] | 20 | // If return >= 0, use free to delete *sptr. |
Peter Collingbourne | 2f97aab | 2018-01-23 18:53:33 +0000 | [diff] [blame] | 21 | int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap ) |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 22 | { |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 23 | *sptr = NULL; |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 24 | // Query the count required. |
| 25 | int count = _vsnprintf( NULL, 0, format, ap ); |
| 26 | if (count < 0) |
| 27 | return count; |
| 28 | size_t buffer_size = static_cast<size_t>(count) + 1; |
| 29 | char* p = static_cast<char*>(malloc(buffer_size)); |
| 30 | if ( ! p ) |
| 31 | return -1; |
| 32 | // If we haven't used exactly what was required, something is wrong. |
| 33 | // Maybe bug in vsnprintf. Report the error and return. |
| 34 | if (_vsnprintf(p, buffer_size, format, ap) != count) { |
| 35 | free(p); |
| 36 | return -1; |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 37 | } |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 38 | // All good. This is returning memory to the caller not freeing it. |
| 39 | *sptr = p; |
Howard Hinnant | 3c78ca0 | 2011-09-22 19:10:18 +0000 | [diff] [blame] | 40 | return count; |
| 41 | } |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 42 | |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 43 | // Returns >= 0: the number of wide characters found in the |
| 44 | // multi byte sequence src (of src_size_bytes), that fit in the buffer dst |
| 45 | // (of max_dest_chars elements size). The count returned excludes the |
| 46 | // null terminator. When dst is NULL, no characters are copied |
| 47 | // and no "out" parameters are updated. |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 48 | // Returns (size_t) -1: an incomplete sequence encountered. |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 49 | // Leaves *src pointing the next character to convert or NULL |
| 50 | // if a null character was converted from *src. |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 51 | size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src, |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 52 | size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps ) |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 53 | { |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 54 | const size_t terminated_sequence = static_cast<size_t>(0); |
| 55 | //const size_t invalid_sequence = static_cast<size_t>(-1); |
| 56 | const size_t incomplete_sequence = static_cast< size_t>(-2); |
| 57 | |
| 58 | size_t dest_converted = 0; |
| 59 | size_t source_converted = 0; |
| 60 | size_t source_remaining = src_size_bytes; |
| 61 | size_t result = 0; |
| 62 | bool have_result = false; |
| 63 | |
Marshall Clow | 8681a3b | 2019-01-23 18:27:22 +0000 | [diff] [blame] | 64 | // If dst is null then max_dest_chars should be ignored according to the |
| 65 | // standard. Setting max_dest_chars to a large value has this effect. |
| 66 | if (!dst) |
| 67 | max_dest_chars = static_cast<size_t>(-1); |
| 68 | |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 69 | while ( source_remaining ) { |
| 70 | if ( dst && dest_converted >= max_dest_chars ) |
| 71 | break; |
| 72 | // Converts one multi byte character. |
| 73 | // if result > 0, it's the size in bytes of that character. |
| 74 | // othewise if result is zero it indicates the null character has been found. |
| 75 | // otherwise it's an error and errno may be set. |
| 76 | size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps ); |
| 77 | // Don't do anything to change errno from here on. |
| 78 | if ( char_size > 0 ) { |
| 79 | source_remaining -= char_size; |
| 80 | source_converted += char_size; |
| 81 | ++dest_converted; |
| 82 | continue; |
| 83 | } |
| 84 | result = char_size; |
| 85 | have_result = true; |
| 86 | break; |
| 87 | } |
| 88 | if ( dst ) { |
| 89 | if ( have_result && result == terminated_sequence ) |
| 90 | *src = NULL; |
| 91 | else |
| 92 | *src += source_converted; |
| 93 | } |
| 94 | if ( have_result && result != terminated_sequence && result != incomplete_sequence ) |
| 95 | return static_cast<size_t>(-1); |
| 96 | |
| 97 | return dest_converted; |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 98 | } |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 99 | |
| 100 | // Converts max_source_chars from the wide character buffer pointer to by *src, |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 101 | // into the multi byte character sequence buffer stored at dst which must be |
| 102 | // dst_size_bytes bytes in size. |
Alp Toker | f03763a | 2014-05-15 11:27:39 +0000 | [diff] [blame] | 103 | // Returns >= 0: the number of bytes in the sequence |
| 104 | // converted from *src, excluding the null terminator. |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 105 | // Returns size_t(-1) if an error occurs, also sets errno. |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 106 | // If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst |
| 107 | // and no "out" parameters are updated. |
Howard Hinnant | e438337 | 2011-10-22 20:59:45 +0000 | [diff] [blame] | 108 | size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src, |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 109 | size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps ) |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 110 | { |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 111 | //const size_t invalid_sequence = static_cast<size_t>(-1); |
| 112 | |
| 113 | size_t source_converted = 0; |
| 114 | size_t dest_converted = 0; |
| 115 | size_t dest_remaining = dst_size_bytes; |
| 116 | size_t char_size = 0; |
| 117 | const errno_t no_error = ( errno_t) 0; |
| 118 | errno_t result = ( errno_t ) 0; |
| 119 | bool have_result = false; |
| 120 | bool terminator_found = false; |
| 121 | |
Marshall Clow | 8681a3b | 2019-01-23 18:27:22 +0000 | [diff] [blame] | 122 | // If dst is null then dst_size_bytes should be ignored according to the |
| 123 | // standard. Setting dest_remaining to a large value has this effect. |
| 124 | if (!dst) |
| 125 | dest_remaining = static_cast<size_t>(-1); |
| 126 | |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 127 | while ( source_converted != max_source_chars ) { |
| 128 | if ( ! dest_remaining ) |
| 129 | break; |
| 130 | wchar_t c = (*src)[source_converted]; |
| 131 | if ( dst ) |
| 132 | result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps); |
| 133 | else |
Howard Hinnant | 9daaf57 | 2013-05-16 17:13:40 +0000 | [diff] [blame] | 134 | result = wcrtomb_s( &char_size, NULL, 0, c, ps); |
Howard Hinnant | 0ed0d69 | 2013-08-26 20:18:01 +0000 | [diff] [blame] | 135 | // If result is zero there is no error and char_size contains the |
| 136 | // size of the multi-byte-sequence converted. |
Howard Hinnant | 4594482 | 2013-04-02 22:14:51 +0000 | [diff] [blame] | 137 | // Otherwise result indicates an errno type error. |
| 138 | if ( result == no_error ) { |
| 139 | if ( c == L'\0' ) { |
| 140 | terminator_found = true; |
| 141 | break; |
| 142 | } |
| 143 | ++source_converted; |
| 144 | if ( dst ) |
| 145 | dest_remaining -= char_size; |
| 146 | dest_converted += char_size; |
| 147 | continue; |
| 148 | } |
| 149 | have_result = true; |
| 150 | break; |
| 151 | } |
| 152 | if ( dst ) { |
| 153 | if ( terminator_found ) |
| 154 | *src = NULL; |
| 155 | else |
| 156 | *src = *src + source_converted; |
| 157 | } |
| 158 | if ( have_result && result != no_error ) { |
| 159 | errno = result; |
| 160 | return static_cast<size_t>(-1); |
| 161 | } |
| 162 | |
| 163 | return dest_converted; |
Howard Hinnant | dbe8111 | 2011-09-23 16:11:27 +0000 | [diff] [blame] | 164 | } |