Handle PowerShell UTF-16 output and show Windows edition

This commit is contained in:
Esdras Renan 2025-10-20 21:52:17 -03:00
parent 459bd53693
commit 0aa474c88e
2 changed files with 49 additions and 15 deletions

View file

@ -680,25 +680,45 @@ fn collect_windows_extended() -> serde_json::Value {
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
fn parse_utf16_le_bytes(bytes: &[u8]) -> Option<serde_json::Value> {
if bytes.len() % 2 != 0 {
return None;
}
let utf16: Vec<u16> = bytes
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
let text = String::from_utf16(&utf16).ok()?;
let trimmed = text.trim();
if trimmed.is_empty() {
return None;
}
serde_json::from_str(trimmed).ok()
}
fn parse_powershell_json(bytes: &[u8]) -> Option<serde_json::Value> {
if bytes.is_empty() {
return None;
}
if bytes.starts_with(&[0xFF, 0xFE]) {
// PowerShell usa UTF-16LE por padrão no Windows; converter para UTF-8.
let data = &bytes[2..];
if data.len() % 2 != 0 {
return None;
}
let utf16: Vec<u16> = data
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
let text = String::from_utf16(&utf16).ok()?;
serde_json::from_str(&text).ok()
} else {
serde_json::from_slice(bytes).ok()
return parse_utf16_le_bytes(&bytes[2..]);
}
if bytes.len() >= 2 && bytes[1] == 0 {
if let Some(val) = parse_utf16_le_bytes(bytes) {
return Some(val);
}
}
if bytes.contains(&0) {
if let Some(val) = parse_utf16_le_bytes(bytes) {
return Some(val);
}
}
let text = std::str::from_utf8(bytes).ok()?;
let trimmed = text.trim();
if trimmed.is_empty() {
return None;
}
serde_json::from_str(trimmed).ok()
}
fn ps(cmd: &str) -> Option<serde_json::Value> {