| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| //! Pure `#![no_std]` Zircon `processargs` namespace directory name table | |
| //! (`names_off` / `names_num`) lookup helper. | |
| //! | |
| //! Mirrors `zircon/system/public/zircon/processargs.h` and | |
| //! `sdk/lib/c/startup/musl-processargs/processargs.c`. | |
| #![no_std] | |
| /// Handle info type for namespace directory handles (`PA_HND(PA_NS_DIR, idx)`). | |
| pub const PA_NS_DIR: u8 = 0x20; | |
| /// Size in bytes of `struct zx_proc_args` (`9 * sizeof(u32)`). | |
| pub const ZX_PROCARGS_HEADER_SIZE: u32 = 36; | |
| /// Canonical `PA_HND(PA_NS_DIR, idx)` handle-info tag (`zircon/processargs.h`). | |
| #[inline(always)] | |
| pub const fn pa_ns_dir(idx: u16) -> u32 { | |
| (PA_NS_DIR as u32) | ((idx as u32) << 16) | |
| } | |
| #[inline(always)] | |
| fn validate_names_fields(buf_len: usize, names_off: u32, names_num: u32) -> bool { | |
| let off = names_off as usize; | |
| let num = names_num as usize; | |
| off >= (ZX_PROCARGS_HEADER_SIZE as usize) && off <= buf_len && (buf_len - off) >= num | |
| } | |
| /// Validate the `names_off` / `names_num` NUL-terminated namespace path table | |
| /// inside `buf` and return the 0-based index `< names_num` of the first entry | |
| /// whose bytes equal `target`. | |
| /// | |
| /// Returns `None` if: | |
| /// - `names_num == 0`, | |
| /// - `names_off` / `names_num` fail validation against `buf.len()`, | |
| /// - any of the `names_num` entries is missing its NUL terminator before `buf.len()`, or | |
| /// - no entry equals `target`. | |
| /// | |
| /// Pure, read-only leaf function with a 0-byte stack frame. | |
| #[curry::verified] | |
| pub fn find_ns_name_index( | |
| buf: &[u8], | |
| names_off: u32, | |
| names_num: u32, | |
| target: &[u8], | |
| ) -> Option<usize> { | |
| let buf_len = buf.len(); | |
| if names_num == 0 || !validate_names_fields(buf_len, names_off, names_num) { | |
| return None; | |
| } | |
| let buf_ptr = buf.as_ptr(); | |
| let target_ptr = target.as_ptr(); | |
| let target_len = target.len(); | |
| let mut pos: usize = names_off as usize; | |
| let mut idx: u32 = 0; | |
| let mut entry_pos: usize = 0; | |
| let mut matched: bool = true; | |
| let mut found: Option<usize> = None; | |
| while idx < names_num { | |
| if pos >= buf_len { | |
| return None; | |
| } | |
| // SAFETY: `pos < buf_len` is checked directly above. | |
| let b = unsafe { *buf_ptr.add(pos) }; | |
| pos += 1; | |
| if b == 0 { | |
| if matched && entry_pos == target_len && found.is_none() { | |
| found = Some(idx as usize); | |
| } | |
| idx += 1; | |
| entry_pos = 0; | |
| matched = true; | |
| } else if matched { | |
| // SAFETY: `entry_pos < target_len` is checked directly below. | |
| if entry_pos < target_len && unsafe { *target_ptr.add(entry_pos) } == b { | |
| entry_pos += 1; | |
| } else { | |
| matched = false; | |
| } | |
| } | |
| } | |
| found | |
| } |