Debian Codegraph @

third_party/rust/winreg/src/types.rs

raw
1 // Copyright 2015, Igor Shaula
2 // Licensed under the MIT License <LICENSE or
3 // http://opensource.org/licenses/MIT>. This file
4 // may not be copied, modified, or distributed
5 // except according to those terms.
6
7 //! Traits for loading/saving Registry values
8 use super::enums::*;
9 use super::winapi::shared::winerror;
10 use super::RegValue;
11 use super::{to_utf16, v16_to_v8};
12 use std::ffi::{OsStr, OsString};
13 use std::io;
14 use std::os::windows::ffi::OsStringExt;
15 use std::slice;
16
17 /// A trait for types that can be loaded from registry values.
18 ///
19 /// **NOTE:** Uses `from_utf16_lossy` when converting to `String`.
20 ///
21 /// **NOTE:** When converting to `String` or `OsString`, trailing `NULL` characters are trimmed
22 /// and line separating `NULL` characters in `REG_MULTI_SZ` are replaced by `\n`
23 /// effectively representing the value as a multiline string.
24 /// When converting to `Vec<String>` or `Vec<OsString>` `NULL` is used as a strings separator.
25 pub trait FromRegValue: Sized {
26 fn from_reg_value(val: &RegValue) -> io::Result<Self>;
27 }
28
29 impl FromRegValue for String {
30 fn from_reg_value(val: &RegValue) -> io::Result<String> {
31 match val.vtype {
32 REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => {
33 let words = unsafe {
34 #[allow(clippy::cast_ptr_alignment)]
35 slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
36 };
37 let mut s = String::from_utf16_lossy(words);
38 while s.ends_with('\u{0}') {
39 s.pop();
40 }
41 if val.vtype == REG_MULTI_SZ {
42 return Ok(s.replace("\u{0}", "\n"));
43 }
44 Ok(s)
45 }
46 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
47 }
48 }
49 }
50
51 impl FromRegValue for Vec<String> {
52 fn from_reg_value(val: &RegValue) -> io::Result<Vec<String>> {
53 match val.vtype {
54 REG_MULTI_SZ => {
55 let words = unsafe {
56 slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
57 };
58 let mut s = String::from_utf16_lossy(words);
59 while s.ends_with('\u{0}') {
60 s.pop();
61 }
62 let v: Vec<String> = s.split('\u{0}').map(|x| x.to_owned()).collect();
63 Ok(v)
64 }
65 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
66 }
67 }
68 }
69
70 impl FromRegValue for OsString {
71 fn from_reg_value(val: &RegValue) -> io::Result<OsString> {
72 match val.vtype {
73 REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => {
74 let mut words = unsafe {
75 #[allow(clippy::cast_ptr_alignment)]
76 slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
77 };
78 while let Some(0) = words.last() {
79 words = &words[0..words.len() - 1];
80 }
81 let s = OsString::from_wide(words);
82 Ok(s)
83 }
84 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
85 }
86 }
87 }
88
89 impl FromRegValue for Vec<OsString> {
90 fn from_reg_value(val: &RegValue) -> io::Result<Vec<OsString>> {
91 match val.vtype {
92 REG_MULTI_SZ => {
93 let mut words = unsafe {
94 slice::from_raw_parts(val.bytes.as_ptr() as *const u16, val.bytes.len() / 2)
95 };
96 while let Some(0) = words.last() {
97 words = &words[0..words.len() - 1];
98 }
99 let v: Vec<OsString> = words
100 .split(|ch| *ch == 0u16)
101 .map(|x| OsString::from_wide(x))
102 .collect();
103 Ok(v)
104 }
105 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
106 }
107 }
108 }
109
110 impl FromRegValue for u32 {
111 fn from_reg_value(val: &RegValue) -> io::Result<u32> {
112 match val.vtype {
113 #[allow(clippy::cast_ptr_alignment)]
114 REG_DWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u32) }),
115 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
116 }
117 }
118 }
119
120 impl FromRegValue for u64 {
121 fn from_reg_value(val: &RegValue) -> io::Result<u64> {
122 match val.vtype {
123 #[allow(clippy::cast_ptr_alignment)]
124 REG_QWORD => Ok(unsafe { *(val.bytes.as_ptr() as *const u64) }),
125 _ => werr!(winerror::ERROR_BAD_FILE_TYPE),
126 }
127 }
128 }
129
130 /// A trait for types that can be written into registry values.
131 ///
132 /// **NOTE:** Adds trailing `NULL` character to `str`, `String`, `OsStr` and `OsString` values
133 pub trait ToRegValue {
134 fn to_reg_value(&self) -> RegValue;
135 }
136
137 macro_rules! to_reg_value_sz {
138 ($t:ty$(, $l:lifetime)*) => {
139 impl<$($l,)*> ToRegValue for $t {
140 fn to_reg_value(&self) -> RegValue {
141 RegValue {
142 bytes: v16_to_v8(&to_utf16(self)),
143 vtype: REG_SZ,
144 }
145 }
146 }
147 }
148 }
149
150 to_reg_value_sz!(String);
151 to_reg_value_sz!(&'a str, 'a);
152 to_reg_value_sz!(OsString);
153 to_reg_value_sz!(&'a OsStr, 'a);
154
155 macro_rules! to_reg_value_multi_sz {
156 ($t:ty$(, $l:lifetime)*) => {
157 impl<$($l,)*> ToRegValue for Vec<$t> {
158 fn to_reg_value(&self) -> RegValue {
159 let mut os_strings = self
160 .into_iter()
161 .map(to_utf16)
162 .collect::<Vec<_>>()
163 .concat();
164 os_strings.push(0);
165 RegValue {
166 bytes: v16_to_v8(&os_strings),
167 vtype: REG_MULTI_SZ,
168 }
169 }
170 }
171 }
172 }
173
174 to_reg_value_multi_sz!(String);
175 to_reg_value_multi_sz!(&'a str, 'a);
176 to_reg_value_multi_sz!(OsString);
177 to_reg_value_multi_sz!(&'a OsStr, 'a);
178
179 impl ToRegValue for u32 {
180 fn to_reg_value(&self) -> RegValue {
181 let bytes: Vec<u8> =
182 unsafe { slice::from_raw_parts((self as *const u32) as *const u8, 4).to_vec() };
183 RegValue {
184 bytes,
185 vtype: REG_DWORD,
186 }
187 }
188 }
189
190 impl ToRegValue for u64 {
191 fn to_reg_value(&self) -> RegValue {
192 let bytes: Vec<u8> =
193 unsafe { slice::from_raw_parts((self as *const u64) as *const u8, 8).to_vec() };
194 RegValue {
195 bytes,
196 vtype: REG_QWORD,
197 }
198 }
199 }