xref: /llvm-project/libcxx/src/support/win32/support.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
1 // -*- C++ -*-
2 //===----------------------- support/win32/support.h ----------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
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
16 
17 
18 // Like sprintf, but when return value >= 0 it returns
19 // a pointer to a malloc'd string in *sptr.
20 // If return >= 0, use free to delete *sptr.
21 int __libcpp_vasprintf( char **sptr, const char *__restrict format, va_list ap )
22 {
23     *sptr = NULL;
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;
37     }
38     // All good. This is returning memory to the caller not freeing it.
39     *sptr = p;
40     return count;
41 }
42 
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.
48 // Returns (size_t) -1: an incomplete sequence encountered.
49 // Leaves *src pointing the next character to convert or NULL
50 // if a null character was converted from *src.
51 size_t mbsnrtowcs( wchar_t *__restrict dst, const char **__restrict src,
52                    size_t src_size_bytes, size_t max_dest_chars, mbstate_t *__restrict ps )
53 {
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 
64     while ( source_remaining ) {
65         if ( dst && dest_converted >= max_dest_chars )
66             break;
67         // Converts one multi byte character.
68         // if result > 0, it's the size in bytes of that character.
69         // othewise if result is zero it indicates the null character has been found.
70         // otherwise it's an error and errno may be set.
71         size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
72         // Don't do anything to change errno from here on.
73         if ( char_size > 0 ) {
74             source_remaining -= char_size;
75             source_converted += char_size;
76             ++dest_converted;
77             continue;
78         }
79         result = char_size;
80         have_result = true;
81         break;
82     }
83     if ( dst ) {
84         if ( have_result && result == terminated_sequence )
85             *src = NULL;
86         else
87             *src += source_converted;
88     }
89     if ( have_result && result != terminated_sequence && result != incomplete_sequence )
90         return static_cast<size_t>(-1);
91 
92     return dest_converted;
93 }
94 
95 // Converts max_source_chars from the wide character buffer pointer to by *src,
96 // into the multi byte character sequence buffer stored at dst which must be
97 // dst_size_bytes bytes in size.
98 // Returns >= 0: the number of bytes in the sequence
99 // converted from *src, excluding the null terminator.
100 // Returns size_t(-1) if an error occurs, also sets errno.
101 // If dst is NULL dst_size_bytes is ignored and no bytes are copied to dst
102 // and no "out" parameters are updated.
103 size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
104                    size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
105 {
106     //const size_t invalid_sequence = static_cast<size_t>(-1);
107 
108     size_t source_converted = 0;
109     size_t dest_converted = 0;
110     size_t dest_remaining = dst_size_bytes;
111     size_t char_size = 0;
112     const errno_t no_error = ( errno_t) 0;
113     errno_t result = ( errno_t ) 0;
114     bool have_result = false;
115     bool terminator_found = false;
116 
117     while ( source_converted != max_source_chars ) {
118         if ( ! dest_remaining )
119             break;
120         wchar_t c = (*src)[source_converted];
121         if ( dst )
122             result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
123         else
124             result = wcrtomb_s( &char_size, NULL, 0, c, ps);
125         // If result is zero there is no error and char_size contains the
126         // size of the multi-byte-sequence converted.
127         // Otherwise result indicates an errno type error.
128         if ( result == no_error ) {
129             if ( c == L'\0' ) {
130                 terminator_found = true;
131                 break;
132             }
133             ++source_converted;
134             if ( dst )
135                 dest_remaining -= char_size;
136             dest_converted += char_size;
137             continue;
138         }
139         have_result = true;
140         break;
141     }
142     if ( dst ) {
143         if ( terminator_found )
144             *src = NULL;
145         else
146             *src = *src + source_converted;
147     }
148     if ( have_result && result != no_error ) {
149         errno = result;
150         return static_cast<size_t>(-1);
151     }
152 
153     return dest_converted;
154 }
155