| 1 | use craterust-analyzer cargo udevrs 0.4.0 crate/::{Errorrust-analyzer cargo udevrs 0.4.0 error/Error#, Resultrust-analyzer cargo udevrs 0.4.0 error/Result#}; |
| 2 | |
| 3 | /// Gets whether the provided character is whitelisted. |
| 4 | pub fn whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c: char, white: &str) -> bool { |
| 5 | c.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_digit().() |
| 6 | || c.is_ascii_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_uppercase().() |
| 7 | || c.is_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_lowercase().() |
| 8 | || "#+-.:=@_".containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(c) |
| 9 | || white.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(c) |
| 10 | } |
| 11 | |
| 12 | /// Encodes a `devnode` name, removing potentially dangerous characters. |
| 13 | pub fn encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/encode_devnode_name().(arg: &str) -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 14 | if arg.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 15 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo udevrs 0.4.0 error/Error#::UdevUtilrust-analyzer cargo udevrs 0.4.0 error/Error#UdevUtil#("empty encode string".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())) |
| 16 | } else { |
| 17 | let arg_len = arg.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 18 | let mut ret = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(arg_len.saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(4)); |
| 19 | // check for a nul-terminated string |
| 20 | let null_pos = arg.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('\0').unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(arg_len); |
| 21 | |
| 22 | for c in arg[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#null_pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 23 | let seqlen = c.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().(); |
| 24 | if seqlen rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().1 { |
| 25 | let mut bytes = [0u8; 4]; |
| 26 | ret.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(c.encode_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]encode_utf8().(&mut bytes)); |
| 27 | } else if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'\\' || !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "") { |
| 28 | ret = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{ret}\\x{:02x}", c as u8); |
| 29 | } else { |
| 30 | ret.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(c); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ret) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | #[cfg(test)] |
| 39 | mod testrust-analyzer cargo udevrs 0.4.0 util/device_nodes/test/ { |
| 40 | use superrust-analyzer cargo udevrs 0.4.0 util/device_nodes/::*; |
| 41 | |
| 42 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 43 | fn test_encode_devnode_string_emptyrust-analyzer cargo udevrs 0.4.0 util/device_nodes/test/test_encode_devnode_string_empty().() { |
| 44 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/encode_devnode_name().("").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 45 | } |
| 46 | |
| 47 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 48 | fn test_whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/test/test_whitelisted_char_for_devnode().() { |
| 49 | // check ASCII digits are whitelisted |
| 50 | for c in '0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'9' { |
| 51 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "")); |
| 52 | } |
| 53 | // check ASCII lowercase are whitelisted |
| 54 | for c in 'a'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'z' { |
| 55 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "")); |
| 56 | } |
| 57 | // check ASCII uppercase are whitelisted |
| 58 | for c in 'A'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'Z' { |
| 59 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "")); |
| 60 | } |
| 61 | // check ASCII special are whitelisted |
| 62 | for c in "#+-.:=@_".charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 63 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "")); |
| 64 | } |
| 65 | // check non-default whitelist are rejected |
| 66 | for c in "`~%^&*(){}!$|\\".charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 67 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "")); |
| 68 | } |
| 69 | // check non-default whitelist accepted with custom whitelist |
| 70 | for c in "`~%^&*(){}!$|\\".charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() { |
| 71 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(whitelisted_char_for_devnoderust-analyzer cargo udevrs 0.4.0 util/device_nodes/whitelisted_char_for_devnode().(c, "`~%^&*(){}!$|\\")); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 76 | fn test_encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/test/test_encode_devnode_name().() -> Resultrust-analyzer cargo udevrs 0.4.0 error/Result#<()> { |
| 77 | let arg_str: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = "#+-.:=@_" |
| 78 | .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().() |
| 79 | .chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(('a'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'z').chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().('A'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'Z').chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().('0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'9')) |
| 80 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 81 | let enc_str = encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/encode_devnode_name().(arg_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 82 | |
| 83 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(arg_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), enc_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 84 | |
| 85 | let esc_str = "`~%^&*(){}!$|\\"; |
| 86 | let exp_str = encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/encode_devnode_name().(esc_str)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | |
| 88 | // check non-whitelisted ASCII characters |
| 89 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(esc_str.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().().saturating_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_mul().(4), exp_str.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()); |
| 90 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 91 | exp_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), |
| 92 | "\\x60\\x7e\\x25\\x5e\\x26\\x2a\\x28\\x29\\x7b\\x7d\\x21\\x24\\x7c\\x5c" |
| 93 | ); |
| 94 | |
| 95 | // check multi-byte UTF-8 characters |
| 96 | let utf8_str = "💖"; |
| 97 | let exp_utf8_str = encode_devnode_namerust-analyzer cargo udevrs 0.4.0 util/device_nodes/encode_devnode_name().(utf8_str)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 98 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(exp_utf8_str.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), "💖"); |
| 99 | |
| 100 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 101 | } |
| 102 | } |