codekingpro commited on
Commit
90219c5
·
verified ·
1 Parent(s): ae1db56

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/AclUI.Lib +0 -0
  2. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/ActiveDS.Lib +0 -0
  3. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/AuthZ.Lib +0 -0
  4. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/BluetoothApis.lib +0 -0
  5. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/Cabinet.Lib +0 -0
  6. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/advpack.Lib +0 -0
  7. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/ahadmin.lib +0 -0
  8. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/amsi.lib +0 -0
  9. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appmgmts.lib +0 -0
  10. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appmgr.lib +0 -0
  11. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appnotify.lib +0 -0
  12. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/audioeng.lib +0 -0
  13. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/avifil32.Lib +0 -0
  14. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/avrt.lib +0 -0
  15. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/basesrv.lib +0 -0
  16. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bcp47mrm.Lib +0 -0
  17. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bcrypt.lib +0 -0
  18. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bindlink.lib +0 -0
  19. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bthprops.lib +0 -0
  20. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/certadm.lib +0 -0
  21. msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/certca.lib +0 -0
  22. msvc/Windows Kits/10/Redist/MBN/arm64/microsoft.mbn.dll +0 -0
  23. msvc/Windows Kits/10/Redist/MBN/x64/microsoft.mbn.dll +0 -0
  24. msvc/Windows Kits/10/Redist/MBN/x86/microsoft.mbn.dll +0 -0
  25. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cgets.cpp +128 -0
  26. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cgetws.cpp +156 -0
  27. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cprintf.cpp +104 -0
  28. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cputs.cpp +38 -0
  29. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cputws.cpp +63 -0
  30. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cscanf.cpp +56 -0
  31. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/getch.cpp +569 -0
  32. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/getwch.cpp +201 -0
  33. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/initcon.cpp +83 -0
  34. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/initconin.cpp +162 -0
  35. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/pipe.cpp +113 -0
  36. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/popen.cpp +488 -0
  37. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/putch.cpp +82 -0
  38. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/putwch.cpp +39 -0
  39. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_ctype.cpp +254 -0
  40. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_fptostr.cpp +173 -0
  41. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_mbslen.cpp +119 -0
  42. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_wctype.cpp +171 -0
  43. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atof.cpp +96 -0
  44. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atoldbl.cpp +833 -0
  45. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atox.cpp +107 -0
  46. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/c16rtomb.cpp +94 -0
  47. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/c32rtomb.cpp +93 -0
  48. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/cfout.cpp +367 -0
  49. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/common_utf8.cpp +27 -0
  50. msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/cvt.cpp +849 -0
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/AclUI.Lib ADDED
Binary file (2.85 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/ActiveDS.Lib ADDED
Binary file (7.39 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/AuthZ.Lib ADDED
Binary file (11 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/BluetoothApis.lib ADDED
Binary file (29 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/Cabinet.Lib ADDED
Binary file (6.74 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/advpack.Lib ADDED
Binary file (21.1 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/ahadmin.lib ADDED
Binary file (62.1 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/amsi.lib ADDED
Binary file (3.5 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appmgmts.lib ADDED
Binary file (5.29 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appmgr.lib ADDED
Binary file (1.53 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/appnotify.lib ADDED
Binary file (5.24 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/audioeng.lib ADDED
Binary file (1.91 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/avifil32.Lib ADDED
Binary file (16.7 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/avrt.lib ADDED
Binary file (6.57 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/basesrv.lib ADDED
Binary file (2.97 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bcp47mrm.Lib ADDED
Binary file (5.69 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bcrypt.lib ADDED
Binary file (14.6 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bindlink.lib ADDED
Binary file (4.37 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/bthprops.lib ADDED
Binary file (10.9 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/certadm.lib ADDED
Binary file (12.6 kB). View file
 
msvc/Windows Kits/10/Lib/10.0.26100.0/um/x64/certca.lib ADDED
Binary file (1.53 kB). View file
 
msvc/Windows Kits/10/Redist/MBN/arm64/microsoft.mbn.dll ADDED
Binary file (43.4 kB). View file
 
msvc/Windows Kits/10/Redist/MBN/x64/microsoft.mbn.dll ADDED
Binary file (43.4 kB). View file
 
msvc/Windows Kits/10/Redist/MBN/x86/microsoft.mbn.dll ADDED
Binary file (43.4 kB). View file
 
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cgets.cpp ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cgets.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _cgets() and _cgets_s(), which read a string from the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <corecrt_internal_securecrt.h>
10
+ #include <stdlib.h>
11
+
12
+
13
+
14
+ extern "C" extern intptr_t __dcrt_lowio_console_input_handle;
15
+
16
+
17
+
18
+ // Use of the following buffer variables is primarily for syncronizing with
19
+ // _cgets_s. _cget_s fills the MBCS buffer and if the user passes in single
20
+ // character buffer and the unicode character is not converted to single byte
21
+ // MBC, then _cget_s should buffer that character so that next call to
22
+ // _cgetws_s can return the same character.
23
+ extern "C" extern wchar_t __console_wchar_buffer;
24
+ extern "C" extern int __console_wchar_buffer_used;
25
+
26
+
27
+
28
+ // Reads a string from the console; always null-terminates the buffer. Returns
29
+ // 0 on success; returns an errno error code on failure.
30
+ extern "C" errno_t __cdecl _cgets_s(char* const source_string, size_t const size_in_bytes, size_t* const size_read)
31
+ {
32
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(source_string != nullptr, EINVAL);
33
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_in_bytes > 0, EINVAL);
34
+ _RESET_STRING(source_string, size_in_bytes);
35
+
36
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_read != nullptr, EINVAL);
37
+
38
+ errno_t error = 0;
39
+ char* string = source_string;
40
+
41
+ __acrt_lock(__acrt_conio_lock);
42
+ __try
43
+ {
44
+ // The implementation of cgets is slightly tricky. The reason being,
45
+ // the code page for console is different from the CRT code page.
46
+ // What this means is the program may interpret character
47
+ // differently from it's acctual value. To fix this, what we really
48
+ // want to do is read the input as unicode string and then convert
49
+ // it to proper MBCS representation.
50
+ //
51
+ // This fix this we are really converting from Unicode to MBCS.
52
+ // This adds performance problem as we may endup doing this
53
+ // character by character. The basic problem here is that we have
54
+ // no way to know how many UNICODE characters will be needed to fit
55
+ // them in given size of MBCS buffer. To fix this issue we will be
56
+ // converting one Unicode character at a time to MBCS. This makes
57
+ // this slow, but then this is already console input,
58
+ *size_read = 0;
59
+
60
+ size_t available = size_in_bytes - 1;
61
+ do
62
+ {
63
+ wchar_t wchar_buff[2];
64
+ size_t sizeRead = 0;
65
+
66
+ error = _cgetws_s(wchar_buff, _countof(wchar_buff), &sizeRead);
67
+ if (error != 0)
68
+ break;
69
+
70
+ if (wchar_buff[0] == '\0')
71
+ break;
72
+
73
+ int size_converted = 0;
74
+ errno_t const wctomb_result = wctomb_s(&size_converted, string, available, wchar_buff[0]);
75
+ if (wctomb_result != 0)
76
+ {
77
+ // Put the wide character back in the buffer so that the
78
+ // unutilized wide character is still in the stream:
79
+ __console_wchar_buffer = wchar_buff[0];
80
+ __console_wchar_buffer_used = 1;
81
+ break;
82
+ }
83
+
84
+ string += size_converted;
85
+ *size_read += size_converted;
86
+ available -= size_converted;
87
+ }
88
+ while (available > 0);
89
+ }
90
+ __finally
91
+ {
92
+ __acrt_unlock(__acrt_conio_lock);
93
+ }
94
+
95
+ *string++ = '\0';
96
+
97
+ if (error != 0)
98
+ errno = error;
99
+
100
+ return error;
101
+ }
102
+
103
+
104
+
105
+ // Reads a string from the console via ReadConsole on a cooked console handle.
106
+ // string[0] must contain the maximum length of the string. The number of
107
+ // characters written is stored in string[1]. The return value is a pointer to
108
+ // string[2] on success; nullptr on failure.
109
+ extern "C" char* __cdecl _cgets(_Inout_z_ char* const string)
110
+ {
111
+ _VALIDATE_CLEAR_OSSERR_RETURN(string != nullptr, EINVAL, nullptr);
112
+ _VALIDATE_CLEAR_OSSERR_RETURN(string[0] > 0, EINVAL, nullptr);
113
+
114
+ size_t const size_in_bytes = static_cast<size_t>(string[0]);
115
+
116
+ size_t size_read = 0;
117
+ // warning 26018: Potential overflow of null terminated buffer using expression string+2
118
+ // Suppressing warning since _cgets is purposefully unsafe.
119
+ #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
120
+ errno_t const result = _cgets_s(string + 2, size_in_bytes, &size_read);
121
+
122
+ // warning 26018: Potential overflow of null terminated buffer using expression string[1]
123
+ // Suppressing warning since _cgets is purposefully unsafe.
124
+ #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
125
+ string[1] = static_cast<char>(size_read);
126
+
127
+ return result == 0 ? string + 2 : nullptr;
128
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cgetws.cpp ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cgetws.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _cgetws() and _cgetws_s(), which read a wide string from the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <corecrt_internal_lowio.h>
10
+ #include <corecrt_internal_securecrt.h>
11
+ #include <stdlib.h>
12
+
13
+ // Use of the following buffer variables is primarily for syncronizing with
14
+ // _cgets_s. _cget_s fills the MBCS buffer and if the user passes in single
15
+ // character buffer and the unicode character is not converted to single byte
16
+ // MBC, then _cget_s should buffer that character so that next call to
17
+ // _cgetws_s can return the same character.
18
+ extern "C" wchar_t __console_wchar_buffer = 0;
19
+ extern "C" int __console_wchar_buffer_used = 0;
20
+
21
+
22
+
23
+ // Reads a string from the console; always null-terminates the buffer. Returns
24
+ // 0 on success; returns an errno error code on failure.
25
+ extern "C" errno_t __cdecl _cgetws_s(wchar_t* const string_buffer, size_t const size_in_words, size_t* const size_read)
26
+ {
27
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(string_buffer != nullptr, EINVAL);
28
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_in_words > 0, EINVAL);
29
+ _RESET_STRING(string_buffer, size_in_words);
30
+
31
+ _VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(size_read != nullptr, EINVAL);
32
+
33
+ __acrt_lock(__acrt_conio_lock);
34
+ errno_t retval = 0;
35
+ __try
36
+ {
37
+ wchar_t* string = string_buffer;
38
+
39
+ // We need to decrement size_in_words because ReadConsole reads as many
40
+ // characters as the parameter passed. It doesn't null-terminate:
41
+ size_t size_remaining = size_in_words - 1;
42
+ *size_read = 0;
43
+
44
+ // If size_in_words was 1, then there's only room for the null terminator:
45
+ if (size_remaining == 0)
46
+ __leave;
47
+
48
+ // If there is a buffered character, first fill with the buffered
49
+ // character then proceed with reading from the console:
50
+ if (__console_wchar_buffer_used != 0)
51
+ {
52
+ *string++ = __console_wchar_buffer;
53
+ --size_remaining;
54
+ (*size_read)++;
55
+
56
+ if (__console_wchar_buffer == L'\0')
57
+ size_remaining = 0;
58
+
59
+ __console_wchar_buffer = 0;
60
+ }
61
+
62
+ if (size_remaining == 0)
63
+ __leave;
64
+
65
+ /*
66
+ * __dcrt_lowio_console_input_handle, the handle to the console input, is created the first
67
+ * time that either _getch() or _cgets() or _kbhit() is called.
68
+ */
69
+
70
+ if (__dcrt_lowio_ensure_console_input_initialized() == FALSE)
71
+ {
72
+ __acrt_errno_map_os_error(GetLastError());
73
+ retval = errno;
74
+ __leave;
75
+ }
76
+
77
+ ULONG old_state;
78
+ __dcrt_get_input_console_mode(&old_state);
79
+ __dcrt_set_input_console_mode(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT);
80
+
81
+ ULONG wchars_read;
82
+ BOOL read_console_result = __dcrt_read_console(
83
+ string,
84
+ static_cast<DWORD>(size_remaining),
85
+ &wchars_read);
86
+
87
+ if (!read_console_result)
88
+ {
89
+ __acrt_errno_map_os_error(GetLastError());
90
+ retval = errno;
91
+ __leave;
92
+ }
93
+
94
+ // Set the length of the string and null terminate it:
95
+ if (wchars_read >= 2 && string[wchars_read - 2] == L'\r')
96
+ {
97
+ *size_read += wchars_read - 2;
98
+ string[wchars_read - 2] = L'\0';
99
+ }
100
+ else if (wchars_read == size_remaining && string[wchars_read - 1] == L'\r')
101
+ {
102
+ // Special case 1: \r\n straddles the boundary:
103
+ string[wchars_read - 1] = L'\0';
104
+ *size_read += wchars_read - 1;
105
+ }
106
+ else if (wchars_read == 1 && string[0] == L'\n')
107
+ {
108
+ // Special case 2: Read a single \n:
109
+ string[0] = L'\0';
110
+ *size_read += 0;
111
+ }
112
+ else
113
+ {
114
+ *size_read += wchars_read;
115
+ string[wchars_read] = L'\0';
116
+ }
117
+
118
+ __dcrt_set_input_console_mode(old_state);
119
+ }
120
+ __finally
121
+ {
122
+ __acrt_unlock(__acrt_conio_lock);
123
+ }
124
+
125
+ return retval;
126
+ }
127
+
128
+
129
+
130
+ // Reads a string from the console via ReadConsole on a cooked console handle.
131
+ // string[0] must contain the maximum length of the string. The number of
132
+ // characters written is stored in string[1]. The return value is a pointer to
133
+ // string[2] on success; nullptr on failure.
134
+ //
135
+ // Note that _cgetws() does NOT check the pushback character buffer, so it will
136
+ // not return any character that is pushed back by a call to _ungetwch().
137
+ extern "C" wchar_t* __cdecl _cgetws(_Inout_z_ wchar_t* const string)
138
+ {
139
+ _VALIDATE_CLEAR_OSSERR_RETURN(string != nullptr, EINVAL, nullptr);
140
+ _VALIDATE_CLEAR_OSSERR_RETURN(string[0] > 0, EINVAL, nullptr);
141
+
142
+ size_t const size_in_words = static_cast<size_t>(string[0]);
143
+
144
+ size_t size_read = 0;
145
+ // warning 26018: Potential overflow of null terminated buffer using expression string+2
146
+ // Suppressing warning since _cgetws is purposefully unsafe.
147
+ #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
148
+ errno_t const result = _cgetws_s(string + 2, size_in_words, &size_read);
149
+
150
+ // warning 26018: Potential overflow of null terminated buffer using expression string[1]
151
+ // Suppressing warning since _cgetws is purposefully unsafe.
152
+ #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED)
153
+ string[1] = static_cast<wchar_t>(size_read);
154
+
155
+ return result == 0 ? string + 2 : nullptr;
156
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cprintf.cpp ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cprintf.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // The standard output functions, which perform formatted output to the
7
+ // console.
8
+ //
9
+ #include <corecrt_internal_stdio_output.h>
10
+
11
+
12
+ using namespace __crt_stdio_output;
13
+
14
+
15
+
16
+ template <template <typename, typename> class Base, typename Character>
17
+ static int __cdecl common_vcprintf(
18
+ unsigned __int64 const options,
19
+ Character const* const format,
20
+ __crt_cached_ptd_host& ptd,
21
+ va_list const arglist
22
+ )
23
+ {
24
+ typedef output_processor<
25
+ Character,
26
+ console_output_adapter<Character>,
27
+ Base<Character, console_output_adapter<Character>>
28
+ > processor_type;
29
+
30
+ processor_type processor(
31
+ console_output_adapter<Character>(),
32
+ options,
33
+ format,
34
+ ptd,
35
+ arglist);
36
+
37
+ return processor.process();
38
+ }
39
+
40
+ extern "C" int __cdecl __conio_common_vcprintf(
41
+ unsigned __int64 const options,
42
+ char const* const format,
43
+ _locale_t const locale,
44
+ va_list const arglist
45
+ )
46
+ {
47
+ __crt_cached_ptd_host ptd{locale};
48
+ return common_vcprintf<standard_base>(options, format, ptd, arglist);
49
+ }
50
+
51
+ extern "C" int __cdecl __conio_common_vcprintf_s(
52
+ unsigned __int64 const options,
53
+ char const* const format,
54
+ _locale_t const locale,
55
+ va_list const arglist
56
+ )
57
+ {
58
+ __crt_cached_ptd_host ptd{locale};
59
+ return common_vcprintf<format_validation_base>(options, format, ptd, arglist);
60
+ }
61
+
62
+ extern "C" int __cdecl __conio_common_vcprintf_p(
63
+ unsigned __int64 const options,
64
+ char const* const format,
65
+ _locale_t const locale,
66
+ va_list const arglist
67
+ )
68
+ {
69
+ __crt_cached_ptd_host ptd{locale};
70
+ return common_vcprintf<positional_parameter_base>(options, format, ptd, arglist);
71
+ }
72
+
73
+ extern "C" int __cdecl __conio_common_vcwprintf(
74
+ unsigned __int64 const options,
75
+ wchar_t const* const format,
76
+ _locale_t const locale,
77
+ va_list const arglist
78
+ )
79
+ {
80
+ __crt_cached_ptd_host ptd{locale};
81
+ return common_vcprintf<standard_base>(options, format, ptd, arglist);
82
+ }
83
+
84
+ extern "C" int __cdecl __conio_common_vcwprintf_s(
85
+ unsigned __int64 const options,
86
+ wchar_t const* const format,
87
+ _locale_t const locale,
88
+ va_list const arglist
89
+ )
90
+ {
91
+ __crt_cached_ptd_host ptd{locale};
92
+ return common_vcprintf<format_validation_base>(options, format, ptd, arglist);
93
+ }
94
+
95
+ extern "C" int __cdecl __conio_common_vcwprintf_p(
96
+ unsigned __int64 const options,
97
+ wchar_t const* const format,
98
+ _locale_t const locale,
99
+ va_list const arglist
100
+ )
101
+ {
102
+ __crt_cached_ptd_host ptd{locale};
103
+ return common_vcprintf<positional_parameter_base>(options, format, ptd, arglist);
104
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cputs.cpp ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cputs.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _cputs(), which writes a string directly to the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <corecrt_internal_lowio.h>
10
+
11
+ // Writes the given string directly to the console. No newline is appended.
12
+ // Returns 0 on success; nonzero on failure.
13
+ extern "C" int __cdecl _cputs(char const* const string)
14
+ {
15
+ _VALIDATE_CLEAR_OSSERR_RETURN(string != nullptr, EINVAL, -1);
16
+
17
+ __acrt_lock(__acrt_conio_lock);
18
+ int result = 0;
19
+ __try
20
+ {
21
+ // Write the string directly to the console. Each character is written
22
+ // individually, as performance of this function is not considered
23
+ // critical.
24
+ for (char const* p = string; *p; ++p)
25
+ {
26
+ if (_putch_nolock(*p) == EOF)
27
+ {
28
+ result = -1;
29
+ __leave;
30
+ }
31
+ }
32
+ }
33
+ __finally
34
+ {
35
+ __acrt_unlock(__acrt_conio_lock);
36
+ }
37
+ return result;
38
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cputws.cpp ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cputws.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _cputws(), which writes a wide string directly to the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <errno.h>
10
+ #include <corecrt_internal_lowio.h>
11
+ #include <limits.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+
15
+ // Writes the given string directly to the console. No newline is appended.
16
+ //
17
+ // Returns 0 on success; nonzero on failure.
18
+ extern "C" int __cdecl _cputws(wchar_t const* string)
19
+ {
20
+ _VALIDATE_CLEAR_OSSERR_RETURN((string != nullptr), EINVAL, -1);
21
+
22
+ if (__dcrt_lowio_ensure_console_output_initialized() == FALSE)
23
+ return -1;
24
+
25
+ // Write string to console file handle:
26
+ ptrdiff_t length = wcslen(string);
27
+
28
+ __acrt_lock(__acrt_conio_lock);
29
+
30
+ int result = 0;
31
+
32
+ __try
33
+ {
34
+ while (length > 0)
35
+ {
36
+ static size_t const max_write_bytes = 65535;
37
+ static size_t const max_write_wchars = max_write_bytes / sizeof(wchar_t);
38
+
39
+ DWORD const wchars_to_write = length > max_write_wchars
40
+ ? max_write_wchars
41
+ : static_cast<DWORD>(length);
42
+
43
+ DWORD wchars_written;
44
+ if (__dcrt_write_console(
45
+ string,
46
+ wchars_to_write,
47
+ &wchars_written) == FALSE)
48
+ {
49
+ result = -1;
50
+ __leave;
51
+ }
52
+
53
+ string += wchars_to_write;
54
+ length -= wchars_to_write;
55
+ }
56
+ }
57
+ __finally
58
+ {
59
+ __acrt_unlock(__acrt_conio_lock);
60
+ }
61
+
62
+ return result;
63
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/cscanf.cpp ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cscanf.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // The core formatted input functions for direct console I/O.
7
+ //
8
+ #define _ALLOW_OLD_VALIDATE_MACROS
9
+ #include <corecrt_internal_stdio_input.h>
10
+
11
+ using namespace __crt_stdio_input;
12
+
13
+ template <typename Character>
14
+ static int __cdecl common_cscanf(
15
+ unsigned __int64 const options,
16
+ Character const* const format,
17
+ _locale_t const locale,
18
+ va_list const arglist
19
+ ) throw()
20
+ {
21
+ typedef input_processor<
22
+ Character,
23
+ console_input_adapter<Character>
24
+ > processor_type;
25
+
26
+ _LocaleUpdate locale_update(locale);
27
+
28
+ processor_type processor(
29
+ console_input_adapter<Character>(),
30
+ options,
31
+ format,
32
+ locale_update.GetLocaleT(),
33
+ arglist);
34
+
35
+ return processor.process();
36
+ }
37
+
38
+ extern "C" int __cdecl __conio_common_vcscanf(
39
+ unsigned __int64 const options,
40
+ char const* const format,
41
+ _locale_t const locale,
42
+ va_list const arglist
43
+ )
44
+ {
45
+ return common_cscanf(options, format, locale, arglist);
46
+ }
47
+
48
+ extern "C" int __cdecl __conio_common_vcwscanf(
49
+ unsigned __int64 const options,
50
+ wchar_t const* const format,
51
+ _locale_t const locale,
52
+ va_list const arglist
53
+ )
54
+ {
55
+ return common_cscanf(options, format, locale, arglist);
56
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/getch.cpp ADDED
@@ -0,0 +1,569 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // getch.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _getch(), _getche(), and _ungetch(), which get and unget
7
+ // characters directly from the console.
8
+ //
9
+ #include <conio.h>
10
+ #include <corecrt_internal_lowio.h>
11
+ #include <limits.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+
15
+
16
+
17
+ namespace
18
+ {
19
+ struct CharPair
20
+ {
21
+ unsigned char LeadChar;
22
+ unsigned char SecondChar;
23
+ };
24
+
25
+ struct EnhKeyVals
26
+ {
27
+ unsigned short ScanCode;
28
+ CharPair RegChars;
29
+ CharPair ShiftChars;
30
+ CharPair CtrlChars;
31
+ CharPair AltChars;
32
+ };
33
+
34
+ struct NormKeyVals
35
+ {
36
+ CharPair RegChars;
37
+ CharPair ShiftChars;
38
+ CharPair CtrlChars;
39
+ CharPair AltChars;
40
+ };
41
+ }
42
+
43
+
44
+
45
+ // Table of enhanced key values:
46
+ static EnhKeyVals const EnhancedKeys[] =
47
+ {
48
+ { 28, { 13, 0 }, { 13, 0 }, { 10, 0 }, { 0, 166 } },
49
+ { 53, { 47, 0 }, { 63, 0 }, { 0, 149 }, { 0, 164 } },
50
+ { 71, { 224, 71 }, { 224, 71 }, { 224, 119 }, { 0, 151 } },
51
+ { 72, { 224, 72 }, { 224, 72 }, { 224, 141 }, { 0, 152 } },
52
+ { 73, { 224, 73 }, { 224, 73 }, { 224, 134 }, { 0, 153 } },
53
+ { 75, { 224, 75 }, { 224, 75 }, { 224, 115 }, { 0, 155 } },
54
+ { 77, { 224, 77 }, { 224, 77 }, { 224, 116 }, { 0, 157 } },
55
+ { 79, { 224, 79 }, { 224, 79 }, { 224, 117 }, { 0, 159 } },
56
+ { 80, { 224, 80 }, { 224, 80 }, { 224, 145 }, { 0, 160 } },
57
+ { 81, { 224, 81 }, { 224, 81 }, { 224, 118 }, { 0, 161 } },
58
+ { 82, { 224, 82 }, { 224, 82 }, { 224, 146 }, { 0, 162 } },
59
+ { 83, { 224, 83 }, { 224, 83 }, { 224, 147 }, { 0, 163 } }
60
+ };
61
+
62
+ // The number of elements in EnhancedKeys:
63
+ #define NUM_EKA_ELTS (sizeof(EnhancedKeys) / sizeof(EnhKeyVals))
64
+
65
+
66
+
67
+ // Table of key values for normal keys. Note that the table is padded so that
68
+ // the key scan code serves as an index into the table.
69
+ static NormKeyVals const NormalKeys[] =
70
+ {
71
+ /* padding */
72
+ { /* 0 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
73
+
74
+ { /* 1 */ { 27, 0 }, { 27, 0 }, { 27, 0 }, { 0, 1 } },
75
+ { /* 2 */ { 49, 0 }, { 33, 0 }, { 0, 0 }, { 0, 120 } },
76
+ { /* 3 */ { 50, 0 }, { 64, 0 }, { 0, 3 }, { 0, 121 } },
77
+ { /* 4 */ { 51, 0 }, { 35, 0 }, { 0, 0 }, { 0, 122 } },
78
+ { /* 5 */ { 52, 0 }, { 36, 0 }, { 0, 0 }, { 0, 123 } },
79
+ { /* 6 */ { 53, 0 }, { 37, 0 }, { 0, 0 }, { 0, 124 } },
80
+ { /* 7 */ { 54, 0 }, { 94, 0 }, { 30, 0 }, { 0, 125 } },
81
+ { /* 8 */ { 55, 0 }, { 38, 0 }, { 0, 0 }, { 0, 126 } },
82
+ { /* 9 */ { 56, 0 }, { 42, 0 }, { 0, 0 }, { 0, 127 } },
83
+ { /* 10 */ { 57, 0 }, { 40, 0 }, { 0, 0 }, { 0, 128 } },
84
+ { /* 11 */ { 48, 0 }, { 41, 0 }, { 0, 0 }, { 0, 129 } },
85
+ { /* 12 */ { 45, 0 }, { 95, 0 }, { 31, 0 }, { 0, 130 } },
86
+ { /* 13 */ { 61, 0 }, { 43, 0 }, { 0, 0 }, { 0, 131 } },
87
+ { /* 14 */ { 8, 0 }, { 8, 0 }, { 127, 0 }, { 0, 14 } },
88
+ { /* 15 */ { 9, 0 }, { 0, 15 }, { 0, 148 }, { 0, 15 } },
89
+ { /* 16 */ { 113, 0 }, { 81, 0 }, { 17, 0 }, { 0, 16 } },
90
+ { /* 17 */ { 119, 0 }, { 87, 0 }, { 23, 0 }, { 0, 17 } },
91
+ { /* 18 */ { 101, 0 }, { 69, 0 }, { 5, 0 }, { 0, 18 } },
92
+ { /* 19 */ { 114, 0 }, { 82, 0 }, { 18, 0 }, { 0, 19 } },
93
+ { /* 20 */ { 116, 0 }, { 84, 0 }, { 20, 0 }, { 0, 20 } },
94
+ { /* 21 */ { 121, 0 }, { 89, 0 }, { 25, 0 }, { 0, 21 } },
95
+ { /* 22 */ { 117, 0 }, { 85, 0 }, { 21, 0 }, { 0, 22 } },
96
+ { /* 23 */ { 105, 0 }, { 73, 0 }, { 9, 0 }, { 0, 23 } },
97
+ { /* 24 */ { 111, 0 }, { 79, 0 }, { 15, 0 }, { 0, 24 } },
98
+ { /* 25 */ { 112, 0 }, { 80, 0 }, { 16, 0 }, { 0, 25 } },
99
+ { /* 26 */ { 91, 0 }, { 123, 0 }, { 27, 0 }, { 0, 26 } },
100
+ { /* 27 */ { 93, 0 }, { 125, 0 }, { 29, 0 }, { 0, 27 } },
101
+ { /* 28 */ { 13, 0 }, { 13, 0 }, { 10, 0 }, { 0, 28 } },
102
+
103
+ /* padding */
104
+ { /* 29 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
105
+
106
+ { /* 30 */ { 97, 0 }, { 65, 0 }, { 1, 0 }, { 0, 30 } },
107
+ { /* 31 */ { 115, 0 }, { 83, 0 }, { 19, 0 }, { 0, 31 } },
108
+ { /* 32 */ { 100, 0 }, { 68, 0 }, { 4, 0 }, { 0, 32 } },
109
+ { /* 33 */ { 102, 0 }, { 70, 0 }, { 6, 0 }, { 0, 33 } },
110
+ { /* 34 */ { 103, 0 }, { 71, 0 }, { 7, 0 }, { 0, 34 } },
111
+ { /* 35 */ { 104, 0 }, { 72, 0 }, { 8, 0 }, { 0, 35 } },
112
+ { /* 36 */ { 106, 0 }, { 74, 0 }, { 10, 0 }, { 0, 36 } },
113
+ { /* 37 */ { 107, 0 }, { 75, 0 }, { 11, 0 }, { 0, 37 } },
114
+ { /* 38 */ { 108, 0 }, { 76, 0 }, { 12, 0 }, { 0, 38 } },
115
+ { /* 39 */ { 59, 0 }, { 58, 0 }, { 0, 0 }, { 0, 39 } },
116
+ { /* 40 */ { 39, 0 }, { 34, 0 }, { 0, 0 }, { 0, 40 } },
117
+ { /* 41 */ { 96, 0 }, { 126, 0 }, { 0, 0 }, { 0, 41 } },
118
+
119
+ /* padding */
120
+ { /* 42 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
121
+
122
+ { /* 43 */ { 92, 0 }, { 124, 0 }, { 28, 0 }, { 0, 0 } },
123
+ { /* 44 */ { 122, 0 }, { 90, 0 }, { 26, 0 }, { 0, 44 } },
124
+ { /* 45 */ { 120, 0 }, { 88, 0 }, { 24, 0 }, { 0, 45 } },
125
+ { /* 46 */ { 99, 0 }, { 67, 0 }, { 3, 0 }, { 0, 46 } },
126
+ { /* 47 */ { 118, 0 }, { 86, 0 }, { 22, 0 }, { 0, 47 } },
127
+ { /* 48 */ { 98, 0 }, { 66, 0 }, { 2, 0 }, { 0, 48 } },
128
+ { /* 49 */ { 110, 0 }, { 78, 0 }, { 14, 0 }, { 0, 49 } },
129
+ { /* 50 */ { 109, 0 }, { 77, 0 }, { 13, 0 }, { 0, 50 } },
130
+ { /* 51 */ { 44, 0 }, { 60, 0 }, { 0, 0 }, { 0, 51 } },
131
+ { /* 52 */ { 46, 0 }, { 62, 0 }, { 0, 0 }, { 0, 52 } },
132
+ { /* 53 */ { 47, 0 }, { 63, 0 }, { 0, 0 }, { 0, 53 } },
133
+
134
+ /* padding */
135
+ { /* 54 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
136
+
137
+ { /* 55 */ { 42, 0 }, { 0, 0 }, { 114, 0 }, { 0, 0 } },
138
+
139
+ /* padding */
140
+ { /* 56 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
141
+
142
+ { /* 57 */ { 32, 0 }, { 32, 0 }, { 32, 0 }, { 32, 0 } },
143
+
144
+ /* padding */
145
+ { /* 58 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
146
+
147
+ { /* 59 */ { 0, 59 }, { 0, 84 }, { 0, 94 }, { 0, 104 } },
148
+ { /* 60 */ { 0, 60 }, { 0, 85 }, { 0, 95 }, { 0, 105 } },
149
+ { /* 61 */ { 0, 61 }, { 0, 86 }, { 0, 96 }, { 0, 106 } },
150
+ { /* 62 */ { 0, 62 }, { 0, 87 }, { 0, 97 }, { 0, 107 } },
151
+ { /* 63 */ { 0, 63 }, { 0, 88 }, { 0, 98 }, { 0, 108 } },
152
+ { /* 64 */ { 0, 64 }, { 0, 89 }, { 0, 99 }, { 0, 109 } },
153
+ { /* 65 */ { 0, 65 }, { 0, 90 }, { 0, 100 }, { 0, 110 } },
154
+ { /* 66 */ { 0, 66 }, { 0, 91 }, { 0, 101 }, { 0, 111 } },
155
+ { /* 67 */ { 0, 67 }, { 0, 92 }, { 0, 102 }, { 0, 112 } },
156
+ { /* 68 */ { 0, 68 }, { 0, 93 }, { 0, 103 }, { 0, 113 } },
157
+
158
+ /* padding */
159
+ { /* 69 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
160
+ { /* 70 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
161
+
162
+ { /* 71 */ { 0, 71 }, { 55, 0 }, { 0, 119 }, { 0, 0 } },
163
+ { /* 72 */ { 0, 72 }, { 56, 0 }, { 0, 141 }, { 0, 0 } },
164
+ { /* 73 */ { 0, 73 }, { 57, 0 }, { 0, 132 }, { 0, 0 } },
165
+ { /* 74 */ { 0, 0 }, { 45, 0 }, { 0, 0 }, { 0, 0 } },
166
+ { /* 75 */ { 0, 75 }, { 52, 0 }, { 0, 115 }, { 0, 0 } },
167
+ { /* 76 */ { 0, 0 }, { 53, 0 }, { 0, 0 }, { 0, 0 } },
168
+ { /* 77 */ { 0, 77 }, { 54, 0 }, { 0, 116 }, { 0, 0 } },
169
+ { /* 78 */ { 0, 0 }, { 43, 0 }, { 0, 0 }, { 0, 0 } },
170
+ { /* 79 */ { 0, 79 }, { 49, 0 }, { 0, 117 }, { 0, 0 } },
171
+ { /* 80 */ { 0, 80 }, { 50, 0 }, { 0, 145 }, { 0, 0 } },
172
+ { /* 81 */ { 0, 81 }, { 51, 0 }, { 0, 118 }, { 0, 0 } },
173
+ { /* 82 */ { 0, 82 }, { 48, 0 }, { 0, 146 }, { 0, 0 } },
174
+ { /* 83 */ { 0, 83 }, { 46, 0 }, { 0, 147 }, { 0, 0 } },
175
+
176
+ /* padding */
177
+ { /* 84 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
178
+ { /* 85 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
179
+ { /* 86 */ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
180
+
181
+ { /* 87 */ { 224, 133 }, { 224, 135 }, { 224, 137 }, { 224, 139 } },
182
+ { /* 88 */ { 224, 134 }, { 224, 136 }, { 224, 138 }, { 224, 140 } }
183
+ };
184
+
185
+ // The primary purpose of the pushback buffer is so that if a
186
+ // multi-byte character or extended key code is read, we can return
187
+ // the first byte and store the rest of the data in the buffer for
188
+ // subsequent calls. UTF-8 characters can be up to 4 bytes long, so
189
+ // the pushback buffer must be able to store the 3 remaining bytes.
190
+ size_t const getch_pushback_buffer_capacity = 3;
191
+
192
+ static int getch_pushback_buffer[getch_pushback_buffer_capacity];
193
+ static int getch_pushback_buffer_index = 0;
194
+ static int getch_pushback_buffer_current_size = 0;
195
+
196
+ static bool is_getch_pushback_buffer_full()
197
+ {
198
+ return getch_pushback_buffer_current_size >= getch_pushback_buffer_capacity;
199
+ }
200
+
201
+ static void add_to_getch_pushback_buffer(int const c)
202
+ {
203
+ _ASSERTE(!is_getch_pushback_buffer_full());
204
+ getch_pushback_buffer[getch_pushback_buffer_current_size++] = c;
205
+ }
206
+
207
+ static int peek_next_getch_pushback_buffer()
208
+ {
209
+ if (getch_pushback_buffer_current_size == 0) {
210
+ return EOF;
211
+ }
212
+
213
+ return getch_pushback_buffer[getch_pushback_buffer_index];
214
+ }
215
+
216
+ static int get_next_getch_pushback_buffer()
217
+ {
218
+ if (getch_pushback_buffer_current_size == 0) {
219
+ return EOF;
220
+ }
221
+
222
+ int const ret_val = getch_pushback_buffer[getch_pushback_buffer_index++];
223
+
224
+ if (getch_pushback_buffer_index == getch_pushback_buffer_current_size) {
225
+ getch_pushback_buffer_index = 0;
226
+ getch_pushback_buffer_current_size = 0;
227
+ }
228
+
229
+ return ret_val;
230
+ }
231
+
232
+ extern "C" extern intptr_t __dcrt_lowio_console_input_handle;
233
+
234
+ extern "C" CharPair const* __cdecl _getextendedkeycode(KEY_EVENT_RECORD*);
235
+ extern "C" int __cdecl _kbhit_nolock();
236
+
237
+
238
+
239
+ // These functions read a single character from the console. _getch() does not
240
+ // echo the character; _getche() does echo the character. If the push-back
241
+ // buffer is nonempty, the buffered character is returned immediately, without
242
+ // being echoed.
243
+ //
244
+ // On success, the read character is returned; on failure, EOF is returned.
245
+ extern "C" int __cdecl _getch()
246
+ {
247
+ __acrt_lock(__acrt_conio_lock);
248
+ int result = 0;
249
+ __try
250
+ {
251
+ result = _getch_nolock();
252
+ }
253
+ __finally
254
+ {
255
+ __acrt_unlock(__acrt_conio_lock);
256
+ }
257
+ return result;
258
+ }
259
+
260
+ extern "C" int __cdecl _getche()
261
+ {
262
+ __acrt_lock(__acrt_conio_lock);
263
+ int result = 0;
264
+ __try
265
+ {
266
+ result = _getche_nolock();
267
+ }
268
+ __finally
269
+ {
270
+ __acrt_unlock(__acrt_conio_lock);
271
+ }
272
+ return result;
273
+ }
274
+
275
+ extern "C" int __cdecl _getch_nolock()
276
+ {
277
+ // Check the pushback buffer for a character. If one is present, return it:
278
+ int const pushback = get_next_getch_pushback_buffer();
279
+ if (pushback != EOF)
280
+ {
281
+ return pushback;
282
+ }
283
+
284
+ if (__dcrt_lowio_ensure_console_input_initialized() == FALSE) {
285
+ return EOF;
286
+ }
287
+
288
+ // Switch console to raw mode:
289
+ DWORD old_console_mode;
290
+ __dcrt_get_input_console_mode(&old_console_mode);
291
+ __dcrt_set_input_console_mode(0);
292
+
293
+ int result = 0;
294
+
295
+ __try
296
+ {
297
+ for ( ; ; )
298
+ {
299
+ // Get a console input event:
300
+ INPUT_RECORD input_record;
301
+ DWORD num_read;
302
+
303
+ if (__dcrt_read_console_input(&input_record, 1, &num_read) == FALSE || num_read == 0)
304
+ {
305
+ result = EOF;
306
+ __leave;
307
+ }
308
+
309
+ // Look for, and decipher, key events.
310
+ if (input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown)
311
+ {
312
+ // Simple case: if UnicodeChar is non-zero, we can convert it to char and return it.
313
+ wchar_t const c = input_record.Event.KeyEvent.uChar.UnicodeChar;
314
+ if (c != 0)
315
+ {
316
+ wchar_t const c_buffer[2] = {c, L'\0'};
317
+ char mb_chars[4];
318
+
319
+ size_t const amount_written = __acrt_wcs_to_mbs_cp_array(
320
+ c_buffer,
321
+ mb_chars,
322
+ GetConsoleCP()
323
+ );
324
+
325
+ // Mask with 0xFF to just get lowest byte
326
+ if (amount_written >= 1) {
327
+ result = mb_chars[0] & 0xFF;
328
+ }
329
+
330
+ if (amount_written >= 2) {
331
+ for (size_t i = 1; i < amount_written; ++i) {
332
+ add_to_getch_pushback_buffer(mb_chars[i] & 0xFF);
333
+ }
334
+ }
335
+ __leave;
336
+ }
337
+
338
+ // Hard case: either it is an extended code or an event which
339
+ // should not be recognized. Let _getextendedkeycode do the work:
340
+ CharPair const* const cp = _getextendedkeycode(&input_record.Event.KeyEvent);
341
+ if (cp != nullptr)
342
+ {
343
+ // Mask with 0xFF to just get lowest byte
344
+ add_to_getch_pushback_buffer(cp->SecondChar & 0xFF);
345
+ result = cp->LeadChar & 0xFF;
346
+ __leave;
347
+ }
348
+ }
349
+ }
350
+ }
351
+ __finally
352
+ {
353
+ // Restore the previous console mode:
354
+ __dcrt_set_input_console_mode(old_console_mode);
355
+ }
356
+ return result;
357
+ }
358
+
359
+
360
+
361
+ extern "C" int __cdecl _getche_nolock()
362
+ {
363
+ // Check the pushback buffer for a character. If one is present, return
364
+ // it without echoing:
365
+ int const pushback = get_next_getch_pushback_buffer();
366
+ if (pushback != EOF)
367
+ {
368
+ return pushback;
369
+ }
370
+
371
+ // Otherwise, read the next character from the console and echo it:
372
+ int const c = _getch_nolock();
373
+ if (c == EOF)
374
+ return EOF;
375
+
376
+ if (_putch_nolock(c) == EOF)
377
+ return EOF;
378
+
379
+ return c;
380
+ }
381
+
382
+
383
+
384
+ // Returns nonzero if a keystroke is waiting to be read; otherwise returns zero.
385
+ extern "C" int __cdecl _kbhit()
386
+ {
387
+ __acrt_lock(__acrt_conio_lock);
388
+ int result = 0;
389
+ __try
390
+ {
391
+ result = _kbhit_nolock();
392
+ }
393
+ __finally
394
+ {
395
+ __acrt_unlock(__acrt_conio_lock);
396
+ }
397
+ return result;
398
+ }
399
+
400
+
401
+
402
+ extern "C" int __cdecl _kbhit_nolock()
403
+ {
404
+ // If a character has been pushed back, return TRUE:
405
+ if (peek_next_getch_pushback_buffer() != EOF) {
406
+ return TRUE;
407
+ }
408
+
409
+ if (__dcrt_lowio_ensure_console_input_initialized() == FALSE) {
410
+ return FALSE;
411
+ }
412
+
413
+ // Peek at all pending console events:
414
+ DWORD num_pending;
415
+ if (__dcrt_get_number_of_console_input_events(&num_pending) == FALSE) {
416
+ return FALSE;
417
+ }
418
+
419
+ if (num_pending == 0) {
420
+ return FALSE;
421
+ }
422
+
423
+ __crt_scoped_stack_ptr<INPUT_RECORD> const input_buffer(_malloca_crt_t(INPUT_RECORD, num_pending));
424
+ if (input_buffer.get() == nullptr) {
425
+ return FALSE;
426
+ }
427
+
428
+ DWORD num_peeked;
429
+ // AsciiChar is not read, so using the narrow Win32 API is permitted.
430
+ if (__dcrt_peek_console_input_a(input_buffer.get(), num_pending, &num_peeked) == FALSE) {
431
+ return FALSE;
432
+ }
433
+
434
+ if (num_peeked == 0 || num_peeked > num_pending) {
435
+ return FALSE;
436
+ }
437
+
438
+ // Scan all of the peeked events to determine if any is a key event
439
+ // that should be recognized:
440
+ for (INPUT_RECORD* p = input_buffer.get(); num_peeked > 0; --num_peeked, ++p)
441
+ {
442
+ if (p->EventType != KEY_EVENT)
443
+ continue;
444
+
445
+ if (!p->Event.KeyEvent.bKeyDown)
446
+ continue;
447
+
448
+ if (p->Event.KeyEvent.uChar.AsciiChar == 0 &&
449
+ _getextendedkeycode(&p->Event.KeyEvent) == nullptr)
450
+ continue;
451
+
452
+ return TRUE;
453
+ }
454
+
455
+ return FALSE;
456
+ }
457
+
458
+
459
+
460
+ // Pushes back ("ungets") one character to be read next by _getwch() or
461
+ // _getwche(). On success, returns the character that was pushed back; on
462
+ // failure, returns EOF.
463
+ extern "C" int __cdecl _ungetch(int const c)
464
+ {
465
+ __acrt_lock(__acrt_conio_lock);
466
+ int result = 0;
467
+ __try
468
+ {
469
+ result = _ungetch_nolock(c);
470
+ }
471
+ __finally
472
+ {
473
+ __acrt_unlock(__acrt_conio_lock);
474
+ }
475
+ return result;
476
+ }
477
+
478
+
479
+
480
+ extern "C" int __cdecl _ungetch_nolock(int const c)
481
+ {
482
+ // Fail if the character is EOF or the pusback buffer is nonempty:
483
+ if (c == EOF || is_getch_pushback_buffer_full()) {
484
+ return EOF;
485
+ }
486
+
487
+ add_to_getch_pushback_buffer(c);
488
+ return c;
489
+ }
490
+
491
+
492
+
493
+ // Returns the extended code (if there is one) for a key event. This is the
494
+ // core function for the _getch() and _getche() functions and their wide
495
+ // character equivalents, and is essential to _kbhit(). This is the function
496
+ // that determines whether or not a key event NOT accompanied by an ASCII
497
+ // character has an extended code and returns that code.
498
+ //
499
+ // On success, a pointer to a CharPair value holding the lead and second
500
+ // characters of the extended code is returned. On failure, nullptr is returned.
501
+ extern "C" CharPair const* __cdecl _getextendedkeycode(KEY_EVENT_RECORD* const pKE)
502
+ {
503
+ DWORD const CKS = pKE->dwControlKeyState;
504
+
505
+ if (CKS & ENHANCED_KEY)
506
+ {
507
+ // Find the appropriate entry in EnhancedKeys[]:
508
+ for (int i = 0 ; i < NUM_EKA_ELTS; ++i)
509
+ {
510
+ if (EnhancedKeys[i].ScanCode != pKE->wVirtualScanCode)
511
+ {
512
+ continue;
513
+ }
514
+
515
+ // We found a match! Determine which pair to return:
516
+ if (CKS & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
517
+ {
518
+ return &EnhancedKeys[i].AltChars;
519
+ }
520
+ else if (CKS & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
521
+ {
522
+ return &EnhancedKeys[i].CtrlChars;
523
+ }
524
+ else if (CKS & SHIFT_PRESSED)
525
+ {
526
+ return &EnhancedKeys[i].ShiftChars;
527
+ }
528
+ else
529
+ {
530
+ return &EnhancedKeys[i].RegChars;
531
+ }
532
+ }
533
+
534
+ return nullptr;
535
+ }
536
+ else
537
+ {
538
+ // Regular key or keyboard event which shouldn't be recognized.
539
+ // Determine which by getting the proper field of the proper entry in
540
+ // NormalKeys[] and examining the extended code.
541
+ CharPair const* pCP;
542
+
543
+ if (CKS & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
544
+ {
545
+ pCP = &NormalKeys[pKE->wVirtualScanCode].AltChars;
546
+ }
547
+ else if (CKS & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
548
+ {
549
+ pCP = &NormalKeys[pKE->wVirtualScanCode].CtrlChars;
550
+ }
551
+ else if (CKS & SHIFT_PRESSED)
552
+ {
553
+ pCP = &NormalKeys[pKE->wVirtualScanCode].ShiftChars;
554
+ }
555
+ else
556
+ {
557
+ pCP = &NormalKeys[pKE->wVirtualScanCode].RegChars;
558
+ }
559
+
560
+ // Make sure it wasn't a keyboard event which should not be recognized
561
+ // (e.g. the shift key was pressed):
562
+ if ((pCP->LeadChar != 0 && pCP->LeadChar != 224) || pCP->SecondChar == 0)
563
+ {
564
+ return nullptr;
565
+ }
566
+
567
+ return pCP;
568
+ }
569
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/getwch.cpp ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // getwch.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _getwch(), _getwche(), and _ungetwch(), which get and unget
7
+ // characters directly from the console.
8
+ //
9
+ #include <conio.h>
10
+ #include <corecrt_internal_lowio.h>
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+ #include <wchar.h>
15
+
16
+
17
+
18
+ namespace
19
+ {
20
+ struct CharPair
21
+ {
22
+ unsigned char LeadChar;
23
+ unsigned char SecondChar;
24
+ };
25
+ }
26
+
27
+
28
+ static wint_t wchbuf = WEOF; // The push-back buffer
29
+
30
+ extern "C" extern intptr_t __dcrt_lowio_console_input_handle;
31
+
32
+ extern "C" CharPair const* __cdecl _getextendedkeycode(KEY_EVENT_RECORD*);
33
+
34
+
35
+
36
+ // Reads one character directly from the console. The _getwche() form also
37
+ // echoes the character back to the console. If the push-back buffer is
38
+ // nonempty, then its value is returned and the push-back buffer is marked as
39
+ // empty.
40
+ //
41
+ // Returns the character that is read on success; returns WEOF on failure.
42
+ extern "C" wint_t __cdecl _getwch()
43
+ {
44
+ __acrt_lock(__acrt_conio_lock);
45
+ wint_t result = 0;
46
+ __try
47
+ {
48
+ result = _getwch_nolock();
49
+ }
50
+ __finally
51
+ {
52
+ __acrt_unlock(__acrt_conio_lock);
53
+ }
54
+ return result;
55
+ }
56
+
57
+
58
+ extern "C" wint_t __cdecl _getwche()
59
+ {
60
+ __acrt_lock(__acrt_conio_lock);
61
+ wint_t result = 0;
62
+ __try
63
+ {
64
+ result = _getwche_nolock();
65
+ }
66
+ __finally
67
+ {
68
+ __acrt_unlock(__acrt_conio_lock);
69
+ }
70
+ return result;
71
+ }
72
+
73
+
74
+
75
+ extern "C" wint_t __cdecl _getwch_nolock()
76
+ {
77
+ // First check the pushback buffer for a character. If it has one, return
78
+ // it without echoing and reset the buffer:
79
+ if (wchbuf != WEOF)
80
+ {
81
+ wchar_t const buffered_wchar = static_cast<wchar_t>(wchbuf & 0xFFFF);
82
+ wchbuf = WEOF;
83
+ return buffered_wchar;
84
+ }
85
+
86
+ // The console input handle is created the first time that _getwch(),
87
+ // _cgetws(), or _kbhit() is called:
88
+ if (__dcrt_lowio_ensure_console_input_initialized() == FALSE)
89
+ return WEOF;
90
+
91
+ // Switch to raw mode (no line input, no echo input):
92
+ DWORD old_console_mode;
93
+ __dcrt_get_input_console_mode(&old_console_mode);
94
+ __dcrt_set_input_console_mode(0);
95
+ wint_t result = 0;
96
+ __try
97
+ {
98
+ for ( ; ; )
99
+ {
100
+ // Get a console input event:
101
+ INPUT_RECORD input_record;
102
+ DWORD num_read;
103
+ if (__dcrt_read_console_input(&input_record, 1, &num_read) == FALSE)
104
+ {
105
+ result = WEOF;
106
+ __leave;
107
+ }
108
+
109
+ if (num_read == 0)
110
+ {
111
+ result = WEOF;
112
+ __leave;
113
+ }
114
+
115
+ // Look for, and decipher, key events.
116
+ if (input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown)
117
+ {
118
+ // Easy case: if UnicodeChar is non-zero, we can just return it:
119
+ wchar_t const c = static_cast<wchar_t>(input_record.Event.KeyEvent.uChar.UnicodeChar);
120
+ if (c != 0)
121
+ {
122
+ result = c;
123
+ __leave;
124
+ }
125
+
126
+ // Hard case: either it is an extended code or an event which
127
+ // should not be recognized. Let _getextendedkeycode do the work:
128
+ CharPair const* const cp = _getextendedkeycode(&input_record.Event.KeyEvent);
129
+ if (cp != nullptr)
130
+ {
131
+ wchbuf = cp->SecondChar;
132
+ result = cp->LeadChar;
133
+ __leave;
134
+ }
135
+ }
136
+ }
137
+ }
138
+ __finally
139
+ {
140
+ // Restore the previous console mode:
141
+ __dcrt_set_input_console_mode(old_console_mode);
142
+ }
143
+ return result;
144
+ }
145
+
146
+
147
+
148
+ extern "C" wint_t __cdecl _getwche_nolock()
149
+ {
150
+ // First check the pushback buffer for a character. If it has one, return
151
+ // it without echoing and reset the buffer:
152
+ if (wchbuf != WEOF)
153
+ {
154
+ wchar_t const buffered_wchar = static_cast<wchar_t>(wchbuf & 0xFFFF);
155
+ wchbuf = WEOF;
156
+ return buffered_wchar;
157
+ }
158
+
159
+ // Othwrwise, read the character, echo it, and return it. If anything fails,
160
+ // we immediately return WEOF.
161
+ wchar_t const gotten_wchar = _getwch_nolock();
162
+ if (gotten_wchar == WEOF)
163
+ return WEOF;
164
+
165
+ if (_putwch_nolock(gotten_wchar) == WEOF)
166
+ return WEOF;
167
+
168
+ return gotten_wchar;
169
+ }
170
+
171
+
172
+
173
+ // Pushes back ("ungets") one character to be read next by _getwch() or
174
+ // _getwche(). On success, returns the character that was pushed back; on
175
+ // failure, returns EOF.
176
+ extern "C" wint_t __cdecl _ungetwch(wint_t const c)
177
+ {
178
+ __acrt_lock(__acrt_conio_lock);
179
+ wint_t result = 0;
180
+ __try
181
+ {
182
+ result = _ungetwch_nolock(c);
183
+ }
184
+ __finally
185
+ {
186
+ __acrt_unlock(__acrt_conio_lock);
187
+ }
188
+ return result;
189
+ }
190
+
191
+
192
+
193
+ extern "C" wint_t __cdecl _ungetwch_nolock(wint_t const c)
194
+ {
195
+ // Fail if the char is EOF or the pushback buffer is non-empty:
196
+ if (c == WEOF || wchbuf != WEOF)
197
+ return static_cast<wint_t>(EOF);
198
+
199
+ wchbuf = (c & 0xFF);
200
+ return wchbuf;
201
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/initcon.cpp ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // initcon.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the global console output handle and the __dcrt_lowio_initialize_console_output() and
7
+ // __dcrt_terminate_console_output() functions, which initialize and close that handle.
8
+ // Also defines functions that can be used instead of the Console API to enable retry behavior
9
+ // in case the cached console handle is freed.
10
+
11
+ #include <corecrt_internal_lowio.h>
12
+
13
+ _CRT_LINKER_FORCE_INCLUDE(__dcrt_console_output_terminator);
14
+
15
+ // The global console output handle.
16
+ static HANDLE __dcrt_lowio_console_output_handle = _console_uninitialized_handle;
17
+
18
+ // Initializes the global console output handle
19
+ static void __dcrt_lowio_initialize_console_output()
20
+ {
21
+ __dcrt_lowio_console_output_handle = CreateFileW(
22
+ L"CONOUT$",
23
+ GENERIC_WRITE,
24
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
25
+ nullptr,
26
+ OPEN_EXISTING,
27
+ 0,
28
+ nullptr);
29
+ }
30
+
31
+ extern "C" BOOL __cdecl __dcrt_lowio_ensure_console_output_initialized()
32
+ {
33
+ if (__dcrt_lowio_console_output_handle == _console_uninitialized_handle)
34
+ {
35
+ __dcrt_lowio_initialize_console_output();
36
+ }
37
+
38
+ if (__dcrt_lowio_console_output_handle == _console_invalid_handle)
39
+ {
40
+ return FALSE;
41
+ }
42
+ return TRUE;
43
+ }
44
+
45
+ // Closes the global console output handle
46
+ extern "C" void __cdecl __dcrt_terminate_console_output()
47
+ {
48
+ if ( __dcrt_lowio_console_output_handle != _console_invalid_handle
49
+ && __dcrt_lowio_console_output_handle != _console_uninitialized_handle)
50
+ {
51
+ CloseHandle(__dcrt_lowio_console_output_handle);
52
+ }
53
+ }
54
+
55
+ template <typename Func>
56
+ static BOOL console_output_reopen_and_retry(Func const& fp) throw()
57
+ {
58
+ BOOL result = fp();
59
+ if (!result && GetLastError() == ERROR_INVALID_HANDLE) {
60
+ __dcrt_terminate_console_output();
61
+ __dcrt_lowio_initialize_console_output();
62
+ result = fp();
63
+ }
64
+ return result;
65
+ }
66
+
67
+ extern "C" BOOL __cdecl __dcrt_write_console(
68
+ _In_ void const * lpBuffer,
69
+ _In_ DWORD nNumberOfCharsToWrite,
70
+ _Out_ LPDWORD lpNumberOfCharsWritten
71
+ )
72
+ {
73
+ return console_output_reopen_and_retry(
74
+ [lpBuffer, nNumberOfCharsToWrite, lpNumberOfCharsWritten]()
75
+ {
76
+ return ::WriteConsoleW(
77
+ __dcrt_lowio_console_output_handle,
78
+ lpBuffer,
79
+ nNumberOfCharsToWrite,
80
+ lpNumberOfCharsWritten,
81
+ nullptr);
82
+ });
83
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/initconin.cpp ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // initconin.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the global console input handle and the __dcrt_lowio_initialize_console_input() and
7
+ // __dcrt_terminate_console_input() functions, which initialize and close that handle.
8
+ // Also defines functions that can be used instead of the Console API to enable retry behavior
9
+ // in case the cached console handle is freed.
10
+
11
+ #include <corecrt_internal_lowio.h>
12
+
13
+ _CRT_LINKER_FORCE_INCLUDE(__dcrt_console_input_terminator);
14
+
15
+ // The global console input handle.
16
+ static HANDLE __dcrt_lowio_console_input_handle = _console_uninitialized_handle;
17
+
18
+ // Initializes the global console input handle
19
+ static void __dcrt_lowio_initialize_console_input()
20
+ {
21
+ __dcrt_lowio_console_input_handle = CreateFileW(
22
+ L"CONIN$",
23
+ GENERIC_READ | GENERIC_WRITE,
24
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
25
+ nullptr,
26
+ OPEN_EXISTING,
27
+ 0,
28
+ nullptr);
29
+ }
30
+
31
+ extern "C" BOOL __cdecl __dcrt_lowio_ensure_console_input_initialized()
32
+ {
33
+ if (__dcrt_lowio_console_input_handle == _console_uninitialized_handle)
34
+ {
35
+ __dcrt_lowio_initialize_console_input();
36
+ }
37
+
38
+ if (__dcrt_lowio_console_input_handle == _console_invalid_handle)
39
+ {
40
+ return FALSE;
41
+ }
42
+ return TRUE;
43
+ }
44
+
45
+ // Closes the global console input handle
46
+ extern "C" void __cdecl __dcrt_terminate_console_input()
47
+ {
48
+ if ( __dcrt_lowio_console_input_handle != _console_invalid_handle
49
+ && __dcrt_lowio_console_input_handle != _console_uninitialized_handle)
50
+ {
51
+ CloseHandle(__dcrt_lowio_console_input_handle);
52
+ }
53
+ }
54
+
55
+ template <typename Func>
56
+ static BOOL console_input_reopen_and_retry(Func const& fp) throw()
57
+ {
58
+ BOOL result = fp();
59
+ if (!result && GetLastError() == ERROR_INVALID_HANDLE) {
60
+ __dcrt_terminate_console_input();
61
+ __dcrt_lowio_initialize_console_input();
62
+ result = fp();
63
+ }
64
+ return result;
65
+ }
66
+
67
+ extern "C" BOOL __cdecl __dcrt_read_console_input(
68
+ _Out_ PINPUT_RECORD lpBuffer,
69
+ _In_ DWORD nLength,
70
+ _Out_ LPDWORD lpNumberOfEventsRead
71
+ )
72
+ {
73
+ return console_input_reopen_and_retry(
74
+ [lpBuffer, nLength, lpNumberOfEventsRead]()
75
+ {
76
+ return ::ReadConsoleInputW(
77
+ __dcrt_lowio_console_input_handle,
78
+ lpBuffer,
79
+ nLength,
80
+ lpNumberOfEventsRead
81
+ );
82
+ });
83
+ }
84
+
85
+ extern "C" BOOL __cdecl __dcrt_read_console(
86
+ _Out_ LPVOID lpBuffer,
87
+ _In_ DWORD nNumberOfCharsToRead,
88
+ _Out_ LPDWORD lpNumberOfCharsRead
89
+ )
90
+ {
91
+ return console_input_reopen_and_retry(
92
+ [lpBuffer, nNumberOfCharsToRead, lpNumberOfCharsRead]()
93
+ {
94
+ return ::ReadConsoleW(
95
+ __dcrt_lowio_console_input_handle,
96
+ lpBuffer,
97
+ nNumberOfCharsToRead,
98
+ lpNumberOfCharsRead,
99
+ nullptr
100
+ );
101
+ });
102
+ }
103
+
104
+ extern "C" BOOL __cdecl __dcrt_get_number_of_console_input_events(
105
+ _Out_ LPDWORD lpcNumberOfEvents
106
+ )
107
+ {
108
+ return console_input_reopen_and_retry(
109
+ [lpcNumberOfEvents]()
110
+ {
111
+ return ::GetNumberOfConsoleInputEvents(
112
+ __dcrt_lowio_console_input_handle,
113
+ lpcNumberOfEvents
114
+ );
115
+ });
116
+ }
117
+
118
+ extern "C" BOOL __cdecl __dcrt_peek_console_input_a(
119
+ _Out_ PINPUT_RECORD lpBuffer,
120
+ _In_ DWORD nLength,
121
+ _Out_ LPDWORD lpNumberOfEventsRead
122
+ )
123
+ {
124
+ return console_input_reopen_and_retry(
125
+ [lpBuffer, nLength, lpNumberOfEventsRead]()
126
+ {
127
+ return ::PeekConsoleInputA(
128
+ __dcrt_lowio_console_input_handle,
129
+ lpBuffer,
130
+ nLength,
131
+ lpNumberOfEventsRead
132
+ );
133
+ });
134
+ }
135
+
136
+ extern "C" BOOL __cdecl __dcrt_get_input_console_mode(
137
+ _Out_ LPDWORD lpMode
138
+ )
139
+ {
140
+ return console_input_reopen_and_retry(
141
+ [lpMode]()
142
+ {
143
+ return ::GetConsoleMode(
144
+ __dcrt_lowio_console_input_handle,
145
+ lpMode
146
+ );
147
+ });
148
+ }
149
+
150
+ extern "C" BOOL __cdecl __dcrt_set_input_console_mode(
151
+ _In_ DWORD dwMode
152
+ )
153
+ {
154
+ return console_input_reopen_and_retry(
155
+ [dwMode]()
156
+ {
157
+ return ::SetConsoleMode(
158
+ __dcrt_lowio_console_input_handle,
159
+ dwMode
160
+ );
161
+ });
162
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/pipe.cpp ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // pipe.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _pipe(), which creates a pipe.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <stdlib.h>
10
+
11
+
12
+
13
+ // Creates a pipe. The phandles pointer must be a pointer to an array of two
14
+ // int objects. Upon successful return, phandles[0] is the read handle, and
15
+ // phandles[1] is the write handle for the pipe. On success, 0 is returned; on
16
+ // failure, -1 is returned and errno is set.
17
+ //
18
+ // The psize is the amount of memory, in bytes, to ask the OS to reserve for the
19
+ // pipe. The textmode is the text mode that will be given to the pipe.
20
+ extern "C" int __cdecl _pipe(int* const phandles, unsigned const psize, int const textmode)
21
+ {
22
+ _VALIDATE_CLEAR_OSSERR_RETURN(phandles != nullptr, EINVAL, -1);
23
+ phandles[0] = phandles[1] = -1;
24
+
25
+ _VALIDATE_CLEAR_OSSERR_RETURN((textmode & ~(_O_NOINHERIT | _O_BINARY | _O_TEXT)) == 0, EINVAL, -1);
26
+ _VALIDATE_CLEAR_OSSERR_RETURN((textmode & (_O_BINARY | _O_TEXT)) != (_O_BINARY | _O_TEXT), EINVAL, -1);
27
+
28
+
29
+ SECURITY_ATTRIBUTES security_attributes;
30
+ security_attributes.nLength = sizeof(security_attributes);
31
+ security_attributes.lpSecurityDescriptor = nullptr;
32
+ security_attributes.bInheritHandle = (textmode & _O_NOINHERIT) == 0;
33
+
34
+
35
+ // Create the pipe:
36
+ HANDLE read_handle, write_handle;
37
+ if (!CreatePipe(&read_handle, &write_handle, &security_attributes, psize))
38
+ {
39
+ __acrt_errno_map_os_error(GetLastError());
40
+ return -1;
41
+ }
42
+
43
+ // Create the CRT read handle for the pipe:
44
+ int const crt_read_handle = _alloc_osfhnd();
45
+ if (crt_read_handle == -1)
46
+ {
47
+ errno = EMFILE;
48
+ _doserrno = 0;
49
+ CloseHandle(read_handle);
50
+ CloseHandle(write_handle);
51
+ return -1;
52
+ }
53
+
54
+ __try
55
+ {
56
+ _osfile(crt_read_handle) = FOPEN | FPIPE | FTEXT;
57
+ _textmode(crt_read_handle) = __crt_lowio_text_mode::ansi;
58
+ _tm_unicode(crt_read_handle) = false;
59
+ }
60
+ __finally
61
+ {
62
+ __acrt_lowio_unlock_fh(crt_read_handle);
63
+ }
64
+
65
+ // Create the CRT write handle for the pipe:
66
+ int const crt_write_handle = _alloc_osfhnd();
67
+ if (crt_write_handle == -1)
68
+ {
69
+ _osfile(crt_read_handle) = 0;
70
+ errno = EMFILE;
71
+ _doserrno = 0;
72
+ CloseHandle(read_handle);
73
+ CloseHandle(write_handle);
74
+ return -1;
75
+ }
76
+
77
+ __try
78
+ {
79
+ _osfile(crt_write_handle) = FOPEN | FPIPE | FTEXT;
80
+ _textmode(crt_write_handle) = __crt_lowio_text_mode::ansi;
81
+ _tm_unicode(crt_write_handle) = false;
82
+ }
83
+ __finally
84
+ {
85
+ __acrt_lowio_unlock_fh(crt_write_handle);
86
+ }
87
+
88
+ // Figure out which textmode the file gets:
89
+ int fmode = 0;
90
+ _ERRCHECK(_get_fmode(&fmode));
91
+
92
+ // The pipe gets binary mode if (a) binary was requested or (b) the
93
+ // global default fmode was changed to binary.
94
+ if ((textmode & _O_BINARY) || ((textmode & _O_TEXT) == 0 && fmode == _O_BINARY))
95
+ {
96
+ _osfile(crt_read_handle) &= ~FTEXT;
97
+ _osfile(crt_write_handle) &= ~FTEXT;
98
+ }
99
+
100
+ if (textmode & _O_NOINHERIT)
101
+ {
102
+ _osfile(crt_read_handle) |= FNOINHERIT;
103
+ _osfile(crt_write_handle) |= FNOINHERIT;
104
+ }
105
+
106
+ __acrt_lowio_set_os_handle(crt_read_handle, reinterpret_cast<intptr_t>(read_handle));
107
+ __acrt_lowio_set_os_handle(crt_write_handle, reinterpret_cast<intptr_t>(write_handle));
108
+
109
+ phandles[0] = crt_read_handle;
110
+ phandles[1] = crt_write_handle;
111
+
112
+ return 0;
113
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/popen.cpp ADDED
@@ -0,0 +1,488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // popen.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // The _popen() and _pclose() functions, which open a pipe to a child process.
7
+ //
8
+ #include <corecrt_internal_stdio.h>
9
+ #include <process.h>
10
+
11
+
12
+
13
+ #define STDIN 0
14
+ #define STDOUT 1
15
+
16
+
17
+
18
+ namespace {
19
+
20
+ template <typename Character>
21
+ struct fdopen_mode
22
+ {
23
+ Character mode[3];
24
+ };
25
+
26
+ // This is the entry type for the stream pointer / process handle pairs that
27
+ // are stored for each outstanding popen.
28
+ struct process_handle_pair
29
+ {
30
+ FILE* stream;
31
+ intptr_t process_handle;
32
+ };
33
+
34
+ struct stream_traits
35
+ {
36
+ typedef FILE* type;
37
+
38
+ static bool close(_In_ type h) throw()
39
+ {
40
+ fclose(h);
41
+ return true;
42
+ }
43
+
44
+ static type get_invalid_value() throw()
45
+ {
46
+ return nullptr;
47
+ }
48
+ };
49
+
50
+ struct process_handle_pair_traits
51
+ {
52
+ typedef process_handle_pair* type;
53
+
54
+ static bool close(_In_ type h) throw()
55
+ {
56
+ h->process_handle = 0;
57
+ h->stream = nullptr;
58
+ return true;
59
+ }
60
+
61
+ static type get_invalid_value() throw()
62
+ {
63
+ return nullptr;
64
+ }
65
+ };
66
+
67
+ typedef __crt_unique_handle_t<stream_traits> unique_stream;
68
+ typedef __crt_unique_handle_t<process_handle_pair_traits> unique_process_handle_pair;
69
+ }
70
+
71
+
72
+
73
+ // The global table of stream pointer / process handle pairs. Access to this
74
+ // global tbale is only done via the idtab function. The table is expanded as
75
+ // necessary (by idtab), and free table entries are reused. (An entry is free
76
+ // if its stream is null.) The table is never contracted.
77
+ static unsigned __idtabsiz;
78
+ static process_handle_pair* __idpairs;
79
+
80
+
81
+
82
+ // Finds the entry for the given stream in the global table. If the stream is
83
+ // found, a pointer to it is returned; if the stream is not found, null is
84
+ // returned.
85
+ //
86
+ // If the stream is null, a new entry is allocated and a pointer to it is
87
+ // returned. If no entries are available and expansion of the table fails,
88
+ // null is returned.
89
+ //
90
+ // This function assumes the caller has acquired the lock on the table already.
91
+ static process_handle_pair* __cdecl idtab(FILE* const stream) throw()
92
+ {
93
+ // Search the table, and return the matching entry if one is found:
94
+ process_handle_pair* const first = __idpairs;
95
+ process_handle_pair* const last = first + __idtabsiz;
96
+ for (process_handle_pair* it = first; it != last; ++it)
97
+ {
98
+ if (it->stream == stream)
99
+ return it;
100
+ }
101
+
102
+ // We did not find an entry in the table. If the stream is null, then we
103
+ // try creating or expanding the table. Otherwise, we return null. Note
104
+ // that when the table is created or expanded, exactly one new entry is
105
+ // produced. This must not be changed unless code is added to mark the
106
+ // extra entries as being free (e.g., by setting their stream fields to null.
107
+ if (stream != nullptr)
108
+ return nullptr;
109
+
110
+ if (__idtabsiz + 1 < __idtabsiz)
111
+ return nullptr;
112
+
113
+ if (__idtabsiz + 1 >= SIZE_MAX / sizeof(process_handle_pair))
114
+ return nullptr;
115
+
116
+ process_handle_pair* const newptr = _recalloc_crt_t(process_handle_pair, __idpairs, __idtabsiz + 1).detach();
117
+ if (newptr == nullptr)
118
+ return nullptr;
119
+
120
+ __idpairs = newptr;
121
+ process_handle_pair* const pairptr = newptr + __idtabsiz;
122
+ ++__idtabsiz;
123
+
124
+ return pairptr;
125
+ }
126
+
127
+
128
+
129
+ template <typename Character>
130
+ static fdopen_mode<Character> __cdecl convert_popen_type_to_fdopen_mode(
131
+ Character const* const type
132
+ ) throw()
133
+ {
134
+ fdopen_mode<Character> result = fdopen_mode<Character>();
135
+
136
+ Character const* type_it = type;
137
+
138
+ while (*type_it == ' ')
139
+ ++type_it;
140
+
141
+ _VALIDATE_RETURN(*type_it == 'w' || *type_it == 'r', EINVAL, result);
142
+ result.mode[0] = *type_it++;
143
+
144
+ while (*type_it == ' ')
145
+ ++type_it;
146
+
147
+ _VALIDATE_RETURN(*type_it == '\0' || *type_it == 't' || *type_it == 'b', EINVAL, result);
148
+ result.mode[1] = *type_it;
149
+
150
+ return result;
151
+ }
152
+
153
+
154
+
155
+ template <typename Character>
156
+ static Character const* __cdecl get_comspec() throw()
157
+ {
158
+ typedef __acrt_stdio_char_traits<Character> stdio_traits;
159
+
160
+ static Character const comspec_name[] = { 'C', 'O', 'M', 'S', 'P', 'E', 'C', '\0' };
161
+
162
+ Character* comspec_value = nullptr;
163
+ if (_ERRCHECK_EINVAL(stdio_traits::tdupenv_s_crt(&comspec_value, nullptr, comspec_name)) != 0)
164
+ return nullptr;
165
+
166
+ return comspec_value;
167
+ }
168
+
169
+
170
+
171
+ template <typename Character>
172
+ static Character const* __cdecl get_path() throw()
173
+ {
174
+ typedef __acrt_stdio_char_traits<Character> stdio_traits;
175
+
176
+ static Character const path_name[] = { 'P', 'A', 'T', 'H', '\0' };
177
+
178
+ Character* path_value = nullptr;
179
+ if (_ERRCHECK_EINVAL(stdio_traits::tdupenv_s_crt(&path_value, nullptr, path_name)) != 0)
180
+ return nullptr;
181
+
182
+ return path_value;
183
+ }
184
+
185
+
186
+
187
+ template <typename Character>
188
+ static Character const* __cdecl get_executable_path(
189
+ Character const* const executable
190
+ ) throw()
191
+ {
192
+ typedef __acrt_stdio_char_traits<Character> stdio_traits;
193
+
194
+ // If we can access the given path, just use it:
195
+ if (stdio_traits::taccess_s(executable, 0) == 0)
196
+ return executable;
197
+
198
+ // Otherwise, we need to search the PATH:
199
+ __crt_unique_heap_ptr<Character> buffer(_calloc_crt_t(Character, MAX_PATH));
200
+ if (buffer.get() == nullptr)
201
+ return nullptr;
202
+
203
+ __crt_unique_heap_ptr<Character const> path(get_path<Character>());
204
+
205
+ Character const* current = path.get();
206
+ while ((current = stdio_traits::tgetpath(current, buffer.get(), MAX_PATH - 1)) != 0)
207
+ {
208
+ if (__crt_stdio_path_requires_backslash(buffer.get()))
209
+ {
210
+ static Character const backslash[] = { '\\', '\0' };
211
+ _ERRCHECK(stdio_traits::tcscat_s(buffer.get(), MAX_PATH, backslash));
212
+ }
213
+
214
+ if (stdio_traits::tcslen(buffer.get()) + stdio_traits::tcslen(executable) >= MAX_PATH)
215
+ return nullptr;
216
+
217
+ _ERRCHECK(stdio_traits::tcscat_s(buffer.get(), MAX_PATH, executable));
218
+
219
+ if (stdio_traits::taccess_s(buffer.get(), 0) == 0)
220
+ return buffer.detach();
221
+ }
222
+
223
+ return nullptr;
224
+ }
225
+
226
+
227
+
228
+ template <typename Character>
229
+ static FILE* __cdecl common_popen_nolock(
230
+ Character const* const command,
231
+ Character const* const fdopen_mode,
232
+ int const std_fh,
233
+ int (&pipe_handles)[2]
234
+ ) throw()
235
+ {
236
+ typedef __acrt_stdio_char_traits<Character> stdio_traits;
237
+
238
+ HANDLE const process_handle = GetCurrentProcess();
239
+
240
+ // We only return the second pipe handle to the caller; for the first pipe,
241
+ // we just need to use the HANDLE:
242
+ __crt_unique_handle new_pipe_handle;
243
+ if (!DuplicateHandle(
244
+ process_handle,
245
+ reinterpret_cast<HANDLE>(_osfhnd(pipe_handles[0])),
246
+ process_handle,
247
+ new_pipe_handle.get_address_of(),
248
+ 0,
249
+ TRUE,
250
+ DUPLICATE_SAME_ACCESS))
251
+ {
252
+ return nullptr;
253
+ }
254
+
255
+ _close(pipe_handles[0]);
256
+ pipe_handles[0] = -1;
257
+
258
+ // Associate a stream with the pipe handle to be returned to the caller:
259
+ unique_stream pipe_stream(stdio_traits::tfdopen(pipe_handles[1], fdopen_mode));
260
+ if (!pipe_stream)
261
+ return nullptr;
262
+
263
+ // Obtain a proces handle pair in which to store the process handle:
264
+ unique_process_handle_pair id_pair(idtab(nullptr));
265
+ if (!id_pair)
266
+ return nullptr;
267
+
268
+ // Determine which command processor to use: command.com or cmd.exe:
269
+ static Character const default_cmd_exe[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', '\0' };
270
+
271
+ __crt_unique_heap_ptr<Character const> const comspec_variable(get_comspec<Character>());
272
+ Character const* const cmd_exe = comspec_variable.get() != nullptr
273
+ ? comspec_variable.get()
274
+ : default_cmd_exe;
275
+
276
+ STARTUPINFOW startup_info = { 0 };
277
+ startup_info.cb = sizeof(startup_info);
278
+
279
+ // The following arguments are used by the OS for duplicating the handles:
280
+ startup_info.dwFlags = STARTF_USESTDHANDLES;
281
+ startup_info.hStdInput = std_fh == STDIN ? new_pipe_handle.get() : reinterpret_cast<HANDLE>(_osfhnd(0));
282
+ startup_info.hStdOutput = std_fh == STDOUT ? new_pipe_handle.get() : reinterpret_cast<HANDLE>(_osfhnd(1));
283
+ startup_info.hStdError = reinterpret_cast<HANDLE>(_osfhnd(2));
284
+
285
+ static Character const slash_c[] = { ' ', '/', 'c', ' ', '\0' };
286
+
287
+ size_t const command_line_count =
288
+ stdio_traits::tcslen(cmd_exe) +
289
+ stdio_traits::tcslen(slash_c) +
290
+ stdio_traits::tcslen(command) +
291
+ 1;
292
+
293
+ __crt_unique_heap_ptr<Character> const command_line(_calloc_crt_t(Character, command_line_count));
294
+ if (command_line.get() == nullptr)
295
+ return nullptr;
296
+
297
+ _ERRCHECK(stdio_traits::tcscpy_s(command_line.get(), command_line_count, cmd_exe));
298
+ _ERRCHECK(stdio_traits::tcscat_s(command_line.get(), command_line_count, slash_c));
299
+ _ERRCHECK(stdio_traits::tcscat_s(command_line.get(), command_line_count, command));
300
+
301
+ // Find the path at which the executable is accessible:
302
+ Character const* const selected_cmd_exe(get_executable_path(cmd_exe));
303
+ if (selected_cmd_exe == nullptr)
304
+ return nullptr;
305
+
306
+ // If get_executable_path() returned a path other than the one we gave it,
307
+ // we must be sure to free the string when we return:
308
+ __crt_unique_heap_ptr<Character const> const owned_final_exe_path(selected_cmd_exe != cmd_exe
309
+ ? selected_cmd_exe
310
+ : nullptr);
311
+
312
+ PROCESS_INFORMATION process_info = PROCESS_INFORMATION();
313
+ BOOL const child_status = stdio_traits::create_process(
314
+ selected_cmd_exe,
315
+ command_line.get(),
316
+ nullptr,
317
+ nullptr,
318
+ TRUE,
319
+ 0,
320
+ nullptr,
321
+ nullptr,
322
+ &startup_info,
323
+ &process_info);
324
+
325
+ if (!child_status)
326
+ return nullptr;
327
+
328
+ FILE* const result_stream = pipe_stream.detach();
329
+
330
+ CloseHandle(process_info.hThread);
331
+ id_pair.get()->process_handle = reinterpret_cast<intptr_t>(process_info.hProcess);
332
+ id_pair.get()->stream = result_stream;
333
+ id_pair.detach();
334
+ return result_stream;
335
+ }
336
+
337
+
338
+
339
+ template <typename Character>
340
+ static FILE* __cdecl common_popen(
341
+ Character const* const command,
342
+ Character const* const type
343
+ ) throw()
344
+ {
345
+ _VALIDATE_RETURN(command != nullptr, EINVAL, nullptr);
346
+ _VALIDATE_RETURN(type != nullptr, EINVAL, nullptr);
347
+
348
+ fdopen_mode<Character> const fdopen_mode = convert_popen_type_to_fdopen_mode(type);
349
+ if (fdopen_mode.mode[0] == '\0')
350
+ return nullptr;
351
+
352
+ // Do the _pipe(). Note that neither of the resulting handles is inheritable.
353
+ int pipe_mode = _O_NOINHERIT;
354
+ if (fdopen_mode.mode[1] == 't') { pipe_mode |= _O_TEXT; }
355
+ if (fdopen_mode.mode[1] == 'b') { pipe_mode |= _O_BINARY; }
356
+
357
+ int pipe_handles[2];
358
+ if (_pipe(pipe_handles, 1024, pipe_mode) == -1)
359
+ return nullptr;
360
+
361
+ int const std_fh = fdopen_mode.mode[0] == 'w'
362
+ ? STDIN
363
+ : STDOUT;
364
+
365
+ int ordered_pipe_handles[] =
366
+ {
367
+ std_fh == STDIN ? pipe_handles[0] : pipe_handles[1],
368
+ std_fh == STDIN ? pipe_handles[1] : pipe_handles[0]
369
+ };
370
+
371
+ FILE* return_value = nullptr;
372
+
373
+ __acrt_lock(__acrt_popen_lock);
374
+ __try
375
+ {
376
+ errno_t const saved_errno = errno;
377
+
378
+ return_value = common_popen_nolock(
379
+ command,
380
+ fdopen_mode.mode,
381
+ std_fh,
382
+ ordered_pipe_handles);
383
+
384
+ errno = saved_errno;
385
+
386
+ if (return_value != nullptr)
387
+ __leave;
388
+
389
+ // If the implementation function returned successfully, everything was
390
+ // cleaned up except the lock.
391
+ int* const first = ordered_pipe_handles;
392
+ int* const last = first + _countof(ordered_pipe_handles);
393
+ for (int* it = first; it != last; ++it)
394
+ {
395
+ if (*it != -1)
396
+ _close(*it);
397
+ }
398
+ }
399
+ __finally
400
+ {
401
+ __acrt_unlock(__acrt_popen_lock);
402
+ }
403
+
404
+ return return_value;
405
+ }
406
+
407
+
408
+
409
+ // Starts a child process using the given 'command' and opens a pipe to it, as
410
+ // requested via the 'type'. If the 'type' string contains an 'r', the calling
411
+ // process can read the child command's standard output via the returned stream.
412
+ // If the 'type' string contains a 'w', the calling process can write to the
413
+ // child command's standard input via the returned stream.
414
+ //
415
+ // Returns a usable stream on success; returns null on failure.
416
+ extern "C" FILE* __cdecl _popen(
417
+ char const* const command,
418
+ char const* const type
419
+ )
420
+ {
421
+ return common_popen(command, type);
422
+ }
423
+
424
+
425
+
426
+ extern "C" FILE* __cdecl _wpopen(
427
+ wchar_t const* const command,
428
+ wchar_t const* const type
429
+ )
430
+ {
431
+ return common_popen(command, type);
432
+ }
433
+
434
+
435
+
436
+ // Waits on the child command with which the 'stream' is associated, then closes
437
+ // the stream and its associated pipe. The 'stream' must have been returned from
438
+ // a previous call to _popen(). This function looks up the process handle in the
439
+ // global table, waits on it, then closes the stream.
440
+ //
441
+ // On success, the exit status of the child command is returned. The format of
442
+ // the return value is the same as for cwait(), except that the low order and
443
+ // high order bytes are swapped. If an error occurs, -1 is returned.
444
+ extern "C" int __cdecl _pclose(FILE* const stream)
445
+ {
446
+ _VALIDATE_RETURN(stream != nullptr, EINVAL, -1);
447
+
448
+ int return_value = -1;
449
+
450
+ __acrt_lock(__acrt_popen_lock);
451
+ __try
452
+ {
453
+ process_handle_pair* const id_pair = idtab(stream);
454
+ if (id_pair == nullptr)
455
+ {
456
+ errno = EBADF;
457
+ __leave;
458
+ }
459
+
460
+ fclose(stream);
461
+
462
+ intptr_t const process_handle = id_pair->process_handle;
463
+
464
+ // Mark the id pair as free (we will close the handle in the call to _cwait):
465
+ id_pair->stream = nullptr;
466
+ id_pair->process_handle = 0;
467
+
468
+ // Wait on the child copy of the command processor and its children:
469
+ errno_t const saved_errno = errno;
470
+ errno = 0;
471
+
472
+ int status = 0;
473
+ if (_cwait(&status, process_handle, _WAIT_GRANDCHILD) != -1 || errno == EINTR)
474
+ {
475
+ errno = saved_errno;
476
+ return_value = status;
477
+ __leave;
478
+ }
479
+
480
+ errno = saved_errno;
481
+ }
482
+ __finally
483
+ {
484
+ __acrt_unlock(__acrt_popen_lock);
485
+ }
486
+
487
+ return return_value;
488
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/putch.cpp ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // putch.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _putch(), which writes a character to the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <corecrt_internal_lowio.h>
10
+ #include <corecrt_internal_mbstring.h>
11
+ #include <corecrt_internal_stdio.h>
12
+ #include <corecrt_internal_ptd_propagation.h>
13
+ #include <stdlib.h>
14
+
15
+ // Writes a wide character to the console. Returns the character on success,
16
+ // EOF on failure.
17
+ extern "C" int __cdecl _putch(int const c)
18
+ {
19
+ return __acrt_lock_and_call(__acrt_conio_lock, [&]
20
+ {
21
+ return _putch_nolock(c);
22
+ });
23
+ }
24
+
25
+ extern "C" int __cdecl _putch_nolock_internal(int const c, __crt_cached_ptd_host& ptd)
26
+ {
27
+ __acrt_ptd* const raw_ptd = ptd.get_raw_ptd();
28
+ unsigned char* const ch_buf = raw_ptd->_putch_buffer;
29
+ unsigned short* const pch_buf_used = &raw_ptd->_putch_buffer_used;
30
+
31
+ // We can only use the character directly if we are sure that the machine
32
+ // is big-endian.
33
+ int result = c;
34
+
35
+ // Why are we using putwch to write to Console when we could have
36
+ // written straight away to Console? The problem we have in writing to
37
+ // Console is that CRT codepage is different from Console codepage and
38
+ // thus to write to console, we will need to convert the codepage. Here
39
+ // we can use unicode version of these routines and this way we will
40
+ // only have to do one conversion and rest will be handled by putwch.
41
+
42
+ // The usual way people call putch is character by character. Also
43
+ // there is no way we can convert partial MBCS to unicode character. To
44
+ // address this issue, we buffer all the lead bytes and combine them
45
+ // with trail bytes and then do the conversion.
46
+ if (*pch_buf_used == 1)
47
+ {
48
+ _ASSERTE(isleadbyte(ch_buf[0]) != 0);
49
+
50
+ ch_buf[1] = static_cast<unsigned char>(c);
51
+ }
52
+ else
53
+ {
54
+ ch_buf[0] = static_cast<unsigned char>(c);
55
+ }
56
+
57
+ if (*pch_buf_used == 0 && isleadbyte(ch_buf[0]))
58
+ {
59
+ // We still need trail byte, wait for it.
60
+ *pch_buf_used = 1;
61
+ }
62
+ else
63
+ {
64
+ wchar_t wchar;
65
+ if (_mbtowc_internal(&wchar, reinterpret_cast<char const*>(ch_buf), *pch_buf_used + 1, ptd) == -1 ||
66
+ _putwch_nolock(wchar) == WEOF)
67
+ {
68
+ result = EOF;
69
+ }
70
+
71
+ // Since we have processed full MBCS character, we should reset ch_buf_used.
72
+ *pch_buf_used = 0;
73
+ }
74
+
75
+ return result;
76
+ }
77
+
78
+ extern "C" int __cdecl _putch_nolock(int const c)
79
+ {
80
+ __crt_cached_ptd_host ptd;
81
+ return _putch_nolock_internal(c, ptd);
82
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/conio/putwch.cpp ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // putwch.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _putwch(), which writes a wide character to the console.
7
+ //
8
+ #include <conio.h>
9
+ #include <corecrt_internal_lowio.h>
10
+ #include <corecrt_internal_ptd_propagation.h>
11
+
12
+ // Writes a wide character to the console. Returns the character on success,
13
+ // WEOF on failure.
14
+ extern "C" wint_t __cdecl _putwch(wchar_t const c)
15
+ {
16
+ return __acrt_lock_and_call(__acrt_conio_lock, [&]
17
+ {
18
+ return _putwch_nolock(c);
19
+ });
20
+ }
21
+
22
+ extern "C" wint_t __cdecl _putwch_nolock(wchar_t const c)
23
+ {
24
+ if (__dcrt_lowio_ensure_console_output_initialized() == FALSE)
25
+ return WEOF;
26
+
27
+ // Write character to console:
28
+ DWORD charsWritten;
29
+ if (__dcrt_write_console(&c, 1, &charsWritten) == FALSE)
30
+ return WEOF;
31
+
32
+ return c;
33
+ }
34
+
35
+ extern "C" wint_t __cdecl _putwch_nolock_internal(wchar_t const c, __crt_cached_ptd_host&)
36
+ {
37
+ // Currently _putwch_nolock does not require any PTD access. Do not need to propagate __crt_cached_ptd_host&.
38
+ return _putwch_nolock(c);
39
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_ctype.cpp ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // _ctype.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the function equivalents of the character classification macros in
7
+ // <ctype.h>. These function definitions make use of the macros. The functions
8
+ // return zero if the character does not meet the requirements, and nonzero if
9
+ // the character does meet the requirements.
10
+ //
11
+ #include <corecrt_internal.h>
12
+ #include <ctype.h>
13
+ #include <locale.h>
14
+
15
+ // The ctype functions (isalnum(), isupper(), etc) are very small and quick.
16
+ // Optimizing for size has a measurable negative impact, so we optimize for speed here.
17
+ #pragma optimize("t", on)
18
+
19
+ static __forceinline int fast_check_initial_locale(int const c, int const mask) throw()
20
+ {
21
+ #ifdef _DEBUG
22
+ return _chvalidator(c, mask);
23
+ #else
24
+ return __acrt_locale_get_ctype_array_value(__acrt_initial_locale_data._public._locale_pctype, c, mask);
25
+ #endif
26
+ }
27
+
28
+ static __forceinline int fast_check_current_locale(int const c, int const mask) throw()
29
+ {
30
+ // Avoid PTD lookup when locale is unchanged.
31
+ if (!__acrt_locale_changed())
32
+ {
33
+ return fast_check_initial_locale(c, mask);
34
+ }
35
+
36
+ // Avoid _LocaleUpdate overhead:
37
+ // * Extra un-inlined function calls
38
+ // * Multibyte locale synchronization
39
+ // * Marking/unmarking current thread as using per-thread-locales.
40
+
41
+ __acrt_ptd * const ptd{__acrt_getptd()};
42
+ __crt_locale_data * locale_info{ptd->_locale_info};
43
+ __acrt_update_locale_info(ptd, &locale_info);
44
+
45
+ // Common case
46
+ if (c >= -1 && c <= 255)
47
+ {
48
+ return locale_info->_public._locale_pctype[c] & mask;
49
+ }
50
+
51
+ // Microsoft Extension: Translate int values outside of unsigned char range for DBCS locales
52
+ // Note that our documentation also clearly states this is undefined.
53
+ if (locale_info->_public._locale_mb_cur_max > 1)
54
+ {
55
+ return _isctype_l(c, mask, nullptr);
56
+ }
57
+
58
+ return 0;
59
+ }
60
+
61
+ static __forceinline int fast_check_given_locale(int const c, int const mask, _locale_t const locale) throw()
62
+ {
63
+ // Avoid _LocaleUpdate overhead - just check whether it's nullptr.
64
+ if (locale == nullptr) {
65
+ return fast_check_current_locale(c, mask);
66
+ }
67
+
68
+ // Common case
69
+ if (c >= -1 && c <= 255)
70
+ {
71
+ return locale->locinfo->_public._locale_pctype[c] & mask;
72
+ }
73
+
74
+ // Microsoft Extension: Translate int values outside of unsigned char range for DBCS locales
75
+ // Note that our documentation also clearly states this is undefined.
76
+ if (locale->locinfo->_public._locale_mb_cur_max > 1)
77
+ {
78
+ return _isctype_l(c, mask, locale);
79
+ }
80
+
81
+ return 0;
82
+ }
83
+
84
+
85
+ extern "C" int (__cdecl _isalpha_l)(int const c, _locale_t const locale)
86
+ {
87
+ return fast_check_given_locale(c, _ALPHA, locale);
88
+ }
89
+
90
+ extern "C" int (__cdecl isalpha)(int const c)
91
+ {
92
+ return fast_check_current_locale(c, _ALPHA);
93
+ }
94
+
95
+
96
+ extern "C" int (__cdecl _isupper_l)(int const c, _locale_t const locale)
97
+ {
98
+ return fast_check_given_locale(c, _UPPER, locale);
99
+ }
100
+
101
+ extern "C" int (__cdecl isupper)(int const c)
102
+ {
103
+ return fast_check_current_locale(c, _UPPER);
104
+ }
105
+
106
+
107
+ extern "C" int (__cdecl _islower_l)(int const c, _locale_t const locale)
108
+ {
109
+ return fast_check_given_locale(c, _LOWER, locale);
110
+ }
111
+
112
+ extern "C" int (__cdecl islower)(int const c)
113
+ {
114
+ return fast_check_current_locale(c, _LOWER);
115
+ }
116
+
117
+
118
+ extern "C" int (__cdecl _isdigit_l)(int const c, _locale_t const locale)
119
+ {
120
+ return fast_check_given_locale(c, _DIGIT, locale);
121
+ }
122
+
123
+ extern "C" int (__cdecl isdigit)(int const c)
124
+ {
125
+ return fast_check_current_locale(c, _DIGIT);
126
+ }
127
+
128
+
129
+ extern "C" int (__cdecl _isxdigit_l)(int const c, _locale_t const locale)
130
+ {
131
+ return fast_check_given_locale(c, _HEX, locale);
132
+ }
133
+
134
+ extern "C" int (__cdecl isxdigit)(int const c)
135
+ {
136
+ return fast_check_current_locale(c, _HEX);
137
+ }
138
+
139
+
140
+ extern "C" int (__cdecl _isspace_l)(int const c, _locale_t const locale)
141
+ {
142
+ return fast_check_given_locale(c, _SPACE, locale);
143
+ }
144
+
145
+ extern "C" int (__cdecl isspace)(int const c)
146
+ {
147
+ return fast_check_current_locale(c, _SPACE);
148
+ }
149
+
150
+
151
+ extern "C" int (__cdecl _ispunct_l)(int const c, _locale_t const locale)
152
+ {
153
+ return fast_check_given_locale(c, _PUNCT, locale);
154
+ }
155
+
156
+ extern "C" int (__cdecl ispunct)(int const c)
157
+ {
158
+ return fast_check_current_locale(c, _PUNCT);
159
+ }
160
+
161
+
162
+ extern "C" int (__cdecl _isblank_l)(int const c, _locale_t const locale)
163
+ {
164
+ return fast_check_given_locale(c, _BLANK, locale);
165
+ }
166
+
167
+ extern "C" int (__cdecl isblank)(int const c)
168
+ {
169
+ // \t is a blank character, but is not registered as _Blank on the table, because that will make it
170
+ //printable. Also Windows (via GetStringType()) considered all _BLANK characters to also be _PRINT characters,
171
+ //so does not have a way to specify blank, non-printable.
172
+ if (c == '\t') {
173
+ return _BLANK;
174
+ }
175
+
176
+ return fast_check_current_locale(c, _BLANK);
177
+ }
178
+
179
+
180
+ extern "C" int (__cdecl _isalnum_l)(int const c, _locale_t const locale)
181
+ {
182
+ return fast_check_given_locale(c, _ALPHA | _DIGIT, locale);
183
+ }
184
+
185
+ extern "C" int (__cdecl isalnum)(int const c)
186
+ {
187
+ return fast_check_current_locale(c, _ALPHA | _DIGIT);
188
+ }
189
+
190
+
191
+ extern "C" int (__cdecl _isprint_l)(int const c, _locale_t const locale)
192
+ {
193
+ return fast_check_given_locale(c, _BLANK | _PUNCT | _ALPHA | _DIGIT, locale);
194
+ }
195
+
196
+ extern "C" int (__cdecl isprint)(int const c)
197
+ {
198
+ return fast_check_current_locale(c, _BLANK | _PUNCT | _ALPHA | _DIGIT);
199
+ }
200
+
201
+
202
+ extern "C" int (__cdecl _isgraph_l)(int const c, _locale_t const locale)
203
+ {
204
+ return fast_check_given_locale(c, _PUNCT | _ALPHA | _DIGIT, locale);
205
+ }
206
+
207
+ extern "C" int (__cdecl isgraph)(int const c)
208
+ {
209
+ return fast_check_current_locale(c, _PUNCT | _ALPHA | _DIGIT);
210
+ }
211
+
212
+
213
+ extern "C" int (__cdecl _iscntrl_l)(int const c, _locale_t const locale)
214
+ {
215
+ return fast_check_given_locale(c, _CONTROL, locale);
216
+ }
217
+
218
+ extern "C" int (__cdecl iscntrl)(int const c)
219
+ {
220
+ return fast_check_current_locale(c, _CONTROL);
221
+ }
222
+
223
+
224
+ extern "C" int (__cdecl __isascii)(int const c)
225
+ {
226
+ return __isascii(c);
227
+ }
228
+
229
+ extern "C" int (__cdecl __toascii)(int const c)
230
+ {
231
+ return __toascii(c);
232
+ }
233
+
234
+
235
+ extern "C" int (__cdecl _iscsymf_l)(int const c, _locale_t const locale)
236
+ {
237
+ return (_isalpha_l)(c, locale) || c == '_';
238
+ }
239
+
240
+ extern "C" int (__cdecl __iscsymf)(int const c)
241
+ {
242
+ return __iscsymf(c);
243
+ }
244
+
245
+
246
+ extern "C" int (__cdecl _iscsym_l)(int const c, _locale_t const locale)
247
+ {
248
+ return (_isalnum_l)(c, locale) || c == '_';
249
+ }
250
+
251
+ extern "C" int (__cdecl __iscsym)(int const c)
252
+ {
253
+ return __iscsym(static_cast<unsigned char>(c));
254
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_fptostr.cpp ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // _fptostr.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the internal function that writes a mantissa (in a STRFLT) into a
7
+ // buffer. This function puts all of the digits into the buffer, handles
8
+ // rounding based on the requested precision (digits), and updates the decimal
9
+ // point position in the STRFLT object. Note that this function does not change
10
+ // the mantissa field of the STRFLT, so callers of this function may rely on it
11
+ // being unmodified.
12
+ //
13
+ #include <corecrt_internal.h>
14
+ #include <corecrt_internal_fltintrn.h>
15
+ #include <corecrt_internal_ptd_propagation.h>
16
+ #include <fenv.h>
17
+ #include <string.h>
18
+ #include <stddef.h>
19
+
20
+ // __acrt_has_trailing_digits::no_trailing isn't indicative of how to round if we're rounding to a point before the end of the generated digits.
21
+ // __acrt_has_trailing_digits::no_trailing only says if there are any digits after the mantisa we got.
22
+ // We need to ensure the remaining generated digits are all zero before choosing rounds-to-even behavior intended for exactly representable floating point numbers.
23
+ static bool check_trailing(char const * mantissa_it, __acrt_has_trailing_digits const trailing_digits)
24
+ {
25
+ if (trailing_digits == __acrt_has_trailing_digits::trailing)
26
+ {
27
+ return true;
28
+ }
29
+
30
+ while (*mantissa_it == '0')
31
+ {
32
+ mantissa_it++;
33
+ }
34
+
35
+ if (*mantissa_it != '\0')
36
+ {
37
+ return true;
38
+ }
39
+
40
+ return false;
41
+ }
42
+
43
+ static bool should_round_up(
44
+ char const * const mantissa_base,
45
+ char const * const mantissa_it,
46
+ int const sign,
47
+ __acrt_has_trailing_digits const trailing_digits,
48
+ __acrt_rounding_mode const rounding_mode
49
+ )
50
+ {
51
+ if (rounding_mode == __acrt_rounding_mode::legacy)
52
+ {
53
+ return *mantissa_it >= '5';
54
+ }
55
+
56
+ int const round_mode = fegetround();
57
+
58
+ if (round_mode == FE_TONEAREST)
59
+ {
60
+ if (*mantissa_it > '5')
61
+ {
62
+ return true;
63
+ }
64
+
65
+ if (*mantissa_it < '5')
66
+ {
67
+ return false;
68
+ }
69
+
70
+ // If there are trailing digits we are in a scenario like this .5000000001 and should round up
71
+ if (check_trailing(mantissa_it + 1, trailing_digits))
72
+ {
73
+ return true;
74
+ }
75
+
76
+ // At this point, the number is exactly representable and we are rounding 5.
77
+ // In this case, IEEE 754 states to round towards the nearest even number.
78
+ // Therefore: if the previous digit is odd, we round up (1.5 -> 2).
79
+ // if the previous digit is even, we round down (2.5 -> 2).
80
+
81
+ // If there is no preceding digit, it is considered zero, so round down.
82
+ if (mantissa_it == mantissa_base)
83
+ {
84
+ return false;
85
+ }
86
+
87
+ // If the previous digit is odd, we should round up to the closest even.
88
+ return *(mantissa_it - 1) % 2;
89
+ }
90
+
91
+ if (round_mode == FE_UPWARD)
92
+ {
93
+ return check_trailing(mantissa_it, trailing_digits) && sign != '-';
94
+ }
95
+
96
+ if (round_mode == FE_DOWNWARD)
97
+ {
98
+ return check_trailing(mantissa_it, trailing_digits) && sign == '-';
99
+ }
100
+
101
+ return false;
102
+ }
103
+
104
+ extern "C" errno_t __cdecl __acrt_fp_strflt_to_string(
105
+ char* const buffer,
106
+ size_t const buffer_count,
107
+ int digits,
108
+ STRFLT const pflt,
109
+ __acrt_has_trailing_digits const trailing_digits,
110
+ __acrt_rounding_mode const rounding_mode,
111
+ __crt_cached_ptd_host& ptd
112
+ )
113
+ {
114
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, buffer != nullptr, EINVAL);
115
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, buffer_count > 0, EINVAL);
116
+ buffer[0] = '\0';
117
+
118
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, buffer_count > static_cast<size_t>((digits > 0 ? digits : 0) + 1), ERANGE);
119
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, pflt != nullptr, EINVAL);
120
+
121
+ char* buffer_it = buffer;
122
+ char* const mantissa_base = pflt->mantissa;
123
+ char* mantissa_it = pflt->mantissa;
124
+
125
+ // The buffer will contain 'digits' decimal digits plus an optional overflow
126
+ // digit for the rounding.
127
+
128
+ // Initialize the first digit in the buffer to '0' (Note: not '\0') and set
129
+ // the pointer to the second digit of the buffer. The first digit is used
130
+ // to handle overflow on rounding (e.g. 9.999... becomes 10.000...), which
131
+ // requires a carry into the first digit.
132
+ *buffer_it++ = '0';
133
+
134
+ // Copy the digits of the value into the buffer (with '0' padding) and
135
+ // insert the null terminator:
136
+ while (digits > 0)
137
+ {
138
+ *buffer_it++ = *mantissa_it ? *mantissa_it++ : '0';
139
+ --digits;
140
+ }
141
+
142
+ *buffer_it = '\0';
143
+
144
+ // Do any rounding which may be needed. Note: if digits < 0, we don't do
145
+ // any rounding because in this case, the rounding occurs in a digit which
146
+ // will not be output because of the precision requested.
147
+ if (digits >= 0 && should_round_up(mantissa_base, mantissa_it, pflt->sign, trailing_digits, rounding_mode))
148
+ {
149
+ buffer_it--;
150
+
151
+ while (*buffer_it == '9')
152
+ {
153
+ *buffer_it-- = '0';
154
+ }
155
+
156
+ *buffer_it += 1;
157
+ }
158
+
159
+ if (*buffer == '1')
160
+ {
161
+ // The rounding caused overflow into the leading digit (e.g. 9.999...
162
+ // became 10.000...), so increment the decimal point position by 1:
163
+ pflt->decpt++;
164
+ }
165
+ else
166
+ {
167
+ // Move the entire string to the left one digit to remove the unused
168
+ // overflow digit:
169
+ memmove(buffer, buffer + 1, strlen(buffer + 1) + 1);
170
+ }
171
+
172
+ return 0;
173
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_mbslen.cpp ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // _mbslen.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the mbstrlen functions, which return the number of multibyte
7
+ // characters in a multibyte string, excluding the null terminator. This
8
+ // function is locale-dependent. If the string contains invalid multibyte
9
+ // characters, -1 is returned and errno is set to EILSEQ.
10
+ //
11
+ // There are two variations of these functions: mbstrnlen (note the 'n'),
12
+ // which takes a max_size, which is the maximum number of bytes to scan
13
+ // in the string. These variations also validate their arguments. The
14
+ // variations without the 'n' (mbstrlen) stop only when the null terminator
15
+ // is reached and do not validate their arguments.
16
+ //
17
+ #include <corecrt_internal.h>
18
+ #include <stdlib.h>
19
+
20
+
21
+
22
+ _Check_return_
23
+ _Post_satisfies_((return <= _String_length_(string) && return <= max_size) || return == (size_t)-1)
24
+ static size_t __cdecl common_mbstrlen_l(
25
+ char const* const string,
26
+ size_t const max_size,
27
+ _locale_t const locale
28
+ )
29
+ {
30
+ _LocaleUpdate locale_update(locale);
31
+
32
+ _ASSERTE(
33
+ locale_update.GetLocaleT()->locinfo->_public._locale_mb_cur_max == 1 ||
34
+ locale_update.GetLocaleT()->locinfo->_public._locale_mb_cur_max == 2);
35
+
36
+ // Handle single byte character sets:
37
+ if (locale_update.GetLocaleT()->locinfo->_public._locale_mb_cur_max == 1)
38
+ {
39
+ return strnlen(string, max_size);
40
+ }
41
+
42
+ // Verify that all of the multibyte characters are valid:
43
+ if (__acrt_MultiByteToWideChar(
44
+ locale_update.GetLocaleT()->locinfo->_public._locale_lc_codepage,
45
+ MB_PRECOMPOSED | MB_ERR_INVALID_CHARS,
46
+ string,
47
+ static_cast<int>(max_size),
48
+ nullptr,
49
+ 0
50
+ ) == 0)
51
+ {
52
+ // There was a bad multibyte character:
53
+ errno = EILSEQ;
54
+ return static_cast<size_t>(-1);
55
+ }
56
+
57
+ // Count multibyte characters:
58
+ size_t n = 0; // Number of multibyte characters read
59
+ size_t size = 0; // Number of bytes read
60
+ for (char const* it = string; size < max_size && *it; ++n, ++it, ++size)
61
+ {
62
+ if (_isleadbyte_fast_internal(static_cast<unsigned char>(*it), locale_update.GetLocaleT()))
63
+ {
64
+ ++size;
65
+ if (size >= max_size)
66
+ break;
67
+
68
+ ++it;
69
+ if (*it == '\0')
70
+ break;
71
+ }
72
+ }
73
+
74
+ return size >= max_size ? max_size : n;
75
+ }
76
+
77
+
78
+
79
+ extern "C" size_t __cdecl _mbstrlen_l(
80
+ char const* const string,
81
+ _locale_t const locale
82
+ )
83
+ {
84
+ return common_mbstrlen_l(string, _CRT_UNBOUNDED_BUFFER_SIZE, locale);
85
+ }
86
+
87
+ extern "C" size_t __cdecl _mbstrlen(char const* const string)
88
+ {
89
+ if (!__acrt_locale_changed())
90
+ {
91
+ return strlen(string);
92
+ }
93
+ else
94
+ {
95
+ return _mbstrlen_l(string, nullptr);
96
+ }
97
+ }
98
+
99
+
100
+
101
+ extern "C" size_t __cdecl _mbstrnlen_l(
102
+ char const* const string,
103
+ size_t const max_size,
104
+ _locale_t const locale
105
+ )
106
+ {
107
+ _VALIDATE_RETURN(string != nullptr, EINVAL, static_cast<size_t>(-1));
108
+ _VALIDATE_RETURN(max_size <= INT_MAX, EINVAL, static_cast<size_t>(-1));
109
+
110
+ return common_mbstrlen_l(string, max_size, locale);
111
+ }
112
+
113
+ extern "C" size_t __cdecl _mbstrnlen(
114
+ char const* const string,
115
+ size_t const max_size
116
+ )
117
+ {
118
+ return _mbstrnlen_l(string, max_size, nullptr);
119
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/_wctype.cpp ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // _wctype.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the function equivalents of the wide character classification macros
7
+ // in <ctype.h>. These function definitions make use of the macros. The
8
+ // functions return zero if the character does not meet the requirements, and
9
+ // nonzero if the character does meet the requirements.
10
+ //
11
+ #include <corecrt_internal.h>
12
+ #include <ctype.h>
13
+ #include <locale.h>
14
+
15
+
16
+
17
+ extern "C" extern __inline int (__cdecl _isleadbyte_l)(int const c, _locale_t const locale)
18
+ {
19
+ _LocaleUpdate locale_update(locale);
20
+ return __acrt_locale_get_ctype_array_value(locale_update.GetLocaleT()->locinfo->_public._locale_pctype, c, _LEADBYTE);
21
+ }
22
+
23
+ extern "C" extern __inline int (__cdecl isleadbyte)(int const c)
24
+ {
25
+ return (_isleadbyte_l)(c, nullptr);
26
+ }
27
+
28
+ extern "C" extern __inline int (__cdecl _iswalpha_l)(wint_t const c, _locale_t)
29
+ {
30
+ return iswalpha(c);
31
+ }
32
+
33
+ extern "C" extern __inline int (__cdecl iswalpha)(wint_t const c)
34
+ {
35
+ return iswalpha(c);
36
+ }
37
+
38
+ extern "C" extern __inline int (__cdecl _iswupper_l)(wint_t const c, _locale_t)
39
+ {
40
+ return iswupper(c);
41
+ }
42
+
43
+ extern "C" extern __inline int (__cdecl iswupper)(wint_t const c)
44
+ {
45
+ return iswupper(c);
46
+ }
47
+
48
+ extern "C" extern __inline int (__cdecl _iswlower_l)(wint_t const c, _locale_t)
49
+ {
50
+ return iswlower(c);
51
+ }
52
+
53
+ extern "C" extern __inline int (__cdecl iswlower)(wint_t const c)
54
+ {
55
+ return iswlower(c);
56
+ }
57
+
58
+ extern "C" extern __inline int (__cdecl _iswdigit_l)(wint_t const c, _locale_t)
59
+ {
60
+ return iswdigit(c);
61
+ }
62
+
63
+ extern "C" extern __inline int (__cdecl iswdigit)(wint_t const c)
64
+ {
65
+ return iswdigit(c);
66
+ }
67
+
68
+ extern "C" extern __inline int (__cdecl _iswxdigit_l)(wint_t const c, _locale_t)
69
+ {
70
+ return iswxdigit(c);
71
+ }
72
+
73
+ extern "C" extern __inline int (__cdecl iswxdigit)(wint_t const c)
74
+ {
75
+ return iswxdigit(c);
76
+ }
77
+
78
+ extern "C" extern __inline int (__cdecl _iswspace_l)(wint_t const c, _locale_t)
79
+ {
80
+ return iswspace(c);
81
+ }
82
+
83
+ extern "C" extern __inline int (__cdecl iswspace)(wint_t const c)
84
+ {
85
+ return iswspace(c);
86
+ }
87
+
88
+ extern "C" extern __inline int (__cdecl _iswpunct_l)(wint_t const c, _locale_t)
89
+ {
90
+ return iswpunct(c);
91
+ }
92
+
93
+ extern "C" extern __inline int (__cdecl iswpunct)(wint_t const c)
94
+ {
95
+ return iswpunct(c);
96
+ }
97
+
98
+ extern "C" extern __inline int (__cdecl _iswblank_l)(wint_t const c, _locale_t)
99
+ {
100
+ return iswblank(c);
101
+ }
102
+
103
+ extern "C" extern __inline int (__cdecl iswblank)(wint_t const c)
104
+ {
105
+ return iswblank(c);
106
+ }
107
+
108
+ extern "C" extern __inline int (__cdecl _iswalnum_l)(wint_t const c, _locale_t)
109
+ {
110
+ return iswalnum(c);
111
+ }
112
+
113
+ extern "C" extern __inline int (__cdecl iswalnum)(wint_t const c)
114
+ {
115
+ return iswalnum(c);
116
+ }
117
+
118
+ extern "C" extern __inline int (__cdecl _iswprint_l)(wint_t const c, _locale_t)
119
+ {
120
+ return iswprint(c);
121
+ }
122
+
123
+ extern "C" extern __inline int (__cdecl iswprint)(wint_t const c)
124
+ {
125
+ return iswprint(c);
126
+ }
127
+
128
+ extern "C" extern __inline int (__cdecl _iswgraph_l)(wint_t const c, _locale_t)
129
+ {
130
+ return iswgraph(c);
131
+ }
132
+
133
+ extern "C" extern __inline int (__cdecl iswgraph)(wint_t const c)
134
+ {
135
+ return iswgraph(c);
136
+ }
137
+
138
+ extern "C" extern __inline int (__cdecl _iswcntrl_l)(wint_t const c, _locale_t)
139
+ {
140
+ return iswcntrl(c);
141
+ }
142
+
143
+ extern "C" extern __inline int (__cdecl iswcntrl)(wint_t const c)
144
+ {
145
+ return iswcntrl(c);
146
+ }
147
+
148
+ extern "C" extern __inline int (__cdecl iswascii)(wint_t const c)
149
+ {
150
+ return iswascii(c);
151
+ }
152
+
153
+ extern "C" extern __inline int (__cdecl _iswcsym_l)(wint_t const c, _locale_t)
154
+ {
155
+ return __iswcsym(c);
156
+ }
157
+
158
+ extern "C" extern __inline int (__cdecl __iswcsym)(wint_t const c)
159
+ {
160
+ return __iswcsym(c);
161
+ }
162
+
163
+ extern "C" extern __inline int (__cdecl _iswcsymf_l)(wint_t const c, _locale_t)
164
+ {
165
+ return __iswcsymf(c);
166
+ }
167
+
168
+ extern "C" extern __inline int (__cdecl __iswcsymf)(wint_t const c)
169
+ {
170
+ return __iswcsymf(c);
171
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atof.cpp ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // atof.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Definitions of the functions that convert strings to floating point numbers.
7
+ // Note that the str- and wcs-prefixed functions are defined elsewhere. The
8
+ // functions defined here are provided for legacy support.
9
+ //
10
+ // The _atoxxx family of functions convert a string into a floating point
11
+ // number and return either zero (success), or _UNDERFLOW or _OVERFLOW.
12
+ //
13
+ #define _ALLOW_OLD_VALIDATE_MACROS
14
+ #include <corecrt_internal_strtox.h>
15
+
16
+
17
+
18
+ template <typename FloatingType, typename Character>
19
+ static int __cdecl common_atodbl_l(
20
+ FloatingType* const result,
21
+ Character const* const string,
22
+ _locale_t const locale
23
+ ) throw()
24
+ {
25
+ _VALIDATE_RETURN(result != nullptr, EINVAL, _DOMAIN);
26
+
27
+ _LocaleUpdate locale_update(locale);
28
+ SLD_STATUS const status = __crt_strtox::parse_floating_point(
29
+ locale_update.GetLocaleT(),
30
+ __crt_strtox::make_c_string_character_source(string, nullptr),
31
+ result);
32
+
33
+ switch (status)
34
+ {
35
+ case SLD_OVERFLOW: return _OVERFLOW;
36
+ case SLD_UNDERFLOW: return _UNDERFLOW;
37
+ default: return 0;
38
+ }
39
+ }
40
+
41
+ extern "C" int __cdecl _atoflt_l(_CRT_FLOAT* const result, char const* const string, _locale_t const locale)
42
+ {
43
+ return common_atodbl_l(&result->f, string, locale);
44
+ }
45
+
46
+ extern "C" int __cdecl _atoflt(_CRT_FLOAT* const result, char const* const string)
47
+ {
48
+ return common_atodbl_l(&result->f, string, nullptr);
49
+ }
50
+
51
+ extern "C" int __cdecl _atodbl_l(_CRT_DOUBLE* const result, char* const string, _locale_t const locale)
52
+ {
53
+ return common_atodbl_l(&result->x, string, locale);
54
+ }
55
+
56
+ extern "C" int __cdecl _atodbl(_CRT_DOUBLE* const result, char* const string)
57
+ {
58
+ return common_atodbl_l(&result->x, string, nullptr);
59
+ }
60
+
61
+
62
+
63
+ template <typename Character>
64
+ static double __cdecl common_atof_l(Character const* const string, _locale_t const locale) throw()
65
+ {
66
+ _VALIDATE_RETURN(string != nullptr, EINVAL, 0.0);
67
+
68
+ _LocaleUpdate locale_update(locale);
69
+
70
+ double result{};
71
+ __crt_strtox::parse_floating_point(
72
+ locale_update.GetLocaleT(),
73
+ __crt_strtox::make_c_string_character_source(string, nullptr),
74
+ &result);
75
+ return result;
76
+ }
77
+
78
+ extern "C" double __cdecl _atof_l(char const* const string, _locale_t const locale)
79
+ {
80
+ return common_atof_l(string, locale);
81
+ }
82
+
83
+ extern "C" double __cdecl atof(char const* const string)
84
+ {
85
+ return common_atof_l(string, nullptr);
86
+ }
87
+
88
+ extern "C" double __cdecl _wtof_l(wchar_t const* const string, _locale_t const locale)
89
+ {
90
+ return common_atof_l(string, locale);
91
+ }
92
+
93
+ extern "C" double __cdecl _wtof(wchar_t const* const string)
94
+ {
95
+ return common_atof_l(string, nullptr);
96
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atoldbl.cpp ADDED
@@ -0,0 +1,833 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // atoldbl.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // The _atoldbl and _atoldbl_l functions, which convert a string representation
7
+ // of a floating point number into a 10-byte _LDOUBLE object.
8
+ //
9
+ #define _ALLOW_OLD_VALIDATE_MACROS
10
+ #include <corecrt_internal.h>
11
+ #include <corecrt_internal_fltintrn.h>
12
+ #include <corecrt_internal_strtox.h>
13
+ #include <float.h>
14
+ #include <locale.h>
15
+ #include <math.h>
16
+ #include <stdlib.h>
17
+ #include <string.h>
18
+
19
+
20
+
21
+ #define PTR_12(x) ((uint8_t*)(&(x)->ld12))
22
+
23
+ #define MSB_USHORT ((uint16_t) 0x8000)
24
+ #define MSB_ULONG ((uint32_t) 0x80000000)
25
+
26
+ #define TMAX10 5200 /* maximum temporary decimal exponent */
27
+ #define TMIN10 -5200 /* minimum temporary decimal exponent */
28
+ #define LD_MAX_EXP_LEN 4 /* maximum number of decimal exponent digits */
29
+ #define LD_MAX_MAN_LEN 24 /* maximum length of mantissa (decimal)*/
30
+ #define LD_MAX_MAN_LEN1 25 /* MAX_MAN_LEN+1 */
31
+
32
+ #define LD_BIAS 0x3fff /* exponent bias for long double */
33
+ #define LD_BIASM1 0x3ffe /* LD_BIAS - 1 */
34
+ #define LD_MAXEXP 0x7fff /* maximum biased exponent */
35
+
36
+ #define D_BIAS 0x3ff /* exponent bias for double */
37
+ #define D_BIASM1 0x3fe /* D_BIAS - 1 */
38
+ #define D_MAXEXP 0x7ff /* maximum biased exponent */
39
+
40
+ // Macros for manipulation of a 12-byte long double number (an ordinary 10-byte
41
+ // long double plus two extra bytes of mantissa).
42
+ // byte layout:
43
+ //
44
+ // +-----+--------+--------+-------+
45
+ // |XT(2)|MANLO(4)|MANHI(4)|EXP(2) |
46
+ // +-----+--------+--------+-------+
47
+ // |<-UL_LO->|<-UL_MED->|<-UL_HI ->|
48
+ // (4) (4) (4)
49
+ #define ALIGN(x) ((unsigned long _UNALIGNED*)(x))
50
+
51
+ #define U_EXP_12(p) ((uint16_t *)(PTR_12(p) + 10))
52
+ #define UL_MANHI_12(p) ((uint32_t _UNALIGNED*)(PTR_12(p) + 6))
53
+ #define UL_MANLO_12(p) ((uint32_t _UNALIGNED*)(PTR_12(p) + 2))
54
+ #define U_XT_12(p) ((uint16_t *)(PTR_12(p) ))
55
+
56
+ // Pointers to the four low, mid, and high order bytes of the extended mantissa
57
+ #define UL_LO_12(p) ((uint32_t*)(PTR_12(p) ))
58
+ #define UL_MED_12(p) ((uint32_t*)(PTR_12(p) + 4))
59
+ #define UL_HI_12(p) ((uint32_t*)(PTR_12(p) + 8))
60
+
61
+ // Pointers to the uint8_t, uint16_t, and uint32_t of order i (LSB = 0; MSB = 9)
62
+ #define UCHAR_12(p, i) ((uint8_t *)( PTR_12(p) + (i)))
63
+ #define USHORT_12(p, i) ((uint16_t*)((uint8_t*)PTR_12(p) + (i)))
64
+ #define ULONG_12(p, i) ((uint32_t*)((uint8_t*)PTR_12(p) + (i)))
65
+ #define TEN_BYTE_PART(p) ((uint8_t *)( PTR_12(p) + 2 ))
66
+
67
+ // Manipulation of a 10-byte long double number
68
+ #define U_EXP_LD(p) ((uint16_t*)(_PTR_LD(p) + 8))
69
+ #define UL_MANHI_LD(p) ((uint32_t*)(_PTR_LD(p) + 4))
70
+ #define UL_MANLO_LD(p) ((uint32_t*)(_PTR_LD(p) ))
71
+
72
+ // Manipulation of a 64bit IEEE double
73
+ #define U_SHORT4_D(p) ((uint16_t*)(p) + 3)
74
+ #define UL_HI_D(p) ((uint32_t*)(p) + 1)
75
+ #define UL_LO_D(p) ((uint32_t*)(p) )
76
+
77
+ #define PUT_INF_12(p, sign) \
78
+ *UL_HI_12 (p) = (sign) ? 0xffff8000 : 0x7fff8000; \
79
+ *UL_MED_12(p) = 0; \
80
+ *UL_LO_12 (p) = 0;
81
+
82
+ #define PUT_ZERO_12(p) \
83
+ *UL_HI_12 (p) = 0; \
84
+ *UL_MED_12(p) = 0; \
85
+ *UL_LO_12 (p) = 0;
86
+
87
+ #define ISZERO_12(p) \
88
+ ((*UL_HI_12 (p) & 0x7fffffff) == 0 && \
89
+ *UL_MED_12(p) == 0 && \
90
+ *UL_LO_12 (p) == 0)
91
+
92
+ #define PUT_INF_LD(p, sign) \
93
+ *U_EXP_LD (p) = (sign) ? 0xffff : 0x7fff; \
94
+ *UL_MANHI_LD(p) = 0x8000; \
95
+ *UL_MANLO_LD(p) = 0;
96
+
97
+ #define PUT_ZERO_LD(p) \
98
+ *U_EXP_LD (p) = 0; \
99
+ *UL_MANHI_LD(p) = 0; \
100
+ *UL_MANLO_LD(p) = 0;
101
+
102
+ #define ISZERO_LD(p) \
103
+ ((*U_EXP_LD (p) & 0x7fff) == 0 && \
104
+ *UL_MANHI_LD(p) == 0 && \
105
+ *UL_MANLO_LD(p) == 0)
106
+
107
+
108
+
109
+ static _LDBL12 const ld12_pow10_positive[] =
110
+ {
111
+ /*P0001*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA0,0x02,0x40}},
112
+ /*P0002*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC8,0x05,0x40}},
113
+ /*P0003*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFA,0x08,0x40}},
114
+ /*P0004*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x9C,0x0C,0x40}},
115
+ /*P0005*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x50,0xC3,0x0F,0x40}},
116
+ /*P0006*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x24,0xF4,0x12,0x40}},
117
+ /*P0007*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x80,0x96,0x98,0x16,0x40}},
118
+ /*P0008*/ {{0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x20,0xBC,0xBE,0x19,0x40}},
119
+ /*P0016*/ {{0x00,0x00, 0x00,0x00,0x00,0x04,0xBF,0xC9,0x1B,0x8E,0x34,0x40}},
120
+ /*P0024*/ {{0x00,0x00, 0x00,0xA1,0xED,0xCC,0xCE,0x1B,0xC2,0xD3,0x4E,0x40}},
121
+ /*P0032*/ {{0x20,0xF0, 0x9E,0xB5,0x70,0x2B,0xA8,0xAD,0xC5,0x9D,0x69,0x40}},
122
+ /*P0040*/ {{0xD0,0x5D, 0xFD,0x25,0xE5,0x1A,0x8E,0x4F,0x19,0xEB,0x83,0x40}},
123
+ /*P0048*/ {{0x71,0x96, 0xD7,0x95,0x43,0x0E,0x05,0x8D,0x29,0xAF,0x9E,0x40}},
124
+ /*P0056*/ {{0xF9,0xBF, 0xA0,0x44,0xED,0x81,0x12,0x8F,0x81,0x82,0xB9,0x40}},
125
+ /*P0064*/ {{0xBF,0x3C, 0xD5,0xA6,0xCF,0xFF,0x49,0x1F,0x78,0xC2,0xD3,0x40}},
126
+ /*P0128*/ {{0x6F,0xC6, 0xE0,0x8C,0xE9,0x80,0xC9,0x47,0xBA,0x93,0xA8,0x41}},
127
+ /*P0192*/ {{0xBC,0x85, 0x6B,0x55,0x27,0x39,0x8D,0xF7,0x70,0xE0,0x7C,0x42}},
128
+ /*P0256*/ {{0xBC,0xDD, 0x8E,0xDE,0xF9,0x9D,0xFB,0xEB,0x7E,0xAA,0x51,0x43}},
129
+ /*P0320*/ {{0xA1,0xE6, 0x76,0xE3,0xCC,0xF2,0x29,0x2F,0x84,0x81,0x26,0x44}},
130
+ /*P0384*/ {{0x28,0x10, 0x17,0xAA,0xF8,0xAE,0x10,0xE3,0xC5,0xC4,0xFA,0x44}},
131
+ /*P0448*/ {{0xEB,0xA7, 0xD4,0xF3,0xF7,0xEB,0xE1,0x4A,0x7A,0x95,0xCF,0x45}},
132
+ /*P0512*/ {{0x65,0xCC, 0xC7,0x91,0x0E,0xA6,0xAE,0xA0,0x19,0xE3,0xA3,0x46}},
133
+ /*P1024*/ {{0x0D,0x65, 0x17,0x0C,0x75,0x81,0x86,0x75,0x76,0xC9,0x48,0x4D}},
134
+ /*P1536*/ {{0x58,0x42, 0xE4,0xA7,0x93,0x39,0x3B,0x35,0xB8,0xB2,0xED,0x53}},
135
+ /*P2048*/ {{0x4D,0xA7, 0xE5,0x5D,0x3D,0xC5,0x5D,0x3B,0x8B,0x9E,0x92,0x5A}},
136
+ /*P2560*/ {{0xFF,0x5D, 0xA6,0xF0,0xA1,0x20,0xC0,0x54,0xA5,0x8C,0x37,0x61}},
137
+ /*P3072*/ {{0xD1,0xFD, 0x8B,0x5A,0x8B,0xD8,0x25,0x5D,0x89,0xF9,0xDB,0x67}},
138
+ /*P3584*/ {{0xAA,0x95, 0xF8,0xF3,0x27,0xBF,0xA2,0xC8,0x5D,0xDD,0x80,0x6E}},
139
+ /*P4096*/ {{0x4C,0xC9, 0x9B,0x97,0x20,0x8A,0x02,0x52,0x60,0xC4,0x25,0x75}}
140
+ };
141
+
142
+ static _LDBL12 const ld12_pow10_negative[] =
143
+ {
144
+ /*N0001*/ {{0xCD,0xCC, 0xCD,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xFB,0x3F}},
145
+ /*N0002*/ {{0x71,0x3D, 0x0A,0xD7,0xA3,0x70,0x3D,0x0A,0xD7,0xA3,0xF8,0x3F}},
146
+ /*N0003*/ {{0x5A,0x64, 0x3B,0xDF,0x4F,0x8D,0x97,0x6E,0x12,0x83,0xF5,0x3F}},
147
+ /*N0004*/ {{0xC3,0xD3, 0x2C,0x65,0x19,0xE2,0x58,0x17,0xB7,0xD1,0xF1,0x3F}},
148
+ /*N0005*/ {{0xD0,0x0F, 0x23,0x84,0x47,0x1B,0x47,0xAC,0xC5,0xA7,0xEE,0x3F}},
149
+ /*N0006*/ {{0x40,0xA6, 0xB6,0x69,0x6C,0xAF,0x05,0xBD,0x37,0x86,0xEB,0x3F}},
150
+ /*N0007*/ {{0x33,0x3D, 0xBC,0x42,0x7A,0xE5,0xD5,0x94,0xBF,0xD6,0xE7,0x3F}},
151
+ /*N0008*/ {{0xC2,0xFD, 0xFD,0xCE,0x61,0x84,0x11,0x77,0xCC,0xAB,0xE4,0x3F}},
152
+ /*N0016*/ {{0x2F,0x4C, 0x5B,0xE1,0x4D,0xC4,0xBE,0x94,0x95,0xE6,0xC9,0x3F}},
153
+ /*N0024*/ {{0x92,0xC4, 0x53,0x3B,0x75,0x44,0xCD,0x14,0xBE,0x9A,0xAF,0x3F}},
154
+ /*N0032*/ {{0xDE,0x67, 0xBA,0x94,0x39,0x45,0xAD,0x1E,0xB1,0xCF,0x94,0x3F}},
155
+ /*N0040*/ {{0x24,0x23, 0xC6,0xE2,0xBC,0xBA,0x3B,0x31,0x61,0x8B,0x7A,0x3F}},
156
+ /*N0048*/ {{0x61,0x55, 0x59,0xC1,0x7E,0xB1,0x53,0x7C,0x12,0xBB,0x5F,0x3F}},
157
+ /*N0056*/ {{0xD7,0xEE, 0x2F,0x8D,0x06,0xBE,0x92,0x85,0x15,0xFB,0x44,0x3F}},
158
+ /*N0064*/ {{0x24,0x3F, 0xA5,0xE9,0x39,0xA5,0x27,0xEA,0x7F,0xA8,0x2A,0x3F}},
159
+ /*N0128*/ {{0x7D,0xAC, 0xA1,0xE4,0xBC,0x64,0x7C,0x46,0xD0,0xDD,0x55,0x3E}},
160
+ /*N0192*/ {{0x63,0x7B, 0x06,0xCC,0x23,0x54,0x77,0x83,0xFF,0x91,0x81,0x3D}},
161
+ /*N0256*/ {{0x91,0xFA, 0x3A,0x19,0x7A,0x63,0x25,0x43,0x31,0xC0,0xAC,0x3C}},
162
+ /*N0320*/ {{0x21,0x89, 0xD1,0x38,0x82,0x47,0x97,0xB8,0x00,0xFD,0xD7,0x3B}},
163
+ /*N0384*/ {{0xDC,0x88, 0x58,0x08,0x1B,0xB1,0xE8,0xE3,0x86,0xA6,0x03,0x3B}},
164
+ /*N0448*/ {{0xC6,0x84, 0x45,0x42,0x07,0xB6,0x99,0x75,0x37,0xDB,0x2E,0x3A}},
165
+ /*N0512*/ {{0x33,0x71, 0x1C,0xD2,0x23,0xDB,0x32,0xEE,0x49,0x90,0x5A,0x39}},
166
+ /*N1024*/ {{0xA6,0x87, 0xBE,0xC0,0x57,0xDA,0xA5,0x82,0xA6,0xA2,0xB5,0x32}},
167
+ /*N1536*/ {{0xE2,0x68, 0xB2,0x11,0xA7,0x52,0x9F,0x44,0x59,0xB7,0x10,0x2C}},
168
+ /*N2048*/ {{0x25,0x49, 0xE4,0x2D,0x36,0x34,0x4F,0x53,0xAE,0xCE,0x6B,0x25}},
169
+ /*N2560*/ {{0x8F,0x59, 0x04,0xA4,0xC0,0xDE,0xC2,0x7D,0xFB,0xE8,0xC6,0x1E}},
170
+ /*N3072*/ {{0x9E,0xE7, 0x88,0x5A,0x57,0x91,0x3C,0xBF,0x50,0x83,0x22,0x18}},
171
+ /*N3584*/ {{0x4E,0x4B, 0x65,0x62,0xFD,0x83,0x8F,0xAF,0x06,0x94,0x7D,0x11}},
172
+ /*N4096*/ {{0xE4,0x2D, 0xDE,0x9F,0xCE,0xD2,0xC8,0x04,0xDD,0xA6,0xD8,0x0A}}
173
+ };
174
+
175
+
176
+
177
+ // Adds x and y, storing the result in *sum. Returns true if overflow occurred;
178
+ // false otherwise.
179
+ static __forceinline bool __cdecl add_uint32_carry(uint32_t const x, uint32_t const y, uint32_t* const sum) throw()
180
+ {
181
+ uint32_t const r = x + y;
182
+
183
+ *sum = r;
184
+
185
+ return r < x || r < y; // carry
186
+ }
187
+
188
+ // Adds *x and *y as 12-byte integers, storing the result in *x. Overflow is ignored.
189
+ static __forceinline void __cdecl add_ld12(_LDBL12* const x, _LDBL12 const* const y) throw()
190
+ {
191
+ if (add_uint32_carry(*UL_LO_12(x), *UL_LO_12(y), UL_LO_12(x)))
192
+ {
193
+ if (add_uint32_carry(*UL_MED_12(x), 1, UL_MED_12(x)))
194
+ {
195
+ ++*UL_HI_12(x);
196
+ }
197
+ }
198
+
199
+ if (add_uint32_carry(*UL_MED_12(x), *UL_MED_12(y), UL_MED_12(x)))
200
+ {
201
+ ++*UL_HI_12(x);
202
+ }
203
+
204
+ // Ignore next carry -- assume no overflow will occur
205
+ add_uint32_carry(*UL_HI_12(x), *UL_HI_12(y), UL_HI_12(x));
206
+ }
207
+
208
+ // Shifts *p N bits to the left. The number is shifted as a 12-byte integer.
209
+ template <uint32_t N>
210
+ static __forceinline void __cdecl shl_ld12(_LDBL12* const p) throw()
211
+ {
212
+ uint32_t const total_bits{sizeof(uint32_t) * CHAR_BIT};
213
+ uint32_t const msb_bits{N};
214
+ uint32_t const lsb_bits{total_bits - N};
215
+
216
+ static_assert(msb_bits <= total_bits, "shift too large");
217
+
218
+ uint32_t const lsb_mask{(1 << (lsb_bits - 1)) - 1};
219
+ uint32_t const msb_mask{static_cast<uint32_t>(-1) ^ lsb_mask};
220
+
221
+ uint32_t const lo_carry {(*UL_LO_12 (p) & msb_mask) >> lsb_bits};
222
+ uint32_t const med_carry{(*UL_MED_12(p) & msb_mask) >> lsb_bits};
223
+
224
+ *UL_LO_12 (p) = (*UL_LO_12 (p) << msb_bits);
225
+ *UL_MED_12(p) = (*UL_MED_12(p) << msb_bits) | lo_carry;
226
+ *UL_HI_12 (p) = (*UL_HI_12 (p) << msb_bits) | med_carry;
227
+ }
228
+
229
+ // Shifts *p one bit to the right. The number is shifted as a 12-byte integer.
230
+ static __forceinline void __cdecl shr_ld12(_LDBL12* const p) throw()
231
+ {
232
+ uint32_t const c2 = *UL_HI_12 (p) & 0x1 ? MSB_ULONG : 0;
233
+ uint32_t const c1 = *UL_MED_12(p) & 0x1 ? MSB_ULONG : 0;
234
+
235
+ *UL_HI_12 (p) >>= 1;
236
+ *UL_MED_12(p) = *UL_MED_12(p) >> 1 | c2;
237
+ *UL_LO_12 (p) = *UL_LO_12 (p) >> 1 | c1;
238
+ }
239
+
240
+ // Multiplies *px and *py, storing the result in *px.
241
+ static __forceinline void __cdecl multiply_ld12(_LDBL12* const px, _LDBL12 const* const py) throw()
242
+ {
243
+ _LDBL12 tempman; // This is actually a 12-byte mantissa, not a 12-byte long double
244
+ *UL_LO_12 (&tempman) = 0;
245
+ *UL_MED_12(&tempman) = 0;
246
+ *UL_HI_12 (&tempman) = 0;
247
+
248
+ uint16_t expx = *U_EXP_12(px);
249
+ uint16_t expy = *U_EXP_12(py);
250
+
251
+ uint16_t const sign = (expx ^ expy) & static_cast<uint16_t>(0x8000);
252
+ expx &= 0x7fff;
253
+ expy &= 0x7fff;
254
+ uint16_t expsum = expx + expy;
255
+
256
+ if (expx >= LD_MAXEXP ||
257
+ expy >= LD_MAXEXP ||
258
+ expsum > LD_MAXEXP + LD_BIASM1)
259
+ {
260
+ // Overflow to infinity
261
+ PUT_INF_12(px, sign);
262
+ return;
263
+ }
264
+
265
+ if (expsum <= LD_BIASM1 - 63)
266
+ {
267
+ // Underflow to zero
268
+ PUT_ZERO_12(px);
269
+ return;
270
+ }
271
+
272
+ if (expx == 0)
273
+ {
274
+ // If this is a denormal temp real then the mantissa was shifted right
275
+ // once to set bit 63 to zero.
276
+ ++expsum; // Correct for this
277
+
278
+ if (ISZERO_12(px))
279
+ {
280
+ // Put positive sign:
281
+ *U_EXP_12(px) = 0;
282
+ return;
283
+ }
284
+ }
285
+
286
+ if (expy == 0)
287
+ {
288
+ ++expsum; // Because arg2 is denormal
289
+ if (ISZERO_12(py))
290
+ {
291
+ PUT_ZERO_12(px);
292
+ return;
293
+ }
294
+ }
295
+
296
+ int roffs = 0;
297
+ for (int i = 0; i < 5; ++i)
298
+ {
299
+ int poffs = i << 1;
300
+ int qoffs = 8;
301
+ for (int j = 5 - i; j > 0; --j)
302
+ {
303
+ bool carry = false;
304
+
305
+ uint16_t* const p = USHORT_12(px, poffs);
306
+ uint16_t* const q = USHORT_12(py, qoffs);
307
+ uint32_t* const r = ULONG_12(&tempman, roffs);
308
+ uint32_t const prod = static_cast<uint32_t>(*p) * static_cast<uint32_t>(*q);
309
+
310
+ #if defined _M_X64 || defined _M_ARM
311
+ // handle misalignment problems
312
+ if (i & 0x1) // i is odd
313
+ {
314
+ uint32_t sum = 0;
315
+ carry = add_uint32_carry(*ALIGN(r), prod, &sum);
316
+ *ALIGN(r) = sum;
317
+ }
318
+ else // i is even
319
+ {
320
+ carry = add_uint32_carry(*r, prod, r);
321
+ }
322
+ #else
323
+ carry = add_uint32_carry(*r, prod, r);
324
+ #endif
325
+
326
+ if (carry)
327
+ {
328
+ // roffs should be less than 8 in this case
329
+ ++*USHORT_12(&tempman, roffs + 4);
330
+ }
331
+
332
+ poffs += 2;
333
+ qoffs -= 2;
334
+ }
335
+
336
+ roffs += 2;
337
+ }
338
+
339
+ expsum -= LD_BIASM1;
340
+
341
+ // Normalize
342
+ while (static_cast<int16_t>(expsum) > 0 && (*UL_HI_12(&tempman) & MSB_ULONG) == 0)
343
+ {
344
+ shl_ld12<1>(&tempman);
345
+ expsum--;
346
+ }
347
+
348
+ if (static_cast<int16_t>(expsum) <= 0)
349
+ {
350
+ bool sticky = false;
351
+
352
+ expsum--;
353
+ while (static_cast<int16_t>(expsum) < 0)
354
+ {
355
+ if (*U_XT_12(&tempman) & 0x1)
356
+ sticky = true;
357
+
358
+ shr_ld12(&tempman);
359
+ expsum++;
360
+ }
361
+
362
+ if (sticky)
363
+ {
364
+ *U_XT_12(&tempman) |= 0x1;
365
+ }
366
+ }
367
+
368
+ if (*U_XT_12(&tempman) > 0x8000 || (*UL_LO_12(&tempman) & 0x1ffff) == 0x18000)
369
+ {
370
+ // Round up:
371
+ if (*UL_MANLO_12(&tempman) == UINT32_MAX)
372
+ {
373
+ *UL_MANLO_12(&tempman) = 0;
374
+
375
+ if (*UL_MANHI_12(&tempman) == UINT32_MAX)
376
+ {
377
+ *UL_MANHI_12(&tempman) = 0;
378
+
379
+ if (*U_EXP_12(&tempman) == UINT16_MAX)
380
+ {
381
+ // 12-byte mantissa overflow:
382
+ *U_EXP_12(&tempman) = MSB_USHORT;
383
+ ++expsum;
384
+ }
385
+ else
386
+ {
387
+ ++*U_EXP_12(&tempman);
388
+ }
389
+ }
390
+ else
391
+ {
392
+ ++*UL_MANHI_12(&tempman);
393
+ }
394
+ }
395
+ else
396
+ {
397
+ ++*UL_MANLO_12(&tempman);
398
+ }
399
+ }
400
+
401
+
402
+ // Check for exponent overflow:
403
+ if (expsum >= 0x7fff)
404
+ {
405
+ PUT_INF_12(px, sign);
406
+ return;
407
+ }
408
+
409
+ // Put result in px:
410
+ *U_XT_12 (px) = *USHORT_12(&tempman, 2);
411
+ *UL_MANLO_12(px) = *UL_MED_12(&tempman);
412
+ *UL_MANHI_12(px) = *UL_HI_12 (&tempman);
413
+ *U_EXP_12 (px) = expsum | sign;
414
+ }
415
+
416
+ // Multiplies *pld12 by 10^pow.
417
+ static __forceinline void __cdecl multiply_ten_pow_ld12(_LDBL12* const pld12, int pow) throw()
418
+ {
419
+ if (pow == 0)
420
+ return;
421
+
422
+ _LDBL12 const* pow_10p = ld12_pow10_positive - 8;
423
+ if (pow < 0)
424
+ {
425
+ pow = -pow;
426
+ pow_10p = ld12_pow10_negative-8;
427
+ }
428
+
429
+ while (pow != 0)
430
+ {
431
+ pow_10p += 7;
432
+ int const last3 = pow & 0x7; // The three least significant bits of pow
433
+ pow >>= 3;
434
+
435
+ if (last3 == 0)
436
+ continue;
437
+
438
+ _LDBL12 const* py = pow_10p + last3;
439
+
440
+ _LDBL12 unround;
441
+
442
+ // Do an exact 12byte multiplication:
443
+ if (*U_XT_12(py) >= 0x8000)
444
+ {
445
+ // Copy number:
446
+ unround = *py;
447
+
448
+ // Unround adjacent byte:
449
+ --*UL_MANLO_12(&unround);
450
+
451
+ // Point to new operand:
452
+ py = &unround;
453
+ }
454
+
455
+ multiply_ld12(pld12, py);
456
+ }
457
+ }
458
+
459
+ // Multiplies *ld12 by 2^power.
460
+ static __forceinline void __cdecl multiply_two_pow_ld12(_LDBL12* const ld12, int const power) throw()
461
+ {
462
+ _LDBL12 multiplicand{};
463
+ *U_XT_12 (&multiplicand) = 0;
464
+ *UL_MANLO_12(&multiplicand) = 0;
465
+ *UL_MANHI_12(&multiplicand) = (1u << (sizeof(uint32_t) * CHAR_BIT - 1));
466
+ *U_EXP_12 (&multiplicand) = static_cast<uint16_t>(power + LD_BIAS);
467
+
468
+ multiply_ld12(ld12, &multiplicand);
469
+ }
470
+
471
+
472
+ // These multiply a 12-byte integer stored in an _LDBL12 by N. N must be 10 or 16.
473
+ template <uint32_t N>
474
+ static __forceinline void __cdecl multiply_ld12_by(_LDBL12*) throw();
475
+
476
+ template <>
477
+ __forceinline void __cdecl multiply_ld12_by<10>(_LDBL12* const ld12) throw()
478
+ {
479
+ _LDBL12 const original_ld12 = *ld12;
480
+ shl_ld12<2>(ld12);
481
+ add_ld12(ld12, &original_ld12);
482
+ shl_ld12<1>(ld12);
483
+ }
484
+
485
+ template <>
486
+ __forceinline void __cdecl multiply_ld12_by<16>(_LDBL12* const ld12) throw()
487
+ {
488
+ shl_ld12<4>(ld12);
489
+ }
490
+
491
+ // Converts a mantissa into an _LDBL12. The mantissa to be converted must be
492
+ // represented as an array of BCD digits, one per byte, read from the byte range
493
+ // [mantissa, mantissa + mantissa_count).
494
+ template <uint32_t Base>
495
+ static __forceinline void __cdecl convert_mantissa_to_ld12(
496
+ uint8_t const* const mantissa,
497
+ size_t const mantissa_count,
498
+ _LDBL12* const ld12
499
+ ) throw()
500
+ {
501
+ *UL_LO_12 (ld12) = 0;
502
+ *UL_MED_12(ld12) = 0;
503
+ *UL_HI_12 (ld12) = 0;
504
+
505
+ uint8_t const* const mantissa_last = mantissa + mantissa_count;
506
+ for (uint8_t const* it = mantissa; it != mantissa_last; ++it)
507
+ {
508
+ multiply_ld12_by<Base>(ld12);
509
+
510
+ // Add the new digit into the mantissa:
511
+ _LDBL12 digit_ld12{};
512
+ *UL_LO_12 (&digit_ld12) = *it;
513
+ *UL_MED_12(&digit_ld12) = 0;
514
+ *UL_HI_12 (&digit_ld12) = 0;
515
+ add_ld12(ld12, &digit_ld12);
516
+ }
517
+
518
+ uint16_t expn = LD_BIASM1 + 80;
519
+
520
+ // Normalize mantissa. First shift word-by-word:
521
+ while (*UL_HI_12(ld12) == 0)
522
+ {
523
+ *UL_HI_12 (ld12) = *UL_MED_12(ld12) >> 16;
524
+ *UL_MED_12(ld12) = *UL_MED_12(ld12) << 16 | *UL_LO_12(ld12) >> 16;
525
+ *UL_LO_12 (ld12) <<= 16;
526
+ expn -= 16;
527
+ }
528
+
529
+ while ((*UL_HI_12(ld12) & MSB_USHORT) == 0)
530
+ {
531
+ shl_ld12<1>(ld12);
532
+ --expn;
533
+ }
534
+
535
+ *U_EXP_12(ld12) = expn;
536
+ }
537
+
538
+ namespace __crt_strtox {
539
+
540
+ void __cdecl assemble_floating_point_zero(bool const is_negative, _LDBL12& result) throw()
541
+ {
542
+ uint16_t const sign_bit{static_cast<uint16_t>(is_negative ? MSB_USHORT : 0x0000)};
543
+
544
+ // Zero is all zero bits with an optional sign bit:
545
+ *U_XT_12 (&result) = 0;
546
+ *UL_MANLO_12(&result) = 0;
547
+ *UL_MANHI_12(&result) = 0;
548
+ *U_EXP_12 (&result) = sign_bit;
549
+ }
550
+
551
+ void __cdecl assemble_floating_point_infinity(bool const is_negative, _LDBL12& result) throw()
552
+ {
553
+ uint16_t const sign_bit{static_cast<uint16_t>(is_negative ? MSB_USHORT : 0x0000)};
554
+
555
+ // Infinity has an all-zero mantissa and an all-one exponent
556
+ *U_XT_12 (&result) = 0;
557
+ *UL_MANLO_12(&result) = 0;
558
+ *UL_MANHI_12(&result) = 0;
559
+ *U_EXP_12 (&result) = static_cast<uint16_t>(LD_MAXEXP) | sign_bit;
560
+ }
561
+
562
+ void __cdecl assemble_floating_point_qnan(bool const is_negative, _LDBL12& result) throw()
563
+ {
564
+ uint16_t const sign_bit{static_cast<uint16_t>(is_negative ? MSB_USHORT : 0x0000)};
565
+
566
+ *U_XT_12 (&result) = 0xffff;
567
+ *UL_MANLO_12(&result) = 0xffffffff;
568
+ *UL_MANHI_12(&result) = 0xffffffff;
569
+ *U_EXP_12 (&result) = static_cast<uint16_t>(LD_MAXEXP) | sign_bit;
570
+ }
571
+
572
+ void __cdecl assemble_floating_point_snan(bool const is_negative, _LDBL12& result) throw()
573
+ {
574
+ uint16_t const sign_bit{static_cast<uint16_t>(is_negative ? MSB_USHORT : 0x0000)};
575
+
576
+ *U_XT_12 (&result) = 0xffff;
577
+ *UL_MANLO_12(&result) = 0xffffffff;
578
+ *UL_MANHI_12(&result) = 0xbfffffff;
579
+ *U_EXP_12 (&result) = static_cast<uint16_t>(LD_MAXEXP) | sign_bit;
580
+ }
581
+
582
+ void __cdecl assemble_floating_point_ind(_LDBL12& result) throw()
583
+ {
584
+ uint16_t const sign_bit{static_cast<uint16_t>(MSB_USHORT)};
585
+
586
+ *U_XT_12 (&result) = 0x0000;
587
+ *UL_MANLO_12(&result) = 0x00000000;
588
+ *UL_MANHI_12(&result) = 0xc0000000;
589
+ *U_EXP_12 (&result) = static_cast<uint16_t>(LD_MAXEXP) | sign_bit;
590
+ }
591
+
592
+ static SLD_STATUS __cdecl common_convert_to_ldbl12(
593
+ floating_point_string const& immutable_data,
594
+ bool const is_hexadecimal,
595
+ _LDBL12 & result
596
+ ) throw()
597
+ {
598
+ floating_point_string data = immutable_data;
599
+
600
+ // Cap the number of digits to LD_MAX_MAN_LEN, and round the last digit:
601
+ if (data._mantissa_count > LD_MAX_MAN_LEN)
602
+ {
603
+ if (data._mantissa[LD_MAX_MAN_LEN] >= (is_hexadecimal ? 8 : 5))
604
+ {
605
+ ++data._mantissa[LD_MAX_MAN_LEN - 1];
606
+ }
607
+
608
+ data._mantissa_count = LD_MAX_MAN_LEN;
609
+ }
610
+
611
+ // The input exponent is an adjustment from the left (so 12.3456 is represented
612
+ // as a mantiss a of 123456 with an exponent of 2), but the legacy functions
613
+ // used here expect an adjustment from the right (so 12.3456 is represented
614
+ // with an exponent of -4).
615
+ int const exponent_adjustment_multiplier = is_hexadecimal ? 4 : 1;
616
+ data._exponent -= data._mantissa_count * exponent_adjustment_multiplier;
617
+
618
+ if (is_hexadecimal)
619
+ {
620
+ convert_mantissa_to_ld12<16>(data._mantissa, data._mantissa_count, &result);
621
+ multiply_two_pow_ld12(&result, data._exponent);
622
+ }
623
+ else
624
+ {
625
+ convert_mantissa_to_ld12<10>(data._mantissa, data._mantissa_count, &result);
626
+ multiply_ten_pow_ld12(&result, data._exponent);
627
+ }
628
+
629
+ if (data._is_negative)
630
+ {
631
+ *U_EXP_12(&result) |= 0x8000;
632
+ }
633
+
634
+ // If the combination of the mantissa and the exponent produced an infinity,
635
+ // we've overflowed the range of the _LDBL12.
636
+ if ((*U_EXP_12(&result) & LD_MAXEXP) == LD_MAXEXP)
637
+ {
638
+ return SLD_OVERFLOW;
639
+ }
640
+
641
+ return SLD_OK;
642
+ }
643
+
644
+ SLD_STATUS __cdecl convert_decimal_string_to_floating_type(
645
+ floating_point_string const& data,
646
+ _LDBL12 & result
647
+ ) throw()
648
+ {
649
+ return common_convert_to_ldbl12(data, false, result);
650
+ }
651
+
652
+ SLD_STATUS __cdecl convert_hexadecimal_string_to_floating_type(
653
+ floating_point_string const& data,
654
+ _LDBL12 & result
655
+ ) throw()
656
+ {
657
+ return common_convert_to_ldbl12(data, true, result);
658
+ }
659
+
660
+ } // namespace __crt_strtox
661
+
662
+ using namespace __crt_strtox;
663
+
664
+
665
+
666
+ static int __cdecl transform_into_return_value(SLD_STATUS const status) throw()
667
+ {
668
+ switch (status)
669
+ {
670
+ case SLD_OVERFLOW: return _OVERFLOW;
671
+ case SLD_UNDERFLOW: return _UNDERFLOW;
672
+ default: return 0;
673
+ }
674
+ }
675
+
676
+ // The internal mantissa length in ints
677
+ #define INTRNMAN_LEN 3
678
+
679
+ // Internal mantissaa representation for string conversion routines
680
+ typedef uint32_t* mantissa_t;
681
+
682
+
683
+ // Tests whether a mantissa ends in nbit zeroes. Returns true if all mantissa
684
+ // bits after (and including) nbit are zero; returns false otherwise.
685
+ static __forceinline bool __cdecl mantissa_has_zero_tail(mantissa_t const mantissa, int const nbit) throw()
686
+ {
687
+ int nl = nbit / 32;
688
+ int const nb = 31 - nbit % 32;
689
+
690
+ //
691
+ // |<---- tail to be checked --->
692
+ //
693
+ // -- ------------------------ ----
694
+ // |... | | ... |
695
+ // -- ------------------------ ----
696
+ // ^ ^ ^
697
+ // | | |<----nb----->
698
+ // man nl nbit
699
+ //
700
+
701
+ uint32_t const bitmask = ~(UINT32_MAX << nb);
702
+
703
+ if (mantissa[nl] & bitmask)
704
+ return false;
705
+
706
+ ++nl;
707
+
708
+ for (; nl < INTRNMAN_LEN; ++nl)
709
+ {
710
+ if (mantissa[nl])
711
+ return false;
712
+ }
713
+
714
+ return true;
715
+ }
716
+
717
+
718
+
719
+ // Increments a mantissa. The nbit argument specifies the end of the part to
720
+ // be incremented. Returns true if overflow occurs; false otherwise.
721
+ static __forceinline bool __cdecl increment_mantissa(mantissa_t const mantissa, int const nbit) throw()
722
+ {
723
+ int nl = nbit / 32;
724
+ int const nb = 31 - nbit % 32;
725
+
726
+ //
727
+ // |<--- part to be incremented -->|
728
+ //
729
+ // ---------------------------------
730
+ // |... | | ... |
731
+ // ---------------------------------
732
+ // ^ ^ ^
733
+ // | | |<--nb-->
734
+ // man nl nbit
735
+ //
736
+
737
+ uint32_t const one = static_cast<uint32_t>(1) << nb;
738
+
739
+ bool carry = add_uint32_carry(mantissa[nl], one, &mantissa[nl]);
740
+
741
+ --nl;
742
+
743
+ for (; nl >= 0 && carry; --nl)
744
+ {
745
+ carry = add_uint32_carry(mantissa[nl], 1, &mantissa[nl]);
746
+ }
747
+
748
+ return carry;
749
+ }
750
+
751
+ // Rounds a mantissa to the given precision. Returns true if overflow occurs;
752
+ // returns false otherwise.
753
+ static __forceinline bool __cdecl round_mantissa(mantissa_t const mantissa, int const precision) throw()
754
+ {
755
+ // The order of the n'th bit is n-1, since the first bit is bit 0
756
+ // therefore decrement precision to get the order of the last bit
757
+ // to be kept
758
+
759
+ int const nbit = precision - 1;
760
+
761
+ int const rndbit = nbit + 1;
762
+
763
+ int const nl = rndbit / 32;
764
+ int const nb = 31 - rndbit % 32;
765
+
766
+ // Get value of round bit
767
+ uint32_t const rndmask = static_cast<uint32_t>(1) << nb;
768
+
769
+ bool retval = false;
770
+ if ((mantissa[nl] & rndmask) && !mantissa_has_zero_tail(mantissa, rndbit))
771
+ {
772
+ // round up
773
+ retval = increment_mantissa(mantissa, nbit);
774
+ }
775
+
776
+ // Fill rest of mantissa with zeroes
777
+ mantissa[nl] &= UINT32_MAX << nb;
778
+ for (int i = nl + 1; i < INTRNMAN_LEN; ++i)
779
+ {
780
+ mantissa[i] = 0;
781
+ }
782
+
783
+ return retval;
784
+ }
785
+
786
+ static void __cdecl convert_ld12_to_ldouble(
787
+ _LDBL12 const* const pld12,
788
+ _LDOUBLE* const result
789
+ ) throw()
790
+ {
791
+ // This implementation is based on the fact that the _LDBL12 format is
792
+ // identical to the long double and has 2 extra bytes of mantissa
793
+ uint16_t exponent = *U_EXP_12(pld12) & static_cast<uint16_t>(0x7fff);
794
+ uint16_t const sign = *U_EXP_12(pld12) & static_cast<uint16_t>(0x8000);
795
+
796
+ uint32_t mantissa[] =
797
+ {
798
+ *UL_MANHI_12(pld12),
799
+ *UL_MANLO_12(pld12),
800
+ uint32_t ((*U_XT_12(pld12)) << 16)
801
+ };
802
+
803
+ if (round_mantissa(mantissa, 64))
804
+ {
805
+ // The MSB of the mantissa is explicit and should be 1
806
+ // since we had a carry, the mantissa is now 0.
807
+ mantissa[0] = MSB_ULONG;
808
+ ++exponent;
809
+ }
810
+
811
+ *UL_MANHI_LD(result) = mantissa[0];
812
+ *UL_MANLO_LD(result) = mantissa[1];
813
+ *U_EXP_LD (result) = sign | exponent;
814
+ }
815
+
816
+ extern "C" int __cdecl _atoldbl_l(_LDOUBLE* const result, char* const string, _locale_t const locale)
817
+ {
818
+ _LocaleUpdate locale_update(locale);
819
+
820
+ _LDBL12 intermediate_result{};
821
+ SLD_STATUS const conversion_status = parse_floating_point(
822
+ locale_update.GetLocaleT(),
823
+ make_c_string_character_source(string, nullptr),
824
+ &intermediate_result);
825
+
826
+ convert_ld12_to_ldouble(&intermediate_result, result);
827
+ return transform_into_return_value(conversion_status);
828
+ }
829
+
830
+ extern "C" int __cdecl _atoldbl(_LDOUBLE* const result, char* const string)
831
+ {
832
+ return _atoldbl_l(result, string, nullptr);
833
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/atox.cpp ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // atox.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // The "simple" string conversion functions: atoi, atol, atoll, and their wide
7
+ // string functions.
8
+ //
9
+ #define _ALLOW_OLD_VALIDATE_MACROS
10
+ #include <corecrt_internal_strtox.h>
11
+ #include <stdlib.h>
12
+
13
+
14
+
15
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16
+ //
17
+ // Narrow Strings => Various Integers (Simple Functions, wtox)
18
+ //
19
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20
+ extern "C" int __cdecl atoi(char const* const string)
21
+ {
22
+ // Note: We parse as a long to avoid an extra specialization of parse_integer_from_string
23
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, nullptr);
24
+ }
25
+
26
+ extern "C" int __cdecl _atoi_l(char const* const string, _locale_t const locale)
27
+ {
28
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, locale);
29
+ }
30
+
31
+ extern "C" long __cdecl atol(char const* const string)
32
+ {
33
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, nullptr);
34
+ }
35
+
36
+ extern "C" long __cdecl _atol_l(char const* const string, _locale_t const locale)
37
+ {
38
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, locale);
39
+ }
40
+
41
+ extern "C" long long __cdecl atoll(char const* const string)
42
+ {
43
+ return __crt_strtox::parse_integer_from_string<long long>(string, nullptr, 10, nullptr);
44
+ }
45
+
46
+ extern "C" long long __cdecl _atoll_l(char const* const string, _locale_t const locale)
47
+ {
48
+ return __crt_strtox::parse_integer_from_string<long long>(string, nullptr, 10, locale);
49
+ }
50
+
51
+ extern "C" __int64 __cdecl _atoi64(char const* const string)
52
+ {
53
+ return __crt_strtox::parse_integer_from_string<__int64>(string, nullptr, 10, nullptr);
54
+ }
55
+
56
+ extern "C" __int64 __cdecl _atoi64_l(char const* const string, _locale_t const locale)
57
+ {
58
+ return __crt_strtox::parse_integer_from_string<__int64>(string, nullptr, 10, locale);
59
+ }
60
+
61
+
62
+
63
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64
+ //
65
+ // Wide Strings => Various Integers (Simple Functions, wtox)
66
+ //
67
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68
+ extern "C" int __cdecl _wtoi(wchar_t const* const string)
69
+ {
70
+ // Note: We parse as a long to avoid an extra specialization of parse_integer_from_string
71
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, nullptr);
72
+ }
73
+
74
+ extern "C" int __cdecl _wtoi_l(wchar_t const* const string, _locale_t const locale)
75
+ {
76
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, locale);
77
+ }
78
+
79
+ extern "C" long __cdecl _wtol(wchar_t const* const string)
80
+ {
81
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, nullptr);
82
+ }
83
+
84
+ extern "C" long __cdecl _wtol_l(wchar_t const* const string, _locale_t const locale)
85
+ {
86
+ return __crt_strtox::parse_integer_from_string<long>(string, nullptr, 10, locale);
87
+ }
88
+
89
+ extern "C" long long __cdecl _wtoll(wchar_t const* const string)
90
+ {
91
+ return __crt_strtox::parse_integer_from_string<long long>(string, nullptr, 10, nullptr);
92
+ }
93
+
94
+ extern "C" long long __cdecl _wtoll_l(wchar_t const* const string, _locale_t const locale)
95
+ {
96
+ return __crt_strtox::parse_integer_from_string<long long>(string, nullptr, 10, locale);
97
+ }
98
+
99
+ extern "C" __int64 __cdecl _wtoi64(wchar_t const* const string)
100
+ {
101
+ return __crt_strtox::parse_integer_from_string<__int64>(string, nullptr, 10, nullptr);
102
+ }
103
+
104
+ extern "C" __int64 __cdecl _wtoi64_l(wchar_t const* const string, _locale_t const locale)
105
+ {
106
+ return __crt_strtox::parse_integer_from_string<__int64>(string, nullptr, 10, locale);
107
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/c16rtomb.cpp ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // c16rtomb.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+
7
+ #include <corecrt_internal_mbstring.h>
8
+ #include <corecrt_internal_ptd_propagation.h>
9
+ #include <uchar.h>
10
+
11
+ using namespace __crt_mbstring;
12
+
13
+ namespace
14
+ {
15
+ inline void store_first_surrogate(char16_t c16, mbstate_t* ps)
16
+ {
17
+ // Store the first (high) surrogate masked and shifted, so that all that's left to do
18
+ // is to add the maksed second surrogate.
19
+ // Note: _Wchar is the only field used in mbstate_t for this function
20
+ const char32_t c32 = ((c16 & ~0xd800) << 10) + 0x10000;
21
+ ps->_Wchar = c32;
22
+ }
23
+
24
+ inline char32_t combine_second_surrogate(char16_t c16, mbstate_t* ps)
25
+ {
26
+ return ps->_Wchar + (c16 & ~0xdc00);
27
+ }
28
+
29
+ inline constexpr bool has_surrogate(mbstate_t* ps)
30
+ {
31
+ return ps->_Wchar != 0;
32
+ }
33
+
34
+ inline constexpr bool is_first_surrogate(char16_t c16)
35
+ {
36
+ // UTF-16 high surrogate
37
+ return 0xd800 <= c16 && c16 <= 0xdbff;
38
+ }
39
+
40
+ inline constexpr bool is_second_surrogate(char16_t c16)
41
+ {
42
+ // UTF-16 low surrogate
43
+ return 0xdc00 <= c16 && c16 <= 0xdfff;
44
+ }
45
+ }
46
+
47
+ extern "C" size_t __cdecl c16rtomb(char* s, char16_t c16, mbstate_t* ps)
48
+ {
49
+ // TODO: Bug 13307590 says this is always assuming UTF-8.
50
+ __crt_cached_ptd_host ptd;
51
+ return __c16rtomb_utf8(s, c16, ps, ptd);
52
+ }
53
+
54
+ size_t __cdecl __crt_mbstring::__c16rtomb_utf8(char* s, char16_t c16, mbstate_t* ps, __crt_cached_ptd_host& ptd)
55
+ {
56
+ static mbstate_t internal_pst{};
57
+ if (ps == nullptr)
58
+ {
59
+ ps = &internal_pst;
60
+ }
61
+
62
+ if (!has_surrogate(ps))
63
+ {
64
+ if (is_second_surrogate(c16))
65
+ {
66
+ return return_illegal_sequence(ps, ptd);
67
+ }
68
+ else if (is_first_surrogate(c16))
69
+ {
70
+ store_first_surrogate(c16, ps);
71
+ return 0;
72
+ }
73
+ else
74
+ {
75
+ return __c32rtomb_utf8(s, static_cast<char32_t>(c16), ps, ptd);
76
+ }
77
+ }
78
+ else
79
+ {
80
+ // We've already seen the first (high) surrogate, so we're
81
+ // expecting to complete this code point with its other half
82
+ // in the second (low) surrogate
83
+ if (!is_second_surrogate(c16))
84
+ {
85
+ return return_illegal_sequence(ps, ptd);
86
+ }
87
+ const char32_t c32 = combine_second_surrogate(c16, ps);
88
+
89
+ mbstate_t temp{};
90
+ const size_t retval = __c32rtomb_utf8(s, c32, &temp, ptd);
91
+ return reset_and_return(retval, ps);
92
+ }
93
+ }
94
+
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/c32rtomb.cpp ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // c32rtomb.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+
7
+ #include <corecrt_internal_mbstring.h>
8
+ #include <corecrt_internal_ptd_propagation.h>
9
+ #include <stdint.h>
10
+ #include <uchar.h>
11
+
12
+ using namespace __crt_mbstring;
13
+
14
+ extern "C" size_t __cdecl c32rtomb(char* s, char32_t c32, mbstate_t* ps)
15
+ {
16
+ // TODO: Bug 13307590 says this is always assuming UTF-8.
17
+ __crt_cached_ptd_host ptd;
18
+ return __c32rtomb_utf8(s, c32, ps, ptd);
19
+ }
20
+
21
+ size_t __cdecl __crt_mbstring::__c32rtomb_utf8(char* s, char32_t c32, mbstate_t* ps, __crt_cached_ptd_host& ptd)
22
+ {
23
+ if (!s)
24
+ {
25
+ // Equivalent to c32rtomb(buf, U'\0', ps) for some internal buffer buf
26
+ *ps = {};
27
+ return 1;
28
+ }
29
+
30
+ if (c32 == U'\0')
31
+ {
32
+ *s = '\0';
33
+ *ps = {};
34
+ return 1;
35
+ }
36
+
37
+ // Fast path for ASCII
38
+ if ((c32 & ~0x7f) == 0)
39
+ {
40
+ *s = static_cast<char>(c32);
41
+ return 1;
42
+ }
43
+
44
+ // Figure out how many trail bytes we need
45
+ size_t trail_bytes;
46
+ uint8_t lead_byte;
47
+ if ((c32 & ~0x7ff) == 0)
48
+ {
49
+ trail_bytes = 1;
50
+ lead_byte = 0xc0;
51
+ }
52
+ else if ((c32 & ~0xffff) == 0)
53
+ {
54
+ // high/low surrogates are only valid in UTF-16 encoded data
55
+ if (0xd800 <= c32 && c32 <= 0xdfff)
56
+ {
57
+ return return_illegal_sequence(ps, ptd);
58
+ }
59
+ trail_bytes = 2;
60
+ lead_byte = 0xe0;
61
+ }
62
+ else if ((c32 & ~0x001fffff) == 0)
63
+ {
64
+ // Unicode's max code point is 0x10ffff
65
+ if (0x10ffff < c32)
66
+ {
67
+ return return_illegal_sequence(ps, ptd);
68
+ }
69
+ trail_bytes = 3;
70
+ lead_byte = 0xf0;
71
+ }
72
+ else
73
+ {
74
+ return return_illegal_sequence(ps, ptd);
75
+ }
76
+ _ASSERTE(1 <= trail_bytes && trail_bytes <= 3);
77
+
78
+ // Put six bits into each of the trail bytes
79
+ // Lowest bits are in the last UTF-8 byte.
80
+ // Filling back to front.
81
+ for (size_t i = trail_bytes; i > 0; --i)
82
+ {
83
+ s[i] = (c32 & 0x3f) | 0x80;
84
+ c32 >>= 6;
85
+ }
86
+
87
+ // The first byte needs the upper (trail_bytes + 1) bits to store the length
88
+ // And the lower (7 - trail_bytes) to store the upper bits of the code point
89
+ _ASSERTE(c32 < (1u << (7 - trail_bytes)));
90
+ s[0] = static_cast<uint8_t>(c32) | lead_byte;
91
+
92
+ return reset_and_return(trail_bytes + 1, ps);
93
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/cfout.cpp ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cfout.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Floating point binary to decimal conversion routines
7
+ //
8
+ #include <corecrt_internal_fltintrn.h>
9
+ #include <corecrt_internal_big_integer.h>
10
+ #include <fenv.h>
11
+ #include <string.h>
12
+
13
+
14
+
15
+ using namespace __crt_strtox;
16
+
17
+ namespace
18
+ {
19
+ // Guard class for floating point control word modifications in the interrupt exception mask.
20
+ class fp_control_word_guard
21
+ {
22
+ public:
23
+ explicit fp_control_word_guard(unsigned int const mask = ~0u) : _mask(mask)
24
+ {
25
+ _controlfp_s(&_original_control_word, 0, 0);
26
+ }
27
+
28
+ fp_control_word_guard(unsigned int const new_control, unsigned int const mask) : _mask(mask)
29
+ {
30
+ unsigned int float_control;
31
+ _controlfp_s(&_original_control_word, 0, 0);
32
+ _controlfp_s(&float_control, new_control, _mask);
33
+ }
34
+
35
+ ~fp_control_word_guard()
36
+ {
37
+ unsigned int reset_cw;
38
+ _controlfp_s(&reset_cw, _original_control_word, _mask);
39
+ }
40
+
41
+ private:
42
+ unsigned int _original_control_word;
43
+ unsigned int _mask;
44
+ };
45
+
46
+ class scoped_fp_state_reset
47
+ {
48
+ public:
49
+
50
+ scoped_fp_state_reset() throw()
51
+ {
52
+ // The calls to feholdexcept and fesetenv are relatively expensive,
53
+ // so we only call them if there are any unmasked exceptions.
54
+ fegetenv(&_environment);
55
+ if ((_environment._Fe_ctl & FE_ALL_EXCEPT) == FE_ALL_EXCEPT)
56
+ {
57
+ _requires_reset = false;
58
+ }
59
+ else
60
+ {
61
+ feholdexcept(&_environment);
62
+ _requires_reset = true;
63
+ }
64
+ }
65
+
66
+ ~scoped_fp_state_reset() throw()
67
+ {
68
+ if (_requires_reset)
69
+ {
70
+ fesetenv(&_environment);
71
+ }
72
+ }
73
+
74
+ private:
75
+
76
+ scoped_fp_state_reset(scoped_fp_state_reset const&);
77
+ scoped_fp_state_reset& operator=(scoped_fp_state_reset const&);
78
+
79
+ fenv_t _environment;
80
+ bool _requires_reset;
81
+ };
82
+ }
83
+
84
+
85
+
86
+ // This function converts a finite, positive floating point value into its
87
+ // decimal representation. The decimal mantissa and exponent are returned
88
+ // via the out parameters and the return value affirms if there were trailing
89
+ // digits. If the value is zero, negative, infinity, or nan, the behavior is
90
+ // undefined.
91
+ //
92
+ // This function is based on the digit generation algorithm described in the
93
+ // paper "Printing floating point numbers quickly and accurately," by Robert G.
94
+ // Burger and R. Kent Dybvig, from ACM SIGPLAN 1996 Conference on Programming
95
+ // Language Design and Implementation, June 1996. This function deviates from
96
+ // that algorithm in its termination conditions: the Burger-Dybvig algorithm
97
+ // terminates when it runs out of significant digits; this function continues
98
+ // generating digits, even if they are insignificant (this allows us to print
99
+ // large, exactly representable values exactly, e.g. large powers of two).
100
+ //
101
+ // This function stops generating digits when the first of the following
102
+ // conditions is true: [1] the mantissa buffer is exhausted, [2] sufficient
103
+ // digits have been generated for a %f specifier with the requested precision,
104
+ // or [3] all remaining digits are known to be zero.
105
+ template <typename FloatingType>
106
+ __forceinline static __acrt_has_trailing_digits __cdecl convert_to_fos_high_precision(
107
+ FloatingType const value,
108
+ uint32_t const precision,
109
+ __acrt_precision_style const precision_style,
110
+ int* const exponent,
111
+ char* const mantissa_buffer,
112
+ size_t const mantissa_buffer_count
113
+ ) throw()
114
+ {
115
+ using floating_traits = __acrt_floating_type_traits<FloatingType>;
116
+ using components_type = typename floating_traits::components_type;
117
+
118
+ _ASSERTE(mantissa_buffer_count > 0);
119
+
120
+ components_type const& value_components = reinterpret_cast<components_type const&>(value);
121
+
122
+ // Special handling is required for denormal values: because the implicit
123
+ // high order bit is a zero, not a one, we need to [1] not set that bit when
124
+ // we expand the mantissa, and [2] increment the exponent to account for the
125
+ // extra shift.
126
+ bool const is_denormal = value_components._exponent == 0;
127
+
128
+ uint64_t const mantissa_adjustment = is_denormal
129
+ ? 0
130
+ : static_cast<uint64_t>(1) << (floating_traits::mantissa_bits - 1);
131
+
132
+ int32_t const exponent_adjustment = is_denormal
133
+ ? 2
134
+ : 1;
135
+
136
+ // f and e are the unbiased mantissa and exponent, respectively. (Where one-
137
+ // -letter variable names are used, they match the same variables from the
138
+ // Burger-Dybvig paper.)
139
+ uint64_t const f = value_components._mantissa + mantissa_adjustment;
140
+ int32_t const e =
141
+ static_cast<int32_t>(value_components._exponent) -
142
+ floating_traits::exponent_bias -
143
+ floating_traits::mantissa_bits +
144
+ exponent_adjustment;
145
+
146
+ // k is the decimal exponent, such that the resulting decimal number is of
147
+ // the form 0.mmmmm * 10^k, where mmmm are the mantissa digits we generate.
148
+ // Note that the use of log10 here may not give the correct result: it may
149
+ // be off by one, e.g. as is the case for powers of ten (log10(100) is two,
150
+ // but the correct value of k for 100 is 3). We detect off-by-one errors
151
+ // later and correct for them.
152
+ int32_t k = static_cast<int32_t>(ceil(log10(value)));
153
+ if (k == INT32_MAX || k == INT32_MIN)
154
+ {
155
+ _ASSERTE(("unexpected input value; log10 failed", 0));
156
+ k = 0;
157
+ }
158
+
159
+ // The floating point number is represented as a fraction, r / s. The
160
+ // initialization of these two values is as described in the Burger-Dybvig
161
+ // paper.
162
+ big_integer r = make_big_integer(f);
163
+ big_integer s{};
164
+
165
+ if (e >= 0)
166
+ {
167
+ if (r != make_big_integer_power_of_two(floating_traits::mantissa_bits - 1))
168
+ {
169
+ shift_left(r, e + 1); // f * b^e * 2
170
+ s = make_big_integer(2); // 2
171
+ }
172
+ else
173
+ {
174
+ shift_left(r, e + 2); // f * b^(e+1) * 2
175
+ s = make_big_integer(4); // b * 2
176
+ }
177
+ }
178
+ else
179
+ {
180
+ if (e == floating_traits::minimum_binary_exponent ||
181
+ r != make_big_integer_power_of_two(floating_traits::mantissa_bits - 1))
182
+ {
183
+ shift_left(r, 1); // f * 2
184
+ s = make_big_integer_power_of_two(-e + 1); // b^-e * 2
185
+ }
186
+ else
187
+ {
188
+ shift_left(r, 2); // f * b * 2
189
+ s = make_big_integer_power_of_two(-e + 2); // b^(-e+1) * 2
190
+ }
191
+ }
192
+
193
+ if (k >= 0)
194
+ {
195
+ multiply_by_power_of_ten(s, k);
196
+ }
197
+ else
198
+ {
199
+ multiply_by_power_of_ten(r, -k);
200
+ }
201
+
202
+ char* mantissa_it = mantissa_buffer;
203
+
204
+ // Perform a trial digit generation to handle off-by-one errors in the
205
+ // computation of 'k': There are three possible cases, which we handle
206
+ // in turn:
207
+ multiply(r, 10);
208
+ uint32_t const initial_digit = static_cast<uint32_t>(divide(r, s));
209
+
210
+ // If the initial digit was computed as 10, k is too small. We increment k
211
+ // and adjust s as if it had been computed with the correct k above. We
212
+ // then treat the digit as a one, which is what it would have been extracted
213
+ // as had k, r, and s been correct to begin with.
214
+ if (initial_digit == 10)
215
+ {
216
+ ++k;
217
+ *mantissa_it++ = '1';
218
+ multiply(s, 10);
219
+ }
220
+ // If the initial digit is zero, k is too large. We decrement k and ignore
221
+ // the zero that we read (the next digit that we read will be the "real"
222
+ // initial digit.
223
+ else if (initial_digit == 0)
224
+ {
225
+ --k;
226
+ }
227
+ // Otherwise, k was correct and the digit we read was the actual initial
228
+ // digit of the number; just store it.
229
+ else
230
+ {
231
+ *mantissa_it++ = static_cast<char>('0' + initial_digit);
232
+ }
233
+
234
+ *exponent = k; // k is now correct; store it for our caller
235
+
236
+ // convert_to_fos_high_precision() generates digits assuming we're formatting with %f
237
+ // When %e is the format specifier, adding the exponent to the number of required digits
238
+ // is not needed.
239
+ uint32_t const required_digits = k >= 0 && precision <= INT_MAX && precision_style == __acrt_precision_style::fixed
240
+ ? k + precision
241
+ : precision;
242
+
243
+ char* const mantissa_last = mantissa_buffer + __min(mantissa_buffer_count - 1, required_digits);
244
+
245
+ // We must track whether there are any non-zero digits that were not written to the mantissa,
246
+ // since just checking whether 'r' is zero is insufficient for knowing whether there are any
247
+ // remaining zeros after the generated digits.
248
+ bool unwritten_nonzero_digits_in_chunk = false;
249
+ for (;;)
250
+ {
251
+ if (mantissa_it == mantissa_last)
252
+ {
253
+ break;
254
+ }
255
+
256
+ if (is_zero(r))
257
+ {
258
+ break;
259
+ }
260
+
261
+ // To reduce the number of expensive high precision division operations,
262
+ // we generate multiple digits per iteration. Our quotient type is a
263
+ // uint32_t, and the largest power of ten representable by a uint32_t is
264
+ // 10^9, so we generate nine digits per iteration.
265
+ uint32_t const digits_per_iteration = 9;
266
+ uint32_t const digits_per_iteration_multiplier = 1000 * 1000 * 1000;
267
+
268
+ multiply(r, digits_per_iteration_multiplier);
269
+ uint32_t quotient = static_cast<uint32_t>(divide(r, s));
270
+
271
+ _ASSERTE(quotient < digits_per_iteration_multiplier);
272
+
273
+ // Decompose the quotient into its nine component digits by repeatedly
274
+ // dividing by ten. This generates digits in reverse order.
275
+ #pragma warning(suppress: 6293) // For-loop counts down from minimum
276
+ for (uint32_t i = digits_per_iteration - 1; i != static_cast<uint32_t>(-1); --i)
277
+ {
278
+ char const d = static_cast<char>('0' + quotient % 10);
279
+ quotient /= 10;
280
+
281
+ // We may not have room in the mantissa buffer for all of the digits.
282
+ // Ignore the ones for which we do not have room.
283
+ // Last value in mantissa must be null terminator, account for one place after digit generation.
284
+ if (static_cast<uint32_t>(mantissa_last - mantissa_it) <= i)
285
+ {
286
+ if (d != '0')
287
+ {
288
+ unwritten_nonzero_digits_in_chunk = true;
289
+ }
290
+
291
+ continue;
292
+ }
293
+
294
+ mantissa_it[i] = d;
295
+ }
296
+
297
+ mantissa_it += __min(digits_per_iteration, mantissa_last - mantissa_it);
298
+ }
299
+
300
+ *mantissa_it = '\0';
301
+
302
+ // To detect whether there are zeros after the generated digits for the purposes of rounding,
303
+ // we must check whether 'r' is zero, but also whether there were any non-zero numbers that were
304
+ // not written to the mantissa in the last evaluated chunk.
305
+ bool const all_zeros_after_chunk = is_zero(r);
306
+
307
+ if (all_zeros_after_chunk && !unwritten_nonzero_digits_in_chunk)
308
+ {
309
+ return __acrt_has_trailing_digits::no_trailing;
310
+ }
311
+
312
+ return __acrt_has_trailing_digits::trailing;
313
+ }
314
+
315
+ extern "C" __acrt_has_trailing_digits __cdecl __acrt_fltout(
316
+ _CRT_DOUBLE value,
317
+ unsigned const precision,
318
+ __acrt_precision_style const precision_style,
319
+ STRFLT const flt,
320
+ char* const result,
321
+ size_t const result_count
322
+ )
323
+ {
324
+ using floating_traits = __acrt_floating_type_traits<double>;
325
+ using components_type = floating_traits::components_type;
326
+
327
+ scoped_fp_state_reset const reset_fp_state;
328
+
329
+ components_type& components = reinterpret_cast<components_type&>(value);
330
+
331
+ flt->sign = components._sign == 1 ? '-' : ' ';
332
+ flt->mantissa = result;
333
+
334
+ unsigned int float_control;
335
+ _controlfp_s(&float_control, 0, 0);
336
+ bool const value_is_zero = components._exponent == 0 && (components._mantissa == 0 || float_control & _DN_FLUSH);
337
+ if (value_is_zero)
338
+ {
339
+ flt->decpt = 0;
340
+ _ERRCHECK(strcpy_s(result, result_count, "0"));
341
+ return __acrt_has_trailing_digits::no_trailing;
342
+ }
343
+
344
+ // Handle special cases:
345
+ __acrt_fp_class const classification = __acrt_fp_classify(value.x);
346
+ if (classification != __acrt_fp_class::finite)
347
+ {
348
+ flt->decpt = 1;
349
+ }
350
+
351
+ switch (classification)
352
+ {
353
+ case __acrt_fp_class::infinity: _ERRCHECK(strcpy_s(result, result_count, "1#INF" )); return __acrt_has_trailing_digits::trailing;
354
+ case __acrt_fp_class::quiet_nan: _ERRCHECK(strcpy_s(result, result_count, "1#QNAN")); return __acrt_has_trailing_digits::no_trailing;
355
+ case __acrt_fp_class::signaling_nan: _ERRCHECK(strcpy_s(result, result_count, "1#SNAN")); return __acrt_has_trailing_digits::no_trailing;
356
+ case __acrt_fp_class::indeterminate: _ERRCHECK(strcpy_s(result, result_count, "1#IND" )); return __acrt_has_trailing_digits::no_trailing;
357
+ }
358
+
359
+ // Make the number positive before we pass it to the digit generator:
360
+ components._sign = 0;
361
+
362
+ // The digit generator produces a truncated sequence of digits. To allow
363
+ // our caller to correctly round the mantissa, we need to generate an extra
364
+ // digit.
365
+ fp_control_word_guard const fpc(_MCW_EM, _MCW_EM);
366
+ return convert_to_fos_high_precision(value.x, precision + 1, precision_style, &flt->decpt, result, result_count);
367
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/common_utf8.cpp ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // common_utf8.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Common UTF-8 utilities
7
+ //
8
+
9
+ #include <corecrt_internal.h>
10
+ #include <corecrt_internal_mbstring.h>
11
+ #include <corecrt_internal_ptd_propagation.h>
12
+
13
+ namespace __crt_mbstring
14
+ {
15
+ size_t return_illegal_sequence(mbstate_t* ps, __crt_cached_ptd_host& ptd)
16
+ {
17
+ *ps = {};
18
+ ptd.get_errno().set(EILSEQ);
19
+ return INVALID;
20
+ }
21
+
22
+ size_t reset_and_return(size_t retval, mbstate_t* ps)
23
+ {
24
+ *ps = {};
25
+ return retval;
26
+ }
27
+ }
msvc/Windows Kits/10/Source/10.0.26100.0/ucrt/convert/cvt.cpp ADDED
@@ -0,0 +1,849 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // cvt.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Functions for formatting floating point values with the %a, %e, %f, and %g
7
+ // printf format specifiers.
8
+ //
9
+ #include <corecrt_internal.h>
10
+ #include <corecrt_internal_ptd_propagation.h>
11
+ #include <ctype.h>
12
+ #include <string.h>
13
+ #include <math.h>
14
+ #include <locale.h>
15
+ #include <corecrt_internal_fltintrn.h>
16
+ #include <fenv.h>
17
+ #include <float.h>
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <stddef.h>
21
+
22
+ // The C String pointed to by string is shifted distance bytes to the right.
23
+ // If distance is negative, the string is shifted to the left.
24
+ // The C String pointed to by string and all shifting operations must be
25
+ // contained within buffer_base or buffer_count.
26
+ static void __cdecl shift_bytes(
27
+ _Maybe_unsafe_(_Inout_updates_z_, buffer_count) char * const buffer_base,
28
+ _In_ size_t const buffer_count,
29
+ _In_range_(buffer_base, buffer_base + buffer_count) char* const string,
30
+ _In_ int const distance
31
+ ) throw()
32
+ {
33
+ UNREFERENCED_PARAMETER(buffer_base);
34
+ UNREFERENCED_PARAMETER(buffer_count);
35
+
36
+ if (distance != 0)
37
+ {
38
+ memmove(string + distance, string, strlen(string) + 1);
39
+ }
40
+ }
41
+
42
+
43
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44
+ //
45
+ // NaN and Infinity Formatting
46
+ //
47
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48
+ _Success_(return == 0)
49
+ static errno_t __cdecl fp_format_nan_or_infinity(
50
+ _In_ __acrt_fp_class const classification,
51
+ _In_range_(0,1) bool const is_negative,
52
+ _Maybe_unsafe_(_Out_writes_z_, result_buffer_count) char* result_buffer,
53
+ _In_range_(1,SIZE_MAX) size_t result_buffer_count,
54
+ _In_range_(0,1) bool const use_capitals
55
+ ) throw()
56
+ {
57
+ using floating_traits = __acrt_floating_type_traits<double>;
58
+ using components_type = floating_traits::components_type;
59
+
60
+ // Ensure that we have sufficient space to store at least the basic three-
61
+ // character INF or NAN string, plus the minus sign if required:
62
+ if (result_buffer_count < _countof("INF") + is_negative)
63
+ {
64
+ *result_buffer = '\0';
65
+ return ENOMEM;
66
+ }
67
+
68
+ if (is_negative)
69
+ {
70
+ *result_buffer++ = '-';
71
+ *result_buffer = '\0';
72
+ if (result_buffer_count != _CRT_UNBOUNDED_BUFFER_SIZE)
73
+ {
74
+ --result_buffer_count;
75
+ }
76
+ }
77
+
78
+ static char const* const strings[][4] =
79
+ {
80
+ { "INF", "INF", "inf", "inf" }, // Infinity
81
+ { "NAN", "NAN", "nan", "nan" }, // Quiet NAN
82
+ { "NAN(SNAN)", "NAN", "nan(snan)", "nan" }, // Signaling NAN
83
+ { "NAN(IND)", "NAN", "nan(ind)", "nan" }, // Indeterminate
84
+ };
85
+
86
+ uint32_t const row = static_cast<uint32_t>(classification) - 1;
87
+ uint32_t const column = use_capitals ? 0 : 2;
88
+
89
+ bool const long_string_will_fit = result_buffer_count > strlen(strings[row][column]);
90
+ _ERRCHECK(strcpy_s(
91
+ result_buffer,
92
+ result_buffer_count,
93
+ strings[row][column + !long_string_will_fit]));
94
+ return 0;
95
+ }
96
+
97
+
98
+
99
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
+ //
101
+ // %e formatting
102
+ //
103
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104
+ // These functions handle the formatting of floating point values in the %e
105
+ // printf format. This format has the form [-]d.ddde(+/-)ddd, where there will
106
+ // be 'precision' digits following the decimal point. If the precision is less
107
+ // than or equal to zero, no decimal point will appear. The low order digit is
108
+ // rounded. If 'capitals' is true, then the exponent will appear as E(+/-)ddd.
109
+ _Success_(return == 0)
110
+ static errno_t fp_format_e_internal(
111
+ _Maybe_unsafe_(_Inout_updates_z_, result_buffer_count) char* const result_buffer,
112
+ _In_fits_precision_(precision) size_t const result_buffer_count,
113
+ _In_ int const precision,
114
+ _In_ bool const capitals,
115
+ _In_ unsigned const min_exponent_digits,
116
+ _In_ STRFLT const pflt,
117
+ _In_ bool const g_fmt,
118
+ _Inout_ __crt_cached_ptd_host& ptd
119
+ ) throw()
120
+ {
121
+ // The max length if calculated like this:
122
+ // 3 = sign + first digit + decimal point
123
+ // precision = decimal digits
124
+ // 5 = exponent letter (e or E), exponent sign, three digits exponent
125
+ // 1 = extra space for rounding
126
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, result_buffer_count > static_cast<size_t>(3 + (precision > 0 ? precision : 0) + 5 + 1), ERANGE);
127
+
128
+ // Place the output in the buffer and round. Leave space in the buffer
129
+ // for the '-' sign (if any) and the decimal point (if any):
130
+ if (g_fmt)
131
+ {
132
+ // Shift it right one place if nec. for decimal point:
133
+ char* const p = result_buffer + (pflt->sign == '-');
134
+ shift_bytes(result_buffer, result_buffer_count, p, precision > 0);
135
+ }
136
+
137
+ // Now fix the number up to be in e format:
138
+ char* p = result_buffer;
139
+
140
+ // Put in negative sign if needed:
141
+ if (pflt->sign == '-')
142
+ *p++ = '-';
143
+
144
+ // Put in decimal point if needed. Copy the first digit to the place left
145
+ // for it and put the decimal point in its place:
146
+ if (precision > 0)
147
+ {
148
+ *p = *(p + 1);
149
+ *++p = *ptd.get_locale()->locinfo->lconv->decimal_point;
150
+ }
151
+
152
+ // Find the end of the string, attach the exponent field and save the
153
+ // exponent position:
154
+ p = p + precision + (g_fmt ? 0 : 1);
155
+ _ERRCHECK(strcpy_s(
156
+ p,
157
+ result_buffer_count == _CRT_UNBOUNDED_BUFFER_SIZE
158
+ ? result_buffer_count
159
+ : result_buffer_count - (p - result_buffer), "e+000"));
160
+ char* exponentpos = p + 2;
161
+
162
+ // Adjust exponent indicator according to capitals flag and increment
163
+ // pointer to point to exponent sign:
164
+ if (capitals)
165
+ *p = 'E';
166
+
167
+ ++p;
168
+
169
+ // If mantissa is zero, then the number is 0 and we are done; otherwise
170
+ // adjust the exponent sign (if necessary) and value:
171
+ if (*pflt->mantissa != '0')
172
+ {
173
+ // Check to see if exponent is negative; if so adjust exponent sign and
174
+ // exponent value:
175
+ int exp = pflt->decpt - 1;
176
+ if (exp < 0)
177
+ {
178
+ exp = -exp;
179
+ *p = '-';
180
+ }
181
+
182
+ ++p;
183
+
184
+ if (exp >= 100)
185
+ {
186
+ *p += static_cast<char>(exp / 100);
187
+ exp %= 100;
188
+ }
189
+
190
+ ++p;
191
+
192
+ if (exp >= 10)
193
+ {
194
+ *p += static_cast<char>(exp / 10);
195
+ exp %= 10;
196
+ }
197
+
198
+ *++p += static_cast<char>(exp);
199
+ }
200
+
201
+ if (min_exponent_digits == 2)
202
+ {
203
+ // If possible, reduce the exponent to two digits:
204
+ if (*exponentpos == '0')
205
+ {
206
+ memmove(exponentpos, exponentpos + 1, 3);
207
+ }
208
+ }
209
+
210
+ return 0;
211
+ }
212
+
213
+ _Success_(return == 0)
214
+ static errno_t __cdecl fp_format_e(
215
+ _In_ double const* const argument,
216
+ _Maybe_unsafe_(_Inout_updates_z_, result_buffer_count) char* const result_buffer,
217
+ _In_fits_precision_(precision) size_t const result_buffer_count,
218
+ _Out_writes_(scratch_buffer_count) char* const scratch_buffer,
219
+ _In_ size_t const scratch_buffer_count,
220
+ _In_ int const precision,
221
+ _In_ bool const capitals,
222
+ _In_ unsigned const min_exponent_digits,
223
+ _In_ __acrt_rounding_mode const rounding_mode,
224
+ _Inout_ __crt_cached_ptd_host& ptd
225
+ ) throw()
226
+ {
227
+ // The precision passed to __acrt_fltout is the number of fractional digits.
228
+ // To ensure that we get enough digits, we require a total of precision + 1 digits,
229
+ // to account for the digit placed to the left of the decimal point when all digits are fractional.
230
+ _strflt strflt;
231
+
232
+ __acrt_has_trailing_digits const trailing_digits = __acrt_fltout(
233
+ *reinterpret_cast<_CRT_DOUBLE const*>(argument),
234
+ precision + 1,
235
+ __acrt_precision_style::scientific,
236
+ &strflt,
237
+ scratch_buffer,
238
+ scratch_buffer_count
239
+ );
240
+
241
+ errno_t const e = __acrt_fp_strflt_to_string(
242
+ result_buffer + (strflt.sign == '-') + (precision > 0),
243
+ (result_buffer_count == _CRT_UNBOUNDED_BUFFER_SIZE
244
+ ? result_buffer_count
245
+ : result_buffer_count - (strflt.sign == '-') - (precision > 0)),
246
+ precision + 1,
247
+ &strflt,
248
+ trailing_digits,
249
+ rounding_mode,
250
+ ptd);
251
+
252
+ if (e != 0)
253
+ {
254
+ result_buffer[0] = '\0';
255
+ return e;
256
+ }
257
+
258
+ return fp_format_e_internal(result_buffer, result_buffer_count, precision, capitals, min_exponent_digits, &strflt, false, ptd);
259
+ }
260
+
261
+ static bool fe_to_nearest(double const* const argument, unsigned __int64 const mask, short const maskpos)
262
+ {
263
+ using floating_traits = __acrt_floating_type_traits<double>;
264
+ using components_type = floating_traits::components_type;
265
+ components_type const* const components = reinterpret_cast<components_type const*>(argument);
266
+
267
+ unsigned short digit = static_cast<unsigned short>((components->_mantissa & mask) >> maskpos);
268
+
269
+ if (digit > 8)
270
+ {
271
+ return true;
272
+ }
273
+
274
+ if (digit < 8)
275
+ {
276
+ return false;
277
+ }
278
+
279
+ unsigned __int64 const roundBitsMask = (static_cast<unsigned __int64>(1) << maskpos) - 1;
280
+ if (components->_mantissa & roundBitsMask)
281
+ {
282
+ return true;
283
+ }
284
+
285
+ //if we still have digits to the left to compare
286
+ if (maskpos != DBL_MANT_DIG - 5)
287
+ {
288
+ // We divide the mantisa by 16 to move the digits to the right, after that we apply the mask
289
+ // to get the digit at the left.
290
+ digit = static_cast<unsigned short>(((components->_mantissa / 16) & mask) >> maskpos);
291
+ }
292
+ else
293
+ {
294
+ digit = components->_exponent == 0 ? 0 : 1;
295
+ }
296
+
297
+ return digit % 2 == 1;
298
+ }
299
+
300
+ static bool should_round_up(double const* const argument, unsigned __int64 const mask, short const maskpos, __acrt_rounding_mode const rounding_mode)
301
+ {
302
+ using floating_traits = __acrt_floating_type_traits<double>;
303
+ using components_type = floating_traits::components_type;
304
+ components_type const* const components = reinterpret_cast<components_type const*>(argument);
305
+
306
+ unsigned short const digit = static_cast<unsigned short>((components->_mantissa & mask) >> maskpos);
307
+
308
+ if (rounding_mode == __acrt_rounding_mode::legacy)
309
+ {
310
+ return digit >= 8;
311
+ }
312
+ int const round_mode = fegetround();
313
+
314
+ if (round_mode == FE_TONEAREST)
315
+ {
316
+ return fe_to_nearest(argument, mask, maskpos);
317
+ }
318
+
319
+ if (round_mode == FE_UPWARD)
320
+ {
321
+ return digit != 0 && !components->_sign;
322
+ }
323
+
324
+ if (round_mode == FE_DOWNWARD)
325
+ {
326
+ return digit != 0 && components->_sign;
327
+ }
328
+
329
+ return false;
330
+ }
331
+
332
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
333
+ //
334
+ // %a formatting
335
+ //
336
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337
+ // These functions handle the formatting of floating point values in the %a
338
+ // printf format. This format has the form [-]0xh.hhhhp(+/-)d, where there will
339
+ // be 'precision' hex digits following the decimal point. If the precision is
340
+ // less than or equal to zero, no decimal point will appear. If 'capitals' is
341
+ // true, then the number will appear as [-]0xH.HHHHP(+/-)d.
342
+ _Success_(return == 0)
343
+ static errno_t __cdecl fp_format_a(
344
+ _In_ double const* const argument,
345
+ _Maybe_unsafe_(_Inout_updates_z_, result_buffer_count) char* result_buffer,
346
+ _In_fits_precision_(precision) size_t const result_buffer_count,
347
+ _Out_writes_(scratch_buffer_count) char* const scratch_buffer,
348
+ _In_ size_t const scratch_buffer_count,
349
+ _In_ int precision,
350
+ _In_ bool const capitals,
351
+ _In_ unsigned const min_exponent_digits,
352
+ _In_ __acrt_rounding_mode const rounding_mode,
353
+ _Inout_ __crt_cached_ptd_host& ptd
354
+ )
355
+ {
356
+ using floating_traits = __acrt_floating_type_traits<double>;
357
+ using components_type = floating_traits::components_type;
358
+
359
+ if (precision < 0)
360
+ {
361
+ precision = 0;
362
+ }
363
+
364
+ result_buffer[0] = '\0';
365
+
366
+ // the constraint for the size of buffer is:
367
+ // 1 (sign)
368
+ // 4 ("0xh.")
369
+ // precision (decimal digits)
370
+ // 6 ("p+0000")
371
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, result_buffer_count > static_cast<size_t>(1 + 4 + precision + 6), ERANGE);
372
+
373
+ // Let __acrt_fp_format_e handle the special cases like SNAN, etc.:
374
+ components_type const* const components = reinterpret_cast<components_type const*>(argument);
375
+ if (components->_exponent == floating_traits::exponent_mask)
376
+ {
377
+ errno_t const e = fp_format_e(
378
+ argument,
379
+ result_buffer,
380
+ result_buffer_count,
381
+ scratch_buffer,
382
+ scratch_buffer_count,
383
+ precision,
384
+ false,
385
+ min_exponent_digits,
386
+ rounding_mode,
387
+ ptd);
388
+
389
+ if (e != 0)
390
+ {
391
+ // An error occurred
392
+ result_buffer[0] = '\0';
393
+ return e;
394
+ }
395
+
396
+ // Substitute the e with p:
397
+ char* p = strrchr(result_buffer, 'e');
398
+ if (p)
399
+ {
400
+ *p = capitals ? 'P' : 'p';
401
+
402
+ // Trim the exponent (which is 0) to only one digit; skip the
403
+ // exponent sign and the first digit and put the terminating 0:
404
+ p += 3;
405
+ *p = 0;
406
+ }
407
+ return e;
408
+ }
409
+
410
+ // Sign:
411
+ if (components->_sign)
412
+ {
413
+ *result_buffer++ = '-';
414
+ }
415
+
416
+ int const hexadd = (capitals ? 'A' : 'a') - '9' - 1;
417
+
418
+ // Leading digit (and set the debias):
419
+ unsigned __int64 debias = floating_traits::exponent_bias;
420
+ if (components->_exponent == 0)
421
+ {
422
+ *result_buffer++ = '0';
423
+ if (components->_mantissa == 0)
424
+ {
425
+ // Zero:
426
+ debias = 0;
427
+ }
428
+ else
429
+ {
430
+ // Denormal:
431
+ debias--;
432
+ }
433
+ }
434
+ else
435
+ {
436
+ *result_buffer++ = '1';
437
+ }
438
+
439
+ // Decimal point (save the position in pos):
440
+ char* pos = result_buffer++;
441
+ if (precision == 0)
442
+ {
443
+ // If precision is 0, then we don't have to print the decimal point:
444
+ // we mark this putting 0 instead of the decimal point itself
445
+ *pos = 0;
446
+ }
447
+ else
448
+ {
449
+ *pos = *ptd.get_locale()->locinfo->lconv->decimal_point;
450
+ }
451
+
452
+ // Mantissa:
453
+ if (components->_mantissa > 0)
454
+ {
455
+ // Print 4 bits at a time, and skip the initial zeroes
456
+ // Prepare the mask to read the first 4 bits
457
+ short maskpos = (floating_traits::mantissa_bits - 1) - 4;
458
+
459
+ unsigned __int64 mask = 0xf;
460
+ mask <<= maskpos;
461
+
462
+ while (maskpos >= 0 && precision > 0)
463
+ {
464
+ unsigned short digit = static_cast<unsigned short>((components->_mantissa & mask) >> maskpos);
465
+ digit += '0';
466
+ if (digit > '9')
467
+ {
468
+ digit += static_cast<unsigned short>(hexadd);
469
+ }
470
+ *result_buffer++ = static_cast<char>(digit);
471
+ mask >>= 4;
472
+ maskpos -= 4;
473
+ --precision;
474
+ }
475
+
476
+ // Round the mantissa:
477
+ if (maskpos >= 0)
478
+ {
479
+ if (should_round_up(argument, mask, maskpos, rounding_mode))
480
+ {
481
+ char* p = result_buffer;
482
+ --p;
483
+ // If the last digit is 'f', we need to add one to the previous
484
+ // digit, too; pos is the position of the decimal point
485
+ while (*p == 'f' || *p == 'F')
486
+ {
487
+ *p-- = '0';
488
+ }
489
+ // If we reached the decimal point, it means we are rounding
490
+ // something like 0x0.fffff so this will become 0x1.00000 :
491
+ if (p != pos)
492
+ {
493
+ if (*p == '9')
494
+ {
495
+ *p += static_cast<char>(1 + hexadd);
496
+ }
497
+ else
498
+ {
499
+ *p += 1;
500
+ }
501
+ }
502
+ else // p == pos
503
+ {
504
+ // Skip the decimal point:
505
+ --p;
506
+
507
+ // The first digit is always 0 or 1, so we don't need to
508
+ // add hexadd:
509
+ *p += 1;
510
+ }
511
+ }
512
+ }
513
+ }
514
+
515
+ // Add the final zeroes, if needed:
516
+ for (; precision > 0; --precision)
517
+ {
518
+ *result_buffer++ = '0';
519
+ }
520
+
521
+ // Move back the buffer pointer if there is no decimal point:
522
+ if (*pos == 0)
523
+ {
524
+ result_buffer = pos;
525
+ }
526
+
527
+ // Exponent:
528
+ *result_buffer++ = capitals ? 'P' : 'p';
529
+ __int64 exponent = components->_exponent - debias;
530
+ if (exponent >= 0)
531
+ {
532
+ *result_buffer++ = '+';
533
+ }
534
+ else
535
+ {
536
+ *result_buffer++ = '-';
537
+ exponent = -exponent;
538
+ }
539
+ // Save the position in pos and write a '0':
540
+ pos = result_buffer;
541
+ *pos = '0';
542
+
543
+ if (exponent >= 1000)
544
+ {
545
+ *result_buffer++ = '0' + static_cast<char>(exponent / 1000);
546
+ exponent %= 1000;
547
+ }
548
+ if (result_buffer != pos || exponent >= 100)
549
+ {
550
+ *result_buffer++ = '0' + static_cast<char>(exponent / 100);
551
+ exponent %= 100;
552
+ }
553
+ if (result_buffer != pos || exponent >= 10)
554
+ {
555
+ *result_buffer++ = '0' + static_cast<char>(exponent / 10);
556
+ exponent %= 10;
557
+ }
558
+
559
+ *result_buffer++ = '0' + static_cast<char>(exponent);
560
+
561
+ // Terminate the string:
562
+ *result_buffer = '\0';
563
+
564
+ return 0;
565
+ }
566
+
567
+
568
+
569
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570
+ //
571
+ // %f formatting
572
+ //
573
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
574
+ // These functions handle the formatting of floating point values in the %f
575
+ // printf format. This format has the form [-]ddddd.ddddd, where there will be
576
+ // precision digits following the decimal point. If precision is less than or
577
+ // equal to zero, no decimal point will appear. The low order digit is rounded.
578
+ _Success_(return == 0)
579
+ static errno_t fp_format_f_internal(
580
+ _Pre_z_ _Maybe_unsafe_(_Inout_updates_z_, buffer_count) char* const buffer,
581
+ _In_fits_precision_(precision) size_t const buffer_count,
582
+ _In_ int const precision,
583
+ _In_ STRFLT const pflt,
584
+ _In_ bool const g_fmt,
585
+ _Inout_ __crt_cached_ptd_host& ptd
586
+ ) throw()
587
+ {
588
+ int const g_magnitude = pflt->decpt - 1;
589
+
590
+ // Place the output in the user's buffer and round. Save space for the
591
+ // minus sign now if it will be needed:
592
+ if (g_fmt && g_magnitude == precision)
593
+ {
594
+ char* const p = g_magnitude + buffer + (pflt->sign == '-');
595
+ p[0] = '0';
596
+ p[1] = '\0';
597
+
598
+ // Allows for extra place-holding '0' in the exponent == precision case
599
+ // of the %g format.
600
+ }
601
+
602
+ // Now fix up the number to be in the correct %f format:
603
+ char* p = buffer;
604
+
605
+ // Put in a negative sign, if necessary:
606
+ if (pflt->sign == '-')
607
+ *p++ = '-';
608
+
609
+ // Insert a leading zero for purely fractional values and position ourselves
610
+ // at the correct spot for inserting the decimal point:
611
+ if (pflt->decpt <= 0)
612
+ {
613
+ // In the specific scenario of the 0 when using g format this would cause
614
+ // to have an extra zero at the end
615
+ bool const is_zero_pflt = pflt->decpt == 0 && *pflt->mantissa == '0';
616
+ if (!g_fmt || !is_zero_pflt) {
617
+ shift_bytes(buffer, buffer_count, p, 1);
618
+ }
619
+ *p++ = '0';
620
+ }
621
+ else
622
+ {
623
+ p += pflt->decpt;
624
+ }
625
+
626
+ // Put a decimal point if required, and add any needed zero padding:
627
+ if (precision > 0)
628
+ {
629
+ shift_bytes(buffer, buffer_count, p, 1);
630
+ *p++ = *ptd.get_locale()->locinfo->lconv->decimal_point;
631
+
632
+ // If the value is less than 1 then we may need to put zeroes out in
633
+ // front of the first non-zero digit of the mantissa:
634
+ if (pflt->decpt < 0)
635
+ {
636
+ int const computed_precision = (g_fmt || -pflt->decpt < precision)
637
+ ? -pflt->decpt
638
+ : precision;
639
+
640
+ shift_bytes(buffer, buffer_count, p, computed_precision);
641
+ memset(p, '0', computed_precision);
642
+ }
643
+ }
644
+
645
+ return 0;
646
+ }
647
+
648
+ _Success_(return == 0)
649
+ static errno_t __cdecl fp_format_f(
650
+ _In_ double const* const argument,
651
+ _Maybe_unsafe_(_Inout_updates_z_, result_buffer_count) char* const result_buffer,
652
+ _In_fits_precision_(precision) size_t const result_buffer_count,
653
+ _Out_writes_(scratch_buffer_count) char* const scratch_buffer,
654
+ _In_ size_t const scratch_buffer_count,
655
+ _In_ int const precision,
656
+ _In_ __acrt_rounding_mode const rounding_mode,
657
+ _Inout_ __crt_cached_ptd_host& ptd
658
+ ) throw()
659
+ {
660
+ _strflt strflt{};
661
+ __acrt_has_trailing_digits const trailing_digits = __acrt_fltout(
662
+ *reinterpret_cast<_CRT_DOUBLE const*>(argument),
663
+ precision,
664
+ __acrt_precision_style::fixed,
665
+ &strflt,
666
+ scratch_buffer,
667
+ scratch_buffer_count
668
+ );
669
+
670
+ errno_t const e = __acrt_fp_strflt_to_string(
671
+ result_buffer + (strflt.sign == '-'),
672
+ (result_buffer_count == _CRT_UNBOUNDED_BUFFER_SIZE ? result_buffer_count : result_buffer_count - (strflt.sign == '-')),
673
+ precision + strflt.decpt,
674
+ &strflt,
675
+ trailing_digits,
676
+ rounding_mode,
677
+ ptd);
678
+
679
+ if (e != 0)
680
+ {
681
+ result_buffer[0] = '\0';
682
+ return e;
683
+ }
684
+
685
+ return fp_format_f_internal(result_buffer, result_buffer_count, precision, &strflt, false, ptd);
686
+ }
687
+
688
+
689
+
690
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691
+ //
692
+ // %g formatting
693
+ //
694
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695
+ // These functions handle the formatting of floating point values in the %g
696
+ // printf format. The form used depends on the value converted. The printf %e
697
+ // form will be used if the magnitude of the value is less than -4 or is greater
698
+ // than 'precision', otherwise %f will be used. The 'precision' always specifies
699
+ // the number of digits following the decimal point. The low order digit is
700
+ // appropriately rounded.
701
+ _Success_(return == 0)
702
+ static errno_t __cdecl fp_format_g(
703
+ _In_ double const* const argument,
704
+ _Maybe_unsafe_(_Inout_updates_z_, result_buffer_count) char* const result_buffer,
705
+ _In_fits_precision_(precision) size_t const result_buffer_count,
706
+ _Out_writes_(scratch_buffer_count) char* const scratch_buffer,
707
+ _In_ size_t const scratch_buffer_count,
708
+ _In_ int const precision,
709
+ _In_ bool const capitals,
710
+ _In_ unsigned const min_exponent_digits,
711
+ _In_ __acrt_rounding_mode const rounding_mode,
712
+ _Inout_ __crt_cached_ptd_host& ptd
713
+ ) throw()
714
+ {
715
+ _strflt strflt{};
716
+
717
+ // Generate digits as though we will use %f formatting, then decide based on the result
718
+ // whether to use %f or %e formatting. %f always requires more generated digits than %e,
719
+ // so generating them all now will avoid generating more later (generation isn't resumable).
720
+ __acrt_has_trailing_digits const trailing_digits = __acrt_fltout(
721
+ *reinterpret_cast<_CRT_DOUBLE const*>(argument),
722
+ precision,
723
+ __acrt_precision_style::fixed,
724
+ &strflt,
725
+ scratch_buffer,
726
+ scratch_buffer_count
727
+ );
728
+
729
+ size_t const minus_sign_length = strflt.sign == '-' ? 1 : 0;
730
+
731
+ int g_magnitude = strflt.decpt - 1;
732
+ char* p = result_buffer + minus_sign_length;
733
+
734
+ size_t const buffer_count_for_fptostr = result_buffer_count == _CRT_UNBOUNDED_BUFFER_SIZE
735
+ ? result_buffer_count
736
+ : result_buffer_count - minus_sign_length;
737
+
738
+ errno_t const fptostr_result = __acrt_fp_strflt_to_string(p, buffer_count_for_fptostr, precision, &strflt, trailing_digits, rounding_mode, ptd);
739
+ if (fptostr_result != 0)
740
+ {
741
+ result_buffer[0] = '\0';
742
+ return fptostr_result;
743
+ }
744
+
745
+ bool const g_round_expansion = g_magnitude < (strflt.decpt - 1);
746
+
747
+ // Compute the magnitude of value:
748
+ g_magnitude = strflt.decpt - 1;
749
+
750
+ // Convert value to the C Language g format:
751
+ if (g_magnitude < -4 || g_magnitude >= precision) // Use e format
752
+ {
753
+ // We can ignore the round expansion flag here: the extra digit will be
754
+ // overwritten by "e+xxx".
755
+ return fp_format_e_internal(result_buffer, result_buffer_count, precision, capitals, min_exponent_digits, &strflt, true, ptd);
756
+ }
757
+ else // Use f format
758
+ {
759
+ if (g_round_expansion)
760
+ {
761
+ // Throw away extra final digit from expansion:
762
+ while (*p++)
763
+ {
764
+ // Iterate to the end of the string
765
+ }
766
+
767
+ *(p - 2) = '\0';
768
+ }
769
+
770
+ return fp_format_f_internal(result_buffer, result_buffer_count, precision, &strflt, true, ptd);
771
+ }
772
+ }
773
+
774
+
775
+
776
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
777
+ //
778
+ // Format Dispatch
779
+ //
780
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
781
+ // The main floating point formatting dispatch function. This function just
782
+ // looks at the 'format' character, calls the right formatting function, and
783
+ // returns the result. The other parameters are passed on to the selected
784
+ // formatting function and are used as described in the documentation for
785
+ // those functions.
786
+ extern "C" errno_t __cdecl __acrt_fp_format(
787
+ double const* const value,
788
+ char* const result_buffer,
789
+ size_t const result_buffer_count,
790
+ char* const scratch_buffer,
791
+ size_t const scratch_buffer_count,
792
+ int const format,
793
+ int const precision,
794
+ uint64_t const options,
795
+ __acrt_rounding_mode rounding_mode,
796
+ __crt_cached_ptd_host& ptd
797
+ )
798
+ {
799
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, result_buffer != nullptr, EINVAL);
800
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, result_buffer_count > 0, EINVAL);
801
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, scratch_buffer != nullptr, EINVAL);
802
+ _UCRT_VALIDATE_RETURN_ERRCODE(ptd, scratch_buffer_count > 0, EINVAL);
803
+
804
+ bool const use_capitals = format == 'A' || format == 'E' || format == 'F' || format == 'G';
805
+
806
+ // Detect special cases (NaNs and infinities) and handle them specially.
807
+ // Note that the underlying __acrt_fltout function will also handle these
808
+ // special cases, but it does so using the legacy strings (e.g. 1.#INF).
809
+ // Our special handling here uses the C99 strings (e.g. INF).
810
+ if ((options & _CRT_INTERNAL_PRINTF_LEGACY_MSVCRT_COMPATIBILITY) == 0)
811
+ {
812
+ __acrt_fp_class const classification = __acrt_fp_classify(*value);
813
+ if (classification != __acrt_fp_class::finite)
814
+ {
815
+ return fp_format_nan_or_infinity(
816
+ classification,
817
+ __acrt_fp_is_negative(*value),
818
+ result_buffer,
819
+ result_buffer_count,
820
+ use_capitals);
821
+ }
822
+ }
823
+
824
+ unsigned const min_exponent_digits = (options & _CRT_INTERNAL_PRINTF_LEGACY_THREE_DIGIT_EXPONENTS) != 0 ? 3 : 2;
825
+ if ((options & _CRT_INTERNAL_PRINTF_STANDARD_ROUNDING) == 0) {
826
+ rounding_mode = __acrt_rounding_mode::legacy;
827
+ }
828
+
829
+ switch (format)
830
+ {
831
+ case 'a':
832
+ case 'A':
833
+ return fp_format_a(value, result_buffer, result_buffer_count, scratch_buffer, scratch_buffer_count, precision, use_capitals, min_exponent_digits, rounding_mode, ptd);
834
+
835
+ case 'e':
836
+ case 'E':
837
+ return fp_format_e(value, result_buffer, result_buffer_count, scratch_buffer, scratch_buffer_count, precision, use_capitals, min_exponent_digits, rounding_mode, ptd);
838
+
839
+ case 'f':
840
+ case 'F':
841
+ return fp_format_f(value, result_buffer, result_buffer_count, scratch_buffer, scratch_buffer_count, precision, rounding_mode, ptd);
842
+
843
+ default:
844
+ _ASSERTE(("Unsupported format specifier", 0));
845
+ case 'g':
846
+ case 'G':
847
+ return fp_format_g(value, result_buffer, result_buffer_count, scratch_buffer, scratch_buffer_count, precision, use_capitals, min_exponent_digits, rounding_mode, ptd);
848
+ }
849
+ }