Skip to content
Merged
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
29 changes: 13 additions & 16 deletions crates/vm/src/stdlib/winreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ mod winreg {

// Slice the data buffer to the actual size returned.
let data_slice = &ret_data_buf[..current_data_size as usize];
let py_data = reg_to_py(vm, data_slice, reg_type)?;
let py_data = reg_to_py(vm, data_slice, reg_type);

// Return tuple (value_name, data, type)
return Ok(vm
Expand Down Expand Up @@ -646,7 +646,7 @@ mod winreg {
let wide_name = WideCString::from_str(name).map_err(|e| e.to_pyexception(vm))?;
let (ret_buf, typ) = host_winreg::query_value_bytes(hkey, &wide_name)
.map_err(|err| os_error_from_windows_code(vm, err as i32))?;
let obj = reg_to_py(vm, &ret_buf, typ)?;
let obj = reg_to_py(vm, &ret_buf, typ);
// Return tuple (value, type)
Ok(vm.ctx.new_tuple(vec![obj, vm.ctx.new_int(typ).into()]))
}
Expand Down Expand Up @@ -696,22 +696,22 @@ mod winreg {
}
}

fn reg_to_py(vm: &VirtualMachine, ret_data: &[u8], typ: u32) -> PyResult {
fn reg_to_py(vm: &VirtualMachine, ret_data: &[u8], typ: u32) -> PyObjectRef {
match typ {
REG_DWORD => {
// If there isn’t enough data, return 0.
let val = ret_data
.first_chunk::<4>()
.copied()
.map_or(0, u32::from_ne_bytes);
Ok(vm.ctx.new_int(val).into())
vm.ctx.new_int(val).into()
}
REG_QWORD => {
let val = ret_data
.first_chunk::<8>()
.copied()
.map_or(0, u64::from_ne_bytes);
Ok(vm.ctx.new_int(val).into())
vm.ctx.new_int(val).into()
}
REG_SZ | REG_EXPAND_SZ => {
let u16_slice = host_winreg::bytes_as_wide_slice(ret_data);
Expand All @@ -720,13 +720,12 @@ mod winreg {
.iter()
.position(|&c| c == 0)
.unwrap_or(u16_slice.len());
let s = String::from_utf16(&u16_slice[..len])
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
Ok(vm.ctx.new_str(s).into())
let s = Wtf8Buf::from_wide(&u16_slice[..len]);
vm.ctx.new_str(s).into()
}
REG_MULTI_SZ => {
if ret_data.is_empty() {
Ok(vm.ctx.new_list(vec![]).into())
vm.ctx.new_list(vec![]).into()
} else {
let u16_slice = host_winreg::bytes_as_wide_slice(ret_data);
let u16_count = u16_slice.len();
Expand All @@ -742,27 +741,25 @@ mod winreg {
let mut start = 0;
for i in 0..len {
if u16_slice[i] == 0 {
let s = String::from_utf16(&u16_slice[start..i])
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
let s = Wtf8Buf::from_wide(&u16_slice[start..i]);
strings.push(vm.ctx.new_str(s).into());
start = i + 1;
}
}
// Handle last string if not null-terminated
if start < len {
let s = String::from_utf16(&u16_slice[start..len])
.map_err(|e| vm.new_value_error(format!("UTF16 error: {e}")))?;
let s = Wtf8Buf::from_wide(&u16_slice[start..len]);
strings.push(vm.ctx.new_str(s).into());
}
Ok(vm.ctx.new_list(strings).into())
vm.ctx.new_list(strings).into()
}
}
// For REG_BINARY and any other unknown types, return a bytes object if data exists.
_ => {
if ret_data.is_empty() {
Ok(vm.ctx.none())
vm.ctx.none()
} else {
Ok(vm.ctx.new_bytes(ret_data.to_vec()).into())
vm.ctx.new_bytes(ret_data.to_vec()).into()
}
}
}
Expand Down
Loading