team-ljyzipelo

공개
457줄11 KB
1use std::path::{Path, PathBuf};
2
3pub const MAX_PREVIEW_TEXT_BYTES: u64 = 1024 * 1024;
4pub const MAX_PREVIEW_IMAGE_BYTES: u64 = 8 * 1024 * 1024;
5pub const MAX_PREVIEW_RGBA_BYTES: u64 = 64 * 1024 * 1024;
6pub const MAX_PREVIEW_DIMENSION: u32 = 8192;
7pub const MAX_PREVIEW_PIXELS: u64 = 16_000_000;
8pub const MAX_PREVIEW_CHUNK_BYTES: usize = 32 * 1024;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum NativePath {
12 Unix(Vec<u8>),
13 Windows(Vec<u16>),
14}
15impl NativePath {
16 pub fn from_path(path: &Path) -> Self {
17 #[cfg(unix)]
18 {
19 use std::os::unix::ffi::OsStrExt;
20 Self::Unix(path.as_os_str().as_bytes().to_vec())
21 }
22 #[cfg(windows)]
23 {
24 use std::os::windows::ffi::OsStrExt;
25 Self::Windows(path.as_os_str().encode_wide().collect())
26 }
27 }
28 pub fn to_path(&self) -> Result<PathBuf, ErrorCode> {
29 #[cfg(unix)]
30 if let Self::Unix(bytes) = self {
31 use std::os::unix::ffi::OsStringExt;
32 if bytes.contains(&0) {
33 return Err(ErrorCode::InvalidRequest);
34 }
35 let path = PathBuf::from(std::ffi::OsString::from_vec(bytes.clone()));
36 return if path.is_absolute() {
37 Ok(path)
38 } else {
39 Err(ErrorCode::InvalidRequest)
40 };
41 }
42 #[cfg(windows)]
43 if let Self::Windows(units) = self {
44 use std::os::windows::ffi::OsStringExt;
45 if units.contains(&0) {
46 return Err(ErrorCode::InvalidRequest);
47 }
48 let path = PathBuf::from(std::ffi::OsString::from_wide(units));
49 let text = path.to_string_lossy();
50 if path.is_absolute() && !text.starts_with("\\\\") && !text.starts_with("//") {
51 return Ok(path);
52 }
53 return Err(ErrorCode::InvalidRequest);
54 }
55 Err(ErrorCode::UnsupportedFeature)
56 }
57}
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub struct Limits {
60 pub input: u64,
61 pub output: u64,
62 pub entries: u32,
63}
64impl Default for Limits {
65 fn default() -> Self {
66 Self {
67 input: u64::MAX,
68 output: 16 * 1024 * 1024 * 1024,
69 entries: 10_000,
70 }
71 }
72}
73impl Limits {
74 pub(crate) fn legacy(self) -> Self {
75 Self {
76 input: self.input.min(64 * 1024 * 1024),
77 output: self.output.min(128 * 1024 * 1024),
78 entries: self.entries,
79 }
80 }
81
82 pub fn valid(self) -> bool {
83 let hard = Self::default();
84 self.input > 0
85 && self.input <= hard.input
86 && self.output > 0
87 && self.output <= hard.output
88 && self.entries > 0
89 && self.entries <= hard.entries
90 }
91}
92#[derive(Clone, Debug, PartialEq, Eq)]
93pub struct GuardedStart {
94 pub start: Start,
95 pub ceiling: Limits,
96 pub interactive: bool,
97}
98impl GuardedStart {
99 pub fn valid(&self) -> bool {
100 self.start.limits.valid()
101 && self.ceiling.input == self.start.limits.input
102 && self.ceiling.output >= self.start.limits.output
103 && self.ceiling.output <= 64 * 1024 * 1024 * 1024
104 && self.ceiling.entries >= self.start.limits.entries
105 && self.ceiling.entries <= 200_000
106 && (!self.interactive
107 || matches!(
108 self.start.action,
109 Action::Extract { .. } | Action::Preview { .. }
110 ))
111 && (!matches!(self.start.action, Action::Preview { .. }) || self.interactive)
112 }
113}
114#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
115pub struct ResourceScope {
116 pub output: bool,
117 pub entries: bool,
118 pub ratio: bool,
119}
120impl ResourceScope {
121 pub fn any(self) -> bool {
122 self.output || self.entries || self.ratio
123 }
124}
125#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
126pub struct ResourceUsage {
127 pub estimated_bytes: Option<u64>,
128 pub actual_bytes: Option<u64>,
129 pub estimated_entries: Option<u32>,
130 pub actual_entries: Option<u32>,
131 pub compressed_bytes: Option<u64>,
132 pub ratio_output_bytes: Option<u64>,
133 pub available_bytes: Option<u64>,
134}
135#[derive(Clone, Copy, Debug, PartialEq, Eq)]
136pub enum Format {
137 Zip,
138 Tar,
139 Gzip,
140 TarGzip,
141 SevenZip,
142}
143#[derive(Clone, Debug, PartialEq, Eq)]
144pub enum SourceObject {
145 Unix {
146 device: u64,
147 inode: u64,
148 },
149 Windows {
150 volume_serial: u64,
151 file_id: [u8; 16],
152 },
153}
154#[derive(Clone, Debug, PartialEq, Eq)]
155pub struct ListId {
156 pub catalog_version: u16,
157 pub format: Format,
158 pub source_object: SourceObject,
159 pub archive_bytes: u64,
160 pub archive_sha256: [u8; 32],
161}
162#[derive(Clone, Debug, PartialEq, Eq)]
163pub enum Selection {
164 All,
165 EntryIds(Vec<u32>),
166}
167#[derive(Clone, Copy, Debug, PartialEq, Eq)]
168pub enum Method {
169 Store,
170 Deflate,
171}
172#[derive(Clone, Copy, Debug, PartialEq, Eq)]
173pub enum Encryption {
174 None,
175 Aes256,
176}
177#[derive(Clone, Debug, PartialEq, Eq)]
178pub struct Source {
179 pub path: NativePath,
180 pub archive_name: String,
181}
182#[derive(Clone, Debug, PartialEq, Eq)]
183pub enum Action {
184 List {
185 archive: NativePath,
186 },
187 Extract {
188 archive: NativePath,
189 destination: NativePath,
190 selection: Selection,
191 list_id: Option<ListId>,
192 gzip_output_name: Option<String>,
193 },
194 CreateZip {
195 sources: Vec<Source>,
196 output_file: NativePath,
197 method: Method,
198 encryption: Encryption,
199 },
200 Preview {
201 archive: NativePath,
202 entry_id: u32,
203 list_id: ListId,
204 },
205}
206#[derive(Clone, Copy, Debug, PartialEq, Eq)]
207pub enum ActionKind {
208 List,
209 Extract,
210 CreateZip,
211 Preview,
212}
213impl Action {
214 pub fn kind(&self) -> ActionKind {
215 match self {
216 Self::List { .. } => ActionKind::List,
217 Self::Extract { .. } => ActionKind::Extract,
218 Self::CreateZip { .. } => ActionKind::CreateZip,
219 Self::Preview { .. } => ActionKind::Preview,
220 }
221 }
222}
223#[derive(Clone, Debug, PartialEq, Eq)]
224pub struct Start {
225 pub action: Action,
226 pub limits: Limits,
227}
228#[derive(Clone, Copy, Debug, PartialEq, Eq)]
229pub enum EntryKind {
230 File,
231 Directory,
232 Link,
233 Other,
234}
235#[derive(Clone, Debug, PartialEq, Eq)]
236pub struct Entry {
237 pub entry_id: u32,
238 pub name: String,
239 pub kind: EntryKind,
240 pub size: u64,
241 pub compressed_size: u64,
242 pub encrypted: bool,
243}
244#[derive(Clone, Copy, Debug, PartialEq, Eq)]
245pub enum PreviewFormat {
246 Utf8Text,
247 Rgba8,
248}
249#[derive(Clone, Copy, Debug, PartialEq, Eq)]
250pub struct PreviewInfo {
251 pub entry_id: u32,
252 pub format: PreviewFormat,
253 pub total_bytes: u64,
254 pub width: u32,
255 pub height: u32,
256}
257impl PreviewInfo {
258 pub fn valid(&self) -> bool {
259 match self.format {
260 PreviewFormat::Utf8Text => {
261 self.width == 0 && self.height == 0 && self.total_bytes <= MAX_PREVIEW_TEXT_BYTES
262 }
263 PreviewFormat::Rgba8 => {
264 if !(1..=MAX_PREVIEW_DIMENSION).contains(&self.width)
265 || !(1..=MAX_PREVIEW_DIMENSION).contains(&self.height)
266 {
267 return false;
268 }
269 let pixels = u64::from(self.width) * u64::from(self.height);
270 pixels <= MAX_PREVIEW_PIXELS
271 && pixels.checked_mul(4) == Some(self.total_bytes)
272 && self.total_bytes <= MAX_PREVIEW_RGBA_BYTES
273 }
274 }
275 }
276}
277#[derive(Clone, Copy, Debug, PartialEq, Eq)]
278pub enum Stage {
279 Scan,
280 Read,
281 Decode,
282 Write,
283 Verify,
284 Commit,
285}
286#[derive(Clone, Copy, Debug, PartialEq, Eq)]
287pub enum Disposition {
288 Created,
289 PartialRetained,
290 Removed,
291 CommitUncertain,
292}
293#[derive(Clone, Copy, Debug, PartialEq, Eq)]
294pub enum Outcome {
295 Completed,
296 Failed,
297 Cancelled,
298}
299#[derive(Clone, Copy, Debug, PartialEq, Eq)]
300pub enum ErrorCode {
301 None,
302 Integrity,
303 UnsupportedFormat,
304 UnsupportedFeature,
305 WrongPassword,
306 UnsafePath,
307 Conflict,
308 Limit,
309 Io,
310 Protocol,
311 Internal,
312 SourceChanged,
313 InvalidRequest,
314 DiskFull,
315}
316#[derive(Clone, Copy, Debug, PartialEq, Eq)]
317pub enum Publication {
318 None,
319 Partial,
320 Complete,
321 Uncertain,
322}
323#[derive(Clone, Copy, Debug, PartialEq, Eq)]
324pub enum Durability {
325 NotApplicable,
326 Synced,
327 Unconfirmed,
328 Failed,
329}
330#[derive(Clone, Copy, Debug, PartialEq, Eq)]
331pub enum Cleanup {
332 Complete,
333 Failed,
334 Unconfirmed,
335}
336#[derive(Clone, Copy, Debug, PartialEq, Eq)]
337pub enum CancelStatus {
338 Requested,
339 AlreadyRequested,
340 TooLate,
341 Finished,
342}
343#[derive(Clone, Copy, Debug, PartialEq, Eq)]
344pub enum PasswordPurpose {
345 Decrypt,
346 Encrypt,
347}
348#[derive(Clone, Copy, Debug, PartialEq, Eq)]
349pub enum ConflictChoice {
350 Overwrite,
351 Skip,
352 Rename,
353}
354#[derive(Clone, Debug, PartialEq, Eq)]
355pub struct Terminal {
356 pub outcome: Outcome,
357 pub error: ErrorCode,
358 pub publication: Publication,
359 pub created_count: u64,
360 pub retained_partial_count: u64,
361 pub durability: Durability,
362 pub cleanup: Cleanup,
363 pub detail: String,
364}
365pub struct Secret(pub Vec<u8>);
366impl std::fmt::Debug for Secret {
367 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
368 f.write_str("[redacted]")
369 }
370}
371impl Drop for Secret {
372 fn drop(&mut self) {
373 zeroize::Zeroize::zeroize(&mut self.0);
374 }
375}
376#[derive(Debug)]
377pub enum Message {
378 Start(Start),
379 Password {
380 challenge_id: u64,
381 secret: Secret,
382 },
383 Cancel,
384 StartInteractive(Start),
385 StartGuarded(GuardedStart),
386 ResourceReply {
387 challenge_id: u64,
388 scope: ResourceScope,
389 allow: bool,
390 },
391 ResourceRequired {
392 challenge_id: u64,
393 action: ActionKind,
394 scope: ResourceScope,
395 usage: ResourceUsage,
396 soft_limits: Limits,
397 ceiling: Limits,
398 },
399 ConflictReply {
400 challenge_id: u64,
401 choice: ConflictChoice,
402 apply_to_remaining: bool,
403 },
404 Accepted {
405 action: ActionKind,
406 encryption: bool,
407 },
408 Entry(Entry),
409 ListEnd {
410 entry_count: u32,
411 list_id: ListId,
412 },
413 PasswordRequired {
414 challenge_id: u64,
415 attempt: u32,
416 purpose: PasswordPurpose,
417 },
418 Progress {
419 stage: Stage,
420 processed_bytes: u64,
421 total_bytes: Option<u64>,
422 completed_entries: u32,
423 },
424 Output {
425 entry_id: Option<u32>,
426 relative_name: String,
427 disposition: Disposition,
428 },
429 PreviewBegin(PreviewInfo),
430 PreviewChunk {
431 entry_id: u32,
432 offset: u64,
433 data: Vec<u8>,
434 },
435 PreviewEnd {
436 entry_id: u32,
437 total_bytes: u64,
438 sha256: [u8; 32],
439 },
440 Terminal(Terminal),
441 CancelAck {
442 request_sequence: u64,
443 status: CancelStatus,
444 },
445 ConflictRequired {
446 challenge_id: u64,
447 entry_id: u32,
448 relative_name: String,
449 incoming_size: u64,
450 existing_size: u64,
451 },
452 ConflictResolved {
453 entry_id: u32,
454 relative_name: String,
455 choice: ConflictChoice,
456 },
457}