Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_id.rs « structured_values « src - github.com/windirstat/ntfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 292308a2933b7e1852367d6ec498089c393936b6 (plain)
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
// Copyright 2021 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use crate::attribute::NtfsAttributeType;
use crate::error::{NtfsError, Result};
use crate::guid::{NtfsGuid, GUID_SIZE};
use crate::structured_values::{NtfsStructuredValue, NtfsStructuredValueFromData};
use binread::io::Cursor;
use binread::BinReaderExt;

#[derive(Clone, Debug)]
pub struct NtfsObjectId {
    object_id: NtfsGuid,
    birth_volume_id: Option<NtfsGuid>,
    birth_object_id: Option<NtfsGuid>,
    domain_id: Option<NtfsGuid>,
}

impl NtfsObjectId {
    pub fn birth_object_id(&self) -> Option<&NtfsGuid> {
        self.birth_object_id.as_ref()
    }

    pub fn birth_volume_id(&self) -> Option<&NtfsGuid> {
        self.birth_volume_id.as_ref()
    }

    pub fn domain_id(&self) -> Option<&NtfsGuid> {
        self.domain_id.as_ref()
    }

    pub fn object_id(&self) -> &NtfsGuid {
        &self.object_id
    }
}

impl NtfsStructuredValue for NtfsObjectId {
    const TY: NtfsAttributeType = NtfsAttributeType::ObjectId;
}

impl<'d> NtfsStructuredValueFromData<'d> for NtfsObjectId {
    fn from_data(data: &'d [u8], position: u64) -> Result<Self> {
        if data.len() < GUID_SIZE {
            return Err(NtfsError::InvalidStructuredValueSize {
                position,
                ty: NtfsAttributeType::ObjectId,
                expected: GUID_SIZE,
                actual: data.len(),
            });
        }

        let mut cursor = Cursor::new(data);
        let object_id = cursor.read_le::<NtfsGuid>()?;

        let mut birth_volume_id = None;
        if data.len() >= 2 * GUID_SIZE {
            birth_volume_id = Some(cursor.read_le::<NtfsGuid>()?);
        }

        let mut birth_object_id = None;
        if data.len() >= 3 * GUID_SIZE {
            birth_object_id = Some(cursor.read_le::<NtfsGuid>()?);
        }

        let mut domain_id = None;
        if data.len() >= 4 * GUID_SIZE {
            domain_id = Some(cursor.read_le::<NtfsGuid>()?);
        }

        Ok(Self {
            object_id,
            birth_volume_id,
            birth_object_id,
            domain_id,
        })
    }
}