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
use crate::codec::{Codec, StdError, WithOffset, WithSize};
#[cfg(test)]
use crate::test_tools::test_item;
#[cfg(test)]
use hex_literal::hex;

/// Permissions of a given user regarding a specific file.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UserPermissions {
    pub read: bool,
    pub write: bool,
    pub run: bool,
}
impl std::fmt::Display for UserPermissions {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}{}{}",
            if self.read { "R" } else { "-" },
            if self.write { "W" } else { "-" },
            if self.run { "X" } else { "-" }
        )
    }
}
/// Description of the permissions for a file for all users.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Permissions {
    /// Whether data element is encrypted
    /// WARNING: This meaning might be deprecated
    pub encrypted: bool,
    /// Whether data element is executable
    /// WARNING: This meaning might be deprecated
    pub executable: bool,
    // ALP_SPEC: Why can't we set {read, write, run} level permission encoded on 2 bit instead?
    // Because allowing guest but not user makes no sense.
    /// Permissions for role "user"
    pub user: UserPermissions,
    /// Permissions for role "guest"
    pub guest: UserPermissions,
    // ALP_SPEC: Where are the permissions for role root?
}
impl std::fmt::Display for Permissions {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}{}|user={}|guest={}",
            if self.encrypted { "E" } else { "-" },
            if self.executable { "X" } else { "-" },
            self.user,
            self.guest
        )
    }
}
impl Permissions {
    pub fn to_byte(self) -> u8 {
        let mut ret = 0;
        ret |= (self.encrypted as u8) << 7;
        ret |= (self.executable as u8) << 6;
        ret |= (self.user.read as u8) << 5;
        ret |= (self.user.write as u8) << 4;
        ret |= (self.user.run as u8) << 3;
        ret |= (self.guest.read as u8) << 2;
        ret |= (self.guest.write as u8) << 1;
        ret |= self.guest.run as u8;
        ret
    }
    pub fn from_byte(n: u8) -> Self {
        Self {
            encrypted: n & 0x80 != 0,
            executable: n & 0x40 != 0,
            user: UserPermissions {
                read: n & 0x20 != 0,
                write: n & 0x10 != 0,
                run: n & 0x08 != 0,
            },
            guest: UserPermissions {
                read: n & 0x04 != 0,
                write: n & 0x02 != 0,
                run: n & 0x01 != 0,
            },
        }
    }
}
/// File access type event that will trigger an ALP action.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ActionCondition {
    /// Check for existence
    /// (L)
    List = 0,
    /// Trigger upon file read
    /// (R)
    Read = 1,
    /// Trigger upon file write
    /// (W)
    Write = 2,
    /// Trigger upon file write-flush
    /// (V)
    // ALP_SPEC Action write-flush does not exist. Only write and flush exist.
    WriteFlush = 3,
    Unknown4 = 4,
    Unknown5 = 5,
    Unknown6 = 6,
    Unknown7 = 7,
}
impl ActionCondition {
    fn from(n: u8) -> Self {
        match n {
            0 => ActionCondition::List,
            1 => ActionCondition::Read,
            2 => ActionCondition::Write,
            3 => ActionCondition::WriteFlush,
            4 => ActionCondition::Unknown4,
            5 => ActionCondition::Unknown5,
            6 => ActionCondition::Unknown6,
            7 => ActionCondition::Unknown7,
            // Impossible
            _ => panic!(),
        }
    }
}
impl std::fmt::Display for ActionCondition {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::List => write!(f, "L"),
            Self::Read => write!(f, "R"),
            Self::Write => write!(f, "W"),
            Self::WriteFlush => write!(f, "V"),
            x => write!(f, "{}", *x as u8),
        }
    }
}
/// Type of storage
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StorageClass {
    /// The content is not kept in memory. It cannot be read back.
    Transient = 0,
    /// The content is kept in a volatile memory of the device. It is accessible for
    /// read, and is lost on power off.
    Volatile = 1,
    /// The content is kept in a volatile memory of the device. It is accessible for
    /// read, and can be backed-up upon request in a permanent storage
    /// location. It is restored from the permanent location on device power on.
    Restorable = 2,
    /// The content is kept in a permanent memory of the device. It is accessible
    /// for read and write.
    Permanent = 3,
}
impl StorageClass {
    fn from(n: u8) -> Self {
        match n {
            0 => StorageClass::Transient,
            1 => StorageClass::Volatile,
            2 => StorageClass::Restorable,
            3 => StorageClass::Permanent,
            _ => panic!(),
        }
    }
}
impl std::fmt::Display for StorageClass {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Transient => "T",
                Self::Volatile => "V",
                Self::Restorable => "R",
                Self::Permanent => "P",
            }
        )
    }
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FileProperties {
    /// Enables the D7AActP (ALP action to trigger upon some type of access to this file)
    pub act_en: bool,
    /// Type of access needed to trigger the D7AActP
    pub act_cond: ActionCondition,
    /// Type of storage of this file
    pub storage_class: StorageClass,
}
impl std::fmt::Display for FileProperties {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}{}{}",
            self.act_en as u8, self.act_cond, self.storage_class
        )
    }
}
impl FileProperties {
    pub fn to_byte(self) -> u8 {
        let mut ret = 0;
        ret |= (self.act_en as u8) << 7;
        ret |= (self.act_cond as u8) << 4;
        ret |= self.storage_class as u8;
        ret
    }
    pub fn from_byte(n: u8) -> Self {
        Self {
            act_en: n & 0x80 != 0,
            act_cond: ActionCondition::from((n >> 4) & 0x7),
            storage_class: StorageClass::from(n & 0x03),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FileHeader {
    /// Permissions of the file
    pub permissions: Permissions,
    /// Properties of the file
    pub properties: FileProperties,
    /// Index of the File containing the ALP Command, executed
    /// by D7AActP. Discarded if the ACT_EN field in Properties
    /// is set to 0.
    pub alp_cmd_fid: u8,
    /// Index of the File containing the Interface, on which the
    /// result of D7AActP is sent. Discarded if the ACT_EN field
    /// in Properties is set to 0.
    pub interface_file_id: u8,
    /// Current size of the file.
    pub file_size: u32,
    /// Size, allocated for the file in memory (appending data to
    /// the file cannot exceed this value)
    pub allocated_size: u32,
    // ALP_SPEC What is the difference between file_size and allocated_size? When a file is
    // declared, less than its size is allocated and then it grows dynamically?
}
impl std::fmt::Display for FileHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "[{}|{}|f({}),{},{},{}]",
            self.permissions,
            self.properties,
            self.alp_cmd_fid,
            self.interface_file_id,
            self.file_size,
            self.allocated_size
        )
    }
}
impl Codec for FileHeader {
    type Error = StdError;
    fn encoded_size(&self) -> usize {
        12
    }
    unsafe fn encode_in(&self, out: &mut [u8]) -> usize {
        out[0] = self.permissions.to_byte();
        out[1] = self.properties.to_byte();
        out[2] = self.alp_cmd_fid;
        out[3] = self.interface_file_id;
        out[4..4 + 4].clone_from_slice(&self.file_size.to_be_bytes());
        out[8..8 + 4].clone_from_slice(&self.allocated_size.to_be_bytes());
        12
    }
    fn decode(out: &[u8]) -> Result<WithSize<Self>, WithOffset<Self::Error>> {
        if out.len() < 12 {
            return Err(WithOffset::new_head(Self::Error::MissingBytes(
                12 - out.len(),
            )));
        }
        let mut file_size_bytes = [0u8; 4];
        file_size_bytes.clone_from_slice(&out[4..4 + 4]);
        let mut allocated_size_bytes = [0u8; 4];
        allocated_size_bytes.clone_from_slice(&out[8..8 + 4]);
        Ok(WithSize {
            value: Self {
                permissions: Permissions::from_byte(out[0]),
                properties: FileProperties::from_byte(out[1]),
                alp_cmd_fid: out[2],
                interface_file_id: out[3],
                file_size: u32::from_be_bytes(file_size_bytes),
                allocated_size: u32::from_be_bytes(allocated_size_bytes),
            },
            size: 12,
        })
    }
}
#[test]
fn test_file_header() {
    test_item(
        FileHeader {
            permissions: Permissions {
                encrypted: true,
                executable: false,
                user: UserPermissions {
                    read: true,
                    write: true,
                    run: true,
                },
                guest: UserPermissions {
                    read: false,
                    write: false,
                    run: false,
                },
            },
            properties: FileProperties {
                act_en: false,
                act_cond: ActionCondition::Read,
                storage_class: StorageClass::Permanent,
            },
            alp_cmd_fid: 1,
            interface_file_id: 2,
            file_size: 0xDEAD_BEEF,
            allocated_size: 0xBAAD_FACE,
        },
        &hex!("B8 13 01 02 DEADBEEF BAADFACE"),
    )
}