1 /* Proposed SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Feb 2018
4
5
6 Boost Software License - Version 1.0 - August 17th, 2003
7
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
14
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 */
30
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NT_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_NT_CODE_HPP
33
34 #if !defined(_WIN32) && !defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
35 #error This file should only be included on Windows
36 #endif
37
38 #include "win32_code.hpp"
39
40 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
41
42 //! \exclude
43 namespace win32
44 {
45 // A Win32 NTSTATUS
46 using NTSTATUS = long;
47 // A Win32 HMODULE
48 using HMODULE = void *;
49 // Used to retrieve where the NTDLL DLL is mapped into memory
50 extern HMODULE __stdcall GetModuleHandleW(const wchar_t *lpModuleName);
51 #pragma comment(lib, "kernel32.lib")
52 #if defined(_WIN64)
53 #pragma comment(linker, "/alternatename:?GetModuleHandleW@win32@system_error2@@YAPEAXPEB_W@Z=GetModuleHandleW")
54 #else
55 #pragma comment(linker, "/alternatename:?GetModuleHandleW@win32@system_error2@@YGPAXPB_W@Z=__imp__GetModuleHandleW@4")
56 #endif
57 } // namespace win32
58
59 class _nt_code_domain;
60 //! (Windows only) A NT error code, those returned by NT kernel functions.
61 using nt_code = status_code<_nt_code_domain>;
62 //! (Windows only) A specialisation of `status_error` for the NT error code domain.
63 using nt_error = status_error<_nt_code_domain>;
64
65 /*! (Windows only) The implementation of the domain for NT error codes, those returned by NT kernel functions.
66 */
67 class _nt_code_domain : public status_code_domain
68 {
69 template <class DomainType> friend class status_code;
70 template <class StatusCode> friend class detail::indirecting_domain;
71 friend class _com_code_domain;
72 using _base = status_code_domain;
_nt_code_to_errno(win32::NTSTATUS c)73 static int _nt_code_to_errno(win32::NTSTATUS c)
74 {
75 if(c >= 0)
76 {
77 return 0; // success
78 }
79 switch(static_cast<unsigned>(c))
80 {
81 #include "detail/nt_code_to_generic_code.ipp"
82 }
83 return -1;
84 }
_nt_code_to_win32_code(win32::NTSTATUS c)85 static win32::DWORD _nt_code_to_win32_code(win32::NTSTATUS c) // NOLINT
86 {
87 if(c >= 0)
88 {
89 return 0; // success
90 }
91 switch(static_cast<unsigned>(c))
92 {
93 #include "detail/nt_code_to_win32_code.ipp"
94 }
95 return static_cast<win32::DWORD>(-1);
96 }
97 //! Construct from a NT error code
_make_string_ref(win32::NTSTATUS c)98 static _base::string_ref _make_string_ref(win32::NTSTATUS c) noexcept
99 {
100 wchar_t buffer[32768];
101 static win32::HMODULE ntdll = win32::GetModuleHandleW(L"NTDLL.DLL");
102 win32::DWORD wlen = win32::FormatMessageW(0x00000800 /*FORMAT_MESSAGE_FROM_HMODULE*/ | 0x00001000 /*FORMAT_MESSAGE_FROM_SYSTEM*/ | 0x00000200 /*FORMAT_MESSAGE_IGNORE_INSERTS*/, ntdll, c, (1 << 10) /*MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)*/, buffer, 32768, nullptr);
103 size_t allocation = wlen + (wlen >> 1);
104 win32::DWORD bytes;
105 if(wlen == 0)
106 {
107 return _base::string_ref("failed to get message from system");
108 }
109 for(;;)
110 {
111 auto *p = static_cast<char *>(malloc(allocation)); // NOLINT
112 if(p == nullptr)
113 {
114 return _base::string_ref("failed to get message from system");
115 }
116 bytes = win32::WideCharToMultiByte(65001 /*CP_UTF8*/, 0, buffer, (int) (wlen + 1), p, (int) allocation, nullptr, nullptr);
117 if(bytes != 0)
118 {
119 char *end = strchr(p, 0);
120 while(end[-1] == 10 || end[-1] == 13)
121 {
122 --end;
123 }
124 *end = 0; // NOLINT
125 return _base::atomic_refcounted_string_ref(p, end - p);
126 }
127 free(p); // NOLINT
128 if(win32::GetLastError() == 0x7a /*ERROR_INSUFFICIENT_BUFFER*/)
129 {
130 allocation += allocation >> 2;
131 continue;
132 }
133 return _base::string_ref("failed to get message from system");
134 }
135 }
136
137 public:
138 //! The value type of the NT code, which is a `win32::NTSTATUS`
139 using value_type = win32::NTSTATUS;
140 using _base::string_ref;
141
142 public:
143 //! Default constructor
_nt_code_domain(typename _base::unique_id_type id=0x93f3b4487e4af25b)144 constexpr explicit _nt_code_domain(typename _base::unique_id_type id = 0x93f3b4487e4af25b) noexcept
145 : _base(id)
146 {
147 }
148 _nt_code_domain(const _nt_code_domain &) = default;
149 _nt_code_domain(_nt_code_domain &&) = default;
150 _nt_code_domain &operator=(const _nt_code_domain &) = default;
151 _nt_code_domain &operator=(_nt_code_domain &&) = default;
152 ~_nt_code_domain() = default;
153
154 //! Constexpr singleton getter. Returns the constexpr nt_code_domain variable.
155 static inline constexpr const _nt_code_domain &get();
156
name() const157 virtual string_ref name() const noexcept override { return string_ref("NT domain"); } // NOLINT
158 protected:
_do_failure(const status_code<void> & code) const159 virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
160 {
161 assert(code.domain() == *this);
162 return static_cast<const nt_code &>(code).value() < 0; // NOLINT
163 }
_do_equivalent(const status_code<void> & code1,const status_code<void> & code2) const164 virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
165 {
166 assert(code1.domain() == *this);
167 const auto &c1 = static_cast<const nt_code &>(code1); // NOLINT
168 if(code2.domain() == *this)
169 {
170 const auto &c2 = static_cast<const nt_code &>(code2); // NOLINT
171 return c1.value() == c2.value();
172 }
173 if(code2.domain() == generic_code_domain)
174 {
175 const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
176 if(static_cast<int>(c2.value()) == _nt_code_to_errno(c1.value()))
177 {
178 return true;
179 }
180 }
181 if(code2.domain() == win32_code_domain)
182 {
183 const auto &c2 = static_cast<const win32_code &>(code2); // NOLINT
184 if(c2.value() == _nt_code_to_win32_code(c1.value()))
185 {
186 return true;
187 }
188 }
189 return false;
190 }
_generic_code(const status_code<void> & code) const191 virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
192 {
193 assert(code.domain() == *this);
194 const auto &c = static_cast<const nt_code &>(code); // NOLINT
195 return generic_code(static_cast<errc>(_nt_code_to_errno(c.value())));
196 }
_do_message(const status_code<void> & code) const197 virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
198 {
199 assert(code.domain() == *this);
200 const auto &c = static_cast<const nt_code &>(code); // NOLINT
201 return _make_string_ref(c.value());
202 }
203 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
_do_throw_exception(const status_code<void> & code) const204 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
205 {
206 assert(code.domain() == *this);
207 const auto &c = static_cast<const nt_code &>(code); // NOLINT
208 throw status_error<_nt_code_domain>(c);
209 }
210 #endif
211 };
212 //! (Windows only) A constexpr source variable for the NT code domain, which is that of NT kernel functions. Returned by `_nt_code_domain::get()`.
213 constexpr _nt_code_domain nt_code_domain;
get()214 inline constexpr const _nt_code_domain &_nt_code_domain::get()
215 {
216 return nt_code_domain;
217 }
218
219 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
220
221 #endif
222