use base64::Engine;
use base64::engine::general_purpose::STANDARD as B64;
#[derive(Debug)]
pub struct CommandOutput {
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub exit_code: i32,
}
pub fn encode_powershell_command(script: &str) -> String {
let mut utf16 = Vec::with_capacity(script.len() * 2);
for c in script.encode_utf16() {
utf16.extend_from_slice(&c.to_le_bytes());
}
B64.encode(&utf16)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_powershell_roundtrip() {
let script = "Get-Process";
let encoded = encode_powershell_command(script);
let decoded_bytes = B64.decode(&encoded).unwrap();
let u16s: Vec<u16> = decoded_bytes
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect();
let decoded = String::from_utf16(&u16s).unwrap();
assert_eq!(decoded, script);
}
#[test]
fn encode_powershell_unicode() {
let script = "Write-Output 'h\u{00e9}llo w\u{00f6}rld'";
let encoded = encode_powershell_command(script);
let decoded_bytes = B64.decode(&encoded).unwrap();
let u16s: Vec<u16> = decoded_bytes
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect();
let decoded = String::from_utf16(&u16s).unwrap();
assert_eq!(decoded, script);
}
#[test]
fn encode_powershell_empty() {
let encoded = encode_powershell_command("");
assert!(encoded.is_empty());
}
}