Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 164 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions crates/zipfixup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ publish.workspace = true
name = "zipfixup"
crate-type = ["cdylib"]

test = false

[dependencies]
retour = "0.3.1"
region = "3.0.2"

[dependencies.winapi]
version = "0.3.9"
[dependencies.windows]
version = "0.62.0"
default-features = false
features = ["debugapi", "libloaderapi"]
features = [
"Win32_System_Diagnostics_Debug", # OutputDebugStringA/OutputDebugStringW
"Win32_System_LibraryLoader", # DisableThreadLibraryCalls
"Win32_System_SystemServices", # DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH
]

[build-dependencies]
cc = "1.2"
17 changes: 7 additions & 10 deletions crates/zipfixup/src/dbg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! [`output!`] macro to log messages via the Debug API, to be viewed in e.g.
//! [DebugView](https://learn.microsoft.com/en-us/sysinternals/downloads/debugview).
use ::winapi::um::debugapi::{OutputDebugStringA, OutputDebugStringW};
use windows::Win32::System::Diagnostics::Debug::{OutputDebugStringA, OutputDebugStringW};
use windows::core::{PCSTR, PCWSTR};

macro_rules! output {
($fmt:literal $(, $args:expr)* $(,)?) => {{
Expand Down Expand Up @@ -36,22 +37,18 @@ fn encode_unicode(msg: &str) -> Vec<u16> {
/// version may be slightly faster.
pub(crate) fn output_debug_string_w(msg: &str) {
let v: Vec<u16> = encode_unicode(msg);
let p: *const u16 = v.as_ptr();
let p = PCWSTR::from_raw(v.as_ptr());
unsafe { OutputDebugStringW(p) };
// paranoia: ensure `v` is valid until after `OutputDebugStringW`
drop(v);
}

fn encode_ascii(msg: &str) -> Vec<i8> {
fn encode_ascii(msg: &str) -> Vec<u8> {
const ZF: &[u8] = b"[ZF] ";
let msg = msg
.chars()
.map(|c| if c.is_ascii() { c as u8 } else { b'?' });
ZF.iter()
.copied()
.chain(msg.chain(Some(0)))
.map(|b| b as i8)
.collect()
ZF.iter().copied().chain(msg.chain(Some(0))).collect()
}

/// Output an ASCII debug string.
Expand All @@ -61,8 +58,8 @@ fn encode_ascii(msg: &str) -> Vec<i8> {
/// Microsoft Unicode ineptness).
#[allow(dead_code, reason = "Use Unicode version by default")]
pub(crate) fn output_debug_string_a(msg: &str) {
let v: Vec<i8> = encode_ascii(&msg);
let p: *const i8 = v.as_ptr();
let v: Vec<u8> = encode_ascii(&msg);
let p = PCSTR::from_raw(v.as_ptr());
unsafe { OutputDebugStringA(p) };
// paranoia: ensure `v` is valid until after `OutputDebugStringA`
drop(v);
Expand Down
14 changes: 9 additions & 5 deletions crates/zipfixup/src/dll_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
//! executable that loaded the DLL.
use crate::{Result, output};
use std::sync::Mutex;
use winapi::shared::minwindef::{BOOL, DWORD, HINSTANCE, LPVOID, TRUE};
use winapi::um::libloaderapi::DisableThreadLibraryCalls;
use winapi::um::winnt::{DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH};
use windows::Win32::Foundation::{HMODULE, TRUE};
use windows::Win32::System::LibraryLoader::DisableThreadLibraryCalls;
use windows::Win32::System::SystemServices::{DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH};

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[unsafe(no_mangle)]
#[allow(non_snake_case)]
extern "system" fn DllMain(dll_module: HINSTANCE, call_reason: DWORD, _reserved: LPVOID) -> BOOL {
extern "system" fn DllMain(
hlibmodule: HMODULE,
call_reason: u32,
_reserved: *mut std::ffi::c_void,
) -> windows::core::BOOL {
match call_reason {
DLL_PROCESS_ATTACH => {
output!("DLL_PROCESS_ATTACH");
// disable DLL_THREAD_ATTACH/DLL_THREAD_DETACH notifications, since
// we don't need them, and may help with spawning threads
unsafe { DisableThreadLibraryCalls(dll_module) };
let _ = unsafe { DisableThreadLibraryCalls(hlibmodule) };
// it's unclear what is allowed to be done in DllMain.
// theoretically, even spawning a thread is not "recommended":
// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
Expand Down
Loading