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