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
//! [LinkInfo](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-shllink/6813269d-0cc8-4be2-933f-e96e8e3412dc) related structs

mod common_network_relative_link;
mod volume_id;
use byteorder::{LittleEndian, ReadBytesExt};
pub use common_network_relative_link::CommonNetworkRelativeLink;
use getset::Getters;
use serde::{Serialize, Serializer};
use winparsingtools::ReaderError;
use std::fmt::{self, Display};
use std::io::{Cursor, Read, Seek, SeekFrom};
pub use volume_id::VolumeID;
use winparsingtools::{traits::Path, utils};

/// The LinkInfo structure specifies information necessary to resolve a link target if it is not found in its original location.
#[derive(Debug, Serialize, Getters)]
#[getset(get = "pub with_prefix")]
pub struct LinkInfo {
    #[serde(skip_serializing)]
    pub size: u32,
    #[serde(skip_serializing)]
    pub header_size: u32,
    #[serde(skip_serializing)]
    pub flags: LinkInfoFlags,
    #[serde(skip_serializing)]
    pub volume_id_offset: u32,
    #[serde(skip_serializing)]
    pub local_base_path_offset: u32,
    #[serde(skip_serializing)]
    pub common_network_relative_link_offset: u32,
    #[serde(skip_serializing)]
    pub common_path_suffix_offset: u32,
    #[serde(skip_serializing)]
    pub local_base_path_offset_unicode: Option<u32>,
    #[serde(skip_serializing)]
    pub common_path_suffix_offset_unicode: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub volume_id: Option<VolumeID>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub local_base_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub common_network_relative_link: Option<CommonNetworkRelativeLink>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub common_path_suffix: Option<String>,
}

impl LinkInfo {
    pub fn from_buffer(buf: &[u8]) -> Result<Self, ReaderError> {
        Self::from_reader(&mut Cursor::new(buf))
    }

    pub fn from_reader<R: Read + Seek>(r: &mut R) -> Result<Self, ReaderError> {
        let size = r.read_u32::<LittleEndian>()?;
        let mut link_info_data = vec![0; (size - 4) as usize];
        r.read_exact(&mut link_info_data)?;
        let r = &mut Cursor::new(link_info_data);
        let header_size = r.read_u32::<LittleEndian>()?;
        let flags = LinkInfoFlags::from_u32(r.read_u32::<LittleEndian>()?)?;
        let volume_id_offset = r.read_u32::<LittleEndian>()?;
        let local_base_path_offset = r.read_u32::<LittleEndian>()?;
        let common_network_relative_link_offset = r.read_u32::<LittleEndian>()?;
        let common_path_suffix_offset = r.read_u32::<LittleEndian>()?;

        let mut local_base_path_offset_unicode = None;
        let mut common_path_suffix_offset_unicode = None;

        // Only available if the header size is greater than or equal to 0x24
        if header_size >= 0x24 {
            local_base_path_offset_unicode = Some(r.read_u32::<LittleEndian>()?);
            common_path_suffix_offset_unicode = Some(r.read_u32::<LittleEndian>()?);
        }

        let mut volume_id = None;
        let mut common_network_relative_link = None;

        if flags.VolumeIDAndLocalBasePath {
            r.seek(SeekFrom::Start((volume_id_offset - 4) as u64))?;
            volume_id = Some(VolumeID::from_reader(r)?);
        }

        if flags.CommonNetworkRelativeLinkAndPathSuffix {
            r.seek(SeekFrom::Start(
                (common_network_relative_link_offset - 4) as u64,
            ))?;
            common_network_relative_link = Some(CommonNetworkRelativeLink::from_reader(r)?);
        }

        // Read unicode local_base_path if available, else read normal local_base_path
        let local_base_path = match local_base_path_offset_unicode {
            Some(offset) => match offset {
                0 => None,
                _ => {
                    r.seek(SeekFrom::Start((offset - 4) as u64))?;
                    match utils::read_utf16_string(r, None)? {
                        s if !s.is_empty() => Some(s),
                        _ => None,
                    }
                }
            },
            None => match local_base_path_offset {
                0 => None,
                offset => {
                    r.seek(SeekFrom::Start((offset - 4) as u64))?;
                    match utils::read_cp1252_string(r, None)? {
                        s if !s.is_empty() => Some(s),
                        _ => None,
                    }
                }
            },
        };

        let common_path_suffix = match common_path_suffix_offset_unicode {
            Some(offset) => match offset {
                0 => None,
                _ => {
                    r.seek(SeekFrom::Start((offset - 4) as u64))?;
                    match utils::read_utf16_string(r, None)? {
                        s if !s.is_empty() => Some(s),
                        _ => None,
                    }
                }
            },
            None => match common_path_suffix_offset {
                0 => None,
                offset => {
                    r.seek(SeekFrom::Start((offset - 4) as u64))?;
                    match utils::read_cp1252_string(r, None)? {
                        s if !s.is_empty() => Some(s),
                        _ => None,
                    }
                }
            },
        };

        Ok(LinkInfo {
            size,
            header_size,
            flags,
            volume_id_offset,
            local_base_path_offset,
            common_network_relative_link_offset,
            common_path_suffix_offset,
            local_base_path_offset_unicode,
            common_path_suffix_offset_unicode,
            volume_id,
            local_base_path,
            common_network_relative_link,
            common_path_suffix,
        })
    }
}

impl Path for LinkInfo {
    fn path(&self) -> Option<String> {
        let path = match &self.local_base_path {
            Some(local_base_path) => match &self.common_path_suffix {
                // if `common_path_suffix` and `local_base_path` are present then return {local_base_path}\{common_path_suffix}
                Some(common_path_suffix) => Some(
                    format!("{}\\{}", local_base_path, common_path_suffix).replace("\\\\", "\\"),
                ),
                None => Some(local_base_path.to_string().replace("\\\\", "\\")),
            },
            None => None,
        };

        match path {
            Some(p) => Some(p),
            None => match &self.common_network_relative_link {
                Some(common_network_relative_link) => common_network_relative_link.path(),
                None => None,
            },
        }
    }
}

/// Flags that specify whether the VolumeID, LocalBasePath, LocalBasePathUnicode, and CommonNetworkRelativeLink fields are present in this structure.
#[derive(Debug, Getters)]
#[getset(get = "pub with_prefix")]
pub struct LinkInfoFlags {
    VolumeIDAndLocalBasePath: bool,
    CommonNetworkRelativeLinkAndPathSuffix: bool,
}

impl LinkInfoFlags {
    pub fn new(
        VolumeIDAndLocalBasePath: bool,
        CommonNetworkRelativeLinkAndPathSuffix: bool,
    ) -> Result<LinkInfoFlags, ReaderError> {
        Ok(LinkInfoFlags {
            VolumeIDAndLocalBasePath,
            CommonNetworkRelativeLinkAndPathSuffix,
        })
    }

    pub fn from_u32(flags: u32) -> Result<LinkInfoFlags, ReaderError> {
        Ok(LinkInfoFlags {
            VolumeIDAndLocalBasePath: (flags & 0x01 != 0),
            CommonNetworkRelativeLinkAndPathSuffix: (flags & 0x02 != 0),
        })
    }
}

impl Display for LinkInfoFlags {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut result = vec![];
        if self.VolumeIDAndLocalBasePath {
            result.push("VolumeIDAndLocalBasePath");
        }
        if self.CommonNetworkRelativeLinkAndPathSuffix {
            result.push("CommonNetworkRelativeLinkAndPathSuffix");
        }
        write!(f, "{}", result.join(","))
    }
}

impl Serialize for LinkInfoFlags {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_some(&self.to_string().split(',').collect::<Vec<&str>>())
    }
}