wxtla 0.3.1

Wired eXploring Target Layer Accessor
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Read-only RAR archive surface backed by cached extraction.

use std::{
  collections::{BTreeMap, HashMap},
  io::Read,
  path::{Path, PathBuf},
  process::{ExitStatus, Stdio},
  thread,
  time::{Duration, Instant},
};

use super::DESCRIPTOR;
use crate::{
  ByteSourceHandle, Error, FileDataSource, NamespaceDirectoryEntry, NamespaceNodeId,
  NamespaceNodeKind, NamespaceNodeRecord, Result, SourceHints,
  archives::{
    Archive,
    cache::{ArchiveCachePaths, ensure_cache_space, prepare_archive_cache, reset_extract_dir},
    tool::find_tool,
  },
};

const ROOT_ENTRY_ID: u64 = 0;
const LIST_TIMEOUT: Duration = Duration::from_secs(15);
const EXTRACT_TIMEOUT: Duration = Duration::from_secs(60);

pub struct RarArchive {
  entries: Vec<RarEntry>,
  path_to_id: HashMap<String, NamespaceNodeId>,
  cache: ArchiveCachePaths,
  total_uncompressed_size: u64,
  locked: bool,
  headers_locked: bool,
}

#[derive(Clone)]
struct RarEntry {
  record: NamespaceNodeRecord,
  children: Vec<NamespaceDirectoryEntry>,
  extracted_path: Option<PathBuf>,
}

#[derive(Clone)]
struct RarListingEntry {
  path: String,
  kind: NamespaceNodeKind,
  size: u64,
  encrypted: bool,
}

impl RarArchive {
  pub fn open(source: ByteSourceHandle) -> Result<Self> {
    Self::open_with_hints(source, SourceHints::new())
  }

  pub fn open_with_hints(source: ByteSourceHandle, _hints: SourceHints<'_>) -> Result<Self> {
    let cache = prepare_archive_cache(source.as_ref(), "rar")?;
    match list_archive(&cache.source_path, None) {
      Ok(listing) => {
        let total_uncompressed_size = listing.iter().map(|entry| entry.size).sum();
        let locked = listing.iter().any(|entry| entry.encrypted);
        let (entries, path_to_id) = build_tree(&listing, Some(&cache.extract_dir))?;
        let mut archive = Self {
          entries,
          path_to_id,
          cache,
          total_uncompressed_size,
          locked,
          headers_locked: false,
        };
        if !archive.locked {
          archive.extract(None)?;
        }
        Ok(archive)
      }
      Err(Error::InvalidSourceReference(message))
        if message.contains("encrypted headers")
          || message.contains("Wrong password")
          || message.contains("Can not open encrypted archive")
          || message.contains("Break signaled") =>
      {
        Ok(Self {
          entries: vec![RarEntry {
            record: NamespaceNodeRecord::new(
              NamespaceNodeId::from_u64(ROOT_ENTRY_ID),
              NamespaceNodeKind::Directory,
              0,
            ),
            children: Vec::new(),
            extracted_path: None,
          }],
          path_to_id: HashMap::new(),
          cache,
          total_uncompressed_size: 0,
          locked: true,
          headers_locked: true,
        })
      }
      Err(error) => Err(error),
    }
  }

  pub fn find_entry_by_path(&self, path: &str) -> Option<NamespaceNodeId> {
    self.path_to_id.get(path).cloned()
  }

  #[allow(dead_code)]
  fn populate_listing(&mut self, password: &str) -> Result<()> {
    let listing = list_archive(&self.cache.source_path, Some(password))?;
    self.total_uncompressed_size = listing.iter().map(|entry| entry.size).sum();
    let (entries, path_to_id) = build_tree(&listing, Some(&self.cache.extract_dir))?;
    self.entries = entries;
    self.path_to_id = path_to_id;
    self.headers_locked = false;
    Ok(())
  }

  fn extract(&mut self, password: Option<&str>) -> Result<()> {
    ensure_cache_space(&self.cache.extract_dir, self.total_uncompressed_size)?;
    reset_extract_dir(&self.cache.extract_dir)?;

    if let Ok(tool) = find_tool(&["unrar"]) {
      let output = extract_with_tool(
        &tool,
        &self.cache.source_path,
        &self.cache.extract_dir,
        password,
      )?;
      if output.status.success() {
        self.locked = false;
        self.refresh_extracted_paths();
        return Ok(());
      }
      return Err(Error::invalid_source_reference(if password.is_some() {
        "rar archive password unlock failed"
      } else {
        "unable to extract rar archive into cache"
      }));
    }

    if let Ok(tool) = find_tool(&["unar"]) {
      let output = extract_with_unar_tool(
        &tool,
        &self.cache.source_path,
        &self.cache.extract_dir,
        password,
      )?;
      if output.status.success() {
        self.locked = false;
        self.refresh_extracted_paths();
        return Ok(());
      }
      return Err(Error::invalid_source_reference(if password.is_some() {
        "rar archive password unlock failed"
      } else {
        "unable to extract rar archive into cache"
      }));
    }

    let tool = find_tool(&["7z", "7zz"])?;
    let mut command = std::process::Command::new(&tool);
    command
      .arg("x")
      .arg("-y")
      .arg("-bb0")
      .arg("-bso0")
      .arg("-bsp0")
      .arg(format!("-o{}", self.cache.extract_dir.display()));
    if let Some(password) = password {
      command.arg(format!("-p{password}"));
    }
    command.arg(&self.cache.source_path);
    command.stdout(Stdio::null());
    let output = run_command_with_timeout(command, EXTRACT_TIMEOUT)?;
    if !output.status.success() {
      return Err(Error::invalid_source_reference(if password.is_some() {
        "rar archive password unlock failed"
      } else {
        "unable to extract rar archive into cache"
      }));
    }

    self.locked = false;
    self.refresh_extracted_paths();
    Ok(())
  }

  fn refresh_extracted_paths(&mut self) {
    for entry in self.entries.iter_mut().skip(1) {
      entry.extracted_path = if entry.record.kind == NamespaceNodeKind::File {
        Some(self.cache.extract_dir.join(&entry.record.path))
      } else {
        None
      };
    }
  }

  fn entry_ref(&self, entry_id: &NamespaceNodeId) -> Result<&RarEntry> {
    let index = entry_id_to_index(entry_id)?;
    self
      .entries
      .get(index)
      .ok_or_else(|| Error::not_found(format!("missing rar archive entry index: {index}")))
  }
}

impl Archive for RarArchive {
  fn descriptor(&self) -> crate::FormatDescriptor {
    DESCRIPTOR
  }

  fn root_entry_id(&self) -> NamespaceNodeId {
    NamespaceNodeId::from_u64(ROOT_ENTRY_ID)
  }

  fn entry(&self, entry_id: &NamespaceNodeId) -> Result<NamespaceNodeRecord> {
    if self.headers_locked {
      return Err(Error::invalid_source_reference(
        "rar archive headers are encrypted; unlock the archive before reading entries".to_string(),
      ));
    }
    Ok(self.entry_ref(entry_id)?.record.clone())
  }

  fn read_dir(&self, directory_id: &NamespaceNodeId) -> Result<Vec<NamespaceDirectoryEntry>> {
    if self.headers_locked {
      return Err(Error::invalid_source_reference(
        "rar archive headers are encrypted; unlock the archive before listing entries".to_string(),
      ));
    }
    let entry = self.entry_ref(directory_id)?;
    if entry.record.kind != NamespaceNodeKind::Directory {
      return Err(Error::invalid_format(
        "rar directory reads require a directory entry".to_string(),
      ));
    }
    Ok(entry.children.clone())
  }

  fn open_file(&self, entry_id: &NamespaceNodeId) -> Result<ByteSourceHandle> {
    if self.locked {
      return Err(Error::invalid_source_reference(
        "rar archive is locked; unlock it with a password before opening files".to_string(),
      ));
    }
    let entry = self.entry_ref(entry_id)?;
    if entry.record.kind != NamespaceNodeKind::File {
      return Err(Error::invalid_format(
        "rar file opens require a regular file entry".to_string(),
      ));
    }
    let path = entry.extracted_path.as_ref().ok_or_else(|| {
      Error::invalid_format("rar file entry does not have a cached extraction path")
    })?;
    Ok(std::sync::Arc::new(FileDataSource::open(path)?) as ByteSourceHandle)
  }

  fn is_locked(&self) -> bool {
    self.locked || self.headers_locked
  }

  fn unlock_with_password(&mut self, password: &str) -> Result<bool> {
    if !self.headers_locked && !self.locked {
      return Ok(true);
    }
    if self.headers_locked {
      match self.populate_listing(password) {
        Ok(()) => {}
        Err(Error::InvalidSourceReference(_)) => return Ok(false),
        Err(error) => return Err(error),
      }
    }
    match self.extract(Some(password)) {
      Ok(()) => Ok(true),
      Err(Error::InvalidSourceReference(_)) => Ok(false),
      Err(error) => Err(error),
    }
  }
}

fn list_archive(source_path: &Path, password: Option<&str>) -> Result<Vec<RarListingEntry>> {
  let tool = find_tool(&["7z", "7zz"])?;
  let mut command = std::process::Command::new(&tool);
  command.arg("l").arg("-slt");
  if let Some(password) = password {
    command.arg(format!("-p{password}"));
  }
  command.arg(source_path);
  let output = run_command_with_timeout(command, LIST_TIMEOUT)?;
  if !output.status.success() {
    return Err(Error::invalid_source_reference(format!(
      "unable to list rar archive contents: {}",
      String::from_utf8_lossy(&output.stderr)
    )));
  }
  parse_rar_listing(&String::from_utf8_lossy(&output.stdout))
}

fn extract_with_tool(
  tool: &Path, source_path: &Path, extract_dir: &Path, password: Option<&str>,
) -> Result<CommandOutput> {
  let mut command = std::process::Command::new(tool);
  command
    .arg("x")
    .arg("-o+")
    .arg(format!("-op{}", extract_dir.display()))
    .arg(match password {
      Some(password) => format!("-p{password}"),
      None => "-p-".to_string(),
    })
    .arg(source_path);
  run_command_with_timeout(command, EXTRACT_TIMEOUT)
}

fn extract_with_unar_tool(
  tool: &Path, source_path: &Path, extract_dir: &Path, password: Option<&str>,
) -> Result<CommandOutput> {
  let mut command = std::process::Command::new(tool);
  command.arg("-f").arg("-D").arg("-q");
  if let Some(password) = password {
    command.arg("-p").arg(password);
  }
  command.arg("-o").arg(extract_dir).arg(source_path);
  run_command_with_timeout(command, EXTRACT_TIMEOUT)
}

fn run_command_with_timeout(
  mut command: std::process::Command, timeout: Duration,
) -> Result<CommandOutput> {
  command.stdout(Stdio::piped());
  command.stderr(Stdio::piped());

  let mut child = command.spawn()?;
  let mut stdout = child
    .stdout
    .take()
    .ok_or_else(|| Error::invalid_source_reference("missing command stdout pipe"))?;
  let mut stderr = child
    .stderr
    .take()
    .ok_or_else(|| Error::invalid_source_reference("missing command stderr pipe"))?;
  let start = Instant::now();

  loop {
    if let Some(status) = child.try_wait()? {
      let mut stdout_bytes = Vec::new();
      let mut stderr_bytes = Vec::new();
      stdout.read_to_end(&mut stdout_bytes)?;
      stderr.read_to_end(&mut stderr_bytes)?;
      return Ok(CommandOutput {
        status,
        stdout: stdout_bytes,
        stderr: stderr_bytes,
      });
    }

    if start.elapsed() >= timeout {
      let _ = child.kill();
      let _ = child.wait();
      return Err(Error::invalid_source_reference(format!(
        "rar helper command timed out after {} seconds",
        timeout.as_secs()
      )));
    }

    thread::sleep(Duration::from_millis(50));
  }
}

struct CommandOutput {
  status: ExitStatus,
  stdout: Vec<u8>,
  stderr: Vec<u8>,
}

fn parse_rar_listing(text: &str) -> Result<Vec<RarListingEntry>> {
  let mut entries = Vec::new();
  let mut current = BTreeMap::<String, String>::new();
  let mut in_entries = false;
  for line in text.lines() {
    let line = line.trim_end();
    if line == "----------" {
      in_entries = true;
      continue;
    }
    if !in_entries {
      continue;
    }
    if line.is_empty() {
      if let Some(entry) = listing_entry_from_map(&current)? {
        entries.push(entry);
      }
      current.clear();
      continue;
    }
    if let Some((key, value)) = line.split_once(" = ") {
      current.insert(key.to_string(), value.to_string());
    }
  }
  if let Some(entry) = listing_entry_from_map(&current)? {
    entries.push(entry);
  }
  Ok(entries)
}

fn listing_entry_from_map(map: &BTreeMap<String, String>) -> Result<Option<RarListingEntry>> {
  let Some(path) = map.get("Path") else {
    return Ok(None);
  };
  if map.get("Type").is_some() {
    return Ok(None);
  }
  let is_dir = map.get("Folder").is_some_and(|value| value == "+")
    || map
      .get("Attributes")
      .is_some_and(|value| value.starts_with('D'));
  let normalized = normalize_path(path, is_dir)?;
  if normalized.is_empty() {
    return Ok(None);
  }
  let size = map
    .get("Size")
    .and_then(|value| value.parse().ok())
    .unwrap_or(0);
  Ok(Some(RarListingEntry {
    path: normalized,
    kind: if is_dir {
      NamespaceNodeKind::Directory
    } else {
      NamespaceNodeKind::File
    },
    size,
    encrypted: map.get("Encrypted").is_some_and(|value| value == "+")
      || map
        .get("Flags")
        .is_some_and(|value| value.to_ascii_lowercase().contains("encrypted"))
      || map
        .get("Method")
        .is_some_and(|value| value.contains("AES") || value.contains("Crypt")),
  }))
}

fn build_tree(
  listing: &[RarListingEntry], extract_root: Option<&Path>,
) -> Result<(Vec<RarEntry>, HashMap<String, NamespaceNodeId>)> {
  let mut builders = BTreeMap::<String, RarListingEntry>::new();
  for entry in listing {
    ensure_parent_directories(&mut builders, &entry.path)?;
    builders.insert(entry.path.clone(), entry.clone());
  }

  let mut path_to_id = HashMap::new();
  let ordered_paths = builders.keys().cloned().collect::<Vec<_>>();
  for (index, path) in ordered_paths.iter().enumerate() {
    path_to_id.insert(path.clone(), NamespaceNodeId::from_u64(index as u64 + 1));
  }

  let mut entries = Vec::with_capacity(ordered_paths.len() + 1);
  entries.push(RarEntry {
    record: NamespaceNodeRecord::new(
      NamespaceNodeId::from_u64(ROOT_ENTRY_ID),
      NamespaceNodeKind::Directory,
      0,
    ),
    children: Vec::new(),
    extracted_path: None,
  });
  for path in &ordered_paths {
    let entry = builders.get(path).ok_or_else(|| {
      Error::invalid_format(format!("missing rar entry builder for path: {path}"))
    })?;
    let id = path_to_id.get(path).cloned().ok_or_else(|| {
      Error::invalid_format(format!("missing rar entry identifier for path: {path}"))
    })?;
    entries.push(RarEntry {
      record: NamespaceNodeRecord::new(id, entry.kind, entry.size).with_path(path.clone()),
      children: Vec::new(),
      extracted_path: extract_root
        .and_then(|root| (entry.kind == NamespaceNodeKind::File).then(|| root.join(path))),
    });
  }

  for path in &ordered_paths {
    let child_id = path_to_id
      .get(path)
      .cloned()
      .ok_or_else(|| Error::invalid_format(format!("missing rar path mapping for path: {path}")))?;
    let child_index = entry_id_to_index(&child_id)?;
    let child_kind = entries[child_index].record.kind;
    let name = relative_name(path);
    let parent_index = match parent_path(path) {
      Some(parent) => entry_id_to_index(path_to_id.get(parent).ok_or_else(|| {
        Error::invalid_format(format!("missing rar parent directory mapping: {parent}"))
      })?)?,
      None => 0,
    };
    entries[parent_index]
      .children
      .push(NamespaceDirectoryEntry::new(name, child_id, child_kind));
  }
  for entry in &mut entries {
    entry
      .children
      .sort_by(|left, right| left.name.cmp(&right.name));
  }
  Ok((entries, path_to_id))
}

fn ensure_parent_directories(
  builders: &mut BTreeMap<String, RarListingEntry>, path: &str,
) -> Result<()> {
  let mut current = path;
  while let Some(parent) = parent_path(current) {
    builders
      .entry(parent.to_string())
      .or_insert_with(|| RarListingEntry {
        path: parent.to_string(),
        kind: NamespaceNodeKind::Directory,
        size: 0,
        encrypted: false,
      });
    current = parent;
  }
  Ok(())
}

fn normalize_path(path: &str, is_dir: bool) -> Result<String> {
  let path = path.trim_matches('').trim();
  let path = path.strip_prefix("./").unwrap_or(path);
  let components = path
    .split('/')
    .filter(|component| !component.is_empty() && *component != ".")
    .collect::<Vec<_>>();
  if components.contains(&"..") {
    return Err(Error::invalid_format(
      "rar paths must not contain parent directory traversals".to_string(),
    ));
  }
  let normalized = components.join("/");
  if normalized.is_empty() && !is_dir {
    return Err(Error::invalid_format(
      "rar file entries must have a non-empty path".to_string(),
    ));
  }
  Ok(normalized)
}

fn parent_path(path: &str) -> Option<&str> {
  path.rsplit_once('/').map(|(parent, _)| parent)
}

fn relative_name(path: &str) -> String {
  path
    .rsplit_once('/')
    .map_or_else(|| path.to_string(), |(_, name)| name.to_string())
}

fn entry_id_to_index(entry_id: &NamespaceNodeId) -> Result<usize> {
  let bytes: [u8; 8] = entry_id.as_bytes().try_into().map_err(|_| {
    Error::invalid_format("rar archive entry identifiers must be native u64 values")
  })?;
  usize::try_from(u64::from_le_bytes(bytes))
    .map_err(|_| Error::invalid_range("rar archive entry index is too large"))
}

#[cfg(test)]
mod tests {
  use std::{path::Path, sync::Arc};

  use super::*;
  use crate::ByteSource;

  struct MemDataSource {
    data: Vec<u8>,
  }

  impl ByteSource for MemDataSource {
    fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<usize> {
      let offset = usize::try_from(offset)
        .map_err(|_| Error::invalid_range("test read offset is too large"))?;
      if offset >= self.data.len() {
        return Ok(0);
      }
      let read = buf.len().min(self.data.len() - offset);
      buf[..read].copy_from_slice(&self.data[offset..offset + read]);
      Ok(read)
    }

    fn size(&self) -> Result<u64> {
      Ok(self.data.len() as u64)
    }
  }

  fn sample_source(relative_path: &str) -> ByteSourceHandle {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
      .join("formats")
      .join(relative_path);
    Arc::new(MemDataSource {
      data: std::fs::read(path).unwrap(),
    })
  }

  fn md5_hex(data: &[u8]) -> String {
    format!("{:x}", md5::compute(data))
  }

  #[test]
  fn opens_plain_fixture_metadata_and_contents() {
    let archive = RarArchive::open(sample_source("rar/version.rar")).unwrap();
    let id = archive.find_entry_by_path("VERSION").unwrap();
    let data = archive.open_file(&id).unwrap().read_all().unwrap();
    assert_eq!(std::str::from_utf8(&data).unwrap(), "unrar-0.4.0");
  }

  #[test]
  fn unlocks_encrypted_fixture_with_password() {
    let mut archive = RarArchive::open(sample_source("rar/crypted.rar")).unwrap();
    assert!(archive.is_locked());
    assert!(!archive.unlock_with_password("wrong").unwrap());
    assert!(archive.unlock_with_password("unrar").unwrap());

    let id = archive.find_entry_by_path(".gitignore").unwrap();
    let data = archive.open_file(&id).unwrap().read_all().unwrap();
    assert_eq!(std::str::from_utf8(&data).unwrap(), "target\nCargo.lock\n");
  }

  #[test]
  fn unlocks_header_encrypted_fixture_with_password() {
    let mut archive = RarArchive::open(sample_source("rar/comment-hpw-password.rar")).unwrap();
    assert!(archive.is_locked());
    assert!(archive.read_dir(&archive.root_entry_id()).is_err());
    assert!(archive.unlock_with_password("password").unwrap());

    let id = archive.find_entry_by_path(".gitignore").unwrap();
    let data = archive.open_file(&id).unwrap().read_all().unwrap();
    assert_eq!(md5_hex(&data), md5_hex(b"target\nCargo.lock\n"));
  }
}

crate::archives::driver::impl_archive_data_source!(RarArchive);