#![allow(non_snake_case)]
use std::boxed::Box;
use core::ffi::c_void;
use core::ptr;
use windows_sys::Win32::Foundation::{FALSE, HANDLE, SetLastError, TRUE};
use windows_sys::Win32::Storage::FileSystem::{
BY_HANDLE_FILE_INFORMATION, FILE_ATTRIBUTE_REPARSE_POINT, GetFileInformationByHandle,
};
use windows_sys::Win32::System::Threading::{
CRITICAL_SECTION, DeleteCriticalSection, EnterCriticalSection, InitializeCriticalSection,
LeaveCriticalSection, TlsAlloc, TlsFree, TlsSetValue, TryEnterCriticalSection,
};
use windows_sys::core::BOOL;
const CSTR_LESS_THAN: i32 = 1;
const CSTR_EQUAL: i32 = 2;
const CSTR_GREATER_THAN: i32 = 3;
#[unsafe(no_mangle)]
pub unsafe extern "system" fn CompareStringOrdinal(
first: *const u16,
first_length: i32,
second: *const u16,
second_length: i32,
ignore_case: BOOL,
) -> i32 {
if first.is_null() || second.is_null() {
return CSTR_EQUAL;
}
let flags = if ignore_case != FALSE {
windows_sys::Win32::Globalization::NORM_IGNORECASE
} else {
0
};
let answer = unsafe {
windows_sys::Win32::Globalization::CompareStringW(
windows_sys::Win32::Globalization::LOCALE_USER_DEFAULT,
flags,
first,
first_length,
second,
second_length,
)
};
if answer == 0 {
let left = unsafe { core::slice::from_raw_parts(first, first_length.max(0) as usize) };
let right = unsafe { core::slice::from_raw_parts(second, second_length.max(0) as usize) };
return match left.cmp(right) {
core::cmp::Ordering::Less => CSTR_LESS_THAN,
core::cmp::Ordering::Equal => CSTR_EQUAL,
core::cmp::Ordering::Greater => CSTR_GREATER_THAN,
};
}
match answer.cmp(&0) {
core::cmp::Ordering::Less => CSTR_LESS_THAN,
core::cmp::Ordering::Equal => CSTR_EQUAL,
core::cmp::Ordering::Greater => CSTR_GREATER_THAN,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn CreateSymbolicLinkW(
_link: *const u16,
_target: *const u16,
_flags: u32,
) -> BOOL {
unsafe { SetLastError(50) };
FALSE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn CreateWaitableTimerExW(
_attributes: *const c_void,
_name: *const u16,
_flags: u32,
_access: u32,
) -> HANDLE {
ptr::null_mut()
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn DeleteProcThreadAttributeList(_list: *mut c_void) {}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn FlsAlloc(_callback: *const c_void) -> u32 {
let key = unsafe { TlsAlloc() };
if key == u32::MAX {
return u32::MAX;
}
key
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn FlsFree(index: u32) -> BOOL {
unsafe { TlsFree(index) }
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn FlsSetValue(index: u32, value: *mut c_void) -> BOOL {
unsafe { TlsSetValue(index, value) }
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn GetFileInformationByHandleEx(
handle: HANDLE,
class: i32,
information: *mut c_void,
_size: u32,
) -> BOOL {
let mut found: BY_HANDLE_FILE_INFORMATION = unsafe { core::mem::zeroed() };
let answer = unsafe { GetFileInformationByHandle(handle, &mut found) };
if answer == FALSE {
return FALSE;
}
if class == 9 {
let attributes = found.dwFileAttributes & !FILE_ATTRIBUTE_REPARSE_POINT;
unsafe {
let place = information as *mut u32;
*place = attributes;
*place.add(1) = 0;
}
}
TRUE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn GetFinalPathNameByHandleW(
_handle: HANDLE,
_buffer: *mut u16,
_size: u32,
_flags: u32,
) -> u32 {
0
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn GetHostNameW(name: *mut u16, size: *mut u32) -> i32 {
unsafe { windows_sys::Win32::System::WindowsProgramming::GetComputerNameW(name, size) }
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn InitializeProcThreadAttributeList(
_list: *mut c_void,
_count: u32,
_flags: u32,
size: *mut usize,
) -> BOOL {
if !size.is_null() {
unsafe { *size = 0 };
}
unsafe { SetLastError(100) };
FALSE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn IsThreadAFiber() -> BOOL {
FALSE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn SetFileInformationByHandle(
_handle: HANDLE,
_class: i32,
_information: *mut c_void,
_size: u32,
) -> BOOL {
FALSE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn SetThreadStackGuarantee(_size: *mut u32) -> BOOL {
FALSE
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn UpdateProcThreadAttribute(
_list: *mut c_void,
_flags: u32,
_attribute: usize,
_value: *mut c_void,
_size: usize,
) -> BOOL {
FALSE
}
struct TheLock {
section: CRITICAL_SECTION,
}
unsafe fn the_section(lock: *mut c_void) -> *mut CRITICAL_SECTION {
if lock.is_null() {
return ptr::null_mut();
}
let place = lock as *mut core::sync::atomic::AtomicPtr<CRITICAL_SECTION>;
let current = unsafe { (*place).load(core::sync::atomic::Ordering::Acquire) };
if !current.is_null() {
return current;
}
let mut fresh = Box::new(TheLock {
section: unsafe { core::mem::zeroed() },
});
unsafe { InitializeCriticalSection(&mut fresh.section) };
let pointer = Box::into_raw(fresh) as *mut CRITICAL_SECTION;
let previous = unsafe {
(*place).compare_exchange(
core::ptr::null_mut(),
pointer,
core::sync::atomic::Ordering::AcqRel,
core::sync::atomic::Ordering::Acquire,
)
};
match previous {
Ok(_) => pointer,
Err(previous) => {
unsafe {
DeleteCriticalSection(&mut *pointer);
let _ = Box::from_raw(pointer as *mut TheLock);
}
previous
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn AcquireSRWLockExclusive(lock: *mut c_void) {
let section = unsafe { the_section(lock) };
if section.is_null() {
return;
}
unsafe { EnterCriticalSection(section) };
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn ReleaseSRWLockExclusive(lock: *mut c_void) {
let section = unsafe { the_section(lock) };
if section.is_null() {
return;
}
unsafe { LeaveCriticalSection(section) };
}
#[unsafe(no_mangle)]
pub unsafe extern "system" fn TryAcquireSRWLockExclusive(lock: *mut c_void) -> BOOL {
let section = unsafe { the_section(lock) };
if section.is_null() {
return FALSE;
}
unsafe { TryEnterCriticalSection(section) }
}
struct TheList([*const (); 18]);
unsafe impl Sync for TheList {}
unsafe impl Send for TheList {}
#[used]
static THE_FLOOR: TheList = TheList([
CompareStringOrdinal as *const (),
CreateSymbolicLinkW as *const (),
CreateWaitableTimerExW as *const (),
DeleteProcThreadAttributeList as *const (),
FlsAlloc as *const (),
FlsFree as *const (),
FlsSetValue as *const (),
GetFileInformationByHandleEx as *const (),
GetFinalPathNameByHandleW as *const (),
GetHostNameW as *const (),
InitializeProcThreadAttributeList as *const (),
IsThreadAFiber as *const (),
SetFileInformationByHandle as *const (),
SetThreadStackGuarantee as *const (),
UpdateProcThreadAttribute as *const (),
AcquireSRWLockExclusive as *const (),
ReleaseSRWLockExclusive as *const (),
TryAcquireSRWLockExclusive as *const (),
]);
pub fn the_floor() -> usize {
unsafe {
let _ = CompareStringOrdinal(ptr::null(), 0, ptr::null(), 0, FALSE);
let _ = CreateSymbolicLinkW(ptr::null(), ptr::null(), 0);
let _ = CreateWaitableTimerExW(ptr::null(), ptr::null(), 0, 0);
DeleteProcThreadAttributeList(ptr::null_mut());
let key = FlsAlloc(ptr::null());
let _ = FlsSetValue(key, ptr::null_mut());
let _ = FlsFree(key);
let _ = GetFileInformationByHandleEx(ptr::null_mut(), 0, ptr::null_mut(), 0);
let _ = GetFinalPathNameByHandleW(ptr::null_mut(), ptr::null_mut(), 0, 0);
let mut nome = [0u16; 64];
let mut tamanho = 64u32;
let _ = GetHostNameW(nome.as_mut_ptr(), &mut tamanho);
let mut tamanho_da_lista = 0usize;
let _ = InitializeProcThreadAttributeList(ptr::null_mut(), 0, 0, &mut tamanho_da_lista);
let _ = IsThreadAFiber();
let _ = SetFileInformationByHandle(ptr::null_mut(), 0, ptr::null_mut(), 0);
let mut pilha = 0u32;
let _ = SetThreadStackGuarantee(&mut pilha);
let _ = UpdateProcThreadAttribute(ptr::null_mut(), 0, 0, ptr::null_mut(), 0);
let _ = AcquireSRWLockExclusive(ptr::null_mut());
let _ = ReleaseSRWLockExclusive(ptr::null_mut());
let _ = TryAcquireSRWLockExclusive(ptr::null_mut());
}
THE_FLOOR.0.len()
}