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
// Copyright 2020-2021 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use crate::error::{NtHiveError, Result};
use crate::helpers::byte_subrange;
use crate::hive::Hive;
use crate::index_root::IndexRootItemRange;
use crate::key_node::KeyNode;
use crate::subkeys_list::SubkeysList;
use ::byteorder::LittleEndian;
use core::iter::FusedIterator;
use core::mem;
use core::ops::{Deref, Range};
use zerocopy::*;

/// On-Disk Structure of a Fast Leaf item (On-Disk Signature: `lf`).
/// They are supported since Windows NT 4.
#[allow(dead_code)]
#[derive(AsBytes, FromBytes, Unaligned)]
#[repr(packed)]
struct FastLeafItem {
    key_node_offset: U32<LittleEndian>,
    name_hint: [u8; 4],
}

/// On-Disk Structure of a Hash Leaf item (On-Disk Signature: `lh`).
/// They are supported since Windows XP.
#[allow(dead_code)]
#[derive(AsBytes, FromBytes, Unaligned)]
#[repr(packed)]
struct HashLeafItem {
    key_node_offset: U32<LittleEndian>,
    name_hash: [u8; 4],
}

/// On-Disk Structure of an Index Leaf item (On-Disk Signature: `li`).
/// They are supported in all Windows versions.
#[derive(AsBytes, FromBytes, Unaligned)]
#[repr(packed)]
struct IndexLeafItem {
    key_node_offset: U32<LittleEndian>,
}

/// All known and supported Leaf types.
///
/// We first had only Index Leafs, then got Fast Leafs with Windows NT 4 which add a
/// `name_hint` (first 4 characters of the key name), and finally got Hash Leafs with
/// Windows XP which come with a `name_hash` (simple hash of the entire key name)
/// instead.
/// Both Fast Leafs and Hash Leafs were introduced to speed up key lookups.
/// However, their performance benefits are marginal to non-existing in 2020
/// when we assume that the entire registry hive is randomly accessible.
/// Therefore, the nt-hive crate treats all types equally by only accessing the
/// `key_node_offset` field and ignoring all other fields.
#[derive(Clone, Copy)]
pub(crate) enum LeafType {
    Fast,
    Hash,
    Index,
}

impl LeafType {
    pub(crate) fn from_signature(signature: &[u8]) -> Option<Self> {
        match signature {
            b"lf" => Some(Self::Fast),
            b"lh" => Some(Self::Hash),
            b"li" => Some(Self::Index),
            _ => None,
        }
    }

    fn item_size(&self) -> usize {
        match self {
            Self::Fast => mem::size_of::<FastLeafItem>(),
            Self::Hash => mem::size_of::<HashLeafItem>(),
            Self::Index => mem::size_of::<IndexLeafItem>(),
        }
    }
}

/// Byte range of a single Leaf item returned by [`LeafItemRanges`].
pub(crate) struct LeafItemRange(Range<usize>);

impl LeafItemRange {
    pub fn key_node_offset<B>(&self, hive: &Hive<B>) -> u32
    where
        B: ByteSlice,
    {
        // We make use of the fact that a `FastLeafItem` or `HashLeafItem` is just an
        // `IndexLeafItem` with additional fields.
        // As they all have the `key_node_offset` as their first field, treat them equally.
        let (index_leaf_item, _) =
            LayoutVerified::<&[u8], IndexLeafItem>::new_from_prefix(&hive.data[self.0.clone()])
                .unwrap();
        index_leaf_item.key_node_offset.get()
    }
}

impl Deref for LeafItemRange {
    type Target = Range<usize>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Iterator over
///   a contiguous range of data bytes containing Leaf items of any type (Fast/Hash/Index),
///   returning a [`LeafItemRange`] for each Leaf item.
///
/// On-Disk Signatures: `lf`, `lh`, `li`
#[derive(Clone)]
pub(crate) struct LeafItemRanges {
    items_range: Range<usize>,
    leaf_type: LeafType,
}

impl LeafItemRanges {
    pub fn new(
        count: u16,
        count_field_offset: usize,
        data_range: Range<usize>,
        leaf_type: LeafType,
    ) -> Result<Self> {
        let byte_count = count as usize * leaf_type.item_size();

        let items_range = byte_subrange(&data_range, byte_count).ok_or_else(|| {
            NtHiveError::InvalidSizeField {
                offset: count_field_offset,
                expected: byte_count,
                actual: data_range.len(),
            }
        })?;

        Ok(Self {
            items_range,
            leaf_type,
        })
    }

    pub fn from_index_root_item_range<B>(
        hive: &Hive<B>,
        index_root_item_range: IndexRootItemRange,
    ) -> Result<Self>
    where
        B: ByteSlice,
    {
        let subkeys_list_offset = index_root_item_range.subkeys_list_offset(hive);
        let cell_range = hive.cell_range_from_data_offset(subkeys_list_offset)?;
        let subkeys_list = SubkeysList::new_without_index_root(hive, cell_range)?;

        let header = subkeys_list.header();
        let count = header.count.get();
        let count_field_offset = hive.offset_of_field(&header.count);

        // Subkeys Lists belonging to Index Root items need to contain at least 1 item.
        // Otherwise, we can't perform efficient binary search on them, which is the sole reason
        // Index Roots exist.
        if count == 0 {
            return Err(NtHiveError::InvalidSizeField {
                offset: count_field_offset,
                expected: 1,
                actual: 0,
            });
        }

        let leaf_type = LeafType::from_signature(&header.signature).unwrap();
        LeafItemRanges::new(
            count,
            count_field_offset,
            subkeys_list.data_range,
            leaf_type,
        )
    }
}

impl Iterator for LeafItemRanges {
    type Item = LeafItemRange;

    fn next(&mut self) -> Option<Self::Item> {
        let item_size = self.leaf_type.item_size();
        let item_range = byte_subrange(&self.items_range, item_size)?;
        self.items_range.start += item_size;

        Some(LeafItemRange(item_range))
    }

    fn count(self) -> usize {
        let (size, _) = self.size_hint();
        size
    }

    fn last(mut self) -> Option<Self::Item> {
        let (size, _) = self.size_hint();
        if size == 0 {
            return None;
        }

        self.nth(size - 1)
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        // `n` is arbitrary and usize, so we may hit boundaries here. Check that!
        let bytes_to_skip = n.checked_mul(self.leaf_type.item_size())?;
        self.items_range.start = self.items_range.start.checked_add(bytes_to_skip)?;
        self.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.items_range.len() / self.leaf_type.item_size();
        (size, Some(size))
    }
}

impl<B: ByteSlice> From<LeafKeyNodes<'_, B>> for LeafItemRanges {
    fn from(leaf_key_nodes: LeafKeyNodes<'_, B>) -> LeafItemRanges {
        leaf_key_nodes.leaf_item_ranges
    }
}

impl ExactSizeIterator for LeafItemRanges {}
impl FusedIterator for LeafItemRanges {}

/// Iterator over
///   a contiguous range of data bytes containing Leaf items of any type (Fast/Hash/Index),
///   returning a constant [`KeyNode`] for each Leaf item,
///   used by [`SubKeyNodes`].
///
/// On-Disk Signatures: `lf`, `lh`, `li`
///
/// [`SubKeyNodes`]: crate::subkeys_list::SubKeyNodes
#[derive(Clone)]
pub struct LeafKeyNodes<'a, B: ByteSlice> {
    hive: &'a Hive<B>,
    leaf_item_ranges: LeafItemRanges,
}

impl<'a, B> LeafKeyNodes<'a, B>
where
    B: ByteSlice,
{
    pub(crate) fn new(
        hive: &'a Hive<B>,
        count: u16,
        count_field_offset: usize,
        data_range: Range<usize>,
        leaf_type: LeafType,
    ) -> Result<Self> {
        let leaf_item_ranges =
            LeafItemRanges::new(count, count_field_offset, data_range, leaf_type)?;

        Ok(Self {
            hive,
            leaf_item_ranges,
        })
    }
}

impl<'a, B> Iterator for LeafKeyNodes<'a, B>
where
    B: ByteSlice,
{
    type Item = Result<KeyNode<&'a Hive<B>, B>>;

    fn next(&mut self) -> Option<Self::Item> {
        let leaf_item_range = self.leaf_item_ranges.next()?;
        let key_node = iter_try!(KeyNode::from_leaf_item_range(self.hive, leaf_item_range));
        Some(Ok(key_node))
    }

    fn count(self) -> usize {
        self.leaf_item_ranges.count()
    }

    fn last(mut self) -> Option<Self::Item> {
        let (size, _) = self.size_hint();
        if size == 0 {
            return None;
        }

        self.nth(size - 1)
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        // `n` is arbitrary and usize, so we may hit boundaries here. Check that!
        let bytes_to_skip = n.checked_mul(self.leaf_item_ranges.leaf_type.item_size())?;
        self.leaf_item_ranges.items_range.start = self
            .leaf_item_ranges
            .items_range
            .start
            .checked_add(bytes_to_skip)?;
        self.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.leaf_item_ranges.size_hint()
    }
}

impl<'a, B> ExactSizeIterator for LeafKeyNodes<'a, B> where B: ByteSlice {}
impl<'a, B> FusedIterator for LeafKeyNodes<'a, B> where B: ByteSlice {}

/// Iterator over
///   a contiguous range of data bytes containing Leaf items of any type (Fast/Hash/Index),
///   returning a mutable [`KeyNode`] for each Leaf item,
///   used by [`SubKeyNodesMut`].
///
/// On-Disk Signatures: `lf`, `lh`, `li`
///
/// [`SubKeyNodesMut`]: crate::subkeys_list::SubKeyNodesMut
pub(crate) struct LeafKeyNodesMut<'a, B: ByteSliceMut> {
    hive: &'a mut Hive<B>,
    leaf_item_ranges: LeafItemRanges,
}

impl<'a, B> LeafKeyNodesMut<'a, B>
where
    B: ByteSliceMut,
{
    pub(crate) fn new(
        hive: &'a mut Hive<B>,
        count: u16,
        count_field_offset: usize,
        data_range: Range<usize>,
        leaf_type: LeafType,
    ) -> Result<Self> {
        let leaf_item_ranges =
            LeafItemRanges::new(count, count_field_offset, data_range, leaf_type)?;

        Ok(Self {
            hive,
            leaf_item_ranges,
        })
    }

    pub(crate) fn next(&mut self) -> Option<Result<KeyNode<&mut Hive<B>, B>>> {
        let leaf_item_range = self.leaf_item_ranges.next()?;
        let key_node = iter_try!(KeyNode::from_leaf_item_range(
            &mut *self.hive,
            leaf_item_range
        ));
        Some(Ok(key_node))
    }
}