hivex 0.2.1

(Hopefully) idiomatic bindings to the Hivex library
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Registry hive value wrappers and descriptions

use {
	crate::{
		sys,
		utils::{boxed_slice_from_ffi, boxed_str_from_ffi, check_pointer_null, check_status_zero},
		BorrowedHive, LibCBox,
	},
	std::{
		borrow::Cow,
		ffi::{CStr, CString},
		str::Utf8Error,
	},
};

/// A handle to a value
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct ValueHandle(pub(crate) sys::hive_value_h);

#[must_use]
pub struct SelectedValue<'hive> {
	pub(crate) hive: BorrowedHive<'hive>,
	pub(crate) handle: ValueHandle,
}

impl<'hive> SelectedValue<'hive> {
	/// Get registry hive from which this value is selected
	pub fn hive(&self) -> BorrowedHive<'hive> {
		self.hive.clone()
	}

	/// Get a handle used for selecting in registry by in this value
	pub const fn handle(&self) -> ValueHandle {
		self.handle
	}

	/// Return the key of a [`Value`]
	///
	/// Note that this function can return a zero-length string. In the context
	/// of Windows Registries, this means that this value is the default key for
	/// this node in the tree. This is usually written as `"@"`.
	pub fn key(&self) -> std::io::Result<LibCBox<str>> {
		unsafe {
			let raw_ptr = self.hive.as_handle();
			let data = sys::hivex_value_key(raw_ptr, self.handle.0);
			check_pointer_null(data)?;

			let len = sys::hivex_value_key_len(raw_ptr, self.handle.0);
			Ok(boxed_str_from_ffi(data, len))
		}
	}

	/// Return the lenght and data type of [`Value`]
	///
	/// You can use [`Self::get`] if you know the type in advance.
	pub fn type_of(&self) -> Option<ValueTypeRet> {
		let mut ffi_type = 0;
		let mut len = 0;
		let result = unsafe {
			sys::hivex_value_type(
				self.hive.as_handle(),
				self.handle.0,
				&mut ffi_type,
				&mut len,
			)
		};

		let ty = ValueType::from_ffi(ffi_type)?;
		(result == 0).then_some(ValueTypeRet { ty, len })
	}

	/// Return [`RawValue`] of [`Value`]. The byte slice should be
	/// interpreted according to its type.
	pub fn raw(&self) -> std::io::Result<RawValue> {
		let mut ffi_type = 0;
		let mut len = 0;

		let data = unsafe {
			sys::hivex_value_value(
				self.hive.as_handle(),
				self.handle.0,
				&mut ffi_type,
				&mut len,
			)
		};

		check_pointer_null(data)?;

		let ty =
			ValueType::from_ffi(ffi_type).ok_or_else(|| std::io::Error::other("Invalid type"))?;

		let bytes = unsafe { boxed_slice_from_ffi(data.cast(), len) };

		Ok(RawValue { ty, data: bytes })
	}

	/// Get a [`Value`] by its handle.
	pub fn get(&self) -> std::io::Result<Value<LibCBox<str>>> {
		let mut ffi_ty = 0;
		let mut len = 0;
		let status = unsafe {
			sys::hivex_value_type(self.hive.as_handle(), self.handle.0, &mut ffi_ty, &mut len)
		};

		check_status_zero(status)?;

		let ty = ValueType::from_ffi(ffi_ty).expect("Expected valid type");

		let value = match ty {
			ValueType::None => Value::None,
			ValueType::Sz | ValueType::ExpandSz | ValueType::Link => {
				Value::Sz(self.downcast_string()?)
			}
			ValueType::MultiSz => Value::MultiSz(self.downcast_multiple_strings()),
			ValueType::Binary
			| ValueType::ResourceList
			| ValueType::FullResourceDescriptor
			| ValueType::ResourceRequirementsList => Value::Binary(self.downcast_binary()?),
			ValueType::Dword | ValueType::DwordBe => Value::Dword(self.downcast_dword()),
			ValueType::Qword => Value::Qword(self.downcast_qword()),
		};

		Ok(value)
	}

	/// Expecting string-like value, extract it or error out
	fn downcast_string(&self) -> std::io::Result<LibCBox<str>> {
		unsafe {
			let data = sys::hivex_value_string(self.hive.as_handle(), self.handle.0);
			check_pointer_null(data)?;
			let c_str_len = CStr::from_ptr(data).to_bytes().len();
			Ok(boxed_str_from_ffi(data, c_str_len))
		}
	}

	/// Expected binary value, extract it or error out
	fn downcast_binary(&self) -> std::io::Result<LibCBox<[u8]>> {
		let mut _ffi_ty = 0;
		let mut len = 0;

		unsafe {
			let data = sys::hivex_value_value(
				self.hive.as_handle(),
				self.handle.0,
				&mut _ffi_ty,
				&mut len,
			);
			check_pointer_null(data)?;
			Ok(boxed_slice_from_ffi(data.cast(), len))
		}
	}

	/// Expected multiple strings value, extract it or error out
	fn downcast_multiple_strings(&self) -> Box<[LibCBox<str>]> {
		let mut strings = vec![];

		unsafe {
			let mut array_of_strings =
				sys::hivex_value_multiple_strings(self.hive.as_handle(), self.handle.0);

			loop {
				let str_ptr = array_of_strings.read();
				assert!(!str_ptr.is_null(), "There should be no null pointer");

				let len = CStr::from_ptr(str_ptr).to_bytes().len();

				// Terminated by empty string
				if len == 0 {
					break;
				}

				let str = boxed_str_from_ffi(str_ptr, len);
				strings.push(str);
				array_of_strings = array_of_strings.add(1);
			}
		}

		strings.into_boxed_slice()
	}

	/// Expected LE or BE DWORD int, extract and convert or error out
	pub fn downcast_dword(&self) -> u32 {
		unsafe { sys::hivex_value_dword(self.hive.as_handle(), self.handle.0) as u32 }
	}

	/// Expected QWORD int, extract or error out
	pub fn downcast_qword(&self) -> u64 {
		unsafe { sys::hivex_value_qword(self.hive.as_handle(), self.handle.0) as u64 }
	}
}

/// [`SelectedValue::type_of`] return structure
///
/// Contains value type and underlying raw data size
#[derive(Clone, Copy, Debug)]
pub struct ValueTypeRet {
	/// Value type
	pub ty: ValueType,
	/// Lenght of raw data
	pub len: usize,
}

/// [`SelectedValue::raw`] return structure
pub struct RawValue {
	/// Value type
	pub ty: ValueType,
	/// Underlying value raw data
	pub data: LibCBox<[u8]>,
}

/// Value type tag
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
#[non_exhaustive]
pub enum ValueType {
	/// No defined value type
	None = sys::hive_type_hive_t_REG_NONE as _,
	/// A Windows string (encoding is unknown, but often UTF16-LE)
	Sz = sys::hive_type_hive_t_REG_SZ as _,
	/// A Windows string that contains %env% (environment variable expansion)
	ExpandSz = sys::hive_type_hive_t_REG_EXPAND_SZ as _,
	/// A blob of binary
	Binary = sys::hive_type_hive_t_REG_BINARY as _,
	/// DWORD (32 bit integer), little endian
	Dword = sys::hive_type_hive_t_REG_DWORD as _,
	/// DWORD (32 bit integer), big endian
	DwordBe = sys::hive_type_hive_t_REG_DWORD_BIG_ENDIAN as _,
	/// Symbolic link to another part of the registry tree
	Link = sys::hive_type_hive_t_REG_LINK as _,
	/// Multiple Windows strings
	MultiSz = sys::hive_type_hive_t_REG_MULTI_SZ as _,
	/// Resource list
	ResourceList = sys::hive_type_hive_t_REG_RESOURCE_LIST as _,
	/// Resource descriptor
	FullResourceDescriptor = sys::hive_type_hive_t_REG_FULL_RESOURCE_DESCRIPTOR as _,
	/// Resouce requirements list
	ResourceRequirementsList = sys::hive_type_hive_t_REG_RESOURCE_REQUIREMENTS_LIST as _,
	/// QWORD (64 bit integer), unspecified endianness but usually little endian
	Qword = sys::hive_type_hive_t_REG_QWORD as _,
}

impl ValueType {
	pub(crate) fn from_ffi(n: sys::hive_type) -> Option<Self> {
		(n <= Self::Qword as sys::hive_type).then(|| unsafe { std::mem::transmute(n as u8) })
	}
}

/// Converted registry value
///
/// # Data representation note
/// Windows RegEdit itself shows [`Value::ResourceList`],
/// [`Value::FullResourceDescriptor`] and [`Value::ResourceRequirementsList`] as
/// plain bytes. I may try to reverse-engineer some inner structure and then
/// support that. Until then, enjoy your bytes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Value<Str> {
	/// No defined value type
	None,
	/// A string
	Sz(Str),
	/// A string that contains %env% (environment variable expansion)
	ExpandSz(Str),
	/// Blob of bytes
	Binary(LibCBox<[u8]>),
	/// 32 bit LE integer
	Dword(u32),
	/// 32 bit BE integer (should not appear in returned Value)
	DwordBe(u32),
	/// Symbolic link to another part of the registry tree
	Link(Str),
	/// Multiple strings
	MultiSz(Box<[Str]>),
	/// Resource list
	ResourceList(LibCBox<[u8]>),
	/// Resource descriptor
	FullResourceDescriptor(LibCBox<[u8]>),
	/// Resource requirements list
	ResourceRequirementsList(LibCBox<[u8]>),
	/// 64 bit integer
	Qword(u64),
}

impl<Str> From<u32> for Value<Str> {
	fn from(val: u32) -> Self {
		Self::Dword(val)
	}
}

impl<Str> From<u64> for Value<Str> {
	fn from(value: u64) -> Self {
		Self::Qword(value)
	}
}

impl<Str: ValueString> From<Str> for Value<Str> {
	fn from(value: Str) -> Self {
		Self::Sz(value)
	}
}

impl<Str> Value<Str> {
	/// Get a type of value
	pub const fn type_of(&self) -> ValueType {
		match self {
			Value::None => ValueType::None,
			Value::Sz(_) => ValueType::Sz,
			Value::ExpandSz(_) => ValueType::ExpandSz,
			Value::Binary(_) => ValueType::Binary,
			Value::Dword(_) => ValueType::Dword,
			Value::DwordBe(_) => ValueType::DwordBe,
			Value::Link(_) => ValueType::Link,
			Value::MultiSz(_) => ValueType::MultiSz,
			Value::ResourceList(_) => ValueType::ResourceList,
			Value::FullResourceDescriptor(_) => ValueType::FullResourceDescriptor,
			Value::ResourceRequirementsList(_) => ValueType::ResourceRequirementsList,
			Value::Qword(_) => ValueType::Qword,
		}
	}
}

/// A trait to be used for user-provided string values
pub trait ValueString {
	/// Make the string into a nul-terminated one
	///
	/// If it already is NUL-terminated, no need to
	/// copy anything, so that's why [`Cow`] is used.
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a;

	/// Get string lenght without NUL terminator
	fn c_strlen(&self) -> usize;

	/// Convert string to UTF-16 encoded byte sequence
	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error>;
}

impl ValueString for &str {
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a,
	{
		ValueString::into_c_string(self.as_bytes())
	}

	fn c_strlen(&self) -> usize {
		ValueString::c_strlen(&self.as_bytes())
	}

	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error> {
		Ok(self.encode_utf16().collect())
	}
}

impl ValueString for &[u8] {
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a,
	{
		if let Ok(x) = CStr::from_bytes_until_nul(self) {
			Cow::Borrowed(x)
		} else {
			let cstring = CString::new(self);
			match cstring {
  					Ok(cstring) => Cow::Owned(cstring),
  					Err(_) => unreachable!("This branch exists solely for converting non-NUL terminated string. NUL found."),
  				}
		}
	}

	fn c_strlen(&self) -> usize {
		match CStr::from_bytes_until_nul(self) {
			Ok(x) => x.to_bytes().len(),
			Err(_) => self.len(),
		}
	}

	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error> {
		let utf8 = std::str::from_utf8(self)?;
		Ok(utf8.encode_utf16().collect())
	}
}

impl ValueString for &CStr {
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a,
	{
		Cow::Borrowed(self)
	}

	fn c_strlen(&self) -> usize {
		self.to_bytes().len()
	}

	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error> {
		ValueString::to_utf16(&self.to_bytes())
	}
}

impl ValueString for CString {
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a,
	{
		Cow::Owned(self)
	}

	fn c_strlen(&self) -> usize {
		self.as_bytes().len()
	}

	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error> {
		// Reuse implementation for &CStr
		self.as_c_str().to_utf16()
	}
}

impl ValueString for Cow<'_, CStr> {
	fn into_c_string<'a>(self) -> Cow<'a, CStr>
	where
		Self: 'a,
	{
		self
	}

	fn c_strlen(&self) -> usize {
		self.to_bytes().c_strlen()
	}

	fn to_utf16(&self) -> Result<Box<[u16]>, Utf8Error> {
		self.to_bytes().to_utf16()
	}
}