| 1 | use super::trie_string; |
| 2 | use crate::{Error, Result, TrieEntry, UdevHwdb, UdevList}; |
| 3 | use heapless::Vec; |
| 4 | |
| 5 | /// Maximum length for a file line. |
| 6 | pub const LINE_MAX: usize = 4096; |
| 7 | |
| 8 | /// Line buffer for parsing HWDB on-disk file format. |
| 9 | #[repr(C)] |
| 10 | #[derive(Clone, Debug, Default, PartialEq)] |
| 11 | pub struct LineBuf { |
| 12 | bytes: Vec<u8, LINE_MAX>, |
| 13 | } |
| 14 | |
| 15 | impl LineBuf { |
| 16 | /// Creates a new [LineBuf]. |
| 17 | pub const fn new() -> Self { |
| 18 | Self { bytes: Vec::new() } |
| 19 | } |
| 20 | |
| 21 | /// Gets the line buffer as a string reference. |
| 22 | pub fn get(&self) -> &str { |
| 23 | std::str::from_utf8(self.bytes.as_ref()).unwrap_or("") |
| 24 | } |
| 25 | |
| 26 | /// Adds `val` string to the [LineBuf]. |
| 27 | /// |
| 28 | /// `val` must not cause the total length of the [LineBuf] to exceed [LINE_MAX]. |
| 29 | pub fn add(&mut self, val: &str) -> Result<()> { |
| 30 | self.bytes.extend_from_slice(val.as_bytes()).map_err(|_| { |
| 31 | Error::UdevHwdb(format!( |
| 32 | "byte string exceeds the max line length: {LINE_MAX}" |
| 33 | )) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | /// Adds `val` character to the [LineBuf]. |
| 38 | /// |
| 39 | /// `val` must not cause the total length of the [LineBuf] to exceed [LINE_MAX]. |
| 40 | pub fn add_char<C: Into<u8>>(&mut self, val: C) -> Result<()> { |
| 41 | self.bytes.push(val.into()).map_err(|_| { |
| 42 | Error::UdevHwdb(format!("character exceeds the max line length: {LINE_MAX}")) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | /// Removes `count` characters from the [LineBuf]. |
| 47 | /// |
| 48 | /// **NOTE**: clears the buffer if count is larger than the [LineBuf] length. |
| 49 | pub fn remove(&mut self, count: usize) { |
| 50 | if count >= self.bytes.len() { |
| 51 | self.bytes.clear(); |
| 52 | } else { |
| 53 | self.bytes.truncate(self.bytes.len() - count); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Removes a single character from the [LineBuf]. |
| 58 | pub fn remove_char(&mut self) { |
| 59 | self.remove(1); |
| 60 | } |
| 61 | |
| 62 | /// Searches the [LineBuf] for a matching property. |
| 63 | /// |
| 64 | /// If a property is found, it is added to [`list`](UdevList). |
| 65 | pub fn trie_fnmatch( |
| 66 | &mut self, |
| 67 | list: &mut UdevList, |
| 68 | hwdb_buf: &[u8], |
| 69 | entry: &TrieEntry, |
| 70 | p: usize, |
| 71 | search: &str, |
| 72 | ) -> Result<()> { |
| 73 | let prefix_off = entry.node().prefix_off() as usize; |
| 74 | let prefix = trie_string(hwdb_buf, prefix_off)?; |
| 75 | let prefix_len = prefix.len(); |
| 76 | |
| 77 | log::trace!( |
| 78 | "Entering fnmatch, prefix: {prefix}, glob: {}, search: {search}", |
| 79 | self.get() |
| 80 | ); |
| 81 | |
| 82 | let (start, end) = ( |
| 83 | p, |
| 84 | prefix |
| 85 | .as_bytes() |
| 86 | .iter() |
| 87 | .skip(p) |
| 88 | .position(|c| *c == b'\0') |
| 89 | .unwrap_or(prefix_len), |
| 90 | ); |
| 91 | |
| 92 | // the logic of add only if within bounds but always remove is odd but the linebuf_add return is not checked in https://github.com/cr8t/udev/issues/25#L187 |
| 93 | // so behavior should match |
| 94 | if (start..=end).contains(&prefix_len) { |
| 95 | self.add(&prefix[start..end])?; |
| 96 | } |
| 97 | |
| 98 | for child in entry.children().iter() { |
| 99 | let child_off = child.child_off() as usize; |
| 100 | if child_off < hwdb_buf.len() { |
| 101 | self.add_char(child.c())?; |
| 102 | self.trie_fnmatch( |
| 103 | list, |
| 104 | hwdb_buf, |
| 105 | &TrieEntry::try_from(&hwdb_buf[child_off..])?, |
| 106 | 0, |
| 107 | search, |
| 108 | )?; |
| 109 | self.remove_char(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if glob::Pattern::new(self.get())?.matches(search) { |
| 114 | log::trace!("Found matching entry, entry: {entry:?}, search: {search}"); |
| 115 | for value in entry.values().iter() { |
| 116 | UdevHwdb::_add_property( |
| 117 | list, |
| 118 | trie_string(hwdb_buf, value.key_off() as usize)?, |
| 119 | trie_string(hwdb_buf, value.value_off() as usize)?, |
| 120 | )?; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | self.remove(end.saturating_sub(start)); |
| 125 | |
| 126 | Ok(()) |
| 127 | } |
| 128 | } |