windows_registry/
value.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use super::*;

/// A registry value.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Value {
    pub(crate) data: Data,
    pub(crate) ty: Type,
}

impl Value {
    /// Gets the type of the registry value.
    pub fn ty(&self) -> Type {
        self.ty
    }

    /// Sets the type of the registry value. This does not change the value.
    pub fn set_ty(&mut self, ty: Type) {
        self.ty = ty;
    }

    /// Gets the value as a slice of u16 for raw wide characters.
    pub fn as_wide(&self) -> &[u16] {
        self.data.as_wide()
    }
}

impl core::ops::Deref for Value {
    type Target = [u8];

    fn deref(&self) -> &[u8] {
        &self.data
    }
}

impl AsRef<[u8]> for Value {
    fn as_ref(&self) -> &[u8] {
        &self.data
    }
}

impl From<u32> for Value {
    fn from(from: u32) -> Self {
        Self {
            data: from.to_le_bytes().into(),
            ty: Type::U32,
        }
    }
}

impl TryFrom<Value> for u32 {
    type Error = Error;
    fn try_from(from: Value) -> Result<Self> {
        Ok(from_le_bytes(from.ty, &from)?.try_into()?)
    }
}

impl From<u64> for Value {
    fn from(from: u64) -> Self {
        Self {
            data: from.to_le_bytes().into(),
            ty: Type::U64,
        }
    }
}

impl TryFrom<Value> for u64 {
    type Error = Error;
    fn try_from(from: Value) -> Result<Self> {
        from_le_bytes(from.ty, &from)
    }
}

impl TryFrom<Value> for String {
    type Error = Error;
    fn try_from(from: Value) -> Result<Self> {
        match from.ty {
            Type::String | Type::ExpandString => Ok(Self::from_utf16(trim(from.data.as_wide()))?),
            _ => Err(invalid_data()),
        }
    }
}

impl From<&str> for Value {
    fn from(from: &str) -> Self {
        Self {
            data: Data::from_slice(pcwstr(from).as_bytes()),
            ty: Type::String,
        }
    }
}

impl TryFrom<Value> for Vec<String> {
    type Error = Error;
    fn try_from(from: Value) -> Result<Self> {
        match from.ty {
            Type::MultiString => Ok(from
                .data
                .as_wide()
                .split(|c| *c == 0)
                .map(String::from_utf16_lossy)
                .collect()),
            _ => Ok(vec![String::try_from(from)?]),
        }
    }
}

impl TryFrom<Value> for HSTRING {
    type Error = Error;
    fn try_from(from: Value) -> Result<Self> {
        match from.ty {
            Type::String | Type::ExpandString => Ok(Self::from_wide(trim(from.data.as_wide()))),
            _ => Err(invalid_data()),
        }
    }
}

impl From<&HSTRING> for Value {
    fn from(from: &HSTRING) -> Self {
        Self {
            data: Data::from_slice(as_bytes(from)),
            ty: Type::String,
        }
    }
}

impl From<&[u8]> for Value {
    fn from(from: &[u8]) -> Self {
        Self {
            data: Data::from_slice(from),
            ty: Type::Bytes,
        }
    }
}

impl<const N: usize> From<[u8; N]> for Value {
    fn from(from: [u8; N]) -> Self {
        Self {
            data: Data::from_slice(&from),
            ty: Type::Bytes,
        }
    }
}

fn trim(mut wide: &[u16]) -> &[u16] {
    while wide.last() == Some(&0) {
        wide = &wide[..wide.len() - 1];
    }

    wide
}