paredeyes commited on
Commit
9c0e023
·
verified ·
1 Parent(s): d7369b4

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. .gitattributes +1 -0
  2. miniMSVC/Windows Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP.Lib +3 -0
  3. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/SetEnvironmentVariableA.cpp +40 -0
  4. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/shared_initialization.cpp +67 -0
  5. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/win_policies.cpp +182 -0
  6. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/winapi_thunks.cpp +982 -0
  7. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/CompareStringA.cpp +232 -0
  8. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/CompareStringW.cpp +67 -0
  9. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/GetLocaleInfoA.cpp +165 -0
  10. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/GetStringTypeA.cpp +100 -0
  11. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/LCMapStringA.cpp +235 -0
  12. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/LCMapStringW.cpp +74 -0
  13. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/ctype.cpp +745 -0
  14. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/get_qualified_locale.cpp +851 -0
  15. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/getqloc_downlevel.cpp +852 -0
  16. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/glstatus.cpp +13 -0
  17. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initctype.cpp +339 -0
  18. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initmon.cpp +312 -0
  19. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initnum.cpp +235 -0
  20. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/inittime.cpp +177 -0
  21. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/lcidtoname_downlevel.cpp +604 -0
  22. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/lconv_unsigned_char_initialization.cpp +52 -0
  23. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/locale_refcounting.cpp +353 -0
  24. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/locale_update.cpp +55 -0
  25. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/localeconv.cpp +86 -0
  26. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/nlsdata.cpp +140 -0
  27. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/setlocale.cpp +118 -0
  28. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/wsetlocale.cpp +1430 -0
  29. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/chsize.cpp +148 -0
  30. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/close.cpp +101 -0
  31. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/commit.cpp +36 -0
  32. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/creat.cpp +41 -0
  33. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/dup.cpp +95 -0
  34. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/dup2.cpp +120 -0
  35. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/eof.cpp +50 -0
  36. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/filelength.cpp +62 -0
  37. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/ioinit.cpp +265 -0
  38. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/isatty.cpp +32 -0
  39. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/locking.cpp +120 -0
  40. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/lseek.cpp +171 -0
  41. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/mktemp.cpp +168 -0
  42. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/open.cpp +907 -0
  43. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/osfinfo.cpp +348 -0
  44. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/read.cpp +635 -0
  45. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/setmode.cpp +121 -0
  46. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/tell.cpp +18 -0
  47. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/telli64.cpp +18 -0
  48. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/txtmode.cpp +47 -0
  49. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/umask.cpp +66 -0
  50. miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/write.cpp +754 -0
.gitattributes CHANGED
@@ -230,3 +230,4 @@ miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/nanosrv.lib filter=lf
230
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP_apiset.Lib filter=lfs diff=lfs merge=lfs -text
231
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP_downlevel.Lib filter=lfs diff=lfs merge=lfs -text
232
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCore.Lib filter=lfs diff=lfs merge=lfs -text
 
 
230
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP_apiset.Lib filter=lfs diff=lfs merge=lfs -text
231
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP_downlevel.Lib filter=lfs diff=lfs merge=lfs -text
232
  miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCore.Lib filter=lfs diff=lfs merge=lfs -text
233
+ miniMSVC/Windows[[:space:]]Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP.Lib filter=lfs diff=lfs merge=lfs -text
miniMSVC/Windows Kits/10/Lib/10.0.26100.0/um/x64/OneCoreUAP.Lib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:161f77fcd9be3c3543d98be9d97f150da2cd766aec2b288c05556f28a4117a09
3
+ size 3053074
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/SetEnvironmentVariableA.cpp ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // SetEnvironmentVariableA.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Definition of __acrt_SetEnvironmentVariableA.
7
+ //
8
+
9
+ #include <corecrt_internal_win32_buffer.h>
10
+
11
+ BOOL __cdecl __acrt_SetEnvironmentVariableA(
12
+ LPCSTR const lpName,
13
+ LPCSTR const lpValue
14
+ )
15
+ {
16
+ __crt_internal_win32_buffer<wchar_t> wide_name;
17
+ __crt_internal_win32_buffer<wchar_t> wide_value;
18
+
19
+ errno_t const cvt1 = __acrt_mbs_to_wcs_cp(
20
+ lpName,
21
+ wide_name,
22
+ __acrt_get_utf8_acp_compatibility_codepage()
23
+ );
24
+
25
+ if (cvt1 != 0) {
26
+ return FALSE;
27
+ }
28
+
29
+ errno_t const cvt2 = __acrt_mbs_to_wcs_cp(
30
+ lpValue,
31
+ wide_value,
32
+ __acrt_get_utf8_acp_compatibility_codepage()
33
+ );
34
+
35
+ if (cvt2 != 0) {
36
+ return FALSE;
37
+ }
38
+
39
+ return ::SetEnvironmentVariableW(wide_name.data(), wide_value.data());
40
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/shared_initialization.cpp ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // shared_initialization.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Shared initialization logic used by both the AppCRT and the DesktopCRT.
7
+ //
8
+ #include <corecrt_internal.h>
9
+
10
+ extern "C" bool __cdecl __acrt_execute_initializers(
11
+ __acrt_initializer const* const first,
12
+ __acrt_initializer const* const last
13
+ )
14
+ {
15
+ if (first == last)
16
+ return true;
17
+
18
+ // Execute the initializers in [first, last), in order:
19
+ __acrt_initializer const* it = first;
20
+ for (; it != last; ++it)
21
+ {
22
+ if (it->_initialize == nullptr)
23
+ continue;
24
+
25
+ if (!(it->_initialize)())
26
+ break;
27
+ }
28
+
29
+ // If we reached the end, all initializers completed successfully:
30
+ if (it == last)
31
+ return true;
32
+
33
+ // Otherwise, the initializer pointed to by it failed. We need to roll back
34
+ // the initialization by executing the uninitializers corresponding to each
35
+ // of the initializers that completed successfully:
36
+ for (; it != first; --it)
37
+ {
38
+ // During initialization roll back, we do not execute uninitializers
39
+ // that have no corresponding initializer:
40
+ if ((it - 1)->_initialize == nullptr || (it - 1)->_uninitialize == nullptr)
41
+ continue;
42
+
43
+ (it - 1)->_uninitialize(false);
44
+ }
45
+
46
+ return false;
47
+ }
48
+
49
+ extern "C" bool __cdecl __acrt_execute_uninitializers(
50
+ __acrt_initializer const* const first,
51
+ __acrt_initializer const* const last
52
+ )
53
+ {
54
+ if (first == last)
55
+ return true;
56
+
57
+ // Execute the uninitializers in [first, last), in reverse order:
58
+ for (__acrt_initializer const* it = last; it != first; --it)
59
+ {
60
+ if ((it - 1)->_uninitialize == nullptr)
61
+ continue;
62
+
63
+ (it - 1)->_uninitialize(false);
64
+ }
65
+
66
+ return true;
67
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/win_policies.cpp ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // win_policies.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Wrapper functions that determine what policies should apply for the process in question.
7
+ //
8
+
9
+ #include <corecrt_internal.h>
10
+
11
+ template<typename TPolicy>
12
+ static typename TPolicy::policy_type __cdecl get_win_policy(typename TPolicy::appmodel_policy_type defaultValue)
13
+ {
14
+ TPolicy::appmodel_policy_type appmodelPolicy = defaultValue;
15
+
16
+ // Secure processes cannot load the appmodel DLL, so only attempt loading
17
+ // policy information if this is not a secure process.
18
+ if (!__acrt_is_secure_process())
19
+ {
20
+ TPolicy::appmodel_get_policy(&appmodelPolicy);
21
+ }
22
+
23
+ return TPolicy::appmodel_policy_to_policy_type(appmodelPolicy);
24
+ }
25
+
26
+ template<typename TPolicy>
27
+ static typename TPolicy::policy_type __cdecl get_cached_win_policy(typename TPolicy::appmodel_policy_type defaultValue)
28
+ {
29
+ static long state_cache = 0;
30
+ if (long const cached_state = __crt_interlocked_read(&state_cache))
31
+ {
32
+ return static_cast<TPolicy::policy_type>(cached_state);
33
+ }
34
+
35
+ TPolicy::appmodel_policy_type appmodelPolicy = defaultValue;
36
+
37
+ // Secure processes cannot load the appmodel DLL, so only attempt loading
38
+ // policy information if this is not a secure process.
39
+ if (!__acrt_is_secure_process())
40
+ {
41
+ TPolicy::appmodel_get_policy(&appmodelPolicy);
42
+ }
43
+
44
+ TPolicy::policy_type const policyValue = TPolicy::appmodel_policy_to_policy_type(appmodelPolicy);
45
+
46
+ long const cached_state = _InterlockedExchange(&state_cache, static_cast<long>(policyValue));
47
+ if (cached_state)
48
+ {
49
+ _ASSERTE(cached_state == static_cast<long>(policyValue));
50
+ }
51
+
52
+ return policyValue;
53
+ }
54
+
55
+ // Determines whether ExitProcess() or TerminateProcess() should be used to end the process
56
+ extern "C" process_end_policy __cdecl __acrt_get_process_end_policy()
57
+ {
58
+ struct process_end_policy_properties
59
+ {
60
+ typedef process_end_policy policy_type;
61
+ typedef AppPolicyProcessTerminationMethod appmodel_policy_type;
62
+
63
+ static policy_type appmodel_policy_to_policy_type(appmodel_policy_type const appmodelPolicy) throw()
64
+ {
65
+ if (appmodelPolicy == AppPolicyProcessTerminationMethod_TerminateProcess)
66
+ {
67
+ return process_end_policy_terminate_process;
68
+ }
69
+ else
70
+ {
71
+ return process_end_policy_exit_process;
72
+ }
73
+ }
74
+
75
+ static LONG appmodel_get_policy(appmodel_policy_type* appmodelPolicy)
76
+ {
77
+ return __acrt_AppPolicyGetProcessTerminationMethodInternal(appmodelPolicy);
78
+ }
79
+ };
80
+
81
+ return get_win_policy<process_end_policy_properties>(AppPolicyProcessTerminationMethod_ExitProcess);
82
+ }
83
+
84
+ // Determines whether RoInitialize() should be called when creating a thread
85
+ extern "C" begin_thread_init_policy __cdecl __acrt_get_begin_thread_init_policy()
86
+ {
87
+ struct begin_thread_init_policy_properties
88
+ {
89
+ typedef begin_thread_init_policy policy_type;
90
+ typedef AppPolicyThreadInitializationType appmodel_policy_type;
91
+
92
+ static_assert(begin_thread_init_policy_unknown == 0, "Default value for cache must be 0");
93
+
94
+ static policy_type appmodel_policy_to_policy_type(long const appmodelPolicy) throw()
95
+ {
96
+ if (appmodelPolicy == AppPolicyThreadInitializationType_InitializeWinRT)
97
+ {
98
+ return begin_thread_init_policy_ro_initialize;
99
+ }
100
+ else
101
+ {
102
+ return begin_thread_init_policy_none;
103
+ }
104
+ }
105
+
106
+ static LONG appmodel_get_policy(appmodel_policy_type* appmodelPolicy)
107
+ {
108
+ return __acrt_AppPolicyGetThreadInitializationTypeInternal(appmodelPolicy);
109
+ }
110
+ };
111
+
112
+ return get_cached_win_policy<begin_thread_init_policy_properties>(AppPolicyThreadInitializationType_None);
113
+ }
114
+
115
+ // Determines whether we should attempt to display assert dialog
116
+ extern "C" developer_information_policy __cdecl __acrt_get_developer_information_policy()
117
+ {
118
+ struct developer_information_policy_properties
119
+ {
120
+ typedef developer_information_policy policy_type;
121
+ typedef AppPolicyShowDeveloperDiagnostic appmodel_policy_type;
122
+
123
+ static_assert(developer_information_policy_unknown == 0, "Default value for cache must be 0");
124
+
125
+ static policy_type appmodel_policy_to_policy_type(long const appmodelPolicy) throw()
126
+ {
127
+ if (appmodelPolicy == AppPolicyShowDeveloperDiagnostic_ShowUI)
128
+ {
129
+ return developer_information_policy_ui;
130
+ }
131
+ else
132
+ {
133
+ return developer_information_policy_none;
134
+ }
135
+ }
136
+
137
+ static LONG appmodel_get_policy(appmodel_policy_type* appmodelPolicy)
138
+ {
139
+ return __acrt_AppPolicyGetShowDeveloperDiagnosticInternal(appmodelPolicy);
140
+ }
141
+ };
142
+
143
+ return get_cached_win_policy<developer_information_policy_properties>(AppPolicyShowDeveloperDiagnostic_ShowUI);
144
+ }
145
+
146
+ // Determines what type of windowing model technology is available
147
+ extern "C" windowing_model_policy __cdecl __acrt_get_windowing_model_policy()
148
+ {
149
+ struct windowing_model_policy_properties
150
+ {
151
+ typedef windowing_model_policy policy_type;
152
+ typedef AppPolicyWindowingModel appmodel_policy_type;
153
+
154
+ static_assert(windowing_model_policy_unknown == 0, "Default value for cache must be 0");
155
+
156
+ static policy_type appmodel_policy_to_policy_type(long const appmodelPolicy) throw()
157
+ {
158
+ switch (appmodelPolicy)
159
+ {
160
+ case AppPolicyWindowingModel_ClassicDesktop:
161
+ return windowing_model_policy_hwnd;
162
+
163
+ case AppPolicyWindowingModel_Universal:
164
+ return windowing_model_policy_corewindow;
165
+
166
+ case AppPolicyWindowingModel_ClassicPhone:
167
+ return windowing_model_policy_legacyphone;
168
+
169
+ case AppPolicyWindowingModel_None:
170
+ default:
171
+ return windowing_model_policy_none;
172
+ }
173
+ }
174
+
175
+ static LONG appmodel_get_policy(appmodel_policy_type* appmodelPolicy)
176
+ {
177
+ return __acrt_AppPolicyGetWindowingModelInternal(appmodelPolicy);
178
+ }
179
+ };
180
+
181
+ return get_cached_win_policy<windowing_model_policy_properties>(AppPolicyWindowingModel_ClassicDesktop);
182
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/internal/winapi_thunks.cpp ADDED
@@ -0,0 +1,982 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // winapi_thunks.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Definitions of wrappers for Windows API functions that cannot be called
7
+ // directly because they are not available on all supported operating systems.
8
+ //
9
+
10
+ #include <nt.h>
11
+ #include <ntrtl.h>
12
+ #include <nturtl.h>
13
+ #include <ntsecapi.h>
14
+ #include <corecrt_internal.h>
15
+ #include <appmodel.h>
16
+ #include <roapi.h>
17
+
18
+ WINBASEAPI
19
+ _Success_(return > 0 && return < BufferLength)
20
+ DWORD
21
+ WINAPI
22
+ GetTempPath2W(
23
+ _In_ DWORD BufferLength,
24
+ _Out_writes_to_opt_(BufferLength,return + 1) LPWSTR Buffer
25
+ );
26
+
27
+ // The XState APIs are declared by the Windows headers only when building for
28
+ // x86 and x64. We declare them here unconditionally so that we can share the
29
+ // same code for all architectures (we simply avoid use of these functions on
30
+ // other architectures).
31
+ extern "C" WINBASEAPI DWORD64 WINAPI GetEnabledXStateFeatures();
32
+
33
+ _Must_inspect_result_
34
+ extern "C" WINBASEAPI BOOL WINAPI GetXStateFeaturesMask(
35
+ _In_ PCONTEXT context,
36
+ _Out_ PDWORD64 feature_mask
37
+ );
38
+
39
+ _Success_(return != NULL)
40
+ extern "C" WINBASEAPI PVOID WINAPI LocateXStateFeature(
41
+ _In_ PCONTEXT context,
42
+ _In_ DWORD feature_id,
43
+ _Out_opt_ PDWORD length
44
+ );
45
+
46
+ _Must_inspect_result_
47
+ extern "C" WINBASEAPI BOOL WINAPI GetFileInformationByName(
48
+ _In_ PCWSTR FileName,
49
+ _In_ DWORD /* FILE_INFO_BY_NAME_CLASS */ FileInformationClass,
50
+ _Out_writes_bytes_(FileInfoBufferSize) PVOID FileInfoBuffer,
51
+ _In_ ULONG FileInfoBufferSize
52
+ );
53
+
54
+ #define _ACRT_APPLY_TO_LATE_BOUND_MODULES_0 \
55
+ _APPLY(api_ms_win_core_datetime_l1_1_1, "api-ms-win-core-datetime-l1-1-1" ) \
56
+ _APPLY(api_ms_win_core_fibers_l1_1_2, "api-ms-win-core-fibers-l1-1-2" ) \
57
+ _APPLY(api_ms_win_core_file_l1_2_4, "api-ms-win-core-file-l1-2-4" ) \
58
+ _APPLY(api_ms_win_core_file_l1_2_2, "api-ms-win-core-file-l1-2-2" ) \
59
+ _APPLY(api_ms_win_core_file_l2_1_4, "api-ms-win-core-file-l2-1-4" ) \
60
+ _APPLY(api_ms_win_core_localization_l1_2_1, "api-ms-win-core-localization-l1-2-1" ) \
61
+ _APPLY(api_ms_win_core_localization_obsolete_l1_2_0, "api-ms-win-core-localization-obsolete-l1-2-0") \
62
+ _APPLY(api_ms_win_core_processthreads_l1_1_2, "api-ms-win-core-processthreads-l1-1-2" ) \
63
+ _APPLY(api_ms_win_core_string_l1_1_0, "api-ms-win-core-string-l1-1-0" ) \
64
+ _APPLY(api_ms_win_core_synch_l1_2_0, "api-ms-win-core-synch-l1-2-0" ) \
65
+ _APPLY(api_ms_win_core_sysinfo_l1_2_1, "api-ms-win-core-sysinfo-l1-2-1" ) \
66
+ _APPLY(api_ms_win_core_winrt_l1_1_0, "api-ms-win-core-winrt-l1-1-0" ) \
67
+ _APPLY(api_ms_win_core_xstate_l2_1_0, "api-ms-win-core-xstate-l2-1-0" ) \
68
+ _APPLY(api_ms_win_rtcore_ntuser_window_l1_1_0, "api-ms-win-rtcore-ntuser-window-l1-1-0" ) \
69
+ _APPLY(api_ms_win_security_systemfunctions_l1_1_0, "api-ms-win-security-systemfunctions-l1-1-0" ) \
70
+ _APPLY(ext_ms_win_ntuser_dialogbox_l1_1_0, "ext-ms-win-ntuser-dialogbox-l1-1-0" ) \
71
+ _APPLY(ext_ms_win_ntuser_windowstation_l1_1_0, "ext-ms-win-ntuser-windowstation-l1-1-0" ) \
72
+ _APPLY(advapi32, "advapi32" ) \
73
+ _APPLY(kernel32, "kernel32" ) \
74
+ _APPLY(kernelbase, "kernelbase" ) \
75
+ _APPLY(ntdll, "ntdll" ) \
76
+ _APPLY(api_ms_win_appmodel_runtime_l1_1_2, "api-ms-win-appmodel-runtime-l1-1-2" ) \
77
+ _APPLY(user32, "user32" )
78
+
79
+ #define _ACRT_APPLY_TO_LATE_BOUND_MODULES \
80
+ _ACRT_APPLY_TO_LATE_BOUND_MODULES_0 \
81
+
82
+ #define _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS_0 \
83
+ _APPLY(AreFileApisANSI, ({ /* api_ms_win_core_file_l1_2_2, */ kernel32 })) \
84
+ _APPLY(CompareStringEx, ({ api_ms_win_core_string_l1_1_0, kernel32 })) \
85
+ _APPLY(EnumSystemLocalesEx, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
86
+ _APPLY(FlsGetValue2, ({ api_ms_win_core_fibers_l1_1_2, kernelbase })) \
87
+ _APPLY(GetActiveWindow, ({ api_ms_win_rtcore_ntuser_window_l1_1_0, user32 })) \
88
+ _APPLY(GetDateFormatEx, ({ api_ms_win_core_datetime_l1_1_1, kernel32 })) \
89
+ _APPLY(GetTempPath2W, ({ api_ms_win_core_file_l1_2_4, kernelbase })) \
90
+ _APPLY(GetFileInformationByName, ({ api_ms_win_core_file_l2_1_4, kernelbase })) \
91
+ _APPLY(GetEnabledXStateFeatures, ({ api_ms_win_core_xstate_l2_1_0, kernel32 })) \
92
+ _APPLY(GetLastActivePopup, ({ ext_ms_win_ntuser_dialogbox_l1_1_0, user32 })) \
93
+ _APPLY(GetLocaleInfoEx, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
94
+ _APPLY(GetProcessWindowStation, ({ ext_ms_win_ntuser_windowstation_l1_1_0, user32 })) \
95
+ _APPLY(GetSystemTimePreciseAsFileTime, ({ api_ms_win_core_sysinfo_l1_2_1 })) \
96
+ _APPLY(GetTimeFormatEx, ({ api_ms_win_core_datetime_l1_1_1, kernel32 })) \
97
+ _APPLY(GetUserDefaultLocaleName, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
98
+ _APPLY(GetUserObjectInformationW, ({ ext_ms_win_ntuser_windowstation_l1_1_0, user32 })) \
99
+ _APPLY(GetXStateFeaturesMask, ({ api_ms_win_core_xstate_l2_1_0, kernel32 })) \
100
+ _APPLY(IsValidLocaleName, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
101
+ _APPLY(LCMapStringEx, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
102
+ _APPLY(LCIDToLocaleName, ({ api_ms_win_core_localization_obsolete_l1_2_0, kernel32 })) \
103
+ _APPLY(LocaleNameToLCID, ({ api_ms_win_core_localization_l1_2_1, kernel32 })) \
104
+ _APPLY(LocateXStateFeature, ({ api_ms_win_core_xstate_l2_1_0, kernel32 })) \
105
+ _APPLY(MessageBoxA, ({ ext_ms_win_ntuser_dialogbox_l1_1_0, user32 })) \
106
+ _APPLY(MessageBoxW, ({ ext_ms_win_ntuser_dialogbox_l1_1_0, user32 })) \
107
+ _APPLY(RoInitialize, ({ api_ms_win_core_winrt_l1_1_0 })) \
108
+ _APPLY(RoUninitialize, ({ api_ms_win_core_winrt_l1_1_0 })) \
109
+ _APPLY(AppPolicyGetProcessTerminationMethod, ({ api_ms_win_appmodel_runtime_l1_1_2 })) \
110
+ _APPLY(AppPolicyGetThreadInitializationType, ({ api_ms_win_appmodel_runtime_l1_1_2 })) \
111
+ _APPLY(AppPolicyGetShowDeveloperDiagnostic, ({ api_ms_win_appmodel_runtime_l1_1_2 })) \
112
+ _APPLY(AppPolicyGetWindowingModel, ({ api_ms_win_appmodel_runtime_l1_1_2 })) \
113
+ _APPLY(SetThreadStackGuarantee, ({ api_ms_win_core_processthreads_l1_1_2, kernel32 })) \
114
+ _APPLY(SystemFunction036, ({ api_ms_win_security_systemfunctions_l1_1_0, advapi32 }))
115
+
116
+ #define _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS \
117
+ _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS_0 \
118
+
119
+ namespace
120
+ {
121
+ // Generate enumerators for each of the modules:
122
+ enum module_id : unsigned
123
+ {
124
+ #define _APPLY(_SYMBOL, _NAME) _SYMBOL,
125
+ _ACRT_APPLY_TO_LATE_BOUND_MODULES
126
+ #undef _APPLY
127
+
128
+ module_id_count
129
+ };
130
+
131
+ // Generate a table of module names that can be indexed by the module_id
132
+ // enumerators:
133
+ static wchar_t const* const module_names[module_id_count] =
134
+ {
135
+ #define _APPLY(_SYMBOL, _NAME) _CRT_WIDE(_NAME),
136
+ _ACRT_APPLY_TO_LATE_BOUND_MODULES
137
+ #undef _APPLY
138
+ };
139
+
140
+ // Generate enumerators for each of the functions:
141
+ enum function_id : unsigned
142
+ {
143
+ #define _APPLY(_FUNCTION, _MODULES) _CRT_CONCATENATE(_FUNCTION, _id),
144
+ _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS
145
+ #undef _APPLY
146
+
147
+ function_id_count
148
+ };
149
+
150
+ // Generate a typedef for each function of the form function_pft.
151
+ #define _APPLY(_FUNCTION, _MODULES) \
152
+ using _CRT_CONCATENATE(_FUNCTION, _pft) = decltype(_FUNCTION)*;
153
+ _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS
154
+ #undef _APPLY
155
+ }
156
+
157
+ // This table stores the module handles that we have obtained via LoadLibrary.
158
+ // If a handle is null, we have not yet attempted to load that module. If a
159
+ // handle is -1 (INVALID_HANDLE_VALUE), we have attempted to load the module
160
+ // but the attempt failed.
161
+ static HMODULE module_handles[module_id_count];
162
+
163
+ // This table stores the function pointers that we have loaded dynamically.
164
+ // If a function pointer is a null pointer, we have not yet attempted to
165
+ // get that function pointer. If a function pointer is an -1, we have
166
+ // attempted to get that function pointer but the attempt failed.
167
+ #pragma data_seg(push, almostro, ".fptable")
168
+ #pragma bss_seg(push, almostro, ".fptable")
169
+ static void* function_pointers[function_id_count];
170
+ #pragma bss_seg(pop, almostro)
171
+ #pragma data_seg(pop, almostro)
172
+
173
+ extern "C" bool __cdecl __acrt_initialize_winapi_thunks()
174
+ {
175
+ DWORD old_prot;
176
+ return VirtualProtect(function_pointers, sizeof(function_pointers), PAGE_READONLY, &old_prot) ? true : false;
177
+ }
178
+
179
+ extern "C" bool __cdecl __acrt_uninitialize_winapi_thunks(bool const terminating)
180
+ {
181
+ // If the process is terminating, there's no need to free any module handles
182
+ if (terminating)
183
+ {
184
+ return true;
185
+ }
186
+
187
+ for (HMODULE& module : module_handles)
188
+ {
189
+ if (module)
190
+ {
191
+ if (module != INVALID_HANDLE_VALUE)
192
+ {
193
+ FreeLibrary(module);
194
+ }
195
+
196
+ module = nullptr;
197
+ }
198
+ }
199
+
200
+ return true;
201
+ }
202
+
203
+ static __forceinline void* __cdecl invalid_function_sentinel() throw()
204
+ {
205
+ return reinterpret_cast<void*>(static_cast<uintptr_t>(-1));
206
+ }
207
+
208
+ static HMODULE __cdecl try_load_library_from_system_directory(wchar_t const* const name) throw()
209
+ {
210
+ HMODULE const handle = LoadLibraryExW(name, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
211
+ if (handle)
212
+ {
213
+ return handle;
214
+ }
215
+
216
+ // LOAD_LIBRARY_SEARCH_SYSTEM32 is only supported by Windows 7 and above; if
217
+ // the OS does not support this flag, try again without it. On these OSes,
218
+ // all APISets will be forwarders. To prevent DLL hijacking, do not attempt
219
+ // to load the APISet forwarders dynamically. This will cause our caller to
220
+ // fall back to the real DLL (e.g. kernel32). All of those are known DLLs.
221
+ if (GetLastError() == ERROR_INVALID_PARAMETER &&
222
+ wcsncmp(name, L"api-ms-", 7) != 0 &&
223
+ wcsncmp(name, L"ext-ms-", 7) != 0)
224
+ {
225
+ return LoadLibraryExW(name, nullptr, 0);
226
+ }
227
+
228
+ return nullptr;
229
+ }
230
+
231
+ static HMODULE __cdecl try_get_module(module_id const id) throw()
232
+ {
233
+ // First check to see if we've cached the module handle:
234
+ if (HMODULE const cached_handle = __crt_interlocked_read_pointer(module_handles + id))
235
+ {
236
+ if (cached_handle == INVALID_HANDLE_VALUE)
237
+ {
238
+ return nullptr;
239
+ }
240
+
241
+ return cached_handle;
242
+ }
243
+
244
+ // If we haven't yet cached the module handle, try to load the library. If
245
+ // this fails, cache the sentinel handle value INVALID_HANDLE_VALUE so that
246
+ // we don't attempt to load the module again:
247
+ HMODULE const new_handle = try_load_library_from_system_directory(module_names[id]);
248
+ if (!new_handle)
249
+ {
250
+ if (HMODULE const cached_handle = __crt_interlocked_exchange_pointer(module_handles + id, INVALID_HANDLE_VALUE))
251
+ {
252
+ _ASSERTE(cached_handle == INVALID_HANDLE_VALUE);
253
+ }
254
+
255
+ return nullptr;
256
+ }
257
+
258
+ // Swap the new handle into the cache. If the cache no longer contained a
259
+ // null handle, then some other thread loaded the module and cached the
260
+ // handle while we were doing the same. In that case, we free the handle
261
+ // once to maintain the reference count:
262
+ if (HMODULE const cached_handle = __crt_interlocked_exchange_pointer(module_handles + id, new_handle))
263
+ {
264
+ _ASSERTE(cached_handle == new_handle);
265
+ FreeLibrary(new_handle);
266
+ }
267
+
268
+ return new_handle;
269
+ }
270
+
271
+ static HMODULE __cdecl try_get_first_available_module(
272
+ module_id const* const first,
273
+ module_id const* const last
274
+ ) throw()
275
+ {
276
+ for (module_id const* it = first; it != last; ++it)
277
+ {
278
+ HMODULE const handle = try_get_module(*it);
279
+ if (handle)
280
+ {
281
+ return handle;
282
+ }
283
+ }
284
+
285
+ return nullptr;
286
+ }
287
+
288
+ static __forceinline void* __cdecl try_get_proc_address_from_first_available_module(
289
+ char const* const name,
290
+ module_id const* const first_module_id,
291
+ module_id const* const last_module_id
292
+ ) throw()
293
+ {
294
+ HMODULE const module_handle = try_get_first_available_module(first_module_id, last_module_id);
295
+ if (!module_handle)
296
+ {
297
+ return nullptr;
298
+ }
299
+
300
+ return reinterpret_cast<void*>(GetProcAddress(module_handle, name));
301
+ }
302
+
303
+ DECLSPEC_NOINLINE
304
+ static void* __cdecl try_get_function_slow(
305
+ function_id const id,
306
+ char const* const name,
307
+ module_id const* const first_module_id,
308
+ module_id const* const last_module_id
309
+ ) throw()
310
+ {
311
+ // If we haven't yet cached the function pointer, try to import it from any
312
+ // of the modules in which it might be defined. If this fails, cache the
313
+ // sentinel pointer so that we don't attempt to load this function again:
314
+ void* const new_fp = try_get_proc_address_from_first_available_module(name, first_module_id, last_module_id);
315
+
316
+ __acrt_lock(__acrt_function_pointer_table_lock);
317
+
318
+ DWORD old_prot;
319
+ BOOL vp_result = VirtualProtect(function_pointers, sizeof(function_pointers), PAGE_READWRITE, &old_prot);
320
+ if (vp_result == FALSE)
321
+ {
322
+ abort();
323
+ }
324
+
325
+ if (!new_fp)
326
+ {
327
+ void* const cached_fp =
328
+ __crt_interlocked_exchange_pointer(
329
+ function_pointers + id,
330
+ invalid_function_sentinel());
331
+
332
+ if (cached_fp)
333
+ {
334
+ _ASSERTE(cached_fp == invalid_function_sentinel());
335
+ }
336
+
337
+ goto end;
338
+ }
339
+
340
+ // Swap the newly obtained function pointer into the cache. The cache may
341
+ // no longer contain a null pointer if another thread obtained the
342
+ // function address while we were doing the same (both threads should have
343
+ // gotten the same function pointer):
344
+ {
345
+ void* const cached_fp =
346
+ __crt_interlocked_exchange_pointer(
347
+ function_pointers + id,
348
+ new_fp);
349
+
350
+ if (cached_fp)
351
+ {
352
+ _ASSERTE(cached_fp == new_fp);
353
+ }
354
+ }
355
+
356
+ end:
357
+ vp_result = VirtualProtect(function_pointers, sizeof(function_pointers), PAGE_READONLY, &old_prot);
358
+ if (vp_result == FALSE)
359
+ {
360
+ abort();
361
+ }
362
+
363
+ __acrt_unlock(__acrt_function_pointer_table_lock);
364
+
365
+ return new_fp;
366
+ }
367
+
368
+ static __forceinline void* __cdecl try_get_function(
369
+ function_id const id,
370
+ char const* const name,
371
+ module_id const* const first_module_id,
372
+ module_id const* const last_module_id
373
+ ) throw()
374
+ {
375
+ // First check to see if we've cached the function pointer:
376
+ {
377
+ void* const cached_fp =
378
+ ReadPointerNoFence(function_pointers + id);
379
+
380
+ if (cached_fp == invalid_function_sentinel())
381
+ {
382
+ return nullptr;
383
+ }
384
+
385
+ if (cached_fp)
386
+ {
387
+ return cached_fp;
388
+ }
389
+ }
390
+
391
+ return try_get_function_slow(id, name, first_module_id, last_module_id);
392
+ }
393
+
394
+ // Generate accessors that wrap the general try_get_function for each function,
395
+ // passing the correct set of candidate modules and returning a function pointer
396
+ // of the correct type:
397
+ #define _APPLY(_FUNCTION, _MODULES) \
398
+ static _CRT_CONCATENATE(_FUNCTION, _pft) __cdecl _CRT_CONCATENATE(try_get_, _FUNCTION)() throw() \
399
+ { \
400
+ static module_id const candidate_modules[] = _CRT_UNPARENTHESIZE(_MODULES); \
401
+ \
402
+ return reinterpret_cast<_CRT_CONCATENATE(_FUNCTION, _pft)>(try_get_function( \
403
+ _CRT_CONCATENATE(_FUNCTION, _id), \
404
+ _CRT_STRINGIZE(_FUNCTION), \
405
+ candidate_modules, \
406
+ candidate_modules + _countof(candidate_modules))); \
407
+ }
408
+ _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS
409
+ #undef _APPLY
410
+
411
+ static __forceinline void* __cdecl get_function(
412
+ function_id const id,
413
+ char const* const name,
414
+ module_id const* const first_module_id,
415
+ module_id const* const last_module_id
416
+ ) throw()
417
+ {
418
+ UNREFERENCED_PARAMETER(name);
419
+ UNREFERENCED_PARAMETER(first_module_id);
420
+ UNREFERENCED_PARAMETER(last_module_id);
421
+
422
+ // This function assumes the cached function pointer is always valid
423
+ void* const cached_fp =
424
+ ReadPointerNoFence(function_pointers + id);
425
+
426
+ return cached_fp;
427
+
428
+ }
429
+
430
+ #pragma warning(push)
431
+ #pragma warning(disable: 4505) // unreferenced local function has been removed
432
+ // Generate accessors that wrap the general try_get_function for each function,
433
+ // passing the correct set of candidate modules and returning a function pointer
434
+ // of the correct type:
435
+ #define _APPLY(_FUNCTION, _MODULES) \
436
+ static __forceinline _CRT_CONCATENATE(_FUNCTION, _pft) __cdecl _CRT_CONCATENATE(get_, _FUNCTION)() throw() \
437
+ { \
438
+ static module_id const candidate_modules[] = _CRT_UNPARENTHESIZE(_MODULES); \
439
+ \
440
+ return reinterpret_cast<_CRT_CONCATENATE(_FUNCTION, _pft)>(get_function( \
441
+ _CRT_CONCATENATE(_FUNCTION, _id), \
442
+ _CRT_STRINGIZE(_FUNCTION), \
443
+ candidate_modules, \
444
+ candidate_modules + _countof(candidate_modules))); \
445
+ }
446
+ _ACRT_APPLY_TO_LATE_BOUND_FUNCTIONS
447
+ #undef _APPLY
448
+ #pragma warning(pop)
449
+
450
+ extern "C" BOOL WINAPI __acrt_AreFileApisANSI()
451
+ {
452
+ if (auto const are_file_apis_ansi = try_get_AreFileApisANSI())
453
+ {
454
+ return are_file_apis_ansi();
455
+ }
456
+
457
+ // If we were unable to get the AreFileApisANSI function, we can safely
458
+ // assume that the file APIs are, in fact, ANSI:
459
+ return TRUE;
460
+ }
461
+
462
+ extern "C" int WINAPI __acrt_CompareStringEx(
463
+ LPCWSTR const locale_name,
464
+ DWORD const flags,
465
+ LPCWCH const string1,
466
+ int const string1_count,
467
+ LPCWCH const string2,
468
+ int const string2_count,
469
+ LPNLSVERSIONINFO const version,
470
+ LPVOID const reserved,
471
+ LPARAM const param
472
+ )
473
+ {
474
+ if (auto const compare_string_ex = try_get_CompareStringEx())
475
+ {
476
+ // On WCOS devices, CompareStringEx may calls into icu.dll which is an OS component using the UCRT.
477
+ // If icu.dll calls any UCRT export under OS mode (ex: malloc), then CompareStringEx will return under Prog Mode even if
478
+ // we started in OS mode. To prevent this, an OS mode guard is in place.
479
+ __crt_state_management::scoped_global_state_reset os_mode_guard;
480
+ return compare_string_ex(locale_name, flags, string1, string1_count, string2, string2_count, version, reserved, param);
481
+ }
482
+
483
+ return CompareStringW(__acrt_LocaleNameToLCID(locale_name, 0), flags, string1, string1_count, string2, string2_count);
484
+ }
485
+
486
+ // This has been split into its own function to work around a bug in the Dev12
487
+ // C++ compiler where nested captureless lambdas are not convertible to the
488
+ // required function pointer type.
489
+ static BOOL enum_system_locales_ex_nolock(
490
+ LOCALE_ENUMPROCEX const enum_proc
491
+ ) throw()
492
+ {
493
+ static LOCALE_ENUMPROCEX static_enum_proc;
494
+
495
+ static_enum_proc = enum_proc;
496
+ BOOL const result = EnumSystemLocalesW(
497
+ [](LPWSTR locale_string) { return (static_enum_proc)(locale_string, 0, 0); },
498
+ LCID_INSTALLED);
499
+ static_enum_proc = nullptr;
500
+
501
+ return result;
502
+ }
503
+
504
+ extern "C" BOOL WINAPI __acrt_EnumSystemLocalesEx(
505
+ LOCALE_ENUMPROCEX const enum_proc,
506
+ DWORD const flags,
507
+ LPARAM const param,
508
+ LPVOID const reserved
509
+ )
510
+ {
511
+ if (auto const enum_system_locales_ex = try_get_EnumSystemLocalesEx())
512
+ {
513
+ return enum_system_locales_ex(enum_proc, flags, param, reserved);
514
+ }
515
+
516
+ return __acrt_lock_and_call(__acrt_locale_lock, [&]() -> BOOL
517
+ {
518
+ return enum_system_locales_ex_nolock(enum_proc);
519
+ });
520
+ }
521
+
522
+ // Historically, we needed to conditionally call FLS functions for x86 since the UCRT needed to be consumable down to XP.
523
+ // This is no longer the case as XP is no longer supported, however the enclave implementations of these functions
524
+ // unconditionally call the TLS equivalent APIs (TlsAlloc, etc), hence why these are still kept around
525
+ extern "C" DWORD WINAPI __acrt_FlsAlloc(PFLS_CALLBACK_FUNCTION const callback)
526
+ {
527
+ return FlsAlloc(callback);
528
+ }
529
+
530
+ extern "C" BOOL WINAPI __acrt_FlsFree(DWORD const fls_index)
531
+ {
532
+ return FlsFree(fls_index);
533
+ }
534
+
535
+ extern "C" PVOID WINAPI __acrt_FlsGetValue(DWORD const fls_index)
536
+ {
537
+ return FlsGetValue(fls_index);
538
+ }
539
+
540
+ extern "C"
541
+ DECLSPEC_GUARDNOCF
542
+ PVOID WINAPI __acrt_FlsGetValue2(DWORD const fls_index)
543
+ {
544
+ #if (defined(_M_ARM64_) || defined(_M_ARM64EC)) && _UCRT_DLL
545
+ return FlsGetValue2(fls_index);
546
+ #else
547
+ /*
548
+ * N.B.: This function must be called after checking __acrt_use_tls2_apis.
549
+ */
550
+ auto const flsgetvalue2 = get_FlsGetValue2();
551
+ return flsgetvalue2(fls_index);
552
+ #endif
553
+ }
554
+
555
+ extern "C" BOOL WINAPI __acrt_FlsSetValue(DWORD const fls_index, PVOID const fls_data)
556
+ {
557
+ return FlsSetValue(fls_index, fls_data);
558
+ }
559
+
560
+ extern "C" BOOL WINAPI __acrt_IsThreadAFiber()
561
+ {
562
+ return IsThreadAFiber();
563
+ }
564
+
565
+ extern "C" int WINAPI __acrt_GetDateFormatEx(
566
+ LPCWSTR const locale_name,
567
+ DWORD const flags,
568
+ SYSTEMTIME CONST* const date,
569
+ LPCWSTR const format,
570
+ LPWSTR const buffer,
571
+ int const buffer_count,
572
+ LPCWSTR const calendar
573
+ )
574
+ {
575
+ if (auto const get_date_format_ex = try_get_GetDateFormatEx())
576
+ {
577
+ return get_date_format_ex(locale_name, flags, date, format, buffer, buffer_count, calendar);
578
+ }
579
+
580
+ return GetDateFormatW(__acrt_LocaleNameToLCID(locale_name, 0), flags, date, format, buffer, buffer_count);
581
+ }
582
+
583
+ extern "C" int WINAPI __acrt_GetTempPath2W(
584
+ DWORD nBufferLength,
585
+ LPWSTR lpBuffer
586
+ )
587
+ {
588
+ if (auto const get_temp_path2w = try_get_GetTempPath2W())
589
+ {
590
+ return get_temp_path2w(nBufferLength, lpBuffer);
591
+ }
592
+ return GetTempPathW(nBufferLength, lpBuffer);
593
+ }
594
+
595
+ extern "C" BOOL WINAPI __acrt_GetFileInformationByName(
596
+ LPCWSTR FileName,
597
+ DWORD FileInformationClass, // FILE_INFO_BY_NAME_CLASS
598
+ PVOID FileInfoBuffer,
599
+ ULONG FileInfoBufferSize
600
+ )
601
+ {
602
+ if (auto const get_file_info = try_get_GetFileInformationByName())
603
+ {
604
+ return get_file_info(FileName, FileInformationClass, FileInfoBuffer, FileInfoBufferSize);
605
+ }
606
+ return FALSE;
607
+ }
608
+
609
+ extern "C" DWORD64 WINAPI __acrt_GetEnabledXStateFeatures()
610
+ {
611
+ if (auto const get_enabled_xstate_features = try_get_GetEnabledXStateFeatures())
612
+ {
613
+ return get_enabled_xstate_features();
614
+ }
615
+
616
+ abort(); // No fallback; callers should check availablility before calling
617
+ }
618
+
619
+ extern "C" int WINAPI __acrt_GetLocaleInfoEx(
620
+ LPCWSTR const locale_name,
621
+ LCTYPE const lc_type,
622
+ LPWSTR const data,
623
+ int const data_count
624
+ )
625
+ {
626
+ if (auto const get_locale_info_ex = try_get_GetLocaleInfoEx())
627
+ {
628
+ return get_locale_info_ex(locale_name, lc_type, data, data_count);
629
+ }
630
+
631
+ return GetLocaleInfoW(__acrt_LocaleNameToLCID(locale_name, 0), lc_type, data, data_count);
632
+ }
633
+
634
+ extern "C" VOID WINAPI __acrt_GetSystemTimePreciseAsFileTime(LPFILETIME const system_time)
635
+ {
636
+ if (auto const get_system_time_precise_as_file_time = try_get_GetSystemTimePreciseAsFileTime())
637
+ {
638
+ return get_system_time_precise_as_file_time(system_time);
639
+ }
640
+
641
+ return GetSystemTimeAsFileTime(system_time);
642
+ }
643
+
644
+ extern "C" int WINAPI __acrt_GetTimeFormatEx(
645
+ LPCWSTR const locale_name,
646
+ DWORD const flags,
647
+ SYSTEMTIME CONST* const time,
648
+ LPCWSTR const format,
649
+ LPWSTR const buffer,
650
+ int const buffer_count
651
+ )
652
+ {
653
+ if (auto const get_time_format_ex = try_get_GetTimeFormatEx())
654
+ {
655
+ return get_time_format_ex(locale_name, flags, time, format, buffer, buffer_count);
656
+ }
657
+
658
+ return GetTimeFormatW(__acrt_LocaleNameToLCID(locale_name, 0), flags, time, format, buffer, buffer_count);
659
+ }
660
+
661
+ extern "C" int WINAPI __acrt_GetUserDefaultLocaleName(
662
+ LPWSTR const locale_name,
663
+ int const locale_name_count
664
+ )
665
+ {
666
+ if (auto const get_user_default_locale_name = try_get_GetUserDefaultLocaleName())
667
+ {
668
+ return get_user_default_locale_name(locale_name, locale_name_count);
669
+ }
670
+
671
+ return __acrt_LCIDToLocaleName(GetUserDefaultLCID(), locale_name, locale_name_count, 0);
672
+ }
673
+
674
+ extern "C" BOOL WINAPI __acrt_GetXStateFeaturesMask(
675
+ PCONTEXT const context,
676
+ PDWORD64 const feature_mask
677
+ )
678
+ {
679
+ if (auto const get_xstate_features_mask = try_get_GetXStateFeaturesMask())
680
+ {
681
+ return get_xstate_features_mask(context, feature_mask);
682
+ }
683
+
684
+ abort(); // No fallback; callers should check availablility before calling
685
+ }
686
+
687
+ extern "C" BOOL WINAPI __acrt_InitializeCriticalSectionEx(
688
+ LPCRITICAL_SECTION const critical_section,
689
+ DWORD const spin_count,
690
+ DWORD const flags
691
+ )
692
+ {
693
+ return InitializeCriticalSectionEx(critical_section, spin_count, flags);
694
+ }
695
+
696
+ extern "C" BOOL WINAPI __acrt_IsValidLocaleName(LPCWSTR const locale_name)
697
+ {
698
+ if (auto const is_valid_locale_name = try_get_IsValidLocaleName())
699
+ {
700
+ return is_valid_locale_name(locale_name);
701
+ }
702
+
703
+ return IsValidLocale(__acrt_LocaleNameToLCID(locale_name, 0), LCID_INSTALLED);
704
+ }
705
+
706
+ extern "C" int WINAPI __acrt_LCMapStringEx(
707
+ LPCWSTR const locale_name,
708
+ DWORD const flags,
709
+ LPCWSTR const source,
710
+ int const source_count,
711
+ LPWSTR const destination,
712
+ int const destination_count,
713
+ LPNLSVERSIONINFO const version,
714
+ LPVOID const reserved,
715
+ LPARAM const sort_handle
716
+ )
717
+ {
718
+ if (auto const lc_map_string_ex = try_get_LCMapStringEx())
719
+ {
720
+ return lc_map_string_ex(locale_name, flags, source, source_count, destination, destination_count, version, reserved, sort_handle);
721
+ }
722
+ #pragma warning(disable:__WARNING_PRECONDITION_NULLTERMINATION_VIOLATION) // 26035 LCMapStringW annotation is presently incorrect 11/26/2014 Jaykrell
723
+ return LCMapStringW(__acrt_LocaleNameToLCID(locale_name, 0), flags, source, source_count, destination, destination_count);
724
+ }
725
+
726
+ extern "C" int WINAPI __acrt_LCIDToLocaleName(
727
+ LCID const locale,
728
+ LPWSTR const name,
729
+ int const name_count,
730
+ DWORD const flags
731
+ )
732
+ {
733
+ if (auto const lcid_to_locale_name = try_get_LCIDToLocaleName())
734
+ {
735
+ return lcid_to_locale_name(locale, name, name_count, flags);
736
+ }
737
+
738
+ return __acrt_DownlevelLCIDToLocaleName(locale, name, name_count);
739
+ }
740
+
741
+ extern "C" LCID WINAPI __acrt_LocaleNameToLCID(
742
+ LPCWSTR const name,
743
+ DWORD const flags
744
+ )
745
+ {
746
+ if (auto const locale_name_to_lcid = try_get_LocaleNameToLCID())
747
+ {
748
+ return locale_name_to_lcid(name, flags);
749
+ }
750
+
751
+ return __acrt_DownlevelLocaleNameToLCID(name);
752
+ }
753
+
754
+ extern "C" PVOID WINAPI __acrt_LocateXStateFeature(
755
+ PCONTEXT const content,
756
+ DWORD const feature_id,
757
+ PDWORD const length
758
+ )
759
+ {
760
+ if (auto const locate_xstate_feature = try_get_LocateXStateFeature())
761
+ {
762
+ return locate_xstate_feature(content, feature_id, length);
763
+ }
764
+
765
+ abort(); // No fallback; callers should check availablility before calling
766
+ }
767
+
768
+ extern "C" int WINAPI __acrt_MessageBoxA(
769
+ HWND const hwnd,
770
+ LPCSTR const text,
771
+ LPCSTR const caption,
772
+ UINT const type
773
+ )
774
+ {
775
+ if (auto const message_box_a = try_get_MessageBoxA())
776
+ {
777
+ return message_box_a(hwnd, text, caption, type);
778
+ }
779
+
780
+ abort(); // No fallback; callers should check availablility before calling
781
+ }
782
+
783
+ extern "C" int WINAPI __acrt_MessageBoxW(
784
+ HWND const hwnd,
785
+ LPCWSTR const text,
786
+ LPCWSTR const caption,
787
+ UINT const type
788
+ )
789
+ {
790
+ if (auto const message_box_w = try_get_MessageBoxW())
791
+ {
792
+ return message_box_w(hwnd, text, caption, type);
793
+ }
794
+
795
+ abort(); // No fallback; callers should check availablility before calling
796
+ }
797
+
798
+ extern "C" BOOLEAN WINAPI __acrt_RtlGenRandom(
799
+ PVOID const buffer,
800
+ ULONG const buffer_count
801
+ )
802
+ {
803
+ if (auto const rtl_gen_random = try_get_SystemFunction036())
804
+ {
805
+ return rtl_gen_random(buffer, buffer_count);
806
+ }
807
+
808
+ abort(); // No fallback (this function should exist)
809
+ }
810
+
811
+ extern "C" HRESULT WINAPI __acrt_RoInitialize(RO_INIT_TYPE const init_type)
812
+ {
813
+ if (auto const ro_initialize = try_get_RoInitialize())
814
+ {
815
+ return ro_initialize(init_type);
816
+ }
817
+
818
+ return S_OK; // No fallback (this is a best-effort wrapper)
819
+ }
820
+
821
+ extern "C" void WINAPI __acrt_RoUninitialize()
822
+ {
823
+ if (auto const ro_uninitialize = try_get_RoUninitialize())
824
+ {
825
+ return ro_uninitialize();
826
+ }
827
+
828
+ // No fallback (this is a best-effort wrapper)
829
+ }
830
+
831
+ LONG WINAPI __acrt_AppPolicyGetProcessTerminationMethodInternal(_Out_ AppPolicyProcessTerminationMethod* policy)
832
+ {
833
+ if (auto const app_policy_get_process_terminaton_method_claims = try_get_AppPolicyGetProcessTerminationMethod())
834
+ {
835
+ return app_policy_get_process_terminaton_method_claims(GetCurrentThreadEffectiveToken(), policy);
836
+ }
837
+
838
+ return STATUS_NOT_FOUND;
839
+ }
840
+
841
+ LONG WINAPI __acrt_AppPolicyGetThreadInitializationTypeInternal(_Out_ AppPolicyThreadInitializationType* policy)
842
+ {
843
+ if (auto const app_policy_get_thread_initialization_type_claims = try_get_AppPolicyGetThreadInitializationType())
844
+ {
845
+ return app_policy_get_thread_initialization_type_claims(GetCurrentThreadEffectiveToken(), policy);
846
+ }
847
+
848
+ return STATUS_NOT_FOUND;
849
+ }
850
+
851
+ LONG WINAPI __acrt_AppPolicyGetShowDeveloperDiagnosticInternal(_Out_ AppPolicyShowDeveloperDiagnostic* policy)
852
+ {
853
+ if (auto const app_policy_get_show_developer_diagnostic_claims = try_get_AppPolicyGetShowDeveloperDiagnostic())
854
+ {
855
+ return app_policy_get_show_developer_diagnostic_claims(GetCurrentThreadEffectiveToken(), policy);
856
+ }
857
+
858
+ return STATUS_NOT_FOUND;
859
+ }
860
+
861
+ LONG WINAPI __acrt_AppPolicyGetWindowingModelInternal(_Out_ AppPolicyWindowingModel* policy)
862
+ {
863
+ if (auto const app_policy_get_windowing_model_claims = try_get_AppPolicyGetWindowingModel())
864
+ {
865
+ return app_policy_get_windowing_model_claims(GetCurrentThreadEffectiveToken(), policy);
866
+ }
867
+
868
+ return STATUS_NOT_FOUND;
869
+ }
870
+
871
+ extern "C" BOOL WINAPI __acrt_SetThreadStackGuarantee(PULONG const stack_size_in_bytes)
872
+ {
873
+ if (auto const set_thread_stack_guarantee = try_get_SetThreadStackGuarantee())
874
+ {
875
+ return set_thread_stack_guarantee(stack_size_in_bytes);
876
+ }
877
+
878
+ return FALSE;
879
+ }
880
+
881
+ extern "C" bool __cdecl __acrt_can_show_message_box()
882
+ {
883
+ bool can_show_message_box = false;
884
+ if (__acrt_get_windowing_model_policy() == windowing_model_policy_hwnd
885
+ && try_get_MessageBoxA() != nullptr
886
+ && try_get_MessageBoxW() != nullptr)
887
+ {
888
+ can_show_message_box = true;
889
+ }
890
+ return can_show_message_box;
891
+ }
892
+
893
+ extern "C" bool __cdecl __acrt_can_use_vista_locale_apis()
894
+ {
895
+ return try_get_CompareStringEx() != nullptr;
896
+ }
897
+
898
+ // This function simply attempts to get each of the locale-related APIs. This
899
+ // allows a caller to "pre-load" the modules in which these APIs are hosted. We
900
+ // use this in the _wsetlocale implementation to avoid calls to LoadLibrary while
901
+ // the locale lock is held.
902
+ extern "C" void __cdecl __acrt_eagerly_load_locale_apis()
903
+ {
904
+ try_get_AreFileApisANSI();
905
+ try_get_CompareStringEx();
906
+ try_get_EnumSystemLocalesEx();
907
+ try_get_GetDateFormatEx();
908
+ try_get_GetLocaleInfoEx();
909
+ try_get_GetTimeFormatEx();
910
+ try_get_GetUserDefaultLocaleName();
911
+ try_get_IsValidLocaleName();
912
+ try_get_LCMapStringEx();
913
+ try_get_LCIDToLocaleName();
914
+ try_get_LocaleNameToLCID();
915
+ }
916
+
917
+ extern "C" bool __cdecl __acrt_tls2_supported()
918
+ {
919
+ return (try_get_FlsGetValue2() != nullptr);
920
+ }
921
+
922
+ extern "C" bool __cdecl __acrt_can_use_xstate_apis()
923
+ {
924
+ return try_get_LocateXStateFeature() != nullptr;
925
+ }
926
+
927
+ extern "C" HWND __cdecl __acrt_get_parent_window()
928
+ {
929
+ auto const get_active_window = try_get_GetActiveWindow();
930
+ if (!get_active_window)
931
+ {
932
+ return nullptr;
933
+ }
934
+
935
+ HWND const active_window = get_active_window();
936
+ if (!active_window)
937
+ {
938
+ return nullptr;
939
+ }
940
+
941
+ auto const get_last_active_popup = try_get_GetLastActivePopup();
942
+ if (!get_last_active_popup)
943
+ {
944
+ return active_window;
945
+ }
946
+
947
+ return get_last_active_popup(active_window);
948
+ }
949
+
950
+ extern "C" bool __cdecl __acrt_is_interactive()
951
+ {
952
+ auto const get_process_window_station = try_get_GetProcessWindowStation();
953
+ if (!get_process_window_station)
954
+ {
955
+ return true;
956
+ }
957
+
958
+ auto const get_user_object_information = try_get_GetUserObjectInformationW();
959
+ if (!get_user_object_information)
960
+ {
961
+ return true;
962
+ }
963
+
964
+ HWINSTA const hwinsta = get_process_window_station();
965
+ if (!hwinsta)
966
+ {
967
+ return false;
968
+ }
969
+
970
+ USEROBJECTFLAGS uof{};
971
+ if (!get_user_object_information(hwinsta, UOI_FLAGS, &uof, sizeof(uof), nullptr))
972
+ {
973
+ return false;
974
+ }
975
+
976
+ if ((uof.dwFlags & WSF_VISIBLE) == 0)
977
+ {
978
+ return false;
979
+ }
980
+
981
+ return true;
982
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/CompareStringA.cpp ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *a_cmp.c - A version of CompareString.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Use either CompareStringA or CompareStringW depending on which is
8
+ * available
9
+ *
10
+ *******************************************************************************/
11
+ #include <corecrt_internal.h>
12
+
13
+
14
+
15
+ /***
16
+ *int __cdecl __acrt_CompareStringA - Get type information about an ANSI string.
17
+ *
18
+ *Purpose:
19
+ * Internal support function. Assumes info in ANSI string format. Tries
20
+ * to use NLS API call CompareStringA if available and uses CompareStringW
21
+ * if it must. If neither are available it fails and returns 0.
22
+ *
23
+ *Entry:
24
+ * LPCWSTR LocaleName - locale context for the comparison.
25
+ * DWORD dwCmpFlags - see NT\Chicago docs
26
+ * LPCSTR lpStringn - multibyte string to be compared
27
+ * int cchCountn - char (byte) count (NOT including nullptr)
28
+ * (-1 if nullptr terminated)
29
+ * int code_page - for MB/WC conversion. If 0, use __lc_codepage
30
+ *
31
+ *Exit:
32
+ * Success: 1 - if lpString1 < lpString2
33
+ * 2 - if lpString1 == lpString2
34
+ * 3 - if lpString1 > lpString2
35
+ * Failure: 0
36
+ *
37
+ *Exceptions:
38
+ *
39
+ *******************************************************************************/
40
+
41
+ static int __cdecl InternalCompareStringA(
42
+ _locale_t plocinfo,
43
+ LPCWSTR LocaleName,
44
+ DWORD dwCmpFlags,
45
+ PCCH lpString1,
46
+ int cchCount1,
47
+ PCCH lpString2,
48
+ int cchCount2,
49
+ int code_page
50
+ ) throw()
51
+ {
52
+ /*
53
+ * CompareString will compare past nullptr. Must find nullptr if in string
54
+ * before cchCountn chars.
55
+ */
56
+
57
+ if (cchCount1 > 0)
58
+ cchCount1 = static_cast<int>(__strncnt(lpString1, cchCount1));
59
+ else if (cchCount1 < -1)
60
+ return FALSE;
61
+
62
+ if (cchCount2 > 0)
63
+ cchCount2 = static_cast<int>(__strncnt(lpString2, cchCount2));
64
+ else if (cchCount2 < -1)
65
+ return FALSE;
66
+
67
+
68
+ int buff_size1;
69
+ int buff_size2;
70
+
71
+ /*
72
+ * Use __lc_codepage for conversion if code_page not specified
73
+ */
74
+
75
+ if (0 == code_page)
76
+ code_page = plocinfo->locinfo->_public._locale_lc_codepage;
77
+
78
+ /*
79
+ * Special case: at least one count is zero
80
+ */
81
+
82
+ if (!cchCount1 || !cchCount2)
83
+ {
84
+ unsigned char *cp; // char pointer
85
+ CPINFO cpInfo; // struct for use with GetCPInfo
86
+
87
+ /* both strings zero */
88
+ if (cchCount1 == cchCount2)
89
+ return 2;
90
+
91
+ /* string 1 greater */
92
+ if (cchCount2 > 1)
93
+ return 1;
94
+
95
+ /* string 2 greater */
96
+ if (cchCount1 > 1)
97
+ return 3;
98
+
99
+ /*
100
+ * one has zero count, the other has a count of one
101
+ * - if the one count is a naked lead byte, the strings are equal
102
+ * - otherwise it is a single character and they are unequal
103
+ */
104
+
105
+ if (GetCPInfo(code_page, &cpInfo) == FALSE)
106
+ return 0;
107
+
108
+ _ASSERTE(cchCount1==0 && cchCount2==1 || cchCount1==1 && cchCount2==0);
109
+
110
+ /* string 1 has count of 1 */
111
+ if (cchCount1 > 0)
112
+ {
113
+ if (cpInfo.MaxCharSize < 2)
114
+ return 3;
115
+
116
+ for ( cp = (unsigned char *)cpInfo.LeadByte ;
117
+ cp[0] && cp[1] ;
118
+ cp += 2 )
119
+ if ( (*(unsigned char *)lpString1 >= cp[0]) &&
120
+ (*(unsigned char *)lpString1 <= cp[1]) )
121
+ return 2;
122
+
123
+ return 3;
124
+ }
125
+
126
+ /* string 2 has count of 1 */
127
+ if (cchCount2 > 0)
128
+ {
129
+ if (cpInfo.MaxCharSize < 2)
130
+ return 1;
131
+
132
+ for ( cp = (unsigned char *)cpInfo.LeadByte ;
133
+ cp[0] && cp[1] ;
134
+ cp += 2 )
135
+ if ( (*(unsigned char *)lpString2 >= cp[0]) &&
136
+ (*(unsigned char *)lpString2 <= cp[1]) )
137
+ return 2;
138
+
139
+ return 1;
140
+ }
141
+ }
142
+
143
+ /*
144
+ * Convert strings and return the requested information.
145
+ */
146
+
147
+ /* find out how big a buffer we need (includes nullptr if any) */
148
+ if ( 0 == (buff_size1 = __acrt_MultiByteToWideChar( code_page,
149
+ MB_PRECOMPOSED |
150
+ MB_ERR_INVALID_CHARS,
151
+ lpString1,
152
+ cchCount1,
153
+ nullptr,
154
+ 0 )) )
155
+ return 0;
156
+
157
+ /* allocate enough space for chars */
158
+ __crt_scoped_stack_ptr<wchar_t> wbuffer1(_malloca_crt_t(wchar_t, buff_size1));
159
+ if (wbuffer1.get() == nullptr)
160
+ return 0;
161
+
162
+ /* do the conversion */
163
+ if ( 0 == __acrt_MultiByteToWideChar( code_page,
164
+ MB_PRECOMPOSED,
165
+ lpString1,
166
+ cchCount1,
167
+ wbuffer1.get(),
168
+ buff_size1 ) )
169
+ return 0;
170
+
171
+ /* find out how big a buffer we need (includes nullptr if any) */
172
+ if ( 0 == (buff_size2 = __acrt_MultiByteToWideChar( code_page,
173
+ MB_PRECOMPOSED |
174
+ MB_ERR_INVALID_CHARS,
175
+ lpString2,
176
+ cchCount2,
177
+ nullptr,
178
+ 0 )) )
179
+ return 0;
180
+
181
+ /* allocate enough space for chars */
182
+ __crt_scoped_stack_ptr<wchar_t> const wbuffer2(_malloca_crt_t(wchar_t, buff_size2));
183
+ if (wbuffer2.get() == nullptr)
184
+ return 0;
185
+
186
+ int const actual_size = __acrt_MultiByteToWideChar(
187
+ code_page,
188
+ MB_PRECOMPOSED,
189
+ lpString2,
190
+ cchCount2,
191
+ wbuffer2.get(),
192
+ buff_size2);
193
+
194
+ if (actual_size == 0)
195
+ return 0;
196
+
197
+ return __acrt_CompareStringEx(
198
+ LocaleName,
199
+ dwCmpFlags,
200
+ wbuffer1.get(),
201
+ buff_size1,
202
+ wbuffer2.get(),
203
+ buff_size2,
204
+ nullptr,
205
+ nullptr,
206
+ 0);
207
+ }
208
+
209
+ extern "C" int __cdecl __acrt_CompareStringA(
210
+ _locale_t const locale,
211
+ LPCWSTR const locale_name,
212
+ DWORD const compare_flags,
213
+ PCCH const string1,
214
+ int const string1_count,
215
+ PCCH const string2,
216
+ int const string2_count,
217
+ int const code_page
218
+ )
219
+ {
220
+ _LocaleUpdate locale_update(locale);
221
+
222
+ return InternalCompareStringA(
223
+ locale_update.GetLocaleT(),
224
+ locale_name,
225
+ compare_flags,
226
+ string1,
227
+ string1_count,
228
+ string2,
229
+ string2_count,
230
+ code_page
231
+ );
232
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/CompareStringW.cpp ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *w_cmp.c - W versions of CompareString.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Wrapper for CompareStringW.
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+
12
+ /***
13
+ *int __cdecl __acrt_CompareStringW - Get type information about a wide string.
14
+ *
15
+ *Purpose:
16
+ * Internal support function. Assumes info in wide string format.
17
+ *
18
+ *Entry:
19
+ * LPCWSTR LocaleName - locale context for the comparison.
20
+ * DWORD dwCmpFlags - see NT\Chicago docs
21
+ * LPCWSTR lpStringn - wide string to be compared
22
+ * int cchCountn - wide char (word) count (NOT including nullptr)
23
+ * (-1 if nullptr terminated)
24
+ *
25
+ *Exit:
26
+ * Success: 1 - if lpString1 < lpString2
27
+ * 2 - if lpString1 == lpString2
28
+ * 3 - if lpString1 > lpString2
29
+ * Failure: 0
30
+ *
31
+ *Exceptions:
32
+ *
33
+ *******************************************************************************/
34
+
35
+ extern "C" int __cdecl __acrt_CompareStringW(
36
+ LPCWSTR LocaleName,
37
+ DWORD dwCmpFlags,
38
+ PCWCH lpString1,
39
+ int cchCount1,
40
+ PCWCH lpString2,
41
+ int cchCount2
42
+ )
43
+ {
44
+ /*
45
+ * CompareString will compare past nullptr. Must find nullptr if in string
46
+ * before cchCountn wide characters.
47
+ */
48
+
49
+ if (cchCount1 > 0)
50
+ cchCount1= (int) wcsnlen(lpString1, cchCount1);
51
+ if (cchCount2 > 0)
52
+ cchCount2= (int) wcsnlen(lpString2, cchCount2);
53
+
54
+ if (!cchCount1 || !cchCount2)
55
+ return (cchCount1 - cchCount2 == 0) ? 2 :
56
+ (cchCount1 - cchCount2 < 0) ? 1 : 3;
57
+
58
+ return __acrt_CompareStringEx( LocaleName,
59
+ dwCmpFlags,
60
+ lpString1,
61
+ cchCount1,
62
+ lpString2,
63
+ cchCount2,
64
+ nullptr,
65
+ nullptr,
66
+ 0);
67
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/GetLocaleInfoA.cpp ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // GetLocaleInfoA.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the CRT-internal GetLocaleInfoA implementation.
7
+ //
8
+ #include <corecrt_internal.h>
9
+
10
+
11
+
12
+ // Wraps a call to GetLocaleInfoEx and translates the result into a narrow string.
13
+ _Success_(return > 0)
14
+ static int __cdecl InternalGetLocaleInfoA(
15
+ _locale_t const locale,
16
+ PCWSTR const locale_name,
17
+ LCTYPE const locale_type,
18
+ _Out_writes_z_(result_size) char* const result,
19
+ int const result_size
20
+ )
21
+ {
22
+ _LocaleUpdate locale_update(locale);
23
+
24
+ int const code_page = locale_update.GetLocaleT()->locinfo->_public._locale_lc_codepage;
25
+
26
+ int const buffer_size = __acrt_GetLocaleInfoEx(locale_name, locale_type, nullptr, 0);
27
+ if (buffer_size == 0)
28
+ return 0;
29
+
30
+ __crt_scoped_stack_ptr<wchar_t> const buffer(_malloca_crt_t(wchar_t, buffer_size));
31
+ if (buffer.get() == nullptr)
32
+ return 0;
33
+
34
+ if (__acrt_GetLocaleInfoEx(locale_name, locale_type, buffer.get(), buffer_size) == 0)
35
+ return 0;
36
+
37
+ // Convert from wide to narrow strings:
38
+ return __acrt_WideCharToMultiByte(
39
+ code_page,
40
+ 0,
41
+ buffer.get(),
42
+ -1,
43
+ result_size != 0 ? result : nullptr,
44
+ result_size,
45
+ nullptr,
46
+ nullptr);
47
+ }
48
+
49
+
50
+
51
+ // Gets locale information appropriate for use in the setlocale initialization
52
+ // functions. In particular, wide locale strings can be converted to narrow
53
+ // strings or numeric values depending on the value of the lc_type parameter.
54
+ //
55
+ // The void_result must be reinterpretable as a pointer to one of the following
56
+ // types, depending on the value of LC_TYPE:
57
+ //
58
+ // * LC_STR_TYPE: char*
59
+ // * LC_WSTR_TYPE: wchar_t*
60
+ // * LC_INT_TYPE: unsigned char
61
+ //
62
+ // For the first two cases, where a pointer to a pointer is passed, if the
63
+ // function succeeds, the caller is responsible for freeing the pointed-to
64
+ // buffer.
65
+ //
66
+ // Returns 0 on success; -1 on failure.
67
+ //
68
+ // Future optimization: When converting a large number of wide strings to
69
+ // multibyte, we do not need to query the size of the result. We can convert
70
+ // them one after another into a large character buffer.
71
+ int __cdecl __acrt_GetLocaleInfoA(
72
+ _locale_t const locale,
73
+ int const lc_type,
74
+ wchar_t const* const locale_name,
75
+ LCTYPE const locale_type,
76
+ void* const void_result
77
+ )
78
+ {
79
+ *static_cast<void**>(void_result) = nullptr;
80
+
81
+ if (lc_type == LC_STR_TYPE)
82
+ {
83
+ char** const char_result = static_cast<char**>(void_result);
84
+
85
+ int const local_buffer_size = 128;
86
+ char local_buffer[local_buffer_size];
87
+
88
+ int const local_length = InternalGetLocaleInfoA(
89
+ locale, locale_name, locale_type, local_buffer, local_buffer_size);
90
+
91
+ if (local_length != 0)
92
+ {
93
+ *char_result = _calloc_crt_t(char, local_length).detach();
94
+ if (*char_result == nullptr)
95
+ return -1;
96
+
97
+ _ERRCHECK(strncpy_s(*char_result, local_length, local_buffer, local_length - 1));
98
+ return 0;
99
+ }
100
+
101
+ if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
102
+ return -1;
103
+
104
+ // If the buffer size was too small, compute the required size and use a
105
+ // dynamically allocated buffer:
106
+ int const required_length = InternalGetLocaleInfoA(
107
+ locale, locale_name, locale_type, nullptr, 0);
108
+
109
+ if (required_length == 0)
110
+ return -1;
111
+
112
+ __crt_unique_heap_ptr<char> dynamic_buffer(_calloc_crt_t(char, required_length));
113
+ if (dynamic_buffer.get() == nullptr)
114
+ return -1;
115
+
116
+ int const actual_length = InternalGetLocaleInfoA(
117
+ locale, locale_name, locale_type, dynamic_buffer.get(), required_length);
118
+
119
+ if (actual_length == 0)
120
+ return -1;
121
+
122
+ *char_result = dynamic_buffer.detach();
123
+ return 0;
124
+ }
125
+ else if (lc_type == LC_WSTR_TYPE)
126
+ {
127
+ wchar_t** const wchar_result = static_cast<wchar_t**>(void_result);
128
+
129
+ int const required_length = __acrt_GetLocaleInfoEx(locale_name, locale_type, nullptr, 0);
130
+ if (required_length == 0)
131
+ return -1;
132
+
133
+ __crt_unique_heap_ptr<wchar_t> dynamic_buffer(_calloc_crt_t(wchar_t, required_length));
134
+ if (dynamic_buffer.get() == nullptr)
135
+ return -1;
136
+
137
+ int const actual_length = __acrt_GetLocaleInfoEx(
138
+ locale_name, locale_type, dynamic_buffer.get(), required_length);
139
+
140
+ if (actual_length == 0)
141
+ return -1;
142
+
143
+ *wchar_result = dynamic_buffer.detach();
144
+ return 0;
145
+ }
146
+ else if (lc_type == LC_INT_TYPE)
147
+ {
148
+ unsigned char* const uchar_result = static_cast<unsigned char*>(void_result);
149
+
150
+ DWORD value = 0;
151
+ int const actual_length = __acrt_GetLocaleInfoEx(
152
+ locale_name,
153
+ locale_type | LOCALE_RETURN_NUMBER,
154
+ reinterpret_cast<wchar_t*>(&value),
155
+ sizeof(value) / sizeof(wchar_t));
156
+
157
+ if (actual_length == 0)
158
+ return -1;
159
+
160
+ *uchar_result = static_cast<unsigned char>(value);
161
+ return 0;
162
+ }
163
+
164
+ return -1;
165
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/GetStringTypeA.cpp ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *a_str.c - A version of GetStringType.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Use either GetStringTypeA or GetStringTypeW depending on which is
8
+ * unstubbed.
9
+ *
10
+ *******************************************************************************/
11
+
12
+ #include <corecrt_internal.h>
13
+ #include <locale.h>
14
+
15
+ /***
16
+ *int __cdecl __acrt_GetStringTypeA - Get type information about an ANSI string.
17
+ *
18
+ *Purpose:
19
+ * Internal support function. Assumes info in ANSI string format. Tries
20
+ * to use NLS API call GetStringTypeA if available and uses GetStringTypeW
21
+ * if it must. If neither are available it fails and returns FALSE.
22
+ *
23
+ *Entry:
24
+ * DWORD dwInfoType - see NT\Chicago docs
25
+ * PCCH lpSrcStr - char (byte) string for which character types
26
+ * are requested
27
+ * int cchSrc - char (byte) count of lpSrcStr (including nullptr
28
+ * if any)
29
+ * LPWORD lpCharType - word array to receive character type information
30
+ * (must be twice the size of lpSrcStr)
31
+ * int code_page - for MB/WC conversion. If 0, use __lc_codepage
32
+ * BOOL bError - TRUE if MB_ERR_INVALID_CHARS set on call to
33
+ * MultiByteToWideChar when GetStringTypeW used.
34
+ *
35
+ *Exit:
36
+ * Success: TRUE
37
+ * Failure: FALSE
38
+ *
39
+ *Exceptions:
40
+ *
41
+ *******************************************************************************/
42
+
43
+ extern "C" BOOL __cdecl __acrt_GetStringTypeA(
44
+ _locale_t const locale,
45
+ DWORD const info_type,
46
+ LPCSTR const string,
47
+ int const string_size_in_bytes,
48
+ unsigned short* const char_type,
49
+ int const code_page,
50
+ BOOL const error
51
+ )
52
+ {
53
+ _LocaleUpdate locale_update(locale);
54
+
55
+ // Convert string and return the requested information. Note that
56
+ // we are converting to a wide character string so there is not a
57
+ // one-to-one correspondence between number of multibyte chars in the
58
+ // input string and the number of wide chars in the buffer. However,
59
+ // there had *better be* a one-to-one correspondence between the
60
+ // number of multibyte characters and the number of WORDs in the
61
+ // return buffer.
62
+
63
+ // Use __lc_codepage for conversion if code_page not specified:
64
+ int const actual_code_page = code_page != 0
65
+ ? code_page
66
+ : locale_update.GetLocaleT()->locinfo->_public._locale_lc_codepage;
67
+
68
+ // Find out how big a buffer we need:
69
+ int const required_extent = __acrt_MultiByteToWideChar(
70
+ actual_code_page,
71
+ error ? (MB_PRECOMPOSED | MB_ERR_INVALID_CHARS) : MB_PRECOMPOSED,
72
+ string,
73
+ string_size_in_bytes,
74
+ nullptr,
75
+ 0);
76
+
77
+ if (required_extent == 0)
78
+ return FALSE;
79
+
80
+ // Allocate enough space for the wide character string:
81
+ __crt_scoped_stack_ptr<wchar_t> buffer(_malloca_crt_t(wchar_t, required_extent));
82
+ if (buffer.get() == nullptr)
83
+ return FALSE;
84
+
85
+ memset(buffer.get(), 0, sizeof(wchar_t) * required_extent);
86
+
87
+ // Do the conversion:
88
+ int const actual_extent = __acrt_MultiByteToWideChar(
89
+ actual_code_page,
90
+ MB_PRECOMPOSED,
91
+ string,
92
+ string_size_in_bytes,
93
+ buffer.get(),
94
+ required_extent);
95
+
96
+ if (actual_extent == 0)
97
+ return FALSE;
98
+
99
+ return GetStringTypeW(info_type, buffer.get(), actual_extent, char_type);
100
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/LCMapStringA.cpp ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *a_map.c - A version of LCMapString.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Use either LCMapStringA or LCMapStringW depending on which is available
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+ #include <locale.h>
12
+
13
+ /***
14
+ *int __cdecl __acrt_LCMapStringA - Get type information about an ANSI string.
15
+ *
16
+ *Purpose:
17
+ * Internal support function. Assumes info in ANSI string format. Tries
18
+ * to use NLS API call LCMapStringA if available and uses LCMapStringW
19
+ * if it must. If neither are available it fails and returns 0.
20
+ *
21
+ *Entry:
22
+ * LPCWSTR LocaleName - locale context for the comparison.
23
+ * DWORD dwMapFlags - see NT\Chicago docs
24
+ * PCCH cchSrc - wide char (word) count of input string
25
+ * (including nullptr if any)
26
+ * (-1 if nullptr terminated)
27
+ * PCH lpDestStr - pointer to memory to store mapping
28
+ * int cchDest - char (byte) count of buffer (including nullptr)
29
+ * int code_page - for MB/WC conversion. If 0, use __lc_codepage
30
+ * BOOL bError - TRUE if MB_ERR_INVALID_CHARS set on call to
31
+ * MultiByteToWideChar when GetStringTypeW used.
32
+ *
33
+ *Exit:
34
+ * Success: number of chars written to lpDestStr (including nullptr)
35
+ * Failure: 0
36
+ *
37
+ *Exceptions:
38
+ *
39
+ *******************************************************************************/
40
+
41
+ static _Success_(return != 0) int __cdecl __acrt_LCMapStringA_stat(
42
+ _In_opt_ _locale_t plocinfo,
43
+ _In_ LPCWSTR LocaleName,
44
+ _In_ DWORD dwMapFlags,
45
+ _In_CRT_NLS_string_(cchSrc) PCCH lpSrcStr,
46
+ _In_ int cchSrc,
47
+ _Out_writes_opt_(cchDest) PCH lpDestStr,
48
+ _In_ int cchDest,
49
+ _In_ int code_page,
50
+ _In_ BOOL bError
51
+ )
52
+ {
53
+ // LCMapString will map past the null terminator. We must find the null
54
+ // terminator if it occurs in the string before cchSrc characters
55
+ // and cap the number of characters to be considered.
56
+ if (cchSrc > 0)
57
+ {
58
+ int cchSrcCnt = static_cast<int>(__strncnt(lpSrcStr, cchSrc));
59
+
60
+ // Include the null terminator if the source string is terminated within
61
+ // the buffer.
62
+ if (cchSrcCnt < cchSrc)
63
+ {
64
+ cchSrc = cchSrcCnt + 1;
65
+ }
66
+ else
67
+ {
68
+ cchSrc = cchSrcCnt;
69
+ }
70
+ }
71
+
72
+ int retval = 0;
73
+ int inbuff_size;
74
+ int outbuff_size;
75
+
76
+ /*
77
+ * Convert string and return the requested information. Note that
78
+ * we are converting to a wide string so there is not a
79
+ * one-to-one correspondence between number of wide chars in the
80
+ * input string and the number of *bytes* in the buffer. However,
81
+ * there had *better be* a one-to-one correspondence between the
82
+ * number of wide characters and the number of multibyte characters
83
+ * or the resulting mapped string will be worthless to the user.
84
+ */
85
+
86
+ /*
87
+ * Use __lc_codepage for conversion if code_page not specified
88
+ */
89
+
90
+ if (0 == code_page)
91
+ code_page = plocinfo->locinfo->_public._locale_lc_codepage;
92
+
93
+ /* find out how big a buffer we need (includes nullptr if any) */
94
+ if ( 0 == (inbuff_size =
95
+ __acrt_MultiByteToWideChar( code_page,
96
+ bError ? MB_PRECOMPOSED |
97
+ MB_ERR_INVALID_CHARS :
98
+ MB_PRECOMPOSED,
99
+ lpSrcStr,
100
+ cchSrc,
101
+ nullptr,
102
+ 0 )) )
103
+ return 0;
104
+
105
+ /* allocate enough space for wide chars */
106
+ __crt_scoped_stack_ptr<wchar_t> const inwbuffer(_malloca_crt_t(wchar_t, inbuff_size));
107
+ if (!inwbuffer)
108
+ return 0;
109
+
110
+ /* do the conversion */
111
+ if ( 0 == __acrt_MultiByteToWideChar( code_page,
112
+ MB_PRECOMPOSED,
113
+ lpSrcStr,
114
+ cchSrc,
115
+ inwbuffer.get(),
116
+ inbuff_size) )
117
+ return retval;
118
+
119
+ /* get size required for string mapping */
120
+ if ( 0 == (retval = __acrt_LCMapStringEx( LocaleName,
121
+ dwMapFlags,
122
+ inwbuffer.get(),
123
+ inbuff_size,
124
+ nullptr,
125
+ 0,
126
+ nullptr,
127
+ nullptr,
128
+ 0)) )
129
+ return retval;
130
+
131
+ if (dwMapFlags & LCMAP_SORTKEY) {
132
+ /* retval is size in BYTES */
133
+
134
+ if (0 != cchDest) {
135
+
136
+ if (retval > cchDest)
137
+ return 0;
138
+
139
+ /* do string mapping */
140
+ // The buffer overflow warning here is due to an inadequate annotation
141
+ // on __acrt_LCMapStringEx. When the map flags include LCMAP_SORTKEY,
142
+ // the destination buffer is actually required to be an array of bytes,
143
+ // despite the type of the buffer being a wchar_t*.
144
+ __pragma(warning(suppress: __WARNING_BUFFER_OVERFLOW))
145
+ if ( 0 == (retval = __acrt_LCMapStringEx( LocaleName,
146
+ dwMapFlags,
147
+ inwbuffer.get(),
148
+ inbuff_size,
149
+ reinterpret_cast<PWCH>(lpDestStr),
150
+ cchDest,
151
+ nullptr,
152
+ nullptr,
153
+ 0)) )
154
+ return retval;
155
+ }
156
+ }
157
+ else {
158
+ /* retval is size in wide chars */
159
+
160
+ outbuff_size = retval;
161
+
162
+ /* allocate enough space for wide chars (includes nullptr if any) */
163
+ __crt_scoped_stack_ptr<wchar_t> const outwbuffer(_malloca_crt_t(wchar_t, outbuff_size));
164
+ if (!outwbuffer)
165
+ return 0;
166
+
167
+ /* do string mapping */
168
+ if ( 0 == (retval = __acrt_LCMapStringEx( LocaleName,
169
+ dwMapFlags,
170
+ inwbuffer.get(),
171
+ inbuff_size,
172
+ outwbuffer.get(),
173
+ outbuff_size,
174
+ nullptr,
175
+ nullptr,
176
+ 0)) )
177
+ return retval;
178
+
179
+ if (0 == cchDest) {
180
+ /* get size required */
181
+ if ( 0 == (retval =
182
+ __acrt_WideCharToMultiByte( code_page,
183
+ 0,
184
+ outwbuffer.get(),
185
+ outbuff_size,
186
+ nullptr,
187
+ 0,
188
+ nullptr,
189
+ nullptr )) )
190
+ return retval;
191
+ }
192
+ else {
193
+ /* convert mapping */
194
+ if ( 0 == (retval =
195
+ __acrt_WideCharToMultiByte( code_page,
196
+ 0,
197
+ outwbuffer.get(),
198
+ outbuff_size,
199
+ lpDestStr,
200
+ cchDest,
201
+ nullptr,
202
+ nullptr )) )
203
+ return retval;
204
+ }
205
+ }
206
+
207
+ return retval;
208
+ }
209
+
210
+ extern "C" int __cdecl __acrt_LCMapStringA(
211
+ _locale_t const plocinfo,
212
+ PCWSTR const LocaleName,
213
+ DWORD const dwMapFlags,
214
+ PCCH const lpSrcStr,
215
+ int const cchSrc,
216
+ PCH const lpDestStr,
217
+ int const cchDest,
218
+ int const code_page,
219
+ BOOL const bError
220
+ )
221
+ {
222
+ _LocaleUpdate _loc_update(plocinfo);
223
+
224
+ return __acrt_LCMapStringA_stat(
225
+ _loc_update.GetLocaleT(),
226
+ LocaleName,
227
+ dwMapFlags,
228
+ lpSrcStr,
229
+ cchSrc,
230
+ lpDestStr,
231
+ cchDest,
232
+ code_page,
233
+ bError
234
+ );
235
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/LCMapStringW.cpp ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *w_map.c - W version of LCMapString.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Wrapper for LCMapStringW.
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+
12
+ /***
13
+ *int __cdecl __acrt_LCMapStringW - Get type information about a wide string.
14
+ *
15
+ *Purpose:
16
+ * Internal support function. Assumes info in wide string format.
17
+ *
18
+ *Entry:
19
+ * LPCWSTR LocaleName - locale context for the comparison.
20
+ * DWORD dwMapFlags - see NT\Chicago docs
21
+ * LPCWSTR lpSrcStr - pointer to string to be mapped
22
+ * int cchSrc - wide char (word) count of input string
23
+ * (including nullptr if any)
24
+ * (-1 if nullptr terminated)
25
+ * LPWSTR lpDestStr - pointer to memory to store mapping
26
+ * int cchDest - wide char (word) count of buffer (including nullptr)
27
+ *
28
+ * NOTE: if LCMAP_SORTKEY is specified, then cchDest refers to number
29
+ * of BYTES, not number of wide chars. The return string will be
30
+ * a series of bytes with a nullptr byte terminator.
31
+ *
32
+ *Exit:
33
+ * Success: if LCMAP_SORKEY:
34
+ * number of bytes written to lpDestStr (including nullptr byte
35
+ * terminator)
36
+ * else
37
+ * number of wide characters written to lpDestStr (including
38
+ * nullptr)
39
+ * Failure: 0
40
+ *
41
+ *Exceptions:
42
+ *
43
+ *******************************************************************************/
44
+
45
+ extern "C" int __cdecl __acrt_LCMapStringW(
46
+ LPCWSTR const locale_name,
47
+ DWORD const map_flags,
48
+ LPCWSTR const source,
49
+ int source_count,
50
+ LPWSTR const destination,
51
+ int const destination_count
52
+ )
53
+ {
54
+ // LCMapString will map past the null terminator. We must find the null
55
+ // terminator if it occurs in the string before source_count characters
56
+ // and cap the number of characters to be considered.
57
+ if (source_count > 0)
58
+ {
59
+ int const source_length = static_cast<int>(wcsnlen(source, source_count));
60
+
61
+ // Include the null terminator if the source string is terminated within
62
+ // the buffer.
63
+ if (source_length < source_count)
64
+ {
65
+ source_count = source_length + 1;
66
+ }
67
+ else
68
+ {
69
+ source_count = source_length;
70
+ }
71
+ }
72
+
73
+ return __acrt_LCMapStringEx(locale_name, map_flags, source, source_count, destination, destination_count, nullptr, nullptr, 0);
74
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/ctype.cpp ADDED
@@ -0,0 +1,745 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // ctype.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Definition of the character classification data used by the character
7
+ // classification macros in <ctype.h>.
8
+ //
9
+ #include <corecrt_internal.h>
10
+ #include <ctype.h>
11
+
12
+
13
+
14
+ extern "C" unsigned short const* __cdecl __pwctype_func()
15
+ {
16
+ return _pwctype;
17
+ }
18
+
19
+ extern "C" unsigned short const* __cdecl __pctype_func()
20
+ {
21
+ // This is a leaf function and thus use of _LocaleUpdate is not required.
22
+ __acrt_ptd* const ptd{__acrt_getptd()};
23
+ __crt_locale_data* locale_info{ptd->_locale_info};
24
+
25
+ __acrt_update_locale_info(ptd, &locale_info);
26
+ return locale_info->_public._locale_pctype;
27
+ }
28
+
29
+
30
+
31
+ // NOTE: The characteristics stored in this lookup table are not always
32
+ // consistent with what is required by C. For example, GetStringTypeW returns
33
+ // _DIGIT and _PUNCT for U+00B2 and U+00B3 (SUBSCRIPT TWO and SUBSCRIPT THREE),
34
+ // but these categories are mutually exclusive in C. We have chosen to
35
+ // implement this lookup table for consistency with GetStringTypeW, rather than
36
+ // try to implement something completely different. We expect this is sufficient,
37
+ // as C character classification does not map well to Unicode.
38
+ extern "C" extern unsigned short const _wctype[]
39
+ {
40
+ 0, // -1 EOF
41
+ _CONTROL , // 00 (NUL)
42
+ _CONTROL , // 01 (SOH)
43
+ _CONTROL , // 02 (STX)
44
+ _CONTROL , // 03 (ETX)
45
+ _CONTROL , // 04 (EOT)
46
+ _CONTROL , // 05 (ENQ)
47
+ _CONTROL , // 06 (ACK)
48
+ _CONTROL , // 07 (BEL)
49
+ _CONTROL , // 08 (BS)
50
+ // \t is a blank character, but is not registered as _Blank on the table, because that will make it
51
+ //printable. Also Windows (via GetStringType()) considered all _BLANK characters to also be _PRINT characters,
52
+ //so does not have a way to specify blank, non-printable.
53
+ _SPACE | _CONTROL , // 09 (HT)
54
+ _SPACE | _CONTROL , // 0A (LF)
55
+ _SPACE | _CONTROL , // 0B (VT)
56
+ _SPACE | _CONTROL , // 0C (FF)
57
+ _SPACE | _CONTROL , // 0D (CR)
58
+ _CONTROL , // 0E (SI)
59
+ _CONTROL , // 0F (SO)
60
+ _CONTROL , // 10 (DLE)
61
+ _CONTROL , // 11 (DC1)
62
+ _CONTROL , // 12 (DC2)
63
+ _CONTROL , // 13 (DC3)
64
+ _CONTROL , // 14 (DC4)
65
+ _CONTROL , // 15 (NAK)
66
+ _CONTROL , // 16 (SYN)
67
+ _CONTROL , // 17 (ETB)
68
+ _CONTROL , // 18 (CAN)
69
+ _CONTROL , // 19 (EM)
70
+ _CONTROL , // 1A (SUB)
71
+ _CONTROL , // 1B (ESC)
72
+ _CONTROL , // 1C (FS)
73
+ _CONTROL , // 1D (GS)
74
+ _CONTROL , // 1E (RS)
75
+ _CONTROL , // 1F (US)
76
+ _SPACE | _BLANK , // 20 SPACE
77
+ _PUNCT , // 21 !
78
+ _PUNCT , // 22 "
79
+ _PUNCT , // 23 #
80
+ _PUNCT , // 24 $
81
+ _PUNCT , // 25 %
82
+ _PUNCT , // 26 &
83
+ _PUNCT , // 27 '
84
+ _PUNCT , // 28 (
85
+ _PUNCT , // 29 )
86
+ _PUNCT , // 2A *
87
+ _PUNCT , // 2B +
88
+ _PUNCT , // 2C ,
89
+ _PUNCT , // 2D -
90
+ _PUNCT , // 2E .
91
+ _PUNCT , // 2F /
92
+ _DIGIT | _HEX , // 30 0
93
+ _DIGIT | _HEX , // 31 1
94
+ _DIGIT | _HEX , // 32 2
95
+ _DIGIT | _HEX , // 33 3
96
+ _DIGIT | _HEX , // 34 4
97
+ _DIGIT | _HEX , // 35 5
98
+ _DIGIT | _HEX , // 36 6
99
+ _DIGIT | _HEX , // 37 7
100
+ _DIGIT | _HEX , // 38 8
101
+ _DIGIT | _HEX , // 39 9
102
+ _PUNCT , // 3A :
103
+ _PUNCT , // 3B ;
104
+ _PUNCT , // 3C <
105
+ _PUNCT , // 3D =
106
+ _PUNCT , // 3E >
107
+ _PUNCT , // 3F ?
108
+ _PUNCT , // 40 @
109
+ _UPPER | _HEX | C1_ALPHA , // 41 A
110
+ _UPPER | _HEX | C1_ALPHA , // 42 B
111
+ _UPPER | _HEX | C1_ALPHA , // 43 C
112
+ _UPPER | _HEX | C1_ALPHA , // 44 D
113
+ _UPPER | _HEX | C1_ALPHA , // 45 E
114
+ _UPPER | _HEX | C1_ALPHA , // 46 F
115
+ _UPPER | C1_ALPHA , // 47 G
116
+ _UPPER | C1_ALPHA , // 48 H
117
+ _UPPER | C1_ALPHA , // 49 I
118
+ _UPPER | C1_ALPHA , // 4A J
119
+ _UPPER | C1_ALPHA , // 4B K
120
+ _UPPER | C1_ALPHA , // 4C L
121
+ _UPPER | C1_ALPHA , // 4D M
122
+ _UPPER | C1_ALPHA , // 4E N
123
+ _UPPER | C1_ALPHA , // 4F O
124
+ _UPPER | C1_ALPHA , // 50 P
125
+ _UPPER | C1_ALPHA , // 51 Q
126
+ _UPPER | C1_ALPHA , // 52 R
127
+ _UPPER | C1_ALPHA , // 53 S
128
+ _UPPER | C1_ALPHA , // 54 T
129
+ _UPPER | C1_ALPHA , // 55 U
130
+ _UPPER | C1_ALPHA , // 56 V
131
+ _UPPER | C1_ALPHA , // 57 W
132
+ _UPPER | C1_ALPHA , // 58 X
133
+ _UPPER | C1_ALPHA , // 59 Y
134
+ _UPPER | C1_ALPHA , // 5A Z
135
+ _PUNCT , // 5B [
136
+ _PUNCT , // 5C \
137
+ _PUNCT , // 5D ]
138
+ _PUNCT , // 5E ^
139
+ _PUNCT , // 5F _
140
+ _PUNCT , // 60 `
141
+ _LOWER | _HEX | C1_ALPHA , // 61 a
142
+ _LOWER | _HEX | C1_ALPHA , // 62 b
143
+ _LOWER | _HEX | C1_ALPHA , // 63 c
144
+ _LOWER | _HEX | C1_ALPHA , // 64 d
145
+ _LOWER | _HEX | C1_ALPHA , // 65 e
146
+ _LOWER | _HEX | C1_ALPHA , // 66 f
147
+ _LOWER | C1_ALPHA , // 67 g
148
+ _LOWER | C1_ALPHA , // 68 h
149
+ _LOWER | C1_ALPHA , // 69 i
150
+ _LOWER | C1_ALPHA , // 6A j
151
+ _LOWER | C1_ALPHA , // 6B k
152
+ _LOWER | C1_ALPHA , // 6C l
153
+ _LOWER | C1_ALPHA , // 6D m
154
+ _LOWER | C1_ALPHA , // 6E n
155
+ _LOWER | C1_ALPHA , // 6F o
156
+ _LOWER | C1_ALPHA , // 70 p
157
+ _LOWER | C1_ALPHA , // 71 q
158
+ _LOWER | C1_ALPHA , // 72 r
159
+ _LOWER | C1_ALPHA , // 73 s
160
+ _LOWER | C1_ALPHA , // 74 t
161
+ _LOWER | C1_ALPHA , // 75 u
162
+ _LOWER | C1_ALPHA , // 76 v
163
+ _LOWER | C1_ALPHA , // 77 w
164
+ _LOWER | C1_ALPHA , // 78 x
165
+ _LOWER | C1_ALPHA , // 79 y
166
+ _LOWER | C1_ALPHA , // 7A z
167
+ _PUNCT , // 7B {
168
+ _PUNCT , // 7C |
169
+ _PUNCT , // 7D }
170
+ _PUNCT , // 7E ~
171
+ _CONTROL , // 7F (DEL)
172
+ _CONTROL , // 80 (XXX)
173
+ _CONTROL , // 81 (XXX)
174
+ _CONTROL , // 82 (BPH / BREAK PERMITTED HERE)
175
+ _CONTROL , // 83 (NBH / NO BREAK HERE)
176
+ _CONTROL , // 84 (IND / formerly known as INDEX)
177
+ _SPACE | _CONTROL , // 85 (NEL / NEXT LINE)
178
+ _CONTROL , // 86 (SSA / START OF SELCETED AREA)
179
+ _CONTROL , // 87 (ESA / END OF SELECTED AREA)
180
+ _CONTROL , // 88 (HTS / CHARACTER TABULATION SET)
181
+ _CONTROL , // 89 (HTJ / CHARACTER TABULATION WITH JUSTIFICATION)
182
+ _CONTROL , // 8A (VTS / LINE TABULATION SET)
183
+ _CONTROL , // 8B (PLD / PARTIAL LINE FORWARD)
184
+ _CONTROL , // 8C (PLU / PARTIAL LINE BACKWARD)
185
+ _CONTROL , // 8D (RI / REVERSE LINE FEED)
186
+ _CONTROL , // 8E (SS2 / SINGLE SHIFT TWO)
187
+ _CONTROL , // 8F (SS3 / SINGLE SHIFT THREE)
188
+ _CONTROL , // 90 (DCS / DEVICE CONTROL STRING)
189
+ _CONTROL , // 91 (PU1 / PRIVATE USE ONE)
190
+ _CONTROL , // 92 (PU2 / PRIVATE USE TWO)
191
+ _CONTROL , // 93 (STS / SET TRANSMIT STATE)
192
+ _CONTROL , // 94 (CCH / CANCEL CHARACTER)
193
+ _CONTROL , // 95 (MW / MESSAGE WAITING)
194
+ _CONTROL , // 96 (SPA / START OF GUARDED AREA)
195
+ _CONTROL , // 97 (EPA / END OF GUARDED AREA)
196
+ _CONTROL , // 98 (SOS / START OF STRING)
197
+ _CONTROL , // 99 (XXX)
198
+ _CONTROL , // 9A (SCI / SINGLE CHARACTER INTRODUCER)
199
+ _CONTROL , // 9B (CSI / CONTROL SEQUENCE INTRODUCER)
200
+ _CONTROL , // 9C (ST / STRING TERMINATOR)
201
+ _CONTROL , // 9D (OSC / OPERATING SYSTEM COMMAND)
202
+ _CONTROL , // 9E (PM / PRIVACY MESSAGE)
203
+ _CONTROL , // 9F (APC / APPLICATION PROGRAM COMMAND)
204
+ _SPACE , // A0 (NBSP / NO-BREAK SPACE)
205
+ _PUNCT , // A1 (INVERTED EXCLAMATION MARK)
206
+ _PUNCT , // A2 (CENT SIGN)
207
+ _PUNCT , // A3 (POUND SIGN)
208
+ _PUNCT , // A4 (CURRENCY SIGN)
209
+ _PUNCT , // A5 (YEN SIGN)
210
+ _PUNCT , // A6 (BROKEN BAR)
211
+ _PUNCT , // A7 (SECTION SIGN)
212
+ _PUNCT , // A8 (DIAERESIS)
213
+ _PUNCT , // A9 (COPYRIGHT SIGN)
214
+ _LOWER | _PUNCT | C1_ALPHA, // AA (FEMININE ORDINAL INDICATOR)
215
+ _PUNCT , // AB (LEFT-POINTING DOUBLE ANGLE QUOTATION MARK)
216
+ _PUNCT , // AC (NOT SIGN)
217
+ _PUNCT | _CONTROL , // AD (SOFT HYPHEN)
218
+ _PUNCT , // AE (REGISTERED SIGN)
219
+ _PUNCT , // AF (MACRON)
220
+ _PUNCT , // B0 (DEGREE SIGN)
221
+ _PUNCT , // B1 (PLUS-MINUS SIGN)
222
+ _DIGIT | _PUNCT , // B2 (SUPERSCRIPT TWO)
223
+ _DIGIT | _PUNCT , // B3 (SUPERSCRIPT THREE)
224
+ _PUNCT , // B4 (ACUTE ACCENT)
225
+ _LOWER | _PUNCT | C1_ALPHA, // B5 (MICRO SIGN)
226
+ _PUNCT , // B6 (PILCROW SIGN)
227
+ _PUNCT , // B7 (MIDDLE DOT)
228
+ _PUNCT , // B8 (CEDILLA)
229
+ _DIGIT | _PUNCT , // B9 (SUPERSCRIPT ONE)
230
+ _LOWER | _PUNCT | C1_ALPHA, // BA (MASCULINE ORDINAL INDICATOR)
231
+ _PUNCT , // BB (RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK)
232
+ _PUNCT , // BC (VULGAR FRACTION ONE QUARTER)
233
+ _PUNCT , // BD (VULGAR FRACTION ONE HALF)
234
+ _PUNCT , // BE (VULGAR FRACTION THREE QUARTERS)
235
+ _PUNCT , // BF (INVERTED QUESTION MARK)
236
+ _UPPER | C1_ALPHA , // C0 (LATIN CAPTIAL LETTER A WITH GRAVE)
237
+ _UPPER | C1_ALPHA , // C1 (LATIN CAPITAL LETTER A WITH ACUTE)
238
+ _UPPER | C1_ALPHA , // C2 (LATIN CAPITAL LETTER A WITH CIRCUMFLEX)
239
+ _UPPER | C1_ALPHA , // C3 (LATIN CAPITAL LETTER A WITH TILDE)
240
+ _UPPER | C1_ALPHA , // C4 (LATIN CAPITAL LETTER A WITH DIAERESIS)
241
+ _UPPER | C1_ALPHA , // C5 (LATIN CAPITAL LETTER A WITH RING ABOVE)
242
+ _UPPER | C1_ALPHA , // C6 (LATIN CAPITAL LETTER AE)
243
+ _UPPER | C1_ALPHA , // C7 (LATIN CAPITAL LETTER C WITH CEDILLA)
244
+ _UPPER | C1_ALPHA , // C8 (LATIN CAPITAL LETTER E WITH GRAVE)
245
+ _UPPER | C1_ALPHA , // C9 (LATIN CAPITAL LETTER E WITH ACUTE)
246
+ _UPPER | C1_ALPHA , // CA (LATIN CAPITAL LETTER E WITH CIRCUMFLEX)
247
+ _UPPER | C1_ALPHA , // CB (LATIN CAPITAL LETTER E WITH DIAERESIS)
248
+ _UPPER | C1_ALPHA , // CC (LATIN CAPITAL LETTER I WITH GRAVE)
249
+ _UPPER | C1_ALPHA , // CD (LATIN CAPITAL LETTER I WITH ACUTE)
250
+ _UPPER | C1_ALPHA , // CE (LATIN CAPITAL LETTER I WITH CIRCUMFLEX)
251
+ _UPPER | C1_ALPHA , // CF (LATIN CAPITAL LETTER I WITH DIAERESIS
252
+ _UPPER | C1_ALPHA , // D0 (LATIN CAPITAL LETTER ETH)
253
+ _UPPER | C1_ALPHA , // D1 (LATIN CAPITAL LETTER N WITH TILDE)
254
+ _UPPER | C1_ALPHA , // D2 (LATIN CAPITAL LETTER O WITH GRAVE)
255
+ _UPPER | C1_ALPHA , // D3 (LATIN CAPITAL LETTER O WITH ACUTE)
256
+ _UPPER | C1_ALPHA , // D4 (LATIN CAPITAL LETTER O WITH CIRCUMFLEX)
257
+ _UPPER | C1_ALPHA , // D5 (LATIN CAPITAL LETTER O WITH TILDE)
258
+ _UPPER | C1_ALPHA , // D6 (LATIN CAPITAL LETTER O WITH DIAERESIS)
259
+ _PUNCT , // D7 (MULTIPLICATION SIGN)
260
+ _UPPER | C1_ALPHA , // D8 (LATIN CAPITAL LETTER O WITH STROKE)
261
+ _UPPER | C1_ALPHA , // D9 (LATIN CAPITAL LETTER U WITH GRAVE)
262
+ _UPPER | C1_ALPHA , // DA (LATIN CAPITAL LETTER U WITH ACUTE)
263
+ _UPPER | C1_ALPHA , // DB (LATIN CAPITAL LETTER U WITH CIRCUMFLEX)
264
+ _UPPER | C1_ALPHA , // DC (LATIN CAPITAL LETTER U WITH DIAERESIS)
265
+ _UPPER | C1_ALPHA , // DD (LATIN CAPITAL LETTER Y WITH ACUTE)
266
+ _UPPER | C1_ALPHA , // DE (LATIN CAPITAL LETTER THORN)
267
+ _LOWER | C1_ALPHA , // DF (LATIN SMALL LETTER SHARP S)
268
+ _LOWER | C1_ALPHA , // E0 (LATIN SMALL LETTER A WITH GRAVE)
269
+ _LOWER | C1_ALPHA , // E1 (LATIN SMALL LETTER A WITH ACUTE)
270
+ _LOWER | C1_ALPHA , // E2 (LATIN SMALL LETTER A WITH CIRCUMFLEX)
271
+ _LOWER | C1_ALPHA , // E3 (LATIN SMALL LETTER A WITH TILDE)
272
+ _LOWER | C1_ALPHA , // E4 (LATIN SMALL LETTER A WITH DIAERESIS)
273
+ _LOWER | C1_ALPHA , // E5 (LATIN SMALL LETTER A WITH RING ABOVE)
274
+ _LOWER | C1_ALPHA , // E6 (LATIN SMALL LETTER AE)
275
+ _LOWER | C1_ALPHA , // E7 (LATIN SMALL LETTER C WITH CEDILLA)
276
+ _LOWER | C1_ALPHA , // E8 (LATIN SMALL LETTER E WITH GRAVE)
277
+ _LOWER | C1_ALPHA , // E9 (LATIN SMALL LETTER E WITH ACUTE)
278
+ _LOWER | C1_ALPHA , // EA (LATIN SMALL LETTER E WITH CIRCUMFLEX)
279
+ _LOWER | C1_ALPHA , // EB (LATIN SMALL LETTER E WITH DIAERESIS)
280
+ _LOWER | C1_ALPHA , // EC (LATIN SMALL LETTER I WITH GRAVE)
281
+ _LOWER | C1_ALPHA , // ED (LATIN SMALL LETTER I WITH ACUTE)
282
+ _LOWER | C1_ALPHA , // EE (LATIN SMALL LETTER I WITH CIRCUMFLEX)
283
+ _LOWER | C1_ALPHA , // EF (LATIN SMALL LETTER I WITH DIAERESIS)
284
+ _LOWER | C1_ALPHA , // F0 (LATIN SMALL LETTER ETH)
285
+ _LOWER | C1_ALPHA , // F1 (LATIN SMALL LETTER N WITH TILDE)
286
+ _LOWER | C1_ALPHA , // F2 (LATIN SMALL LETTER O WITH GRAVE)
287
+ _LOWER | C1_ALPHA , // F3 (LATIN SMALL LETTER O WITH ACUTE)
288
+ _LOWER | C1_ALPHA , // F4 (LATIN SMALL LETTER O WITH CIRCUMFLEX)
289
+ _LOWER | C1_ALPHA , // F5 (LATIN SMALL LETTER O WITH TILDE)
290
+ _LOWER | C1_ALPHA , // F6 (LATIN SMALL LETTER O WITH DIAERESIS)
291
+ _PUNCT , // F7 (DIVISION SIGN)
292
+ _LOWER | C1_ALPHA , // F8 (LATIN SMALL LETTER O WITH STROKE)
293
+ _LOWER | C1_ALPHA , // F9 (LATIN SMALL LETTER U WITH GRAVE)
294
+ _LOWER | C1_ALPHA , // FA (LATIN SMALL LETTER U WITH ACUTE)
295
+ _LOWER | C1_ALPHA , // FB (LATIN SMALL LETTER U WITH CIRCUMFLEX)
296
+ _LOWER | C1_ALPHA , // FC (LATIN SMALL LETTER U WITH DIAERESIS)
297
+ _LOWER | C1_ALPHA , // FD (LATIN SMALL LETTER Y WITH ACUTE)
298
+ _LOWER | C1_ALPHA , // FE (LATIN SMALL LETTER THORN)
299
+ _LOWER | C1_ALPHA , // FF (LATIN SMALL LETTER Y WITH DIAERESIS)
300
+ _UPPER | C1_ALPHA , //100 (LATIN CAPITAL LETTER A WITH MACRON)
301
+ };
302
+
303
+ extern "C" extern unsigned short const __newctype[384]
304
+ {
305
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
306
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
307
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
308
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
309
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310
+ 0, 0, 0, 0, 0, 0, 0,
311
+ 0, // -1 EOF
312
+ _CONTROL, // 00 (NUL)
313
+ _CONTROL, // 01 (SOH)
314
+ _CONTROL, // 02 (STX)
315
+ _CONTROL, // 03 (ETX)
316
+ _CONTROL, // 04 (EOT)
317
+ _CONTROL, // 05 (ENQ)
318
+ _CONTROL, // 06 (ACK)
319
+ _CONTROL, // 07 (BEL)
320
+ _CONTROL, // 08 (BS)
321
+ // \t is a blank character, but is not registered as _Blank on the table, because that will make it
322
+ //printable. Also Windows (via GetStringType()) considered all _BLANK characters to also be _PRINT characters,
323
+ //so does not have a way to specify blank, non-printable.
324
+ _SPACE+_CONTROL, // 09 (HT)
325
+ _SPACE+_CONTROL, // 0A (LF)
326
+ _SPACE+_CONTROL, // 0B (VT)
327
+ _SPACE+_CONTROL, // 0C (FF)
328
+ _SPACE+_CONTROL, // 0D (CR)
329
+ _CONTROL, // 0E (SI)
330
+ _CONTROL, // 0F (SO)
331
+ _CONTROL, // 10 (DLE)
332
+ _CONTROL, // 11 (DC1)
333
+ _CONTROL, // 12 (DC2)
334
+ _CONTROL, // 13 (DC3)
335
+ _CONTROL, // 14 (DC4)
336
+ _CONTROL, // 15 (NAK)
337
+ _CONTROL, // 16 (SYN)
338
+ _CONTROL, // 17 (ETB)
339
+ _CONTROL, // 18 (CAN)
340
+ _CONTROL, // 19 (EM)
341
+ _CONTROL, // 1A (SUB)
342
+ _CONTROL, // 1B (ESC)
343
+ _CONTROL, // 1C (FS)
344
+ _CONTROL, // 1D (GS)
345
+ _CONTROL, // 1E (RS)
346
+ _CONTROL, // 1F (US)
347
+ _SPACE+_BLANK, // 20 SPACE
348
+ _PUNCT, // 21 !
349
+ _PUNCT, // 22 "
350
+ _PUNCT, // 23 #
351
+ _PUNCT, // 24 $
352
+ _PUNCT, // 25 %
353
+ _PUNCT, // 26 &
354
+ _PUNCT, // 27 '
355
+ _PUNCT, // 28 (
356
+ _PUNCT, // 29 )
357
+ _PUNCT, // 2A *
358
+ _PUNCT, // 2B +
359
+ _PUNCT, // 2C ,
360
+ _PUNCT, // 2D -
361
+ _PUNCT, // 2E .
362
+ _PUNCT, // 2F /
363
+ _DIGIT+_HEX, // 30 0
364
+ _DIGIT+_HEX, // 31 1
365
+ _DIGIT+_HEX, // 32 2
366
+ _DIGIT+_HEX, // 33 3
367
+ _DIGIT+_HEX, // 34 4
368
+ _DIGIT+_HEX, // 35 5
369
+ _DIGIT+_HEX, // 36 6
370
+ _DIGIT+_HEX, // 37 7
371
+ _DIGIT+_HEX, // 38 8
372
+ _DIGIT+_HEX, // 39 9
373
+ _PUNCT, // 3A :
374
+ _PUNCT, // 3B ;
375
+ _PUNCT, // 3C <
376
+ _PUNCT, // 3D =
377
+ _PUNCT, // 3E >
378
+ _PUNCT, // 3F ?
379
+ _PUNCT, // 40 @
380
+ _UPPER+_HEX, // 41 A
381
+ _UPPER+_HEX, // 42 B
382
+ _UPPER+_HEX, // 43 C
383
+ _UPPER+_HEX, // 44 D
384
+ _UPPER+_HEX, // 45 E
385
+ _UPPER+_HEX, // 46 F
386
+ _UPPER, // 47 G
387
+ _UPPER, // 48 H
388
+ _UPPER, // 49 I
389
+ _UPPER, // 4A J
390
+ _UPPER, // 4B K
391
+ _UPPER, // 4C L
392
+ _UPPER, // 4D M
393
+ _UPPER, // 4E N
394
+ _UPPER, // 4F O
395
+ _UPPER, // 50 P
396
+ _UPPER, // 51 Q
397
+ _UPPER, // 52 R
398
+ _UPPER, // 53 S
399
+ _UPPER, // 54 T
400
+ _UPPER, // 55 U
401
+ _UPPER, // 56 V
402
+ _UPPER, // 57 W
403
+ _UPPER, // 58 X
404
+ _UPPER, // 59 Y
405
+ _UPPER, // 5A Z
406
+ _PUNCT, // 5B [
407
+ _PUNCT, // 5C \ backslash
408
+ _PUNCT, // 5D ]
409
+ _PUNCT, // 5E ^
410
+ _PUNCT, // 5F _
411
+ _PUNCT, // 60 `
412
+ _LOWER+_HEX, // 61 a
413
+ _LOWER+_HEX, // 62 b
414
+ _LOWER+_HEX, // 63 c
415
+ _LOWER+_HEX, // 64 d
416
+ _LOWER+_HEX, // 65 e
417
+ _LOWER+_HEX, // 66 f
418
+ _LOWER, // 67 g
419
+ _LOWER, // 68 h
420
+ _LOWER, // 69 i
421
+ _LOWER, // 6A j
422
+ _LOWER, // 6B k
423
+ _LOWER, // 6C l
424
+ _LOWER, // 6D m
425
+ _LOWER, // 6E n
426
+ _LOWER, // 6F o
427
+ _LOWER, // 70 p
428
+ _LOWER, // 71 q
429
+ _LOWER, // 72 r
430
+ _LOWER, // 73 s
431
+ _LOWER, // 74 t
432
+ _LOWER, // 75 u
433
+ _LOWER, // 76 v
434
+ _LOWER, // 77 w
435
+ _LOWER, // 78 x
436
+ _LOWER, // 79 y
437
+ _LOWER, // 7A z
438
+ _PUNCT, // 7B {
439
+ _PUNCT, // 7C |
440
+ _PUNCT, // 7D }
441
+ _PUNCT, // 7E ~
442
+ _CONTROL, // 7F (DEL)
443
+ // and the rest are 0...
444
+ };
445
+
446
+ extern "C" extern unsigned char const __newclmap[384]
447
+ {
448
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
449
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
450
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
451
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
452
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
453
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
454
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
455
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
456
+ 0x00, // 00 (NUL)
457
+ 0x01, // 01 (SOH)
458
+ 0x02, // 02 (STX)
459
+ 0x03, // 03 (ETX)
460
+ 0x04, // 04 (EOT)
461
+ 0x05, // 05 (ENQ)
462
+ 0x06, // 06 (ACK)
463
+ 0x07, // 07 (BEL)
464
+ 0x08, // 08 (BS)
465
+ 0x09, // 09 (HT)
466
+ 0x0A, // 0A (LF)
467
+ 0x0B, // 0B (VT)
468
+ 0x0C, // 0C (FF)
469
+ 0x0D, // 0D (CR)
470
+ 0x0E, // 0E (SI)
471
+ 0x0F, // 0F (SO)
472
+ 0x10, // 10 (DLE)
473
+ 0x11, // 11 (DC1)
474
+ 0x12, // 12 (DC2)
475
+ 0x13, // 13 (DC3)
476
+ 0x14, // 14 (DC4)
477
+ 0x15, // 15 (NAK)
478
+ 0x16, // 16 (SYN)
479
+ 0x17, // 17 (ETB)
480
+ 0x18, // 18 (CAN)
481
+ 0x19, // 19 (EM)
482
+ 0x1A, // 1A (SUB)
483
+ 0x1B, // 1B (ESC)
484
+ 0x1C, // 1C (FS)
485
+ 0x1D, // 1D (GS)
486
+ 0x1E, // 1E (RS)
487
+ 0x1F, // 1F (US)
488
+ 0x20, // 20 SPACE
489
+ 0x21, // 21 !
490
+ 0x22, // 22 "
491
+ 0x23, // 23 #
492
+ 0x24, // 24 $
493
+ 0x25, // 25 %
494
+ 0x26, // 26 &
495
+ 0x27, // 27 '
496
+ 0x28, // 28 (
497
+ 0x29, // 29 )
498
+ 0x2A, // 2A *
499
+ 0x2B, // 2B +
500
+ 0x2C, // 2C ,
501
+ 0x2D, // 2D -
502
+ 0x2E, // 2E .
503
+ 0x2F, // 2F /
504
+ 0x30, // 30 0
505
+ 0x31, // 31 1
506
+ 0x32, // 32 2
507
+ 0x33, // 33 3
508
+ 0x34, // 34 4
509
+ 0x35, // 35 5
510
+ 0x36, // 36 6
511
+ 0x37, // 37 7
512
+ 0x38, // 38 8
513
+ 0x39, // 39 9
514
+ 0x3A, // 3A :
515
+ 0x3B, // 3B ;
516
+ 0x3C, // 3C <
517
+ 0x3D, // 3D =
518
+ 0x3E, // 3E >
519
+ 0x3F, // 3F ?
520
+ 0x40, // 40 @
521
+ 0x61, // 41 A
522
+ 0x62, // 42 B
523
+ 0x63, // 43 C
524
+ 0x64, // 44 D
525
+ 0x65, // 45 E
526
+ 0x66, // 46 F
527
+ 0x67, // 47 G
528
+ 0x68, // 48 H
529
+ 0x69, // 49 I
530
+ 0x6A, // 4A J
531
+ 0x6B, // 4B K
532
+ 0x6C, // 4C L
533
+ 0x6D, // 4D M
534
+ 0x6E, // 4E N
535
+ 0x6F, // 4F O
536
+ 0x70, // 50 P
537
+ 0x71, // 51 Q
538
+ 0x72, // 52 R
539
+ 0x73, // 53 S
540
+ 0x74, // 54 T
541
+ 0x75, // 55 U
542
+ 0x76, // 56 V
543
+ 0x77, // 57 W
544
+ 0x78, // 58 X
545
+ 0x79, // 59 Y
546
+ 0x7A, // 5A Z
547
+ 0x5B, // 5B [
548
+ 0x5C, // 5C \
549
+ 0x5D, // 5D ]
550
+ 0x5E, // 5E ^
551
+ 0x5F, // 5F _
552
+ 0x60, // 60 `
553
+ 0x61, // 61 a
554
+ 0x62, // 62 b
555
+ 0x63, // 63 c
556
+ 0x64, // 64 d
557
+ 0x65, // 65 e
558
+ 0x66, // 66 f
559
+ 0x67, // 67 g
560
+ 0x68, // 68 h
561
+ 0x69, // 69 i
562
+ 0x6A, // 6A j
563
+ 0x6B, // 6B k
564
+ 0x6C, // 6C l
565
+ 0x6D, // 6D m
566
+ 0x6E, // 6E n
567
+ 0x6F, // 6F o
568
+ 0x70, // 70 p
569
+ 0x71, // 71 q
570
+ 0x72, // 72 r
571
+ 0x73, // 73 s
572
+ 0x74, // 74 t
573
+ 0x75, // 75 u
574
+ 0x76, // 76 v
575
+ 0x77, // 77 w
576
+ 0x78, // 78 x
577
+ 0x79, // 79 y
578
+ 0x7A, // 7A z
579
+ 0x7B, // 7B {
580
+ 0x7C, // 7C |
581
+ 0x7D, // 7D }
582
+ 0x7E, // 7E ~
583
+ 0x7F, // 7F (DEL)
584
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
585
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
586
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
587
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
588
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
589
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
590
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
591
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
592
+ };
593
+
594
+ extern "C" extern unsigned char const __newcumap[384]
595
+ {
596
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
597
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
598
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
599
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
600
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
601
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
602
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
603
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
604
+ 0x00, // 00 (NUL)
605
+ 0x01, // 01 (SOH)
606
+ 0x02, // 02 (STX)
607
+ 0x03, // 03 (ETX)
608
+ 0x04, // 04 (EOT)
609
+ 0x05, // 05 (ENQ)
610
+ 0x06, // 06 (ACK)
611
+ 0x07, // 07 (BEL)
612
+ 0x08, // 08 (BS)
613
+ 0x09, // 09 (HT)
614
+ 0x0A, // 0A (LF)
615
+ 0x0B, // 0B (VT)
616
+ 0x0C, // 0C (FF)
617
+ 0x0D, // 0D (CR)
618
+ 0x0E, // 0E (SI)
619
+ 0x0F, // 0F (SO)
620
+ 0x10, // 10 (DLE)
621
+ 0x11, // 11 (DC1)
622
+ 0x12, // 12 (DC2)
623
+ 0x13, // 13 (DC3)
624
+ 0x14, // 14 (DC4)
625
+ 0x15, // 15 (NAK)
626
+ 0x16, // 16 (SYN)
627
+ 0x17, // 17 (ETB)
628
+ 0x18, // 18 (CAN)
629
+ 0x19, // 19 (EM)
630
+ 0x1A, // 1A (SUB)
631
+ 0x1B, // 1B (ESC)
632
+ 0x1C, // 1C (FS)
633
+ 0x1D, // 1D (GS)
634
+ 0x1E, // 1E (RS)
635
+ 0x1F, // 1F (US)
636
+ 0x20, // 20 SPACE
637
+ 0x21, // 21 !
638
+ 0x22, // 22 "
639
+ 0x23, // 23 #
640
+ 0x24, // 24 $
641
+ 0x25, // 25 %
642
+ 0x26, // 26 &
643
+ 0x27, // 27 '
644
+ 0x28, // 28 (
645
+ 0x29, // 29 )
646
+ 0x2A, // 2A *
647
+ 0x2B, // 2B +
648
+ 0x2C, // 2C ,
649
+ 0x2D, // 2D -
650
+ 0x2E, // 2E .
651
+ 0x2F, // 2F /
652
+ 0x30, // 30 0
653
+ 0x31, // 31 1
654
+ 0x32, // 32 2
655
+ 0x33, // 33 3
656
+ 0x34, // 34 4
657
+ 0x35, // 35 5
658
+ 0x36, // 36 6
659
+ 0x37, // 37 7
660
+ 0x38, // 38 8
661
+ 0x39, // 39 9
662
+ 0x3A, // 3A :
663
+ 0x3B, // 3B ;
664
+ 0x3C, // 3C <
665
+ 0x3D, // 3D =
666
+ 0x3E, // 3E >
667
+ 0x3F, // 3F ?
668
+ 0x40, // 40 @
669
+ 0x41, // 41 A
670
+ 0x42, // 42 B
671
+ 0x43, // 43 C
672
+ 0x44, // 44 D
673
+ 0x45, // 45 E
674
+ 0x46, // 46 F
675
+ 0x47, // 47 G
676
+ 0x48, // 48 H
677
+ 0x49, // 49 I
678
+ 0x4A, // 4A J
679
+ 0x4B, // 4B K
680
+ 0x4C, // 4C L
681
+ 0x4D, // 4D M
682
+ 0x4E, // 4E N
683
+ 0x4F, // 4F O
684
+ 0x50, // 50 P
685
+ 0x51, // 51 Q
686
+ 0x52, // 52 R
687
+ 0x53, // 53 S
688
+ 0x54, // 54 T
689
+ 0x55, // 55 U
690
+ 0x56, // 56 V
691
+ 0x57, // 57 W
692
+ 0x58, // 58 X
693
+ 0x59, // 59 Y
694
+ 0x5A, // 5A Z
695
+ 0x5B, // 5B [
696
+ 0x5C, // 5C \
697
+ 0x5D, // 5D ]
698
+ 0x5E, // 5E ^
699
+ 0x5F, // 5F _
700
+ 0x60, // 60 `
701
+ 0x41, // 61 a
702
+ 0x42, // 62 b
703
+ 0x43, // 63 c
704
+ 0x44, // 64 d
705
+ 0x45, // 65 e
706
+ 0x46, // 66 f
707
+ 0x47, // 67 g
708
+ 0x48, // 68 h
709
+ 0x49, // 69 i
710
+ 0x4A, // 6A j
711
+ 0x4B, // 6B k
712
+ 0x4C, // 6C l
713
+ 0x4D, // 6D m
714
+ 0x4E, // 6E n
715
+ 0x4F, // 6F o
716
+ 0x50, // 70 p
717
+ 0x51, // 71 q
718
+ 0x52, // 72 r
719
+ 0x53, // 73 s
720
+ 0x54, // 74 t
721
+ 0x55, // 75 u
722
+ 0x56, // 76 v
723
+ 0x57, // 77 w
724
+ 0x58, // 78 x
725
+ 0x59, // 79 y
726
+ 0x5A, // 7A z
727
+ 0x7B, // 7B {
728
+ 0x7C, // 7C |
729
+ 0x7D, // 7D }
730
+ 0x7E, // 7E ~
731
+ 0x7F, // 7F (DEL)
732
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
733
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
734
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
735
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
736
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
737
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
738
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
739
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
740
+ };
741
+
742
+
743
+
744
+ extern "C" unsigned short const* _pctype {__newctype + 128}; // Pointer to table for char's
745
+ extern "C" unsigned short const* _pwctype{_wctype + 1 }; // Pointer to table for wchar_t's
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/get_qualified_locale.cpp ADDED
@@ -0,0 +1,851 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *getqloc.c - get qualified locale
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * defines __acrt_get_qualified_locale - get complete locale information
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+ #include <locale.h>
12
+ #include <stdlib.h>
13
+
14
+ extern "C" {
15
+
16
+
17
+
18
+ // local defines
19
+ #define __LOC_DEFAULT 0x1 // default language locale for country
20
+ #define __LOC_PRIMARY 0x2 // primary language locale for country
21
+ #define __LOC_FULL 0x4 // fully matched language locale for country
22
+ #define __LOC_LANGUAGE 0x100 // language default seen
23
+ #define __LOC_EXISTS 0x200 // language is installed
24
+
25
+
26
+
27
+ // non-NLS language string table
28
+ // The three letter Windows names are non-standard and very limited and should not be used.
29
+ extern __crt_locale_string_table const __acrt_rg_language[]
30
+ {
31
+ { L"american", L"ENU" },
32
+ { L"american english", L"ENU" },
33
+ { L"american-english", L"ENU" },
34
+ { L"australian", L"ENA" },
35
+ { L"belgian", L"NLB" },
36
+ { L"canadian", L"ENC" },
37
+ { L"chh", L"ZHH" },
38
+ { L"chi", L"ZHI" },
39
+ { L"chinese", L"CHS" },
40
+ { L"chinese-hongkong", L"ZHH" },
41
+ { L"chinese-simplified", L"CHS" },
42
+ { L"chinese-singapore", L"ZHI" },
43
+ { L"chinese-traditional", L"CHT" },
44
+ { L"dutch-belgian", L"NLB" },
45
+ { L"english-american", L"ENU" },
46
+ { L"english-aus", L"ENA" },
47
+ { L"english-belize", L"ENL" },
48
+ { L"english-can", L"ENC" },
49
+ { L"english-caribbean", L"ENB" },
50
+ { L"english-ire", L"ENI" },
51
+ { L"english-jamaica", L"ENJ" },
52
+ { L"english-nz", L"ENZ" },
53
+ { L"english-south africa", L"ENS" },
54
+ { L"english-trinidad y tobago", L"ENT" },
55
+ { L"english-uk", L"ENG" },
56
+ { L"english-us", L"ENU" },
57
+ { L"english-usa", L"ENU" },
58
+ { L"french-belgian", L"FRB" },
59
+ { L"french-canadian", L"FRC" },
60
+ { L"french-luxembourg", L"FRL" },
61
+ { L"french-swiss", L"FRS" },
62
+ { L"german-austrian", L"DEA" },
63
+ { L"german-lichtenstein", L"DEC" },
64
+ { L"german-luxembourg", L"DEL" },
65
+ { L"german-swiss", L"DES" },
66
+ { L"irish-english", L"ENI" },
67
+ { L"italian-swiss", L"ITS" },
68
+ { L"norwegian", L"NOR" },
69
+ { L"norwegian-bokmal", L"NOR" },
70
+ { L"norwegian-nynorsk", L"NON" },
71
+ { L"portuguese-brazilian", L"PTB" },
72
+ { L"spanish-argentina", L"ESS" },
73
+ { L"spanish-bolivia", L"ESB" },
74
+ { L"spanish-chile", L"ESL" },
75
+ { L"spanish-colombia", L"ESO" },
76
+ { L"spanish-costa rica", L"ESC" },
77
+ { L"spanish-dominican republic", L"ESD" },
78
+ { L"spanish-ecuador", L"ESF" },
79
+ { L"spanish-el salvador", L"ESE" },
80
+ { L"spanish-guatemala", L"ESG" },
81
+ { L"spanish-honduras", L"ESH" },
82
+ { L"spanish-mexican", L"ESM" },
83
+ { L"spanish-modern", L"ESN" },
84
+ { L"spanish-nicaragua", L"ESI" },
85
+ { L"spanish-panama", L"ESA" },
86
+ { L"spanish-paraguay", L"ESZ" },
87
+ { L"spanish-peru", L"ESR" },
88
+ { L"spanish-puerto rico", L"ESU" },
89
+ { L"spanish-uruguay", L"ESY" },
90
+ { L"spanish-venezuela", L"ESV" },
91
+ { L"swedish-finland", L"SVF" },
92
+ { L"swiss", L"DES" },
93
+ { L"uk", L"ENG" },
94
+ { L"us", L"ENU" },
95
+ { L"usa", L"ENU" }
96
+ };
97
+
98
+ // non-NLS country/region string table
99
+ // The three letter Windows names are non-standard and very limited and should not be used.
100
+ extern __crt_locale_string_table const __acrt_rg_country[]
101
+ {
102
+ { L"america", L"USA" },
103
+ { L"britain", L"GBR" },
104
+ { L"china", L"CHN" },
105
+ { L"czech", L"CZE" },
106
+ { L"england", L"GBR" },
107
+ { L"great britain", L"GBR" },
108
+ { L"holland", L"NLD" },
109
+ { L"hong-kong", L"HKG" },
110
+ { L"new-zealand", L"NZL" },
111
+ { L"nz", L"NZL" },
112
+ { L"pr china", L"CHN" },
113
+ { L"pr-china", L"CHN" },
114
+ { L"puerto-rico", L"PRI" },
115
+ { L"slovak", L"SVK" },
116
+ { L"south africa", L"ZAF" },
117
+ { L"south korea", L"KOR" },
118
+ { L"south-africa", L"ZAF" },
119
+ { L"south-korea", L"KOR" },
120
+ { L"trinidad & tobago", L"TTO" },
121
+ { L"uk", L"GBR" },
122
+ { L"united-kingdom", L"GBR" },
123
+ { L"united-states", L"USA" },
124
+ { L"us", L"USA" },
125
+ };
126
+
127
+ // Number of entries in the language and country tables
128
+ extern size_t const __acrt_rg_language_count{_countof(__acrt_rg_language)};
129
+ extern size_t const __acrt_rg_country_count {_countof(__acrt_rg_country )};
130
+
131
+
132
+
133
+
134
+ // function prototypes
135
+ BOOL __cdecl __acrt_get_qualified_locale(const __crt_locale_strings*, UINT*, __crt_locale_strings*);
136
+ static BOOL TranslateName(const __crt_locale_string_table *, int, const wchar_t **);
137
+
138
+ static void GetLocaleNameFromLangCountry (__crt_qualified_locale_data* _psetloc_data);
139
+ static BOOL CALLBACK LangCountryEnumProcEx(_In_z_ LPWSTR, DWORD, LPARAM);
140
+
141
+ static void GetLocaleNameFromLanguage (__crt_qualified_locale_data* _psetloc_data);
142
+ static BOOL CALLBACK LanguageEnumProcEx(_In_z_ LPWSTR, DWORD, LPARAM);
143
+
144
+ static void GetLocaleNameFromDefault (__crt_qualified_locale_data* _psetloc_data);
145
+
146
+ static int ProcessCodePage (LPCWSTR lpCodePageStr, __crt_qualified_locale_data* _psetloc_data);
147
+ static BOOL TestDefaultCountry(LPCWSTR localeName);
148
+ static BOOL TestDefaultLanguage (LPCWSTR localeName, BOOL bTestPrimary, __crt_qualified_locale_data* _psetloc_data);
149
+
150
+ static int GetPrimaryLen(LPCWSTR);
151
+
152
+
153
+ /***
154
+ *BOOL __acrt_get_qualified_locale - return fully qualified locale
155
+ *
156
+ *Purpose:
157
+ * get default locale, qualify partially complete locales
158
+ *
159
+ *Entry:
160
+ * lpInStr - input strings to be qualified
161
+ * lpOutStr - pointer to string locale names and codepage output
162
+ *
163
+ *Exit:
164
+ * TRUE if success, qualified locale is valid
165
+ * FALSE if failure
166
+ *
167
+ *Exceptions:
168
+ *
169
+ *******************************************************************************/
170
+ BOOL __cdecl __acrt_get_qualified_locale(const __crt_locale_strings* lpInStr, UINT* lpOutCodePage, __crt_locale_strings* lpOutStr)
171
+ {
172
+ int iCodePage;
173
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
174
+ _psetloc_data->_cacheLocaleName[0] = L'\x0'; // Initialize to invariant localename
175
+
176
+ // initialize pointer to call locale info routine based on operating system
177
+
178
+ _psetloc_data->iLocState = 0;
179
+ _psetloc_data->pchLanguage = lpInStr->szLanguage;
180
+ _psetloc_data->pchCountry = lpInStr->szCountry;
181
+
182
+ // if country defined
183
+ // convert non-NLS country strings to three-letter abbreviations
184
+ if (*_psetloc_data->pchCountry)
185
+ TranslateName(__acrt_rg_country, static_cast<int>(__acrt_rg_country_count - 1),
186
+ &_psetloc_data->pchCountry);
187
+
188
+
189
+ // if language defined ...
190
+ if (*_psetloc_data->pchLanguage)
191
+ {
192
+ // and country defined
193
+ if (*_psetloc_data->pchCountry)
194
+ {
195
+ // both language and country strings defined
196
+ // get locale info using language and country
197
+ GetLocaleNameFromLangCountry(_psetloc_data);
198
+ }
199
+ else
200
+ {
201
+ // language string defined, but country string undefined
202
+ // get locale info using language only
203
+ GetLocaleNameFromLanguage(_psetloc_data);
204
+ }
205
+
206
+ // still not done?
207
+ if (_psetloc_data->iLocState == 0)
208
+ {
209
+ // first attempt failed, try substituting the language name
210
+ // convert non-NLS language strings to three-letter abbrevs
211
+ if (TranslateName(__acrt_rg_language, static_cast<int>(__acrt_rg_language_count - 1),
212
+ &_psetloc_data->pchLanguage))
213
+ {
214
+ if (*_psetloc_data->pchCountry)
215
+ {
216
+ // get locale info using language and country
217
+ GetLocaleNameFromLangCountry(_psetloc_data);
218
+ }
219
+ else
220
+ {
221
+ // get locale info using language only
222
+ GetLocaleNameFromLanguage(_psetloc_data);
223
+ }
224
+ }
225
+ }
226
+ }
227
+ else
228
+ {
229
+ // language is an empty string, use the User Default locale name
230
+ GetLocaleNameFromDefault(_psetloc_data);
231
+ }
232
+
233
+ // test for error in locale processing
234
+ if (_psetloc_data->iLocState == 0)
235
+ return FALSE;
236
+
237
+ // process codepage value
238
+ if (lpInStr == nullptr || *lpInStr->szLanguage || *lpInStr->szCodePage )
239
+ {
240
+ // If there's no input, then get the current codepage
241
+ // If they explicitly chose a language, use that default codepage
242
+ // If they explicilty set a codepage, then use that
243
+ iCodePage = ProcessCodePage(lpInStr ? lpInStr->szCodePage : nullptr, _psetloc_data);
244
+ }
245
+ else
246
+ {
247
+ // No language or codepage means that they want to set to the
248
+ // user default settings, get that codepage (could be UTF-8)
249
+ iCodePage = GetACP();
250
+ }
251
+
252
+ // verify codepage validity
253
+ // CP_UTF7 is unexpected and has never been previously permitted.
254
+ // CP_UTF8 is the current preferred codepage
255
+ if (!iCodePage || iCodePage == CP_UTF7 || !IsValidCodePage((WORD)iCodePage))
256
+ return FALSE;
257
+
258
+ // set codepage
259
+ if (lpOutCodePage)
260
+ {
261
+ *lpOutCodePage = (UINT)iCodePage;
262
+ }
263
+
264
+ // set locale name and codepage results
265
+ if (lpOutStr)
266
+ {
267
+ lpOutStr->szLocaleName[0] = L'\x0'; // Init the locale name to empty string
268
+
269
+ _ERRCHECK(wcsncpy_s(lpOutStr->szLocaleName, _countof(lpOutStr->szLocaleName), _psetloc_data->_cacheLocaleName, wcslen(_psetloc_data->_cacheLocaleName) + 1));
270
+
271
+ // Get and store the English language lang name, to be returned to user
272
+ if (__acrt_GetLocaleInfoEx(lpOutStr->szLocaleName, LOCALE_SENGLISHLANGUAGENAME,
273
+ lpOutStr->szLanguage, MAX_LANG_LEN) == 0)
274
+ return FALSE;
275
+
276
+ // Get and store the English language country name, to be returned to user
277
+ if (__acrt_GetLocaleInfoEx(lpOutStr->szLocaleName, LOCALE_SENGLISHCOUNTRYNAME,
278
+ lpOutStr->szCountry, MAX_CTRY_LEN) == 0)
279
+ return FALSE;
280
+
281
+ // Special case: Both '.' and '_' are separators in string passed to setlocale,
282
+ // so if found in Country we use abbreviated name instead.
283
+ if (wcschr(lpOutStr->szCountry, L'_') || wcschr(lpOutStr->szCountry, L'.'))
284
+ if (__acrt_GetLocaleInfoEx(lpOutStr->szLocaleName, LOCALE_SABBREVCTRYNAME,
285
+ lpOutStr->szCountry, MAX_CTRY_LEN) == 0)
286
+ return FALSE;
287
+
288
+ if (iCodePage == CP_UTF8)
289
+ {
290
+ // We want UTF-8 to look like utf8, not 65001
291
+ _ERRCHECK(wcsncpy_s(lpOutStr->szCodePage, _countof(lpOutStr->szCodePage), L"utf8", 5));
292
+ }
293
+ else
294
+ {
295
+ _itow_s((int)iCodePage, (wchar_t *)lpOutStr->szCodePage, MAX_CP_LEN, 10);
296
+ }
297
+ }
298
+
299
+ return TRUE;
300
+ }
301
+
302
+ /***
303
+ *BOOL TranslateName - convert known non-NLS string to NLS equivalent
304
+ *
305
+ *Purpose:
306
+ * Provide compatibility with existing code for non-NLS strings
307
+ *
308
+ *Entry:
309
+ * lpTable - pointer to __crt_locale_string_table used for translation
310
+ * high - maximum index of table (size - 1)
311
+ * ppchName - pointer to pointer of string to translate
312
+ *
313
+ *Exit:
314
+ * ppchName - pointer to pointer of string possibly translated
315
+ * TRUE if string translated, FALSE if unchanged
316
+ *
317
+ *Exceptions:
318
+ *
319
+ *******************************************************************************/
320
+ static BOOL TranslateName (
321
+ const __crt_locale_string_table * lpTable,
322
+ int high,
323
+ const wchar_t** ppchName)
324
+ {
325
+ int i;
326
+ int cmp = 1;
327
+ int low = 0;
328
+
329
+ // typical binary search - do until no more to search or match
330
+ while (low <= high && cmp != 0)
331
+ {
332
+ i = (low + high) / 2;
333
+ cmp = _wcsicmp(*ppchName, (const wchar_t *)(*(lpTable + i)).szName);
334
+
335
+ if (cmp == 0)
336
+ *ppchName = (*(lpTable + i)).chAbbrev;
337
+ else if (cmp < 0)
338
+ high = i - 1;
339
+ else
340
+ low = i + 1;
341
+ }
342
+
343
+ return !cmp;
344
+ }
345
+
346
+ /***
347
+ *void GetLocaleNameFromLangCountry - get locale names from language and country strings
348
+ *
349
+ *Purpose:
350
+ * Match the best locale names to the language and country string given.
351
+ * After global variables are initialized, the LangCountryEnumProcEx
352
+ * routine is registered as an EnumSystemLocalesEx callback to actually
353
+ * perform the matching as the locale names are enumerated.
354
+ *
355
+ *
356
+ *WARNING:
357
+ * This depends on an exact match with a localized string that can change!
358
+ * It is strongly recommended that locales be selected with valid BCP-47
359
+ * tags instead of the English names.
360
+ *
361
+ * This API is also very brute-force and resource intensive, reading in all
362
+ * of the locales, forcing them to be cached, and looking up their names.
363
+ *
364
+ *WARNING:
365
+ * In the event of a 2 or 3 letter friendly name (Asu, Edo, Ewe, Yi, ...)
366
+ * then this function will fail
367
+ *
368
+ *Entry:
369
+ * pchLanguage - language string
370
+ * bAbbrevLanguage - language string is a three-letter abbreviation
371
+ * pchCountry - country string
372
+ * bAbbrevCountry - country string ia a three-letter abbreviation
373
+ * iPrimaryLen - length of language string with primary name
374
+ *
375
+ *Exit:
376
+ * localeName - locale name of given language and country
377
+ *
378
+ *Exceptions:
379
+ *
380
+ *******************************************************************************/
381
+ static void GetLocaleNameFromLangCountry (__crt_qualified_locale_data* _psetloc_data)
382
+ {
383
+ // initialize static variables for callback use
384
+ _psetloc_data->bAbbrevLanguage = wcslen(_psetloc_data->pchLanguage) == 3;
385
+ _psetloc_data->bAbbrevCountry = wcslen(_psetloc_data->pchCountry) == 3;
386
+
387
+ _psetloc_data->iPrimaryLen = _psetloc_data->bAbbrevLanguage ?
388
+ 2 : GetPrimaryLen(_psetloc_data->pchLanguage);
389
+
390
+ // Enumerate all locales that come with the operating system,
391
+ // including replacement locales, but excluding alternate sorts.
392
+ __acrt_EnumSystemLocalesEx(LangCountryEnumProcEx, LOCALE_WINDOWS | LOCALE_SUPPLEMENTAL, 0, nullptr);
393
+
394
+ // locale value is invalid if the language was not installed or the language
395
+ // was not available for the country specified
396
+ if (!(_psetloc_data->iLocState & __LOC_LANGUAGE) ||
397
+ !(_psetloc_data->iLocState & __LOC_EXISTS) ||
398
+ !(_psetloc_data->iLocState & (__LOC_FULL |
399
+ __LOC_PRIMARY |
400
+ __LOC_DEFAULT)))
401
+ _psetloc_data->iLocState = 0;
402
+ }
403
+
404
+ /***
405
+ *BOOL CALLBACK LangCountryEnumProcEx - callback routine for GetLocaleNameFromLangCountry
406
+ *
407
+ *Purpose:
408
+ * Determine if locale name given matches the language in pchLanguage
409
+ * and country in pchCountry.
410
+ *
411
+ *Entry:
412
+ * lpLocaleString - pointer to locale name string string
413
+ * pchCountry - pointer to country name
414
+ * bAbbrevCountry - set if country is three-letter abbreviation
415
+ *
416
+ *Exit:
417
+ * iLocState - status of match
418
+ * __LOC_FULL - both language and country match (best match)
419
+ * __LOC_PRIMARY - primary language and country match (better)
420
+ * __LOC_DEFAULT - default language and country match (good)
421
+ * __LOC_LANGUAGE - default primary language exists
422
+ * __LOC_EXISTS - full match of language string exists
423
+ * (Overall match occurs for the best of FULL/PRIMARY/DEFAULT
424
+ * and LANGUAGE/EXISTS both set.)
425
+ * localeName - lpLocaleString matched
426
+ * FALSE if match occurred to terminate enumeration, else TRUE.
427
+ *
428
+ *Exceptions:
429
+ *
430
+ *******************************************************************************/
431
+ static BOOL CALLBACK LangCountryEnumProcEx(LPWSTR lpLocaleString, DWORD dwFlags, LPARAM lParam)
432
+ {
433
+ UNREFERENCED_PARAMETER(dwFlags);
434
+ UNREFERENCED_PARAMETER(lParam);
435
+
436
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
437
+ wchar_t rgcInfo[MAX_LANG_LEN]; // MAX_LANG_LEN == MAX_CTRY_LEN == 64
438
+
439
+ // test locale country against input value
440
+ if (__acrt_GetLocaleInfoEx(lpLocaleString,
441
+ _psetloc_data->bAbbrevCountry ? LOCALE_SABBREVCTRYNAME : LOCALE_SENGLISHCOUNTRYNAME,
442
+ rgcInfo, _countof(rgcInfo)) == 0)
443
+ {
444
+ // set error condition and exit
445
+ _psetloc_data->iLocState = 0;
446
+ return TRUE;
447
+ }
448
+
449
+ // if country names matched
450
+ if (_wcsicmp(_psetloc_data->pchCountry, rgcInfo) == 0)
451
+ {
452
+ // test for language match
453
+ if (__acrt_GetLocaleInfoEx(lpLocaleString,
454
+ _psetloc_data->bAbbrevLanguage ?
455
+ LOCALE_SABBREVLANGNAME : LOCALE_SENGLISHLANGUAGENAME,
456
+ rgcInfo, _countof(rgcInfo)) == 0)
457
+ {
458
+ // set error condition and exit
459
+ _psetloc_data->iLocState = 0;
460
+ return TRUE;
461
+ }
462
+
463
+ if (_wcsicmp(_psetloc_data->pchLanguage, rgcInfo) == 0)
464
+ {
465
+ // language matched also - set state and value
466
+ // this is the best match
467
+ _psetloc_data->iLocState |= (__LOC_FULL |
468
+ __LOC_LANGUAGE |
469
+ __LOC_EXISTS);
470
+
471
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
472
+ }
473
+ // test if match already for primary langauage
474
+ else if (!(_psetloc_data->iLocState & __LOC_PRIMARY))
475
+ {
476
+ // if not, use _psetloc_data->iPrimaryLen to partial match language string
477
+ if (_psetloc_data->iPrimaryLen && !_wcsnicmp(_psetloc_data->pchLanguage, rgcInfo, _psetloc_data->iPrimaryLen))
478
+ {
479
+ // primary language matched - set locale name
480
+ _psetloc_data->iLocState |= __LOC_PRIMARY;
481
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
482
+ }
483
+
484
+ // test if default language already defined
485
+ else if (!(_psetloc_data->iLocState & __LOC_DEFAULT))
486
+ {
487
+ // if not, test if locale language is default for country
488
+ if (TestDefaultCountry(lpLocaleString))
489
+ {
490
+ // default language for country - set state, value
491
+ _psetloc_data->iLocState |= __LOC_DEFAULT;
492
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
493
+ }
494
+ }
495
+ }
496
+ }
497
+
498
+ // test if input language both exists and default primary language defined
499
+ if ((_psetloc_data->iLocState & (__LOC_LANGUAGE | __LOC_EXISTS)) !=
500
+ (__LOC_LANGUAGE | __LOC_EXISTS))
501
+ {
502
+ // test language match to determine whether it is installed
503
+ if (__acrt_GetLocaleInfoEx(lpLocaleString, _psetloc_data->bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
504
+ : LOCALE_SENGLISHLANGUAGENAME,
505
+ rgcInfo, _countof(rgcInfo)) == 0)
506
+ {
507
+ // set error condition and exit
508
+ _psetloc_data->iLocState = 0;
509
+ return TRUE;
510
+ }
511
+
512
+ // the input language matches
513
+ if (_wcsicmp(_psetloc_data->pchLanguage, rgcInfo) == 0)
514
+ {
515
+ // language matched - set bit for existance
516
+ _psetloc_data->iLocState |= __LOC_EXISTS;
517
+
518
+ if (_psetloc_data->bAbbrevLanguage)
519
+ {
520
+ // abbreviation - set state
521
+ // also set language locale name if not set already
522
+ _psetloc_data->iLocState |= __LOC_LANGUAGE;
523
+ if (!_psetloc_data->_cacheLocaleName[0])
524
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
525
+ }
526
+
527
+ // test if language is primary only (no sublanguage)
528
+ else if (_psetloc_data->iPrimaryLen && ((int)wcslen(_psetloc_data->pchLanguage) == _psetloc_data->iPrimaryLen))
529
+ {
530
+ // primary language only - test if default locale name
531
+ if (TestDefaultLanguage(lpLocaleString, TRUE, _psetloc_data))
532
+ {
533
+ // default primary language - set state
534
+ // also set locale name if not set already
535
+ _psetloc_data->iLocState |= __LOC_LANGUAGE;
536
+ if (!_psetloc_data->_cacheLocaleName[0])
537
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
538
+ }
539
+ }
540
+ else
541
+ {
542
+ // language with sublanguage - set state
543
+ // also set locale name if not set already
544
+ _psetloc_data->iLocState |= __LOC_LANGUAGE;
545
+ if (!_psetloc_data->_cacheLocaleName[0])
546
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
547
+ }
548
+ }
549
+ }
550
+
551
+ // if LOCALE_FULL set, return FALSE to stop enumeration,
552
+ // else return TRUE to continue
553
+ return (_psetloc_data->iLocState & __LOC_FULL) == 0;
554
+ }
555
+
556
+ /***
557
+ *void GetLocaleNameFromLanguage - get locale name from language string
558
+ *
559
+ *Purpose:
560
+ * Match the best locale name to the language string given. After global
561
+ * variables are initialized, the LanguageEnumProcEx routine is
562
+ * registered as an EnumSystemLocalesEx callback to actually perform
563
+ * the matching as the locale names are enumerated.
564
+ *
565
+ *WARNING:
566
+ * This depends on an exact match with a localized string that can change!
567
+ * It is strongly recommended that locales be selected with valid BCP-47
568
+ * tags instead of the English names.
569
+ *
570
+ * This API is also very brute-force and resource intensive, reading in all
571
+ * of the locales, forcing them to be cached, and looking up their names.
572
+ *
573
+ *WARNING:
574
+ * In the event of a 3 letter BCP-47 tag that happens to match a Windows
575
+ * propriatary language code, this function will return the wrong answer!
576
+ *
577
+ *WARNING:
578
+ * In the event of a 2 or 3 letter friendly name (Asu, Edo, Ewe, Yi, ...)
579
+ * then this function will fail
580
+ *
581
+ *Entry:
582
+ * pchLanguage - language string
583
+ * bAbbrevLanguage - language string is a three-letter abbreviation
584
+ * iPrimaryLen - length of language string with primary name
585
+ *
586
+ *Exit:
587
+ * localeName - locale name of language with default country
588
+ *
589
+ *Exceptions:
590
+ *
591
+ *******************************************************************************/
592
+ static void GetLocaleNameFromLanguage (__crt_qualified_locale_data* _psetloc_data)
593
+ {
594
+ // initialize static variables for callback use
595
+ _psetloc_data->bAbbrevLanguage = wcslen(_psetloc_data->pchLanguage) == 3;
596
+ _psetloc_data->iPrimaryLen = _psetloc_data->bAbbrevLanguage ? 2 : GetPrimaryLen(_psetloc_data->pchLanguage);
597
+
598
+ // Enumerate all locales that come with the operating system, including replacement locales,
599
+ // but excluding alternate sorts.
600
+ __acrt_EnumSystemLocalesEx(LanguageEnumProcEx, LOCALE_WINDOWS | LOCALE_SUPPLEMENTAL, 0, nullptr);
601
+
602
+ // locale value is invalid if the language was not installed
603
+ // or the language was not available for the country specified
604
+ if ((_psetloc_data->iLocState & __LOC_FULL) == 0)
605
+ _psetloc_data->iLocState = 0;
606
+ }
607
+
608
+ /***
609
+ *BOOL CALLBACK LanguageEnumProcEx - callback routine for GetLocaleNameFromLanguage
610
+ *
611
+ *Purpose:
612
+ * Determine if locale name given matches the default country for the
613
+ * language in pchLanguage.
614
+ *
615
+ *Entry:
616
+ * lpLocaleString - pointer to string with locale name
617
+ * dwFlags - not used
618
+ * lParam - not used
619
+ *
620
+ *Exit:
621
+ * localeName - locale name matched
622
+ * FALSE if match occurred to terminate enumeration, else TRUE.
623
+ *
624
+ *Exceptions:
625
+ *
626
+ *******************************************************************************/
627
+ static BOOL CALLBACK LanguageEnumProcEx (LPWSTR lpLocaleString, DWORD dwFlags, LPARAM lParam)
628
+ {
629
+ UNREFERENCED_PARAMETER(dwFlags);
630
+ UNREFERENCED_PARAMETER(lParam);
631
+
632
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
633
+ wchar_t rgcInfo[120];
634
+
635
+ // test locale for language specified
636
+ if (__acrt_GetLocaleInfoEx(lpLocaleString, _psetloc_data->bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
637
+ : LOCALE_SENGLISHLANGUAGENAME,
638
+ rgcInfo, _countof(rgcInfo)) == 0)
639
+ {
640
+ // set error condition and exit
641
+ _psetloc_data->iLocState = 0;
642
+ return TRUE;
643
+ }
644
+
645
+ if (_wcsicmp(_psetloc_data->pchLanguage, rgcInfo) == 0)
646
+ {
647
+ // language matches
648
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), lpLocaleString, wcslen(lpLocaleString) + 1));
649
+
650
+ _psetloc_data->iLocState |= __LOC_FULL;
651
+ }
652
+
653
+ return (_psetloc_data->iLocState & __LOC_FULL) == 0;
654
+ }
655
+
656
+
657
+ /***
658
+ *void GetLocaleNameFromDefault - get default locale names
659
+ *
660
+ *Purpose:
661
+ * Set both language and country locale names to the user default.
662
+ *
663
+ *Entry:
664
+ * None.
665
+ *
666
+ *Exceptions:
667
+ *
668
+ *******************************************************************************/
669
+ static void GetLocaleNameFromDefault (__crt_qualified_locale_data* _psetloc_data)
670
+ {
671
+ wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
672
+ _psetloc_data->iLocState |= (__LOC_FULL | __LOC_LANGUAGE);
673
+
674
+ // Store the default user locale name. The returned buffer size includes the
675
+ // terminating null character, so only store if the size returned is > 1
676
+ if (__acrt_GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) > 1)
677
+ {
678
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), localeName, wcslen(localeName) + 1));
679
+ }
680
+ }
681
+
682
+ /***
683
+ *int ProcessCodePage - convert codepage string to numeric value
684
+ *
685
+ *Purpose:
686
+ * Process codepage string consisting of a decimal string, or the
687
+ * special case strings "ACP" and "OCP", for ANSI and OEM codepages,
688
+ * respectively. Null pointer or string returns the ANSI codepage.
689
+ *
690
+ *Entry:
691
+ * lpCodePageStr - pointer to codepage string
692
+ *
693
+ *Exit:
694
+ * Returns numeric value of codepage, zero if GetLocaleInfoEx failed.
695
+ * (which then would mean caller aborts and locale is not set)
696
+ *
697
+ *Exceptions:
698
+ *
699
+ *******************************************************************************/
700
+ static int ProcessCodePage (LPCWSTR lpCodePageStr, __crt_qualified_locale_data* _psetloc_data)
701
+ {
702
+ int iCodePage;
703
+
704
+ if (!lpCodePageStr || !*lpCodePageStr || wcscmp(lpCodePageStr, L"ACP") == 0)
705
+ {
706
+ // get ANSI codepage for the country locale name
707
+ // CONSIDER: If system is running UTF-8 ACP, then always return UTF-8?
708
+ if (__acrt_GetLocaleInfoEx(_psetloc_data->_cacheLocaleName, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
709
+ (LPWSTR) &iCodePage, sizeof(iCodePage) / sizeof(wchar_t)) == 0)
710
+ return 0;
711
+
712
+ // Locales with no code page ("Unicode only locales") should return UTF-8
713
+ // (0, 1 & 2 are Unicode-Only ACP, OEMCP & MacCP flags)
714
+ if (iCodePage < 3)
715
+ {
716
+ return CP_UTF8;
717
+ }
718
+
719
+ }
720
+ else if (_wcsicmp(lpCodePageStr, L"utf8") == 0 ||
721
+ _wcsicmp(lpCodePageStr, L"utf-8") == 0)
722
+ {
723
+ // Use UTF-8
724
+ return CP_UTF8;
725
+ }
726
+ else if (wcscmp(lpCodePageStr, L"OCP") == 0)
727
+ {
728
+ // get OEM codepage for the country locale name
729
+ // CONSIDER: If system is running UTF-8 ACP, then always return UTF-8?
730
+ if (__acrt_GetLocaleInfoEx(_psetloc_data->_cacheLocaleName, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
731
+ (LPWSTR) &iCodePage, sizeof(iCodePage) / sizeof(wchar_t)) == 0)
732
+ return 0;
733
+
734
+ // Locales with no code page ("unicode only locales") should return UTF-8
735
+ // (0, 1 & 2 are Unicode-Only ACP, OEMCP & MacCP flags)
736
+ if (iCodePage < 3)
737
+ {
738
+ return CP_UTF8;
739
+ }
740
+ }
741
+ else
742
+ {
743
+ // convert decimal string to numeric value
744
+ iCodePage = (int)_wtol(lpCodePageStr);
745
+ }
746
+
747
+ return iCodePage;
748
+ }
749
+
750
+ /***
751
+ *BOOL TestDefaultCountry - determine if default locale for country
752
+ *
753
+ *Purpose:
754
+ * Determine if the locale of the given locale name has the default sublanguage.
755
+ * This is determined by checking if the given language is neutral.
756
+ *
757
+ *Entry:
758
+ * localeName - name of locale to test
759
+ *
760
+ *Exit:
761
+ * Returns TRUE if default sublanguage, else FALSE.
762
+ *
763
+ *Exceptions:
764
+ *
765
+ *******************************************************************************/
766
+ static BOOL TestDefaultCountry (LPCWSTR localeName)
767
+ {
768
+ wchar_t sIso639LangName[9]; // The maximum length for LOCALE_SISO3166CTRYNAME
769
+ // is 9 including nullptr
770
+
771
+ // Get 2-letter ISO Standard 639 or 3-letter ISO 639-2 value
772
+ if (__acrt_GetLocaleInfoEx(localeName, LOCALE_SISO639LANGNAME,
773
+ sIso639LangName, _countof(sIso639LangName)) == 0)
774
+ return FALSE;
775
+
776
+ // Determine if this is a neutral language
777
+ if (wcsncmp(sIso639LangName, localeName, _countof(sIso639LangName)) == 0)
778
+ return TRUE;
779
+
780
+ return FALSE;
781
+ }
782
+
783
+ /***
784
+ *BOOL TestDefaultLanguage - determine if default locale for language
785
+ *
786
+ *Purpose:
787
+ * Determines if the given locale name has the default sublanguage.
788
+ * If bTestPrimary is set, also allow TRUE when string contains an
789
+ * implicit sublanguage.
790
+ *
791
+ *Entry:
792
+ * localeName - locale name of locale to test
793
+ * bTestPrimary - set if testing if language is primary
794
+ *
795
+ *Exit:
796
+ * Returns TRUE if sublanguage is default for locale tested.
797
+ * If bTestPrimary set, TRUE is language has implied sublanguge.
798
+ *
799
+ *Exceptions:
800
+ *
801
+ *******************************************************************************/
802
+ static BOOL TestDefaultLanguage(LPCWSTR localeName, BOOL bTestPrimary, __crt_qualified_locale_data* _psetloc_data)
803
+ {
804
+ if (!TestDefaultCountry (localeName))
805
+ {
806
+ // test if string contains an implicit sublanguage by
807
+ // having a character other than upper/lowercase letters.
808
+ if (bTestPrimary && GetPrimaryLen(_psetloc_data->pchLanguage) == (int)wcslen(_psetloc_data->pchLanguage))
809
+ return FALSE;
810
+ }
811
+
812
+ return TRUE;
813
+ }
814
+
815
+ /***
816
+ *int GetPrimaryLen - get length of primary language name
817
+ *
818
+ *Purpose:
819
+ * Determine primary language string length by scanning until
820
+ * first non-alphabetic character.
821
+ *
822
+ *Entry:
823
+ * pchLanguage - string to scan
824
+ *
825
+ *Exit:
826
+ * Returns length of primary language string.
827
+ *
828
+ *Exceptions:
829
+ *
830
+ *******************************************************************************/
831
+ static int GetPrimaryLen(LPCWSTR pchLanguage)
832
+ {
833
+ int len = 0;
834
+ wchar_t ch;
835
+
836
+ if (!pchLanguage)
837
+ return 0;
838
+
839
+ ch = *pchLanguage++;
840
+ while ((ch >= L'A' && ch <= L'Z') || (ch >= L'a' && ch <= L'z'))
841
+ {
842
+ len++;
843
+ ch = *pchLanguage++;
844
+ }
845
+
846
+ return len;
847
+ }
848
+
849
+
850
+
851
+ } // extern "C"
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/getqloc_downlevel.cpp ADDED
@@ -0,0 +1,852 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *getqloc_downlevel.c - get qualified locale for downlevel OS
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * defines __acrt_get_qualified_locale_downlevel - get complete locale information
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+ #include <locale.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+
15
+ extern "C" {
16
+
17
+ // local defines
18
+ #define __LCID_DEFAULT 0x1 // default language locale for country
19
+ #define __LCID_PRIMARY 0x2 // primary language locale for country
20
+ #define __LCID_FULL 0x4 // fully matched language locale for country
21
+ #define __LCID_LANGUAGE 0x100 // language default seen
22
+ #define __LCID_EXISTS 0x200 // language is installed
23
+
24
+ typedef struct tagRGLOCINFO
25
+ {
26
+ LCID lcid;
27
+ wchar_t chILanguage[8];
28
+ wchar_t * pchSEngLanguage;
29
+ wchar_t chSAbbrevLangName[4];
30
+ wchar_t * pchSEngCountry;
31
+ wchar_t chSAbbrevCtryName[4];
32
+ wchar_t chIDefaultCodepage[8];
33
+ wchar_t chIDefaultAnsiCodepage[8];
34
+ } RGLOCINFO;
35
+
36
+ static bool TranslateName(const __crt_locale_string_table *, int, const wchar_t **);
37
+
38
+ static void GetLcidFromLangCountry (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
39
+ static BOOL CALLBACK LangCountryEnumProc(_In_z_ PWSTR);
40
+
41
+ static void GetLcidFromLanguage (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
42
+ static BOOL CALLBACK LanguageEnumProc(_In_z_ PWSTR);
43
+
44
+ static void GetLcidFromCountry (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
45
+ static BOOL CALLBACK CountryEnumProc(_In_z_ PWSTR);
46
+
47
+ static void GetLcidFromDefault (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
48
+
49
+ static int ProcessCodePage (LPCWSTR lpCodePageStr, __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
50
+ static BOOL TestDefaultCountry(LCID);
51
+ static BOOL TestDefaultLanguage (LCID lcid, BOOL bTestPrimary, __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data);
52
+
53
+ static LCID LcidFromHexString(_In_z_ PCWSTR);
54
+ static int GetPrimaryLen(wchar_t const*);
55
+
56
+ // LANGID's of locales of nondefault languages
57
+ extern __declspec(selectany) LANGID const __rglangidNotDefault[] =
58
+ {
59
+ MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_CANADIAN),
60
+ MAKELANGID(LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC),
61
+ MAKELANGID(LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG),
62
+ MAKELANGID(LANG_AFRIKAANS, SUBLANG_DEFAULT),
63
+ MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_BELGIAN),
64
+ MAKELANGID(LANG_BASQUE, SUBLANG_DEFAULT),
65
+ MAKELANGID(LANG_CATALAN, SUBLANG_DEFAULT),
66
+ MAKELANGID(LANG_FRENCH, SUBLANG_FRENCH_SWISS),
67
+ MAKELANGID(LANG_ITALIAN, SUBLANG_ITALIAN_SWISS),
68
+ MAKELANGID(LANG_SWEDISH, SUBLANG_SWEDISH_FINLAND)
69
+ };
70
+
71
+ /***
72
+ *BOOL __acrt_get_qualified_locale_downlevel - return fully qualified locale
73
+ *
74
+ *Purpose:
75
+ * get default locale, qualify partially complete locales
76
+ *
77
+ *Entry:
78
+ * lpInStr - input strings to be qualified
79
+ * lpOutId - pointer to numeric LCIDs and codepage output
80
+ * lpOutStr - pointer to string LCIDs and codepage output
81
+ *
82
+ *Exit:
83
+ * TRUE if success, qualified locale is valid
84
+ * FALSE if failure
85
+ *
86
+ *Exceptions:
87
+ *
88
+ *******************************************************************************/
89
+ BOOL __cdecl __acrt_get_qualified_locale_downlevel(const __crt_locale_strings* lpInStr, UINT* lpOutCodePage, __crt_locale_strings* lpOutStr)
90
+ {
91
+ int iCodePage;
92
+
93
+ // Get setloc data from per thread data struct
94
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
95
+
96
+ // Set downlevel setloc data in per thread data struct for use by LCID downlevel APIs
97
+ __crt_qualified_locale_data_downlevel downlevel_setloc;
98
+ __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data;
99
+
100
+ memset(&downlevel_setloc, '\0', sizeof(__crt_qualified_locale_data_downlevel));
101
+ _psetloc_downlevel_data = __acrt_getptd()->_setloc_downlevel_data = &downlevel_setloc;
102
+
103
+ // initialize pointer to call locale info routine based on operating system
104
+
105
+ _psetloc_data->pchLanguage = lpInStr->szLanguage;
106
+
107
+ // convert non-NLS country strings to three-letter abbreviations
108
+ _psetloc_data->pchCountry = lpInStr->szCountry;
109
+ if (_psetloc_data->pchCountry && *_psetloc_data->pchCountry)
110
+ TranslateName(__acrt_rg_country,
111
+ static_cast<int>(__acrt_rg_country_count - 1),
112
+ &_psetloc_data->pchCountry);
113
+
114
+ _psetloc_downlevel_data->iLcidState = 0;
115
+
116
+ if (_psetloc_data->pchLanguage && *_psetloc_data->pchLanguage)
117
+ {
118
+ if (_psetloc_data->pchCountry && *_psetloc_data->pchCountry)
119
+ {
120
+ // both language and country strings defined
121
+ GetLcidFromLangCountry(_psetloc_downlevel_data);
122
+ }
123
+ else
124
+ {
125
+ // language string defined, but country string undefined
126
+ GetLcidFromLanguage(_psetloc_downlevel_data);
127
+ }
128
+
129
+ if (!_psetloc_downlevel_data->iLcidState) {
130
+ // first attempt failed, try substituting the language name
131
+ // convert non-NLS language strings to three-letter abbrevs
132
+ if (TranslateName(__acrt_rg_language,
133
+ static_cast<int>(__acrt_rg_language_count - 1),
134
+ &_psetloc_data->pchLanguage))
135
+ {
136
+ if (_psetloc_data->pchCountry && *_psetloc_data->pchCountry)
137
+ {
138
+ GetLcidFromLangCountry(_psetloc_downlevel_data);
139
+ }
140
+ else
141
+ {
142
+ GetLcidFromLanguage(_psetloc_downlevel_data);
143
+ }
144
+ }
145
+ }
146
+ }
147
+ else
148
+ {
149
+ if (_psetloc_data->pchCountry && *_psetloc_data->pchCountry)
150
+ {
151
+ // country string defined, but language string undefined
152
+ GetLcidFromCountry(_psetloc_downlevel_data);
153
+ }
154
+ else
155
+ {
156
+ // both language and country strings undefined
157
+ GetLcidFromDefault(_psetloc_downlevel_data);
158
+ }
159
+ }
160
+
161
+ // test for error in LCID processing
162
+ if (!_psetloc_downlevel_data->iLcidState)
163
+ return FALSE;
164
+
165
+ // process codepage value
166
+ iCodePage = ProcessCodePage(lpInStr ? lpInStr->szCodePage: nullptr, _psetloc_downlevel_data);
167
+
168
+ // verify codepage validity
169
+ if (!iCodePage || !IsValidCodePage((WORD)iCodePage))
170
+ return FALSE;
171
+
172
+ // verify locale is installed
173
+ if (!IsValidLocale(_psetloc_downlevel_data->lcidLanguage, LCID_INSTALLED))
174
+ return FALSE;
175
+
176
+ // set codepage
177
+ if (lpOutCodePage)
178
+ {
179
+ *lpOutCodePage = (UINT)iCodePage;
180
+ }
181
+
182
+ // store locale name in cache
183
+ __acrt_LCIDToLocaleName(
184
+ _psetloc_downlevel_data->lcidLanguage,
185
+ _psetloc_data->_cacheLocaleName,
186
+ (int)_countof(_psetloc_data->_cacheLocaleName),
187
+ 0);
188
+
189
+ // set locale name and codepage results
190
+ if (lpOutStr)
191
+ {
192
+ __acrt_LCIDToLocaleName(
193
+ _psetloc_downlevel_data->lcidLanguage,
194
+ lpOutStr->szLocaleName,
195
+ (int)_countof(lpOutStr->szLocaleName),
196
+ 0);
197
+
198
+ if (GetLocaleInfoW(_psetloc_downlevel_data->lcidLanguage, LOCALE_SENGLANGUAGE,
199
+ lpOutStr->szLanguage, MAX_LANG_LEN) == 0)
200
+ return FALSE;
201
+
202
+ if (GetLocaleInfoW(_psetloc_downlevel_data->lcidCountry, LOCALE_SENGCOUNTRY,
203
+ lpOutStr->szCountry, MAX_CTRY_LEN) == 0)
204
+ return FALSE;
205
+
206
+ _itow_s((int)iCodePage, (wchar_t *)lpOutStr->szCodePage, MAX_CP_LEN, 10);
207
+ }
208
+
209
+ return TRUE;
210
+ }
211
+
212
+ /***
213
+ *BOOL TranslateName - convert known non-NLS string to NLS equivalent
214
+ *
215
+ *Purpose:
216
+ * Provide compatibility with existing code for non-NLS strings
217
+ *
218
+ *Entry:
219
+ * lpTable - pointer to __crt_locale_string_table used for translation
220
+ * high - maximum index of table (size - 1)
221
+ * ppchName - pointer to pointer of string to translate
222
+ *
223
+ *Exit:
224
+ * ppchName - pointer to pointer of string possibly translated
225
+ * TRUE if string translated, FALSE if unchanged
226
+ *
227
+ *Exceptions:
228
+ *
229
+ *******************************************************************************/
230
+ static bool TranslateName (
231
+ const __crt_locale_string_table* lpTable,
232
+ int high,
233
+ const wchar_t ** ppchName)
234
+ {
235
+ int low = 0;
236
+
237
+ // typical binary search - do until no more to search or match
238
+ while (low <= high)
239
+ {
240
+ int const i = (low + high) / 2;
241
+ int const cmp = _wcsicmp(*ppchName, lpTable[i].szName);
242
+
243
+ if (cmp == 0)
244
+ {
245
+ *ppchName = lpTable[i].chAbbrev;
246
+ return true;
247
+ }
248
+ else if (cmp < 0)
249
+ high = i - 1;
250
+ else
251
+ low = i + 1;
252
+ }
253
+
254
+ return false;
255
+ }
256
+
257
+ /***
258
+ *void GetLcidFromLangCountry - get LCIDs from language and country strings
259
+ *
260
+ *Purpose:
261
+ * Match the best LCIDs to the language and country string given.
262
+ * After global variables are initialized, the LangCountryEnumProc
263
+ * routine is registered as an EnumSystemLocalesA callback to actually
264
+ * perform the matching as the LCIDs are enumerated.
265
+ *
266
+ *Entry:
267
+ * pchLanguage - language string
268
+ * bAbbrevLanguage - language string is a three-letter abbreviation
269
+ * pchCountry - country string
270
+ * bAbbrevCountry - country string ia a three-letter abbreviation
271
+ * iPrimaryLen - length of language string with primary name
272
+ *
273
+ *Exit:
274
+ * lcidLanguage - LCID of language string
275
+ * lcidCountry - LCID of country string
276
+ *
277
+ *Exceptions:
278
+ *
279
+ *******************************************************************************/
280
+ static void GetLcidFromLangCountry (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
281
+ {
282
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
283
+
284
+ // initialize static variables for callback use
285
+ _psetloc_data->bAbbrevLanguage = wcslen(_psetloc_data->pchLanguage) == 3;
286
+ _psetloc_data->bAbbrevCountry = wcslen(_psetloc_data->pchCountry) == 3;
287
+ _psetloc_downlevel_data->lcidLanguage = 0;
288
+ _psetloc_data->iPrimaryLen = _psetloc_data->bAbbrevLanguage ?
289
+ 2 : GetPrimaryLen(_psetloc_data->pchLanguage);
290
+
291
+ EnumSystemLocalesW(LangCountryEnumProc, LCID_INSTALLED);
292
+
293
+ // locale value is invalid if the language was not installed or the language
294
+ // was not available for the country specified
295
+ if (!(_psetloc_downlevel_data->iLcidState & __LCID_LANGUAGE) ||
296
+ !(_psetloc_downlevel_data->iLcidState & __LCID_EXISTS) ||
297
+ !(_psetloc_downlevel_data->iLcidState & (__LCID_FULL |
298
+ __LCID_PRIMARY |
299
+ __LCID_DEFAULT)))
300
+ _psetloc_downlevel_data->iLcidState = 0;
301
+ }
302
+
303
+ /***
304
+ *BOOL CALLBACK LangCountryEnumProc - callback routine for GetLcidFromLangCountry
305
+ *
306
+ *Purpose:
307
+ * Determine if LCID given matches the language in pchLanguage
308
+ * and country in pchCountry.
309
+ *
310
+ *Entry:
311
+ * lpLcidString - pointer to string with decimal LCID
312
+ * pchCountry - pointer to country name
313
+ * bAbbrevCountry - set if country is three-letter abbreviation
314
+ *
315
+ *Exit:
316
+ * iLcidState - status of match
317
+ * __LCID_FULL - both language and country match (best match)
318
+ * __LCID_PRIMARY - primary language and country match (better)
319
+ * __LCID_DEFAULT - default language and country match (good)
320
+ * __LCID_LANGUAGE - default primary language exists
321
+ * __LCID_EXISTS - full match of language string exists
322
+ * (Overall match occurs for the best of FULL/PRIMARY/DEFAULT
323
+ * and LANGUAGE/EXISTS both set.)
324
+ * lcidLanguage - LCID matched
325
+ * lcidCountry - LCID matched
326
+ * FALSE if match occurred to terminate enumeration, else TRUE.
327
+ *
328
+ *Exceptions:
329
+ *
330
+ *******************************************************************************/
331
+ static BOOL CALLBACK LangCountryEnumProc (_In_z_ PWSTR lpLcidString)
332
+ {
333
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
334
+ __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data = __acrt_getptd()->_setloc_downlevel_data;
335
+ LCID lcid = LcidFromHexString(lpLcidString);
336
+ wchar_t rgcInfo[120];
337
+
338
+ // test locale country against input value
339
+ if (GetLocaleInfoW(lcid,
340
+ _psetloc_data->bAbbrevCountry ?
341
+ LOCALE_SABBREVCTRYNAME : LOCALE_SENGCOUNTRY,
342
+ rgcInfo, _countof(rgcInfo)) == 0)
343
+ {
344
+ // set error condition and exit
345
+ _psetloc_downlevel_data->iLcidState = 0;
346
+ return TRUE;
347
+ }
348
+ if (!_wcsicmp(_psetloc_data->pchCountry, rgcInfo))
349
+ {
350
+ // country matched - test for language match
351
+ if (GetLocaleInfoW(lcid,
352
+ _psetloc_data->bAbbrevLanguage ?
353
+ LOCALE_SABBREVLANGNAME : LOCALE_SENGLANGUAGE,
354
+ rgcInfo, _countof(rgcInfo)) == 0)
355
+ {
356
+ // set error condition and exit
357
+ _psetloc_downlevel_data->iLcidState = 0;
358
+ return TRUE;
359
+ }
360
+ if (!_wcsicmp(_psetloc_data->pchLanguage, rgcInfo))
361
+ {
362
+ // language matched also - set state and value
363
+ _psetloc_downlevel_data->iLcidState |= (__LCID_FULL |
364
+ __LCID_LANGUAGE |
365
+ __LCID_EXISTS);
366
+ _psetloc_downlevel_data->lcidLanguage = _psetloc_downlevel_data->lcidCountry = lcid;
367
+ }
368
+
369
+ // test if match already for primary langauage
370
+ else if (!(_psetloc_downlevel_data->iLcidState & __LCID_PRIMARY))
371
+ {
372
+ // if not, use _psetloc_data->iPrimaryLen to partial match language string
373
+ if (_psetloc_data->iPrimaryLen && !_wcsnicmp(_psetloc_data->pchLanguage, rgcInfo, _psetloc_data->iPrimaryLen))
374
+ {
375
+ // primary language matched - set state and country LCID
376
+ _psetloc_downlevel_data->iLcidState |= __LCID_PRIMARY;
377
+ _psetloc_downlevel_data->lcidCountry = lcid;
378
+
379
+ // if language is primary only (no subtype), set language LCID
380
+ if ((int)wcslen(_psetloc_data->pchLanguage) == _psetloc_data->iPrimaryLen)
381
+ _psetloc_downlevel_data->lcidLanguage = lcid;
382
+ }
383
+
384
+ // test if default language already defined
385
+ else if (!(_psetloc_downlevel_data->iLcidState & __LCID_DEFAULT))
386
+ {
387
+ // if not, test if locale language is default for country
388
+ if (TestDefaultCountry(lcid))
389
+ {
390
+ // default language for country - set state, value
391
+ _psetloc_downlevel_data->iLcidState |= __LCID_DEFAULT;
392
+ _psetloc_downlevel_data->lcidCountry = lcid;
393
+ }
394
+ }
395
+ }
396
+ }
397
+ // test if input language both exists and default primary language defined
398
+ if ((_psetloc_downlevel_data->iLcidState & (__LCID_LANGUAGE | __LCID_EXISTS)) !=
399
+ (__LCID_LANGUAGE | __LCID_EXISTS))
400
+ {
401
+ // test language match to determine whether it is installed
402
+ if (GetLocaleInfoW(lcid, _psetloc_data->bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
403
+ : LOCALE_SENGLANGUAGE,
404
+ rgcInfo, _countof(rgcInfo)) == 0)
405
+ {
406
+ // set error condition and exit
407
+ _psetloc_downlevel_data->iLcidState = 0;
408
+ return TRUE;
409
+ }
410
+
411
+ if (!_wcsicmp(_psetloc_data->pchLanguage, rgcInfo))
412
+ {
413
+ // language matched - set bit for existance
414
+ _psetloc_downlevel_data->iLcidState |= __LCID_EXISTS;
415
+
416
+ if (_psetloc_data->bAbbrevLanguage)
417
+ {
418
+ // abbreviation - set state
419
+ // also set language LCID if not set already
420
+ _psetloc_downlevel_data->iLcidState |= __LCID_LANGUAGE;
421
+ if (!_psetloc_downlevel_data->lcidLanguage)
422
+ _psetloc_downlevel_data->lcidLanguage = lcid;
423
+ }
424
+
425
+ // test if language is primary only (no sublanguage)
426
+ else if (_psetloc_data->iPrimaryLen && ((int)wcslen(_psetloc_data->pchLanguage) == _psetloc_data->iPrimaryLen))
427
+ {
428
+ // primary language only - test if default LCID
429
+ if (TestDefaultLanguage(lcid, TRUE, _psetloc_downlevel_data))
430
+ {
431
+ // default primary language - set state
432
+ // also set LCID if not set already
433
+ _psetloc_downlevel_data->iLcidState |= __LCID_LANGUAGE;
434
+ if (!_psetloc_downlevel_data->lcidLanguage)
435
+ _psetloc_downlevel_data->lcidLanguage = lcid;
436
+ }
437
+ }
438
+ else
439
+ {
440
+ // language with sublanguage - set state
441
+ // also set LCID if not set already
442
+ _psetloc_downlevel_data->iLcidState |= __LCID_LANGUAGE;
443
+ if (!_psetloc_downlevel_data->lcidLanguage)
444
+ _psetloc_downlevel_data->lcidLanguage = lcid;
445
+ }
446
+ }
447
+ else if (!_psetloc_data->bAbbrevLanguage && _psetloc_data->iPrimaryLen
448
+ && !_wcsicmp(_psetloc_data->pchLanguage, rgcInfo))
449
+ {
450
+ // primary language match - test for default language only
451
+ if (TestDefaultLanguage(lcid, FALSE, _psetloc_downlevel_data))
452
+ {
453
+ // default primary language - set state
454
+ // also set LCID if not set already
455
+ _psetloc_downlevel_data->iLcidState |= __LCID_LANGUAGE;
456
+ if (!_psetloc_downlevel_data->lcidLanguage)
457
+ _psetloc_downlevel_data->lcidLanguage = lcid;
458
+ }
459
+ }
460
+ }
461
+
462
+ // if LOCALE_FULL set, return FALSE to stop enumeration,
463
+ // else return TRUE to continue
464
+ return (_psetloc_downlevel_data->iLcidState & __LCID_FULL) == 0;
465
+ }
466
+
467
+ /***
468
+ *void GetLcidFromLanguage - get LCIDs from language string
469
+ *
470
+ *Purpose:
471
+ * Match the best LCIDs to the language string given. After global
472
+ * variables are initialized, the LanguageEnumProc routine is
473
+ * registered as an EnumSystemLocalesA callback to actually perform
474
+ * the matching as the LCIDs are enumerated.
475
+ *
476
+ *Entry:
477
+ * pchLanguage - language string
478
+ * bAbbrevLanguage - language string is a three-letter abbreviation
479
+ * iPrimaryLen - length of language string with primary name
480
+ *
481
+ *Exit:
482
+ * lcidLanguage - lcidCountry - LCID of language with default
483
+ * country
484
+ *
485
+ *Exceptions:
486
+ *
487
+ *******************************************************************************/
488
+ static void GetLcidFromLanguage (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
489
+ {
490
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
491
+
492
+ // initialize static variables for callback use
493
+ _psetloc_data->bAbbrevLanguage = wcslen(_psetloc_data->pchLanguage) == 3;
494
+ _psetloc_data->iPrimaryLen = _psetloc_data->bAbbrevLanguage ? 2 : GetPrimaryLen(_psetloc_data->pchLanguage);
495
+
496
+ EnumSystemLocalesW(LanguageEnumProc, LCID_INSTALLED);
497
+
498
+ // locale value is invalid if the language was not installed
499
+ // or the language was not available for the country specified
500
+ if (!(_psetloc_downlevel_data->iLcidState & __LCID_FULL))
501
+ _psetloc_downlevel_data->iLcidState = 0;
502
+ }
503
+
504
+ /***
505
+ *BOOL CALLBACK LanguageEnumProc - callback routine for GetLcidFromLanguage
506
+ *
507
+ *Purpose:
508
+ * Determine if LCID given matches the default country for the
509
+ * language in pchLanguage.
510
+ *
511
+ *Entry:
512
+ * lpLcidString - pointer to string with decimal LCID
513
+ * pchLanguage - pointer to language name
514
+ * bAbbrevLanguage - set if language is three-letter abbreviation
515
+ *
516
+ *Exit:
517
+ * lcidLanguage - lcidCountry - LCID matched
518
+ * FALSE if match occurred to terminate enumeration, else TRUE.
519
+ *
520
+ *Exceptions:
521
+ *
522
+ *******************************************************************************/
523
+ static BOOL CALLBACK LanguageEnumProc (_In_z_ PWSTR lpLcidString)
524
+ {
525
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
526
+ __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data = __acrt_getptd()->_setloc_downlevel_data;
527
+
528
+ LCID lcid = LcidFromHexString(lpLcidString);
529
+ wchar_t rgcInfo[120];
530
+
531
+ // test locale for language specified
532
+ if (GetLocaleInfoW(lcid, _psetloc_data->bAbbrevLanguage ? LOCALE_SABBREVLANGNAME
533
+ : LOCALE_SENGLANGUAGE,
534
+ rgcInfo, _countof(rgcInfo)) == 0)
535
+ {
536
+ // set error condition and exit
537
+ _psetloc_downlevel_data->iLcidState = 0;
538
+ return TRUE;
539
+ }
540
+
541
+ if (!_wcsicmp(_psetloc_data->pchLanguage, rgcInfo))
542
+ {
543
+ // language matched - test if locale country is default
544
+ // or if locale is implied in the language string
545
+ if (_psetloc_data->bAbbrevLanguage || TestDefaultLanguage(lcid, TRUE, _psetloc_downlevel_data))
546
+ {
547
+ // this locale has the default country
548
+ _psetloc_downlevel_data->lcidLanguage = _psetloc_downlevel_data->lcidCountry = lcid;
549
+ _psetloc_downlevel_data->iLcidState |= __LCID_FULL;
550
+ }
551
+ }
552
+ else if (!_psetloc_data->bAbbrevLanguage && _psetloc_data->iPrimaryLen
553
+ && !_wcsicmp(_psetloc_data->pchLanguage, rgcInfo))
554
+ {
555
+ // primary language matched - test if locale country is default
556
+ if (TestDefaultLanguage(lcid, FALSE, _psetloc_downlevel_data))
557
+ {
558
+ // this is the default country
559
+ _psetloc_downlevel_data->lcidLanguage = _psetloc_downlevel_data->lcidCountry = lcid;
560
+ _psetloc_downlevel_data->iLcidState |= __LCID_FULL;
561
+ }
562
+ }
563
+
564
+ return (_psetloc_downlevel_data->iLcidState & __LCID_FULL) == 0;
565
+ }
566
+
567
+ /***
568
+ *void GetLcidFromCountry - get LCIDs from country string
569
+ *
570
+ *Purpose:
571
+ * Match the best LCIDs to the country string given. After global
572
+ * variables are initialized, the CountryEnumProc routine is
573
+ * registered as an EnumSystemLocalesA callback to actually perform
574
+ * the matching as the LCIDs are enumerated.
575
+ *
576
+ *Entry:
577
+ * pchCountry - country string
578
+ * bAbbrevCountry - country string is a three-letter abbreviation
579
+ *
580
+ *Exit:
581
+ * lcidLanguage - lcidCountry - LCID of country with default
582
+ * language
583
+ *
584
+ *Exceptions:
585
+ *
586
+ *******************************************************************************/
587
+ static void GetLcidFromCountry (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
588
+ {
589
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
590
+ _psetloc_data->bAbbrevCountry = wcslen(_psetloc_data->pchCountry) == 3;
591
+
592
+ EnumSystemLocalesW(CountryEnumProc, LCID_INSTALLED);
593
+
594
+ // locale value is invalid if the country was not defined or
595
+ // no default language was found
596
+ if (!(_psetloc_downlevel_data->iLcidState & __LCID_FULL))
597
+ _psetloc_downlevel_data->iLcidState = 0;
598
+ }
599
+
600
+ /***
601
+ *BOOL CALLBACK CountryEnumProc - callback routine for GetLcidFromCountry
602
+ *
603
+ *Purpose:
604
+ * Determine if LCID given matches the default language for the
605
+ * country in pchCountry.
606
+ *
607
+ *Entry:
608
+ * lpLcidString - pointer to string with decimal LCID
609
+ * pchCountry - pointer to country name
610
+ * bAbbrevCountry - set if country is three-letter abbreviation
611
+ *
612
+ *Exit:
613
+ * lcidLanguage - lcidCountry - LCID matched
614
+ * FALSE if match occurred to terminate enumeration, else TRUE.
615
+ *
616
+ *Exceptions:
617
+ *
618
+ *******************************************************************************/
619
+ static BOOL CALLBACK CountryEnumProc (_In_z_ PWSTR lpLcidString)
620
+ {
621
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
622
+ __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data = __acrt_getptd()->_setloc_downlevel_data;
623
+ LCID lcid = LcidFromHexString(lpLcidString);
624
+ wchar_t rgcInfo[120];
625
+
626
+ // test locale for country specified
627
+ if (GetLocaleInfoW(lcid, _psetloc_data->bAbbrevCountry ? LOCALE_SABBREVCTRYNAME
628
+ : LOCALE_SENGCOUNTRY,
629
+ rgcInfo, _countof(rgcInfo)) == 0)
630
+ {
631
+ // set error condition and exit
632
+ _psetloc_downlevel_data->iLcidState = 0;
633
+ return TRUE;
634
+ }
635
+ if (!_wcsicmp(_psetloc_data->pchCountry, rgcInfo))
636
+ {
637
+ // language matched - test if locale country is default
638
+ if (TestDefaultCountry(lcid))
639
+ {
640
+ // this locale has the default language
641
+ _psetloc_downlevel_data->lcidLanguage = _psetloc_downlevel_data->lcidCountry = lcid;
642
+ _psetloc_downlevel_data->iLcidState |= __LCID_FULL;
643
+ }
644
+ }
645
+ return (_psetloc_downlevel_data->iLcidState & __LCID_FULL) == 0;
646
+ }
647
+
648
+ /***
649
+ *void GetLcidFromDefault - get default LCIDs
650
+ *
651
+ *Purpose:
652
+ * Set both language and country LCIDs to the system default.
653
+ *
654
+ *Entry:
655
+ * None.
656
+ *
657
+ *Exit:
658
+ * lcidLanguage - set to system LCID
659
+ * lcidCountry - set to system LCID
660
+ *
661
+ *Exceptions:
662
+ *
663
+ *******************************************************************************/
664
+ static void GetLcidFromDefault (__crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
665
+ {
666
+ _psetloc_downlevel_data->iLcidState |= (__LCID_FULL | __LCID_LANGUAGE);
667
+ _psetloc_downlevel_data->lcidLanguage = _psetloc_downlevel_data->lcidCountry = GetUserDefaultLCID();
668
+ }
669
+
670
+ /***
671
+ *int ProcessCodePage - convert codepage string to numeric value
672
+ *
673
+ *Purpose:
674
+ * Process codepage string consisting of a decimal string, or the
675
+ * special case strings "ACP" and "OCP", for ANSI and OEM codepages,
676
+ * respectively. Null pointer or string returns the ANSI codepage.
677
+ *
678
+ *Entry:
679
+ * lpCodePageStr - pointer to codepage string
680
+ *
681
+ *Exit:
682
+ * Returns numeric value of codepage.
683
+ *
684
+ *Exceptions:
685
+ *
686
+ *******************************************************************************/
687
+ static int ProcessCodePage (LPCWSTR lpCodePageStr, __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
688
+ {
689
+ int iCodePage;
690
+
691
+ if (!lpCodePageStr || !*lpCodePageStr || !wcscmp(lpCodePageStr, L"ACP"))
692
+ {
693
+ // get ANSI codepage for the country LCID
694
+ if (GetLocaleInfoW(_psetloc_downlevel_data->lcidCountry, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
695
+ (LPWSTR) &iCodePage, sizeof(iCodePage) / sizeof(wchar_t)) == 0)
696
+ return 0;
697
+
698
+ if (iCodePage == 0) // for locales have no assoicated ANSI codepage, e.g. Hindi locale
699
+ return GetACP();
700
+ }
701
+ else if (!wcscmp(lpCodePageStr, L"OCP"))
702
+ {
703
+ // get OEM codepage for the country LCID
704
+ if (GetLocaleInfoW(_psetloc_downlevel_data->lcidCountry, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
705
+ (LPWSTR) &iCodePage, sizeof(iCodePage) / sizeof(wchar_t)) == 0)
706
+ return 0;
707
+ }
708
+ else
709
+ {
710
+ // convert decimal string to numeric value
711
+ iCodePage = (int)_wtol(lpCodePageStr);
712
+ }
713
+
714
+ return iCodePage;
715
+ }
716
+
717
+ /***
718
+ *BOOL TestDefaultCountry - determine if default locale for country
719
+ *
720
+ *Purpose:
721
+ * Using a hardcoded list, determine if the locale of the given LCID
722
+ * has the default sublanguage for the locale primary language. The
723
+ * list contains the locales NOT having the default sublanguage.
724
+ *
725
+ *Entry:
726
+ * lcid - LCID of locale to test
727
+ *
728
+ *Exit:
729
+ * Returns TRUE if default sublanguage, else FALSE.
730
+ *
731
+ *Exceptions:
732
+ *
733
+ *******************************************************************************/
734
+ static BOOL TestDefaultCountry (LCID lcid)
735
+ {
736
+ LANGID langid = LANGIDFROMLCID(lcid);
737
+ int i;
738
+
739
+ for (i = 0; i < _countof(__rglangidNotDefault); i++)
740
+ {
741
+ if (langid == __rglangidNotDefault[i])
742
+ return FALSE;
743
+ }
744
+ return TRUE;
745
+ }
746
+
747
+ /***
748
+ *BOOL TestDefaultLanguage - determine if default locale for language
749
+ *
750
+ *Purpose:
751
+ * Determines if the given LCID has the default sublanguage.
752
+ * If bTestPrimary is set, also allow TRUE when string contains an
753
+ * implicit sublanguage.
754
+ *
755
+ *Entry:
756
+ * LCID - lcid of locale to test
757
+ * bTestPrimary - set if testing if language is primary
758
+ *
759
+ *Exit:
760
+ * Returns TRUE if sublanguage is default for locale tested.
761
+ * If bTestPrimary set, TRUE is language has implied sublanguge.
762
+ *
763
+ *Exceptions:
764
+ *
765
+ *******************************************************************************/
766
+ static BOOL TestDefaultLanguage (LCID lcid, BOOL bTestPrimary, __crt_qualified_locale_data_downlevel* _psetloc_downlevel_data)
767
+ {
768
+ UNREFERENCED_PARAMETER(_psetloc_downlevel_data); // CRT_REFACTOR TODO
769
+
770
+ DWORD dwLanguage;
771
+ LCID lcidDefault = MAKELCID(MAKELANGID(PRIMARYLANGID(LANGIDFROMLCID(lcid)), SUBLANG_DEFAULT), SORT_DEFAULT);
772
+ __crt_qualified_locale_data* _psetloc_data = &__acrt_getptd()->_setloc_data;
773
+
774
+ if (GetLocaleInfoW(lcidDefault, LOCALE_ILANGUAGE | LOCALE_RETURN_NUMBER,
775
+ (LPWSTR) &dwLanguage, sizeof(dwLanguage) / sizeof(wchar_t)) == 0)
776
+ return FALSE;
777
+
778
+ if (lcid != dwLanguage)
779
+ {
780
+ // test if string contains an implicit sublanguage by
781
+ // having a character other than upper/lowercase letters.
782
+ if (bTestPrimary && GetPrimaryLen(_psetloc_data->pchLanguage) == (int)wcslen(_psetloc_data->pchLanguage))
783
+ return FALSE;
784
+ }
785
+ return TRUE;
786
+ }
787
+
788
+ /***
789
+ *LCID LcidFromHexString - convert hex string to value for LCID
790
+ *
791
+ *Purpose:
792
+ * LCID values returned in hex ANSI strings - straight conversion
793
+ *
794
+ *Entry:
795
+ * lpHexString - pointer to hex string to convert
796
+ *
797
+ *Exit:
798
+ * Returns LCID computed.
799
+ *
800
+ *Exceptions:
801
+ *
802
+ *******************************************************************************/
803
+ static LCID LcidFromHexString (_In_z_ PCWSTR lpHexString)
804
+ {
805
+ wchar_t ch;
806
+ DWORD lcid = 0;
807
+
808
+ #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 This is an idiomatic nul termination check that Prefast doesn't understand.
809
+ while ((ch = *lpHexString++) != '\0')
810
+ {
811
+ if (ch >= 'a' && ch <= 'f')
812
+ ch += static_cast<wchar_t>('9' + 1 - 'a');
813
+ else if (ch >= 'A' && ch <= 'F')
814
+ ch += static_cast<wchar_t>('9' + 1 - 'A');
815
+ lcid = lcid * 0x10 + ch - '0';
816
+ }
817
+
818
+ return (LCID)lcid;
819
+ }
820
+
821
+ /***
822
+ *int GetPrimaryLen - get length of primary language name
823
+ *
824
+ *Purpose:
825
+ * Determine primary language string length by scanning until
826
+ * first non-alphabetic character.
827
+ *
828
+ *Entry:
829
+ * pchLanguage - string to scan
830
+ *
831
+ *Exit:
832
+ * Returns length of primary language string.
833
+ *
834
+ *Exceptions:
835
+ *
836
+ *******************************************************************************/
837
+ static int GetPrimaryLen (wchar_t const* pchLanguage)
838
+ {
839
+ int len = 0;
840
+ wchar_t ch;
841
+
842
+ ch = *pchLanguage++;
843
+ while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
844
+ {
845
+ len++;
846
+ ch = *pchLanguage++;
847
+ }
848
+
849
+ return len;
850
+ }
851
+
852
+ } // extern "C"
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/glstatus.cpp ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *glstatus.c - sets the __globallocalestatus flag
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Sets the __globallocalestatus flag to disable per thread locale
8
+ *
9
+ *******************************************************************************/
10
+
11
+ #include <corecrt_internal.h>
12
+
13
+ extern "C" int __globallocalestatus = (~_GLOBAL_LOCALE_BIT);
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initctype.cpp ADDED
@@ -0,0 +1,339 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *initctype.cpp - contains __acrt_locale_initialize_ctype
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the locale-category initialization function: __acrt_locale_initialize_ctype().
8
+ *
9
+ * Each initialization function sets up locale-specific information
10
+ * for their category, for use by functions which are affected by
11
+ * their locale category.
12
+ *
13
+ * *** For internal use by setlocale() only ***
14
+ *
15
+ *******************************************************************************/
16
+ #include <corecrt_internal.h>
17
+ #include <locale.h>
18
+ #include <stdlib.h>
19
+
20
+ #define _CTABSIZE 257 /* size of ctype tables */
21
+
22
+ // Enclaves have no ability to create new locales.
23
+ #ifndef _UCRT_ENCLAVE_BUILD
24
+
25
+ /***
26
+ *int __acrt_locale_initialize_ctype() - initialization for LC_CTYPE locale category.
27
+ *
28
+ *Purpose:
29
+ * In non-C locales, preread ctype tables for chars and wide-chars.
30
+ * Old tables are freed when new tables are fully established, else
31
+ * the old tables remain intact (as if original state unaltered).
32
+ * The leadbyte table is implemented as the high bit in ctype1.
33
+ *
34
+ * In the C locale, ctype tables are freed, and pointers point to
35
+ * the static ctype table.
36
+ *
37
+ * Tables contain 257 entries: -1 to 256.
38
+ * Table pointers point to entry 0 (to allow index -1).
39
+ *
40
+ *Entry:
41
+ * None.
42
+ *
43
+ *Exit:
44
+ * 0 success
45
+ * 1 fail
46
+ *
47
+ *Exceptions:
48
+ *
49
+ *******************************************************************************/
50
+
51
+ int __cdecl __acrt_locale_initialize_ctype (
52
+ __crt_locale_data* ploci
53
+ )
54
+ {
55
+ /* non-C locale table for char's */
56
+ unsigned short *newctype1 = nullptr; /* temp new table */
57
+ unsigned char *newclmap = nullptr; /* temp new map table */
58
+ unsigned char *newcumap = nullptr; /* temp new map table */
59
+
60
+ /* non-C locale table for wchar_t's */
61
+
62
+ unsigned char *cbuffer = nullptr; /* char working buffer */
63
+
64
+ int i; /* general purpose counter */
65
+ unsigned char *cp; /* char pointer */
66
+ CPINFO cpInfo; /* struct for use with GetCPInfo */
67
+ int mb_cur_max;
68
+ __crt_locale_pointers locinfo;
69
+
70
+ locinfo.locinfo = ploci;
71
+ locinfo.mbcinfo = 0;
72
+
73
+ /* allocate and set up buffers before destroying old ones */
74
+ /* codepage will be restored by setlocale if error */
75
+
76
+ if (ploci->locale_name[LC_CTYPE] != nullptr)
77
+ {
78
+ long* refcount = nullptr;
79
+
80
+ if (ploci->_public._locale_lc_codepage == 0)
81
+ { /* code page was not specified */
82
+ if ( __acrt_GetLocaleInfoA( &locinfo, LC_INT_TYPE,
83
+ ploci->locale_name[LC_CTYPE],
84
+ LOCALE_IDEFAULTANSICODEPAGE,
85
+ (char **)&ploci->_public._locale_lc_codepage ) )
86
+ goto error_cleanup;
87
+ }
88
+
89
+ /* allocate a new (thread) reference counter */
90
+ refcount = _calloc_crt_t(long, 1).detach();
91
+
92
+ /* allocate new buffers for tables */
93
+ newctype1 = _calloc_crt_t(unsigned short, _COFFSET + _CTABSIZE).detach();
94
+ newclmap = _calloc_crt_t(unsigned char, _COFFSET + _CTABSIZE).detach();
95
+ newcumap = _calloc_crt_t(unsigned char, _COFFSET + _CTABSIZE).detach();
96
+ cbuffer = _calloc_crt_t(unsigned char, _CTABSIZE).detach();
97
+
98
+ if (!refcount || !newctype1 || !cbuffer || !newclmap || !newcumap)
99
+ goto error_cleanup;
100
+
101
+ /* construct string composed of first 256 chars in sequence */
102
+ for (cp=cbuffer, i=0; i<_CTABSIZE-1; i++)
103
+ *cp++ = (unsigned char)i;
104
+
105
+ if (GetCPInfo( ploci->_public._locale_lc_codepage, &cpInfo) == FALSE)
106
+ goto error_cleanup;
107
+
108
+ if (cpInfo.MaxCharSize > MB_LEN_MAX)
109
+ goto error_cleanup;
110
+
111
+ mb_cur_max = (unsigned short) cpInfo.MaxCharSize;
112
+
113
+ /* zero (space actually) out leadbytes so GetStringType and
114
+ LCMapStringA don't interpret them as multi-byte chars */
115
+ if (mb_cur_max > 1)
116
+ {
117
+ if (ploci->_public._locale_lc_codepage == CP_UTF8)
118
+ {
119
+ // For UTF-8 anything above 0x7f is part of a multibyte sequence and
120
+ // would confuse the codepage/character code below.
121
+ for (i = 0x80; i <= 0xff; i++)
122
+ {
123
+ // spaces are safe.
124
+ cbuffer[i] = ' ';
125
+ }
126
+ }
127
+ else
128
+ {
129
+ // use the lead byte table to mark off the appropriate bytes
130
+ for (cp = (unsigned char *)cpInfo.LeadByte; cp[0] && cp[1]; cp += 2)
131
+ {
132
+ for (i = cp[0]; i <= cp[1]; i++)
133
+ cbuffer[i] = ' ';
134
+ }
135
+ }
136
+ }
137
+
138
+ /*
139
+ * LCMapString will map past nullptr. Must find nullptr if in string
140
+ * before cchSrc characters.
141
+ */
142
+ if ( __acrt_LCMapStringA(nullptr,
143
+ ploci->locale_name[LC_CTYPE],
144
+ LCMAP_LOWERCASE,
145
+ reinterpret_cast<char*>(cbuffer + 1),
146
+ _CTABSIZE-2,
147
+ reinterpret_cast<char*>(newclmap + 2 + _COFFSET),
148
+ _CTABSIZE-2,
149
+ ploci->_public._locale_lc_codepage,
150
+ FALSE ) == FALSE)
151
+ goto error_cleanup;
152
+
153
+ if ( __acrt_LCMapStringA(nullptr,
154
+ ploci->locale_name[LC_CTYPE],
155
+ LCMAP_UPPERCASE,
156
+ reinterpret_cast<char*>(cbuffer + 1),
157
+ _CTABSIZE-2,
158
+ reinterpret_cast<char*>(newcumap + 2 + _COFFSET),
159
+ _CTABSIZE-2,
160
+ ploci->_public._locale_lc_codepage,
161
+ FALSE ) == FALSE)
162
+ goto error_cleanup;
163
+
164
+ /* convert to newctype1 table - ignore invalid char errors */
165
+ if ( __acrt_GetStringTypeA(nullptr, CT_CTYPE1,
166
+ reinterpret_cast<char*>(cbuffer),
167
+ _CTABSIZE-1,
168
+ newctype1+1+_COFFSET,
169
+ ploci->_public._locale_lc_codepage,
170
+ FALSE ) == FALSE )
171
+ goto error_cleanup;
172
+
173
+ newctype1[_COFFSET] = 0; /* entry for EOF */
174
+ newclmap[_COFFSET] = 0;
175
+ newcumap[_COFFSET] = 0;
176
+ newclmap[_COFFSET+1] = 0; /* entry for null */
177
+ newcumap[_COFFSET+1] = 0; /* entry for null */
178
+
179
+ /* ignore DefaultChar */
180
+
181
+ /* mark lead-byte entries in newctype1 table and
182
+ restore original values for lead-byte entries for clmap/cumap */
183
+ if (mb_cur_max > 1)
184
+ {
185
+ if (ploci->_public._locale_lc_codepage == CP_UTF8)
186
+ {
187
+ // For UTF-8 anything above 0x7f is part of a multibyte sequence
188
+ // "real" leadbytes start with C0 and end at F7
189
+ // However, C0 & C1 are overlong encoded ASCII, F5 & F6 would be > U+10FFFF.
190
+ // Note that some starting with E0 and F0 are overlong and not legal though.
191
+ for (i = 0x80; i <= 0xff; i++)
192
+ {
193
+ newctype1[_COFFSET + i + 1] = (i >= 0xc2 && i < 0xf5) ? _LEADBYTE : 0;
194
+ newclmap[_COFFSET + i + 1] = static_cast<unsigned char>(i);
195
+ newcumap[_COFFSET + i + 1] = static_cast<unsigned char>(i);
196
+ }
197
+ }
198
+ else
199
+ {
200
+ for (cp = (unsigned char *)cpInfo.LeadByte; cp[0] && cp[1]; cp += 2)
201
+ {
202
+ for (i = cp[0]; i <= cp[1]; i++)
203
+ {
204
+ newctype1[_COFFSET + i + 1] = _LEADBYTE;
205
+ newclmap[_COFFSET + i + 1] = static_cast<unsigned char>(i);
206
+ newcumap[_COFFSET + i + 1] = static_cast<unsigned char>(i);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ /* copy last-1 _COFFSET unsigned short to front
212
+ * note -1, we don't really want to copy 0xff
213
+ */
214
+ memcpy(newctype1,newctype1+_CTABSIZE-1,_COFFSET*sizeof(unsigned short));
215
+ memcpy(newclmap,newclmap+_CTABSIZE-1,_COFFSET*sizeof(char));
216
+ memcpy(newcumap,newcumap+_CTABSIZE-1,_COFFSET*sizeof(char));
217
+
218
+ /* free old tables */
219
+ if ((ploci->ctype1_refcount) &&
220
+ (InterlockedDecrement(ploci->ctype1_refcount) == 0))
221
+ {
222
+ _ASSERT(0);
223
+ _free_crt(ploci->ctype1 - _COFFSET);
224
+ _free_crt((char *)(ploci->pclmap - _COFFSET - 1));
225
+ _free_crt((char *)(ploci->pcumap - _COFFSET - 1));
226
+ _free_crt(ploci->ctype1_refcount);
227
+ }
228
+ (*refcount) = 1;
229
+ ploci->ctype1_refcount = refcount;
230
+ /* set pointers to point to entry 0 of tables */
231
+ ploci->_public._locale_pctype = newctype1 + 1 + _COFFSET;
232
+ ploci->ctype1 = newctype1 + _COFFSET;
233
+ ploci->pclmap = newclmap + 1 + _COFFSET;
234
+ ploci->pcumap = newcumap + 1 + _COFFSET;
235
+ ploci->_public._locale_mb_cur_max = mb_cur_max;
236
+
237
+ /* cleanup and return success */
238
+ _free_crt (cbuffer);
239
+ return 0;
240
+
241
+ error_cleanup:
242
+ _free_crt (refcount);
243
+ _free_crt (newctype1);
244
+ _free_crt (newclmap);
245
+ _free_crt (newcumap);
246
+ _free_crt (cbuffer);
247
+ return 1;
248
+
249
+ } else {
250
+
251
+ if ( (ploci->ctype1_refcount != nullptr)&&
252
+ (InterlockedDecrement(ploci->ctype1_refcount) == 0))
253
+ {
254
+ _ASSERTE(ploci->ctype1_refcount > 0);
255
+ }
256
+ ploci->ctype1_refcount = nullptr;
257
+ ploci->ctype1 = nullptr;
258
+ ploci->_public._locale_pctype = __newctype + 1 + _COFFSET;
259
+ ploci->pclmap = __newclmap + 1 + _COFFSET;
260
+ ploci->pcumap = __newcumap + 1 + _COFFSET;
261
+ ploci->_public._locale_mb_cur_max = 1;
262
+
263
+ return 0;
264
+ }
265
+ }
266
+
267
+ #endif /* _UCRT_ENCLAVE_BUILD */
268
+
269
+ /* Define a number of functions which exist so, under _STATIC_CPPLIB, the
270
+ * static multithread C++ Library libcpmt.lib can access data found in the
271
+ * main CRT DLL without using __declspec(dllimport).
272
+ */
273
+
274
+ int __cdecl ___mb_cur_max_func()
275
+ {
276
+ /*
277
+ * Note that we don't need _LocaleUpdate in this function.
278
+ * The main reason being, that this is a leaf function in
279
+ * locale usage terms.
280
+ */
281
+ __acrt_ptd* const ptd = __acrt_getptd();
282
+ __crt_locale_data* ptloci = ptd->_locale_info;
283
+
284
+ __acrt_update_locale_info(ptd, &ptloci);
285
+
286
+ return ptloci->_public._locale_mb_cur_max;
287
+ }
288
+
289
+ int __cdecl ___mb_cur_max_l_func(_locale_t locale)
290
+ {
291
+ return locale == nullptr
292
+ ? ___mb_cur_max_func()
293
+ : locale->locinfo->_public._locale_mb_cur_max;
294
+ }
295
+
296
+ unsigned int __cdecl ___lc_codepage_func()
297
+ {
298
+ /*
299
+ * Note that we don't need _LocaleUpdate in this function.
300
+ * The main reason being, that this is a leaf function in
301
+ * locale usage terms.
302
+ */
303
+ __acrt_ptd* const ptd = __acrt_getptd();
304
+ __crt_locale_data* ptloci = ptd->_locale_info;
305
+
306
+ __acrt_update_locale_info(ptd, &ptloci);
307
+
308
+ return ptloci->_public._locale_lc_codepage;
309
+ }
310
+
311
+ unsigned int __cdecl ___lc_collate_cp_func()
312
+ {
313
+ /*
314
+ * Note that we don't need _LocaleUpdate in this function.
315
+ * The main reason being, that this is a leaf function in
316
+ * locale usage terms.
317
+ */
318
+ __acrt_ptd* const ptd = __acrt_getptd();
319
+ __crt_locale_data* ptloci = ptd->_locale_info;
320
+
321
+ __acrt_update_locale_info(ptd, &ptloci);
322
+
323
+ return ptloci->lc_collate_cp;
324
+ }
325
+
326
+ wchar_t** __cdecl ___lc_locale_name_func()
327
+ {
328
+ /*
329
+ * Note that we don't need _LocaleUpdate in this function.
330
+ * The main reason being, that this is a leaf function in
331
+ * locale usage terms.
332
+ */
333
+ __acrt_ptd* const ptd = __acrt_getptd();
334
+ __crt_locale_data* ptloci = ptd->_locale_info;
335
+
336
+ __acrt_update_locale_info(ptd, &ptloci);
337
+
338
+ return ptloci->locale_name;
339
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initmon.cpp ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *initmon.c - contains __acrt_locale_initialize_monetary
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the locale-category initialization function: __acrt_locale_initialize_monetary().
8
+ *
9
+ * Each initialization function sets up locale-specific information
10
+ * for their category, for use by functions which are affected by
11
+ * their locale category.
12
+ *
13
+ * *** For internal use by setlocale() only ***
14
+ *
15
+ *******************************************************************************/
16
+
17
+ #include <corecrt_internal.h>
18
+ #include <locale.h>
19
+
20
+ extern "C" {
21
+
22
+
23
+ // Enclaves have no ability to create new locales.
24
+ #ifndef _UCRT_ENCLAVE_BUILD
25
+
26
+ static void fix_grouping(_Inout_z_ char *);
27
+
28
+ /*
29
+ * Note that __acrt_lconv_c is used when the monetary category is in the C locale
30
+ * but the numeric category may not necessarily be in the C locale.
31
+ */
32
+
33
+
34
+ /***
35
+ *int __acrt_locale_initialize_monetary() - initialization for LC_MONETARY locale category.
36
+ *
37
+ *Purpose:
38
+ * In non-C locales, read the localized monetary strings into
39
+ * __acrt_lconv_intl, and also copy the numeric strings from __acrt_lconv into
40
+ * __acrt_lconv_intl. Set __acrt_lconv to point to __acrt_lconv_intl. The old
41
+ * __acrt_lconv_intl is not freed until the new one is fully established.
42
+ *
43
+ * In the C locale, the monetary fields in lconv are filled with
44
+ * contain C locale values. Any allocated __acrt_lconv_intl fields are freed.
45
+ *
46
+ * At startup, __acrt_lconv points to a static lconv structure containing
47
+ * C locale strings. This structure is never used again if
48
+ * __acrt_locale_initialize_monetary is called.
49
+ *
50
+ *Entry:
51
+ * None.
52
+ *
53
+ *Exit:
54
+ * 0 success
55
+ * 1 fail
56
+ *
57
+ *Exceptions:
58
+ *
59
+ *******************************************************************************/
60
+
61
+ int __cdecl __acrt_locale_initialize_monetary (
62
+ __crt_locale_data* ploci
63
+ )
64
+ {
65
+ struct lconv *lc;
66
+ int ret;
67
+ wchar_t* ctrylocalename;
68
+ long *lc_refcount;
69
+ long *lconv_mon_refcount = nullptr;
70
+ __crt_locale_pointers locinfo;
71
+
72
+ locinfo.locinfo = ploci;
73
+ locinfo.mbcinfo = 0;
74
+
75
+ if ( (ploci->locale_name[LC_MONETARY] != nullptr) ||
76
+ (ploci->locale_name[LC_NUMERIC] != nullptr) )
77
+ {
78
+ /*
79
+ * Allocate structure filled with nullptr pointers
80
+ */
81
+ if ((lc = _calloc_crt_t(lconv, 1).detach()) == nullptr)
82
+ return 1;
83
+
84
+ /*
85
+ * Allocate a new reference counter for the lconv structure
86
+ */
87
+ if ( (lc_refcount = _calloc_crt_t(long, 1).detach()) == nullptr )
88
+ {
89
+ _free_crt(lc);
90
+ return 1;
91
+ }
92
+
93
+ if ( ploci->locale_name[LC_MONETARY] != nullptr )
94
+ {
95
+ /*
96
+ * Allocate a new reference counter for the numeric info
97
+ */
98
+ if ( (lconv_mon_refcount = _calloc_crt_t(long, 1).detach()) == nullptr )
99
+ {
100
+ _free_crt(lc);
101
+ _free_crt(lc_refcount);
102
+ return 1;
103
+ }
104
+
105
+ /*
106
+ * Currency is country--not language--dependent. NT
107
+ * work-around.
108
+ */
109
+ ctrylocalename = ploci->locale_name[LC_MONETARY];
110
+
111
+ ret = 0;
112
+
113
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
114
+ LOCALE_SINTLSYMBOL, (void *)&lc->int_curr_symbol );
115
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
116
+ LOCALE_SCURRENCY, (void *)&lc->currency_symbol );
117
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
118
+ LOCALE_SMONDECIMALSEP, (void *)&lc->mon_decimal_point );
119
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
120
+ LOCALE_SMONTHOUSANDSEP, (void *)&lc->mon_thousands_sep );
121
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
122
+ LOCALE_SMONGROUPING, (void *)&lc->mon_grouping );
123
+
124
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
125
+ LOCALE_SPOSITIVESIGN, (void *)&lc->positive_sign);
126
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename,
127
+ LOCALE_SNEGATIVESIGN, (void *)&lc->negative_sign);
128
+
129
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
130
+ LOCALE_IINTLCURRDIGITS, (void *)&lc->int_frac_digits);
131
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
132
+ LOCALE_ICURRDIGITS, (void *)&lc->frac_digits);
133
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
134
+ LOCALE_IPOSSYMPRECEDES, (void *)&lc->p_cs_precedes);
135
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
136
+ LOCALE_IPOSSEPBYSPACE, (void *)&lc->p_sep_by_space);
137
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
138
+ LOCALE_INEGSYMPRECEDES, (void *)&lc->n_cs_precedes);
139
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
140
+ LOCALE_INEGSEPBYSPACE, (void *)&lc->n_sep_by_space);
141
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
142
+ LOCALE_IPOSSIGNPOSN, (void *)&lc->p_sign_posn);
143
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, ctrylocalename,
144
+ LOCALE_INEGSIGNPOSN, (void *)&lc->n_sign_posn);
145
+
146
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
147
+ LOCALE_SINTLSYMBOL, (void *)&lc->_W_int_curr_symbol );
148
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
149
+ LOCALE_SCURRENCY, (void *)&lc->_W_currency_symbol );
150
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
151
+ LOCALE_SMONDECIMALSEP, (void *)&lc->_W_mon_decimal_point );
152
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
153
+ LOCALE_SMONTHOUSANDSEP, (void *)&lc->_W_mon_thousands_sep );
154
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
155
+ LOCALE_SPOSITIVESIGN, (void *)&lc->_W_positive_sign);
156
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename,
157
+ LOCALE_SNEGATIVESIGN, (void *)&lc->_W_negative_sign);
158
+
159
+ if ( ret != 0 ) {
160
+ __acrt_locale_free_monetary(lc);
161
+ _free_crt(lc);
162
+ _free_crt(lc_refcount);
163
+ _free_crt(lconv_mon_refcount);
164
+ return 1;
165
+ }
166
+
167
+ fix_grouping(lc->mon_grouping);
168
+ }
169
+ else {
170
+ /*
171
+ * C locale for monetary category (the numeric category fields,
172
+ * which are NOT of the C locale, get fixed up below). Note
173
+ * that __acrt_lconv_c is copied, rather than directly assigning
174
+ * the fields of lc because of the uncertainty of the values of
175
+ * the int_frac_digits,..., n_sign_posn fields (SCHAR_MAX or
176
+ * UCHAR_MAX, depending on whether or a compliand was built
177
+ * with -J.
178
+ */
179
+ *lc = __acrt_lconv_c;
180
+ }
181
+
182
+ /*
183
+ * Copy the numeric locale fields from the old struct
184
+ */
185
+ lc->decimal_point = ploci->lconv->decimal_point;
186
+ lc->thousands_sep = ploci->lconv->thousands_sep;
187
+ lc->grouping = ploci->lconv->grouping;
188
+ lc->_W_decimal_point = ploci->lconv->_W_decimal_point;
189
+ lc->_W_thousands_sep = ploci->lconv->_W_thousands_sep;
190
+
191
+ *lc_refcount = 1;
192
+ if (lconv_mon_refcount)
193
+ *lconv_mon_refcount = 1;
194
+ }
195
+ else {
196
+ /*
197
+ * C locale for BOTH monetary and numeric categories.
198
+ */
199
+ lconv_mon_refcount = nullptr;
200
+ lc_refcount = nullptr;
201
+ lc = &__acrt_lconv_c; /* point to new one */
202
+
203
+ }
204
+
205
+ if ( (ploci->lconv_mon_refcount != nullptr) &&
206
+ (InterlockedDecrement(ploci->lconv_mon_refcount) == 0))
207
+ {
208
+ _ASSERTE(ploci->lconv_mon_refcount > 0);
209
+ }
210
+ if ( (ploci->lconv_intl_refcount != nullptr) &&
211
+ (InterlockedDecrement(ploci->lconv_intl_refcount) == 0))
212
+ {
213
+ _free_crt(ploci->lconv);
214
+ _free_crt(ploci->lconv_intl_refcount);
215
+ }
216
+ ploci->lconv_mon_refcount = lconv_mon_refcount;
217
+ ploci->lconv_intl_refcount = lc_refcount;
218
+ ploci->lconv = lc; /* point to new one */
219
+
220
+ return 0;
221
+ }
222
+
223
+ static void fix_grouping(
224
+ char *grouping
225
+ )
226
+ {
227
+ /*
228
+ * ANSI specifies that the fields should contain "\3" [\3\0] to indicate
229
+ * thousands groupings (100,000,000.00 for example).
230
+ * NT uses "3;0"; ASCII 3 instead of value 3 and the ';' is extra.
231
+ * So here we convert the NT version to the ANSI version.
232
+ */
233
+
234
+ while (*grouping)
235
+ {
236
+ /* convert '3' to '\3' */
237
+ if (*grouping >= '0' && *grouping <= '9')
238
+ {
239
+ *grouping = *grouping - '0';
240
+ grouping++;
241
+ }
242
+
243
+ /* remove ';' */
244
+ else if (*grouping == ';')
245
+ {
246
+ char *tmp = grouping;
247
+
248
+ do
249
+ *tmp = *(tmp+1);
250
+ while (*++tmp);
251
+ }
252
+
253
+ /* unknown (illegal) character, ignore */
254
+ else
255
+ grouping++;
256
+ }
257
+ }
258
+
259
+ #endif /* _UCRT_ENCLAVE_BUILD */
260
+
261
+ /*
262
+ * Free the lconv monetary strings.
263
+ * Numeric values do not need to be freed.
264
+ */
265
+ void __cdecl __acrt_locale_free_monetary(lconv* l)
266
+ {
267
+ if (l == nullptr)
268
+ return;
269
+
270
+ if ( l->int_curr_symbol != __acrt_lconv_c.int_curr_symbol )
271
+ _free_crt(l->int_curr_symbol);
272
+
273
+ if ( l->currency_symbol != __acrt_lconv_c.currency_symbol )
274
+ _free_crt(l->currency_symbol);
275
+
276
+ if ( l->mon_decimal_point != __acrt_lconv_c.mon_decimal_point )
277
+ _free_crt(l->mon_decimal_point);
278
+
279
+ if ( l->mon_thousands_sep != __acrt_lconv_c.mon_thousands_sep )
280
+ _free_crt(l->mon_thousands_sep);
281
+
282
+ if ( l->mon_grouping != __acrt_lconv_c.mon_grouping )
283
+ _free_crt(l->mon_grouping);
284
+
285
+ if ( l->positive_sign != __acrt_lconv_c.positive_sign )
286
+ _free_crt(l->positive_sign);
287
+
288
+ if ( l->negative_sign != __acrt_lconv_c.negative_sign )
289
+ _free_crt(l->negative_sign);
290
+
291
+ if ( l->_W_int_curr_symbol != __acrt_lconv_c._W_int_curr_symbol )
292
+ _free_crt(l->_W_int_curr_symbol);
293
+
294
+ if ( l->_W_currency_symbol != __acrt_lconv_c._W_currency_symbol )
295
+ _free_crt(l->_W_currency_symbol);
296
+
297
+ if ( l->_W_mon_decimal_point != __acrt_lconv_c._W_mon_decimal_point )
298
+ _free_crt(l->_W_mon_decimal_point);
299
+
300
+ if ( l->_W_mon_thousands_sep != __acrt_lconv_c._W_mon_thousands_sep )
301
+ _free_crt(l->_W_mon_thousands_sep);
302
+
303
+ if ( l->_W_positive_sign != __acrt_lconv_c._W_positive_sign )
304
+ _free_crt(l->_W_positive_sign);
305
+
306
+ if ( l->_W_negative_sign != __acrt_lconv_c._W_negative_sign )
307
+ _free_crt(l->_W_negative_sign);
308
+ }
309
+
310
+
311
+
312
+ } // extern "C"
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/initnum.cpp ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *initnum.c - contains __acrt_locale_initialize_numeric
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the locale-category initialization function: __acrt_locale_initialize_numeric().
8
+ *
9
+ * Each initialization function sets up locale-specific information
10
+ * for their category, for use by functions which are affected by
11
+ * their locale category.
12
+ *
13
+ * *** For internal use by setlocale() only ***
14
+ *
15
+ *******************************************************************************/
16
+ #include <corecrt_internal.h>
17
+ #include <locale.h>
18
+
19
+ extern "C" {
20
+
21
+ // Enclaves have no ability to create new locales.
22
+ #ifndef _UCRT_ENCLAVE_BUILD
23
+
24
+ static void fix_grouping(
25
+ _Inout_z_ char * grouping
26
+ )
27
+ {
28
+ /*
29
+ * ANSI specifies that the fields should contain "\3" [\3\0] to indicate
30
+ * thousands groupings (100,000,000.00 for example).
31
+ * NT uses "3;0"; ASCII 3 instead of value 3 and the ';' is extra.
32
+ * So here we convert the NT version to the ANSI version.
33
+ */
34
+
35
+ while (*grouping)
36
+ {
37
+ /* convert '3' to '\3' */
38
+ if (*grouping >= '0' && *grouping <= '9')
39
+ {
40
+ *grouping = *grouping - '0';
41
+ grouping++;
42
+ }
43
+
44
+ /* remove ';' */
45
+ else if (*grouping == ';')
46
+ {
47
+ char *tmp = grouping;
48
+
49
+ do
50
+ *tmp = *(tmp+1);
51
+ while (*++tmp);
52
+ }
53
+
54
+ /* unknown (illegal) character, ignore */
55
+ else
56
+ grouping++;
57
+ }
58
+ }
59
+
60
+ /***
61
+ *int __acrt_locale_initialize_numeric() - initialization for LC_NUMERIC locale category.
62
+ *
63
+ *Purpose:
64
+ *
65
+ *Entry:
66
+ * None.
67
+ *
68
+ *Exit:
69
+ * 0 success
70
+ * 1 fail
71
+ *
72
+ *Exceptions:
73
+ *
74
+ *******************************************************************************/
75
+
76
+ int __cdecl __acrt_locale_initialize_numeric (
77
+ __crt_locale_data* ploci
78
+ )
79
+ {
80
+ struct lconv *lc;
81
+ int ret = 0;
82
+ wchar_t* ctrylocalename;
83
+ long *lc_refcount;
84
+ long *lconv_num_refcount = nullptr;
85
+ __crt_locale_pointers locinfo;
86
+
87
+ locinfo.locinfo = ploci;
88
+ locinfo.mbcinfo = 0;
89
+
90
+ if ( (ploci->locale_name[LC_NUMERIC] != nullptr) ||
91
+ (ploci->locale_name[LC_MONETARY] != nullptr) )
92
+ {
93
+ /*
94
+ * Allocate structure filled with nullptr pointers
95
+ */
96
+ if ( (lc = (struct lconv *)_calloc_crt(1, sizeof(struct lconv)))
97
+ == nullptr )
98
+ return 1;
99
+
100
+ /*
101
+ * Copy over all fields (esp., the monetary category)
102
+ */
103
+ *lc = *ploci->lconv;
104
+
105
+ /*
106
+ * Allocate a new reference counter for the lconv structure
107
+ */
108
+ if ( (lc_refcount = _malloc_crt_t(long, 1).detach()) == nullptr )
109
+ {
110
+ _free_crt(lc);
111
+ return 1;
112
+ }
113
+ *lc_refcount = 0;
114
+
115
+ if ( ploci->locale_name[LC_NUMERIC] != nullptr )
116
+ {
117
+ /*
118
+ * Allocate a new reference counter for the numeric info
119
+ */
120
+ if ( (lconv_num_refcount = _malloc_crt_t(long, 1).detach()) == nullptr )
121
+ {
122
+ _free_crt(lc);
123
+ _free_crt(lc_refcount);
124
+ return 1;
125
+ }
126
+ *lconv_num_refcount = 0;
127
+
128
+ /*
129
+ * Numeric data is country--not language--dependent.
130
+ */
131
+ ctrylocalename = ploci->locale_name[LC_NUMERIC];
132
+
133
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename, LOCALE_SDECIMAL,
134
+ (void *)&lc->decimal_point);
135
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename, LOCALE_STHOUSAND,
136
+ (void *)&lc->thousands_sep);
137
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, ctrylocalename, LOCALE_SGROUPING,
138
+ (void *)&lc->grouping);
139
+
140
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename, LOCALE_SDECIMAL,
141
+ (void *)&lc->_W_decimal_point);
142
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, ctrylocalename, LOCALE_STHOUSAND,
143
+ (void *)&lc->_W_thousands_sep);
144
+
145
+ if (ret) {
146
+ /* Clean up before returning failure */
147
+ __acrt_locale_free_numeric(lc);
148
+ _free_crt(lc);
149
+ _free_crt(lconv_num_refcount);
150
+ _free_crt(lc_refcount);
151
+ return -1;
152
+ }
153
+
154
+ fix_grouping(lc->grouping);
155
+ }
156
+ else {
157
+ /*
158
+ * C locale for just the numeric category.
159
+ */
160
+ /*
161
+ * nullptr out the reference count pointer
162
+ */
163
+ lconv_num_refcount = nullptr;
164
+ lc->decimal_point = __acrt_lconv_c.decimal_point;
165
+ lc->thousands_sep = __acrt_lconv_c.thousands_sep;
166
+ lc->grouping = __acrt_lconv_c.grouping;
167
+ lc->_W_decimal_point = __acrt_lconv_c._W_decimal_point;
168
+ lc->_W_thousands_sep = __acrt_lconv_c._W_thousands_sep;
169
+ }
170
+ (*lc_refcount) = 1;
171
+ if (lconv_num_refcount)
172
+ (*lconv_num_refcount) = 1;
173
+ }
174
+
175
+ else {
176
+ /*
177
+ * C locale for BOTH numeric and monetary categories.
178
+ */
179
+ lconv_num_refcount = nullptr;
180
+ lc_refcount = nullptr;
181
+ lc = &__acrt_lconv_c; /* point to new one */
182
+ }
183
+ /*
184
+ * If this is part of LC_ALL, then we need to free the old ploci->lconv
185
+ * set up in init_monetary() before this.
186
+ */
187
+ if ( (ploci->lconv_num_refcount != nullptr) &&
188
+ (InterlockedDecrement(ploci->lconv_num_refcount) == 0))
189
+ {
190
+ _ASSERTE(ploci->lconv_num_refcount > 0);
191
+ }
192
+ if ( (ploci->lconv_intl_refcount != nullptr) &&
193
+ (InterlockedDecrement(ploci->lconv_intl_refcount) == 0))
194
+ {
195
+ _free_crt(ploci->lconv_intl_refcount);
196
+ _free_crt(ploci->lconv);
197
+ }
198
+
199
+ ploci->lconv_num_refcount = lconv_num_refcount;
200
+ ploci->lconv_intl_refcount = lc_refcount;
201
+
202
+ ploci->lconv = lc;
203
+ return 0;
204
+ }
205
+
206
+ #endif /* _UCRT_ENCLAVE_BUILD */
207
+
208
+ /*
209
+ * Free the lconv numeric strings.
210
+ * Numeric values do not need to be freed.
211
+ */
212
+ void __cdecl __acrt_locale_free_numeric(lconv* l)
213
+ {
214
+ if (l == nullptr)
215
+ return;
216
+
217
+ if ( l->decimal_point != __acrt_lconv_c.decimal_point )
218
+ _free_crt(l->decimal_point);
219
+
220
+ if ( l->thousands_sep != __acrt_lconv_c.thousands_sep )
221
+ _free_crt(l->thousands_sep);
222
+
223
+ if ( l->grouping != __acrt_lconv_c.grouping )
224
+ _free_crt(l->grouping);
225
+
226
+ if ( l->_W_decimal_point != __acrt_lconv_c._W_decimal_point )
227
+ _free_crt(l->_W_decimal_point);
228
+
229
+ if ( l->_W_thousands_sep != __acrt_lconv_c._W_thousands_sep )
230
+ _free_crt(l->_W_thousands_sep);
231
+ }
232
+
233
+
234
+
235
+ } // extern "C"
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/inittime.cpp ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // inittime.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Functionality for initializing and destroying LC_TIME category data structures
7
+ //
8
+ #include <corecrt_internal.h>
9
+ #include <locale.h>
10
+ #include <stdlib.h>
11
+
12
+
13
+ // Enclaves have no ability to create new locales.
14
+ #ifndef _UCRT_ENCLAVE_BUILD
15
+
16
+ _Success_(return != false)
17
+ static bool __cdecl initialize_lc_time(
18
+ _Inout_ __crt_lc_time_data* const lc_time,
19
+ _In_ __crt_locale_data* const locale_data
20
+ ) throw()
21
+ {
22
+ wchar_t const* const locale_name = locale_data->locale_name[LC_TIME];
23
+
24
+ __crt_locale_pointers locinfo = { locale_data, nullptr };
25
+
26
+ lc_time->_W_ww_locale_name = __acrt_copy_locale_name(locale_name);
27
+
28
+ int ret = 0;
29
+
30
+ // The days of the week. Note that the OS days are numbered 1-7, starting
31
+ // with Monday, whereas the lc_time days are numbered (indexed, really) 0-6
32
+ // starting with Sunday (thus the adjustment to the index in the loop).
33
+ for (unsigned int i = 0; i != 7; ++i)
34
+ {
35
+ unsigned int const result_index = (i + 1) % 7;
36
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SABBREVDAYNAME1 + i, &lc_time->wday_abbr[result_index]);
37
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SDAYNAME1 + i, &lc_time->wday [result_index]);
38
+
39
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SABBREVDAYNAME1 + i, &lc_time->_W_wday_abbr[result_index]);
40
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SDAYNAME1 + i, &lc_time->_W_wday [result_index]);
41
+ }
42
+
43
+ // The months of the year
44
+ for (unsigned int i = 0; i != 12; ++i)
45
+ {
46
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SABBREVMONTHNAME1 + i, &lc_time->month_abbr[i]);
47
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SMONTHNAME1 + i, &lc_time->month [i]);
48
+
49
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SABBREVMONTHNAME1 + i, &lc_time->_W_month_abbr[i]);
50
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SMONTHNAME1 + i, &lc_time->_W_month [i]);
51
+ }
52
+
53
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_S1159, &lc_time->ampm[0]);
54
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_S2359, &lc_time->ampm[1]);
55
+
56
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_S1159, &lc_time->_W_ampm[0]);
57
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_S2359, &lc_time->_W_ampm[1]);
58
+
59
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SSHORTDATE, &lc_time->ww_sdatefmt);
60
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_SLONGDATE, &lc_time->ww_ldatefmt);
61
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_STR_TYPE, locale_name, LOCALE_STIMEFORMAT, &lc_time->ww_timefmt);
62
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_INT_TYPE, locale_name, LOCALE_ICALENDARTYPE, &lc_time->ww_caltype);
63
+
64
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SSHORTDATE, &lc_time->_W_ww_sdatefmt);
65
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_SLONGDATE, &lc_time->_W_ww_ldatefmt);
66
+ ret |= __acrt_GetLocaleInfoA(&locinfo, LC_WSTR_TYPE, locale_name, LOCALE_STIMEFORMAT, &lc_time->_W_ww_timefmt);
67
+
68
+ return ret == 0;
69
+ }
70
+
71
+
72
+
73
+ _Success_(return != nullptr)
74
+ static __crt_lc_time_data const* __cdecl get_or_create_lc_time(
75
+ _In_ __crt_locale_data* const locale_data
76
+ ) throw()
77
+ {
78
+ // C Locale
79
+ if (!locale_data->locale_name[LC_TIME])
80
+ {
81
+ return &__lc_time_c;
82
+ }
83
+
84
+ // Non-C Locale
85
+ __crt_unique_heap_ptr<__crt_lc_time_data> lc_time = _calloc_crt_t(__crt_lc_time_data, 1);
86
+ if (!lc_time)
87
+ {
88
+ return nullptr;
89
+ }
90
+
91
+ if (!initialize_lc_time(lc_time.get(), locale_data))
92
+ {
93
+ __acrt_locale_free_time(lc_time.get());
94
+ return nullptr;
95
+ }
96
+
97
+ lc_time.get()->refcount = 1;
98
+
99
+ return lc_time.detach();
100
+ }
101
+
102
+
103
+
104
+ extern "C" int __cdecl __acrt_locale_initialize_time(
105
+ __crt_locale_data* const locale_data
106
+ )
107
+ {
108
+ __crt_lc_time_data const* const lc_time = get_or_create_lc_time(locale_data);
109
+ if (!lc_time)
110
+ {
111
+ return 1;
112
+ }
113
+
114
+ if (__acrt_locale_release_lc_time_reference(locale_data->lc_time_curr) == 0)
115
+ {
116
+ _ASSERTE(("lc_time_curr unexpectedly has no remaining references", 0));
117
+ }
118
+
119
+ locale_data->lc_time_curr = lc_time;
120
+ return 0;
121
+ }
122
+
123
+ #endif /* _UCRT_ENCLAVE_BUILD */
124
+
125
+
126
+ static void __cdecl free_crt_array_internal(
127
+ _Inout_updates_(array_count) void const** const array,
128
+ _In_ size_t const array_count
129
+ ) throw()
130
+ {
131
+ for (void const* const* it = array; it != array + array_count; ++it)
132
+ {
133
+ _free_crt(const_cast<void*>(*it));
134
+ }
135
+ }
136
+
137
+
138
+
139
+ template <typename T, size_t N>
140
+ static void __cdecl free_crt_array(_Inout_ T* (&array)[N]) throw()
141
+ {
142
+ return free_crt_array_internal(const_cast<void const**>(reinterpret_cast<void const* const*>(array)), N);
143
+ }
144
+
145
+
146
+
147
+ extern "C" void __cdecl __acrt_locale_free_time(
148
+ __crt_lc_time_data* const lc_time
149
+ )
150
+ {
151
+ if (!lc_time)
152
+ {
153
+ return;
154
+ }
155
+
156
+ free_crt_array(lc_time->wday_abbr);
157
+ free_crt_array(lc_time->wday);
158
+ free_crt_array(lc_time->month_abbr);
159
+ free_crt_array(lc_time->month);
160
+ free_crt_array(lc_time->ampm);
161
+
162
+ _free_crt(lc_time->ww_sdatefmt);
163
+ _free_crt(lc_time->ww_ldatefmt);
164
+ _free_crt(lc_time->ww_timefmt);
165
+
166
+ free_crt_array(lc_time->_W_wday_abbr);
167
+ free_crt_array(lc_time->_W_wday);
168
+ free_crt_array(lc_time->_W_month_abbr);
169
+ free_crt_array(lc_time->_W_month);
170
+ free_crt_array(lc_time->_W_ampm);
171
+
172
+ _free_crt(lc_time->_W_ww_sdatefmt);
173
+ _free_crt(lc_time->_W_ww_ldatefmt);
174
+ _free_crt(lc_time->_W_ww_timefmt);
175
+
176
+ _free_crt(lc_time->_W_ww_locale_name);
177
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/lcidtoname_downlevel.cpp ADDED
@@ -0,0 +1,604 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // lcidtoname_downlevel.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Definitions of wrappers for the Windows XP API functions that are not available on
7
+ // all supported operating system versions.
8
+ //
9
+
10
+ #include <corecrt_internal.h>
11
+
12
+ namespace {
13
+
14
+ // Map of LCID to locale name.
15
+ struct LcidToLocaleName
16
+ {
17
+ LCID lcid;
18
+ wchar_t* localeName;
19
+ };
20
+
21
+ // Map of locale name to an index.
22
+ struct LocaleNameIndex
23
+ {
24
+ wchar_t* name;
25
+ int index;
26
+ };
27
+
28
+ // Map of LCID to locale name for Windows XP.
29
+ // Data in this table has been obtained from National Language Support (NLS) API Reference at
30
+ // http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx
31
+ // The table is sorted to improve search performance.
32
+ static const LcidToLocaleName LcidToLocaleNameTable[] =
33
+ {
34
+ { 0x0001, L"ar" },
35
+ { 0x0002, L"bg" },
36
+ { 0x0003, L"ca" },
37
+ { 0x0004, L"zh-CHS" },
38
+ { 0x0005, L"cs" },
39
+ { 0x0006, L"da" },
40
+ { 0x0007, L"de" },
41
+ { 0x0008, L"el" },
42
+ { 0x0009, L"en" },
43
+ { 0x000A, L"es" },
44
+ { 0x000B, L"fi" },
45
+ { 0x000C, L"fr" },
46
+ { 0x000D, L"he" },
47
+ { 0x000E, L"hu" },
48
+ { 0x000F, L"is" },
49
+ { 0x0010, L"it" },
50
+ { 0x0011, L"ja" },
51
+ { 0x0012, L"ko" },
52
+ { 0x0013, L"nl" },
53
+ { 0x0014, L"no" },
54
+ { 0x0015, L"pl" },
55
+ { 0x0016, L"pt" },
56
+ { 0x0018, L"ro" },
57
+ { 0x0019, L"ru" },
58
+ { 0x001A, L"hr" },
59
+ { 0x001B, L"sk" },
60
+ { 0x001C, L"sq" },
61
+ { 0x001D, L"sv" },
62
+ { 0x001E, L"th" },
63
+ { 0x001F, L"tr" },
64
+ { 0x0020, L"ur" },
65
+ { 0x0021, L"id" },
66
+ { 0x0022, L"uk" },
67
+ { 0x0023, L"be" },
68
+ { 0x0024, L"sl" },
69
+ { 0x0025, L"et" },
70
+ { 0x0026, L"lv" },
71
+ { 0x0027, L"lt" },
72
+ { 0x0029, L"fa" },
73
+ { 0x002A, L"vi" },
74
+ { 0x002B, L"hy" },
75
+ { 0x002C, L"az" },
76
+ { 0x002D, L"eu" },
77
+ { 0x002F, L"mk" },
78
+ { 0x0036, L"af" },
79
+ { 0x0037, L"ka" },
80
+ { 0x0038, L"fo" },
81
+ { 0x0039, L"hi" },
82
+ { 0x003E, L"ms" },
83
+ { 0x003F, L"kk" },
84
+ { 0x0040, L"ky" },
85
+ { 0x0041, L"sw" },
86
+ { 0x0043, L"uz" },
87
+ { 0x0044, L"tt" },
88
+ { 0x0046, L"pa" },
89
+ { 0x0047, L"gu" },
90
+ { 0x0049, L"ta" },
91
+ { 0x004A, L"te" },
92
+ { 0x004B, L"kn" },
93
+ { 0x004E, L"mr" },
94
+ { 0x004F, L"sa" },
95
+ { 0x0050, L"mn" },
96
+ { 0x0056, L"gl" },
97
+ { 0x0057, L"kok" },
98
+ { 0x005A, L"syr" },
99
+ { 0x0065, L"div" },
100
+ { 0x007f, L"" },
101
+ { 0x0401, L"ar-SA" },
102
+ { 0x0402, L"bg-BG" },
103
+ { 0x0403, L"ca-ES" },
104
+ { 0x0404, L"zh-TW" },
105
+ { 0x0405, L"cs-CZ" },
106
+ { 0x0406, L"da-DK" },
107
+ { 0x0407, L"de-DE" },
108
+ { 0x0408, L"el-GR" },
109
+ { 0x0409, L"en-US" },
110
+ { 0x040B, L"fi-FI" },
111
+ { 0x040C, L"fr-FR" },
112
+ { 0x040D, L"he-IL" },
113
+ { 0x040E, L"hu-HU" },
114
+ { 0x040F, L"is-IS" },
115
+ { 0x0410, L"it-IT" },
116
+ { 0x0411, L"ja-JP" },
117
+ { 0x0412, L"ko-KR" },
118
+ { 0x0413, L"nl-NL" },
119
+ { 0x0414, L"nb-NO" },
120
+ { 0x0415, L"pl-PL" },
121
+ { 0x0416, L"pt-BR" },
122
+ { 0x0418, L"ro-RO" },
123
+ { 0x0419, L"ru-RU" },
124
+ { 0x041A, L"hr-HR" },
125
+ { 0x041B, L"sk-SK" },
126
+ { 0x041C, L"sq-AL" },
127
+ { 0x041D, L"sv-SE" },
128
+ { 0x041E, L"th-TH" },
129
+ { 0x041F, L"tr-TR" },
130
+ { 0x0420, L"ur-PK" },
131
+ { 0x0421, L"id-ID" },
132
+ { 0x0422, L"uk-UA" },
133
+ { 0x0423, L"be-BY" },
134
+ { 0x0424, L"sl-SI" },
135
+ { 0x0425, L"et-EE" },
136
+ { 0x0426, L"lv-LV" },
137
+ { 0x0427, L"lt-LT" },
138
+ { 0x0429, L"fa-IR" },
139
+ { 0x042A, L"vi-VN" },
140
+ { 0x042B, L"hy-AM" },
141
+ { 0x042C, L"az-AZ-Latn" },
142
+ { 0x042D, L"eu-ES" },
143
+ { 0x042F, L"mk-MK" },
144
+ { 0x0432, L"tn-ZA" },
145
+ { 0x0434, L"xh-ZA" },
146
+ { 0x0435, L"zu-ZA" },
147
+ { 0x0436, L"af-ZA" },
148
+ { 0x0437, L"ka-GE" },
149
+ { 0x0438, L"fo-FO" },
150
+ { 0x0439, L"hi-IN" },
151
+ { 0x043A, L"mt-MT" },
152
+ { 0x043B, L"se-NO" },
153
+ { 0x043E, L"ms-MY" },
154
+ { 0x043F, L"kk-KZ" },
155
+ { 0x0440, L"ky-KG" },
156
+ { 0x0441, L"sw-KE" },
157
+ { 0x0443, L"uz-UZ-Latn" },
158
+ { 0x0444, L"tt-RU" },
159
+ { 0x0445, L"bn-IN" },
160
+ { 0x0446, L"pa-IN" },
161
+ { 0x0447, L"gu-IN" },
162
+ { 0x0449, L"ta-IN" },
163
+ { 0x044A, L"te-IN" },
164
+ { 0x044B, L"kn-IN" },
165
+ { 0x044C, L"ml-IN" },
166
+ { 0x044E, L"mr-IN" },
167
+ { 0x044F, L"sa-IN" },
168
+ { 0x0450, L"mn-MN" },
169
+ { 0x0452, L"cy-GB" },
170
+ { 0x0456, L"gl-ES" },
171
+ { 0x0457, L"kok-IN" },
172
+ { 0x045A, L"syr-SY" },
173
+ { 0x0465, L"div-MV" },
174
+ { 0x046B, L"quz-BO" },
175
+ { 0x046C, L"ns-ZA" },
176
+ { 0x0481, L"mi-NZ" },
177
+ { 0x0801, L"ar-IQ" },
178
+ { 0x0804, L"zh-CN" },
179
+ { 0x0807, L"de-CH" },
180
+ { 0x0809, L"en-GB" },
181
+ { 0x080A, L"es-MX" },
182
+ { 0x080C, L"fr-BE" },
183
+ { 0x0810, L"it-CH" },
184
+ { 0x0813, L"nl-BE" },
185
+ { 0x0814, L"nn-NO" },
186
+ { 0x0816, L"pt-PT" },
187
+ { 0x081A, L"sr-SP-Latn" },
188
+ { 0x081D, L"sv-FI" },
189
+ { 0x082C, L"az-AZ-Cyrl" },
190
+ { 0x083B, L"se-SE" },
191
+ { 0x083E, L"ms-BN" },
192
+ { 0x0843, L"uz-UZ-Cyrl" },
193
+ { 0x086B, L"quz-EC" },
194
+ { 0x0C01, L"ar-EG" },
195
+ { 0x0C04, L"zh-HK" },
196
+ { 0x0C07, L"de-AT" },
197
+ { 0x0C09, L"en-AU" },
198
+ { 0x0C0A, L"es-ES" },
199
+ { 0x0C0C, L"fr-CA" },
200
+ { 0x0C1A, L"sr-SP-Cyrl" },
201
+ { 0x0C3B, L"se-FI" },
202
+ { 0x0C6B, L"quz-PE" },
203
+ { 0x1001, L"ar-LY" },
204
+ { 0x1004, L"zh-SG" },
205
+ { 0x1007, L"de-LU" },
206
+ { 0x1009, L"en-CA" },
207
+ { 0x100A, L"es-GT" },
208
+ { 0x100C, L"fr-CH" },
209
+ { 0x101A, L"hr-BA" },
210
+ { 0x103B, L"smj-NO" },
211
+ { 0x1401, L"ar-DZ" },
212
+ { 0x1404, L"zh-MO" },
213
+ { 0x1407, L"de-LI" },
214
+ { 0x1409, L"en-NZ" },
215
+ { 0x140A, L"es-CR" },
216
+ { 0x140C, L"fr-LU" },
217
+ { 0x141A, L"bs-BA-Latn" },
218
+ { 0x143B, L"smj-SE" },
219
+ { 0x1801, L"ar-MA" },
220
+ { 0x1809, L"en-IE" },
221
+ { 0x180A, L"es-PA" },
222
+ { 0x180C, L"fr-MC" },
223
+ { 0x181A, L"sr-BA-Latn" },
224
+ { 0x183B, L"sma-NO" },
225
+ { 0x1C01, L"ar-TN" },
226
+ { 0x1C09, L"en-ZA" },
227
+ { 0x1C0A, L"es-DO" },
228
+ { 0x1C1A, L"sr-BA-Cyrl" },
229
+ { 0x1C3B, L"sma-SE" },
230
+ { 0x2001, L"ar-OM" },
231
+ { 0x2009, L"en-JM" },
232
+ { 0x200A, L"es-VE" },
233
+ { 0x203B, L"sms-FI" },
234
+ { 0x2401, L"ar-YE" },
235
+ { 0x2409, L"en-CB" },
236
+ { 0x240A, L"es-CO" },
237
+ { 0x243B, L"smn-FI" },
238
+ { 0x2801, L"ar-SY" },
239
+ { 0x2809, L"en-BZ" },
240
+ { 0x280A, L"es-PE" },
241
+ { 0x2C01, L"ar-JO" },
242
+ { 0x2C09, L"en-TT" },
243
+ { 0x2C0A, L"es-AR" },
244
+ { 0x3001, L"ar-LB" },
245
+ { 0x3009, L"en-ZW" },
246
+ { 0x300A, L"es-EC" },
247
+ { 0x3401, L"ar-KW" },
248
+ { 0x3409, L"en-PH" },
249
+ { 0x340A, L"es-CL" },
250
+ { 0x3801, L"ar-AE" },
251
+ { 0x380A, L"es-UY" },
252
+ { 0x3C01, L"ar-BH" },
253
+ { 0x3C0A, L"es-PY" },
254
+ { 0x4001, L"ar-QA" },
255
+ { 0x400A, L"es-BO" },
256
+ { 0x440A, L"es-SV" },
257
+ { 0x480A, L"es-HN" },
258
+ { 0x4C0A, L"es-NI" },
259
+ { 0x500A, L"es-PR" },
260
+ { 0x7C04, L"zh-CHT" },
261
+ { 0x7C1A, L"sr" }
262
+ };
263
+
264
+ // Map of locale name to an index in LcidToLocaleNameTable, for Windows XP.
265
+ // Data in this table has been obtained from National Language Support (NLS) API Reference at
266
+ // http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx
267
+ // The table is sorted to improve search performance.
268
+ static const LocaleNameIndex LocaleNameToIndexTable[] =
269
+ {
270
+ { L"" , 66 },
271
+ { L"af" , 44 },
272
+ { L"af-za" , 113 },
273
+ { L"ar" , 0 },
274
+ { L"ar-ae" , 216 },
275
+ { L"ar-bh" , 218 },
276
+ { L"ar-dz" , 177 },
277
+ { L"ar-eg" , 160 },
278
+ { L"ar-iq" , 143 },
279
+ { L"ar-jo" , 207 },
280
+ { L"ar-kw" , 213 },
281
+ { L"ar-lb" , 210 },
282
+ { L"ar-ly" , 169 },
283
+ { L"ar-ma" , 185 },
284
+ { L"ar-om" , 196 },
285
+ { L"ar-qa" , 220 },
286
+ { L"ar-sa" , 67 },
287
+ { L"ar-sy" , 204 },
288
+ { L"ar-tn" , 191 },
289
+ { L"ar-ye" , 200 },
290
+ { L"az" , 41 },
291
+ { L"az-az-cyrl" , 155 },
292
+ { L"az-az-latn" , 107 },
293
+ { L"be" , 33 },
294
+ { L"be-by" , 99 },
295
+ { L"bg" , 1 },
296
+ { L"bg-bg" , 68 },
297
+ { L"bn-in" , 125 },
298
+ { L"bs-ba-latn" , 183 },
299
+ { L"ca" , 2 },
300
+ { L"ca-es" , 69 },
301
+ { L"cs" , 4 },
302
+ { L"cs-cz" , 71 },
303
+ { L"cy-gb" , 135 },
304
+ { L"da" , 5 },
305
+ { L"da-dk" , 72 },
306
+ { L"de" , 6 },
307
+ { L"de-at" , 162 },
308
+ { L"de-ch" , 145 },
309
+ { L"de-de" , 73 },
310
+ { L"de-li" , 179 },
311
+ { L"de-lu" , 171 },
312
+ { L"div" , 65 },
313
+ { L"div-mv" , 139 },
314
+ { L"el" , 7 },
315
+ { L"el-gr" , 74 },
316
+ { L"en" , 8 },
317
+ { L"en-au" , 163 },
318
+ { L"en-bz" , 205 },
319
+ { L"en-ca" , 172 },
320
+ { L"en-cb" , 201 },
321
+ { L"en-gb" , 146 },
322
+ { L"en-ie" , 186 },
323
+ { L"en-jm" , 197 },
324
+ { L"en-nz" , 180 },
325
+ { L"en-ph" , 214 },
326
+ { L"en-tt" , 208 },
327
+ { L"en-us" , 75 },
328
+ { L"en-za" , 192 },
329
+ { L"en-zw" , 211 },
330
+ { L"es" , 9 },
331
+ { L"es-ar" , 209 },
332
+ { L"es-bo" , 221 },
333
+ { L"es-cl" , 215 },
334
+ { L"es-co" , 202 },
335
+ { L"es-cr" , 181 },
336
+ { L"es-do" , 193 },
337
+ { L"es-ec" , 212 },
338
+ { L"es-es" , 164 },
339
+ { L"es-gt" , 173 },
340
+ { L"es-hn" , 223 },
341
+ { L"es-mx" , 147 },
342
+ { L"es-ni" , 224 },
343
+ { L"es-pa" , 187 },
344
+ { L"es-pe" , 206 },
345
+ { L"es-pr" , 225 },
346
+ { L"es-py" , 219 },
347
+ { L"es-sv" , 222 },
348
+ { L"es-uy" , 217 },
349
+ { L"es-ve" , 198 },
350
+ { L"et" , 35 },
351
+ { L"et-ee" , 101 },
352
+ { L"eu" , 42 },
353
+ { L"eu-es" , 108 },
354
+ { L"fa" , 38 },
355
+ { L"fa-ir" , 104 },
356
+ { L"fi" , 10 },
357
+ { L"fi-fi" , 76 },
358
+ { L"fo" , 46 },
359
+ { L"fo-fo" , 115 },
360
+ { L"fr" , 11 },
361
+ { L"fr-be" , 148 },
362
+ { L"fr-ca" , 165 },
363
+ { L"fr-ch" , 174 },
364
+ { L"fr-fr" , 77 },
365
+ { L"fr-lu" , 182 },
366
+ { L"fr-mc" , 188 },
367
+ { L"gl" , 62 },
368
+ { L"gl-es" , 136 },
369
+ { L"gu" , 55 },
370
+ { L"gu-in" , 127 },
371
+ { L"he" , 12 },
372
+ { L"he-il" , 78 },
373
+ { L"hi" , 47 },
374
+ { L"hi-in" , 116 },
375
+ { L"hr" , 24 },
376
+ { L"hr-ba" , 175 },
377
+ { L"hr-hr" , 90 },
378
+ { L"hu" , 13 },
379
+ { L"hu-hu" , 79 },
380
+ { L"hy" , 40 },
381
+ { L"hy-am" , 106 },
382
+ { L"id" , 31 },
383
+ { L"id-id" , 97 },
384
+ { L"is" , 14 },
385
+ { L"is-is" , 80 },
386
+ { L"it" , 15 },
387
+ { L"it-ch" , 149 },
388
+ { L"it-it" , 81 },
389
+ { L"ja" , 16 },
390
+ { L"ja-jp" , 82 },
391
+ { L"ka" , 45 },
392
+ { L"ka-ge" , 114 },
393
+ { L"kk" , 49 },
394
+ { L"kk-kz" , 120 },
395
+ { L"kn" , 58 },
396
+ { L"kn-in" , 130 },
397
+ { L"ko" , 17 },
398
+ { L"kok" , 63 },
399
+ { L"kok-in" , 137 },
400
+ { L"ko-kr" , 83 },
401
+ { L"ky" , 50 },
402
+ { L"ky-kg" , 121 },
403
+ { L"lt" , 37 },
404
+ { L"lt-lt" , 103 },
405
+ { L"lv" , 36 },
406
+ { L"lv-lv" , 102 },
407
+ { L"mi-nz" , 142 },
408
+ { L"mk" , 43 },
409
+ { L"mk-mk" , 109 },
410
+ { L"ml-in" , 131 },
411
+ { L"mn" , 61 },
412
+ { L"mn-mn" , 134 },
413
+ { L"mr" , 59 },
414
+ { L"mr-in" , 132 },
415
+ { L"ms" , 48 },
416
+ { L"ms-bn" , 157 },
417
+ { L"ms-my" , 119 },
418
+ { L"mt-mt" , 117 },
419
+ { L"nb-no" , 85 },
420
+ { L"nl" , 18 },
421
+ { L"nl-be" , 150 },
422
+ { L"nl-nl" , 84 },
423
+ { L"nn-no" , 151 },
424
+ { L"no" , 19 },
425
+ { L"ns-za" , 141 },
426
+ { L"pa" , 54 },
427
+ { L"pa-in" , 126 },
428
+ { L"pl" , 20 },
429
+ { L"pl-pl" , 86 },
430
+ { L"pt" , 21 },
431
+ { L"pt-br" , 87 },
432
+ { L"pt-pt" , 152 },
433
+ { L"quz-bo" , 140 },
434
+ { L"quz-ec" , 159 },
435
+ { L"quz-pe" , 168 },
436
+ { L"ro" , 22 },
437
+ { L"ro-ro" , 88 },
438
+ { L"ru" , 23 },
439
+ { L"ru-ru" , 89 },
440
+ { L"sa" , 60 },
441
+ { L"sa-in" , 133 },
442
+ { L"se-fi" , 167 },
443
+ { L"se-no" , 118 },
444
+ { L"se-se" , 156 },
445
+ { L"sk" , 25 },
446
+ { L"sk-sk" , 91 },
447
+ { L"sl" , 34 },
448
+ { L"sl-si" , 100 },
449
+ { L"sma-no" , 190 },
450
+ { L"sma-se" , 195 },
451
+ { L"smj-no" , 176 },
452
+ { L"smj-se" , 184 },
453
+ { L"smn-fi" , 203 },
454
+ { L"sms-fi" , 199 },
455
+ { L"sq" , 26 },
456
+ { L"sq-al" , 92 },
457
+ { L"sr" , 227 },
458
+ { L"sr-ba-cyrl" , 194 },
459
+ { L"sr-ba-latn" , 189 },
460
+ { L"sr-sp-cyrl" , 166 },
461
+ { L"sr-sp-latn" , 153 },
462
+ { L"sv" , 27 },
463
+ { L"sv-fi" , 154 },
464
+ { L"sv-se" , 93 },
465
+ { L"sw" , 51 },
466
+ { L"sw-ke" , 122 },
467
+ { L"syr" , 64 },
468
+ { L"syr-sy" , 138 },
469
+ { L"ta" , 56 },
470
+ { L"ta-in" , 128 },
471
+ { L"te" , 57 },
472
+ { L"te-in" , 129 },
473
+ { L"th" , 28 },
474
+ { L"th-th" , 94 },
475
+ { L"tn-za" , 110 },
476
+ { L"tr" , 29 },
477
+ { L"tr-tr" , 95 },
478
+ { L"tt" , 53 },
479
+ { L"tt-ru" , 124 },
480
+ { L"uk" , 32 },
481
+ { L"uk-ua" , 98 },
482
+ { L"ur" , 30 },
483
+ { L"ur-pk" , 96 },
484
+ { L"uz" , 52 },
485
+ { L"uz-uz-cyrl" , 158 },
486
+ { L"uz-uz-latn" , 123 },
487
+ { L"vi" , 39 },
488
+ { L"vi-vn" , 105 },
489
+ { L"xh-za" , 111 },
490
+ { L"zh-chs" , 3 },
491
+ { L"zh-cht" , 226 },
492
+ { L"zh-cn" , 144 },
493
+ { L"zh-hk" , 161 },
494
+ { L"zh-mo" , 178 },
495
+ { L"zh-sg" , 170 },
496
+ { L"zh-tw" , 70 },
497
+ { L"zu-za" , 112 }
498
+ };
499
+
500
+ } // unnamed namespace
501
+
502
+ // Maps input locale name to the index on LcidToLocaleNameTable
503
+ static int GetTableIndexFromLocaleName(const wchar_t* localeName) throw()
504
+ {
505
+ LocaleNameIndex *localeNamesIndex = (LocaleNameIndex *) LocaleNameToIndexTable;
506
+ int bottom = 0;
507
+ int top = _countof(LocaleNameToIndexTable) - 1;
508
+
509
+ while (bottom <= top)
510
+ {
511
+ int middle = (bottom + top) / 2;
512
+ int testIndex = __ascii_wcsnicmp(localeName, localeNamesIndex[middle].name, LOCALE_NAME_MAX_LENGTH);
513
+
514
+ if (testIndex == 0)
515
+ return localeNamesIndex[middle].index;
516
+
517
+ if (testIndex < 0)
518
+ top = middle - 1;
519
+ else
520
+ bottom = middle + 1;
521
+ }
522
+
523
+ return -1;
524
+ }
525
+
526
+ // Maps input LCID to an index in LcidToLocaleNameTable
527
+ static int GetTableIndexFromLcid(LCID lcid) throw()
528
+ {
529
+
530
+ int bottom = 0;
531
+ int top = _countof(LcidToLocaleNameTable) - 1;
532
+
533
+ while (bottom <= top)
534
+ {
535
+ int middle = (bottom + top) / 2;
536
+ int testIndex = lcid - LcidToLocaleNameTable[middle].lcid;
537
+
538
+ if (testIndex == 0)
539
+ return middle;
540
+
541
+ if (testIndex < 0)
542
+ top = middle - 1;
543
+ else
544
+ bottom = middle + 1;
545
+ }
546
+
547
+ return -1;
548
+ }
549
+
550
+
551
+ extern "C" LCID __cdecl __acrt_DownlevelLocaleNameToLCID(LPCWSTR localeName)
552
+ {
553
+ int index;
554
+
555
+ if (localeName == nullptr)
556
+ return 0;
557
+
558
+ index = GetTableIndexFromLocaleName(localeName);
559
+
560
+ if (index < 0 || (index >= _countof(LcidToLocaleNameTable)))
561
+ return 0;
562
+
563
+ return LcidToLocaleNameTable[index].lcid;
564
+ }
565
+
566
+ extern "C" int __cdecl __acrt_DownlevelLCIDToLocaleName(
567
+ LCID lcid,
568
+ LPWSTR outLocaleName,
569
+ int cchLocaleName
570
+ )
571
+ {
572
+ size_t count;
573
+ wchar_t* buffer;
574
+ int index;
575
+
576
+ if (lcid == 0 ||
577
+ lcid == LOCALE_USER_DEFAULT ||
578
+ lcid == LOCALE_SYSTEM_DEFAULT)
579
+ {
580
+ return 0;
581
+ }
582
+
583
+ if ((outLocaleName == nullptr && cchLocaleName>0) || cchLocaleName < 0)
584
+ {
585
+ return 0;
586
+ }
587
+
588
+ index = GetTableIndexFromLcid(lcid);
589
+ if (index < 0)
590
+ return 0;
591
+
592
+ buffer = LcidToLocaleNameTable[index].localeName;
593
+ count = wcsnlen(buffer, LOCALE_NAME_MAX_LENGTH);
594
+
595
+ if (cchLocaleName > 0)
596
+ {
597
+ if ((int)count >= cchLocaleName)
598
+ return 0;
599
+
600
+ _ERRCHECK(wcscpy_s(outLocaleName, cchLocaleName, buffer));
601
+ }
602
+
603
+ return (int) count + 1;
604
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/lconv_unsigned_char_initialization.cpp ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // lconv_unsigned_char_initialization.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // lconv initialization used to support an unsigned 'char' type (enabled by the
7
+ // -J flag during compilation). Per the specification, some members of the lconv
8
+ // structure must be initialized to CHAR_MAX. The value of CHAR_MAX, however,
9
+ // depends on whether the 'char' type is signed or unsigned (it is 127 for a
10
+ // signed 'char'; 255 for an unsigned 'char').
11
+ //
12
+ // By default, these members of lconv are initialized to SCHAR_MAX (127). If an
13
+ // unsigned 'char' type is used, we call this function to update those members
14
+ // to UCHAR_MAX (255). Note that this is not done for DLLs that are linked to
15
+ // the CRT DLL, because we do not want such DLLs to override the -J setting for
16
+ // an EXE linked to the CRT DLL.
17
+ //
18
+ // There is code in several other files that is required to make all of this work:
19
+ //
20
+ // * locale.h: When -J is used to compile a source file that includes <locale.h>,
21
+ // it generates an unresolved external to __do_unsigned_char_lconv_initialization.
22
+ //
23
+ // * lconv_unsigned_char_static.cpp: This file defines a global variable named
24
+ // __do_unsigned_char_lconv_initialization, and is used during the link to
25
+ // satisfy the unresolved external. This file adds a call to
26
+ // __initialize_lconv_for_unsigned_char to the list of initializers to be
27
+ // executed. That function (defined below) updates the fields of the lconv
28
+ // structure to be set to UCHAR_MAX.
29
+ //
30
+ // * localeconv.cpp: This file defines the __acrt_lconv_c structure.
31
+ //
32
+ #include <corecrt_internal.h>
33
+ #include <limits.h>
34
+ #include <locale.h>
35
+
36
+ #pragma warning(disable: 4310) // cast truncates constant value
37
+
38
+
39
+
40
+ extern "C" int __cdecl __initialize_lconv_for_unsigned_char()
41
+ {
42
+ __acrt_lconv_c.int_frac_digits = (char)UCHAR_MAX;
43
+ __acrt_lconv_c.frac_digits = (char)UCHAR_MAX;
44
+ __acrt_lconv_c.p_cs_precedes = (char)UCHAR_MAX;
45
+ __acrt_lconv_c.p_sep_by_space = (char)UCHAR_MAX;
46
+ __acrt_lconv_c.n_cs_precedes = (char)UCHAR_MAX;
47
+ __acrt_lconv_c.n_sep_by_space = (char)UCHAR_MAX;
48
+ __acrt_lconv_c.p_sign_posn = (char)UCHAR_MAX;
49
+ __acrt_lconv_c.n_sign_posn = (char)UCHAR_MAX;
50
+
51
+ return 0;
52
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/locale_refcounting.cpp ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *localref.c - Contains the __[add|remove]localeref() functions.
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the __[add|remove]localeref() functions.
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+ #include <locale.h>
12
+
13
+
14
+
15
+ /***
16
+ * __acrt_add_locale_ref(__crt_locale_data* ptloci)
17
+ *
18
+ * Purpose:
19
+ * Increment the refrence count for each element in the localeinfo struct.
20
+ *
21
+ *******************************************************************************/
22
+ extern "C" void __cdecl __acrt_add_locale_ref(__crt_locale_data* ptloci)
23
+ {
24
+ _InterlockedIncrement(&ptloci->refcount);
25
+
26
+ if (ptloci->lconv_intl_refcount != nullptr)
27
+ {
28
+ _InterlockedIncrement(ptloci->lconv_intl_refcount);
29
+ }
30
+
31
+ if (ptloci->lconv_mon_refcount != nullptr)
32
+ {
33
+ _InterlockedIncrement(ptloci->lconv_mon_refcount);
34
+ }
35
+
36
+ if (ptloci->lconv_num_refcount != nullptr)
37
+ {
38
+ _InterlockedIncrement(ptloci->lconv_num_refcount);
39
+ }
40
+
41
+ if (ptloci->ctype1_refcount != nullptr)
42
+ {
43
+ _InterlockedIncrement(ptloci->ctype1_refcount);
44
+ }
45
+
46
+ for (int category = LC_MIN; category <= LC_MAX; ++category)
47
+ {
48
+ if (ptloci->lc_category[category].wlocale != __acrt_wide_c_locale_string &&
49
+ ptloci->lc_category[category].wrefcount != nullptr)
50
+ {
51
+ _InterlockedIncrement(ptloci->lc_category[category].wrefcount);
52
+ }
53
+
54
+ if (ptloci->lc_category[category].locale != nullptr &&
55
+ ptloci->lc_category[category].refcount != nullptr)
56
+ {
57
+ _InterlockedIncrement(ptloci->lc_category[category].refcount);
58
+ }
59
+ }
60
+
61
+ __acrt_locale_add_lc_time_reference(ptloci->lc_time_curr);
62
+ }
63
+
64
+ /***
65
+ * __acrt_release_locale_ref(__crt_locale_data* ptloci)
66
+ *
67
+ * Purpose:
68
+ * Decrement the refrence count for each elemnt in the localeinfo struct.
69
+ *
70
+ ******************************************************************************/
71
+ extern "C" void __cdecl __acrt_release_locale_ref(__crt_locale_data* ptloci)
72
+ {
73
+ if (ptloci == nullptr)
74
+ return;
75
+
76
+ _InterlockedDecrement(&ptloci->refcount);
77
+
78
+ if (ptloci->lconv_intl_refcount != nullptr)
79
+ _InterlockedDecrement(ptloci->lconv_intl_refcount);
80
+
81
+ if (ptloci->lconv_mon_refcount != nullptr)
82
+ _InterlockedDecrement(ptloci->lconv_mon_refcount);
83
+
84
+ if (ptloci->lconv_num_refcount != nullptr)
85
+ _InterlockedDecrement(ptloci->lconv_num_refcount);
86
+
87
+ if (ptloci->ctype1_refcount != nullptr)
88
+ _InterlockedDecrement(ptloci->ctype1_refcount);
89
+
90
+ for (int category = LC_MIN; category <= LC_MAX; ++category)
91
+ {
92
+ if (ptloci->lc_category[category].wlocale != __acrt_wide_c_locale_string &&
93
+ ptloci->lc_category[category].wrefcount != nullptr)
94
+ {
95
+ _InterlockedDecrement(ptloci->lc_category[category].wrefcount);
96
+ }
97
+
98
+ if (ptloci->lc_category[category].locale != nullptr &&
99
+ ptloci->lc_category[category].refcount != nullptr)
100
+ {
101
+ _InterlockedDecrement(ptloci->lc_category[category].refcount);
102
+ }
103
+ }
104
+
105
+ __acrt_locale_release_lc_time_reference(ptloci->lc_time_curr);
106
+ }
107
+
108
+
109
+ /***
110
+ *__acrt_free_locale() - free __crt_locale_data
111
+ *
112
+ *Purpose:
113
+ * Free up the per-thread locale info structure specified by the passed
114
+ * pointer.
115
+ *
116
+ *Entry:
117
+ * __crt_locale_data* ptloci
118
+ *
119
+ *Exit:
120
+ *
121
+ *Exceptions:
122
+ *
123
+ *******************************************************************************/
124
+
125
+ extern "C" void __cdecl __acrt_free_locale(__crt_locale_data* ptloci)
126
+ {
127
+ /*
128
+ * Free up lconv struct
129
+ */
130
+ if (ptloci->lconv != nullptr &&
131
+ ptloci->lconv != &__acrt_lconv_c &&
132
+ ptloci->lconv_intl_refcount != nullptr &&
133
+ *ptloci->lconv_intl_refcount == 0)
134
+ {
135
+ if (ptloci->lconv_mon_refcount != nullptr && *ptloci->lconv_mon_refcount == 0)
136
+ {
137
+ _free_crt(ptloci->lconv_mon_refcount);
138
+ __acrt_locale_free_monetary(ptloci->lconv);
139
+ }
140
+
141
+ if (ptloci->lconv_num_refcount != nullptr && *ptloci->lconv_num_refcount == 0)
142
+ {
143
+ _free_crt(ptloci->lconv_num_refcount);
144
+ __acrt_locale_free_numeric(ptloci->lconv);
145
+ }
146
+
147
+ _free_crt(ptloci->lconv_intl_refcount);
148
+ _free_crt(ptloci->lconv);
149
+ }
150
+
151
+ /*
152
+ * Free up ctype tables
153
+ */
154
+ if (ptloci->ctype1_refcount != nullptr && *ptloci->ctype1_refcount == 0)
155
+ {
156
+ _free_crt(ptloci->ctype1 - _COFFSET);
157
+ _free_crt(const_cast<unsigned char*>(ptloci->pclmap - _COFFSET - 1));
158
+ _free_crt(const_cast<unsigned char*>(ptloci->pcumap - _COFFSET - 1));
159
+ _free_crt(ptloci->ctype1_refcount);
160
+ }
161
+
162
+ __acrt_locale_free_lc_time_if_unreferenced(ptloci->lc_time_curr);
163
+
164
+ for (int category = LC_MIN; category <= LC_MAX; ++category)
165
+ {
166
+ if (ptloci->lc_category[category].wlocale != __acrt_wide_c_locale_string &&
167
+ ptloci->lc_category[category].wrefcount != nullptr &&
168
+ *ptloci->lc_category[category].wrefcount == 0)
169
+ {
170
+ _free_crt(ptloci->lc_category[category].wrefcount);
171
+ _free_crt(ptloci->locale_name[category]);
172
+ }
173
+
174
+ _ASSERTE((ptloci->lc_category[category].locale != nullptr && ptloci->lc_category[category].refcount != nullptr) ||
175
+ (ptloci->lc_category[category].locale == nullptr && ptloci->lc_category[category].refcount == nullptr));
176
+
177
+ if (ptloci->lc_category[category].locale != nullptr &&
178
+ ptloci->lc_category[category].refcount != nullptr &&
179
+ *ptloci->lc_category[category].refcount == 0)
180
+ {
181
+ _free_crt(ptloci->lc_category[category].refcount);
182
+ }
183
+ }
184
+
185
+ /*
186
+ * Free up the __crt_locale_data struct
187
+ */
188
+ _free_crt(ptloci);
189
+ }
190
+
191
+
192
+ /***
193
+ *
194
+ * _updatelocinfoEx_nolock(__crt_locale_data* *pptlocid, __crt_locale_data* ptlocis)
195
+ *
196
+ * Purpose:
197
+ * Update *pptlocid to ptlocis. This might include freeing contents of *pptlocid.
198
+ *
199
+ ******************************************************************************/
200
+ extern "C" __crt_locale_data* __cdecl _updatetlocinfoEx_nolock(
201
+ __crt_locale_data** pptlocid,
202
+ __crt_locale_data* ptlocis
203
+ )
204
+ {
205
+ if (ptlocis == nullptr || pptlocid == nullptr)
206
+ return nullptr;
207
+
208
+ __crt_locale_data* const ptloci = *pptlocid;
209
+ if (ptloci == ptlocis)
210
+ return ptlocis;
211
+
212
+ /*
213
+ * Update to the current locale info structure and increment the
214
+ * reference counts.
215
+ */
216
+ *pptlocid = ptlocis;
217
+ __acrt_add_locale_ref(ptlocis);
218
+
219
+ /*
220
+ * Decrement the reference counts in the old locale info
221
+ * structure.
222
+ */
223
+ if (ptloci != nullptr)
224
+ {
225
+ __acrt_release_locale_ref(ptloci);
226
+ }
227
+
228
+ /*
229
+ * Free the old locale info structure, if necessary. Must be done
230
+ * after incrementing reference counts in current locale in case
231
+ * any refcounts are shared with the old locale.
232
+ */
233
+ if (ptloci != nullptr &&
234
+ ptloci->refcount == 0 &&
235
+ ptloci != &__acrt_initial_locale_data)
236
+ {
237
+ __acrt_free_locale(ptloci);
238
+ }
239
+
240
+ return ptlocis;
241
+ }
242
+
243
+
244
+ /***
245
+ *__acrt_update_thread_locale_data() - refresh the thread's locale info
246
+ *
247
+ *Purpose:
248
+ * If this thread does not have it's ownlocale which means that either
249
+ * ownlocale flag in ptd is not set or ptd->ptloci == nullptr, then Update
250
+ * the current thread's reference to the locale information to match the
251
+ * current global locale info. Decrement the reference on the old locale
252
+ * information struct and if this count is now zero (so that no threads
253
+ * are using it), free it.
254
+ *
255
+ *Entry:
256
+ *
257
+ *Exit:
258
+ *
259
+ * if (!_getptd()->ownlocale || _getptd()->ptlocinfo == nullptr)
260
+ * _getptd()->ptlocinfo == __acrt_current_locale_data
261
+ * else
262
+ * _getptd()->ptlocinfo
263
+ *
264
+ *Exceptions:
265
+ *
266
+ *******************************************************************************/
267
+ extern "C" __crt_locale_data* __cdecl __acrt_update_thread_locale_data()
268
+ {
269
+ __crt_locale_data* ptloci = nullptr;
270
+ __acrt_ptd* const ptd = __acrt_getptd();
271
+
272
+ if (__acrt_should_sync_with_global_locale(ptd) || ptd->_locale_info == nullptr)
273
+ {
274
+ __acrt_lock(__acrt_locale_lock);
275
+ __try
276
+ {
277
+ ptloci = _updatetlocinfoEx_nolock(&ptd->_locale_info, __acrt_current_locale_data.value());
278
+ }
279
+ __finally
280
+ {
281
+ __acrt_unlock(__acrt_locale_lock);
282
+ }
283
+ }
284
+ else
285
+ {
286
+ ptloci = ptd->_locale_info;
287
+ }
288
+
289
+ if (!ptloci)
290
+ {
291
+ abort();
292
+ }
293
+
294
+ return ptloci;
295
+ }
296
+
297
+
298
+
299
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
300
+ //
301
+ // __crt_lc_time_data reference counting
302
+ //
303
+ //-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
304
+ // The C locale lc_time data is immutable and persistent. The lc_time data for
305
+ // any other locale is dynamically allocated (and thus not actually immutable).
306
+ // The lc_time_curr data member of __crt_locale_data is declared const to help
307
+ // ensure that code does not accidentally attempt to modify the C locale lc_time
308
+ // data.
309
+ //
310
+ // These functions encapsulate all mutation of the lc_time_curr. They accept a
311
+ // __crt_lc_time_data const*, handle the C locale as a special case, then cast
312
+ // away constness to mutate the lc_time data.
313
+ extern "C" long __cdecl __acrt_locale_add_lc_time_reference(
314
+ __crt_lc_time_data const* const lc_time
315
+ )
316
+ {
317
+ if (lc_time == nullptr || lc_time == &__lc_time_c)
318
+ {
319
+ return LONG_MAX;
320
+ }
321
+
322
+ return _InterlockedIncrement(&const_cast<__crt_lc_time_data*>(lc_time)->refcount);
323
+ }
324
+
325
+ extern "C" long __cdecl __acrt_locale_release_lc_time_reference(
326
+ __crt_lc_time_data const* const lc_time
327
+ )
328
+ {
329
+ if (lc_time == nullptr || lc_time == &__lc_time_c)
330
+ {
331
+ return LONG_MAX;
332
+ }
333
+
334
+ return _InterlockedDecrement(&const_cast<__crt_lc_time_data*>(lc_time)->refcount);
335
+ }
336
+
337
+ extern "C" void __cdecl __acrt_locale_free_lc_time_if_unreferenced(
338
+ __crt_lc_time_data const* const lc_time
339
+ )
340
+ {
341
+ if (lc_time == nullptr || lc_time == &__lc_time_c)
342
+ {
343
+ return;
344
+ }
345
+
346
+ if (__crt_interlocked_read(&lc_time->refcount) != 0)
347
+ {
348
+ return;
349
+ }
350
+
351
+ __acrt_locale_free_time(const_cast<__crt_lc_time_data*>(lc_time));
352
+ _free_crt(const_cast<__crt_lc_time_data*>(lc_time));
353
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/locale_update.cpp ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // locale_update.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Encapsulated implementation details of _LocaleUpdate that cannot be defined
7
+ // in the header because they refer to data not exported from the AppCRT DLL.
8
+ //
9
+ #include <corecrt_internal.h>
10
+
11
+ extern "C" void __acrt_update_locale_info(
12
+ __acrt_ptd* const ptd,
13
+ __crt_locale_data** const locale_info
14
+ )
15
+ {
16
+ if (*locale_info != __acrt_current_locale_data.value() && __acrt_should_sync_with_global_locale(ptd))
17
+ {
18
+ *locale_info = __acrt_update_thread_locale_data();
19
+ }
20
+ }
21
+
22
+ extern "C" void __acrt_update_multibyte_info(
23
+ __acrt_ptd* const ptd,
24
+ __crt_multibyte_data** const multibyte_info
25
+ )
26
+ {
27
+ if (*multibyte_info != __acrt_current_multibyte_data.value() && __acrt_should_sync_with_global_locale(ptd))
28
+ {
29
+ *multibyte_info = __acrt_update_thread_multibyte_data();
30
+ }
31
+ }
32
+
33
+ extern "C" void __acrt_update_locale_info_explicit(
34
+ __acrt_ptd* const ptd,
35
+ __crt_locale_data** const locale_info,
36
+ size_t const current_global_state_index
37
+ )
38
+ {
39
+ if (*locale_info != __acrt_current_locale_data.value_explicit(current_global_state_index) && __acrt_should_sync_with_global_locale(ptd))
40
+ {
41
+ *locale_info = __acrt_update_thread_locale_data();
42
+ }
43
+ }
44
+
45
+ extern "C" void __acrt_update_multibyte_info_explicit(
46
+ __acrt_ptd* const ptd,
47
+ __crt_multibyte_data** const multibyte_info,
48
+ size_t const current_global_state_index
49
+ )
50
+ {
51
+ if (*multibyte_info != __acrt_current_multibyte_data.value_explicit(current_global_state_index) && __acrt_should_sync_with_global_locale(ptd))
52
+ {
53
+ *multibyte_info = __acrt_update_thread_multibyte_data();
54
+ }
55
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/localeconv.cpp ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *lconv.c - Contains the localeconv function
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the localeconv() function.
8
+ *
9
+ *******************************************************************************/
10
+ #include <corecrt_internal.h>
11
+ #include <limits.h>
12
+ #include <locale.h>
13
+
14
+
15
+
16
+ /* pointer to original static to avoid freeing */
17
+ extern "C" char __acrt_lconv_static_decimal []{"."};
18
+ extern "C" char __acrt_lconv_static_null []{""};
19
+ extern "C" wchar_t __acrt_lconv_static_W_decimal[]{L"."};
20
+ extern "C" wchar_t __acrt_lconv_static_W_null []{L""};
21
+
22
+ /* lconv settings for "C" locale */
23
+ extern "C" struct lconv __acrt_lconv_c
24
+ {
25
+ __acrt_lconv_static_decimal, // decimal_point
26
+ __acrt_lconv_static_null, // thousands_sep
27
+ __acrt_lconv_static_null, // grouping
28
+ __acrt_lconv_static_null, // int_curr_symbol
29
+ __acrt_lconv_static_null, // currency_symbol
30
+ __acrt_lconv_static_null, // mon_decimal_point
31
+ __acrt_lconv_static_null, // mon_thousands_sep
32
+ __acrt_lconv_static_null, // mon_grouping
33
+ __acrt_lconv_static_null, // positive_sign
34
+ __acrt_lconv_static_null, // negative_sign
35
+ CHAR_MAX, // int_frac_digits
36
+ CHAR_MAX, // frac_digits
37
+ CHAR_MAX, // p_cs_precedes
38
+ CHAR_MAX, // p_sep_by_space
39
+ CHAR_MAX, // n_cs_precedes
40
+ CHAR_MAX, // n_sep_by_space
41
+ CHAR_MAX, // p_sign_posn
42
+ CHAR_MAX, // n_sign_posn
43
+ __acrt_lconv_static_W_decimal, // _W_decimal_point
44
+ __acrt_lconv_static_W_null, // _W_thousands_sep
45
+ __acrt_lconv_static_W_null, // _W_int_curr_symbol
46
+ __acrt_lconv_static_W_null, // _W_currency_symbol
47
+ __acrt_lconv_static_W_null, // _W_mon_decimal_point
48
+ __acrt_lconv_static_W_null, // _W_mon_thousands_sep
49
+ __acrt_lconv_static_W_null, // _W_positive_sign
50
+ __acrt_lconv_static_W_null, // _W_negative_sign
51
+ };
52
+
53
+
54
+ /* pointer to current lconv structure */
55
+
56
+ extern "C" struct lconv* __acrt_lconv{&__acrt_lconv_c};
57
+
58
+ /***
59
+ *struct lconv *localeconv(void) - Return the numeric formatting convention
60
+ *
61
+ *Purpose:
62
+ * The localeconv() routine returns the numeric formatting conventions
63
+ * for the current locale setting. [ANSI]
64
+ *
65
+ *Entry:
66
+ * void
67
+ *
68
+ *Exit:
69
+ * struct lconv * = pointer to struct indicating current numeric
70
+ * formatting conventions.
71
+ *
72
+ *Exceptions:
73
+ *
74
+ *******************************************************************************/
75
+
76
+ extern "C" lconv* __cdecl localeconv()
77
+ {
78
+ // Note that we don't need _LocaleUpdate in this function. The main reason
79
+ // being, that this is a leaf function in locale usage terms.
80
+ __acrt_ptd* const ptd = __acrt_getptd();
81
+ __crt_locale_data* locale_info = ptd->_locale_info;
82
+
83
+ __acrt_update_locale_info(ptd, &locale_info);
84
+
85
+ return locale_info->lconv;
86
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/nlsdata.cpp ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // nlsdata.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Globals for the locale and code page implementation. These are utilized by
7
+ // almost all locale-dependent functions.
8
+ //
9
+ #include <corecrt_internal.h>
10
+
11
+
12
+
13
+ extern "C" int __mb_cur_max{1};
14
+
15
+
16
+
17
+ extern "C" int* __cdecl __p___mb_cur_max()
18
+ {
19
+ __acrt_ptd* ptd = __acrt_getptd();
20
+ __crt_locale_data* locale_info = ptd->_locale_info;
21
+
22
+ __acrt_update_locale_info(ptd, &locale_info);
23
+ return &locale_info->_public._locale_mb_cur_max;
24
+ }
25
+
26
+
27
+
28
+ extern "C" wchar_t __acrt_wide_c_locale_string[]{L"C"};
29
+
30
+
31
+
32
+ extern "C" __crt_lc_time_data const __lc_time_c
33
+ {
34
+ { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
35
+
36
+ {
37
+ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
38
+ "Friday", "Saturday"
39
+ },
40
+
41
+ {
42
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
43
+ "Sep", "Oct", "Nov", "Dec"
44
+ },
45
+
46
+ {
47
+ "January", "February", "March", "April", "May", "June",
48
+ "July", "August", "September", "October",
49
+ "November", "December"
50
+ },
51
+
52
+ { "AM", "PM" },
53
+
54
+ "MM/dd/yy",
55
+ "dddd, MMMM dd, yyyy",
56
+ "HH:mm:ss",
57
+
58
+ 1,
59
+ 0,
60
+
61
+ { L"Sun", L"Mon", L"Tue", L"Wed", L"Thu", L"Fri", L"Sat" },
62
+
63
+ {
64
+ L"Sunday", L"Monday", L"Tuesday", L"Wednesday",
65
+ L"Thursday", L"Friday", L"Saturday"
66
+ },
67
+
68
+ {
69
+ L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun", L"Jul",
70
+ L"Aug", L"Sep", L"Oct", L"Nov", L"Dec"
71
+ },
72
+
73
+ {
74
+ L"January", L"February", L"March", L"April", L"May",
75
+ L"June", L"July", L"August", L"September", L"October",
76
+ L"November", L"December"
77
+ },
78
+
79
+ { L"AM", L"PM" },
80
+
81
+ L"MM/dd/yy",
82
+ L"dddd, MMMM dd, yyyy",
83
+ L"HH:mm:ss",
84
+ L"en-US"
85
+ };
86
+
87
+
88
+
89
+ // The initial locale information structure, containing the C locale data. It
90
+ // is used until the first call to setlocale().
91
+ extern "C" __crt_locale_data __acrt_initial_locale_data
92
+ {
93
+ {
94
+ __newctype + 128, // _locale_pctype
95
+ 1, // _locale_mb_cur_max
96
+ CP_ACP // _locale_lc_codepage
97
+ },
98
+ 1, // refcount
99
+ CP_ACP, // lc_collate_cp
100
+ CP_ACP, // lc_time_cp
101
+ 1, // lc_clike
102
+ {
103
+ { nullptr, nullptr, nullptr, nullptr }, // lc_category[LC_ALL]
104
+ { nullptr, __acrt_wide_c_locale_string, nullptr, nullptr }, // lc_category[LC_COLLATE]
105
+ { nullptr, __acrt_wide_c_locale_string, nullptr, nullptr }, // lc_category[LC_CTYPE]
106
+ { nullptr, __acrt_wide_c_locale_string, nullptr, nullptr }, // lc_category[LC_MONETARY]
107
+ { nullptr, __acrt_wide_c_locale_string, nullptr, nullptr }, // lc_category[LC_NUMERIC]
108
+ { nullptr, __acrt_wide_c_locale_string, nullptr, nullptr } // lc_category[LC_TIME]
109
+ },
110
+ nullptr, // lconv_intl_refcount
111
+ nullptr, // lconv_num_refcount
112
+ nullptr, // lconv_mon_refcount
113
+ &__acrt_lconv_c, // lconv
114
+ nullptr, // ctype1_refcount
115
+ nullptr, // ctype1
116
+ __newclmap + 128, // pclmap
117
+ __newcumap + 128, // pcumap
118
+ &__lc_time_c, // lc_time_curr
119
+ {
120
+ nullptr, // locale_name[LC_ALL]
121
+ nullptr, // locale_name[LC_COLLATE]
122
+ nullptr, // locale_name[LC_CTYPE]
123
+ nullptr, // locale_name[LC_MONETARY]
124
+ nullptr, // locale_name[LC_NUMERIC]
125
+ nullptr // locale_name[LC_TIME]
126
+ }
127
+ };
128
+
129
+
130
+
131
+ // Global pointer to the current per-thread locale information structure.
132
+ __crt_state_management::dual_state_global<__crt_locale_data*> __acrt_current_locale_data;
133
+
134
+
135
+
136
+ extern "C" __crt_locale_pointers __acrt_initial_locale_pointers
137
+ {
138
+ &__acrt_initial_locale_data,
139
+ &__acrt_initial_multibyte_data
140
+ };
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/setlocale.cpp ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *setlocal.c - Contains the setlocale function
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the _wsetlocale() function.
8
+ *
9
+ *******************************************************************************/
10
+
11
+ #include <corecrt_internal.h>
12
+ #include <locale.h>
13
+ #include <stdlib.h>
14
+
15
+ extern "C" static wchar_t* __cdecl call_wsetlocale(int const category, char const* const narrow_locale)
16
+ {
17
+ if (narrow_locale == nullptr)
18
+ return _wsetlocale(category, nullptr);
19
+
20
+ size_t size;
21
+ _ERRCHECK_EINVAL_ERANGE(mbstowcs_s(&size, nullptr, 0, narrow_locale, INT_MAX));
22
+
23
+ __crt_unique_heap_ptr<wchar_t> wide_locale(_calloc_crt_t(wchar_t, size));
24
+ if (wide_locale.get() == nullptr)
25
+ return nullptr;
26
+
27
+ if (_ERRCHECK_EINVAL_ERANGE(mbstowcs_s(nullptr, wide_locale.get(), size, narrow_locale, _TRUNCATE)) != 0)
28
+ return nullptr;
29
+
30
+ return _wsetlocale(category, wide_locale.get());
31
+ }
32
+
33
+ extern "C" char* __cdecl setlocale(int _category, char const* _locale)
34
+ {
35
+ // Deadlock Avoidance: See the comment in _wsetlocale on usage of this function.
36
+ __acrt_eagerly_load_locale_apis();
37
+
38
+ return __acrt_lock_and_call(__acrt_locale_lock, [&]() -> char*
39
+ {
40
+ // Convert ASCII string into a wide character string:
41
+ wchar_t* const outwlocale = call_wsetlocale(_category, _locale);
42
+ if (outwlocale == nullptr)
43
+ {
44
+ return nullptr;
45
+ }
46
+
47
+ __acrt_ptd* const ptd = __acrt_getptd();
48
+
49
+ __crt_locale_pointers locale;
50
+ locale.locinfo = ptd->_locale_info;
51
+ locale.mbcinfo = ptd->_multibyte_info;
52
+
53
+ // We now have a locale string, but the global locale can be changed by
54
+ // another thread. If we allow this thread's locale to be updated before
55
+ // we're done with this string, it might be freed from under us. We call
56
+ // the versions of the wide-to-multibyte conversions that do not update the
57
+ // current thread's locale.
58
+ size_t size = 0;
59
+ if (_ERRCHECK_EINVAL_ERANGE(_wcstombs_s_l(&size, nullptr, 0, outwlocale, 0, &locale)) != 0)
60
+ {
61
+ return nullptr;
62
+ }
63
+
64
+ long* const refcount = static_cast<long*>(_malloc_crt(size + sizeof(long)));
65
+ if (refcount == nullptr)
66
+ {
67
+ return nullptr;
68
+ }
69
+
70
+ char* outlocale = reinterpret_cast<char*>(&refcount[1]);
71
+
72
+ /* convert return value to ASCII */
73
+ if (_ERRCHECK_EINVAL_ERANGE(_wcstombs_s_l(nullptr, outlocale, size, outwlocale, _TRUNCATE, &locale)) != 0)
74
+ {
75
+ _free_crt(refcount);
76
+ return nullptr;
77
+ }
78
+
79
+ __crt_locale_data* ptloci = locale.locinfo;
80
+
81
+ _ASSERTE((ptloci->lc_category[_category].locale != nullptr && ptloci->lc_category[_category].refcount != nullptr) ||
82
+ (ptloci->lc_category[_category].locale == nullptr && ptloci->lc_category[_category].refcount == nullptr));
83
+
84
+ if (ptloci->lc_category[_category].refcount != nullptr &&
85
+ _InterlockedDecrement(ptloci->lc_category[_category].refcount) == 0)
86
+ {
87
+ _free_crt(ptloci->lc_category[_category].refcount);
88
+ ptloci->lc_category[_category].refcount = nullptr;
89
+ }
90
+
91
+ if (__acrt_should_sync_with_global_locale(ptd))
92
+ {
93
+ if (ptloci->lc_category[_category].refcount != nullptr &&
94
+ _InterlockedDecrement(ptloci->lc_category[_category].refcount) == 0)
95
+ {
96
+ _free_crt(ptloci->lc_category[_category].refcount);
97
+ ptloci->lc_category[_category].refcount = nullptr;
98
+ }
99
+ }
100
+
101
+ /*
102
+ * Note that we are using a risky trick here. We are adding this
103
+ * locale to an existing __crt_locale_data struct, and thus starting
104
+ * the locale's refcount with the same value as the whole struct.
105
+ * That means all code which modifies both __crt_locale_data::refcount
106
+ * and __crt_locale_data::lc_category[]::refcount in structs that are
107
+ * potentially shared across threads must make those modifications
108
+ * under the locale lock. Otherwise, there's a race condition
109
+ * for some other thread modifying __crt_locale_data::refcount after
110
+ * we load it but before we store it to refcount.
111
+ */
112
+ *refcount = ptloci->refcount;
113
+ ptloci->lc_category[_category].refcount = refcount;
114
+ ptloci->lc_category[_category].locale = outlocale;
115
+
116
+ return outlocale;
117
+ });
118
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/locale/wsetlocale.cpp ADDED
@@ -0,0 +1,1430 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *wsetlocale.cpp - Contains the wsetlocale function
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * Contains the wsetlocale() function.
8
+ *
9
+ ****/
10
+
11
+ #include <corecrt_internal.h>
12
+ #include <locale.h>
13
+
14
+ extern "C" {
15
+
16
+ static const char _first_127char[] = {
17
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18
+ 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
19
+ 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
20
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
21
+ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
22
+ 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,101,102,
23
+ 103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,
24
+ 120,121,122,123,124,125,126,127
25
+ };
26
+
27
+ extern __crt_locale_data __acrt_initial_locale_data;
28
+ extern const unsigned short _wctype[];
29
+ static const unsigned short *_ctype_loc_style = _wctype + 2;
30
+
31
+ long __acrt_locale_changed_data = FALSE;
32
+
33
+ /* helper function prototypes */
34
+ _Success_(return != 0)
35
+ static wchar_t * _expandlocale(
36
+ _In_z_ wchar_t const * expr,
37
+ _Out_writes_z_(sizeInChars) wchar_t * output,
38
+ _In_ size_t sizeInChars,
39
+ _Out_writes_z_(localeNameSizeInChars) wchar_t * localeNameOutput,
40
+ _In_ size_t localeNameSizeInChars,
41
+ _Out_ UINT& cp
42
+ );
43
+
44
+ void _wcscats(_Inout_updates_z_(_Param_(2)) wchar_t *, size_t, int, ...);
45
+ void __lc_lctowcs(_Inout_updates_z_(_Param_(2)) wchar_t *, size_t, const __crt_locale_strings *);
46
+ int __lc_wcstolc(__crt_locale_strings *, const wchar_t *);
47
+ static wchar_t * __cdecl _wsetlocale_set_cat(__crt_locale_data*, int, const wchar_t *);
48
+ static wchar_t * __cdecl _wsetlocale_get_all(__crt_locale_data*);
49
+ static __crt_locale_data* __cdecl _updatetlocinfo_nolock(void);
50
+ static wchar_t * __cdecl _wsetlocale_nolock(__crt_locale_data*, int, const wchar_t *);
51
+ int __cdecl _setmbcp_nolock(int, __crt_multibyte_data*);
52
+ __crt_locale_data* __cdecl _updatetlocinfoEx_nolock(__crt_locale_data**, __crt_locale_data*);
53
+
54
+ void __cdecl __acrt_set_locale_changed()
55
+ {
56
+ _InterlockedExchange(&__acrt_locale_changed_data, TRUE);
57
+ }
58
+
59
+ /***
60
+ * _configthreadlocale(int i)
61
+ *
62
+ * Purpose:
63
+ * To set _own_locale flag on threadinfo sturct. If this flag is set, this thread
64
+ * is going own it's threadlocinfo struct. Setlocale call on other thread will have
65
+ * no effect on this thread's locale. If 0 is passed then nothing is changed, but
66
+ * current status is returned.
67
+ * Exit :
68
+ * Returns the current status - i.e. per thread locale is enabled or not.
69
+ *
70
+ *******************************************************************************/
71
+ int __cdecl _configthreadlocale(int i)
72
+ {
73
+ /*
74
+ * ownlocale flag struct:
75
+ * bits: 000000000000000000000000 000W 00P1
76
+ * P is set when _ENABLE_PER_THREAD_LOCALE is called for this thread
77
+ * or _ENABLE_PER_THREAD_LOCALE_NEW was set when this thread was created.
78
+ * W is set when _WSETLOCALE_AVOID_SYNC_LOCALE_BIT is set by _wsetlocale.
79
+ * It is used to disable global-ptd resynchronization during a call to _wsetlocale.
80
+ *
81
+ * __globallocalestatus structure:
82
+ * bits: 111111111111111111111111 1111 1N1G
83
+ * G is set if _ENABLE_PER_THREAD_LOCALE_GLOBAL is set.
84
+ * G is 0 if _ENABLE_PER_THREAD_LOCALE_GLOBAL is not set.
85
+ * N is set if _ENABLE_PER_THREAD_LOCALE_NEW is set.
86
+ * N is 0 if _ENABLE_PER_THREAD_LOCALE_NEW is not set.
87
+ */
88
+ __acrt_ptd* const ptd = __acrt_getptd();
89
+ int retval = (ptd->_own_locale & _PER_THREAD_LOCALE_BIT)==0 ? _DISABLE_PER_THREAD_LOCALE:_ENABLE_PER_THREAD_LOCALE;
90
+
91
+ switch(i)
92
+ {
93
+ case _ENABLE_PER_THREAD_LOCALE:
94
+ // same behavior as __acrt_disable_global_locale_sync()
95
+ ptd->_own_locale = ptd->_own_locale | _PER_THREAD_LOCALE_BIT;
96
+ break;
97
+
98
+ case _DISABLE_PER_THREAD_LOCALE:
99
+ // same behavior as __acrt_enable_global_locale_sync()
100
+ ptd->_own_locale = ptd->_own_locale & ~_PER_THREAD_LOCALE_BIT;
101
+ break;
102
+
103
+ case 0:
104
+ break;
105
+
106
+ /* used only during dll startup for linkopt */
107
+ case -1:
108
+ __globallocalestatus = -1;
109
+ break;
110
+
111
+ default:
112
+ _VALIDATE_RETURN(("Invalid parameter for _configthreadlocale",0),EINVAL,-1);
113
+ break;
114
+ }
115
+
116
+ return retval;
117
+ }
118
+
119
+ extern "C" void __cdecl __acrt_uninitialize_locale()
120
+ {
121
+ __acrt_lock_and_call(__acrt_locale_lock, [&]
122
+ {
123
+ __acrt_current_locale_data.uninitialize([](__crt_locale_data*& locale)
124
+ {
125
+ if (locale == &__acrt_initial_locale_data)
126
+ {
127
+ return;
128
+ }
129
+
130
+ locale = _updatetlocinfoEx_nolock(&locale, &__acrt_initial_locale_data);
131
+ });
132
+ });
133
+ }
134
+
135
+ /***
136
+ *_free_locale() - free threadlocinfo
137
+ *
138
+ *Purpose:
139
+ * Free up the per-thread locale info structure specified by the passed
140
+ * pointer.
141
+ *
142
+ *Entry:
143
+ * __crt_locale_data* ptloci
144
+ *
145
+ *Exit:
146
+ *
147
+ *Exceptions:
148
+ *
149
+ *******************************************************************************/
150
+
151
+ void __cdecl _free_locale(
152
+ _locale_t plocinfo
153
+ )
154
+ {
155
+ if (plocinfo != nullptr)
156
+ {
157
+ __acrt_lock(__acrt_multibyte_cp_lock);
158
+ __try
159
+ {
160
+ if (plocinfo->mbcinfo != nullptr &&
161
+ InterlockedDecrement(&(plocinfo->mbcinfo->refcount)) == 0 &&
162
+ plocinfo->mbcinfo != &__acrt_initial_multibyte_data )
163
+ {
164
+ _free_crt(plocinfo->mbcinfo);
165
+ }
166
+ }
167
+ __finally
168
+ {
169
+ __acrt_unlock(__acrt_multibyte_cp_lock);
170
+ }
171
+
172
+ if (plocinfo->locinfo != nullptr)
173
+ {
174
+ /*
175
+ * this portion has to be in locale lock as there may be case when
176
+ * not this thread but some other thread is still holding to this
177
+ * locale and is also trying to free this locale. In this case
178
+ * we may end up leaking memory.
179
+ */
180
+
181
+ __acrt_lock(__acrt_locale_lock);
182
+ __try
183
+ {
184
+ __acrt_release_locale_ref(plocinfo->locinfo);
185
+ if ( (plocinfo->locinfo != nullptr) &&
186
+ (plocinfo->locinfo->refcount == 0) &&
187
+ (plocinfo->locinfo != &__acrt_initial_locale_data) )
188
+ __acrt_free_locale(plocinfo->locinfo);
189
+ }
190
+ __finally
191
+ {
192
+ __acrt_unlock(__acrt_locale_lock);
193
+ }
194
+ }
195
+
196
+ _free_crt(plocinfo);
197
+ }
198
+ }
199
+
200
+ /*
201
+ * _locale_t _get_current_locale() -
202
+ * Gets the current locale setting.
203
+ *
204
+ * Purpose:
205
+ * Gets the current locale setting for this thread. Returns locale
206
+ * in form of _locale_t, which then can be used with other locale
207
+ * aware string functions.
208
+ *
209
+ */
210
+
211
+ _locale_t __cdecl _get_current_locale(void)
212
+ {
213
+ __acrt_ptd* const ptd = __acrt_getptd();
214
+
215
+ auto locale_copy(_calloc_crt_t(__crt_locale_pointers, 1));
216
+ if (!locale_copy)
217
+ return nullptr; // calloc will set errno
218
+
219
+ __acrt_update_thread_locale_data();
220
+ __acrt_update_thread_multibyte_data();
221
+
222
+ // No one can free the data pointed to by ptlocinfo while we're copying
223
+ // it, since we're copying this thread's ptlocinfo, which won't be updated
224
+ // during the copy. So there are no worries about it being freed from
225
+ // under us. We still need a lock while adding a reference for the new
226
+ // copy, though, because of the race condition found in _wsetlocale.
227
+ locale_copy.get()->locinfo = ptd->_locale_info;
228
+ locale_copy.get()->mbcinfo = ptd->_multibyte_info;
229
+ __acrt_lock_and_call(__acrt_locale_lock, [&]
230
+ {
231
+ __acrt_add_locale_ref(locale_copy.get()->locinfo);
232
+ });
233
+
234
+ __acrt_lock_and_call(__acrt_multibyte_cp_lock, [&]
235
+ {
236
+ InterlockedIncrement(&locale_copy.get()->mbcinfo->refcount);
237
+ });
238
+
239
+ return locale_copy.detach();
240
+ }
241
+
242
+ // Enclaves do not support dynamic locales, but they support reading the default one.
243
+ #ifndef _UCRT_ENCLAVE_BUILD
244
+
245
+ // This is used as the initializer if a category does not need initialization.
246
+ static int __cdecl no_op_initialize(__crt_locale_data*)
247
+ {
248
+ return 0;
249
+ }
250
+
251
+ void __cdecl __acrt_free_locale(__crt_locale_data*);
252
+
253
+ _CRT_LINKER_FORCE_INCLUDE(__acrt_locale_terminator);
254
+
255
+ extern __declspec(selectany) struct {
256
+ const wchar_t * catname;
257
+ wchar_t * locale;
258
+ int (* init)(__crt_locale_data*);
259
+ } const __lc_category[LC_MAX-LC_MIN+1] = {
260
+ /* code assumes locale initialization is "__wclocalestr" */
261
+ { L"LC_ALL", nullptr, no_op_initialize },
262
+ { L"LC_COLLATE", __acrt_wide_c_locale_string, no_op_initialize },
263
+ { L"LC_CTYPE", __acrt_wide_c_locale_string, __acrt_locale_initialize_ctype },
264
+ { L"LC_MONETARY",__acrt_wide_c_locale_string, __acrt_locale_initialize_monetary },
265
+ { L"LC_NUMERIC", __acrt_wide_c_locale_string, __acrt_locale_initialize_numeric },
266
+ { L"LC_TIME", __acrt_wide_c_locale_string, __acrt_locale_initialize_time }
267
+ };
268
+
269
+ /***
270
+ *
271
+ * _copytlocinfo_nolock(__crt_locale_data* ptlocid, __crt_locale_data* ptlocis)
272
+ *
273
+ * Purpose:
274
+ * Copy the contents of ptlocis to ptlocid and increase the refcount of all the
275
+ * elements in ptlocid after copy.
276
+ *
277
+ ******************************************************************************/
278
+ static void __cdecl _copytlocinfo_nolock(
279
+ __crt_locale_data* ptlocid,
280
+ __crt_locale_data* ptlocis)
281
+ {
282
+ if (ptlocis != nullptr && ptlocid != nullptr && ptlocid != ptlocis) {
283
+ *ptlocid = *ptlocis;
284
+ ptlocid->refcount = 0;
285
+ __acrt_add_locale_ref(ptlocid);
286
+ }
287
+ }
288
+
289
+ /***
290
+ * void sync_legacy_variables_lk()
291
+ *
292
+ * Purpose:
293
+ * Syncs all the legacy locale specific variables to the global locale.
294
+ *
295
+ *******************************************************************************/
296
+ static __inline void sync_legacy_variables_lk()
297
+ {
298
+ __acrt_lconv = __acrt_current_locale_data.value()->lconv;
299
+ _pctype = __acrt_current_locale_data.value()->_public._locale_pctype;
300
+ __mb_cur_max = __acrt_current_locale_data.value()->_public._locale_mb_cur_max;
301
+ }
302
+
303
+ /***
304
+ * _locale_t _wcreate_locale(int category, char *wlocale) -
305
+ * Set one or all locale categories of global locale structure
306
+ *
307
+ * Purpose:
308
+ * The _wcreate_locale() routine allows the user to create a _locale_t
309
+ * object that can be used with other locale functions.
310
+ *
311
+ * Entry:
312
+ * int category = One of the locale categories defined in locale.h
313
+ * char *locale = String identifying a specific locale.
314
+ *
315
+ * Exit:
316
+ * If supplied locale pointer != nullptr:
317
+ *
318
+ * If locale string is '\0', set locale to default.
319
+ *
320
+ * If desired setting can be honored, return a pointer to the
321
+ * locale string for the appropriate category.
322
+ *
323
+ * If desired setting can NOT be honored, return nullptr.
324
+ *
325
+ * Exceptions:
326
+ * Compound locale strings of the form "LC_COLLATE=xxx;LC_CTYPE=xxx;..."
327
+ * are allowed for the LC_ALL category. This is to support the ability
328
+ * to restore locales with the returned string, as specified by ANSI.
329
+ * Setting the locale with a compound locale string will succeed unless
330
+ * *all* categories failed. The returned string will reflect the current
331
+ * locale. For example, if LC_CTYPE fails in the above string, setlocale
332
+ * will return "LC_COLLATE=xxx;LC_CTYPE=yyy;..." where yyy is the
333
+ * previous locale (or the C locale if restoring the previous locale
334
+ * also failed). Unrecognized LC_* categories are ignored.
335
+ *
336
+ *******************************************************************************/
337
+
338
+ _locale_t __cdecl _wcreate_locale(
339
+ int _category,
340
+ const wchar_t *locale
341
+ )
342
+ {
343
+ if (_category < LC_MIN || _category > LC_MAX || locale == nullptr)
344
+ return nullptr;
345
+
346
+ auto result = _calloc_crt_t(__crt_locale_pointers, 1);
347
+ _VALIDATE_RETURN_NOEXC(result, ENOMEM, nullptr);
348
+
349
+ auto locale_data = _calloc_crt_t(__crt_locale_data, 1);
350
+ _VALIDATE_RETURN_NOEXC(locale_data, ENOMEM, nullptr);
351
+
352
+ auto multibyte_data = _calloc_crt_t(__crt_multibyte_data, 1);
353
+ _VALIDATE_RETURN_NOEXC(multibyte_data, ENOMEM, nullptr);
354
+
355
+ _copytlocinfo_nolock(locale_data.get(), &__acrt_initial_locale_data);
356
+
357
+ if (_wsetlocale_nolock(locale_data.get(), _category, locale) == nullptr ||
358
+ _setmbcp_nolock(locale_data.get()->_public._locale_lc_codepage, multibyte_data.get()) != 0)
359
+ {
360
+ __acrt_release_locale_ref(locale_data.get());
361
+ __acrt_free_locale(locale_data.detach());
362
+ return nullptr;
363
+ }
364
+
365
+ multibyte_data.get()->refcount = 1;
366
+
367
+ result.get()->locinfo = locale_data.detach();
368
+ result.get()->mbcinfo = multibyte_data.detach();
369
+ return result.detach();
370
+ }
371
+
372
+ /***
373
+ * _locale_t _create_locale(int category, char *locale) -
374
+ * Set one or all locale categories of global locale structure
375
+ ****/
376
+ _locale_t __cdecl _create_locale(
377
+ int _category,
378
+ const char *_locale
379
+ )
380
+ {
381
+ wchar_t _wlocale[MAX_LC_LEN];
382
+
383
+ /* Early input validation */
384
+ if ( (_category < LC_MIN) || (_category > LC_MAX) || _locale == nullptr)
385
+ return nullptr;
386
+
387
+ if ( __acrt_MultiByteToWideChar(CP_ACP, 0, _locale, -1, _wlocale, _countof(_wlocale)) == 0 )
388
+ { // conversion to wide char failed
389
+ return nullptr;
390
+ }
391
+
392
+ return _wcreate_locale(_category, _wlocale);
393
+ }
394
+
395
+ /*
396
+ *char * setlocale(int category, char *locale) - Set one or all locale categories
397
+ *
398
+ *Purpose:
399
+ * The setlocale() routine allows the user to set one or more of
400
+ * the locale categories to the specific locale selected by the
401
+ * user. [ANSI]
402
+ *
403
+ * NOTE: Under !_INTL, the C libraries only support the "C" locale.
404
+ * Attempts to change the locale will fail.
405
+ *
406
+ *Entry:
407
+ * int category = One of the locale categories defined in locale.h
408
+ * char *locale = String identifying a specific locale or nullptr to
409
+ * query the current locale.
410
+ *
411
+ *Exit:
412
+ * If supplied locale pointer == nullptr:
413
+ *
414
+ * Return pointer to current locale string and do NOT change
415
+ * the current locale.
416
+ *
417
+ * If supplied locale pointer != nullptr:
418
+ *
419
+ * If locale string is '\0', set locale to default.
420
+ *
421
+ * If desired setting can be honored, return a pointer to the
422
+ * locale string for the appropriate category.
423
+ *
424
+ * If desired setting can NOT be honored, return nullptr.
425
+ *
426
+ *Exceptions:
427
+ * Compound locale strings of the form "LC_COLLATE=xxx;LC_CTYPE=xxx;..."
428
+ * are allowed for the LC_ALL category. This is to support the ability
429
+ * to restore locales with the returned string, as specified by ANSI.
430
+ * Setting the locale with a compound locale string will succeed unless
431
+ * *all* categories failed. The returned string will reflect the current
432
+ * locale. For example, if LC_CTYPE fails in the above string, setlocale
433
+ * will return "LC_COLLATE=xxx;LC_CTYPE=yyy;..." where yyy is the
434
+ * previous locale (or the C locale if restoring the previous locale
435
+ * also failed). Unrecognized LC_* categories are ignored.
436
+ *
437
+ */
438
+
439
+ wchar_t * __cdecl _wsetlocale (
440
+ int _category,
441
+ const wchar_t *_wlocale
442
+ )
443
+ {
444
+ wchar_t * retval=nullptr;
445
+ __crt_locale_data* ptloci = nullptr;
446
+
447
+ /* Validate category */
448
+ _VALIDATE_RETURN(LC_MIN <= _category && _category <= LC_MAX, EINVAL, nullptr);
449
+
450
+ __acrt_ptd* const ptd = __acrt_getptd();
451
+
452
+ // Deadlock Avoidance: When a new thread is created in the process, we
453
+ // create a new PTD for the thread. The PTD initialization function is
454
+ // called under the loader lock. This initialization function will also
455
+ // acquire the locale lock in order to acquire a reference to the current
456
+ // global locale for the new thread.
457
+ //
458
+ // Some of the locale APIs are not available on all supported target OSes.
459
+ // We dynamically obtain these libraries via LoadLibrary/GetProcAddress.
460
+ // We must ensure that no call to LoadLibrary is made while we hold the
461
+ // locale lock, lest we deadlock due to lock order inversion between the
462
+ // loader lock and the locale lock.
463
+ //
464
+ // This function call here will ensure that any required modules are loaded
465
+ // before we acquire the locale lock.
466
+ __acrt_eagerly_load_locale_apis();
467
+
468
+ __acrt_update_thread_locale_data();
469
+
470
+ // Prevent global locale resynchronization - we call things like stricmp() further down
471
+ // without passing our _locale_t which would trigger a resynchronization.
472
+ // Use _WSETLOCALE_AVOID_SYNC_LOCALE_BIT to avoid interfering with other locale settings
473
+ // (see _configthreadlocale() for details).
474
+
475
+ // This may not be necessary anymore and may be a hold-over from when _wsetlocale called
476
+ // setlocale instead of the other way around.
477
+ // (MSFT:20394962 - Investigate whether _WSETLOCALE_AVOID_SYNC_LOCALE_BIT is needed)
478
+ __acrt_disable_global_locale_sync(ptd, _WSETLOCALE_AVOID_SYNC_LOCALE_BIT);
479
+
480
+ __crt_call_and_cleanup([&]
481
+ {
482
+ if ((ptloci = _calloc_crt_t(__crt_locale_data, 1).detach()) != nullptr)
483
+ {
484
+ __acrt_lock_and_call(__acrt_locale_lock, [&]
485
+ {
486
+ _copytlocinfo_nolock(ptloci, ptd->_locale_info);
487
+
488
+ if ((retval = _wsetlocale_nolock(ptloci, _category, _wlocale)) != 0)
489
+ {
490
+ // If no call has been made to setlocale to change locale from "C" locale
491
+ // to some other locale, we keep locale_changed = 0. Other functions that
492
+ // depend on locale use this variable to optimize performance for C locale
493
+ // which is normally the case in applications.
494
+ if (_wlocale != nullptr)
495
+ {
496
+ if (wcscmp(_wlocale, __acrt_wide_c_locale_string) != 0)
497
+ {
498
+ __acrt_set_locale_changed();
499
+ }
500
+ // If it is again set to the C locale, could we not retain that state?
501
+ // else
502
+ // {
503
+ // __acrt_set_locale_unchanged();
504
+ // }
505
+ }
506
+
507
+ (void)_updatetlocinfoEx_nolock(&ptd->_locale_info, ptloci);
508
+ __acrt_release_locale_ref(ptloci);
509
+ // Note that after incrementing _own_locale, if this thread doesn't
510
+ // have its own locale, _own_locale variable should be 1.
511
+
512
+ // Do not use __acrt_should_sync_with_global_locale() because
513
+ // _WSETLOCALE_AVOID_SYNC_LOCALE_BIT will interfere.
514
+ if (!(ptd->_own_locale & _PER_THREAD_LOCALE_BIT) &&
515
+ !(__globallocalestatus & _GLOBAL_LOCALE_BIT))
516
+ {
517
+ (void)_updatetlocinfoEx_nolock(&__acrt_current_locale_data.value(), ptd->_locale_info);
518
+ sync_legacy_variables_lk();
519
+ }
520
+ }
521
+ else
522
+ {
523
+ __acrt_release_locale_ref(ptloci);
524
+ __acrt_free_locale(ptloci);
525
+ }
526
+ });
527
+ }
528
+ },
529
+ [&]{ __acrt_enable_global_locale_sync(ptd, _WSETLOCALE_AVOID_SYNC_LOCALE_BIT); });
530
+
531
+ return retval;
532
+ }
533
+
534
+ static wchar_t * __cdecl _wsetlocale_nolock(
535
+ __crt_locale_data* ploci,
536
+ int _category,
537
+ const wchar_t *_wlocale
538
+ )
539
+ {
540
+ wchar_t * retval;
541
+ /* Interpret locale */
542
+
543
+ if (_category != LC_ALL)
544
+ {
545
+ retval = (_wlocale) ? _wsetlocale_set_cat(ploci, _category,_wlocale) :
546
+ ploci->lc_category[_category].wlocale;
547
+
548
+ } else { /* LC_ALL */
549
+ wchar_t lctemp[MAX_LC_LEN];
550
+ int i;
551
+ int same = 1;
552
+ int fLocaleSet = 0; /* flag to indicate if anything successfully set */
553
+
554
+ if (_wlocale != nullptr)
555
+ {
556
+ if ( (_wlocale[0]==L'L') && (_wlocale[1]==L'C') && (_wlocale[2]==L'_') )
557
+ {
558
+ /* parse compound locale string */
559
+ size_t len;
560
+ const wchar_t * p = _wlocale; /* start of string to parse */
561
+ const wchar_t * s;
562
+
563
+ do {
564
+ s = wcspbrk(p, L"=;");
565
+
566
+ if (s == nullptr || (len=(size_t)(s - p)) == 0 || (*s == L';'))
567
+ return nullptr; /* syntax error */
568
+
569
+ /* match with known LC_ strings, if possible, else ignore */
570
+ for (i=LC_ALL+1; i<=LC_MAX; i++)
571
+ {
572
+ if ((!wcsncmp(__lc_category[i].catname,p,len))
573
+ && (len==wcslen(__lc_category[i].catname)))
574
+ {
575
+ break; /* matched i */
576
+ }
577
+ } /* no match if (i>LC_MAX) -- just ignore */
578
+
579
+ if ((len = wcscspn(++s, L";")) == 0 && *s != L';')
580
+ return nullptr; /* syntax error */
581
+
582
+ if (i<=LC_MAX)
583
+ {
584
+ _ERRCHECK(wcsncpy_s(lctemp, _countof(lctemp), s, len));
585
+ lctemp[len]=L'\0'; /* null terminate string */
586
+
587
+ /* don't fail unless all categories fail */
588
+ if (_wsetlocale_set_cat(ploci, i,lctemp))
589
+ fLocaleSet++; /* record a success */
590
+ }
591
+ if (*(p = s+len)!=L'\0')
592
+ p++; /* skip ';', if present */
593
+
594
+ } while (*p);
595
+
596
+ retval = (fLocaleSet) ? _wsetlocale_get_all(ploci) : nullptr;
597
+
598
+ } else { /* simple LC_ALL locale string */
599
+
600
+ UINT code_page;
601
+ wchar_t localeNameTemp[LOCALE_NAME_MAX_LENGTH];
602
+ /* confirm locale is supported, get expanded locale */
603
+ retval = _expandlocale(_wlocale, lctemp, _countof(lctemp), localeNameTemp, _countof(localeNameTemp), code_page);
604
+ if (retval != 0)
605
+ {
606
+ for (i=LC_MIN; i<=LC_MAX; i++)
607
+ {
608
+ if (i!=LC_ALL)
609
+ {
610
+ if (wcscmp(lctemp, ploci->lc_category[i].wlocale) != 0)
611
+ { // does not match the LC_ALL locale
612
+ if (_wsetlocale_set_cat(ploci, i, lctemp))
613
+ {
614
+ fLocaleSet++; /* record a success */
615
+ }
616
+ else
617
+ {
618
+ same = 0; /* record a failure */
619
+ }
620
+ }
621
+ else
622
+ fLocaleSet++; /* trivial succcess */
623
+ }
624
+ }
625
+ if (same) /* needn't call setlocale_get_all() if all the same */
626
+ {
627
+ retval = _wsetlocale_get_all(ploci);
628
+ /* retval set above */
629
+ }
630
+ else
631
+ retval = (fLocaleSet) ? _wsetlocale_get_all(ploci) : nullptr;
632
+ }
633
+ }
634
+ } else { /* LC_ALL & nullptr */
635
+ retval = _wsetlocale_get_all (ploci);
636
+ }
637
+ }
638
+
639
+ /* common exit point */
640
+ return retval;
641
+ } /* setlocale */
642
+
643
+
644
+ static wchar_t * __cdecl _wsetlocale_set_cat (
645
+ __crt_locale_data* ploci,
646
+ int category,
647
+ const wchar_t * wlocale
648
+ )
649
+ {
650
+ wchar_t * oldlocale;
651
+ wchar_t * oldlocalename;
652
+ UINT oldcodepage;
653
+
654
+ UINT cptemp;
655
+ wchar_t lctemp[MAX_LC_LEN];
656
+ wchar_t localeNameString[LOCALE_NAME_MAX_LENGTH];
657
+ wchar_t * pch = nullptr;
658
+ wchar_t * pch_cat_locale = nullptr;
659
+ size_t cch = 0;
660
+ unsigned short out[sizeof(_first_127char)];
661
+ int i;
662
+ __acrt_ptd* const ptd = __acrt_getptd();
663
+ __crt_ctype_compatibility_data* _Loc_c = ptd->_setloc_data._Loc_c; // __setloc_data._Loc_c is array
664
+ int _LOC_CCACHE = _countof(ptd->_setloc_data._Loc_c);
665
+ __crt_ctype_compatibility_data buf1, buf2;
666
+
667
+ if (!_expandlocale(wlocale, lctemp, _countof(lctemp), localeNameString, _countof(localeNameString), cptemp))
668
+ return nullptr;
669
+
670
+ // if this category's locale hadn't changed
671
+ if (wcscmp(lctemp, ploci->lc_category[category].wlocale) == 0)
672
+ {
673
+ return ploci->lc_category[category].wlocale;
674
+ }
675
+
676
+ cch = wcslen(lctemp) + 1;
677
+ if ((pch = static_cast<wchar_t*>(_malloc_crt(sizeof(int) + (cch * sizeof(wchar_t))))) == nullptr)
678
+ return nullptr; /* error if malloc fails */
679
+
680
+ pch_cat_locale = pch + (sizeof(int) / sizeof(wchar_t));
681
+
682
+ /* save for possible restore */
683
+ oldlocale = ploci->lc_category[category].wlocale;
684
+ oldlocalename = ploci->locale_name[category];
685
+ oldcodepage = ploci->_public._locale_lc_codepage;
686
+
687
+ /* update locale string */
688
+ _ERRCHECK(wcscpy_s(pch_cat_locale, cch, lctemp));
689
+ ploci->lc_category[category].wlocale = pch_cat_locale;
690
+
691
+ /* Copy locale name */
692
+ if (lctemp[0] == L'C' && lctemp[1] == L'\x0') // if "C" locale
693
+ ploci->locale_name[category] = nullptr;
694
+ else
695
+ ploci->locale_name[category] = __acrt_copy_locale_name(localeNameString);
696
+
697
+ /* To speedup locale based comparisions, we identify if the current
698
+ * local has first 127 character set same as CLOCALE. If yes then
699
+ * ploci->lc_clike = TRUE.
700
+ */
701
+
702
+ if (category==LC_CTYPE)
703
+ {
704
+ ploci->_public._locale_lc_codepage = cptemp;
705
+ buf1 = _Loc_c[_LOC_CCACHE -1];
706
+ /* brings the recently used codepage to the top. or else shifts
707
+ * every thing down by one so that new _Loc_c can be placed at
708
+ * the top.
709
+ */
710
+ for ( i = 0; i < _LOC_CCACHE; i++)
711
+ {
712
+ if (ploci->_public._locale_lc_codepage == _Loc_c[i].id)
713
+ {
714
+ /* We don't really want to swap cache around in case what we are looking
715
+ * for is the first element of the cache
716
+ */
717
+ if (i!=0)
718
+ {
719
+ _Loc_c[0] = _Loc_c[i];
720
+ _Loc_c[i] = buf1;
721
+ }
722
+ break;
723
+ }
724
+ else
725
+ {
726
+ buf2 = _Loc_c[i];
727
+ _Loc_c[i] = buf1;
728
+ buf1 = buf2;
729
+ }
730
+ }
731
+ if ( i == _LOC_CCACHE)
732
+ {
733
+ if ( __acrt_GetStringTypeA(nullptr, CT_CTYPE1,
734
+ _first_127char,
735
+ sizeof(_first_127char),
736
+ out,
737
+ ploci->_public._locale_lc_codepage,
738
+ TRUE ))
739
+ {
740
+ int j;
741
+ for ( j = 0; j < sizeof(_first_127char); j++)
742
+ out[j] = out[j]&
743
+ (_UPPER|_LOWER|_DIGIT|_SPACE|_PUNCT|_CONTROL|_BLANK|_HEX|_ALPHA);
744
+ if ( !memcmp(out, _ctype_loc_style, (sizeof(_first_127char)/sizeof(char))*sizeof(short)))
745
+ {
746
+ _Loc_c[0].is_clike = TRUE;
747
+ }
748
+ else
749
+ {
750
+ _Loc_c[0].is_clike = FALSE;
751
+ }
752
+ }
753
+ else
754
+ _Loc_c[0].is_clike = FALSE;
755
+ _Loc_c[0].id = ploci->_public._locale_lc_codepage;
756
+ }
757
+ ploci->lc_clike = _Loc_c[0].is_clike;
758
+ } /* category==LC_CTYPE */
759
+ else if ( category == LC_COLLATE )
760
+ ploci->lc_collate_cp = cptemp;
761
+ else if ( category == LC_TIME )
762
+ ploci->lc_time_cp = cptemp;
763
+
764
+ if (__lc_category[category].init(ploci) != 0)
765
+ {
766
+ /* restore previous state */
767
+ ploci->lc_category[category].wlocale = oldlocale;
768
+ _free_crt(ploci->locale_name[category]);
769
+ ploci->locale_name[category] = oldlocalename;
770
+ _free_crt(pch);
771
+ ploci->_public._locale_lc_codepage = oldcodepage;
772
+
773
+ return nullptr; /* error if non-zero return */
774
+ }
775
+
776
+ /* locale set up successfully */
777
+ /* Cleanup */
778
+ if ((oldlocale != __acrt_wide_c_locale_string) &&
779
+ (InterlockedDecrement(ploci->lc_category[category].wrefcount) == 0)
780
+ )
781
+ {
782
+ _ASSERT(0);
783
+ _free_crt(ploci->lc_category[category].wrefcount);
784
+ _free_crt(ploci->lc_category[category].refcount);
785
+ _free_crt(ploci->locale_name[category]);
786
+ ploci->lc_category[category].wlocale = nullptr;
787
+ ploci->locale_name[category] = nullptr;
788
+ }
789
+ if (pch)
790
+ {
791
+ reinterpret_cast<long&>(*pch) = 1;
792
+ }
793
+ ploci->lc_category[category].wrefcount = reinterpret_cast<long*>(pch);
794
+
795
+ return ploci->lc_category[category].wlocale;
796
+ } /* _wsetlocale_set_cat */
797
+
798
+
799
+
800
+ static wchar_t * __cdecl _wsetlocale_get_all ( __crt_locale_data* ploci)
801
+ {
802
+ int i;
803
+ int same = 1;
804
+ wchar_t *pch = nullptr;
805
+ size_t cch = 0;
806
+ long *refcount = nullptr;
807
+ size_t refcountSize = 0;
808
+
809
+ /* allocate memory if necessary */
810
+ refcountSize = sizeof(int) + (sizeof(wchar_t) * (MAX_LC_LEN+1) * (LC_MAX-LC_MIN+1)) + (sizeof(wchar_t) * CATNAMES_LEN);
811
+ if ( (refcount = static_cast<long*>(_malloc_crt(refcountSize))) == nullptr)
812
+ return nullptr;
813
+
814
+ pch = ((wchar_t*)refcount) + (sizeof(int) / sizeof(wchar_t));
815
+ cch = (refcountSize - sizeof(int)) / sizeof(wchar_t);
816
+ *pch = L'\0';
817
+ *refcount = 1;
818
+ for (i=LC_MIN+1; ; i++)
819
+ {
820
+ _wcscats(pch, cch, 3, __lc_category[i].catname, L"=", ploci->lc_category[i].wlocale);
821
+ if (i<LC_MAX)
822
+ {
823
+ _ERRCHECK(wcscat_s(pch, cch, L";"));
824
+ if (wcscmp(ploci->lc_category[i].wlocale, ploci->lc_category[i+1].wlocale))
825
+ same=0;
826
+ }
827
+ else
828
+ {
829
+ if (!same) {
830
+ if (ploci->lc_category[LC_ALL].wrefcount != nullptr &&
831
+ InterlockedDecrement(ploci->lc_category[LC_ALL].wrefcount) == 0) {
832
+ _ASSERT(0);
833
+ _free_crt(ploci->lc_category[LC_ALL].wrefcount);
834
+ }
835
+ if (ploci->lc_category[LC_ALL].refcount != nullptr &&
836
+ InterlockedDecrement(ploci->lc_category[LC_ALL].refcount) == 0) {
837
+ _ASSERT(0);
838
+ _free_crt(ploci->lc_category[LC_ALL].refcount);
839
+ }
840
+ ploci->lc_category[LC_ALL].refcount = nullptr;
841
+ ploci->lc_category[LC_ALL].locale = nullptr;
842
+ ploci->lc_category[LC_ALL].wrefcount = refcount;
843
+ return ploci->lc_category[LC_ALL].wlocale = pch;
844
+ } else {
845
+ _free_crt(refcount);
846
+ if (ploci->lc_category[LC_ALL].wrefcount != nullptr &&
847
+ InterlockedDecrement(ploci->lc_category[LC_ALL].wrefcount) == 0) {
848
+ _ASSERT(0);
849
+ _free_crt(ploci->lc_category[LC_ALL].wrefcount);
850
+ }
851
+ if (ploci->lc_category[LC_ALL].refcount != nullptr &&
852
+ InterlockedDecrement(ploci->lc_category[LC_ALL].refcount) == 0) {
853
+ _ASSERT(0);
854
+ _free_crt(ploci->lc_category[LC_ALL].refcount);
855
+ }
856
+ ploci->lc_category[LC_ALL].refcount = nullptr;
857
+ ploci->lc_category[LC_ALL].locale = nullptr;
858
+ ploci->lc_category[LC_ALL].wrefcount = nullptr;
859
+ ploci->lc_category[LC_ALL].wlocale = nullptr;
860
+ return ploci->lc_category[LC_CTYPE].wlocale;
861
+ }
862
+ }
863
+ }
864
+ } /* _wsetlocale_get_all */
865
+
866
+ // --- BCP-47 tag parsing for _expandlocale() ---
867
+ //
868
+ // Provides parse_bcp47() which parses a given locale expression and updates a __crt_locale_strings structure accordingly.
869
+ // Unsupported:
870
+ // * Extension and private tags
871
+ // * 4-8 character language subtags
872
+ // * Grandfathered tags
873
+ // * Lone code page specifiers (POSIX extension for BCP-47)
874
+
875
+ enum class _bcp47_section_delimiter
876
+ {
877
+ normal,
878
+ end_of_string,
879
+ code_page
880
+ };
881
+
882
+ struct _bcp47_section
883
+ {
884
+ wchar_t const * ptr;
885
+ size_t length;
886
+ _bcp47_section_delimiter delimiter;
887
+ };
888
+
889
+ static _bcp47_section_delimiter categorize_delimiter(wchar_t const wc)
890
+ {
891
+ switch (wc)
892
+ {
893
+ case '-':
894
+ case '_':
895
+ return _bcp47_section_delimiter::normal;
896
+ case '.':
897
+ return _bcp47_section_delimiter::code_page;
898
+ case '\0':
899
+ default:
900
+ return _bcp47_section_delimiter::end_of_string;
901
+ }
902
+ }
903
+
904
+ static bool string_is_alpha(wchar_t const * const str, size_t const len)
905
+ {
906
+ for (size_t i = 0; i < len; ++i)
907
+ {
908
+ if (!__ascii_isalpha(str[i]))
909
+ {
910
+ return false;
911
+ }
912
+ }
913
+ return true;
914
+ }
915
+
916
+ static bool string_is_digit(wchar_t const * const str, size_t const len)
917
+ {
918
+ for (size_t i = 0; i < len; ++i)
919
+ {
920
+ if (!__ascii_isdigit(str[i]))
921
+ {
922
+ return false;
923
+ }
924
+ }
925
+ return true;
926
+ }
927
+
928
+ static bool parse_bcp47_language(__crt_locale_strings * const names, _bcp47_section const& section)
929
+ {
930
+ if (section.delimiter != _bcp47_section_delimiter::normal)
931
+ {
932
+ return false;
933
+ }
934
+
935
+ if (section.length < 2 || section.length > 3)
936
+ {
937
+ return false; // Failure, only 2-3 letter language codes permitted.
938
+ }
939
+
940
+ if (!string_is_alpha(section.ptr, section.length))
941
+ {
942
+ return false;
943
+ }
944
+
945
+ _ERRCHECK(wcsncpy_s(names->szLanguage, _countof(names->szLanguage), section.ptr, section.length));
946
+ _ERRCHECK(wcsncpy_s(names->szLocaleName, _countof(names->szLocaleName), section.ptr, section.length));
947
+ return true;
948
+ }
949
+
950
+ static bool parse_bcp47_script(__crt_locale_strings * const names, _bcp47_section const& section)
951
+ {
952
+ if (section.delimiter != _bcp47_section_delimiter::normal)
953
+ {
954
+ return false;
955
+ }
956
+
957
+ if (section.length != 4) {
958
+ return false; // Failure, only 4 letter script codes permitted.
959
+ }
960
+
961
+ if (!string_is_alpha(section.ptr, section.length))
962
+ {
963
+ return false;
964
+ }
965
+
966
+ _ERRCHECK(wcsncat_s(names->szLocaleName, _countof(names->szLocaleName), L"-", 1));
967
+ _ERRCHECK(wcsncat_s(names->szLocaleName, _countof(names->szLocaleName), section.ptr, section.length));
968
+ return true;
969
+ }
970
+
971
+ static bool parse_bcp47_region(__crt_locale_strings * const names, _bcp47_section const& section)
972
+ {
973
+ if (section.delimiter != _bcp47_section_delimiter::normal)
974
+ {
975
+ return false;
976
+ }
977
+
978
+ if ( !(section.length == 2 && string_is_alpha(section.ptr, section.length)) // if not 2 letters
979
+ && !(section.length == 3 && string_is_digit(section.ptr, section.length))) // and not 3 digits
980
+ {
981
+ return false;
982
+ }
983
+
984
+ _ERRCHECK(wcsncpy_s(names->szCountry, _countof(names->szCountry), section.ptr, section.length));
985
+ _ERRCHECK(wcsncat_s(names->szLocaleName, _countof(names->szLocaleName), L"-", 1));
986
+ _ERRCHECK(wcsncat_s(names->szLocaleName, _countof(names->szLocaleName), section.ptr, section.length));
987
+ return true;
988
+ }
989
+
990
+ static bool parse_bcp47_code_page(__crt_locale_strings * const names, _bcp47_section const& section)
991
+ {
992
+ if (section.delimiter != _bcp47_section_delimiter::code_page)
993
+ {
994
+ return false;
995
+ }
996
+
997
+ _ERRCHECK(wcsncpy_s(names->szCodePage, _countof(names->szCodePage), section.ptr, section.length));
998
+ return true; // Success
999
+ }
1000
+
1001
+ static bool parse_bcp47(__crt_locale_strings * const names, const wchar_t * const expr)
1002
+ {
1003
+ wchar_t const * p = expr;
1004
+ wchar_t const * const delimiters = L"-_.";
1005
+
1006
+ memset(names, 0, sizeof(__crt_locale_strings));
1007
+
1008
+ size_t const max_sections = 4;
1009
+ _bcp47_section sections[max_sections];
1010
+ size_t num_sections = 0;
1011
+
1012
+ for (auto last_delimiter = _bcp47_section_delimiter::normal;
1013
+ last_delimiter != _bcp47_section_delimiter::end_of_string;
1014
+ last_delimiter = categorize_delimiter(*p++)
1015
+ )
1016
+ {
1017
+ if (num_sections >= max_sections)
1018
+ {
1019
+ // Didn't reach end of string before running out of sections to parse.
1020
+ return false;
1021
+ }
1022
+
1023
+ size_t const section_length = (last_delimiter != _bcp47_section_delimiter::code_page)
1024
+ ? wcscspn(p, delimiters) : wcslen(p);
1025
+
1026
+ sections[num_sections].ptr = p;
1027
+ sections[num_sections].length = section_length;
1028
+ sections[num_sections].delimiter = last_delimiter;
1029
+
1030
+ p += section_length;
1031
+ ++num_sections;
1032
+ }
1033
+
1034
+ switch (num_sections)
1035
+ {
1036
+ case 1:
1037
+ // en
1038
+ return parse_bcp47_language(names, sections[0]);
1039
+ case 2:
1040
+ // en-US
1041
+ // zh-Hans
1042
+ // en.utf-8
1043
+ // .utf-8
1044
+ return parse_bcp47_language(names, sections[0])
1045
+ && ( parse_bcp47_script(names, sections[1])
1046
+ || parse_bcp47_region(names, sections[1])
1047
+ || parse_bcp47_code_page(names, sections[1])
1048
+ );
1049
+ case 3:
1050
+ // en-US.utf-8
1051
+ // zh-Hans-HK
1052
+ return parse_bcp47_language(names, sections[0])
1053
+ && ( parse_bcp47_script(names, sections[1]) && (parse_bcp47_region(names, sections[2]) || parse_bcp47_code_page(names, sections[2]))
1054
+ || (parse_bcp47_region(names, sections[1]) && parse_bcp47_code_page(names, sections[2]))
1055
+ );
1056
+ case 4:
1057
+ // zh-Hans-HK.utf-8
1058
+ return parse_bcp47_language(names, sections[0])
1059
+ && parse_bcp47_script(names, sections[1])
1060
+ && parse_bcp47_region(names, sections[2])
1061
+ && parse_bcp47_code_page(names, sections[3]);
1062
+ default:
1063
+ return false;
1064
+ }
1065
+ }
1066
+
1067
+ // Utility functions/structs for _expandlocale().
1068
+
1069
+ static int get_default_code_page(wchar_t const * const valid_windows_locale_name)
1070
+ {
1071
+ int code_page{};
1072
+
1073
+ int const size_written = __acrt_GetLocaleInfoEx(
1074
+ valid_windows_locale_name,
1075
+ LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
1076
+ reinterpret_cast<LPWSTR>(&code_page),
1077
+ sizeof(int) / sizeof(wchar_t)
1078
+ );
1079
+
1080
+ if (size_written == 0 || code_page == 0)
1081
+ { // Default to UTF-8 if could not find or no default code page.
1082
+ return CP_UTF8;
1083
+ }
1084
+
1085
+ return code_page;
1086
+ }
1087
+
1088
+ class _expandlocale_locale_name_cache
1089
+ {
1090
+ // Scope guard to keep invariants in _expandlocale for the localeNameOutput and _cacheLocaleName variables.
1091
+ // The cacheLocaleName/localeNameOutput is the only cache where work is done progressively in the cache value,
1092
+ // (inside __acrt_get_qualified_locale and __acrt_get_qualified_locale_downlevel)
1093
+ // so we must save/restore at function start and invalidate the cache once we have stuff to commit to both.
1094
+ public:
1095
+ _expandlocale_locale_name_cache(
1096
+ wchar_t * const localeNameOutput,
1097
+ size_t const localeNameSizeInChars,
1098
+ __crt_qualified_locale_data * const psetloc_data
1099
+ ) : _localeNameOutput(localeNameOutput),
1100
+ _localeNameSizeInChars(localeNameSizeInChars),
1101
+ _psetloc_data(psetloc_data),
1102
+ _committed(false)
1103
+ {
1104
+ _ERRCHECK(wcsncpy_s(localeNameOutput, localeNameSizeInChars, psetloc_data->_cacheLocaleName, _countof(psetloc_data->_cacheLocaleName)));
1105
+ }
1106
+
1107
+ ~_expandlocale_locale_name_cache()
1108
+ {
1109
+ if (!_committed)
1110
+ {
1111
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), _localeNameOutput, _localeNameSizeInChars));
1112
+ }
1113
+ }
1114
+
1115
+ _expandlocale_locale_name_cache(_expandlocale_locale_name_cache const&) = delete;
1116
+ _expandlocale_locale_name_cache& operator=(_expandlocale_locale_name_cache const&) = delete;
1117
+
1118
+ void commit_locale_name(wchar_t const * const new_locale_name, size_t const new_locale_name_length)
1119
+ {
1120
+ _ERRCHECK(wcsncpy_s(_psetloc_data->_cacheLocaleName, _countof(_psetloc_data->_cacheLocaleName), new_locale_name, new_locale_name_length));
1121
+ commit_locale_name_cache_already_updated(new_locale_name, new_locale_name_length);
1122
+ }
1123
+
1124
+ void commit_locale_name_cache_already_updated(wchar_t const * const new_locale_name, size_t const new_locale_name_length)
1125
+ {
1126
+ _ERRCHECK(wcsncpy_s(_localeNameOutput, _localeNameSizeInChars, new_locale_name, new_locale_name_length));
1127
+ _committed = true;
1128
+ }
1129
+
1130
+ private:
1131
+ wchar_t * _localeNameOutput;
1132
+ size_t _localeNameSizeInChars;
1133
+ __crt_qualified_locale_data * _psetloc_data;
1134
+ bool _committed;
1135
+ };
1136
+
1137
+ wchar_t * _expandlocale(
1138
+ wchar_t const * const expr,
1139
+ wchar_t * const output,
1140
+ size_t const sizeInChars,
1141
+ wchar_t * const localeNameOutput,
1142
+ size_t const localeNameSizeInChars,
1143
+ UINT& output_code_page
1144
+ )
1145
+ {
1146
+ // Returns: locale name to return to user (lifetime bound to PTD variable _cacheout)
1147
+ // Out Parameters:
1148
+ // * output: locale name to return to user (lifetime bound to supplied buffer)
1149
+ // * localeNameOutput: normalized locale name to be used internally (lifetime bound to supplied buffer)
1150
+ // * output_code_page: code page used
1151
+ //
1152
+ // Effects:
1153
+ // Parses an input locale string and returns the string that will replicate the effects just taken (output), the string to be used
1154
+ // for Win32 calls (localeNameOutput), and the code page.
1155
+ // Note that there are three modes here:
1156
+ // * Legacy and Windows Locale Names:
1157
+ // The output locale string and internally used locale string become normalized for Win32 APIs.
1158
+ // Neither necessarily match the user input locale.
1159
+ // Uses ACP as default if Windows does not know the code page.
1160
+ // * Permissive Windows locale names:
1161
+ // Ask Windows whether full given user string is a known locale name.
1162
+ // In that case, the output locale string, internally used locale string, and user input locale all match.
1163
+ // Uses UTF-8 as default if Windows does not know the code page.
1164
+ // * BCP-47 Locale Names:
1165
+ // Windows doesn't recognize BCP-47 + code page specifiers, but we must support them for proper UTF-8 support.
1166
+ // The output locale name must include the code page, but internally we modify the format so Windows understands it.
1167
+ // Uses UTF-8 as default if Windows does not know the code page.
1168
+ //
1169
+ // Also populates _psetloc_data structure from the PTD:
1170
+ // Caching Internals:
1171
+ // * _cachein: Caches last successful 'expr'.
1172
+ // * _cacheout: Caches last successful 'output' (which is also the return value).
1173
+ // * _cachecp: Caches last successful 'output_code_page'.
1174
+ // * _cacheLocaleName: Caches last successful 'localeNameOutput'.
1175
+ // Legacy Internals (used by __acrt_get_qualified_locale and __acrt_get_qualified_locale_downlevel only):
1176
+ // * pchLanguage: Pointer to start of language name, ex: "English"
1177
+ // * pchCountry: Pointer to start of country/region name, ex: "United States"
1178
+ // * iLocState: Used to record the match degree for locales checked (i.e. language and region match vs just language match)
1179
+ // * bAbbrevLanguage: Whether language name is a three-letter short-form, ex: "ENU"
1180
+ // * bAbbrevCountry: Whether country/region name is a three letter short-form, ex: "USA"
1181
+ // * iPrimaryLen: Length of pchLanguage portion of locale string
1182
+ // Also in _psetloc_data, but not used in _expandlocale: _Loc_c
1183
+
1184
+ if (!expr)
1185
+ {
1186
+ return nullptr; // error if no input
1187
+ }
1188
+
1189
+ // C locale
1190
+ // Callers know that LocaleNameOutput has not been updated and check for "C" locale before using it.
1191
+ if (expr[0] == L'C' && expr[1] == L'\0')
1192
+ {
1193
+ _ERRCHECK(wcscpy_s(output, sizeInChars, L"C"));
1194
+ output_code_page = CP_ACP;
1195
+ return output;
1196
+ }
1197
+
1198
+ __crt_qualified_locale_data * const _psetloc_data = &__acrt_getptd()->_setloc_data;
1199
+ UINT * const pcachecp = &_psetloc_data->_cachecp;
1200
+ wchar_t * const cachein = _psetloc_data->_cachein;
1201
+ size_t const cacheinLen = _countof(_psetloc_data->_cachein);
1202
+ wchar_t * const cacheout = _psetloc_data->_cacheout;
1203
+ size_t const cacheoutLen = _countof(_psetloc_data->_cacheout);
1204
+ size_t charactersInExpression = 0;
1205
+ int iCodePage = 0;
1206
+
1207
+ // This guard owns access to localeNameOutput. It expresses the invariants that localeNameOutput and _cacheLocaleName have.
1208
+ _expandlocale_locale_name_cache locale_name_guard(localeNameOutput, localeNameSizeInChars, _psetloc_data);
1209
+
1210
+ // First, make sure we didn't just do this one
1211
+ charactersInExpression = wcslen(expr);
1212
+ if (charactersInExpression >= MAX_LC_LEN || // we would never have cached this
1213
+ (wcscmp(cacheout, expr) && wcscmp(cachein, expr)))
1214
+ {
1215
+ __crt_locale_strings names;
1216
+ BOOL getqloc_results = FALSE;
1217
+ BOOL const isDownlevel = !__acrt_can_use_vista_locale_apis();
1218
+
1219
+ // Check if can parse as Legacy/Windows locale name
1220
+ if (__lc_wcstolc(&names, expr) == 0)
1221
+ {
1222
+ if (isDownlevel)
1223
+ {
1224
+ getqloc_results = __acrt_get_qualified_locale_downlevel(&names, pcachecp, &names);
1225
+ }
1226
+ else
1227
+ {
1228
+ getqloc_results = __acrt_get_qualified_locale(&names, pcachecp, &names);
1229
+ }
1230
+ }
1231
+
1232
+ // If successfully parsed as Legacy/Windows name
1233
+ if (getqloc_results)
1234
+ {
1235
+ // Legacy/Windows name resolution returns applies normalization to both returned and internally used locale names.
1236
+ __lc_lctowcs(cacheout, cacheoutLen, &names);
1237
+
1238
+ // __acrt_get_qualified_locale and __acrt_get_qualified_locale_downlevel update _cacheLocaleName - if successful commit locale changes
1239
+ locale_name_guard.commit_locale_name_cache_already_updated(names.szLocaleName, wcslen(names.szLocaleName) + 1);
1240
+ }
1241
+ else if (__acrt_IsValidLocaleName(expr))
1242
+ {
1243
+ // Windows recognizes the locale expression - only work is to grab the code page to be used.
1244
+ iCodePage = get_default_code_page(expr);
1245
+
1246
+ // Commit code page
1247
+ *pcachecp = static_cast<WORD>(iCodePage);
1248
+
1249
+ // Commit locale name that we will return to the user (same as input locale string).
1250
+ _ERRCHECK(wcsncpy_s(cacheout, cacheoutLen, expr, charactersInExpression + 1));
1251
+
1252
+ // Commit locale name for internal use (same as input locale string).
1253
+ locale_name_guard.commit_locale_name(expr, charactersInExpression + 1);
1254
+ }
1255
+ else if (parse_bcp47(&names, expr) && __acrt_IsValidLocaleName(names.szLocaleName))
1256
+ {
1257
+ // Parsed as Windows-recognized BCP-47 tag
1258
+ wchar_t const * const normalized_locale_name = names.szLocaleName;
1259
+
1260
+ if (names.szCodePage[0])
1261
+ {
1262
+ wchar_t const * const cp = names.szCodePage;
1263
+
1264
+ // Allow .utf8/.utf-8 for BCP-47 tags.
1265
+ if ( __ascii_towlower(cp[0]) == L'u'
1266
+ && __ascii_towlower(cp[1]) == L't'
1267
+ && __ascii_towlower(cp[2]) == L'f'
1268
+ && (cp[3] == L'8' && cp[4] == L'\0')
1269
+ || (cp[3] == L'-' && cp[4] == L'8' && cp[5] == L'\0'))
1270
+ {
1271
+ iCodePage = CP_UTF8;
1272
+ }
1273
+ else
1274
+ {
1275
+ // Other codepage tokens (fr-FR.1252, etc.) aren't supported for BCP-47 tags (eg: Use Unicode!)
1276
+ return nullptr;
1277
+ }
1278
+ }
1279
+ else
1280
+ {
1281
+ iCodePage = get_default_code_page(normalized_locale_name);
1282
+ }
1283
+
1284
+ // Commit code page
1285
+ *pcachecp = static_cast<WORD>(iCodePage);
1286
+
1287
+ // Commit locale name that we will return to the user (same as input locale string).
1288
+ _ERRCHECK(wcsncpy_s(cacheout, cacheoutLen, expr, charactersInExpression + 1));
1289
+
1290
+ // Commit normalized name for internal use.
1291
+ locale_name_guard.commit_locale_name(normalized_locale_name, wcslen(normalized_locale_name) + 1);
1292
+ }
1293
+ else
1294
+ {
1295
+ return nullptr; // input unrecognized as locale name
1296
+ }
1297
+
1298
+ // Operation succeeded - commit input cache.
1299
+ if (*expr && charactersInExpression < MAX_LC_LEN)
1300
+ {
1301
+ _ERRCHECK(wcsncpy_s(cachein, cacheinLen, expr, charactersInExpression + 1));
1302
+ }
1303
+ else
1304
+ {
1305
+ *cachein = L'\x0';
1306
+ }
1307
+ }
1308
+
1309
+ output_code_page = *pcachecp; // Update code page
1310
+
1311
+ _ERRCHECK(wcscpy_s(output, sizeInChars, cacheout));
1312
+ return cacheout; // Return locale name to be given back to user, with lifetime bound in PTD
1313
+ }
1314
+
1315
+ /* helpers */
1316
+
1317
+ void _wcscats ( wchar_t *outstr, size_t numberOfElements, int n, ...)
1318
+ {
1319
+ int i;
1320
+ va_list substr;
1321
+
1322
+ va_start (substr, n);
1323
+
1324
+ for (i =0; i<n; i++)
1325
+ {
1326
+ _ERRCHECK(wcscat_s(outstr, numberOfElements, va_arg(substr, wchar_t *)));
1327
+ }
1328
+ va_end(substr);
1329
+ }
1330
+
1331
+ // Parse the wlocale string to find the array of language/region/codepage strings (if available)
1332
+ // in the form .CodePage, Language, Language_Region, Language_Region.Codepage, or Language.CodePage
1333
+ // If the input is a BCP-47 tag, then that tag is returned in the names language.
1334
+ int __lc_wcstolc ( __crt_locale_strings *names, const wchar_t *wlocale)
1335
+ {
1336
+ int i;
1337
+ size_t len;
1338
+ wchar_t wch;
1339
+
1340
+ memset((void *)names, '\0', sizeof(__crt_locale_strings)); /* clear out result */
1341
+
1342
+ if (*wlocale==L'\0')
1343
+ return 0; /* trivial case */
1344
+
1345
+ /* only code page is given */
1346
+ if (wlocale[0] == L'.' && wlocale[1] != L'\0')
1347
+ {
1348
+ _ERRCHECK(wcsncpy_s(names->szCodePage, _countof(names->szCodePage), &wlocale[1], MAX_CP_LEN-1));
1349
+ /* Make sure to null terminate the string in case wlocale is > MAX_CP_LEN */
1350
+ names->szCodePage[ MAX_CP_LEN -1] = 0;
1351
+ return 0;
1352
+ }
1353
+
1354
+ // Looks like Language_Country/Region.Codepage
1355
+ for (i=0; ; i++)
1356
+ {
1357
+ // _ language country/region separator, . is before codepage, either , or \0 are end of string.
1358
+ len = wcscspn(wlocale, L"_.,");
1359
+ if (len == 0)
1360
+ return -1; /* syntax error, can't start with a separator */
1361
+
1362
+ wch = wlocale[len];
1363
+
1364
+ if ((i == 0) && (len < MAX_LANG_LEN))
1365
+ {
1366
+ _ERRCHECK(wcsncpy_s(names->szLanguage, _countof(names->szLanguage), wlocale, len));
1367
+ if (wch == L'.')
1368
+ {
1369
+ // '.' is a delimiter before codepage, so codepage is expected next (skip country/region)
1370
+ i++;
1371
+ }
1372
+ }
1373
+
1374
+ else if ((i==1) && (len<MAX_CTRY_LEN) && (wch!=L'_'))
1375
+ _ERRCHECK(wcsncpy_s(names->szCountry, _countof(names->szCountry), wlocale, len));
1376
+
1377
+ else if ((i==2) && (len<MAX_CP_LEN) && (wch==L'\0' || wch==L','))
1378
+ _ERRCHECK(wcsncpy_s(names->szCodePage, _countof(names->szCodePage), wlocale, len));
1379
+
1380
+ else
1381
+ return -1; /* error parsing wlocale string */
1382
+
1383
+ if (wch==L',')
1384
+ {
1385
+ /* modifier not used in current implementation, but it
1386
+ must be parsed to for POSIX/XOpen conformance */
1387
+ /* wcsncpy(names->szModifier, wlocale, MAX_MODIFIER_LEN-1); */
1388
+ break;
1389
+ }
1390
+
1391
+ if (!wch)
1392
+ break;
1393
+ wlocale+=(len+1);
1394
+ }
1395
+ return 0;
1396
+ }
1397
+ // Append locale name pieces together in the form of "Language_country/region.CodePage"
1398
+ // Note that the preferred form is the BCP-47 tag, but we don't want to change legacy names
1399
+ void __lc_lctowcs ( wchar_t *locale, size_t numberOfElements, const __crt_locale_strings *names)
1400
+ {
1401
+ _ERRCHECK(wcscpy_s(locale, numberOfElements, names->szLanguage));
1402
+ if (*(names->szCountry))
1403
+ _wcscats(locale, numberOfElements, 2, L"_", names->szCountry);
1404
+ if (*(names->szCodePage))
1405
+ _wcscats(locale, numberOfElements, 2, L".", names->szCodePage);
1406
+ }
1407
+
1408
+ // Create a copy of a locale name string
1409
+ LPWSTR __cdecl __acrt_copy_locale_name(LPCWSTR localeName)
1410
+ {
1411
+ size_t cch;
1412
+ wchar_t* localeNameCopy;
1413
+
1414
+ if (!localeName) // Input cannot be nullptr
1415
+ return nullptr;
1416
+
1417
+ cch = wcsnlen(localeName, LOCALE_NAME_MAX_LENGTH);
1418
+ if (cch >= LOCALE_NAME_MAX_LENGTH) // locale name length must be <= LOCALE_NAME_MAX_LENGTH
1419
+ return nullptr;
1420
+
1421
+ if ((localeNameCopy = (wchar_t *) _malloc_crt((cch + 1) * sizeof(wchar_t))) == nullptr)
1422
+ return nullptr;
1423
+
1424
+ _ERRCHECK(wcsncpy_s(localeNameCopy, cch+1, localeName, cch+1));
1425
+ return localeNameCopy;
1426
+ }
1427
+
1428
+ #endif /* _UCRT_ENCLAVE_BUILD */
1429
+
1430
+ } // extern "C"
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/chsize.cpp ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // chsize.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _chsize() and _chsize_s(), which change the size of a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_ptd_propagation.h>
10
+
11
+
12
+ // Changes the size of the given file, either extending or truncating it. The
13
+ // file must have been opened with write permissions or this will fail. Returns
14
+ // 0 on success; returns an errno error code on failure,
15
+ static errno_t __cdecl _chsize_s_internal(int const fh, __int64 const size, __crt_cached_ptd_host& ptd)
16
+ {
17
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN_ERRCODE(ptd, fh, EBADF);
18
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF);
19
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(ptd, (_osfile(fh) & FOPEN), EBADF);
20
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE(ptd, (size >= 0), EINVAL);
21
+
22
+ return __acrt_lowio_lock_fh_and_call(fh, [&]()
23
+ {
24
+ if (_osfile(fh) & FOPEN)
25
+ {
26
+ return _chsize_nolock_internal(fh, size, ptd);
27
+ }
28
+ else
29
+ {
30
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
31
+ return ptd.get_errno().set(EBADF);
32
+ }
33
+ });
34
+ }
35
+
36
+ extern "C" errno_t __cdecl _chsize_s(int const fh, __int64 const size)
37
+ {
38
+ __crt_cached_ptd_host ptd;
39
+ return _chsize_s_internal(fh, size, ptd);
40
+ }
41
+
42
+ struct __crt_seek_guard
43
+ {
44
+
45
+ __crt_seek_guard(int const fh, __int64 const size)
46
+ : place(_lseeki64_nolock(fh, 0, SEEK_CUR)),
47
+ end(_lseeki64_nolock(fh, 0, SEEK_END)),
48
+ extend(size - end),
49
+ fhh(fh)
50
+ {
51
+ }
52
+
53
+ ~__crt_seek_guard()
54
+ {
55
+ _lseeki64_nolock(fhh, place, SEEK_SET);
56
+ }
57
+
58
+ __crt_seek_guard(__crt_seek_guard const &) = delete;
59
+ __crt_seek_guard& operator=(__crt_seek_guard const &) = delete;
60
+
61
+ __int64 place;
62
+ __int64 end;
63
+ __int64 extend;
64
+ int fhh;
65
+ };
66
+
67
+ extern "C" errno_t __cdecl _chsize_nolock_internal(int const fh, __int64 const size, __crt_cached_ptd_host& ptd)
68
+ {
69
+ // Get current file position and seek to end
70
+ __crt_seek_guard seek_guard(fh, size);
71
+
72
+ if (seek_guard.place == -1 || seek_guard.end == -1)
73
+ {
74
+ // EBADF if earlier lseek (in __crt_seek_guard) failed
75
+ // EINVAL otherwise (ex: too large of a offset)
76
+ return ptd.get_errno().value_or(EINVAL);
77
+ }
78
+
79
+ // Grow or shrink the file as necessary:
80
+ if (seek_guard.extend > 0)
81
+ {
82
+ // Extend the file by filling the new area with zeroes:
83
+ __crt_unique_heap_ptr<char> const zero_buffer(_calloc_crt_t(char, _INTERNAL_BUFSIZ));
84
+ if (!zero_buffer)
85
+ {
86
+ return ptd.get_errno().set(ENOMEM);
87
+ }
88
+
89
+ int const old_mode = _setmode_nolock(fh, _O_BINARY);
90
+
91
+ do
92
+ {
93
+ int const bytes_to_write = seek_guard.extend >= static_cast<__int64>(_INTERNAL_BUFSIZ)
94
+ ? _INTERNAL_BUFSIZ
95
+ : static_cast<int>(seek_guard.extend);
96
+
97
+ int const bytes_written = _write_nolock(fh, zero_buffer.get(), bytes_to_write, ptd);
98
+ if (bytes_written == -1)
99
+ {
100
+ // Error on write:
101
+ if (ptd.get_doserrno().check(ERROR_ACCESS_DENIED))
102
+ {
103
+ ptd.get_errno().set(EACCES);
104
+ }
105
+
106
+ return ptd.get_errno().value_or(0);
107
+ }
108
+
109
+ seek_guard.extend -= bytes_written;
110
+ } while (seek_guard.extend > 0);
111
+
112
+ #pragma warning(suppress:6031) // return value ignored
113
+ _setmode_nolock(fh, old_mode);
114
+ }
115
+ else if (seek_guard.extend < 0)
116
+ {
117
+ // Shorten the file by truncating it:
118
+ __int64 const new_end = _lseeki64_nolock(fh, size, SEEK_SET);
119
+ if (new_end == -1)
120
+ {
121
+ return ptd.get_errno().value_or(0);
122
+ }
123
+
124
+ if (!SetEndOfFile(reinterpret_cast<HANDLE>(_get_osfhandle(fh))))
125
+ {
126
+ ptd.get_doserrno().set(GetLastError());
127
+ return ptd.get_errno().set(EACCES);
128
+ }
129
+ }
130
+
131
+ return 0;
132
+ }
133
+
134
+ extern "C" errno_t __cdecl _chsize_nolock(int const fh, __int64 const size)
135
+ { // TODO: _chsize_nolock is already internal-only.
136
+ // Once PTD is propagated everywhere, rename _chsize_nolock_internal to _chsize_nolock.
137
+ __crt_cached_ptd_host ptd;
138
+ return _chsize_nolock_internal(fh, size, ptd);
139
+ }
140
+
141
+
142
+ // Changes the size of the given file, either extending or truncating it. The
143
+ // file must have been opened with write permissions or this will fail. Returns
144
+ // 0 on success; returns -1 and sets errno on failure.
145
+ extern "C" int __cdecl _chsize(int const fh, long const size)
146
+ {
147
+ return _chsize_s(fh, size) == 0 ? 0 : -1;
148
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/close.cpp ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // close.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _close(), which closes a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_ptd_propagation.h>
10
+
11
+
12
+
13
+ // Closes the underlying OS file handle, if and only if nt needs to be closed.
14
+ // Returns 0 on success; returns the OS error on failure.
15
+ static DWORD close_os_handle_nolock(int const fh) throw()
16
+ {
17
+ // If the underlying handle is INVALID_HANDLE_VALUE, don't try to acutally
18
+ // close it:
19
+ if (_get_osfhandle(fh) == (intptr_t)INVALID_HANDLE_VALUE)
20
+ {
21
+ return 0;
22
+ }
23
+
24
+ // If the file handle is stdout or stderr, and if stdout and stderr are
25
+ // mapped to the same OS file handle, skip the CloseHandle without error.
26
+ //
27
+ // stdout and stderr are the only handles for which this support is
28
+ // provided. Other handles may be mapped to the same OS file handle only
29
+ // at the programmer's risk.
30
+ bool is_other_std_handle_open =
31
+ fh == 1 && (_osfile(2) & FOPEN) ||
32
+ fh == 2 && (_osfile(1) & FOPEN);
33
+
34
+ if (is_other_std_handle_open && _get_osfhandle(1) == _get_osfhandle(2))
35
+ {
36
+ return 0;
37
+ }
38
+
39
+ // Otherwise, we can go ahead and close the handle:
40
+ if (CloseHandle(reinterpret_cast<HANDLE>(_get_osfhandle(fh))))
41
+ {
42
+ return 0;
43
+ }
44
+
45
+ return GetLastError();
46
+ }
47
+
48
+
49
+
50
+ // Closes the file associated with the given file handle. On success, returns 0.
51
+ // On failure, returns -1 and sets errno.
52
+ extern "C" int __cdecl _close_internal(int const fh, __crt_cached_ptd_host& ptd)
53
+ {
54
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
55
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
56
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(fh) & FOPEN), EBADF, -1);
57
+
58
+ return __acrt_lowio_lock_fh_and_call(fh, [&]()
59
+ {
60
+ if (_osfile(fh) & FOPEN)
61
+ {
62
+ return _close_nolock_internal(fh, ptd);
63
+ }
64
+ else
65
+ {
66
+ ptd.get_errno().set(EBADF);
67
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
68
+ return -1;
69
+ }
70
+ });
71
+ }
72
+
73
+ extern "C" int __cdecl _close(int const fh)
74
+ {
75
+ __crt_cached_ptd_host ptd;
76
+ return _close_internal(fh, ptd);
77
+ }
78
+
79
+
80
+
81
+ extern "C" int __cdecl _close_nolock_internal(int const fh, __crt_cached_ptd_host& ptd)
82
+ {
83
+ DWORD const close_os_handle_error = close_os_handle_nolock(fh);
84
+
85
+ _free_osfhnd(fh);
86
+ _osfile(fh) = 0;
87
+
88
+ if (close_os_handle_error != 0)
89
+ {
90
+ __acrt_errno_map_os_error_ptd(close_os_handle_error, ptd);
91
+ return -1;
92
+ }
93
+
94
+ return 0;
95
+ }
96
+
97
+ extern "C" int __cdecl _close_nolock(int const fh)
98
+ {
99
+ __crt_cached_ptd_host ptd;
100
+ return _close_nolock_internal(fh, ptd);
101
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/commit.cpp ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // commit.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _commit(), which flushes a file buffer to disk.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+
10
+
11
+
12
+ // Flushes the buffer for the given file to disk.
13
+ //
14
+ // On success, 0 is returned. On failure, -1 is returned and errno is set.
15
+ extern "C" int __cdecl _commit(int const fh)
16
+ {
17
+ _CHECK_FH_RETURN(fh, EBADF, -1);
18
+ _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
19
+ _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
20
+
21
+ return __acrt_lowio_lock_fh_and_call(fh, [&]()
22
+ {
23
+ if (_osfile(fh) & FOPEN)
24
+ {
25
+ if (FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(fh))))
26
+ return 0; // Success
27
+
28
+ _doserrno = GetLastError();
29
+ }
30
+
31
+ errno = EBADF;
32
+
33
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
34
+ return -1;
35
+ });
36
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/creat.cpp ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // creat.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _creat() (and _wcreat(), via inclusion), which creates a new file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+
10
+
11
+
12
+ // Creates a new file.
13
+ //
14
+ // If the file does not exist, this function creates a new file with the given
15
+ // permission setting and opens it for writing. If the file already exists and
16
+ // its permission allows writing, this function truncates it to zero length and
17
+ // opens it for writing.
18
+ //
19
+ // The only XENIX mode bit supported is user write (S_IWRITE).
20
+ //
21
+ // On success, the handle for the newly created file is returned. On failure,
22
+ // -1 is returned and errno is set.
23
+ template <typename Character>
24
+ static int __cdecl common_creat(Character const* const path, int const pmode) throw()
25
+ {
26
+ typedef __crt_char_traits<Character> traits;
27
+ // creat is just the same as open...
28
+ int fh = -1;
29
+ errno_t e = traits::tsopen_s(&fh, path, _O_CREAT + _O_TRUNC + _O_RDWR, _SH_DENYNO, pmode);
30
+ return e == 0 ? fh : -1;
31
+ }
32
+
33
+ extern "C" int __cdecl _creat(char const* const path, int const pmode)
34
+ {
35
+ return common_creat(path, pmode);
36
+ }
37
+
38
+ extern "C" int __cdecl _wcreat(wchar_t const* const path, int const pmode)
39
+ {
40
+ return common_creat(path, pmode);
41
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/dup.cpp ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // dup.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _dup() and _dup_nolock, which duplicate lowio file handles
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_ptd_propagation.h>
10
+
11
+ static int __cdecl duplicate_osfhnd(int const fh, int const new_fh, __crt_cached_ptd_host& ptd) throw()
12
+ {
13
+ // Duplicate the file handle:
14
+ intptr_t new_osfhandle;
15
+
16
+ BOOL const result = DuplicateHandle(
17
+ GetCurrentProcess(),
18
+ reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
19
+ GetCurrentProcess(),
20
+ &reinterpret_cast<HANDLE&>(new_osfhandle),
21
+ 0L,
22
+ TRUE,
23
+ DUPLICATE_SAME_ACCESS);
24
+
25
+ if (!result)
26
+ {
27
+ __acrt_errno_map_os_error_ptd(GetLastError(), ptd);
28
+ return -1;
29
+ }
30
+
31
+ // Duplicate the handle state:
32
+ __acrt_lowio_set_os_handle(new_fh, new_osfhandle);
33
+ _osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
34
+ _textmode(new_fh) = _textmode(fh);
35
+ _tm_unicode(new_fh) = _tm_unicode(fh);
36
+ return new_fh;
37
+ }
38
+
39
+ static int __cdecl _dup_nolock_internal(int const fh, __crt_cached_ptd_host& ptd) throw()
40
+ {
41
+ if ((_osfile(fh) & FOPEN) == 0)
42
+ {
43
+ ptd.get_errno().set(EBADF);
44
+ ptd.get_doserrno().set(0);
45
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
46
+ return -1;
47
+ }
48
+
49
+ // Allocate a duplicate handle
50
+ int const new_fh = _alloc_osfhnd();
51
+ if (new_fh == -1)
52
+ {
53
+ ptd.get_errno().set(EMFILE);
54
+ ptd.get_doserrno().set(0);
55
+ return -1;
56
+ }
57
+
58
+ int return_value = -1;
59
+ __try
60
+ {
61
+ return_value = duplicate_osfhnd(fh, new_fh, ptd);
62
+ }
63
+ __finally
64
+ {
65
+ // The handle returned by _alloc_osfhnd is both open and locked. If we
66
+ // failed to duplicate the handle, we need to abandon the handle by
67
+ // unsetting the open flag. We always need to unlock the handle:
68
+ if (return_value == -1)
69
+ {
70
+ _osfile(new_fh) &= ~FOPEN;
71
+ }
72
+
73
+ __acrt_lowio_unlock_fh(new_fh);
74
+ }
75
+ return return_value;
76
+ }
77
+
78
+ static int __cdecl _dup_internal(int const fh, __crt_cached_ptd_host& ptd)
79
+ {
80
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
81
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
82
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(fh) & FOPEN), EBADF, -1);
83
+
84
+ return __acrt_lowio_lock_fh_and_call(fh, [&](){
85
+ return _dup_nolock_internal(fh, ptd);
86
+ });
87
+ }
88
+
89
+ // _dup() duplicates a file handle and returns the duplicate. If the function
90
+ // fails, -1 is returned and errno is set.
91
+ extern "C" int __cdecl _dup(int const fh)
92
+ {
93
+ __crt_cached_ptd_host ptd;
94
+ return _dup_internal(fh, ptd);
95
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/dup2.cpp ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // dup2.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _dup2() and _dup2_nolock, which duplicate lowio file handles
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_ptd_propagation.h>
10
+
11
+ static int __cdecl _dup2_nolock_internal(int const source_fh, int const target_fh, __crt_cached_ptd_host& ptd) throw()
12
+ {
13
+ if ((_osfile(source_fh) & FOPEN) == 0)
14
+ {
15
+ // If the source handle is not open, return an error. Noe that the
16
+ // DuplicateHandle API will not detect this error, because it implies
17
+ // that _osfhnd(source_fh) == INVALID_HANDLE_VALUE, and this is a
18
+ // legal HANDLE value to be duplicated.
19
+ ptd.get_errno().set(EBADF);
20
+ ptd.get_doserrno().set(0);
21
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
22
+ return -1;
23
+ }
24
+
25
+ // Duplicate the source file onto the target file:
26
+ intptr_t new_osfhandle;
27
+
28
+ BOOL const result = DuplicateHandle(
29
+ GetCurrentProcess(),
30
+ reinterpret_cast<HANDLE>(_get_osfhandle(source_fh)),
31
+ GetCurrentProcess(),
32
+ &reinterpret_cast<HANDLE&>(new_osfhandle),
33
+ 0,
34
+ TRUE,
35
+ DUPLICATE_SAME_ACCESS);
36
+
37
+ if (!result)
38
+ {
39
+ __acrt_errno_map_os_error_ptd(GetLastError(), ptd);
40
+ return -1;
41
+ }
42
+
43
+ // If the target is open, close it once we know the OS handle was duplicated successfully.
44
+ // We ignore the possibility of an error here: an error simply means that the OS handle
45
+ // value may remain bound for the duration of the process.
46
+ if (_osfile(target_fh) & FOPEN)
47
+ {
48
+ _close_nolock_internal(target_fh, ptd);
49
+ }
50
+
51
+ __acrt_lowio_set_os_handle(target_fh, new_osfhandle);
52
+
53
+ // Copy the _osfile information, with the FNOINHERIT bit cleared:
54
+ _osfile(target_fh) = _osfile(source_fh) & ~FNOINHERIT;
55
+ _textmode(target_fh) = _textmode(source_fh);
56
+ _tm_unicode(target_fh) = _tm_unicode(source_fh);
57
+
58
+ return 0;
59
+ }
60
+
61
+ static int __cdecl _dup2_internal(int const source_fh, int const target_fh, __crt_cached_ptd_host& ptd) throw()
62
+ {
63
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, source_fh, EBADF, -1);
64
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (source_fh >= 0 && (unsigned)source_fh < (unsigned)_nhandle), EBADF, -1);
65
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(source_fh) & FOPEN), EBADF, -1);
66
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, target_fh, EBADF, -1);
67
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, ((unsigned)target_fh < _NHANDLE_), EBADF, -1);
68
+
69
+ // Make sure there is an __crt_lowio_handle_data struct corresponding to the target_fh:
70
+ if (target_fh >= _nhandle && __acrt_lowio_ensure_fh_exists(target_fh) != 0)
71
+ {
72
+ return -1;
73
+ }
74
+
75
+ // If the source and target are the same, return success (we've already
76
+ // verified that the file handle is open, above). This is for conformance
77
+ // with the POSIX specification for dup2.
78
+ if (source_fh == target_fh)
79
+ {
80
+ return 0;
81
+ }
82
+
83
+ // Obtain the two file handle locks. In order to prevent deadlock, we
84
+ // always obtain the lock for the lower-numbered file handle first:
85
+ if (source_fh < target_fh)
86
+ {
87
+ __acrt_lowio_lock_fh(source_fh);
88
+ __acrt_lowio_lock_fh(target_fh);
89
+ }
90
+ else if (source_fh > target_fh)
91
+ {
92
+ __acrt_lowio_lock_fh(target_fh);
93
+ __acrt_lowio_lock_fh(source_fh);
94
+ }
95
+
96
+ int result = 0;
97
+
98
+ __try
99
+ {
100
+ result = _dup2_nolock_internal(source_fh, target_fh, ptd);
101
+ }
102
+ __finally
103
+ {
104
+ // The order in which we unlock the file handles does not matter:
105
+ __acrt_lowio_unlock_fh(source_fh);
106
+ __acrt_lowio_unlock_fh(target_fh);
107
+ }
108
+ return result;
109
+ }
110
+
111
+ // _dup2() makes the target file handle a duplicate of the source file handle,
112
+ // so that both handles refer to the same file. If the target handle is open
113
+ // upon entry, it is closed so that it is not leaked.
114
+ //
115
+ // Returns 0 if successful; returns -1 and sets errno on failure.
116
+ extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
117
+ {
118
+ __crt_cached_ptd_host ptd;
119
+ return _dup2_internal(source_fh, target_fh, ptd);
120
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/eof.cpp ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // eof.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _eof(), which tests whether a file is at EOF.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+
10
+
11
+
12
+ // Tests if a file is at EOF. Returns:
13
+ // * 1 if at EOF
14
+ // * 0 if not at EOF
15
+ // * -1 on failure (errno is set)
16
+ extern "C" int __cdecl _eof(int const fh)
17
+ {
18
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
19
+ _VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
20
+ _VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
21
+
22
+ return __acrt_lowio_lock_fh_and_call(fh, [&]()
23
+ {
24
+ if ((_osfile(fh) & FOPEN) == 0)
25
+ {
26
+ errno = EBADF;
27
+ _doserrno = 0;
28
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
29
+ return -1;
30
+ }
31
+
32
+ __int64 const here = _lseeki64_nolock(fh, 0i64, SEEK_CUR);
33
+ if (here == -1i64)
34
+ return -1;
35
+
36
+
37
+ __int64 const end = _lseeki64_nolock(fh, 0i64, SEEK_END);
38
+ if (end == -1i64)
39
+ return -1;
40
+
41
+ // Now we can test if we're at the end:
42
+ if (here == end)
43
+ return 1;
44
+
45
+ // If we aren't at the end, we need to reset the stream to its original
46
+ // state before we return:
47
+ _lseeki64_nolock(fh, here, SEEK_SET);
48
+ return 0;
49
+ });
50
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/filelength.cpp ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // filelength.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _filelength(), which computes the length of a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <stddef.h>
10
+ #include <stdio.h>
11
+ #include <stdlib.h>
12
+
13
+
14
+
15
+ // Computes the length, in bytes, of the given file. Returns -1 on failure.
16
+ template <typename Integer>
17
+ _Success_(return != -1)
18
+ static Integer __cdecl common_filelength(int const fh) throw()
19
+ {
20
+ typedef __crt_integer_traits<Integer> traits;
21
+
22
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
23
+ _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
24
+ _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
25
+
26
+ __acrt_lowio_lock_fh(fh);
27
+ Integer end = -1;
28
+ __try
29
+ {
30
+ if ((_osfile(fh) & FOPEN) == 0)
31
+ {
32
+ errno = EBADF;
33
+ _doserrno = 0;
34
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
35
+ __leave;
36
+ }
37
+
38
+ // Seek to the end to get the length:
39
+ Integer const here = traits::lseek_nolock(fh, 0, SEEK_CUR);
40
+ if (here == -1)
41
+ __leave;
42
+
43
+ end = traits::lseek_nolock(fh, 0, SEEK_END);
44
+ if (here != end)
45
+ traits::lseek_nolock(fh, here, SEEK_SET);
46
+ }
47
+ __finally
48
+ {
49
+ __acrt_lowio_unlock_fh(fh);
50
+ }
51
+ return end;
52
+ }
53
+
54
+ extern "C" long __cdecl _filelength(int const fh)
55
+ {
56
+ return common_filelength<long>(fh);
57
+ }
58
+
59
+ extern "C" __int64 __cdecl _filelengthi64(int const fh)
60
+ {
61
+ return common_filelength<__int64>(fh);
62
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/ioinit.cpp ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // ioinit.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines initialization and termination routines for the lowio library, along
7
+ // with global data shared by most of the lowio library.
8
+ //
9
+ #include <corecrt_internal_lowio.h>
10
+ #include <corecrt_internal_stdio.h>
11
+
12
+
13
+
14
+ // This is a special static lowio file object referenced only by the safe access
15
+ // functionality in the internal headers. It is used in certain stdio-level
16
+ // functions to more gracefully handle a FILE with -1 as its lowio file id.
17
+ extern "C" __crt_lowio_handle_data __badioinfo =
18
+ {
19
+ { }, // lock
20
+ static_cast<intptr_t>(-1), // osfhnd
21
+ 0, // startpos
22
+ FTEXT, // osfile
23
+ __crt_lowio_text_mode::ansi, // textmode
24
+ { LF, LF, LF }, // _pipe_lookahead
25
+ };
26
+
27
+
28
+
29
+ // This is the number of lowio file objects that have been allocated. This
30
+ // includes both in-use and unused elements, since not all allocated files
31
+ // are necessarily in use at any given time.
32
+ //
33
+ // This number is in the range of [IOINFO_ARRAY_ELTS, _NHANDLE_]
34
+ extern "C" int _nhandle = 0;
35
+
36
+
37
+
38
+ // This is the global array of file object arrays:
39
+ extern "C" __crt_lowio_handle_data* __pioinfo[IOINFO_ARRAYS] = { 0 };
40
+
41
+
42
+
43
+ static DWORD __cdecl get_std_handle_id(int const fh) throw()
44
+ {
45
+ // Convert the CRT file handle to the OS file handle for the three
46
+ // standard streams:
47
+ switch (fh)
48
+ {
49
+ case 0: return STD_INPUT_HANDLE;
50
+ case 1: return STD_OUTPUT_HANDLE;
51
+ case 2: return STD_ERROR_HANDLE;
52
+ }
53
+
54
+ return STD_ERROR_HANDLE; // Unreachable, but the compiler can't know.
55
+ }
56
+
57
+
58
+
59
+ static void __cdecl initialize_inherited_file_handles_nolock() throw()
60
+ {
61
+ STARTUPINFOW startup_info;
62
+ GetStartupInfoW(&startup_info);
63
+
64
+ // First check and see if we inherited any file handles. If we didn't, then
65
+ // we don't have anything to initialize:
66
+ if (startup_info.cbReserved2 == 0 || startup_info.lpReserved2 == nullptr)
67
+ return;
68
+
69
+ // Get the number of inherited handles:
70
+ int const handle_count = *reinterpret_cast<UNALIGNED int*>(startup_info.lpReserved2);
71
+
72
+ // Compute the start of the passed file info and OS HANDLEs:
73
+ unsigned char* const first_file =
74
+ reinterpret_cast<unsigned char*>(startup_info.lpReserved2) + sizeof(int);
75
+
76
+ UNALIGNED intptr_t* const first_handle =
77
+ reinterpret_cast<UNALIGNED intptr_t*>(first_file + handle_count);
78
+
79
+ // Do not attempt to inherit more than the maximum number of supported handles:
80
+ int handles_to_inherit = handle_count < _NHANDLE_
81
+ ? handle_count
82
+ : _NHANDLE_;
83
+
84
+ // Attempt to allocate the required number of handles. If we fail for any
85
+ // reason, we'll inherit as many handles as we can:
86
+ __acrt_lowio_ensure_fh_exists(handles_to_inherit);
87
+ if (handles_to_inherit > _nhandle)
88
+ handles_to_inherit = _nhandle;
89
+
90
+ // Validate and copy the provided file information:
91
+ unsigned char* it_file = first_file;
92
+ UNALIGNED intptr_t* it_handle = first_handle;
93
+
94
+ for (int fh = 0; fh != handles_to_inherit; ++fh, ++it_file, ++it_handle)
95
+ {
96
+ HANDLE const real_handle = reinterpret_cast<HANDLE>(*it_handle);
97
+
98
+ // If the provided information does not appear to describe an open,
99
+ // valid file or device, skip it:
100
+ if (real_handle == INVALID_HANDLE_VALUE)
101
+ continue;
102
+
103
+ if (*it_handle == _NO_CONSOLE_FILENO)
104
+ continue;
105
+
106
+ if ((*it_file & FOPEN) == 0)
107
+ continue;
108
+
109
+ // GetFileType cannot be called for pipe handles since it may "hang" if
110
+ // there is a blocked read pending on the pipe in the parent.
111
+ if ((*it_file & FPIPE) == 0 && GetFileType(real_handle) == FILE_TYPE_UNKNOWN)
112
+ continue;
113
+
114
+ // Okay, the file looks valid:
115
+ __crt_lowio_handle_data* const pio = _pioinfo(fh);
116
+ pio->osfhnd = *it_handle;
117
+ pio->osfile = *it_file;
118
+ }
119
+ }
120
+
121
+
122
+
123
+ static void initialize_stdio_handles_nolock() throw()
124
+ {
125
+ for (int fh = 0; fh != STDIO_HANDLES_COUNT; ++fh)
126
+ {
127
+ __crt_lowio_handle_data* const pio = _pioinfo(fh);
128
+
129
+ // If this handle was inherited from the parent process and initialized
130
+ // already, make sure it has the FTEXT flag and continue:
131
+ if (pio->osfhnd != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE) &&
132
+ pio->osfhnd != _NO_CONSOLE_FILENO)
133
+ {
134
+ pio->osfile |= FTEXT;
135
+ continue;
136
+ }
137
+
138
+ // Regardless what happens next, the file will be treated as if it is
139
+ // open in text mode:
140
+ pio->osfile = FOPEN | FTEXT;
141
+
142
+ // This handle has not yet been initialized, so let's see if we can get
143
+ // the handle from the OS:
144
+ intptr_t const os_handle = reinterpret_cast<intptr_t>(GetStdHandle(get_std_handle_id(fh)));
145
+
146
+ bool const is_valid_handle =
147
+ os_handle != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE) &&
148
+ os_handle != reinterpret_cast<intptr_t>(nullptr);
149
+
150
+ DWORD const handle_type = is_valid_handle
151
+ ? GetFileType(reinterpret_cast<HANDLE>(os_handle))
152
+ : FILE_TYPE_UNKNOWN;
153
+
154
+
155
+ if (handle_type != FILE_TYPE_UNKNOWN)
156
+ {
157
+ // The file type is known, so we obtained a valid handle from the
158
+ // OS. Finish initializing the lowio file object for this handle,
159
+ // including the flag specifying whether this is a character device
160
+ // or a pipe:
161
+ pio->osfhnd = os_handle;
162
+
163
+ if ((handle_type & 0xff) == FILE_TYPE_CHAR)
164
+ pio->osfile |= FDEV;
165
+
166
+ else if ((handle_type & 0xff) == FILE_TYPE_PIPE)
167
+ pio->osfile |= FPIPE;
168
+ }
169
+ else
170
+ {
171
+ // We were unable to get the handles from the OS. For stdin, stdout,
172
+ // and stderr, if there is no valid OS handle, treat the CRT handle
173
+ // as being open in text mode on a device with _NO_CONSOLE_FILENO
174
+ // underlying it. We use this value instead of INVALID_HANDLE_VALUE
175
+ // to distinguish between a failure in opening a file and a program
176
+ // run without a console:
177
+ pio->osfile |= FDEV;
178
+ pio->osfhnd = _NO_CONSOLE_FILENO;
179
+
180
+ // Also update the corresponding stdio stream, unless stdio was
181
+ // already terminated:
182
+ if (__piob)
183
+ __piob[fh]->_file = _NO_CONSOLE_FILENO;
184
+ }
185
+ }
186
+ }
187
+
188
+
189
+
190
+ // Initializes the lowio library. This initialization comprises several steps:
191
+ //
192
+ // [1] An initial array of __crt_lowio_handle_data structures is allocated.
193
+ //
194
+ // [2] Inherited file handles are initialized. To do this, sthe startup info
195
+ // is obtained from the OS, via the lpReserved2 member. The format of the
196
+ // information is as follows:
197
+ //
198
+ // [Bytes 0 - 3] Integer value N, which is the number of handles that
199
+ // are provided by the parent process.
200
+ //
201
+ // [Bytes 4 - N+3] The N osfile values.
202
+ //
203
+ // [Bytes N+4 - 5*N+3] The N OS HANDLE values, as DWORDs
204
+ //
205
+ // [3] Next, the first three lowio files (corresponding to stdin, stdout, and
206
+ // stderr) are initialized as follows: If the value in osfhnd is
207
+ // INVALID_HANDLE_VALUE, then we try to obtain a HANDLE from the OS. These
208
+ // handles are forced to text mode, as standard input, output, and error
209
+ // always start out in text mode.
210
+ //
211
+ // Notes:
212
+ //
213
+ // [1] In general, not all of the pased info from the parent process will
214
+ // describe open handles. If, for example, only C handle 1 (stdout) and
215
+ // C handle 6 are open in the parent, info for C handles 0 through 6 are
216
+ // passed to the child. 0, 2, 3, 4, and 5 will not describe open handles.
217
+ //
218
+ // [2] Care is taken not to "overflow" the arrays of lowio file objects.
219
+ //
220
+ // [3] See the dospawn logic for the encoding of the file handle info to be
221
+ // passed to a child process.
222
+ //
223
+ // This funtion returns 0 on success; -1 on failure.
224
+ extern "C" bool __cdecl __acrt_initialize_lowio()
225
+ {
226
+ __acrt_lock(__acrt_lowio_index_lock);
227
+ bool result = false;
228
+ __try
229
+ {
230
+ // First, allocate and initialize the initial array of lowio files:
231
+ if (__acrt_lowio_ensure_fh_exists(0) != 0)
232
+ __leave;
233
+
234
+ // Next, process and initialize all inherited file handles:
235
+ initialize_inherited_file_handles_nolock();
236
+
237
+ // Finally, initialize the stdio handles, if they were not inherited:
238
+ initialize_stdio_handles_nolock();
239
+ result = true;
240
+ }
241
+ __finally
242
+ {
243
+ __acrt_unlock(__acrt_lowio_index_lock);
244
+ }
245
+
246
+ return result;
247
+ }
248
+
249
+
250
+
251
+ // Shuts down the lowio library, freeing all allocated memory and destroying all
252
+ // created synchronization objects.
253
+ extern "C" bool __cdecl __acrt_uninitialize_lowio(bool const /* terminating */)
254
+ {
255
+ for (size_t i = 0; i < IOINFO_ARRAYS; ++i)
256
+ {
257
+ if (!__pioinfo[i])
258
+ continue;
259
+
260
+ __acrt_lowio_destroy_handle_array(__pioinfo[i]);
261
+ __pioinfo[i] = nullptr;
262
+ }
263
+
264
+ return true;
265
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/isatty.cpp ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // isatty.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _isatty(), which tests whether a file refers to a character device.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+
10
+ // VSO 41542369 - Ruby tries to locate the __pioinfo pointer by looking
11
+ // backwards from the ret instruction. This is not supported in general,
12
+ // but in addition Ruby expects an [add rsp] instruction before the ret.
13
+ // Whether that pattern exists depends on what optimizations are applied.
14
+ // This creates AppCompat issues as Ruby based applications abort at start.
15
+ // We work around it having _isatty being a jmp to the real fucntion,
16
+ // followed by non-reachable code that Ruby looks for.
17
+ // See also isatty_ruby.asm
18
+
19
+ // Tests if the given file refers to a character device (e.g. terminal, console,
20
+ // printer, serial port, etc.). Returns nonzero if so; zero if not.
21
+
22
+ #if defined(_ISATTY_ASM) && !defined(_ARM64EC_)
23
+ extern "C" int __cdecl _isatty_proc(int const fh)
24
+ #else
25
+ extern "C" int __cdecl _isatty(int const fh)
26
+ #endif
27
+ {
28
+ _CHECK_FH_RETURN(fh, EBADF, 0);
29
+ _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, 0);
30
+
31
+ return static_cast<int>(_osfile(fh) & FDEV);
32
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/locking.cpp ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // locking.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _locking(), which locks and unlocks regions of a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <sys\locking.h>
10
+
11
+
12
+
13
+ static int __cdecl locking_nolock(int const fh, int const locking_mode, long const number_of_bytes) throw()
14
+ {
15
+ __int64 const lock_offset = _lseeki64_nolock(fh, 0L, SEEK_CUR);
16
+ if (lock_offset == -1)
17
+ return -1;
18
+
19
+ OVERLAPPED overlapped = { 0 };
20
+ overlapped.Offset = static_cast<DWORD>(lock_offset);
21
+ overlapped.OffsetHigh = static_cast<DWORD>((lock_offset >> 32) & 0xffffffff);
22
+
23
+ // Set the retry count, based on the mode:
24
+ bool const allow_retry = locking_mode == _LK_LOCK || locking_mode == _LK_RLCK;
25
+ int const retry_count = allow_retry ? 10 : 1;
26
+
27
+ // Ask the OS to lock the file either until the request succeeds or the
28
+ // retry count is reached, whichever comes first. Note that the only error
29
+ // possible is a locking violation, since an invalid handle would have
30
+ // already failed above.
31
+ bool succeeded = false;
32
+ for (int i = 0; i != retry_count; ++i)
33
+ {
34
+ if (locking_mode == _LK_UNLCK)
35
+ {
36
+ succeeded = UnlockFileEx(
37
+ reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
38
+ 0,
39
+ number_of_bytes,
40
+ 0,
41
+ &overlapped) == TRUE;
42
+ }
43
+ else
44
+ {
45
+ // Ensure exclusive lock access, and return immediately if lock
46
+ // acquisition fails:
47
+ succeeded = LockFileEx(
48
+ reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
49
+ LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
50
+ 0,
51
+ number_of_bytes,
52
+ 0,
53
+ &overlapped) == TRUE;
54
+ }
55
+
56
+ if (succeeded)
57
+ {
58
+ break;
59
+ }
60
+
61
+ // Doesnt sleep on the last try
62
+ if (i != retry_count - 1)
63
+ {
64
+ Sleep(1000);
65
+ }
66
+ }
67
+
68
+ // If an OS error occurred (e.g., if the file was already locked), return
69
+ // EDEADLOCK if this was ablocking call; otherwise map the error noramlly:
70
+ if (!succeeded)
71
+ {
72
+ __acrt_errno_map_os_error(GetLastError());
73
+ if (locking_mode == _LK_LOCK || locking_mode == _LK_RLCK)
74
+ {
75
+ errno = EDEADLOCK;
76
+ }
77
+
78
+ return -1;
79
+ }
80
+
81
+ return 0;
82
+ }
83
+
84
+
85
+
86
+ // Locks or unlocks the requested number of bytes in the specified file.
87
+ //
88
+ // Note that this function acquires the lock for the specified file and holds
89
+ // this lock for the entire duration of the call, even during the one second
90
+ // delays between calls into the operating system. This is to prevent other
91
+ // threads from changing the file during the call.
92
+ //
93
+ // Returns 0 on success; returns -1 and sets errno on failure.
94
+ extern "C" int __cdecl _locking(int const fh, int const locking_mode, long const number_of_bytes)
95
+ {
96
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
97
+ _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
98
+ _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
99
+ _VALIDATE_CLEAR_OSSERR_RETURN(number_of_bytes >= 0, EINVAL, -1);
100
+
101
+ __acrt_lowio_lock_fh(fh);
102
+ int result = -1;
103
+ __try
104
+ {
105
+ if ((_osfile(fh) & FOPEN) == 0)
106
+ {
107
+ errno = EBADF;
108
+ _doserrno = 0;
109
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
110
+ __leave;
111
+ }
112
+
113
+ result = locking_nolock(fh, locking_mode, number_of_bytes);
114
+ }
115
+ __finally
116
+ {
117
+ __acrt_lowio_unlock_fh(fh);
118
+ }
119
+ return result;
120
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/lseek.cpp ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // lseek.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _lseek(), which seeks the file pointer of a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_ptd_propagation.h>
10
+ #include <stdlib.h>
11
+
12
+ #if SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END
13
+ #error Xenix and Win32 seek constants not compatible
14
+ #endif
15
+
16
+
17
+
18
+ // These functions actually seek the underlying operating system file handle.
19
+ // The logic is different for 32-bit and 64-bit seek values because a seek may
20
+ // end up moving out of range of 32-bit values.
21
+ static long __cdecl common_lseek_do_seek_nolock(HANDLE const os_handle, long const offset, int const origin, __crt_cached_ptd_host& ptd) throw()
22
+ {
23
+ LARGE_INTEGER const origin_pos = { 0 };
24
+
25
+ LARGE_INTEGER saved_pos;
26
+ if (!SetFilePointerEx(os_handle, origin_pos, &saved_pos, FILE_CURRENT))
27
+ {
28
+ __acrt_errno_map_os_error_ptd(GetLastError(), ptd);
29
+ return -1;
30
+ }
31
+
32
+ LARGE_INTEGER seek_pos = { 0 };
33
+ seek_pos.QuadPart = offset;
34
+
35
+ LARGE_INTEGER new_pos = { 0 };
36
+ if (!SetFilePointerEx(os_handle, seek_pos, &new_pos, origin))
37
+ {
38
+ __acrt_errno_map_os_error_ptd(GetLastError(), ptd);
39
+ return -1;
40
+ }
41
+
42
+ // The call succeeded, but the new file pointer location is too large for
43
+ // the return type or is a negative value. So, restore the file pointer
44
+ // to the saved location and return an error:
45
+ if (new_pos.QuadPart > LONG_MAX)
46
+ {
47
+ SetFilePointerEx(os_handle, saved_pos, nullptr, FILE_BEGIN);
48
+ ptd.get_errno().set(EINVAL);
49
+ return -1;
50
+ }
51
+
52
+ return static_cast<long>(new_pos.LowPart);
53
+ }
54
+
55
+ static __int64 __cdecl common_lseek_do_seek_nolock(HANDLE const os_handle, __int64 const offset, int const origin, __crt_cached_ptd_host& ptd) throw()
56
+ {
57
+ LARGE_INTEGER new_pos;
58
+ if (!SetFilePointerEx(os_handle, *reinterpret_cast<LARGE_INTEGER const*>(&offset), &new_pos, origin))
59
+ {
60
+ __acrt_errno_map_os_error_ptd(GetLastError(), ptd);
61
+ return -1;
62
+ }
63
+
64
+ return new_pos.QuadPart;
65
+ }
66
+
67
+
68
+ template <typename Integer>
69
+ static Integer __cdecl common_lseek_nolock(int const fh, Integer const offset, int const origin, __crt_cached_ptd_host& ptd) throw()
70
+ {
71
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_get_osfhandle(fh));
72
+ if (os_handle == reinterpret_cast<HANDLE>(-1))
73
+ {
74
+ ptd.get_errno().set(EBADF);
75
+ _ASSERTE(("Invalid file descriptor",0));
76
+ return -1;
77
+ }
78
+
79
+ Integer const new_position = common_lseek_do_seek_nolock(os_handle, offset, origin, ptd);
80
+ if (new_position == -1)
81
+ {
82
+ return -1;
83
+ }
84
+
85
+ // The call succeeded, so return success:
86
+ _osfile(fh) &= ~FEOFLAG; // Clear the Ctrl-Z flag
87
+ return new_position;
88
+ }
89
+
90
+ // Moves the file pointer associated with the given file to a new position. The
91
+ // new position is 'offset' bytes (the offset may be negative) away from the
92
+ // origin specified by 'origin'.
93
+ //
94
+ // If origin == SEEK_SET, the origin in the beginning of file
95
+ // If origin == SEEK_CUR, the origin is the current file pointer position
96
+ // If origin == SEEK_END, the origin is the end of the file
97
+ //
98
+ // Returns the offset, in bytes, of the new position, from the beginning of the
99
+ // file. Returns -1 and sets errno on failure. Note that seeking beyond the
100
+ // end of the file is not an error, but seeking before the beginning is.
101
+ template <typename Integer>
102
+ static Integer __cdecl common_lseek(int const fh, Integer const offset, int const origin, __crt_cached_ptd_host& ptd) throw()
103
+ {
104
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
105
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
106
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, _osfile(fh) & FOPEN, EBADF, -1);
107
+
108
+ __acrt_lowio_lock_fh(fh);
109
+ Integer result = -1;
110
+ __try
111
+ {
112
+ if ((_osfile(fh) & FOPEN) == 0)
113
+ {
114
+ ptd.get_errno().set(EBADF);
115
+ ptd.get_doserrno().set(0);
116
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
117
+ __leave;
118
+ }
119
+
120
+ result = common_lseek_nolock(fh, offset, origin, ptd);
121
+ }
122
+ __finally
123
+ {
124
+ __acrt_lowio_unlock_fh(fh);
125
+ }
126
+ return result;
127
+ }
128
+
129
+ extern "C" long __cdecl _lseek_internal(int const fh, long const offset, int const origin, __crt_cached_ptd_host& ptd)
130
+ {
131
+ return common_lseek(fh, offset, origin, ptd);
132
+ }
133
+
134
+ extern "C" __int64 __cdecl _lseeki64_internal(int const fh, __int64 const offset, int const origin, __crt_cached_ptd_host& ptd)
135
+ {
136
+ return common_lseek(fh, offset, origin, ptd);
137
+ }
138
+
139
+ extern "C" long __cdecl _lseek_nolock_internal(int const fh, long const offset, int const origin, __crt_cached_ptd_host& ptd)
140
+ {
141
+ return common_lseek_nolock(fh, offset, origin, ptd);
142
+ }
143
+
144
+ extern "C" __int64 __cdecl _lseeki64_nolock_internal(int const fh, __int64 const offset, int const origin, __crt_cached_ptd_host& ptd)
145
+ {
146
+ return common_lseek_nolock(fh, offset, origin, ptd);
147
+ }
148
+
149
+ extern "C" long __cdecl _lseek(int const fh, long const offset, int const origin)
150
+ {
151
+ __crt_cached_ptd_host ptd;
152
+ return common_lseek(fh, offset, origin, ptd);
153
+ }
154
+
155
+ extern "C" __int64 __cdecl _lseeki64(int const fh, __int64 const offset, int const origin)
156
+ {
157
+ __crt_cached_ptd_host ptd;
158
+ return common_lseek(fh, offset, origin, ptd);
159
+ }
160
+
161
+ extern "C" long __cdecl _lseek_nolock(int const fh, long const offset, int const origin)
162
+ {
163
+ __crt_cached_ptd_host ptd;
164
+ return common_lseek_nolock(fh, offset, origin, ptd);
165
+ }
166
+
167
+ extern "C" __int64 __cdecl _lseeki64_nolock(int const fh, __int64 const offset, int const origin)
168
+ {
169
+ __crt_cached_ptd_host ptd;
170
+ return common_lseek_nolock(fh, offset, origin, ptd);
171
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/mktemp.cpp ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // mktemp.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _tmktemp() and _tmktemp_s(), which create unique file names.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_securecrt.h>
10
+ #include <mbctype.h>
11
+ #include <process.h>
12
+ #include <stddef.h>
13
+ #include <stdio.h>
14
+
15
+
16
+
17
+ // These functions test whether we should continue scanning for X's.
18
+ static bool common_mktemp_s_continue(char const* const string, char const* const current) throw()
19
+ {
20
+ return !_ismbstrail(
21
+ reinterpret_cast<unsigned char const*>(string),
22
+ reinterpret_cast<unsigned char const*>(current));
23
+ }
24
+
25
+ static bool common_mktemp_s_continue(wchar_t const* const, wchar_t const* const) throw()
26
+ {
27
+ return true;
28
+ }
29
+
30
+
31
+
32
+ // Creates a unique file name given a template string of the form "fnamXXXXXX".
33
+ // The "XXXXXX" sequence is replaced by a letter and the five-digit thread
34
+ // identifier.
35
+ //
36
+ // The template string is modified in-place. Returns 0 on success; returns an
37
+ // errno error code on failure.
38
+ template <typename Character>
39
+ _Success_(return == 0)
40
+ static errno_t __cdecl common_mktemp_s(
41
+ _Inout_updates_z_(buffer_size_in_chars) Character* const template_string,
42
+ size_t const buffer_size_in_chars
43
+ ) throw()
44
+ {
45
+ typedef __crt_char_traits<Character> traits;
46
+
47
+ _VALIDATE_RETURN_ERRCODE(template_string != nullptr && buffer_size_in_chars > 0, EINVAL);
48
+
49
+ size_t const template_string_length = traits::tcsnlen(template_string, buffer_size_in_chars);
50
+ if (template_string_length >= buffer_size_in_chars)
51
+ {
52
+ _RESET_STRING(template_string, buffer_size_in_chars);
53
+ _RETURN_DEST_NOT_NULL_TERMINATED(template_string, buffer_size_in_chars);
54
+ }
55
+ _FILL_STRING(template_string, buffer_size_in_chars, template_string_length + 1);
56
+
57
+ if(template_string_length < 6 || buffer_size_in_chars <= template_string_length)
58
+ {
59
+ _RESET_STRING(template_string, buffer_size_in_chars);
60
+ _VALIDATE_RETURN_ERRCODE(("Incorrect Input for mktemp", 0), EINVAL);
61
+ }
62
+
63
+ // The Win32 thread identifier is unique across all threads in all processes.
64
+ // Note, however, that unlike *NIX process identifiers, which are not reused
65
+ // until all values up to 32K have been used, Win32 thread identifiers are
66
+ // frequenty reused and usually have small numbers.
67
+ unsigned number = GetCurrentThreadId();
68
+
69
+ // 'string' points to the null-terminator:
70
+ Character* string = template_string + template_string_length;
71
+
72
+ size_t template_length = 0;
73
+
74
+ // Replace the last five 'X' characters with the number:
75
+ while (--string >= template_string
76
+ && common_mktemp_s_continue(template_string, string)
77
+ && *string == 'X'
78
+ && template_length < 5)
79
+ {
80
+ ++template_length;
81
+ *string = static_cast<Character>((number % 10) + '0');
82
+ number /= 10;
83
+ }
84
+
85
+ // Too few X's?
86
+ if (*string != 'X' || template_length < 5)
87
+ {
88
+ _RESET_STRING(template_string, buffer_size_in_chars);
89
+ _VALIDATE_RETURN_ERRCODE(("Incorrect Input for mktemp", 0), EINVAL);
90
+ }
91
+
92
+ // Finally, add the letter:
93
+ Character letter = 'a';
94
+
95
+ *string = letter++;
96
+
97
+ errno_t const saved_errno = errno;
98
+ errno = 0;
99
+
100
+ // Test each letter, from a-z, until we find a name that is not yet used:
101
+ while (traits::taccess_s(template_string, 0) == 0)
102
+ {
103
+ if (letter == 'z' + 1)
104
+ {
105
+ _RESET_STRING(template_string, buffer_size_in_chars);
106
+ errno = EEXIST;
107
+ return errno;
108
+ }
109
+
110
+ *string = letter++;
111
+ errno = 0;
112
+ }
113
+
114
+ // Restore the old value of errno and return success:
115
+ errno = saved_errno;
116
+ return 0;
117
+ }
118
+
119
+ extern "C" errno_t __cdecl _mktemp_s(
120
+ char* const template_string,
121
+ size_t const buffer_size_in_chars
122
+ )
123
+ {
124
+ return common_mktemp_s(template_string, buffer_size_in_chars);
125
+ }
126
+
127
+ extern "C" errno_t __cdecl _wmktemp_s(
128
+ wchar_t* const template_string,
129
+ size_t const buffer_size_in_chars
130
+ )
131
+ {
132
+ return common_mktemp_s(template_string, buffer_size_in_chars);
133
+ }
134
+
135
+
136
+
137
+ // Creates a unique file name given a template string of the form "fnamXXXXXX".
138
+ // The "XXXXXX" sequence is replaced by a letter and the five-digit thread
139
+ // identifier. The template string is modified in-place.
140
+ //
141
+ // On success, returns the pointer to the modified template string. On failure,
142
+ // nullptr is returned (e.g. if the template string is malformed or there are no
143
+ // more unique names).
144
+ template <typename Character>
145
+ static Character* __cdecl common_mktemp(
146
+ Character* const template_string
147
+ ) throw()
148
+ {
149
+ typedef __crt_char_traits<Character> traits;
150
+
151
+ _VALIDATE_RETURN(template_string != nullptr, EINVAL, nullptr);
152
+
153
+ errno_t const result = common_mktemp_s(
154
+ template_string,
155
+ static_cast<size_t>(traits::tcslen(template_string) + 1));
156
+
157
+ return result == 0 ? template_string : nullptr;
158
+ }
159
+
160
+ extern "C" char* __cdecl _mktemp(char* const template_string)
161
+ {
162
+ return common_mktemp(template_string);
163
+ }
164
+
165
+ extern "C" wchar_t* __cdecl _wmktemp(wchar_t* const template_string)
166
+ {
167
+ return common_mktemp(template_string);
168
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/open.cpp ADDED
@@ -0,0 +1,907 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // open.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _open() and its friends, who are used to open or create files.
7
+ //
8
+ // These functions are used to open a file.
9
+ //
10
+ // oflag: The primary file open flags are passed via this parameter. It may
11
+ // have a combination of the following flags:
12
+ // * _O_APPEND: Reposition file ptr to end before every write
13
+ // * _O_BINARY: Open in binary mode
14
+ // * _O_CREAT: Create a new file* no effect if file already exists
15
+ // * _O_EXCL: Return error if file exists, only use with O_CREAT
16
+ // * _O_RDONLY: Open for reading only
17
+ // * _O_RDWR: Open for reading and writing
18
+ // * _O_TEXT: Open in text mode
19
+ // * _O_TRUNC: Open and truncate to 0 length (must have write permission)
20
+ // * _O_WRONLY: Open for writing only
21
+ // * _O_NOINHERIT: Handle will not be inherited by child processes.
22
+ // Exactly one of _O_RDONLY, _O_WRONLY, and _O_RDWR must be present.
23
+ //
24
+ // shflag: Specifies the sharing options with which the file is to be opened.
25
+ // This parameter is only supported by the sharing-enabled open
26
+ // functions (_tsopen, _tsopen_s, etc.). The following flags are
27
+ // supported:
28
+ // * _SH_COMPAT: Set compatability mode
29
+ // * _SH_DENYRW: Deny read and write access to the file
30
+ // * _SH_DENYWR: Deny write access to the file
31
+ // * _SH_DENYRD: Deny read access to the file
32
+ // * _SH_DENYNO: Permit read and write access
33
+ //
34
+ // pmode: The pmode argument is only required when _O_CREAT is specified. Its
35
+ // flags are as follows:
36
+ // * _S_IWRITE:
37
+ // * _S_IREAD:
38
+ // These flags may be combined (_S_IWRITE | _S_IREAD) to enable both
39
+ // reading and writing. The current file permission mask is applied to
40
+ // pmode before setting the permission (see umask).
41
+ //
42
+ // Functions that return an errno_t return 0 on success and an error code on
43
+ // failure. Functions that return an int return the file handle on success, and
44
+ // return -1 and set errno on failure.
45
+ //
46
+ #include <corecrt_internal_lowio.h>
47
+ #include <stdarg.h>
48
+ #include <stdio.h>
49
+ #include <stdlib.h>
50
+ #include <sys/stat.h>
51
+
52
+
53
+
54
+ namespace
55
+ {
56
+ DWORD const GENERIC_READ_WRITE = (GENERIC_READ | GENERIC_WRITE);
57
+
58
+ struct file_options
59
+ {
60
+ // These are the flags that are used for the osflag of the CRT file
61
+ // object that is created.
62
+ char crt_flags;
63
+
64
+ // These are the flags that are eventually passed to CreateFile to tell
65
+ // the Operating System how to create the file:
66
+ DWORD access;
67
+ DWORD create;
68
+ DWORD share;
69
+ DWORD attributes;
70
+ DWORD flags;
71
+ };
72
+ }
73
+
74
+
75
+
76
+ #define UTF16LE_BOM 0xFEFF // UTF16 Little Endian Byte Order Mark
77
+ #define UTF16BE_BOM 0xFFFE // UTF16 Big Endian Byte Order Mark
78
+ #define BOM_MASK 0xFFFF // Mask for testing Byte Order Mark
79
+ #define UTF8_BOM 0xBFBBEF // UTF8 Byte Order Mark
80
+ #define UTF16_BOMLEN 2 // No of Bytes in a UTF16 BOM
81
+ #define UTF8_BOMLEN 3 // No of Bytes in a UTF8 BOM
82
+
83
+ template <typename Character>
84
+ static int __cdecl common_open(
85
+ _In_z_ Character const* const path,
86
+ int const oflag,
87
+ int const pmode
88
+ ) throw()
89
+ {
90
+ typedef __crt_char_traits<Character> traits;
91
+
92
+ _VALIDATE_RETURN(path != nullptr, EINVAL, -1);
93
+
94
+ int fh = -1;
95
+ int unlock_flag = 0;
96
+ errno_t error_code = 0;
97
+ __try
98
+ {
99
+ error_code = traits::tsopen_nolock(&unlock_flag, &fh, path, oflag, _SH_DENYNO, pmode, 0);
100
+ }
101
+ __finally
102
+ {
103
+ if (unlock_flag)
104
+ {
105
+ if (error_code)
106
+ {
107
+ _osfile(fh) &= ~FOPEN;
108
+ }
109
+
110
+ __acrt_lowio_unlock_fh(fh);
111
+ }
112
+ }
113
+
114
+ if (error_code != 0)
115
+ {
116
+ errno = error_code;
117
+ return -1;
118
+ }
119
+
120
+ return fh;
121
+ }
122
+
123
+ extern "C" int _open(char const* const path, int const oflag, ...)
124
+ {
125
+ va_list arglist;
126
+ va_start(arglist, oflag);
127
+ int const pmode = va_arg(arglist, int);
128
+ va_end(arglist);
129
+
130
+ return common_open(path, oflag, pmode);
131
+ }
132
+
133
+ extern "C" int _wopen(wchar_t const* const path, int const oflag, ...)
134
+ {
135
+ va_list arglist;
136
+ va_start(arglist, oflag);
137
+ int const pmode = va_arg(arglist, int);
138
+ va_end(arglist);
139
+
140
+ return common_open(path, oflag, pmode);
141
+ }
142
+
143
+
144
+
145
+ template <typename Character>
146
+ static errno_t __cdecl common_sopen_dispatch(
147
+ _In_z_ Character const* const path,
148
+ int const oflag,
149
+ int const shflag,
150
+ int const pmode,
151
+ int* const pfh,
152
+ int const secure
153
+ ) throw()
154
+ {
155
+ typedef __crt_char_traits<Character> traits;
156
+
157
+ _VALIDATE_RETURN_ERRCODE(pfh != nullptr, EINVAL);
158
+ *pfh = -1;
159
+
160
+ _VALIDATE_RETURN_ERRCODE(path != nullptr, EINVAL);
161
+
162
+ if(secure)
163
+ {
164
+ _VALIDATE_RETURN_ERRCODE((pmode & (~(_S_IREAD | _S_IWRITE))) == 0, EINVAL);
165
+ }
166
+
167
+
168
+ int unlock_flag = 0;
169
+ errno_t error_code = 0;
170
+ __try
171
+ {
172
+ error_code = traits::tsopen_nolock(&unlock_flag, pfh, path, oflag, shflag, pmode, secure);
173
+ }
174
+ __finally
175
+ {
176
+ if (unlock_flag)
177
+ {
178
+ if (error_code)
179
+ {
180
+ _osfile(*pfh) &= ~FOPEN;
181
+ }
182
+ __acrt_lowio_unlock_fh(*pfh);
183
+ }
184
+ }
185
+
186
+ if (error_code != 0)
187
+ {
188
+ *pfh = -1;
189
+ }
190
+
191
+ return error_code;
192
+ }
193
+
194
+ extern "C" errno_t __cdecl _sopen_dispatch(
195
+ char const* const path,
196
+ int const oflag,
197
+ int const shflag,
198
+ int const pmode,
199
+ int* const pfh,
200
+ int const secure
201
+ )
202
+ {
203
+ return common_sopen_dispatch(path, oflag, shflag, pmode, pfh, secure);
204
+ }
205
+
206
+ extern "C" errno_t __cdecl _wsopen_dispatch(
207
+ wchar_t const* const path,
208
+ int const oflag,
209
+ int const shflag,
210
+ int const pmode,
211
+ int* const pfh,
212
+ int const secure
213
+ )
214
+ {
215
+ return common_sopen_dispatch(path, oflag, shflag, pmode, pfh, secure);
216
+ }
217
+
218
+
219
+
220
+ static HANDLE __cdecl create_file(
221
+ PCWSTR const path,
222
+ SECURITY_ATTRIBUTES* const security_attributes,
223
+ file_options const options
224
+ ) throw()
225
+ {
226
+ return CreateFileW(
227
+ path,
228
+ options.access,
229
+ options.share,
230
+ security_attributes,
231
+ options.create,
232
+ options.flags | options.attributes,
233
+ nullptr);
234
+ }
235
+
236
+
237
+
238
+ static DWORD decode_access_flags(int const oflag) throw()
239
+ {
240
+ switch (oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR))
241
+ {
242
+ case _O_RDONLY:
243
+ return GENERIC_READ;
244
+
245
+ case _O_WRONLY:
246
+ // If the file is being opened in append mode, we give read access as
247
+ // well because in append (a, not a+) mode, we need to read the BOM to
248
+ // determine the encoding (ANSI, UTF-8, or UTF-16).
249
+ if ((oflag & _O_APPEND) && (oflag & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT)) != 0)
250
+ return GENERIC_READ | GENERIC_WRITE;
251
+
252
+ return GENERIC_WRITE;
253
+
254
+ case _O_RDWR:
255
+ return GENERIC_READ | GENERIC_WRITE;
256
+ }
257
+
258
+ // This is unreachable, but the compiler can't tell.
259
+ _VALIDATE_RETURN(("Invalid open flag", 0), EINVAL, static_cast<DWORD>(-1));
260
+ return 0;
261
+ }
262
+
263
+ static DWORD decode_open_create_flags(int const oflag) throw()
264
+ {
265
+ switch (oflag & (_O_CREAT | _O_EXCL | _O_TRUNC))
266
+ {
267
+ case 0:
268
+ case _O_EXCL: // ignore EXCL w/o CREAT
269
+ return OPEN_EXISTING;
270
+
271
+ case _O_CREAT:
272
+ return OPEN_ALWAYS;
273
+
274
+ case _O_CREAT | _O_EXCL:
275
+ case _O_CREAT | _O_TRUNC | _O_EXCL:
276
+ return CREATE_NEW;
277
+
278
+ case _O_TRUNC:
279
+ case _O_TRUNC | _O_EXCL: // ignore EXCL w/o CREAT
280
+ return TRUNCATE_EXISTING;
281
+
282
+ case _O_CREAT | _O_TRUNC:
283
+ return CREATE_ALWAYS;
284
+ }
285
+
286
+ // This is unreachable, but the compiler can't tell.
287
+ _VALIDATE_RETURN(("Invalid open flag", 0), EINVAL, static_cast<DWORD>(-1));
288
+ return 0;
289
+ }
290
+
291
+ static DWORD decode_sharing_flags(int const shflag, int const access) throw()
292
+ {
293
+ switch (shflag)
294
+ {
295
+ case _SH_DENYRW:
296
+ return 0;
297
+
298
+ case _SH_DENYWR:
299
+ return FILE_SHARE_READ;
300
+
301
+ case _SH_DENYRD:
302
+ return FILE_SHARE_WRITE;
303
+
304
+ case _SH_DENYNO:
305
+ return FILE_SHARE_READ | FILE_SHARE_WRITE;
306
+
307
+ case _SH_SECURE:
308
+ if (access == GENERIC_READ)
309
+ return FILE_SHARE_READ;
310
+ else
311
+ return 0;
312
+ }
313
+
314
+ _VALIDATE_RETURN(("Invalid sharing flag", 0), EINVAL, static_cast<DWORD>(-1));
315
+ return 0;
316
+ }
317
+
318
+ static bool is_text_mode(int const oflag) throw()
319
+ {
320
+ if (oflag & _O_BINARY)
321
+ return false;
322
+
323
+ if (oflag & (_O_TEXT | _O_WTEXT | _O_U16TEXT | _O_U8TEXT))
324
+ return true;
325
+
326
+ // Finally, check the global default mode:
327
+ int fmode;
328
+ _ERRCHECK(_get_fmode(&fmode));
329
+ if (fmode != _O_BINARY)
330
+ return true;
331
+
332
+ return false;
333
+ }
334
+
335
+ static file_options decode_options(int const oflag, int const shflag, int const pmode) throw()
336
+ {
337
+ file_options result;
338
+ result.crt_flags = 0;
339
+ result.access = decode_access_flags(oflag);
340
+ result.create = decode_open_create_flags(oflag);
341
+ result.share = decode_sharing_flags(shflag, result.access);
342
+ result.attributes = FILE_ATTRIBUTE_NORMAL;
343
+ result.flags = 0;
344
+
345
+ if (oflag & _O_NOINHERIT)
346
+ {
347
+ result.crt_flags |= FNOINHERIT;
348
+ }
349
+
350
+ if (is_text_mode(oflag))
351
+ {
352
+ result.crt_flags |= FTEXT;
353
+ }
354
+
355
+ if (oflag & _O_CREAT)
356
+ {
357
+ if (((pmode & ~_umaskval) & _S_IWRITE) == 0)
358
+ result.attributes = FILE_ATTRIBUTE_READONLY;
359
+ }
360
+
361
+ if (oflag & _O_TEMPORARY)
362
+ {
363
+ result.flags |= FILE_FLAG_DELETE_ON_CLOSE;
364
+ result.access |= DELETE;
365
+ result.share |= FILE_SHARE_DELETE;
366
+ }
367
+
368
+ if (oflag & _O_SHORT_LIVED)
369
+ {
370
+ result.attributes |= FILE_ATTRIBUTE_TEMPORARY;
371
+ }
372
+
373
+ if (oflag & _O_OBTAIN_DIR)
374
+ {
375
+ result.flags |= FILE_FLAG_BACKUP_SEMANTICS;
376
+ }
377
+
378
+ if (oflag & _O_SEQUENTIAL)
379
+ {
380
+ result.flags |= FILE_FLAG_SEQUENTIAL_SCAN;
381
+ }
382
+ else if (oflag & _O_RANDOM)
383
+ {
384
+ result.flags |= FILE_FLAG_RANDOM_ACCESS;
385
+ }
386
+
387
+ return result;
388
+ }
389
+
390
+
391
+
392
+ // If we open a text mode file for writing, and the file ends in Ctrl+Z, we need
393
+ // to remove the Ctrl+Z character so that appending will work. We do this by
394
+ // seeking to the end of the file, testing if the last character is a Ctrl+Z,
395
+ // truncating the file if it is, then rewinding back to the beginning.
396
+ static errno_t truncate_ctrl_z_if_present(int const fh) throw()
397
+ {
398
+ // No truncation is possible for devices and pipes:
399
+ if (_osfile(fh) & (FDEV | FPIPE))
400
+ return 0;
401
+
402
+ // No truncation is necessary for binary files:
403
+ if ((_osfile(fh) & FTEXT) == 0)
404
+ return 0;
405
+
406
+ // Find the end of the file:
407
+ __int64 const last_char_position = _lseeki64_nolock(fh, -1, SEEK_END);
408
+
409
+ // If the seek failed, either the file is empty or an error occurred.
410
+ // (It's not an error if the file is empty.)
411
+ if (last_char_position == -1)
412
+ {
413
+ if (_doserrno == ERROR_NEGATIVE_SEEK)
414
+ return 0;
415
+
416
+ return errno;
417
+ }
418
+
419
+ // Read the last character. If the read succeeds and the character
420
+ // is a Ctrl+Z, remove the character from the file by shortening:
421
+ wchar_t c = 0;
422
+ if (_read_nolock(fh, &c, 1) == 0 && c == 26)
423
+ {
424
+ if (_chsize_nolock(fh, last_char_position) == -1)
425
+ return errno;
426
+ }
427
+
428
+ // Now, rewind the file pointer back to the beginning:
429
+ if (_lseeki64_nolock(fh, 0, SEEK_SET) == -1)
430
+ return errno;
431
+
432
+ return 0;
433
+ }
434
+
435
+
436
+
437
+ // Computes the text mode to be used for a file, using a combination of the
438
+ // options passed into the open function and the BOM read from the file.
439
+ static errno_t configure_text_mode(
440
+ int const fh,
441
+ file_options const options,
442
+ int oflag,
443
+ __crt_lowio_text_mode& text_mode
444
+ ) throw()
445
+ {
446
+ // The text mode is ANSI by default:
447
+ text_mode = __crt_lowio_text_mode::ansi;
448
+
449
+ // If the file is open in binary mode, it gets the default text mode:
450
+ if ((_osfile(fh) & FTEXT) == 0)
451
+ return 0;
452
+
453
+ // Set the default text mode per the oflag. The BOM may change the default,
454
+ // if one is present. If oflag does not specify a text mode, use the _fmode
455
+ // default:
456
+ DWORD const text_mode_mask = (_O_TEXT | _O_WTEXT | _O_U16TEXT | _O_U8TEXT);
457
+ if ((oflag & text_mode_mask) == 0)
458
+ {
459
+ int fmode = 0;
460
+ _ERRCHECK(_get_fmode(&fmode));
461
+
462
+ if ((fmode & text_mode_mask) == 0)
463
+ oflag |= _O_TEXT; // Default to ANSI.
464
+ else
465
+ oflag |= fmode & text_mode_mask;
466
+ }
467
+
468
+ // Now oflags should be set to one of the text modes:
469
+ _ASSERTE((oflag & text_mode_mask) != 0);
470
+
471
+ switch (oflag & text_mode_mask)
472
+ {
473
+ case _O_TEXT:
474
+ text_mode = __crt_lowio_text_mode::ansi;
475
+ break;
476
+
477
+ case _O_WTEXT:
478
+ case _O_WTEXT | _O_TEXT:
479
+ if ((oflag & (_O_WRONLY | _O_CREAT | _O_TRUNC)) == (_O_WRONLY | _O_CREAT | _O_TRUNC))
480
+ text_mode = __crt_lowio_text_mode::utf16le;
481
+ break;
482
+
483
+ case _O_U16TEXT:
484
+ case _O_U16TEXT | _O_TEXT:
485
+ text_mode = __crt_lowio_text_mode::utf16le;
486
+ break;
487
+
488
+ case _O_U8TEXT:
489
+ case _O_U8TEXT | _O_TEXT:
490
+ text_mode = __crt_lowio_text_mode::utf8;
491
+ break;
492
+ }
493
+
494
+
495
+ // If the file hasn't been opened with the UNICODE flags then we have
496
+ // nothing to do: the text mode is the default mode that we just set:
497
+ if ((oflag & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT)) == 0)
498
+ return 0;
499
+
500
+ // If this file refers to a device, we cannot check the BOM, so we have
501
+ // nothing to do: the text mode is the default mode that we just set:
502
+ if ((options.crt_flags & FDEV) != 0)
503
+ return 0;
504
+
505
+
506
+ // Determine whether we need to check or write the BOM, by testing the
507
+ // access with which the file was opened and whether the file already
508
+ // existed or was just created:
509
+ int check_bom = 0;
510
+ int write_bom = 0;
511
+ switch (options.access & GENERIC_READ_WRITE)
512
+ {
513
+ case GENERIC_READ:
514
+ check_bom = 1;
515
+ break;
516
+
517
+ case GENERIC_WRITE:
518
+ case GENERIC_READ_WRITE:
519
+ switch (options.create)
520
+ {
521
+ // If this file was opened, we will read the BOM if the file was opened
522
+ // with read/write access. We will write the BOM if and only if the
523
+ // file is empty:
524
+ case OPEN_EXISTING:
525
+ case OPEN_ALWAYS:
526
+ {
527
+ if (_lseeki64_nolock(fh, 0, SEEK_END) != 0)
528
+ {
529
+ if (_lseeki64_nolock(fh, 0, SEEK_SET) == -1)
530
+ return errno;
531
+
532
+ // If we have read access, then we need to check the BOM. Note
533
+ // that we've taken a shortcut here: if the file is empty, then
534
+ // we do not set this flag because the file doesn't have a BOM
535
+ // to be read.
536
+ check_bom = (options.access & GENERIC_READ) != 0;
537
+ }
538
+ else
539
+ {
540
+ write_bom = 1;
541
+ break;
542
+ }
543
+ break;
544
+ }
545
+
546
+ // If this is a new or truncated file, then we always write the BOM:
547
+ case CREATE_NEW:
548
+ case CREATE_ALWAYS:
549
+ case TRUNCATE_EXISTING:
550
+ {
551
+ write_bom = 1;
552
+ break;
553
+ }
554
+ }
555
+ break;
556
+ }
557
+
558
+ if (check_bom)
559
+ {
560
+ int bom = 0;
561
+ int const count = _read_nolock(fh, &bom, UTF8_BOMLEN);
562
+
563
+ // Intrernal validation: This branch should never be taken if write_bom
564
+ // is true and count > 0:
565
+ if (count > 0 && write_bom == 1)
566
+ {
567
+ _ASSERTE(0 && "Internal Error");
568
+ write_bom = 0;
569
+ }
570
+
571
+ switch (count)
572
+ {
573
+ case -1:
574
+ return errno;
575
+
576
+ case UTF8_BOMLEN:
577
+ if (bom == UTF8_BOM)
578
+ {
579
+ text_mode = __crt_lowio_text_mode::utf8;
580
+ break;
581
+ }
582
+
583
+ case UTF16_BOMLEN:
584
+ if((bom & BOM_MASK) == UTF16BE_BOM)
585
+ {
586
+ _ASSERTE(0 && "Only UTF-16 little endian & UTF-8 is supported for reads");
587
+ errno = EINVAL;
588
+ return errno;
589
+ }
590
+
591
+ if((bom & BOM_MASK) == UTF16LE_BOM)
592
+ {
593
+ // We have read three bytes, so we should seek back one byte:
594
+ if(_lseeki64_nolock(fh, UTF16_BOMLEN, SEEK_SET) == -1)
595
+ return errno;
596
+
597
+ text_mode = __crt_lowio_text_mode::utf16le;
598
+ break;
599
+ }
600
+
601
+ // Fall through to default case to lseek to beginning of file
602
+
603
+ default:
604
+ // The file has no BOM, so we seek back to the beginning:
605
+ if (_lseeki64_nolock(fh, 0, SEEK_SET) == -1)
606
+ return errno;
607
+
608
+ break;
609
+ }
610
+ }
611
+
612
+ if (write_bom)
613
+ {
614
+ // If we are creating a new file, we write a UTF-16LE or UTF8 BOM:
615
+ int bom_length = 0;
616
+ int bom = 0;
617
+ switch (text_mode)
618
+ {
619
+ case __crt_lowio_text_mode::utf16le:
620
+ {
621
+ bom = UTF16LE_BOM;
622
+ bom_length = UTF16_BOMLEN;
623
+ break;
624
+ }
625
+ case __crt_lowio_text_mode::utf8:
626
+ {
627
+ bom = UTF8_BOM;
628
+ bom_length = UTF8_BOMLEN;
629
+ break;
630
+ }
631
+ }
632
+
633
+ for (int total_written = 0; bom_length > total_written; )
634
+ {
635
+ char const* const bom_begin = reinterpret_cast<char const*>(&bom);
636
+
637
+ // Note that the call to write may write less than bom_length
638
+ // characters but not really fail. We retry until the write fails
639
+ // or we have written all of the characters:
640
+ int const written = _write(fh, bom_begin + total_written, bom_length - total_written);
641
+ if (written == -1)
642
+ return errno;
643
+
644
+ total_written += written;
645
+ }
646
+ }
647
+
648
+ return 0; // Success!
649
+ }
650
+
651
+
652
+
653
+ extern "C" errno_t __cdecl _wsopen_nolock(
654
+ int* const punlock_flag,
655
+ int* const pfh,
656
+ wchar_t const* const path,
657
+ int const oflag,
658
+ int const shflag,
659
+ int const pmode,
660
+ int const secure
661
+ )
662
+ {
663
+ UNREFERENCED_PARAMETER(secure);
664
+
665
+ // First, do the initial parse of the options. The only thing that can fail
666
+ // here is the parsing of the share options, in which case -1 is returned
667
+ // and errno is set.
668
+ file_options options = decode_options(oflag, shflag, pmode);
669
+ if (options.share == static_cast<DWORD>(-1))
670
+ {
671
+ _doserrno = 0;
672
+ *pfh = -1;
673
+ return errno;
674
+ }
675
+
676
+ // Allocate the CRT file handle. Note that if a handle is allocated, it is
677
+ // locked when it is returned by the allocation function. It is our caller's
678
+ // responsibility to unlock the file handle (we do not unlock it before
679
+ // returning).
680
+ *pfh = _alloc_osfhnd();
681
+ if (*pfh == -1)
682
+ {
683
+ _doserrno = 0;
684
+ *pfh = -1;
685
+ errno = EMFILE;
686
+ return errno;
687
+ }
688
+
689
+ // Beyond this point, do not change *pfh, even if an error occurs. Our
690
+ // caller requires the handle in order to release its lock.
691
+ *punlock_flag = 1;
692
+
693
+
694
+
695
+ SECURITY_ATTRIBUTES security_attributes;
696
+ security_attributes.nLength = sizeof(security_attributes);
697
+ security_attributes.lpSecurityDescriptor = nullptr;
698
+ security_attributes.bInheritHandle = (oflag & _O_NOINHERIT) == 0;
699
+
700
+
701
+ // Try to open or create the file:
702
+ HANDLE os_handle = create_file(path, &security_attributes, options);
703
+ if (os_handle == INVALID_HANDLE_VALUE)
704
+ {
705
+ if ((options.access & GENERIC_READ_WRITE) == GENERIC_READ_WRITE && (oflag & _O_WRONLY))
706
+ {
707
+ // The call may have failed because we may be trying to open
708
+ // something for reading that does not allow reading (e.g. a pipe or
709
+ // a device). So, we try again with just GENERIC_WRITE. If this
710
+ // succeeds, we will have to assume the default encoding because we
711
+ // will have no way to read the BOM.
712
+ options.access &= ~GENERIC_READ;
713
+
714
+ os_handle = create_file(path, &security_attributes, options);
715
+ }
716
+ }
717
+
718
+ if (os_handle == INVALID_HANDLE_VALUE)
719
+ {
720
+ // We failed to open the file. We need to free the CRT file handle, but
721
+ // we do not release the lock--our caller releases the lock.
722
+ _osfile(*pfh) &= ~FOPEN;
723
+ __acrt_errno_map_os_error(GetLastError());
724
+ return errno;
725
+ }
726
+
727
+ // Find out what type of file this is (e.g., file, device, pipe, etc.)
728
+ DWORD const file_type = GetFileType(os_handle);
729
+
730
+ if (file_type == FILE_TYPE_UNKNOWN)
731
+ {
732
+ DWORD const last_error = GetLastError();
733
+ __acrt_errno_map_os_error(last_error);
734
+
735
+ _osfile(*pfh) &= ~FOPEN;
736
+ CloseHandle(os_handle);
737
+
738
+ // If GetFileType returns FILE_TYPE_UNKNOWN but doesn't fail, the file
739
+ // type really is unknown. This function is not designed to handle
740
+ // unknown types of files, so we must return an error.
741
+ if (last_error == ERROR_SUCCESS)
742
+ errno = EACCES;
743
+
744
+ return errno;
745
+ }
746
+
747
+ if (file_type == FILE_TYPE_CHAR)
748
+ {
749
+ options.crt_flags |= FDEV;
750
+ }
751
+ else if (file_type == FILE_TYPE_PIPE)
752
+ {
753
+ options.crt_flags |= FPIPE;
754
+ }
755
+
756
+ // The file is open and valid. Set the OS handle:
757
+ __acrt_lowio_set_os_handle(*pfh, reinterpret_cast<intptr_t>(os_handle));
758
+
759
+
760
+ // Mark the handle as open, and store the flags we gathered so far:
761
+ options.crt_flags |= FOPEN;
762
+ _osfile(*pfh) = options.crt_flags;
763
+
764
+
765
+ // The text mode is set to ANSI by default. If we find a BOM, then we will
766
+ // reset this to the appropriate type (this check happens below).
767
+ _textmode(*pfh) = __crt_lowio_text_mode::ansi;
768
+
769
+
770
+ // If the text mode file is opened for writing and allows reading, remove
771
+ // any trailing Ctrl+Z character, if present, to ensure appending works:
772
+ if (oflag & _O_RDWR)
773
+ {
774
+ errno_t const result = truncate_ctrl_z_if_present(*pfh);
775
+ if (result != 0)
776
+ {
777
+ _close_nolock(*pfh);
778
+ return result;
779
+ }
780
+ }
781
+
782
+ // Configure the text mode:
783
+ __crt_lowio_text_mode text_mode = __crt_lowio_text_mode::ansi;
784
+ errno_t const text_mode_result = configure_text_mode(*pfh, options, oflag, text_mode);
785
+ if (text_mode_result != 0)
786
+ {
787
+ _close_nolock(*pfh);
788
+ return text_mode_result;
789
+ }
790
+
791
+ _textmode(*pfh) = text_mode;
792
+ _tm_unicode(*pfh) = (oflag & _O_WTEXT) != 0;
793
+
794
+
795
+ // Set FAPPEND flag if appropriate. Don't do this for devices or pipes:
796
+ if ((options.crt_flags & (FDEV | FPIPE)) == 0 && (oflag & _O_APPEND))
797
+ _osfile(*pfh) |= FAPPEND;
798
+
799
+
800
+ // Finally, if we were asked only to open the file with write access but we
801
+ // opened it with read and write access in order to read the BOM, close the
802
+ // file and re-open it with only write access:
803
+ if ((options.access & GENERIC_READ_WRITE) == GENERIC_READ_WRITE && (oflag & _O_WRONLY))
804
+ {
805
+ CloseHandle(os_handle);
806
+ options.access &= ~GENERIC_READ;
807
+ os_handle = create_file(path, &security_attributes, options);
808
+
809
+ if (os_handle == INVALID_HANDLE_VALUE)
810
+ {
811
+ // Note that we can't use the normal close function here because the
812
+ // file isn't really open anymore. We need only release the file
813
+ // handle by unsetting the FOPEN flag:
814
+ __acrt_errno_map_os_error(GetLastError());
815
+ _osfile(*pfh) &= ~FOPEN;
816
+ _free_osfhnd(*pfh);
817
+ return errno;
818
+ }
819
+ else
820
+ {
821
+ // We were able to open the file successfully, set the file
822
+ // handle in the _ioinfo structure, then we are done. All
823
+ // the options.crt_flags should have been set properly already.
824
+ _osfhnd(*pfh) = reinterpret_cast<intptr_t>(os_handle);
825
+ }
826
+ }
827
+
828
+ return 0; // Success!
829
+ }
830
+
831
+
832
+
833
+ extern "C" errno_t __cdecl _sopen_nolock(
834
+ int* const punlock_flag,
835
+ int* const pfh,
836
+ char const* const path,
837
+ int const oflag,
838
+ int const shflag,
839
+ int const pmode,
840
+ int const secure
841
+ )
842
+ {
843
+ // At this point we know path is not null already
844
+ __crt_internal_win32_buffer<wchar_t> wide_path;
845
+
846
+ errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage());
847
+
848
+ if (cvt != 0) {
849
+ return -1;
850
+ }
851
+
852
+ return _wsopen_nolock(punlock_flag, pfh, wide_path.data(), oflag, shflag, pmode, secure);
853
+ }
854
+
855
+
856
+
857
+ extern "C" int __cdecl _sopen(char const* const path, int const oflag, int const shflag, ...)
858
+ {
859
+ va_list ap;
860
+ va_start(ap, shflag);
861
+ int const pmode = va_arg(ap, int);
862
+ va_end(ap);
863
+
864
+ // The last argument is 0 so thta the pmode is not validated in open_s:
865
+ int fh = -1;
866
+ errno_t const result = _sopen_dispatch(path, oflag, shflag, pmode, &fh, FALSE);
867
+ return result ? -1 : fh;
868
+ }
869
+
870
+ extern "C" int __cdecl _wsopen(wchar_t const* const path, int const oflag, int const shflag, ...)
871
+ {
872
+ va_list ap;
873
+ va_start(ap, shflag);
874
+ int const pmode = va_arg(ap, int);
875
+ va_end(ap);
876
+
877
+ // The last argument is 0 so thta the pmode is not validated in open_s:
878
+ int fh = -1;
879
+ errno_t const result = _wsopen_dispatch(path, oflag, shflag, pmode, &fh, FALSE);
880
+ return result ? -1 : fh;
881
+ }
882
+
883
+
884
+
885
+ extern "C" errno_t __cdecl _sopen_s(
886
+ int* const pfh,
887
+ char const* const path,
888
+ int const oflag,
889
+ int const shflag,
890
+ int const pmode
891
+ )
892
+ {
893
+ // The last argument is 1 so that pmode is validated in open_s:
894
+ return _sopen_dispatch(path, oflag, shflag, pmode, pfh, TRUE);
895
+ }
896
+
897
+ extern "C" errno_t __cdecl _wsopen_s(
898
+ int* const pfh,
899
+ wchar_t const* const path,
900
+ int const oflag,
901
+ int const shflag,
902
+ int const pmode
903
+ )
904
+ {
905
+ // The last argument is 1 so that pmode is validated in open_s:
906
+ return _wsopen_dispatch(path, oflag, shflag, pmode, pfh, TRUE);
907
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/osfinfo.cpp ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // osfinfo.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the functions used to control allocation, locking, and freeing of CRT
7
+ // file handles.
8
+ //
9
+ #include <corecrt_internal_lowio.h>
10
+
11
+
12
+
13
+ extern "C" __crt_lowio_handle_data* __cdecl __acrt_lowio_create_handle_array()
14
+ {
15
+ __crt_unique_heap_ptr<__crt_lowio_handle_data> array(_calloc_crt_t(
16
+ __crt_lowio_handle_data,
17
+ IOINFO_ARRAY_ELTS));
18
+
19
+ if (!array)
20
+ return nullptr;
21
+
22
+ __crt_lowio_handle_data* const first = array.get();
23
+ __crt_lowio_handle_data* const last = first + IOINFO_ARRAY_ELTS;
24
+ for (auto it = first; it != last; ++it)
25
+ {
26
+ __acrt_InitializeCriticalSectionEx(&it->lock, _CORECRT_SPINCOUNT, 0);
27
+ it->osfhnd = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
28
+ it->startpos = 0;
29
+ it->osfile = 0;
30
+ it->textmode = __crt_lowio_text_mode::ansi;
31
+ it->_pipe_lookahead[0] = LF;
32
+ it->_pipe_lookahead[1] = LF;
33
+ it->_pipe_lookahead[2] = LF;
34
+ it->unicode = false;
35
+ it->utf8translations = false;
36
+ it->dbcsBufferUsed = false;
37
+ for (int i = 0; i < sizeof(it->mbBuffer); ++i)
38
+ {
39
+ it->mbBuffer[i] = '\0';
40
+ }
41
+ }
42
+
43
+ return array.detach();
44
+ }
45
+
46
+ extern "C" void __cdecl __acrt_lowio_destroy_handle_array(__crt_lowio_handle_data* const array)
47
+ {
48
+ if (!array)
49
+ return;
50
+
51
+ __crt_lowio_handle_data* const first = array;
52
+ __crt_lowio_handle_data* const last = first + IOINFO_ARRAY_ELTS;
53
+ for (auto it = first; it != last; ++it)
54
+ {
55
+ DeleteCriticalSection(&it->lock);
56
+ }
57
+
58
+ _free_crt(array);
59
+ }
60
+
61
+ // Ensures that a lowio handle data object has been created for file handle 'fh'.
62
+ // The 'fh' must be less than the hard maximum, _NHANDLE_. If 'fh' is already
63
+ // backed by a handle data object, this function has no effect. Otherwise, this
64
+ // function extends the global arrays of handle data objects until 'fh' is backed
65
+ // by a handle data object.
66
+ extern "C" errno_t __cdecl __acrt_lowio_ensure_fh_exists(int const fh)
67
+ {
68
+ _VALIDATE_RETURN_ERRCODE(static_cast<unsigned>(fh) < _NHANDLE_, EBADF);
69
+
70
+ errno_t status = 0;
71
+
72
+ __acrt_lock(__acrt_lowio_index_lock);
73
+ __try
74
+ {
75
+ for (size_t i = 0; fh >= _nhandle; ++i)
76
+ {
77
+ if (__pioinfo[i])
78
+ {
79
+ continue;
80
+ }
81
+
82
+ __pioinfo[i] = __acrt_lowio_create_handle_array();
83
+ if (!__pioinfo[i])
84
+ {
85
+ status = ENOMEM;
86
+ __leave;
87
+ }
88
+
89
+ _nhandle += IOINFO_ARRAY_ELTS;
90
+ }
91
+ }
92
+ __finally
93
+ {
94
+ __acrt_unlock(__acrt_lowio_index_lock);
95
+ }
96
+
97
+ return status;
98
+ }
99
+
100
+
101
+
102
+ // Allocates a CRT file handle. This function finds the first free entry in
103
+ // the arrays of file objects and returns the index of that entry (that index
104
+ // is the CRT file handle) to the caller. The FOPEN flag is set in the new
105
+ // entry, to pevent multithreaded race conditions and deadlocks.
106
+ //
107
+ // Returns the CRT file handle on success; returns -1 on failure (e.g. if no
108
+ // more file handles are available or if memory allocation is required but
109
+ // fails).
110
+ //
111
+ // MULTITHREADING NOTE: If this function is successful and returns a CRT file
112
+ // handle, the handle is locked when it is returned and the FOPEN flag has been
113
+ // set. The caller must be sure to release the lock, and if the caller abandons
114
+ // the file handle, it must clear the FOPEN flag to free the handle.
115
+ extern "C" int __cdecl _alloc_osfhnd()
116
+ {
117
+ __acrt_lock(__acrt_lowio_index_lock);
118
+ int result = -1;
119
+ __try
120
+ {
121
+ // Search the arrays of file objects, in order, looking for the first
122
+ // free entry. The compound index of this free entry is the return
123
+ // value.
124
+ //
125
+ // The compound index of the file object entry *(__pioinfo[i] + j) is
126
+ // k = i * IOINFO_ARRAY_ELTS + j.
127
+ for (int i = 0; i < IOINFO_ARRAYS; ++i)
128
+ {
129
+ // If this __crt_lowio_handle_data array does not yet exist, create a new one:
130
+ if (!__pioinfo[i])
131
+ {
132
+ __pioinfo[i] = __acrt_lowio_create_handle_array();
133
+ if (!__pioinfo[i])
134
+ __leave;
135
+
136
+ _nhandle += IOINFO_ARRAY_ELTS;
137
+
138
+ // The first element of the newly allocated array of handle data
139
+ // objects is our first free entry. Note that since we hold the
140
+ // index lock, no one else can allocate this handle.
141
+ int const fh = i * IOINFO_ARRAY_ELTS;
142
+
143
+ __acrt_lowio_lock_fh(fh);
144
+ _osfile(fh) = FOPEN;
145
+ result = fh;
146
+ __leave;
147
+ }
148
+
149
+ // Otherwise, this file object array already exists. Search it looking
150
+ // for the first free entry:
151
+ __crt_lowio_handle_data* const first = __pioinfo[i];
152
+ __crt_lowio_handle_data* const last = first + IOINFO_ARRAY_ELTS;
153
+ for (__crt_lowio_handle_data* pio = first; pio != last; ++pio)
154
+ {
155
+ if (pio->osfile & FOPEN)
156
+ continue;
157
+
158
+ // Another thread may have grabbed this file handle out from
159
+ // under us while we waited for the lock. If so, continue on
160
+ // searching through the array.
161
+ //
162
+ // CRT_REFACTOR TODO: Resolve lowio synchronization issues.
163
+ EnterCriticalSection(&pio->lock);
164
+ if ((pio->osfile & FOPEN) != 0)
165
+ {
166
+ LeaveCriticalSection(&pio->lock);
167
+ continue;
168
+ }
169
+
170
+ // Otherwise, this entry is ours: we hold the lock, so we can
171
+ // initialize it and return its handle:
172
+ int const fh = i * IOINFO_ARRAY_ELTS + static_cast<int>(pio - first);
173
+ _osfile(fh) = FOPEN;
174
+ _osfhnd(fh) = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
175
+ result = fh;
176
+ __leave;
177
+ }
178
+ }
179
+
180
+ // All entries are in use if we fall out of the loop. return -1 in this case (which result is already set to)
181
+ }
182
+ __finally
183
+ {
184
+ __acrt_unlock(__acrt_lowio_index_lock);
185
+ }
186
+ return result;
187
+ }
188
+
189
+
190
+
191
+ // Sets the Win32 HANDLE associated with the specified CRT file. Returns 0
192
+ // on success; returns -1 and sets errno on failure.
193
+ extern "C" int __cdecl __acrt_lowio_set_os_handle(int const fh, intptr_t const value)
194
+ {
195
+ if (fh >= 0 &&
196
+ static_cast<unsigned>(fh) < static_cast<unsigned>(_nhandle) &&
197
+ _osfhnd(fh) == reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE))
198
+ {
199
+ if (_query_app_type() == _crt_console_app)
200
+ {
201
+ HANDLE const handle_value = reinterpret_cast<HANDLE>(value);
202
+ switch (fh)
203
+ {
204
+ case 0: SetStdHandle(STD_INPUT_HANDLE, handle_value); break;
205
+ case 1: SetStdHandle(STD_OUTPUT_HANDLE, handle_value); break;
206
+ case 2: SetStdHandle(STD_ERROR_HANDLE, handle_value); break;
207
+ }
208
+ }
209
+
210
+ _osfhnd(fh) = value;
211
+ return 0 ;
212
+ }
213
+ else
214
+ {
215
+ errno = EBADF; // Bad handle
216
+ _doserrno = 0; // This is not an OS error
217
+ return -1;
218
+ }
219
+ }
220
+
221
+
222
+
223
+ // Marks the specified CRT file handle as free and available for allocation.
224
+ // Returns 0 on success; returns -1 and sets errno on failure.
225
+ extern "C" int __cdecl _free_osfhnd(int const fh)
226
+ {
227
+ if (fh >= 0 &&
228
+ static_cast<unsigned>(fh) < static_cast<unsigned>(_nhandle) &&
229
+ (_osfile(fh) & FOPEN) &&
230
+ _osfhnd(fh) != reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE))
231
+ {
232
+ if (_query_app_type() == _crt_console_app)
233
+ {
234
+ switch (fh)
235
+ {
236
+ case 0: SetStdHandle(STD_INPUT_HANDLE, nullptr); break;
237
+ case 1: SetStdHandle(STD_OUTPUT_HANDLE, nullptr); break;
238
+ case 2: SetStdHandle(STD_ERROR_HANDLE, nullptr); break;
239
+ }
240
+ }
241
+
242
+ _osfhnd(fh) = reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE);
243
+ return 0;
244
+ }
245
+ else
246
+ {
247
+ errno = EBADF; // Bad handle
248
+ _doserrno = 0; // This is not an OS error
249
+ return -1;
250
+ }
251
+ }
252
+
253
+
254
+
255
+ // Gets the Win32 HANDLE with which the given CRT file handle is associated.
256
+ // On success, returns the Win32 HANDLE; on failure, returns -1 and sets errno.
257
+ extern "C" intptr_t __cdecl _get_osfhandle(int const fh)
258
+ {
259
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1 );
260
+ _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
261
+ _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
262
+
263
+ return _osfhnd(fh);
264
+ }
265
+
266
+
267
+
268
+ // Allocates a free CRT file handle and associates it with the provided Win32
269
+ // HANDLE and sets its flags to the given flags. Returns the CRT file handle
270
+ // on success; returns -1 on failure.
271
+ extern "C" int __cdecl _open_osfhandle(intptr_t const osfhandle, int const source_flags)
272
+ {
273
+ // Copy relevant source_flags from second parameter
274
+ unsigned char file_flags = 0;
275
+
276
+ if (source_flags & _O_APPEND)
277
+ file_flags |= FAPPEND;
278
+
279
+ if (source_flags & _O_TEXT)
280
+ file_flags |= FTEXT;
281
+
282
+ if (source_flags & _O_NOINHERIT)
283
+ file_flags |= FNOINHERIT;
284
+
285
+ // Find out what type of file (file/device/pipe):
286
+ DWORD const file_type = GetFileType(reinterpret_cast<HANDLE>(osfhandle));
287
+ if (file_type == FILE_TYPE_UNKNOWN)
288
+ {
289
+ __acrt_errno_map_os_error(GetLastError());
290
+ return -1;
291
+ }
292
+
293
+ if (file_type == FILE_TYPE_CHAR)
294
+ file_flags |= FDEV;
295
+
296
+ else if (file_type == FILE_TYPE_PIPE)
297
+ file_flags |= FPIPE;
298
+
299
+ // Attempt to allocate a CRT file handle:
300
+ int const fh = _alloc_osfhnd();
301
+ if (fh == -1)
302
+ {
303
+ errno = EMFILE; // Too many open files
304
+ _doserrno = 0L; // This is not an OS error
305
+ return -1;
306
+ }
307
+
308
+ bool success = false;
309
+ __try
310
+ {
311
+ // The file is open. now set the info in _osfhnd array:
312
+ __acrt_lowio_set_os_handle(fh, osfhandle);
313
+
314
+ file_flags |= FOPEN;
315
+
316
+ _osfile(fh) = file_flags;
317
+ _textmode(fh) = __crt_lowio_text_mode::ansi;
318
+ _tm_unicode(fh) = false;
319
+
320
+ success = true;
321
+ }
322
+ __finally
323
+ {
324
+ if (!success)
325
+ {
326
+ _osfile(fh) &= ~FOPEN;
327
+ }
328
+
329
+ __acrt_lowio_unlock_fh(fh);
330
+ }
331
+ return fh;
332
+ }
333
+
334
+
335
+
336
+ // Acquires the lock associated with the given file handle.
337
+ extern "C" void __cdecl __acrt_lowio_lock_fh(int const fh)
338
+ {
339
+ EnterCriticalSection(&_pioinfo(fh)->lock);
340
+ }
341
+
342
+
343
+
344
+ // Releases the lock associated with the given file handle.
345
+ extern "C" void __cdecl __acrt_lowio_unlock_fh(int const fh)
346
+ {
347
+ LeaveCriticalSection(&_pioinfo(fh)->lock);
348
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/read.cpp ADDED
@@ -0,0 +1,635 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // read.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _read(), which reads bytes from a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+
10
+ // Lookup table for UTF-8 lead bytes
11
+ // Probably preferable to just ask if the bits are set than use an entire
12
+ // table, however the macros using this were #defined in the header so
13
+ // removing this extern table would break apps compiled to an earlier verison.
14
+ // 1 for pattern 110xxxxx - 1 trailbyte
15
+ // 2 for pattern 1110xxxx - 2 trailbytes
16
+ // 3 for pattern 11110xxx - 3 trailbytes
17
+ // 0 for everything else, including invalid patterns.
18
+ // We return 0 for invalid patterns because we rely on MultiByteToWideChar to
19
+ // do the validations.
20
+ extern "C" char _lookuptrailbytes[256] =
21
+ {
22
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
32
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
33
+ 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
34
+ 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0
35
+ };
36
+
37
+
38
+ static void store_lookahead(int const fh, char const c) throw()
39
+ {
40
+ _pipe_lookahead(fh)[0] = c;
41
+ }
42
+
43
+ static void store_lookahead(int const fh, wchar_t const c) throw()
44
+ {
45
+ char const* const byte_pointer = reinterpret_cast<char const*>(&c);
46
+ _pipe_lookahead(fh)[0] = byte_pointer[0];
47
+ _pipe_lookahead(fh)[1] = byte_pointer[1];
48
+ _pipe_lookahead(fh)[2] = LF; // Mark as empty
49
+ }
50
+
51
+
52
+
53
+ static int __cdecl translate_utf16_from_console_nolock(
54
+ int const fh,
55
+ _Inout_updates_(count) wchar_t* const buffer,
56
+ size_t const count
57
+ ) throw()
58
+ {
59
+ // The translation can be performend in-place, because we are converting
60
+ // CRLF sequences into LF, so the resulting text will never be longer than
61
+ // any corresponding source text.
62
+ wchar_t* const buffer_end = buffer + count;
63
+
64
+ wchar_t* source_it = buffer;
65
+ wchar_t* result_it = buffer;
66
+
67
+ while (source_it < buffer_end)
68
+ {
69
+ // If at any point during translation we encounter a Ctrl+Z, we stop
70
+ // translating immediately:
71
+ if (*source_it == CTRLZ)
72
+ {
73
+ _osfile(fh) |= FEOFLAG;
74
+ break;
75
+ }
76
+
77
+ // When a CR character is encountered, we must check to see if the next
78
+ // character is an LF. If it is, then we skip the CR and copy only the
79
+ // LF:
80
+ if (*source_it == CR && source_it + 1 < buffer_end && *(source_it + 1) == LF)
81
+ {
82
+ source_it += 2;
83
+ *result_it++ = LF;
84
+ continue;
85
+ }
86
+
87
+ // Otherwise, we just copy the character:
88
+ *result_it++ = *source_it++;
89
+ }
90
+
91
+ // Return the number of bytes that we translated:
92
+ return static_cast<int>((result_it - buffer) * sizeof(wchar_t));
93
+ }
94
+
95
+
96
+
97
+ template <typename Character>
98
+ static int __cdecl translate_text_mode_nolock(
99
+ _In_ int const fh,
100
+ _Pre_writable_size_(count) _Post_readable_byte_size_(return) Character* const buffer,
101
+ _In_ size_t const count
102
+ ) throw()
103
+ {
104
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
105
+
106
+ // If there is an LF at the beginning of the buffer, set the CRLF flag:
107
+ if (count != 0 && *buffer == LF)
108
+ {
109
+ _osfile(fh) |= FCRLF;
110
+ }
111
+ else
112
+ {
113
+ _osfile(fh) &= ~FCRLF;
114
+ }
115
+
116
+ // The translation can be performend in-place, because we are converting
117
+ // CRLF sequences into LF, so the resulting text will never be longer than
118
+ // any corresponding source text.
119
+ Character* const buffer_end = buffer + count;
120
+
121
+ Character* source_it = buffer;
122
+ Character* result_it = buffer;
123
+
124
+ while (source_it < buffer_end)
125
+ {
126
+ // If during translation we encounter a Ctrl+Z, we stop translating
127
+ // immeidately. For devices, we need to just set the Ctrl+Z flag;
128
+ // for other files, we just copy the Ctrl+Z as a normal character
129
+ // before returning:
130
+ if (*source_it == CTRLZ)
131
+ {
132
+ if ((_osfile(fh) & FDEV) == 0)
133
+ {
134
+ _osfile(fh) |= FEOFLAG;
135
+ }
136
+ else
137
+ {
138
+ *result_it++ = *source_it++;
139
+ }
140
+
141
+ break;
142
+ }
143
+
144
+ // If the character is not a CR, then we can simply copy it:
145
+ if (*source_it != CR)
146
+ {
147
+ *result_it++ = *source_it++;
148
+ continue;
149
+ }
150
+
151
+ // Otherwise, the character is a CR. We need to look-ahead to see if
152
+ // the next character is an LF, so that we can perform the CRLF => LF
153
+ // translation. First, handle the easy case where the CR does not
154
+ // appear at the end of the buffer:
155
+ if (source_it + 1 < buffer_end)
156
+ {
157
+ if (*(source_it + 1) == LF)
158
+ {
159
+ source_it += 2;
160
+ *result_it++ = LF; // Convert CRLF => LF
161
+ }
162
+ else
163
+ {
164
+ *result_it++ = *source_it++;
165
+ }
166
+
167
+ continue;
168
+ }
169
+
170
+ // This is the hard case: The CR is at the end of the buffer. We need
171
+ // to peek ahead to see if the next character is an LF:
172
+ ++source_it;
173
+
174
+ Character peek;
175
+ DWORD peek_size;
176
+ if (!ReadFile(os_handle, &peek, sizeof(peek), &peek_size, nullptr) || peek_size == 0)
177
+ {
178
+ // We couldn't peek ahead; just store the CR:
179
+ *result_it++ = CR;
180
+ continue;
181
+ }
182
+
183
+ // The peek succeeded. What we do next depends on whether the file is
184
+ // seekable or not. First we handle the case where the file does not
185
+ // allow seeking:
186
+ if (_osfile(fh) & (FDEV | FPIPE))
187
+ {
188
+ // If the peek character is an LF, then we just need to copy that
189
+ // character to the output buffer:
190
+ if (peek == LF)
191
+ {
192
+ *result_it++ = LF;
193
+ }
194
+ // Otherwise, it was some other character. We need to write the CR
195
+ // to the output buffer, then we need to store the peek character
196
+ // for later retrieval:
197
+ else
198
+ {
199
+ *result_it++ = CR;
200
+ store_lookahead(fh, peek);
201
+ }
202
+ }
203
+ // If the file does allow seeking, then we handle the peek differently.
204
+ // For seekable files, we translate the CRLF => LF by eliminating the
205
+ // CR. If the peek character is an LF, we simply do not write it to
206
+ // the output buffer; instead, we will seek backwards to unpeek the
207
+ // character, then let the LF get retrieved during the next call to
208
+ // read().
209
+ else
210
+ {
211
+ // However, if the buffer is currenty empty, then this is a one-
212
+ // character read, so we store the LF in order that we make progress
213
+ if (peek == LF && result_it == buffer)
214
+ {
215
+ *result_it++ = LF;
216
+ }
217
+ // Otherwise, we do what is described above: we seek backwards and
218
+ // write the CR if and only if the peek character was not an LF:
219
+ else
220
+ {
221
+ _lseeki64_nolock(fh, -1 * static_cast<int>(sizeof(Character)), FILE_CURRENT);
222
+ if (peek != LF)
223
+ {
224
+ *result_it++ = CR;
225
+ }
226
+ }
227
+ }
228
+ }
229
+
230
+ // Return the number of bytes that we translated:
231
+ return static_cast<int>((result_it - buffer) * sizeof(Character));
232
+ }
233
+
234
+
235
+
236
+ _Success_(return != -1)
237
+ static int __cdecl translate_ansi_or_utf8_nolock(
238
+ int const fh,
239
+ _In_reads_(source_count) char* const source_buffer,
240
+ size_t const source_count,
241
+ _Pre_writable_size_(result_count) _Post_readable_byte_size_(return) wchar_t* const result_buffer,
242
+ size_t const result_count
243
+ ) throw()
244
+ {
245
+ int const text_mode_translation_result_size = translate_text_mode_nolock(fh, source_buffer, source_count);
246
+
247
+ // If we read no characters, then we are done:
248
+ if (text_mode_translation_result_size == 0)
249
+ {
250
+ return 0;
251
+ }
252
+
253
+ // If the file is open in ANSI mode, then no further translation is
254
+ // required; we can simply return the number of bytes that we read.
255
+ // Even though there is no translation, there may still be
256
+ // characters in the buffer due to CRLF translation (a CR without
257
+ // a LF would 'unget' the would-be LF).
258
+ // text_mode_translation_result_size has already been adjusted for
259
+ // CRLF translation by translate_text_mode_nolock().
260
+ if (_textmode(fh) == __crt_lowio_text_mode::ansi)
261
+ {
262
+ return text_mode_translation_result_size;
263
+ }
264
+
265
+ // Otherwise, the file is open in UTF-8 mode and we read a nonzero number
266
+ // of characters. We need to translate from UTF-8 to UTF-16. To do this,
267
+ // we first need to hunt for the end of the translatable buffer. This may
268
+ // not be result_it, because we may have read a partial multibyte UTF-8
269
+ // character.
270
+ char* result_it = source_buffer + text_mode_translation_result_size - 1;
271
+
272
+ // If the last character is an independent character, then we can
273
+ // translate the entire buffer:
274
+ if (_utf8_is_independent(*result_it))
275
+ {
276
+ ++result_it; // Reset the result_it
277
+ }
278
+ // Otherwise, we have to find the end of the last full UTF-8 character
279
+ // that was read:
280
+ else
281
+ {
282
+ // Walk backwards from the end of the buffer until we find a lead byte:
283
+ unsigned counter = 1;
284
+ while (!_utf8_is_leadbyte(*result_it) && counter <= 4 && result_it >= source_buffer)
285
+ {
286
+ --result_it;
287
+ ++counter;
288
+ }
289
+
290
+ // Now that we've found the last lead byte, determine whether the
291
+ // character is complete or incomplete. We compute the number of
292
+ // trailbytes...
293
+ unsigned const trailbyte_count = _utf8_no_of_trailbytes(static_cast<const unsigned char>(*result_it));
294
+ if (trailbyte_count == 0)
295
+ {
296
+ // Oh, apparently that wasn't a lead byte; the file contains invalid
297
+ // UTF-8 character sequences:
298
+ errno = EILSEQ;
299
+ return -1;
300
+ }
301
+
302
+ // If the lead byte plus the remaining bytes form a full set, then we
303
+ // can translate the entire buffer:
304
+ if (trailbyte_count + 1 == counter)
305
+ {
306
+ result_it += counter;
307
+ }
308
+ // Otherwise, the last character is incomplete, so we will not include
309
+ // this character in the result. We unget the last characters, either
310
+ // by seeking backwards if the file is seekable, or by buffering the
311
+ // characters. Note that result_it currently points one-past-the-end
312
+ // of the translatable buffer, because it points to the lead byte of
313
+ // the partially read character.
314
+ else
315
+ {
316
+ // If the file does not support seeking, buffer the characters:
317
+ if (_osfile(fh) & (FDEV | FPIPE))
318
+ {
319
+ _pipe_lookahead(fh)[0] = *result_it++;
320
+
321
+ if (counter >= 2)
322
+ {
323
+ _pipe_lookahead(fh)[1] = *result_it++;
324
+ }
325
+
326
+ if (counter == 3)
327
+ {
328
+ _pipe_lookahead(fh)[2] = *result_it++;
329
+ }
330
+
331
+ // Now that we've buffered the characters, seek the end iterator
332
+ // back to the actual end of the translatable sequence:
333
+ result_it -= counter;
334
+
335
+ }
336
+ // If the file does support seeking, we can just seek backwards so
337
+ // that the next read will get the characters directly:
338
+ else
339
+ {
340
+ _lseeki64_nolock(fh, -static_cast<int>(counter), FILE_CURRENT);
341
+ }
342
+ }
343
+ }
344
+
345
+ // Finally, we can translate the characters into the result buffer:
346
+ int const characters_translated = static_cast<int>(__acrt_MultiByteToWideChar(
347
+ CP_UTF8,
348
+ 0,
349
+ source_buffer,
350
+ static_cast<DWORD>(result_it - source_buffer),
351
+ result_buffer,
352
+ static_cast<DWORD>(result_count)));
353
+
354
+ if (characters_translated == 0)
355
+ {
356
+ __acrt_errno_map_os_error(GetLastError());
357
+ return -1;
358
+ }
359
+
360
+ _utf8translations(fh) = (characters_translated != static_cast<int>(result_it - source_buffer));
361
+
362
+ // MultiByteToWideChar returns the number of wide characters that
363
+ // it produced; we need to return the number of bytes:
364
+ return characters_translated * sizeof(wchar_t);
365
+ }
366
+
367
+
368
+
369
+ // Reads bytes from a file. This function attempts to read enough bytes to fill
370
+ // the provided buffer. If the file is in text mode, CRLF sequences are mapped
371
+ // to LF, thus affecting the number of characters read. This mapping does not
372
+ // affect the file pointer.
373
+ //
374
+ // Returns the number of bytes read, which may be less than the number of bytes
375
+ // requested if EOF was reached or if the file is in text mode. Returns -1 and
376
+ // sets errno on failure.
377
+ extern "C" int __cdecl _read(int const fh, void* const buffer, unsigned const buffer_size)
378
+ {
379
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
380
+ _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
381
+ _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
382
+ _VALIDATE_CLEAR_OSSERR_RETURN(buffer_size <= INT_MAX, EINVAL, -1);
383
+
384
+ __acrt_lowio_lock_fh(fh);
385
+ int result = -1;
386
+ __try
387
+ {
388
+ if ((_osfile(fh) & FOPEN) == 0)
389
+ {
390
+ errno = EBADF;
391
+ _doserrno = 0;
392
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
393
+ __leave;
394
+ }
395
+
396
+ result = _read_nolock(fh, buffer, buffer_size);
397
+ }
398
+ __finally
399
+ {
400
+ __acrt_lowio_unlock_fh(fh);
401
+ }
402
+ return result;
403
+ }
404
+
405
+
406
+
407
+ extern "C" int __cdecl _read_nolock(
408
+ int const fh,
409
+ void* const result_buffer,
410
+ unsigned const result_buffer_size
411
+ )
412
+ {
413
+ _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1 );
414
+ _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
415
+ _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
416
+ _VALIDATE_CLEAR_OSSERR_RETURN(result_buffer_size <= INT_MAX, EINVAL, -1);
417
+
418
+ // If there is no data to be written or if the file is at EOF, no work to do:
419
+ if (result_buffer_size == 0 || (_osfile(fh) & FEOFLAG))
420
+ return 0;
421
+
422
+ _VALIDATE_CLEAR_OSSERR_RETURN(result_buffer != nullptr, EINVAL, -1);
423
+
424
+
425
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
426
+ __crt_lowio_text_mode const text_mode = _textmode(fh);
427
+
428
+
429
+ __crt_unique_heap_ptr<char> owned_internal_buffer;
430
+
431
+ char* internal_buffer;
432
+ unsigned internal_buffer_remaining;
433
+ switch (text_mode)
434
+ {
435
+ case __crt_lowio_text_mode::utf8:
436
+ // For UTF-8 files, we need two buffers, because after reading we need
437
+ // to convert the text into Unicode. MultiByteToWideChar doesn't do
438
+ // in-place conversions.
439
+ //
440
+ // The multibyte to wide character conversion may double the size of the
441
+ // text, hence we halve the size here.
442
+ //
443
+ // Since we are reading a UTF-8 stream, the number of bytes read may
444
+ // vary from 'size' characters to 'size/4' characters. For this reason,
445
+ // if we need to read 'size' characters, we will allocate an MBCS buffer
446
+ // of size 'size'. In case the size is zero, we will use four as a
447
+ // minimum value. This will make sure we don't overflow when we read
448
+ // from a pipe.
449
+ //
450
+ // In this case, the number of wide characters that we can read is
451
+ // size / 2. This means that we require a buffer of size size / 2.
452
+
453
+ // For UTF-8 the count always needs to be an even number:
454
+ _VALIDATE_CLEAR_OSSERR_RETURN(result_buffer_size % 2 == 0, EINVAL, -1);
455
+
456
+ internal_buffer_remaining = (result_buffer_size / 2) < 4
457
+ ? 4
458
+ : (result_buffer_size/2);
459
+
460
+ owned_internal_buffer = _malloc_crt_t(char, internal_buffer_remaining);
461
+ internal_buffer = owned_internal_buffer.get();
462
+ if (!internal_buffer)
463
+ {
464
+ errno = ENOMEM;
465
+ _doserrno = ERROR_NOT_ENOUGH_MEMORY;
466
+ return -1;
467
+ }
468
+
469
+ _startpos(fh) = _lseeki64_nolock(fh, 0, FILE_CURRENT);
470
+ break;
471
+
472
+ case __crt_lowio_text_mode::utf16le:
473
+ // For UTF-16 the count always needs to be an even number:
474
+ _VALIDATE_CLEAR_OSSERR_RETURN((result_buffer_size % 2) == 0, EINVAL, -1);
475
+
476
+ // For UTF-16 files, we can directly use the input buffer:
477
+ internal_buffer_remaining = result_buffer_size;
478
+ internal_buffer = static_cast<char*>(result_buffer);
479
+ break;
480
+
481
+ default:
482
+ // For ANSI files, we can directly use the input buffer:
483
+ internal_buffer_remaining = result_buffer_size;
484
+ internal_buffer = static_cast<char*>(result_buffer);
485
+ break;
486
+ }
487
+
488
+ wchar_t* wide_internal_buffer = reinterpret_cast<wchar_t*>(internal_buffer);
489
+
490
+ int bytes_read = 0;
491
+
492
+ // We may have buffered look-ahead characters during the last read. If
493
+ // so, read them into the buffer and set the look-ahead buffers back to
494
+ // empty state (with the value of LF):
495
+ //
496
+ // CRT_REFACTOR This look-ahead buffering could use additional work, but
497
+ // will require nonlocal changes, so that work is not included in this
498
+ // changeset.
499
+ if ((_osfile(fh) & (FPIPE | FDEV)) &&
500
+ _pipe_lookahead(fh)[0] != LF &&
501
+ internal_buffer_remaining != 0)
502
+ {
503
+ *internal_buffer++ = _pipe_lookahead(fh)[0];
504
+ ++bytes_read;
505
+ --internal_buffer_remaining;
506
+ _pipe_lookahead(fh)[0] = LF;
507
+
508
+ // For UTF-16, there may be an additional look-ahead character
509
+ // bufferred. For UTF-8, there may be two more:
510
+ if (text_mode != __crt_lowio_text_mode::ansi &&
511
+ _pipe_lookahead(fh)[1] != LF &&
512
+ internal_buffer_remaining != 0)
513
+ {
514
+ *internal_buffer++ = _pipe_lookahead(fh)[1];
515
+ ++bytes_read;
516
+ --internal_buffer_remaining;
517
+ _pipe_lookahead(fh)[1] = LF;
518
+
519
+ if (text_mode == __crt_lowio_text_mode::utf8 &&
520
+ _pipe_lookahead(fh)[2] != LF &&
521
+ internal_buffer_remaining != 0)
522
+ {
523
+ *internal_buffer++ = _pipe_lookahead(fh)[2];
524
+ ++bytes_read;
525
+ --internal_buffer_remaining;
526
+ _pipe_lookahead(fh)[2] = LF;
527
+ }
528
+ }
529
+ }
530
+
531
+ DWORD console_mode;
532
+ bool const from_console =
533
+ _isatty(fh) &&
534
+ (_osfile(fh) & FTEXT) &&
535
+ GetConsoleMode(os_handle, &console_mode);
536
+
537
+ // Read the data directly from the console:
538
+ if (from_console && text_mode == __crt_lowio_text_mode::utf16le)
539
+ {
540
+ DWORD console_characters_read;
541
+ if (!ReadConsoleW(
542
+ os_handle,
543
+ internal_buffer,
544
+ internal_buffer_remaining / sizeof(wchar_t),
545
+ &console_characters_read,
546
+ nullptr))
547
+ {
548
+ __acrt_errno_map_os_error(GetLastError());
549
+ return -1;
550
+ }
551
+
552
+ // In UTF-16 mode, the return value is the actual number of wide
553
+ // characters read; we need the number of bytes:
554
+ bytes_read += console_characters_read * sizeof(wchar_t);
555
+ }
556
+ // Otherwise, read the data from the file normally:
557
+ else
558
+ {
559
+ DWORD bytes_read_from_file;
560
+ if (!ReadFile(
561
+ os_handle,
562
+ internal_buffer,
563
+ internal_buffer_remaining,
564
+ &bytes_read_from_file,
565
+ nullptr
566
+ ) || bytes_read_from_file > result_buffer_size)
567
+ {
568
+ DWORD const last_error = GetLastError();
569
+ if (last_error == ERROR_ACCESS_DENIED)
570
+ {
571
+ // ERROR_ACCESS_DENIED occurs if the file is open with the wrong
572
+ // read/write mode. For this error, we should return EBADF, not
573
+ // the EACCES that will be set by __acrt_errno_map_os_error:
574
+ errno = EBADF;
575
+ _doserrno = last_error;
576
+ return -1;
577
+
578
+ }
579
+ else if (last_error == ERROR_BROKEN_PIPE)
580
+ {
581
+ // Return 0 if ERROR_BROKEN_PIPE occurs. It means the handle is
582
+ // a read handle on a pipe for which all write handles have been
583
+ // closed and all data has been read:
584
+ return 0;
585
+ }
586
+ else
587
+ {
588
+ // Otherwise, map the error normally and return:
589
+ __acrt_errno_map_os_error(last_error);
590
+ return -1;
591
+ }
592
+ }
593
+
594
+ bytes_read += bytes_read_from_file;
595
+ }
596
+
597
+
598
+ // If the file is open in binary mode, no translation is required, so we
599
+ // can skip all of the rest of this function:
600
+ if ((_osfile(fh) & FTEXT) == 0)
601
+ return bytes_read;
602
+
603
+
604
+ // Perform the CRLF => LF translation and convert to the required
605
+ // encoding (UTF-8 must be converted to UTF-16). This first case
606
+ // handles UTF-8 and ANSI:
607
+ if (text_mode != __crt_lowio_text_mode::utf16le)
608
+ {
609
+ return translate_ansi_or_utf8_nolock(
610
+ fh,
611
+ internal_buffer,
612
+ bytes_read,
613
+ static_cast<wchar_t*>(result_buffer),
614
+ result_buffer_size / sizeof(wchar_t));
615
+ }
616
+
617
+ // The text mode is __crt_lowio_text_mode::utf16le and we are reading from the
618
+ // console:
619
+ else if (from_console)
620
+ {
621
+ return translate_utf16_from_console_nolock(
622
+ fh,
623
+ wide_internal_buffer,
624
+ bytes_read / sizeof(wchar_t));
625
+ }
626
+ // Otherwise, the text mode is __crt_lowio_text_mode::utf16le and we are NOT
627
+ // reading from the console:
628
+ else
629
+ {
630
+ return translate_text_mode_nolock(
631
+ fh,
632
+ wide_internal_buffer,
633
+ bytes_read / sizeof(wchar_t));
634
+ }
635
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/setmode.cpp ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // setmode.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _setmode(), which sets the translation mode for a file, and
7
+ // _set_fmode() and _get_fmode(), which control the global default translation
8
+ // mode.
9
+ //
10
+ #include <corecrt_internal_lowio.h>
11
+ #include <stdlib.h>
12
+
13
+
14
+
15
+ // Sets the file translation mode. This changes the file mode to text or binary,
16
+ // depending on the mode argument. This affects whether reads and writes on the
17
+ // file translate between CRLF and LF. Returns the old file translation mode on
18
+ // success, or -1 on failure.
19
+ extern "C" int __cdecl _setmode(int const fh, int const mode)
20
+ {
21
+ _VALIDATE_RETURN(mode == _O_TEXT ||
22
+ mode == _O_BINARY ||
23
+ mode == _O_WTEXT ||
24
+ mode == _O_U8TEXT ||
25
+ mode == _O_U16TEXT,
26
+ EINVAL, -1);
27
+
28
+ _CHECK_FH_RETURN(fh, EBADF, -1);
29
+ _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
30
+ _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
31
+
32
+ __acrt_lowio_lock_fh(fh);
33
+ int result = -1;
34
+ __try
35
+ {
36
+ if ((_osfile(fh) & FOPEN) == 0)
37
+ {
38
+ errno = EBADF;
39
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
40
+ __leave;
41
+ }
42
+
43
+ result = _setmode_nolock(fh, mode);
44
+ }
45
+ __finally
46
+ {
47
+ __acrt_lowio_unlock_fh(fh);
48
+ }
49
+ return result;
50
+ }
51
+
52
+
53
+
54
+ extern "C" int __cdecl _setmode_nolock(int const fh, int const mode)
55
+ {
56
+ int const old_mode = _osfile(fh) & FTEXT;
57
+ __crt_lowio_text_mode const old_textmode = _textmode(fh);
58
+
59
+ switch (mode)
60
+ {
61
+ case _O_BINARY:
62
+ _osfile(fh) &= ~FTEXT;
63
+ break;
64
+
65
+ case _O_TEXT:
66
+ _osfile(fh) |= FTEXT;
67
+ _textmode(fh) = __crt_lowio_text_mode::ansi;
68
+ break;
69
+
70
+ case _O_U8TEXT:
71
+ _osfile(fh) |= FTEXT;
72
+ _textmode(fh) = __crt_lowio_text_mode::utf8;
73
+ break;
74
+
75
+ case _O_U16TEXT:
76
+ case _O_WTEXT:
77
+ _osfile(fh) |= FTEXT;
78
+ _textmode(fh) = __crt_lowio_text_mode::utf16le;
79
+ break;
80
+ }
81
+
82
+ if (old_mode == 0)
83
+ return _O_BINARY;
84
+
85
+ if (old_textmode == __crt_lowio_text_mode::ansi)
86
+ {
87
+ return _O_TEXT;
88
+ }
89
+ else if (old_textmode == __crt_lowio_text_mode::utf8)
90
+ {
91
+ return _O_U8TEXT;
92
+ }
93
+
94
+ return _O_WTEXT;
95
+ }
96
+
97
+
98
+
99
+ extern "C" errno_t __cdecl _set_fmode(int const mode)
100
+ {
101
+ _VALIDATE_RETURN_ERRCODE(mode == _O_TEXT || mode == _O_BINARY || mode == _O_WTEXT, EINVAL);
102
+
103
+ _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
104
+ _InterlockedExchange(reinterpret_cast<long*>(&_fmode.value()), mode);
105
+ _END_SECURE_CRT_DEPRECATION_DISABLE
106
+
107
+ return 0;
108
+ }
109
+
110
+
111
+
112
+ extern "C" errno_t __cdecl _get_fmode(int* const pMode)
113
+ {
114
+ _VALIDATE_RETURN_ERRCODE(pMode != nullptr, EINVAL);
115
+
116
+ _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
117
+ *pMode = __crt_interlocked_read(&_fmode.value());
118
+ _END_SECURE_CRT_DEPRECATION_DISABLE
119
+
120
+ return 0;
121
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/tell.cpp ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // tell.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _tell(), which gets the current position of the file pointer.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <stdio.h>
10
+
11
+
12
+
13
+ // Gets the current position of the file pointer, without adjustment for
14
+ // buffering. Returns -1 on error.
15
+ extern "C" long __cdecl _tell(int const fh)
16
+ {
17
+ return _lseek(fh, 0, SEEK_CUR);
18
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/telli64.cpp ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // telli64.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _telli64(), which gets the current position of the file pointer.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <stdio.h>
10
+
11
+
12
+
13
+ // Gets the current position of the file pointer, without adjustment for
14
+ // buffering. Returns -1 on error.
15
+ extern "C" __int64 __cdecl _telli64(int const fh)
16
+ {
17
+ return _lseeki64(fh, 0, SEEK_CUR);
18
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/txtmode.cpp ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // txtmode.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines the global _fmode variable and sets the global file mode to text.
7
+ // This is the default behavior.
8
+ //
9
+ #include <corecrt_internal.h>
10
+ #include <fcntl.h>
11
+ #include <stdlib.h>
12
+
13
+ // Clients of the static CRT can choose to access _fmode directly as a global variable; they do so as if it was declared as an int.
14
+ // Because state separation is disabled in the static CRT, the dual_state_global<int> has the same representation as an int, so this is okay, if a bit messy.
15
+ __crt_state_management::dual_state_global<int> _fmode; // This is automatically initialized to zero by the compiler
16
+
17
+ extern "C" int* __cdecl __p__fmode()
18
+ {
19
+ _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
20
+ return &_fmode.value();
21
+ _END_SECURE_CRT_DEPRECATION_DISABLE
22
+ }
23
+
24
+ // Initializer for fmode global correction.
25
+ _CRT_LINKER_FORCE_INCLUDE(__acrt_fmode_initializer);
26
+
27
+ extern "C" int __cdecl __acrt_initialize_fmode()
28
+ {
29
+ // The initial mode for _fmode is initialized during pre_c_initializer() in vcstartup, so that it can be overridden with a linkopt.
30
+ // The default value is provided by _get_startup_file_mode(), but this value is not used to initialize both _fmode states.
31
+ // (ex: if exe is Prog-Mode, OS-Mode _fmode is uninitialized, if exe is OS-Mode, Prog-Mode _fmode is uninitialized)
32
+
33
+ // To fix this, we adjust zero-value _fmode to _O_TEXT, since that is the default.
34
+ // Only the exe can use the linkopt, so the state that is not being initialized could not have requested _O_BINARY.
35
+
36
+ int * const fmode_states = _fmode.dangerous_get_state_array();
37
+ for (unsigned int i = 0; i != __crt_state_management::state_index_count; ++i)
38
+ {
39
+ int& fmode_state = fmode_states[i];
40
+ if (fmode_state == 0)
41
+ {
42
+ fmode_state = _O_TEXT;
43
+ }
44
+ }
45
+
46
+ return 0;
47
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/umask.cpp ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /***
2
+ *umask.c - set file permission mask
3
+ *
4
+ * Copyright (c) Microsoft Corporation. All rights reserved.
5
+ *
6
+ *Purpose:
7
+ * defines _umask() - sets file permission mask of current process*
8
+ * affecting files created by creat, open, or sopen.
9
+ *
10
+ *******************************************************************************/
11
+
12
+ #include <corecrt_internal_lowio.h>
13
+ #include <sys\stat.h>
14
+
15
+ extern "C" int _umaskval = 0;
16
+
17
+ /***
18
+ *errno_t _umask(mode, poldmode) - set the file mode mask
19
+ *
20
+ *Purpose :
21
+ * Works similiar to umask except it validates input params.
22
+ *
23
+ *
24
+ *******************************************************************************/
25
+
26
+ extern "C" errno_t __cdecl _umask_s(
27
+ int const mode,
28
+ int* const old_mode
29
+ )
30
+ {
31
+ _VALIDATE_RETURN_ERRCODE(old_mode != nullptr, EINVAL);
32
+ *old_mode = _umaskval;
33
+ _VALIDATE_RETURN_ERRCODE((mode & ~(_S_IREAD | _S_IWRITE)) == 0, EINVAL);
34
+
35
+ _umaskval = mode;
36
+ return 0;
37
+ }
38
+
39
+ /***
40
+ *int _umask(mode) - set the file mode mask
41
+ *
42
+ *Purpose:
43
+ * Sets the file-permission mask of the current process* which
44
+ * modifies the permission setting of new files created by creat,
45
+ * open, or sopen.
46
+ *
47
+ *Entry:
48
+ * int mode - new file permission mask
49
+ * may contain _S_IWRITE, _S_IREAD, _S_IWRITE | _S_IREAD.
50
+ * The S_IREAD bit has no effect under Win32
51
+ *
52
+ *Exit:
53
+ * returns the previous setting of the file permission mask.
54
+ *
55
+ *Exceptions:
56
+ *
57
+ *******************************************************************************/
58
+ extern "C" int __cdecl _umask(int const mode)
59
+ {
60
+ // Silently ignore non-Windows modes:
61
+ int const masked_mode = mode & (_S_IREAD | _S_IWRITE);
62
+
63
+ int old_mode = 0;
64
+ _umask_s(masked_mode, &old_mode);
65
+ return old_mode;
66
+ }
miniMSVC/Windows Kits/10/Source/10.0.26100.0/ucrt/lowio/write.cpp ADDED
@@ -0,0 +1,754 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //
2
+ // write.cpp
3
+ //
4
+ // Copyright (c) Microsoft Corporation. All rights reserved.
5
+ //
6
+ // Defines _write(), which writes a buffer to a file.
7
+ //
8
+ #include <corecrt_internal_lowio.h>
9
+ #include <corecrt_internal_mbstring.h>
10
+ #include <corecrt_internal_ptd_propagation.h>
11
+ #include <ctype.h>
12
+ #include <locale.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+ #include <wchar.h>
16
+
17
+
18
+
19
+ namespace
20
+ {
21
+ struct write_result
22
+ {
23
+ DWORD error_code;
24
+ DWORD char_count;
25
+ DWORD lf_count;
26
+ };
27
+ }
28
+
29
+
30
+
31
+ // This is the normal size of the LF => CRLF translation buffer. The default
32
+ // buffer is 4K, plus extra room for LF characters. Not all buffers are exactly
33
+ // this size, but this is used as the base size.
34
+ static size_t const BUF_SIZE = 5 * 1024;
35
+
36
+
37
+
38
+ // Writes a buffer to a file. The way in which the buffer is written depends on
39
+ // the mode in which the file was opened (e.g., if the file is a text mode file,
40
+ // linefeed translation will take place).
41
+ //
42
+ // On success, this function returns the number of bytes actually written (note
43
+ // that "bytes" here is "bytes from the original buffer;" more or fewer bytes
44
+ // may have actually been written, due to linefeed translation, codepage
45
+ // translation, and other transformations). On failure, this function returns 0
46
+ // and sets errno.
47
+ extern "C" int __cdecl _write_internal(int const fh, void const* const buffer, unsigned const size, __crt_cached_ptd_host& ptd)
48
+ {
49
+ _UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
50
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
51
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(fh) & FOPEN), EBADF, -1);
52
+
53
+ __acrt_lowio_lock_fh(fh);
54
+ int result = -1;
55
+ __try
56
+ {
57
+ if ((_osfile(fh) & FOPEN) == 0)
58
+ {
59
+ ptd.get_errno().set(EBADF);
60
+ ptd.get_doserrno().set(0);
61
+ _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
62
+ __leave;
63
+ }
64
+
65
+ result = _write_nolock(fh, buffer, size, ptd);
66
+ }
67
+ __finally
68
+ {
69
+ __acrt_lowio_unlock_fh(fh);
70
+ }
71
+ return result;
72
+ }
73
+
74
+ extern "C" int __cdecl _write(int const fh, void const* const buffer, unsigned const size)
75
+ {
76
+ __crt_cached_ptd_host ptd;
77
+ return _write_internal(fh, buffer, size, ptd);
78
+ }
79
+
80
+ static bool __cdecl write_requires_double_translation_nolock(int const fh, __crt_cached_ptd_host& ptd) throw()
81
+ {
82
+ // Double translation is required if both [a] the current locale is not the C
83
+ // locale or the file is open in a non-ANSI mode and [b] we are writing to the
84
+ // console.
85
+
86
+ // If this isn't a TTY or a text mode screen, then it isn't the console:
87
+ if (!_isatty(fh))
88
+ {
89
+ return false;
90
+ }
91
+
92
+ if ((_osfile(fh) & FTEXT) == 0) {
93
+ return false;
94
+ }
95
+
96
+ // Get the current locale. If we're in the C locale and the file is open
97
+ // in ANSI mode, we don't need double translation:
98
+ bool const is_c_locale = ptd.get_locale()->locinfo->locale_name[LC_CTYPE] == nullptr;
99
+ if (is_c_locale && _textmode(fh) == __crt_lowio_text_mode::ansi)
100
+ {
101
+ return false;
102
+ }
103
+
104
+ // If we can't get the console mode, it's not the console:
105
+ DWORD mode;
106
+ if (!GetConsoleMode(reinterpret_cast<HANDLE>(_osfhnd(fh)), &mode))
107
+ {
108
+ return false;
109
+ }
110
+
111
+ // Otherwise, double translation is required:
112
+ return true;
113
+ }
114
+
115
+
116
+
117
+ static write_result __cdecl write_double_translated_ansi_nolock(
118
+ int const fh,
119
+ _In_reads_(buffer_size) char const* const buffer,
120
+ unsigned const buffer_size,
121
+ __crt_cached_ptd_host& ptd
122
+ ) throw()
123
+ {
124
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
125
+ char const* const buffer_end = buffer + buffer_size;
126
+ UINT const console_cp = GetConsoleOutputCP();
127
+ _locale_t const locale = ptd.get_locale();
128
+ bool const is_utf8 = locale->locinfo->_public._locale_lc_codepage == CP_UTF8;
129
+
130
+ write_result result = { 0 };
131
+
132
+ for (char const* source_it = buffer; source_it < buffer_end; )
133
+ {
134
+ char const c = *source_it;
135
+
136
+ // We require double conversion, to convert from the source multibyte
137
+ // to Unicode, then from Unicode back to multibyte, but in the console
138
+ // codepage.
139
+ //
140
+ // Here, we have to take into account that _write() might be called
141
+ // byte-by-byte, so when we see a lead byte without a trail byte, we
142
+ // have to store it and return no error. When this function is called
143
+ // again, that byte will be combined with the next available character.
144
+ wchar_t wc[2] = { 0 };
145
+ int wc_used = 1;
146
+ if (is_utf8)
147
+ {
148
+ _ASSERTE(!_dbcsBufferUsed(fh));
149
+ const int mb_buf_size = sizeof(_mbBuffer(fh));
150
+ int mb_buf_used;
151
+ for (mb_buf_used = 0; mb_buf_used < mb_buf_size && _mbBuffer(fh)[mb_buf_used]; ++mb_buf_used)
152
+ {}
153
+
154
+ if (mb_buf_used > 0)
155
+ {
156
+ const int mb_len = _utf8_no_of_trailbytes(_mbBuffer(fh)[0]) + 1;
157
+ _ASSERTE(1 < mb_len && mb_buf_used < mb_len);
158
+ const int remaining_bytes = mb_len - mb_buf_used;
159
+ if (remaining_bytes <= (buffer_end - source_it))
160
+ {
161
+ // We now have enough bytes to complete the code point
162
+ char mb_buffer[MB_LEN_MAX];
163
+
164
+ for (int i = 0; i < mb_buf_used; ++i)
165
+ {
166
+ mb_buffer[i] = _mbBuffer(fh)[i];
167
+ }
168
+ for (int i = 0; i < remaining_bytes; ++i)
169
+ {
170
+ mb_buffer[i + mb_buf_used] = source_it[i];
171
+ }
172
+
173
+ // Clear out the temp buffer
174
+ for (int i = 0; i < mb_buf_used; ++i)
175
+ {
176
+ _mbBuffer(fh)[i] = 0;
177
+ }
178
+
179
+ mbstate_t state{};
180
+ const char* str = mb_buffer;
181
+ if (mb_len == 4)
182
+ {
183
+ wc_used = 2;
184
+ }
185
+ if (__crt_mbstring::__mbsrtowcs_utf8(wc, &str, wc_used, &state, ptd) == -1)
186
+ {
187
+ return result;
188
+ }
189
+ source_it += (remaining_bytes - 1);
190
+ }
191
+ else
192
+ {
193
+ // Need to add some more bytes to the buffer for later
194
+ const auto bytes_to_add = buffer_end - source_it;
195
+ _ASSERTE(mb_buf_used + bytes_to_add < mb_buf_size);
196
+ for (int i = 0; i < bytes_to_add; ++i)
197
+ {
198
+ _mbBuffer(fh)[i + mb_buf_used] = source_it[i];
199
+ }
200
+ // Pretend we wrote the bytes, because this isn't an error *yet*.
201
+ result.char_count += static_cast<DWORD>(bytes_to_add);
202
+ return result;
203
+ }
204
+ }
205
+ else
206
+ {
207
+ const int mb_len = _utf8_no_of_trailbytes(*source_it) + 1;
208
+ const auto available_bytes = buffer_end - source_it;
209
+ if (mb_len <= (available_bytes))
210
+ {
211
+ // We have enough bytes to write the entire code point
212
+ mbstate_t state{};
213
+ const char* str = source_it;
214
+ if (mb_len == 4)
215
+ {
216
+ wc_used = 2;
217
+ }
218
+ if (__crt_mbstring::__mbsrtowcs_utf8(wc, &str, wc_used, &state, ptd) == -1)
219
+ {
220
+ return result;
221
+ }
222
+ source_it += (mb_len - 1);
223
+ }
224
+ else
225
+ {
226
+ // Not enough bytes for this code point
227
+ _ASSERTE(available_bytes <= sizeof(_mbBuffer(fh)));
228
+ for (int i = 0; i < available_bytes; ++i)
229
+ {
230
+ _mbBuffer(fh)[i] = source_it[i];
231
+ }
232
+ // Pretend we wrote the bytes, because this isn't an error *yet*.
233
+ result.char_count += static_cast<DWORD>(available_bytes);
234
+ return result;
235
+ }
236
+ }
237
+ }
238
+ else if (_dbcsBufferUsed(fh))
239
+ {
240
+ // We already have a DBCS lead byte buffered. Take the current
241
+ // character, combine it with the lead byte, and convert:
242
+ _ASSERTE(_isleadbyte_fast_internal(_dbcsBuffer(fh), locale));
243
+
244
+ char mb_buffer[MB_LEN_MAX];
245
+ mb_buffer[0] = _dbcsBuffer(fh);
246
+ mb_buffer[1] = *source_it;
247
+
248
+ _dbcsBufferUsed(fh) = false;
249
+
250
+ if (_mbtowc_internal(wc, mb_buffer, 2, ptd) == -1)
251
+ {
252
+ return result;
253
+ }
254
+ }
255
+ else
256
+ {
257
+ if (_isleadbyte_fast_internal(*source_it, locale))
258
+ {
259
+ if ((source_it + 1) < buffer_end)
260
+ {
261
+ // And we have more bytes to read, just convert...
262
+ if (_mbtowc_internal(wc, source_it, 2, ptd) == -1)
263
+ {
264
+ return result;
265
+ }
266
+
267
+ // Increment the source_it to accomodate the DBCS character:
268
+ ++source_it;
269
+ }
270
+ else
271
+ {
272
+ // And we ran out of bytes to read, so buffer the lead byte:
273
+ _dbcsBuffer(fh) = *source_it;
274
+ _dbcsBufferUsed(fh) = true;
275
+
276
+ // We lie here that we actually wrote the last character, to
277
+ // ensure we don't consider this an error:
278
+ ++result.char_count;
279
+ return result;
280
+ }
281
+ }
282
+ else
283
+ {
284
+ // single character conversion:
285
+ if (_mbtowc_internal(wc, source_it, 1, ptd) == -1)
286
+ {
287
+ return result;
288
+ }
289
+ }
290
+ }
291
+
292
+ ++source_it;
293
+
294
+ // Translate the Unicode character into Multibyte in the console codepage
295
+ // and write the character to the file:
296
+ char mb_buffer[MB_LEN_MAX];
297
+ DWORD const size = static_cast<DWORD>(__acrt_WideCharToMultiByte(
298
+ console_cp, 0, wc, wc_used, mb_buffer, sizeof(mb_buffer), nullptr, nullptr));
299
+
300
+ if(size == 0)
301
+ return result;
302
+
303
+ DWORD written;
304
+ if (!WriteFile(os_handle, mb_buffer, size, &written, nullptr))
305
+ {
306
+ result.error_code = GetLastError();
307
+ return result;
308
+ }
309
+
310
+ // When we are converting, some conversions may result in:
311
+ //
312
+ // 2 MBCS characters => 1 wide character => 1 MBCS character.
313
+ //
314
+ // For example, when printing Japanese characters in the English console
315
+ // codepage, each source character is transformed into a single question
316
+ // mark. Therefore, we want to track the number of bytes we converted,
317
+ // plus the linefeed count, instead of how many bytes we actually wrote.
318
+ result.char_count = result.lf_count + static_cast<DWORD>(source_it - buffer);
319
+
320
+ // If the write succeeded but didn't write all of the characters, return:
321
+ if (written < size)
322
+ {
323
+ return result;
324
+ }
325
+
326
+ // If the original character that we read was an LF, write a CR too:
327
+ // CRT_REFACTOR TODO Doesn't this write LFCR instead of CRLF?
328
+ if (c == LF)
329
+ {
330
+ wchar_t const cr = CR;
331
+ if (!WriteFile(os_handle, &cr, 1, &written, nullptr))
332
+ {
333
+ result.error_code = GetLastError();
334
+ return result;
335
+ }
336
+
337
+ if (written < 1)
338
+ {
339
+ return result;
340
+ }
341
+
342
+ ++result.lf_count;
343
+ ++result.char_count;
344
+ }
345
+ }
346
+
347
+ return result;
348
+ }
349
+
350
+
351
+
352
+ static write_result __cdecl write_double_translated_unicode_nolock(
353
+ _In_reads_(buffer_size) char const* const buffer,
354
+ _In_ _Pre_satisfies_((buffer_size % 2) == 0) unsigned const buffer_size
355
+ ) throw()
356
+ {
357
+ // When writing to a Unicode file (UTF-8 or UTF-16LE) that corresponds to
358
+ // the console, we don't actually need double translation. We just need to
359
+ // print each character to the console, one-by-one. (This function is
360
+ // named what it is because its use is guarded by the double translation
361
+ // check, and to match the name of the corresponding ANSI function.)
362
+
363
+ write_result result = { 0 };
364
+
365
+ // Needed for SAL to clarify that buffer_size is even.
366
+ _Analysis_assume_((buffer_size/2) != ((buffer_size-1)/2));
367
+ char const* const buffer_end = buffer + buffer_size;
368
+ for (char const* pch = buffer; pch < buffer_end; pch += 2)
369
+ {
370
+ wchar_t const c = *reinterpret_cast<wchar_t const*>(pch);
371
+
372
+ // _putwch_nolock does not depend on global state, no PTD needed to be propagated.
373
+ if (_putwch_nolock(c) == c)
374
+ {
375
+ result.char_count += 2;
376
+ }
377
+ else
378
+ {
379
+ result.error_code = GetLastError();
380
+ return result;
381
+ }
382
+
383
+ // If the character was a carriage return, also emit a line feed.
384
+ // CRT_REFACTOR TODO Doesn't this print LFCR instead of CRLF?
385
+ if (c == LF)
386
+ {
387
+ // _putwch_nolock does not depend on global state, no PTD needed to be propagated.
388
+ if (_putwch_nolock(CR) != CR)
389
+ {
390
+ result.error_code = GetLastError();
391
+ return result;
392
+ }
393
+
394
+ ++result.char_count;
395
+ ++result.lf_count;
396
+ }
397
+ }
398
+
399
+ return result;
400
+ }
401
+
402
+
403
+
404
+ static write_result __cdecl write_text_ansi_nolock(
405
+ int const fh,
406
+ _In_reads_(buffer_size) char const* const buffer,
407
+ unsigned const buffer_size
408
+ ) throw()
409
+ {
410
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
411
+ char const* const buffer_end = buffer + buffer_size;
412
+
413
+ write_result result = { 0 };
414
+
415
+ for (char const* source_it = buffer; source_it < buffer_end; )
416
+ {
417
+ char lfbuf[BUF_SIZE]; // The LF => CRLF translation buffer
418
+
419
+ // One-past-the-end of the translation buffer. Note that we subtract
420
+ // one to account for the case where we're pointing to the last element
421
+ // in the buffer and we need to write both a CR and an LF.
422
+ char* const lfbuf_end = lfbuf + sizeof(lfbuf) - 1;
423
+
424
+ // Translate the source buffer into the translation buffer. Note that
425
+ // both source_it and lfbuf_it are incremented in the loop.
426
+ char* lfbuf_it = lfbuf;
427
+ while (lfbuf_it < lfbuf_end && source_it < buffer_end)
428
+ {
429
+ char const c = *source_it++;
430
+
431
+ if (c == LF)
432
+ {
433
+ ++result.lf_count;
434
+ *lfbuf_it++ = CR;
435
+ }
436
+
437
+ *lfbuf_it++ = c;
438
+ }
439
+
440
+ DWORD const lfbuf_length = static_cast<DWORD>(lfbuf_it - lfbuf);
441
+
442
+ DWORD written;
443
+ if (!WriteFile(os_handle, lfbuf, lfbuf_length, &written, nullptr))
444
+ {
445
+ result.error_code = GetLastError();
446
+ return result;
447
+ }
448
+
449
+ result.char_count += written;
450
+ if (written < lfbuf_length)
451
+ {
452
+ return result; // The write succeeded but didn't write everything
453
+ }
454
+ }
455
+
456
+ return result;
457
+ }
458
+
459
+
460
+
461
+ static write_result __cdecl write_text_utf16le_nolock(
462
+ int const fh,
463
+ _In_reads_(buffer_size) char const* const buffer,
464
+ unsigned const buffer_size
465
+ ) throw()
466
+ {
467
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
468
+ wchar_t const* const buffer_end = reinterpret_cast<wchar_t const*>(buffer + buffer_size);
469
+
470
+ write_result result = { 0 };
471
+
472
+ wchar_t const* source_it = reinterpret_cast<wchar_t const*>(buffer);
473
+ while (source_it < buffer_end)
474
+ {
475
+ wchar_t lfbuf[BUF_SIZE / sizeof(wchar_t)]; // The translation buffer
476
+
477
+ // One-past-the-end of the translation buffer. Note that we subtract
478
+ // one to account for the case where we're pointing to the last element
479
+ // in the buffer and we need to write both a CR and an LF.
480
+ wchar_t const* lfbuf_end = lfbuf + BUF_SIZE / sizeof(wchar_t) - 1;
481
+
482
+ // Translate the source buffer into the translation buffer. Note that
483
+ // both source_it and lfbuf_it are incremented in the loop.
484
+ wchar_t* lfbuf_it = lfbuf;
485
+ while (lfbuf_it < lfbuf_end && source_it < buffer_end)
486
+ {
487
+ wchar_t const c = *source_it++;
488
+
489
+ if (c == LF)
490
+ {
491
+ result.lf_count += 2;
492
+ *lfbuf_it++ = CR;
493
+ }
494
+
495
+ *lfbuf_it++ = c;
496
+ }
497
+
498
+ // Note that this length is in bytes, not wchar_t elemnts, since we need
499
+ // to tell WriteFile how many bytes (not characters) to write:
500
+ DWORD const lfbuf_length = static_cast<DWORD>(lfbuf_it - lfbuf) * sizeof(wchar_t);
501
+
502
+
503
+ // Attempt the write and return immediately if it fails:
504
+ DWORD written;
505
+ if (!WriteFile(os_handle, lfbuf, lfbuf_length, &written, nullptr))
506
+ {
507
+ result.error_code = GetLastError();
508
+ return result;
509
+ }
510
+
511
+ result.char_count += written;
512
+ if (written < lfbuf_length)
513
+ {
514
+ return result; // The write succeeded, but didn't write everything
515
+ }
516
+ }
517
+
518
+ return result;
519
+ }
520
+
521
+
522
+
523
+ static write_result __cdecl write_text_utf8_nolock(
524
+ int const fh,
525
+ _In_reads_(buffer_size) char const* const buffer,
526
+ unsigned const buffer_size
527
+ ) throw()
528
+ {
529
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
530
+ wchar_t const* const buffer_end = reinterpret_cast<wchar_t const*>(buffer + buffer_size);
531
+
532
+ write_result result = { 0 };
533
+
534
+ wchar_t const* source_it = reinterpret_cast<wchar_t const*>(buffer);
535
+ while (source_it < buffer_end)
536
+ {
537
+ // The translation buffer. We use two buffers: the first is used to
538
+ // store the UTF-16 LF => CRLF translation (this is that buffer here).
539
+ // The second is used for storing the conversion to UTF-8 (defined
540
+ // below). The sizes are selected to handle the worst-case scenario
541
+ // where each UTF-8 character is four bytes long.
542
+ wchar_t utf16_buf[BUF_SIZE / 6];
543
+
544
+ // One-past-the-end of the translation buffer. Note that we subtract
545
+ // one to account for the case where we're pointing to the last element
546
+ // in the buffer and we need to write both a CR and an LF.
547
+ wchar_t const* utf16_buf_end = utf16_buf + (BUF_SIZE / 6 - 1);
548
+
549
+ // Translate the source buffer into the translation buffer. Note that
550
+ // both source_it and lfbuf_it are incremented in the loop.
551
+ wchar_t* utf16_buf_it = utf16_buf;
552
+ while (utf16_buf_it < utf16_buf_end && source_it < buffer_end)
553
+ {
554
+ wchar_t const c = *source_it++;
555
+
556
+ if (c == LF)
557
+ {
558
+ // No need to count the number of line-feeds translated; we
559
+ // track the number of written characters by counting the total
560
+ // number of characters written from the UTF8 buffer (see below
561
+ // where we update the char_count).
562
+ *utf16_buf_it++ = CR;
563
+ }
564
+
565
+ *utf16_buf_it++ = c;
566
+ }
567
+
568
+ // Note that this length is in characters, not bytes.
569
+ DWORD const utf16_buf_length = static_cast<DWORD>(utf16_buf_it - utf16_buf);
570
+
571
+
572
+ // This is the second translation, where we translate the UTF-16 text to
573
+ // UTF-8, into the UTF-8 buffer:
574
+ char utf8_buf[(BUF_SIZE * 2) / 3];
575
+ DWORD const bytes_converted = static_cast<DWORD>(__acrt_WideCharToMultiByte(
576
+ CP_UTF8,
577
+ 0,
578
+ utf16_buf,
579
+ utf16_buf_length,
580
+ utf8_buf,
581
+ sizeof(utf8_buf),
582
+ nullptr,
583
+ nullptr));
584
+
585
+ if (bytes_converted == 0)
586
+ {
587
+ result.error_code = GetLastError();
588
+ return result;
589
+ }
590
+
591
+ // Here, we need to make every attempt to write all of the converted
592
+ // characters to avoid corrupting the stream. If, for example, we write
593
+ // only half of the bytes of a UTF-8 character, the stream may be
594
+ // corrupted.
595
+ //
596
+ // This loop will ensure that we exit only if either (a) all of the
597
+ // bytes are written, ensuring that no partial MBCSes are written, or
598
+ // (b) there is an error in the stream.
599
+ for (DWORD bytes_written = 0; bytes_written < bytes_converted; )
600
+ {
601
+ char const* const current = utf8_buf + bytes_written;
602
+ DWORD const current_size = bytes_converted - bytes_written;
603
+
604
+ DWORD written;
605
+ if (!WriteFile(os_handle, current, current_size, &written, nullptr))
606
+ {
607
+ result.error_code = GetLastError();
608
+ return result;
609
+ }
610
+
611
+ bytes_written += written;
612
+ }
613
+
614
+ // If this chunk was committed successfully, update the character count:
615
+ result.char_count = static_cast<DWORD>(reinterpret_cast<char const*>(source_it) - buffer);
616
+ }
617
+
618
+ return result;
619
+ }
620
+
621
+
622
+
623
+ static write_result __cdecl write_binary_nolock(
624
+ int const fh,
625
+ _In_reads_(buffer_size) char const* const buffer,
626
+ unsigned const buffer_size
627
+ ) throw()
628
+ {
629
+ HANDLE const os_handle = reinterpret_cast<HANDLE>(_osfhnd(fh));
630
+
631
+ // Compared to text files, binary files are easy...
632
+ write_result result = { 0 };
633
+ if (!WriteFile(os_handle, buffer, buffer_size, &result.char_count, nullptr))
634
+ {
635
+ result.error_code = GetLastError();
636
+ }
637
+
638
+ return result;
639
+ }
640
+
641
+
642
+
643
+ extern "C" int __cdecl _write_nolock(int const fh, void const* const buffer, unsigned const buffer_size, __crt_cached_ptd_host& ptd)
644
+ {
645
+ // If the buffer is empty, there is nothing to be written:
646
+ if (buffer_size == 0)
647
+ {
648
+ return 0;
649
+ }
650
+
651
+ // If the buffer is null, though... well, that is not allowed:
652
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, buffer != nullptr, EINVAL, -1);
653
+
654
+ __crt_lowio_text_mode const fh_textmode = _textmode(fh);
655
+
656
+ // If the file is open for Unicode, the buffer size must always be even:
657
+ if (fh_textmode == __crt_lowio_text_mode::utf16le || fh_textmode == __crt_lowio_text_mode::utf8)
658
+ {
659
+ _UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, buffer_size % 2 == 0, EINVAL, -1);
660
+ }
661
+
662
+ // If the file is opened for appending, seek to the end of the file. We
663
+ // ignore errors because the underlying file may not allow seeking.
664
+ if (_osfile(fh) & FAPPEND)
665
+ {
666
+ (void)_lseeki64_nolock_internal(fh, 0, FILE_END, ptd);
667
+ }
668
+
669
+ char const* const char_buffer = static_cast<char const*>(buffer);
670
+
671
+ // Dispatch the actual writing to one of the helper routines based on the
672
+ // text mode of the file and whether or not the file refers to the console.
673
+ //
674
+ // Note that in the event that the handle belongs to the console, WriteFile
675
+ // will generate garbage output. To print to the console correctly, we need
676
+ // to print ANSI. Also note that when printing to the console, we need to
677
+ // convert the characters to the console codepge.
678
+ write_result result = { 0 };
679
+ if (write_requires_double_translation_nolock(fh, ptd))
680
+ {
681
+ switch (fh_textmode)
682
+ {
683
+ case __crt_lowio_text_mode::ansi:
684
+ result = write_double_translated_ansi_nolock(fh, char_buffer, buffer_size, ptd);
685
+ break;
686
+
687
+ case __crt_lowio_text_mode::utf16le:
688
+ case __crt_lowio_text_mode::utf8:
689
+ _Analysis_assume_((buffer_size % 2) == 0);
690
+ result = write_double_translated_unicode_nolock(char_buffer, buffer_size);
691
+ break;
692
+ }
693
+ }
694
+ else if (_osfile(fh) & FTEXT)
695
+ {
696
+ switch (fh_textmode)
697
+ {
698
+ case __crt_lowio_text_mode::ansi:
699
+ result = write_text_ansi_nolock(fh, char_buffer, buffer_size);
700
+ break;
701
+
702
+ case __crt_lowio_text_mode::utf16le:
703
+ result = write_text_utf16le_nolock(fh, char_buffer, buffer_size);
704
+ break;
705
+
706
+ case __crt_lowio_text_mode::utf8:
707
+ result = write_text_utf8_nolock(fh, char_buffer, buffer_size);
708
+ break;
709
+ }
710
+ }
711
+ else
712
+ {
713
+ result = write_binary_nolock(fh, char_buffer, buffer_size);
714
+ }
715
+
716
+
717
+ // Why did we not write anything? Lettuce find out...
718
+ if (result.char_count == 0)
719
+ {
720
+ // If nothing was written, check to see if it was due to an OS error:
721
+ if (result.error_code != 0)
722
+ {
723
+ // An OS error occurred. ERROR_ACCESS_DENIED should be mapped in
724
+ // this case to EBADF, not EACCES. All other errors are mapped
725
+ // normally:
726
+ if (result.error_code == ERROR_ACCESS_DENIED)
727
+ {
728
+ ptd.get_errno().set(EBADF);
729
+ ptd.get_doserrno().set(result.error_code);
730
+ }
731
+ else
732
+ {
733
+ __acrt_errno_map_os_error_ptd(result.error_code, ptd);
734
+ }
735
+
736
+ return -1;
737
+ }
738
+
739
+ // If this file is a device and the first character was Ctrl+Z, then
740
+ // writing nothing is the expected behavior and is not an error:
741
+ if ((_osfile(fh) & FDEV) && *char_buffer == CTRLZ)
742
+ {
743
+ return 0;
744
+ }
745
+
746
+ // Otherwise, the error is reported as ENOSPC:
747
+ ptd.get_errno().set(ENOSPC);
748
+ ptd.get_doserrno().set(0);
749
+ return -1;
750
+ }
751
+
752
+ // The write succeeded. Return the adjusted number of bytes written:
753
+ return result.char_count - result.lf_count;
754
+ }