| 1 |
"""Windows-specific structures and types using ctypes. |
| 2 |
|
| 3 |
It includes utility classes such as `NTSTATUS` for representing Windows NT |
| 4 |
status codes and `UNICODE_STRING` for handling Unicode strings in Windows API |
| 5 |
interactions. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import ctypes |
| 11 |
import functools |
| 12 |
from ctypes import wintypes |
| 13 |
from typing import ClassVar |
| 14 |
|
| 15 |
|
| 16 |
@functools.total_ordering |
| 17 |
class NTSTATUS(ctypes.c_long): |
| 18 |
"""Class representing NTSTATUS values.""" |
| 19 |
|
| 20 |
def __eq__(self, other: int | NTSTATUS) -> bool: |
| 21 |
"""Compare the NTSTATUS value with another value. |
| 22 |
|
| 23 |
Args: |
| 24 |
other (int | NTSTATUS): The value to compare with. |
| 25 |
|
| 26 |
Returns: |
| 27 |
bool: True if the NTSTATUS value is equal to the other value, |
| 28 |
False otherwise. |
| 29 |
""" |
| 30 |
if hasattr(other, "value"): |
| 31 |
other = other.value |
| 32 |
return self.value == other |
| 33 |
|
| 34 |
def __ne__(self, other: int | NTSTATUS) -> bool: |
| 35 |
"""Compare the NTSTATUS value with another value. |
| 36 |
|
| 37 |
Args: |
| 38 |
other (int | NTSTATUS): The value to compare with. |
| 39 |
|
| 40 |
Returns: |
| 41 |
bool: True if the NTSTATUS value is not equal to the other value, |
| 42 |
False otherwise. |
| 43 |
""" |
| 44 |
if hasattr(other, "value"): |
| 45 |
other = other.value |
| 46 |
return self.value != other |
| 47 |
|
| 48 |
def __lt__(self, other: int | NTSTATUS) -> bool: |
| 49 |
"""Compare the NTSTATUS value with another value. |
| 50 |
|
| 51 |
Args: |
| 52 |
other (int | NTSTATUS): The value to compare with. |
| 53 |
|
| 54 |
Returns: |
| 55 |
bool: True if the NTSTATUS value is less than the other value, |
| 56 |
False otherwise. |
| 57 |
""" |
| 58 |
if hasattr(other, "value"): |
| 59 |
other = other.value |
| 60 |
return self.value < other |
| 61 |
|
| 62 |
def __bool__(self) -> bool: |
| 63 |
"""Check if the NTSTATUS value indicates success. |
| 64 |
|
| 65 |
Returns: |
| 66 |
bool: True if the NTSTATUS value indicates success (0 or greater), |
| 67 |
False otherwise. |
| 68 |
""" |
| 69 |
return self.value >= 0 |
| 70 |
|
| 71 |
def __repr__(self) -> str: |
| 72 |
"""Return a string representation of the NTSTATUS value. |
| 73 |
|
| 74 |
Returns: |
| 75 |
str: A string representation of the NTSTATUS value in hexadecimal format. |
| 76 |
""" |
| 77 |
value = ctypes.c_ulong.from_buffer(self).value |
| 78 |
return f"NTSTATUS(0x{value:08x})" |
| 79 |
|
| 80 |
|
| 81 |
class UNICODE_STRING(ctypes.Structure): # noqa: N801 |
| 82 |
"""Class representing a UNICODE_STRING structure.""" |
| 83 |
|
| 84 |
_fields_: ClassVar[list[tuple]] = [ |
| 85 |
("Length", wintypes.USHORT), |
| 86 |
("MaximumLength", wintypes.USHORT), |
| 87 |
("Buffer", wintypes.LPWSTR), |
| 88 |
] |