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
//! Metadata encoding for archive entries.
//!
//! This module provides functions for encoding file names, comments, and timestamps
//! in the 7z archive format.
use std::io::{Seek, Write};
use super::encoding_utils::encode_bool_vector;
use super::{PendingEntry, Writer};
impl<W: Write + Seek> Writer<W> {
/// Encodes file names as UTF-16LE.
///
/// Each name is followed by a null terminator (two zero bytes).
pub(crate) fn encode_names(&self) -> Vec<u8> {
let mut data = Vec::new();
for entry in &self.entries {
for c in entry.path.as_str().encode_utf16() {
data.extend_from_slice(&c.to_le_bytes());
}
// Null terminator
data.extend_from_slice(&[0, 0]);
}
data
}
/// Encodes an archive comment as UTF-16LE with external flag.
///
/// The comment is prefixed with an external flag (0 = inline) and
/// followed by a null terminator.
pub(crate) fn encode_comment(&self, comment: &str) -> Vec<u8> {
let mut data = Vec::new();
// External flag = 0 (inline data)
data.push(0);
// UTF-16LE encoded string
for c in comment.encode_utf16() {
data.extend_from_slice(&c.to_le_bytes());
}
// Null terminator
data.extend_from_slice(&[0, 0]);
data
}
/// Encodes timestamps for entries.
///
/// Takes a predicate function to extract the timestamp from each entry,
/// and a boolean vector indicating which entries have the timestamp defined.
pub(crate) fn encode_times<F>(&self, defined: &[bool], getter: F) -> Vec<u8>
where
F: Fn(&PendingEntry) -> Option<u64>,
{
let mut data = Vec::new();
// AllDefined flag
let all_defined = defined.iter().all(|&x| x);
if all_defined {
data.push(1);
} else {
data.push(0);
data.extend_from_slice(&encode_bool_vector(defined));
}
// External = 0
data.push(0);
// Times
for entry in &self.entries {
if let Some(time) = getter(entry) {
data.extend_from_slice(&time.to_le_bytes());
}
}
data
}
}