Skip to content

Commit

Permalink
[NT:MINDEF] Improve TYPE_OF and NTSTATUS related macros
Browse files Browse the repository at this point in the history
  • Loading branch information
RatinCN committed Nov 5, 2024
1 parent aeef28f commit 98b5ddf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Source/Include/KNSoft/NDK/NT/Extension/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#include "../Rtl/Process/Process.h"
#include "../Rtl/Process/EnvironmentBlock.h"

#if !defined(__cplusplus) && _MSC_FULL_VER >= 193933428
#define FIELD_TYPE(type, field) __typeof__(((type*)NULL)->field)
#endif

#pragma region TEB Fast Access

#if defined(_M_X64)
Expand Down Expand Up @@ -101,9 +97,15 @@ NtGetLastError(VOID)
Error = (ULONG)ReadTeb(LastErrorValue);
_Analysis_assume_(Error > 0);
return Error;
}
}

#define NtSetLastError(Error) WriteTeb(LastErrorValue, Error)
FORCEINLINE
VOID
NtSetLastError(
_In_ ULONG Error)
{
WriteTeb(LastErrorValue, Error);
}

/* Gets or sets the last status */

Expand All @@ -119,7 +121,13 @@ NtGetLastStatus(VOID)
return Status;
}

#define NtSetLastStatus(Status) WriteTeb(LastStatusValue, Status)
FORCEINLINE
VOID
NtSetLastStatus(
_In_ NTSTATUS Status)
{
WriteTeb(LastStatusValue, Status);
}

/*
* Error code conversion (NOT translation) Win32 Error/NTSTATUS/HRESULT
Expand Down
26 changes: 26 additions & 0 deletions Source/Include/KNSoft/NDK/NT/MinDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,29 @@ typedef SID* PSID;
#define NT_CUSTOMER_SHIFT 29
#define NT_CUSTOMER(Status) ((((ULONG)(Status)) >> NT_CUSTOMER_SHIFT) & 1)

#define NT_SEVERITY_MASK 3
#define NT_SEVERITY_SHIFT 30
#define NT_SEVERITY(Status) ((((ULONG)(Status)) >> NT_SEVERITY_SHIFT) & NT_SEVERITY_MASK)

#define NT_FACILITY_MASK 0xfff
#define NT_FACILITY_SHIFT 16
#define NT_FACILITY(Status) ((((ULONG)(Status)) >> NT_FACILITY_SHIFT) & NT_FACILITY_MASK)

#define NT_CODE_MASK 0xffff
#define NT_CODE(Status) (((ULONG)(Status)) & NT_CODE_MASK)

FORCEINLINE
NTSTATUS
MAKE_NTSTATUS(
_In_ ULONG Severity,
_In_ ULONG Facility,
_In_ ULONG Code)
{
return (NTSTATUS)(((Severity & NT_SEVERITY_MASK) << NT_SEVERITY_SHIFT) |
((Facility & NT_FACILITY_MASK) << NT_FACILITY_SHIFT) |
(Code & NT_CODE_MASK));
}

#pragma endregion

#pragma region Basic Types
Expand Down Expand Up @@ -300,6 +316,16 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
typedef __typeof__(nullptr) nullptr_t;
#endif

#if defined(__cplusplus)
#define TYPE_OF decltype
#elif _MSC_FULL_VER >= 193933428
#define TYPE_OF __typeof__
#endif

#if defined(TYPE_OF)
#define FIELD_TYPE(type, field) TYPE_OF(((type*)NULL)->field)
#endif

#pragma endregion

#pragma region Types
Expand Down

0 comments on commit 98b5ddf

Please sign in to comment.