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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2023, Igor Shaula
// Licensed under the MIT License <LICENSE or
// http://opensource.org/licenses/MIT>. This file
// may not be copied, modified, or distributed
// except according to those terms.

//! Traits for loading/saving Registry values
use crate::common::*;
use crate::enums::*;
use crate::RegValue;
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::io;
use std::os::windows::ffi::OsStringExt;
use std::slice;
use windows_sys::Win32::Foundation;

/// A trait for types that can be loaded from registry values.
///
/// **NOTE:** Uses `from_utf16_lossy` when converting to `String`.
///
/// **NOTE:** When converting to `String` or `OsString`, trailing `NULL` characters are trimmed
/// and line separating `NULL` characters in `REG_MULTI_SZ` are replaced by `\n`
/// effectively representing the value as a multiline string.
/// When converting to `Vec<String>` or `Vec<OsString>` `NULL` is used as a strings separator.
pub trait FromRegValue: Sized {
    fn from_reg_value(val: &RegValue) -> io::Result<Self>;
}

impl FromRegValue for String {
    fn from_reg_value(val: &RegValue) -> io::Result<String> {
        match val.vtype {
            REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => {
                let words = unsafe {
                    #[allow(clippy::cast_ptr_alignment)]
                    slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
                };
                let mut s = String::from_utf16_lossy(words);
                while s.ends_with('\u{0}') {
                    s.pop();
                }
                if val.vtype == REG_MULTI_SZ {
                    return Ok(s.replace('\u{0}', "\n"));
                }
                Ok(s)
            }
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

impl FromRegValue for Vec<String> {
    fn from_reg_value(val: &RegValue) -> io::Result<Vec<String>> {
        match val.vtype {
            REG_MULTI_SZ => {
                let words = unsafe {
                    slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
                };
                let mut s = String::from_utf16_lossy(words);
                while s.ends_with('\u{0}') {
                    s.pop();
                }
                let v: Vec<String> = s.split('\u{0}').map(|x| x.to_owned()).collect();
                Ok(v)
            }
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

impl FromRegValue for OsString {
    fn from_reg_value(val: &RegValue) -> io::Result<OsString> {
        match val.vtype {
            REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => {
                let mut words = unsafe {
                    #[allow(clippy::cast_ptr_alignment)]
                    slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
                };
                while let Some(0) = words.last() {
                    words = &words[0..words.len() - 1];
                }
                let s = OsString::from_wide(words);
                Ok(s)
            }
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

impl FromRegValue for Vec<OsString> {
    fn from_reg_value(val: &RegValue) -> io::Result<Vec<OsString>> {
        match val.vtype {
            REG_MULTI_SZ => {
                let mut words = unsafe {
                    slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
                };
                while let Some(0) = words.last() {
                    words = &words[0..words.len() - 1];
                }
                let v: Vec<OsString> = words
                    .split(|ch| *ch == 0u16)
                    .map(OsString::from_wide)
                    .collect();
                Ok(v)
            }
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

macro_rules! try_from_reg_value_int {
    ($val:expr, $map:expr) => {
        $val.bytes
            .as_slice()
            .try_into()
            .map($map)
            .map_err(|_| io::Error::from_raw_os_error(Foundation::ERROR_INVALID_DATA as i32))
    };
}

impl FromRegValue for u32 {
    fn from_reg_value(val: &RegValue) -> io::Result<u32> {
        match val.vtype {
            REG_DWORD => try_from_reg_value_int!(val, u32::from_ne_bytes),
            REG_DWORD_BIG_ENDIAN => try_from_reg_value_int!(val, u32::from_be_bytes),
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

impl FromRegValue for u64 {
    fn from_reg_value(val: &RegValue) -> io::Result<u64> {
        match val.vtype {
            REG_QWORD => try_from_reg_value_int!(val, u64::from_ne_bytes),
            _ => werr!(Foundation::ERROR_BAD_FILE_TYPE),
        }
    }
}

/// A trait for types that can be written into registry values.
///
/// **NOTE:** Adds trailing `NULL` character to `str`, `String`, `OsStr` and `OsString` values
pub trait ToRegValue {
    fn to_reg_value(&self) -> RegValue;
}

macro_rules! to_reg_value_sz {
    ($t:ty$(, $l:lifetime)*) => {
        impl<$($l,)*> ToRegValue for $t {
            fn to_reg_value(&self) -> RegValue {
                RegValue {
                    bytes: v16_to_v8(&to_utf16(self)),
                    vtype: REG_SZ,
                }
            }
        }
    }
}

to_reg_value_sz!(String);
to_reg_value_sz!(&'a str, 'a);
to_reg_value_sz!(OsString);
to_reg_value_sz!(&'a OsStr, 'a);

macro_rules! to_reg_value_multi_sz {
    ($t:ty$(, $l:lifetime)*) => {
        impl<$($l,)*> ToRegValue for Vec<$t> {
            fn to_reg_value(&self) -> RegValue {
                let mut os_strings = self
                    .into_iter()
                    .map(to_utf16)
                    .collect::<Vec<_>>()
                    .concat();
                os_strings.push(0);
                RegValue {
                    bytes: v16_to_v8(&os_strings),
                    vtype: REG_MULTI_SZ,
                }
            }
        }
    }
}

to_reg_value_multi_sz!(String);
to_reg_value_multi_sz!(&'a str, 'a);
to_reg_value_multi_sz!(OsString);
to_reg_value_multi_sz!(&'a OsStr, 'a);

impl ToRegValue for u32 {
    fn to_reg_value(&self) -> RegValue {
        let bytes: Vec<u8> =
            unsafe { slice::from_raw_parts((self as *const u32) as *const u8, 4).to_vec() };
        RegValue {
            bytes,
            vtype: REG_DWORD,
        }
    }
}

impl ToRegValue for u64 {
    fn to_reg_value(&self) -> RegValue {
        let bytes: Vec<u8> =
            unsafe { slice::from_raw_parts((self as *const u64) as *const u8, 8).to_vec() };
        RegValue {
            bytes,
            vtype: REG_QWORD,
        }
    }
}