Skip to content

Commit

Permalink
bring back error type
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Apr 27, 2024
1 parent f6a4218 commit 39fd56c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 42 deletions.
12 changes: 10 additions & 2 deletions crates/nsis-fn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{parse::Parse, parse_macro_input, ItemFn};
use syn::{parse::Parse, parse_macro_input, Ident, ItemFn};

struct NsisFn {
func: ItemFn,
Expand All @@ -22,7 +23,12 @@ pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
let block = func.block;
let attrs = func.attrs;

let new_ident = Ident::new(&format!("__{}", ident.to_string()), Span::call_site());

quote! {
#[inline(always)]
pub unsafe fn #new_ident() -> Result<(), ::nsis_plugin_api::Error> #block

#(#attrs)*
#[no_mangle]
#[allow(non_standard_style)]
Expand All @@ -33,7 +39,9 @@ pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
stacktop: *mut *mut ::nsis_plugin_api::stack_t,
) {
::nsis_plugin_api::exdll_init(string_size, variables, stacktop);
#block
if let Err(e) = #new_ident() {
e.push_err();
}
}
}
.into()
Expand Down
55 changes: 42 additions & 13 deletions crates/nsis-plugin-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ pub struct stack_t {
pub text: [wchar_t; 1],
}

static mut G_STRINGSIZE: c_int = 0;
static mut G_VARIABLES: *mut wchar_t = core::ptr::null_mut();
static mut G_STACKTOP: *mut *mut stack_t = core::ptr::null_mut();
pub static mut G_STRINGSIZE: c_int = 0;
pub static mut G_VARIABLES: *mut wchar_t = core::ptr::null_mut();
pub static mut G_STACKTOP: *mut *mut stack_t = core::ptr::null_mut();

#[inline(always)]
pub unsafe fn exdll_init(string_size: c_int, variables: *mut wchar_t, stacktop: *mut *mut stack_t) {
G_STRINGSIZE = string_size;
G_VARIABLES = variables;
Expand All @@ -51,43 +52,71 @@ pub const ONE: [u16; 2] = [49, 0];
pub const ZERO: [u16; 2] = [48, 0];
pub const NEGATIVE_ONE: [u16; 3] = [45, 49, 0];

pub unsafe fn push(bytes: &[u16]) {
#[derive(Debug)]
pub enum Error {
StackIsNull,
ParseIntError,
}

impl Error {
const fn description(&self) -> &str {
match self {
Error::StackIsNull => "Stack is null",
Error::ParseIntError => "Failed to parse integer",
}
}
pub fn push_err(&self) {
let _ = unsafe { pushstr(&self.description()) };
}
}

pub unsafe fn push(bytes: &[u16]) -> Result<(), Error> {
if G_STACKTOP.is_null() {
return;
return Err(Error::StackIsNull);
}

let n = size_of::<stack_t>() + G_STRINGSIZE as usize * 2;
let th = GlobalAlloc(GPTR, n) as *mut stack_t;
lstrcpyW((*th).text.as_ptr() as _, bytes.as_ptr());
(*th).next = *G_STACKTOP;
*G_STACKTOP = th;

Ok(())
}

pub unsafe fn pushstr(str: &str) {
pub unsafe fn pushstr(str: &str) -> Result<(), Error> {
let bytes = encode_wide(str);
push(&bytes)
}

pub unsafe fn pushint(int: i32) {
pub unsafe fn pushint(int: i32) -> Result<(), Error> {
let str = int.to_string();
pushstr(&str)
}

pub unsafe fn pop(out: &mut [u16]) {
pub unsafe fn pop() -> Result<Vec<u16>, Error> {
if G_STACKTOP.is_null() || (*G_STACKTOP).is_null() {
return;
return Err(Error::StackIsNull);
}

let mut out = vec![0_u16; G_STRINGSIZE as _];

let th: *mut stack_t = *G_STACKTOP;
lstrcpyW(out.as_mut_ptr(), (*th).text.as_ptr() as _);
*G_STACKTOP = (*th).next;
GlobalFree(th as _);

Ok(out)
}

pub unsafe fn popstr() -> Result<String, Error> {
let bytes = pop()?;
Ok(decode_wide(&bytes))
}

pub unsafe fn popstring() -> String {
let mut bytes = vec![0_u16; G_STRINGSIZE as _];
pop(&mut bytes);
decode_wide(&bytes)
pub unsafe fn popint() -> Result<i32, Error> {
let str = popstr()?;
str.parse().map_err(|_| Error::ParseIntError)
}

pub fn encode_wide(str: &str) -> Vec<u16> {
Expand Down
35 changes: 17 additions & 18 deletions crates/nsis-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ nsis_plugin!();
///
/// This function always expects 1 string on the stack ($1: name) and will panic otherwise.
#[nsis_fn]
fn FindProcess() {
let name = popstring();
fn FindProcess() -> Result<(), Error> {
let name = popstr()?;

if !get_processes(&name).is_empty() {
push(&ZERO);
push(&ZERO)
} else {
push(&ONE);
push(&ONE)
}
}

Expand All @@ -46,8 +46,8 @@ fn FindProcess() {
///
/// This function always expects 1 string on the stack ($1: name) and will panic otherwise.
#[nsis_fn]
fn FindProcessCurrentUser() {
let name = popstring();
fn FindProcessCurrentUser() -> Result<(), Error> {
let name = popstr()?;

let processes = get_processes(&name);

Expand All @@ -56,15 +56,15 @@ fn FindProcessCurrentUser() {
.into_iter()
.any(|pid| belongs_to_user(user_sid, pid))
{
push(&ZERO);
push(&ZERO)
} else {
push(&ONE);
push(&ONE)
}
// Fall back to perMachine checks if we can't get current user id
} else if processes.is_empty() {
push(&ONE);
push(&ONE)
} else {
push(&ZERO);
push(&ZERO)
}
}

Expand All @@ -74,15 +74,15 @@ fn FindProcessCurrentUser() {
///
/// This function always expects 1 string on the stack ($1: name) and will panic otherwise.
#[nsis_fn]
fn KillProcess() {
let name = popstring();
fn KillProcess() -> Result<(), Error> {
let name = popstr()?;

let processes = get_processes(&name);

if !processes.is_empty() && processes.into_iter().map(kill).all(|b| b) {
push(&ZERO);
push(&ZERO)
} else {
push(&ONE);
push(&ONE)
}
}

Expand All @@ -92,14 +92,13 @@ fn KillProcess() {
///
/// This function always expects 1 string on the stack ($1: name) and will panic otherwise.
#[nsis_fn]
fn KillProcessCurrentUser() {
let name = popstring();
fn KillProcessCurrentUser() -> Result<(), Error> {
let name = popstr()?;

let processes = get_processes(&name);

if processes.is_empty() {
push(&ONE);
return;
return push(&ONE);
}

let success = if let Some(user_sid) = get_sid(GetCurrentProcessId()) {
Expand Down
14 changes: 8 additions & 6 deletions crates/nsis-semvercompare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ nsis_plugin!();
///
/// This function always expects 2 strings on the stack ($v1, $v2) and will panic otherwise.
#[nsis_fn]
fn SemverCompare() {
let v1 = popstring();
let v2 = popstring();
fn SemverCompare() -> Result<(), Error> {
let v1 = popstr()?;
let v2 = popstr()?;

match compare(&v1, &v2) {
-1 => push(&NEGATIVE_ONE),
0 => push(&ZERO),
1 => push(&ONE),
-1 => push(&NEGATIVE_ONE)?,
0 => push(&ZERO)?,
1 => push(&ONE)?,
_ => unreachable!(),
}

Ok(())
}

fn compare(v1: &str, v2: &str) -> i32 {
Expand Down
3 changes: 0 additions & 3 deletions demo.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ Unicode true
ShowInstDetails show

!addplugindir ".\target\i686-pc-windows-msvc\release"
!addplugindir ".\target\i686-pc-windows-msvc\debug"
!addplugindir "$%CARGO_TARGET_DIR%\i686-pc-windows-msvc\release"
!addplugindir "$%CARGO_TARGET_DIR%\i686-pc-windows-msvc\debug"
!addplugindir "$%CARGO_BUILD_TARGET_DIR%\i686-pc-windows-msvc\release"
!addplugindir "$%CARGO_BUILD_TARGET_DIR%\i686-pc-windows-msvc\debug"

!include "MUI2.nsh"

Expand Down

0 comments on commit 39fd56c

Please sign in to comment.