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