osdns 0.2.0

Safe, transactional control of operating-system DNS configuration
Documentation
//! Project-local Windows sys bindings generated by `tools/win32-bindings`.
//!
//! Do not hand-edit [`bindings`]. Regenerate with:
//! `cargo run --manifest-path tools/win32-bindings/Cargo.toml`

#![allow(
    non_snake_case,
    non_camel_case_types,
    non_upper_case_globals,
    dead_code,
    clippy::all
)]

use std::path::PathBuf;

mod bindings;

pub(crate) use bindings::*;

/// Machine-wide ProgramData known-folder id
/// (`FOLDERID_ProgramData` = `{62AB00DC-680E-4B1D-82D8-86ED557DBC6C}`).
///
/// The 0.100 Win32 metadata does not currently expose this GUID constant by
/// name, so the well-known value from the Windows SDK is defined here.
pub(crate) const FOLDERID_PROGRAM_DATA: GUID = GUID {
    data1: 0x62AB_00DC,
    data2: 0x680E,
    data3: 0x4B1D,
    data4: [0x82, 0xD8, 0x86, 0xED, 0x55, 0x7D, 0xBC, 0x6C],
};

/// Opens `HKLM\<path>` with an explicit 64-bit registry view.
pub(crate) fn open_hklm(
    path: &str,
    write: bool,
) -> windows_registry::Result<windows_registry::Key> {
    let mut options = windows_registry::LOCAL_MACHINE.options();
    options.read();
    if write {
        options.write();
    }
    options.wow64_64().open(path)
}

/// Creates or opens `HKLM\<path>` with an explicit 64-bit registry view.
pub(crate) fn create_hklm(path: &str) -> windows_registry::Result<windows_registry::Key> {
    windows_registry::LOCAL_MACHINE
        .options()
        .read()
        .write()
        .create()
        .wow64_64()
        .open(path)
}

/// Reads a NUL-terminated UTF-16 C string. Null pointer → empty string.
///
/// # Safety
/// `pointer` must be null or refer to a valid NUL-terminated wide string.
pub(crate) unsafe fn wide_to_string(pointer: *const u16) -> String {
    if pointer.is_null() {
        return String::new();
    }
    let mut len = 0usize;
    // SAFETY: caller guarantees a valid NUL-terminated wide string.
    unsafe {
        while *pointer.add(len) != 0 {
            len += 1;
        }
        String::from_utf16_lossy(std::slice::from_raw_parts(pointer, len))
    }
}

/// Reads a NUL-terminated UTF-16 path. Null pointer → empty path.
///
/// # Safety
/// `pointer` must be null or refer to a valid NUL-terminated wide string.
pub(crate) unsafe fn wide_to_path(pointer: *const u16) -> PathBuf {
    // SAFETY: same contract as [`wide_to_string`].
    PathBuf::from(unsafe { wide_to_string(pointer) })
}