everything-direct 0.1.0

Direct IPC bindings for Everything Search Engine on Windows.
Documentation
use std::ops::Deref;
use windows::Win32::Foundation::ERROR_INVALID_DATA;
use windows::core::{HSTRING, HStringBuilder, Result};

pub(crate) fn join_hstring<Str, Sep>(strings: &[Str], separator: Sep) -> HSTRING
where
    Str: Deref<Target = [u16]>,
    Sep: Deref<Target = [u16]>,
{
    let mut len: usize = 0;
    for s in strings {
        len += s.len();
    }
    let mut builder = HStringBuilder::new(len + separator.len() * strings.len().saturating_sub(1));
    let buf: &mut [u16] = builder.as_mut();
    let mut index: usize = 0;
    for (i, s) in strings.iter().enumerate() {
        if i > 0 {
            buf[index..index + separator.len()].copy_from_slice(&separator);
            index += separator.len();
        }
        buf[index..index + s.len()].copy_from_slice(&s);
        index += s.len();
    }
    builder.into()
}

/// Reads a flat/POD type from a byte slice at the given offset.
///
/// # Safety
///
/// This function is unsafe because:
/// - It performs unchecked pointer arithmetic
/// - It reads uninitialized memory if the buffer is too small
/// - The caller must ensure `T` is valid for the memory layout
/// - The data must be properly aligned for type `T`
pub(crate) unsafe fn read_flat<T: Copy>(data: &[u8], offset: &mut usize) -> Result<T> {
    let size = std::mem::size_of::<T>();
    if data.len() < *offset + size {
        Err(windows::core::Error::new(
            ERROR_INVALID_DATA.to_hresult(),
            "Data too small to read number",
        ))?;
    }
    // SAFETY: We've verified the buffer has enough bytes.
    // The pointer arithmetic is safe because offset is within bounds.
    let value = unsafe { (data.as_ptr().add(*offset) as *const T).read() };
    *offset += size;
    Ok(value)
}

/// Reads a wide string (UTF-16) from a byte slice at the given offset.
///
/// The string length is read as a u32 prefix, followed by the UTF-16 data.
///
/// # Safety
///
/// This function is unsafe because:
/// - It performs unchecked pointer casts and arithmetic
/// - It reads from raw pointers
/// - The caller must ensure the data contains a valid length-prefixed wide string
pub(crate) unsafe fn read_wstring(data: &[u8], offset: &mut usize) -> Result<HSTRING> {
    // SAFETY: read_flat is safe here because we're reading a u32
    let str_len: u32 = unsafe { read_flat(data, offset)? };
    let str_len = str_len + 1; // include null terminator
    let str_size = str_len as usize * std::mem::size_of::<u16>();
    if data.len() < *offset + str_size {
        Err(windows::core::Error::new(
            ERROR_INVALID_DATA.to_hresult(),
            "Data too small to read HSTRING",
        ))?;
    }
    // SAFETY: We've verified the buffer has enough bytes for the string.
    // The cast to *const u16 is valid because we're reading u16 values.
    let str_slice = unsafe {
        core::slice::from_raw_parts(data.as_ptr().add(*offset) as *const u16, str_len as usize)
    };
    *offset += str_size;
    Ok(HSTRING::from_wide(str_slice))
}