284 lines
7.4 KiB
Rust
284 lines
7.4 KiB
Rust
|
|
|
|
#![allow(non_snake_case)]
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
|
use std::ffi::{c_char, c_void};
|
|
|
|
use super::xgameruntime::Guid;
|
|
|
|
const OK: i32 = 0;
|
|
|
|
const INVALID_STRING_UUID: i32 = 1705;
|
|
|
|
const OUT_OF_MEMORY: i32 = 14;
|
|
|
|
extern "system" fn UuidCreate(uuid: *mut Guid) -> i32 {
|
|
create(uuid, false)
|
|
}
|
|
|
|
extern "system" fn UuidCreateSequential(uuid: *mut Guid) -> i32 {
|
|
create(uuid, true)
|
|
}
|
|
|
|
fn create(uuid: *mut Guid, _sequential: bool) -> i32 {
|
|
if uuid.is_null() {
|
|
return INVALID_STRING_UUID;
|
|
}
|
|
let mut bytes = [0u8; 16];
|
|
if !random(&mut bytes) {
|
|
return OUT_OF_MEMORY;
|
|
}
|
|
|
|
bytes[7] = (bytes[7] & 0x0f) | 0x40;
|
|
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
|
|
unsafe { uuid.write(from_bytes(bytes)) };
|
|
OK
|
|
}
|
|
|
|
fn random(bytes: &mut [u8; 16]) -> bool {
|
|
#[cfg(windows)]
|
|
{
|
|
use windows_sys::Win32::Security::Cryptography::{
|
|
BCRYPT_USE_SYSTEM_PREFERRED_RNG, BCryptGenRandom,
|
|
};
|
|
|
|
let status = unsafe {
|
|
BCryptGenRandom(
|
|
std::ptr::null_mut(),
|
|
bytes.as_mut_ptr(),
|
|
bytes.len() as u32,
|
|
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
|
|
)
|
|
};
|
|
status >= 0
|
|
}
|
|
#[cfg(not(windows))]
|
|
{
|
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
static N: AtomicU64 = AtomicU64::new(0);
|
|
let n = N.fetch_add(1, Ordering::Relaxed)
|
|
^ std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.map_or(0, |d| d.as_nanos() as u64);
|
|
for (i, b) in bytes.iter_mut().enumerate() {
|
|
*b = (n >> (8 * (i % 8))) as u8 ^ (i as u8).wrapping_mul(31);
|
|
}
|
|
true
|
|
}
|
|
}
|
|
|
|
extern "system" fn UuidFromStringA(text: *const c_char, uuid: *mut Guid) -> i32 {
|
|
if text.is_null() || uuid.is_null() {
|
|
return INVALID_STRING_UUID;
|
|
}
|
|
|
|
let bytes = unsafe { std::ffi::CStr::from_ptr(text.cast()) }.to_bytes();
|
|
match parse(bytes.iter().copied()) {
|
|
|
|
Some(g) => unsafe {
|
|
uuid.write(g);
|
|
OK
|
|
},
|
|
None => INVALID_STRING_UUID,
|
|
}
|
|
}
|
|
|
|
extern "system" fn UuidFromStringW(text: *const u16, uuid: *mut Guid) -> i32 {
|
|
if text.is_null() || uuid.is_null() {
|
|
return INVALID_STRING_UUID;
|
|
}
|
|
let mut units = Vec::new();
|
|
|
|
unsafe {
|
|
let mut at = text;
|
|
while *at != 0 {
|
|
units.push(*at);
|
|
at = at.add(1);
|
|
}
|
|
}
|
|
match parse(units.into_iter().map(|u| u as u32)) {
|
|
|
|
Some(g) => unsafe {
|
|
uuid.write(g);
|
|
OK
|
|
},
|
|
None => INVALID_STRING_UUID,
|
|
}
|
|
}
|
|
|
|
extern "system" fn UuidToStringA(uuid: *const Guid, out: *mut *mut c_char) -> i32 {
|
|
|
|
let Some(g) = (unsafe { uuid.as_ref() }) else {
|
|
return INVALID_STRING_UUID;
|
|
};
|
|
if out.is_null() {
|
|
return INVALID_STRING_UUID;
|
|
}
|
|
let text = format(g);
|
|
let mut bytes = Vec::with_capacity(37);
|
|
bytes.extend_from_slice(&text);
|
|
bytes.push(0);
|
|
|
|
unsafe { out.write(allocate(&bytes).cast()) };
|
|
OK
|
|
}
|
|
|
|
extern "system" fn UuidToStringW(uuid: *const Guid, out: *mut *mut u16) -> i32 {
|
|
|
|
let Some(g) = (unsafe { uuid.as_ref() }) else {
|
|
return INVALID_STRING_UUID;
|
|
};
|
|
if out.is_null() {
|
|
return INVALID_STRING_UUID;
|
|
}
|
|
let text = format(g);
|
|
let mut units: Vec<u16> = text.iter().map(|&b| u16::from(b)).collect();
|
|
units.push(0);
|
|
let bytes: Vec<u8> = units.iter().flat_map(|u| u.to_le_bytes()).collect();
|
|
|
|
unsafe { out.write(allocate(&bytes).cast()) };
|
|
OK
|
|
}
|
|
|
|
extern "system" fn RpcStringFreeA(string: *mut *mut c_char) -> i32 {
|
|
free(string.cast());
|
|
OK
|
|
}
|
|
|
|
extern "system" fn RpcStringFreeW(string: *mut *mut u16) -> i32 {
|
|
free(string.cast());
|
|
OK
|
|
}
|
|
|
|
fn from_bytes(b: [u8; 16]) -> Guid {
|
|
Guid {
|
|
data1: u32::from_le_bytes([b[0], b[1], b[2], b[3]]),
|
|
data2: u16::from_le_bytes([b[4], b[5]]),
|
|
data3: u16::from_le_bytes([b[6], b[7]]),
|
|
data4: [b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]],
|
|
}
|
|
}
|
|
|
|
fn format(g: &Guid) -> [u8; 36] {
|
|
const HEX: &[u8; 16] = b"0123456789abcdef";
|
|
let mut text = [b'-'; 36];
|
|
let pairs = [
|
|
(0, g.data1 as u64, 8),
|
|
(9, g.data2 as u64, 4),
|
|
(14, g.data3 as u64, 4),
|
|
(19, g.data4[0] as u64, 2),
|
|
(21, g.data4[1] as u64, 2),
|
|
(24, g.data4[2] as u64, 2),
|
|
(26, g.data4[3] as u64, 2),
|
|
(28, g.data4[4] as u64, 2),
|
|
(30, g.data4[5] as u64, 2),
|
|
(32, g.data4[6] as u64, 2),
|
|
(34, g.data4[7] as u64, 2),
|
|
];
|
|
for (at, value, digits) in pairs {
|
|
for i in 0..digits {
|
|
let shift = 4 * (digits - 1 - i);
|
|
text[at + i] = HEX[((value >> shift) & 0xf) as usize];
|
|
}
|
|
}
|
|
text
|
|
}
|
|
|
|
fn parse(mut chars: impl Iterator<Item = impl Into<u32>>) -> Option<Guid> {
|
|
let mut digits = [0u8; 32];
|
|
let mut n = 0;
|
|
for c in &mut chars {
|
|
let c = c.into();
|
|
if n == 32 {
|
|
|
|
return (c == u32::from(b'}')).then(|| assemble(&digits));
|
|
}
|
|
if c == u32::from(b'-') || (n == 0 && c == u32::from(b'{')) {
|
|
continue;
|
|
}
|
|
let digit = match c {
|
|
0x30..=0x39 => c - 0x30,
|
|
0x61..=0x66 => c - 0x61 + 10,
|
|
0x41..=0x46 => c - 0x41 + 10,
|
|
_ => return None,
|
|
};
|
|
digits[n] = digit as u8;
|
|
n += 1;
|
|
}
|
|
(n == 32).then(|| assemble(&digits))
|
|
}
|
|
|
|
fn assemble(digits: &[u8; 32]) -> Guid {
|
|
let byte = |i: usize| (digits[2 * i] << 4) | digits[2 * i + 1];
|
|
|
|
Guid {
|
|
data1: u32::from_be_bytes([byte(0), byte(1), byte(2), byte(3)]),
|
|
data2: u16::from_be_bytes([byte(4), byte(5)]),
|
|
data3: u16::from_be_bytes([byte(6), byte(7)]),
|
|
data4: [
|
|
byte(8),
|
|
byte(9),
|
|
byte(10),
|
|
byte(11),
|
|
byte(12),
|
|
byte(13),
|
|
byte(14),
|
|
byte(15),
|
|
],
|
|
}
|
|
}
|
|
|
|
fn allocate(bytes: &[u8]) -> *mut u8 {
|
|
let len = 8 + bytes.len();
|
|
let layout = std::alloc::Layout::from_size_align(len, 8).expect("a small allocation");
|
|
|
|
unsafe {
|
|
let base = std::alloc::alloc(layout);
|
|
if base.is_null() {
|
|
return std::ptr::null_mut();
|
|
}
|
|
base.cast::<u64>().write_unaligned(bytes.len() as u64);
|
|
std::ptr::copy_nonoverlapping(bytes.as_ptr(), base.add(8), bytes.len());
|
|
base.add(8)
|
|
}
|
|
}
|
|
|
|
fn free(pointer: *mut *mut c_void) {
|
|
if pointer.is_null() {
|
|
return;
|
|
}
|
|
|
|
unsafe {
|
|
let at = pointer.read();
|
|
if at.is_null() {
|
|
return;
|
|
}
|
|
let base = at.cast::<u8>().sub(8);
|
|
let len = base.cast::<u64>().read_unaligned() as usize;
|
|
let layout = std::alloc::Layout::from_size_align(8 + len, 8).expect("a small allocation");
|
|
std::alloc::dealloc(base, layout);
|
|
pointer.write(std::ptr::null_mut());
|
|
}
|
|
}
|
|
|
|
pub(crate) fn module() -> crate::hle::HleModule {
|
|
crate::hle::HleModule {
|
|
name: "rpcrt4",
|
|
dlls: &["rpcrt4.dll"],
|
|
exports: vec![
|
|
crate::hle_export!(UuidCreate),
|
|
crate::hle_export!(UuidCreateSequential),
|
|
crate::hle_export!(UuidFromStringA),
|
|
crate::hle_export!(UuidFromStringW),
|
|
crate::hle_export!(UuidToStringA),
|
|
crate::hle_export!(UuidToStringW),
|
|
crate::hle_export!(RpcStringFreeA),
|
|
crate::hle_export!(RpcStringFreeW),
|
|
],
|
|
}
|
|
}
|
|
|