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