#[cfg(target_os="windows")]
pub mod windows {
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::ffi::{c_void, c_int};
pub type HANDLE = *mut c_void;
pub type LPVOID = *mut c_void;
pub type SIZE_T = usize;
pub type DWORD = u32;
pub type WORD = u16;
pub type DWORD_PTR = *mut DWORD;
pub type BOOL = c_int;
pub const INVALID_HANDLE_VALUE: HANDLE = std::ptr::with_exposed_provenance_mut(-1_isize as usize);
pub const TRUE : BOOL = 1;
pub struct OwnedHandle(pub HANDLE);
unsafe impl Send for OwnedHandle {}
unsafe impl Sync for OwnedHandle {}
impl OwnedHandle {
pub unsafe fn new(handle: HANDLE) -> Self {
Self(handle)
}
}
impl Drop for OwnedHandle {
fn drop(&mut self) {
unsafe {CloseHandle(self.0)};
}
}
pub type ULONG = u32;
pub type ULONG_PTR = usize;
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[repr(transparent)]
pub struct NTSTATUS(u32);
impl NTSTATUS {
const TOP_NIBBLE: u32 = 0xF0000000;
pub const ERR_EOF: Self = Self(0xc0000011);
pub const PENDING: Self = Self(0x00000103);
pub fn is_success(&self) -> bool {
((self.0 & Self::TOP_NIBBLE) >> 28) < 0x4
}
}
pub type LargeInteger = i64;
pub type ApcIoRoutine = extern "C" fn (*mut c_void, *mut IoStatusBlock, ULONG);
#[repr(C)]
#[derive(Clone, Copy)]
pub struct IoStatusBlock {
pub status: IoStatusBlockStatus,
pub information: ULONG_PTR,
}
impl std::fmt::Debug for IoStatusBlock {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("IoStatusBlock")
.field("status", &unsafe{self.status.status})
.field("information", &self.information)
.finish()
}
}
impl IoStatusBlock{
pub fn empty() -> Self {
Self {
status: IoStatusBlockStatus {status: NTSTATUS(0)},
information: 0,
}
}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub union IoStatusBlockStatus {
pub status: NTSTATUS,
pub pointer: *mut c_void,
}
#[link(name = "kernel32")]
unsafe extern "system" {
pub fn VirtualAllocEx (
hProcess: HANDLE,
lpAddress: LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect : DWORD,
) -> *mut c_void;
pub fn VirtualProtectEx (
process: HANDLE,
Address: LPVOID,
Size: DWORD,
newProtect: DWORD,
oldProtect: &mut DWORD,
) -> BOOL;
pub fn VirtualFreeEx (
process: HANDLE,
Address: LPVOID,
Size: DWORD,
FreeType: DWORD,
) -> BOOL;
pub fn CreateFileA(
file_name: *const u8,
desired_access: DWORD,
share_mode: DWORD,
security_attributes: Option<&mut ()>,
creation_disposition: DWORD,
flags_and_attributes: DWORD,
template_file: HANDLE,
) -> HANDLE;
pub fn NtReadFile(
file_handle: HANDLE,
event: HANDLE,
apc_routine: Option<ApcIoRoutine>,
apc_context: *mut c_void,
io_status_block: *mut IoStatusBlock,
buffer: *mut u8,
length: ULONG,
offset: Option<&LargeInteger>,
key: Option<&ULONG>
) -> NTSTATUS;
pub fn SleepEx(
milliseconds: DWORD,
alertable: BOOL
) -> DWORD;
pub fn GetCurrentProcess() -> HANDLE;
pub fn CloseHandle(
object: HANDLE
) -> BOOL;
}
pub const MEM_COMMIT: DWORD = 0x00001000;
pub const MEM_RESERVE: DWORD = 0x00002000;
pub const MEM_RELEASE: DWORD = 0x00008000;
pub const PAGE_READ_WRITE: DWORD = 0x04;
pub const PAGE_NOACCESS: DWORD = 0x01;
pub struct AccessMask;
impl AccessMask {
pub const SYNCHRONIZE : DWORD = 1 << 20;
pub const GENERIC_READ : DWORD = 1 << 31;
}
pub struct ShareMode;
impl ShareMode {
pub const FILE_SHARE_READ : DWORD = 0x1;
}
pub struct CreateDisposition;
impl CreateDisposition {
pub const OPEN_EXISTING : DWORD = 3;
pub const OPEN_ALWAYS : DWORD = 4;
}
pub struct FlagsAndAttributes;
impl FlagsAndAttributes {
pub const FILE_ATTRIBUTE_NORMAL : DWORD = 0x00000080;
pub const FILE_FLAG_BACKUP_SEMANTICS : DWORD = 0x02000000;
pub const FILE_FLAG_OVERLAPPED : DWORD = 0x40000000;
}
}