xref: /llvm-project/libcxx/src/support/win32/support.cpp (revision 459448241ad9cf507943bb7290aa773cb917f4f5)
1 // -*- C++ -*-
2 //===----------------------- support/win32/support.h ----------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include <support/win32/support.h>
12 #include <stdarg.h> // va_start, va_end
13 #include <stddef.h> // size_t
14 #include <stdlib.h> // malloc
15 #include <stdio.h>  // vsprintf, vsnprintf
16 #include <string.h> // strcpy, wcsncpy
17 
18 int asprintf(char **sptr, const char *__restrict fmt, ...)
19 {
20     va_list ap;
21     va_start(ap, fmt);
22     int result = vasprintf(sptr, fmt, ap);
23     va_end(ap);
24     return result;
25 }
26 
27 // Like sprintf, but when return value >= 0 it returns a pointer to a malloc'd string in *sptr.
28 // If return >= 0, use free to delete *sptr.
29 int vasprintf( char **sptr, const char *__restrict fmt, va_list ap )
30 {
31     *sptr = NULL;
32     int count = vsnprintf( NULL, 0, fmt, ap ); // Query the buffer size required.
33     if( count >= 0 ) {
34         char* p = static_cast<char*>(malloc(count+1)); // Allocate memory for it and the terminator.
35         if ( p == NULL )
36             return -1;
37         if ( vsnprintf( p, count+1, fmt, ap ) == count ) // We should have used exactly what was required.
38             *sptr = p;
39         else { // Otherwise something is wrong, likely a bug in vsnprintf. If so free the memory and report the error.
40             free(p);
41             return -1;
42         }
43     }
44 
45     return count;
46 }
47 
48 // Returns >= 0: the number of wide characters found in the multi byte sequence src (of src_size_bytes),
49 // that fit in the buffer dst (of max_dest_chars elements size). The count returned excludes the null terminator.
50 // When dst is NULL, no characters are copied and no "out" parameters are updated.
51 // Returns (size_t) -1: an incomplete sequence encountered.
52 // Leaves *src pointing the next character to convert or NULL 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     while ( source_remaining ) {
67         if ( dst && dest_converted >= max_dest_chars )
68             break;
69         // Converts one multi byte character.
70         // if result > 0, it's the size in bytes of that character.
71         // othewise if result is zero it indicates the null character has been found.
72         // otherwise it's an error and errno may be set.
73         size_t char_size = mbrtowc( dst ? dst + dest_converted : NULL, *src + source_converted, source_remaining, ps );
74         // Don't do anything to change errno from here on.
75         if ( char_size > 0 ) {
76             source_remaining -= char_size;
77             source_converted += char_size;
78             ++dest_converted;
79             continue;
80         }
81         result = char_size;
82         have_result = true;
83         break;
84     }
85     if ( dst ) {
86         if ( have_result && result == terminated_sequence )
87             *src = NULL;
88         else
89             *src += source_converted;
90     }
91     if ( have_result && result != terminated_sequence && result != incomplete_sequence )
92         return static_cast<size_t>(-1);
93 
94     return dest_converted;
95 }
96 
97 // Converts max_source_chars from the wide character buffer pointer to by *src,
98 // into the multi byte character sequence buffer stored at dst which must be dst_size_bytes bytes in size.
99 // Returns >= 0: the number of bytes in the sequence sequence converted frome *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 and no "out" parameters are updated.
102 size_t wcsnrtombs( char *__restrict dst, const wchar_t **__restrict src,
103                    size_t max_source_chars, size_t dst_size_bytes, mbstate_t *__restrict ps )
104 {
105     //const size_t invalid_sequence = static_cast<size_t>(-1);
106 
107     size_t source_converted = 0;
108     size_t dest_converted = 0;
109     size_t dest_remaining = dst_size_bytes;
110     size_t char_size = 0;
111     const errno_t no_error = ( errno_t) 0;
112     errno_t result = ( errno_t ) 0;
113     bool have_result = false;
114     bool terminator_found = false;
115 
116     while ( source_converted != max_source_chars ) {
117         if ( ! dest_remaining )
118             break;
119         wchar_t c = (*src)[source_converted];
120         if ( dst )
121             result = wcrtomb_s( &char_size, dst + dest_converted, dest_remaining, c, ps);
122         else
123             result = wcrtomb_s( &char_size, NULL, 0, c, ps);
124         // If result is zero there is no error and char_size contains the size of the multi-byte-sequence converted.
125         // Otherwise result indicates an errno type error.
126         if ( result == no_error ) {
127             if ( c == L'\0' ) {
128                 terminator_found = true;
129                 break;
130             }
131             ++source_converted;
132             if ( dst )
133                 dest_remaining -= char_size;
134             dest_converted += char_size;
135             continue;
136         }
137         have_result = true;
138         break;
139     }
140     if ( dst ) {
141         if ( terminator_found )
142             *src = NULL;
143         else
144             *src = *src + source_converted;
145     }
146     if ( have_result && result != no_error ) {
147         errno = result;
148         return static_cast<size_t>(-1);
149     }
150 
151     return dest_converted;
152 }
153