aff4-image 0.2.0

Experimental Rust reader, verifier, and writer for AFF4 evidence containers
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
//! Explicit multi-volume physical reading. Graphs retain their source context.
use super::*;
use std::path::PathBuf;

#[path = "set_verify.rs"]
mod set_verify;
pub use set_verify::{SetVerification, SetVolumeVerification};

/// Attribution of a stored ImageStream to its owning ZIP volume.
#[derive(Debug, Clone, serde::Serialize)]
pub struct VolumeSource {
    /// Data stream resource.
    pub stream: String,
    /// Owning volume identifier.
    pub volume: String,
    /// Supplied path to that volume.
    pub path: PathBuf,
}

/// Whole-image digest over a resolved volume set, distinct from block-map hashes.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SetDigest {
    /// Image identifier.
    pub image: String,
    /// Logical bytes traversed.
    pub bytes: u64,
    /// SHA256 of the assembled logical image.
    pub sha256: String,
    /// Comparison with an independently supplied whole-image SHA256.
    pub external_match: Option<bool>,
    /// Stream owners, sorted by stream identifier.
    pub sources: Vec<VolumeSource>,
}

/// A primary physical container plus explicitly supplied companion volumes.
/// No filenames are guessed, no graphs are flattened, and ambiguous owners fail.
/// This reader does not yet certify a striped image's composite block-map digest.
pub struct VolumeSet {
    volumes: Vec<Container>,
    paths: Vec<PathBuf>,
    owners: BTreeMap<String, usize>,
    validated: BTreeMap<(usize, String), (Arc<Map>, u64)>,
    cached_owner: Option<usize>,
}

impl VolumeSet {
    /// Opens the primary and companions with a shared metadata/directory budget.
    /// The primary must contain the selected image's complete Map. Named foreign
    /// volume references must be supplied; duplicate owners/volume IDs are errors.
    pub fn open(paths: &[PathBuf]) -> Result<Self> {
        Self::open_with_limits(paths, Limits::default())
    }

    /// Opens at most 128 volumes. Metadata bytes, triples, ZIP directory bytes,
    /// and entry counts are aggregate limits across the entire set. Chunk and
    /// member limits apply per object; only one volume retains payload caches.
    pub fn open_with_limits(paths: &[PathBuf], limits: Limits) -> Result<Self> {
        if paths.is_empty() || paths.len() > 128 {
            return Err(malformed("volume set must contain 1 through 128 paths"));
        }
        let mut volumes = Vec::new();
        let mut ids = BTreeSet::new();
        let mut remaining = limits.clone();
        for path in paths {
            let mut volume = Container::open_with_limits(path, remaining.clone())?;
            remaining.metadata_bytes -= volume.usage.metadata_bytes;
            remaining.triples -= volume.usage.triples;
            remaining.directory_bytes -= volume.usage.directory_bytes;
            remaining.archive_entries -= volume.usage.entries;
            volume.limits = limits.clone();
            if volume.version != (1, 0) {
                return Err(Error::Unsupported(
                    "volume sets currently support physical AFF4 1.0".into(),
                ));
            }
            if !ids.insert(volume.volume.clone()) {
                return Err(malformed("duplicate volume identifier"));
            }
            volumes.push(volume);
        }
        Self::from_containers(volumes, paths.to_vec())
    }

    pub(super) fn from_containers(
        mut volumes: Vec<Container>,
        paths: Vec<PathBuf>,
    ) -> Result<Self> {
        let ids: BTreeSet<_> = volumes.iter().map(|volume| volume.volume.clone()).collect();
        if ids.len() != volumes.len() {
            return Err(malformed("duplicate volume identifier"));
        }
        let mut owners = BTreeMap::new();
        for (index, volume) in volumes.iter().enumerate() {
            for id in volume
                .graph
                .keys()
                .filter(|id| volume.has_type(id, "ImageStream"))
            {
                if let Some(stored) = volume.value(id, "stored")?
                    && !ids.contains(&stored)
                {
                    return Err(malformed(format!("missing source volume {stored}")));
                }
                let Ok(size) = volume.number(id, "size") else {
                    continue;
                };
                let path = volume.path(id)?;
                let owns = if size == 0 {
                    volume.value(id, "stored")?.as_deref() == Some(&volume.volume)
                } else {
                    volume
                        .archive
                        .index_for_name(&format!("{path}/00000000"))
                        .is_some()
                };
                if owns {
                    if volume
                        .value(id, "stored")?
                        .is_some_and(|v| v != volume.volume)
                    {
                        return Err(malformed("local stream conflicts with declared owner"));
                    }
                    if owners.insert(id.clone(), index).is_some() {
                        return Err(malformed(format!("ambiguous stream owner: {id}")));
                    }
                }
            }
        }
        // Validate supplied geometry against the owner's metadata. A foreign stub
        // may omit it, but an explicit disagreement is not silently overridden.
        for (id, &owner) in &owners {
            for volume in &volumes {
                if !volume.graph.contains_key(id) {
                    continue;
                }
                for key in [
                    "size",
                    "chunkSize",
                    "chunksInSegment",
                    "compressionMethod",
                    "stored",
                ] {
                    if let Some(value) = volume.value(id, key)? {
                        let expected = if key == "stored" {
                            Some(volumes[owner].volume.clone())
                        } else {
                            volumes[owner].value(id, key)?
                        };
                        if expected.as_ref() != Some(&value) {
                            return Err(malformed(format!("conflicting {key} for {id}")));
                        }
                    }
                }
            }
        }
        let decoded_cache = Arc::new(Mutex::new(DecodedChunkCache::new()));
        for volume in &mut volumes {
            volume.decoded_cache = Arc::clone(&decoded_cache);
        }
        Ok(Self {
            volumes,
            paths,
            owners,
            validated: BTreeMap::new(),
            cached_owner: None,
        })
    }

    /// Identifiers of physical images described by the primary volume.
    pub fn images(&self) -> Vec<String> {
        self.volumes[0]
            .graph
            .keys()
            .filter(|id| self.volumes[0].has_type(id, "DiskImage"))
            .cloned()
            .collect()
    }

    /// Data stream ownership; metadata itself remains scoped to each volume.
    pub fn sources(&self) -> Vec<VolumeSource> {
        self.owners
            .iter()
            .map(|(id, &n)| VolumeSource {
                stream: id.clone(),
                volume: self.volumes[n].volume.clone(),
                path: self.paths[n].clone(),
            })
            .collect()
    }

    /// Returns cumulative payload-reader counters shared by all volumes.
    pub fn reader_statistics(&self) -> ReaderStatistics {
        self.volumes[0].reader_statistics()
    }

    /// Returns shared automatic decoded payload-cache usage.
    pub fn reader_cache_info(&self) -> ReaderCacheInfo {
        self.volumes[0].reader_cache_info()
    }

    /// Opens a primary map and validates all referenced storage before reading.
    fn image_map(&mut self, id: &str) -> Result<(Arc<Map>, u64)> {
        self.image_map_in(0, id)
    }

    fn image_map_in(&mut self, primary: usize, id: &str) -> Result<(Arc<Map>, u64)> {
        if let Some(result) = self.validated.get(&(primary, id.to_owned())) {
            return Ok(result.clone());
        }
        if !self.volumes[primary].has_type(id, "DiskImage") {
            return Err(Error::Unsupported("select a primary DiskImage".into()));
        }
        let target = self.volumes[primary]
            .value(id, "dataStream")?
            .ok_or_else(|| malformed("image has no dataStream"))?;
        self.volumes[primary].load_map(&target)?;
        let map = self.volumes[primary].maps[&target].clone();
        // The set retains one validated map, not one map per primary container.
        self.volumes[primary].maps.clear();
        let size = self.volumes[primary].number(&target, "size")?;
        if self.volumes[primary].value(id, "size")?.is_some()
            && self.volumes[primary].number(id, "size")? != size
        {
            return Err(malformed("image/map size mismatch"));
        }
        let contiguous = self.volumes[primary].has_type(id, "ContiguousImage");
        let mut end = 0;
        for range in &map.ranges {
            if contiguous && range.start != end {
                return Err(malformed("gap in contiguous image"));
            }
            end = range.end;
            if is_symbolic(&range.target) {
                continue;
            }
            let owner = *self
                .owners
                .get(&range.target)
                .ok_or_else(|| malformed(format!("missing owner for {}", range.target)))?;
            let size = self.volumes[owner].number(&range.target, "size")?;
            if range
                .offset
                .checked_add(range.end - range.start)
                .is_none_or(|end| end > size)
            {
                return Err(malformed("map exceeds owned stream"));
            }
        }
        if contiguous && end != size {
            return Err(malformed("truncated contiguous map"));
        }
        if !is_symbolic(&map.gap) && !self.owners.contains_key(&map.gap) {
            return Err(Error::Unsupported("external/nested gap stream".into()));
        }
        self.validated.clear();
        self.validated
            .insert((primary, id.into()), (map.clone(), size));
        Ok((map, size))
    }

    pub(super) fn equivalent_disk_maps(
        &mut self,
        left: usize,
        right: usize,
        id: &str,
    ) -> Result<bool> {
        let (left, size) = self.image_map_in(left, id)?;
        let (right, other_size) = self.image_map_in(right, id)?;
        if size != other_size {
            return Ok(false);
        }
        let mut position = 0;
        while position < size {
            let (a, a_offset, a_end) = map_interval(&left, position, size);
            let (b, b_offset, b_end) = map_interval(&right, position, size);
            if a != b || (!is_symbolic(a) && a_offset != b_offset) {
                return Ok(false);
            }
            position = a_end.min(b_end);
        }
        Ok(true)
    }

    /// Logical size of a selected primary image, after dependency validation.
    pub fn size(&mut self, id: &str) -> Result<u64> {
        self.image_map(id).map(|(_, size)| size)
    }

    /// Reads assembled image bytes using the primary Map and explicit owners.
    /// Unknown/unreadable symbolic data and missing companions fail closed.
    pub fn read_at(&mut self, id: &str, buffer: &mut [u8], offset: u64) -> Result<usize> {
        self.read_disk_at(0, true, id, buffer, offset)
    }

    pub(super) fn disk_size(&mut self, primary: usize, mapped: bool, id: &str) -> Result<u64> {
        if mapped {
            self.image_map_in(primary, id).map(|(_, size)| size)
        } else {
            self.volumes[primary].size(id)
        }
    }

    pub(super) fn read_disk_at(
        &mut self,
        primary: usize,
        mapped: bool,
        id: &str,
        buffer: &mut [u8],
        offset: u64,
    ) -> Result<usize> {
        let size = self.disk_size(primary, mapped, id)?;
        if offset >= size || buffer.is_empty() {
            return Ok(0);
        }
        let length = (size - offset).min(buffer.len() as u64) as usize;
        if length >= LOGICAL_READ_AHEAD_BYTES {
            let read =
                self.read_disk_at_impl(primary, mapped, id, &mut buffer[..length], offset, false)?;
            self.retain_complete_pages(primary, id, &buffer[..read], offset)?;
            return Ok(read);
        }

        let mut done = 0;
        while done < length {
            let position = offset
                .checked_add(done as u64)
                .ok_or_else(|| malformed("AFF4 read offset overflow"))?;
            let page_start =
                position / LOGICAL_READ_AHEAD_BYTES as u64 * LOGICAL_READ_AHEAD_BYTES as u64;
            let page_offset = usize::try_from(position - page_start)
                .map_err(|_| malformed("AFF4 page offset overflow"))?;
            let key = self.disk_page_key(primary, id, page_start)?;
            let cached = self.volumes[primary]
                .decoded_cache
                .lock()
                .map_err(|_| malformed("AFF4 decoded cache lock poisoned"))?
                .get_page(&key);
            let page = if let Some(cached) = cached {
                Some(cached)
            } else {
                let page_length = LOGICAL_READ_AHEAD_BYTES
                    .min(usize::try_from(size - page_start).unwrap_or(LOGICAL_READ_AHEAD_BYTES));
                let mut page = vec![0; page_length];
                match self.read_disk_at_impl(primary, mapped, id, &mut page, page_start, false) {
                    Ok(read) if read == page_length => {
                        let page = Arc::new(page);
                        let mut cache = self.volumes[primary]
                            .decoded_cache
                            .lock()
                            .map_err(|_| malformed("AFF4 decoded cache lock poisoned"))?;
                        cache.record_read_ahead_prefetch();
                        cache.insert(key, Arc::clone(&page));
                        Some(page)
                    }
                    Ok(_) => return Err(malformed("truncated AFF4 disk page")),
                    Err(_) => None,
                }
            };
            let Some(page) = page else {
                // Read-ahead must not make a valid requested range fail only
                // because later bytes in the same page are unreadable.
                let page_remaining = LOGICAL_READ_AHEAD_BYTES - page_offset;
                let take = page_remaining.min(length - done);
                let read = self.read_disk_at_impl(
                    primary,
                    mapped,
                    id,
                    &mut buffer[done..done + take],
                    position,
                    false,
                )?;
                if read != take {
                    return Err(malformed("truncated AFF4 disk read"));
                }
                done += take;
                continue;
            };
            let take = (page.len() - page_offset).min(length - done);
            buffer[done..done + take].copy_from_slice(&page[page_offset..page_offset + take]);
            done += take;
        }
        Ok(length)
    }

    fn disk_page_key(&self, primary: usize, id: &str, offset: u64) -> Result<DecodedCacheKey> {
        let volume = self
            .volumes
            .get(primary)
            .ok_or_else(|| malformed("AFF4 primary volume index out of range"))?
            .volume
            .clone();
        Ok(DecodedCacheKey::DiskPage {
            volume,
            resource: id.to_owned(),
            offset,
        })
    }

    fn retain_complete_pages(
        &self,
        primary: usize,
        id: &str,
        buffer: &[u8],
        offset: u64,
    ) -> Result<()> {
        let request_end = offset
            .checked_add(buffer.len() as u64)
            .ok_or_else(|| malformed("AFF4 read offset overflow"))?;
        let page_bytes = LOGICAL_READ_AHEAD_BYTES as u64;
        let mut page_start = match offset % page_bytes {
            0 => offset,
            remainder => offset
                .checked_add(page_bytes - remainder)
                .ok_or_else(|| malformed("AFF4 page offset overflow"))?,
        };
        while let Some(page_end) = page_start.checked_add(page_bytes) {
            if page_end > request_end {
                break;
            }
            let begin = usize::try_from(page_start - offset)
                .map_err(|_| malformed("AFF4 page offset overflow"))?;
            let page = Arc::new(buffer[begin..begin + LOGICAL_READ_AHEAD_BYTES].to_vec());
            let key = self.disk_page_key(primary, id, page_start)?;
            let mut cache = self.volumes[primary]
                .decoded_cache
                .lock()
                .map_err(|_| malformed("AFF4 decoded cache lock poisoned"))?;
            cache.record_read_ahead_prefetch();
            cache.insert(key, page);
            page_start = page_end;
        }
        Ok(())
    }

    fn read_disk_at_impl(
        &mut self,
        primary: usize,
        mapped: bool,
        id: &str,
        buffer: &mut [u8],
        offset: u64,
        use_decoded_cache: bool,
    ) -> Result<usize> {
        if !mapped {
            self.clear_previous_cache(primary);
            return self.volumes[primary].read_at_impl(id, buffer, offset, use_decoded_cache);
        }
        let (map, size) = self.image_map_in(primary, id)?;
        if offset >= size {
            return Ok(0);
        }
        let length = (size - offset).min(buffer.len() as u64) as usize;
        let mut done = 0;
        while done < length {
            let position = offset + done as u64;
            let next = map.ranges.partition_point(|r| r.end <= position);
            let (target, start, end) = match map.ranges.get(next) {
                Some(range) if range.start <= position => (
                    range.target.as_str(),
                    range.offset + position - range.start,
                    range.end,
                ),
                next => (map.gap.as_str(), position, next.map_or(size, |r| r.start)),
            };
            let take = (end - position).min((length - done) as u64) as usize;
            let owner = if is_symbolic(target) {
                0
            } else {
                *self
                    .owners
                    .get(target)
                    .ok_or_else(|| malformed("missing target volume"))?
            };
            self.clear_previous_cache(owner);
            self.volumes[owner].read_inner(
                target,
                &mut buffer[done..done + take],
                start,
                &mut Vec::new(),
                use_decoded_cache,
            )?;
            done += take;
        }
        Ok(length)
    }

    fn clear_previous_cache(&mut self, owner: usize) {
        if self.cached_owner != Some(owner) {
            if let Some(previous) = self.cached_owner {
                self.volumes[previous].cache = None;
                self.volumes[previous].index_cache = None;
                self.volumes[previous].maps.clear();
            }
            self.cached_owner = Some(owner);
        }
    }

    /// Computes whole-image SHA256 across companions. This does not certify the
    /// per-volume metadata or striped composite hashes. Optional external digest
    /// must cover the assembled image, not information.turtle or a ZIP file.
    pub fn verify_image(
        &mut self,
        id: &str,
        expected_sha256: Option<&str>,
        progress: impl FnMut(u64, u64) -> ControlFlow<()>,
    ) -> Result<SetDigest> {
        self.scan_image(id, expected_sha256, false, progress)
            .map(|(digest, _)| digest)
    }

    fn scan_image(
        &mut self,
        id: &str,
        expected_sha256: Option<&str>,
        all_hashes: bool,
        mut progress: impl FnMut(u64, u64) -> ControlFlow<()>,
    ) -> Result<(SetDigest, Option<Verification>)> {
        if expected_sha256
            .is_some_and(|s| s.len() != 64 || !s.bytes().all(|b| b.is_ascii_hexdigit()))
        {
            return Err(malformed("invalid external SHA256"));
        }
        let size = self.size(id)?;
        if size > self.volumes[0].limits.verification_bytes {
            return Err(malformed("verification byte limit exceeded"));
        }
        let mut buffer = vec![0; 1024 * 1024];
        let mut offset = 0;
        let mut hash = Sha256::new();
        let mut extra = all_hashes.then(|| {
            (
                Md5::new(),
                Sha1::new(),
                sha2::Sha512::new(),
                blake2::Blake2b512::new(),
            )
        });
        loop {
            if progress(offset, size).is_break() {
                return Err(Error::Aborted);
            }
            if offset == size {
                break;
            }
            let read = self.read_disk_at_impl(0, true, id, &mut buffer, offset, false)?;
            if read == 0 {
                return Err(malformed("truncated volume set"));
            }
            hash.update(&buffer[..read]);
            if let Some((md5, sha1, sha512, blake2b)) = &mut extra {
                md5.update(&buffer[..read]);
                sha1.update(&buffer[..read]);
                sha512.update(&buffer[..read]);
                blake2b.update(&buffer[..read]);
            }
            offset += read as u64;
        }
        let sha256 = hex(&hash.finalize());
        let verification = extra.map(|(md5, sha1, sha512, blake2b)| Verification {
            bytes_verified: size,
            md5: hex(&md5.finalize()),
            sha1: hex(&sha1.finalize()),
            sha256: sha256.clone(),
            sha512: hex(&sha512.finalize()),
            blake2b: hex(&blake2b.finalize()),
            references_match: None,
            unsupported_hashes: Vec::new(),
        });
        Ok((
            SetDigest {
                image: id.into(),
                bytes: size,
                external_match: expected_sha256.map(|s| s.eq_ignore_ascii_case(&sha256)),
                sha256,
                sources: self.sources(),
            },
            verification,
        ))
    }
}

pub(super) fn is_symbolic(id: &str) -> bool {
    id == format!("{NS}Zero")
        || id.starts_with(&format!("{NS}SymbolicStream"))
        || id == format!("{NS}UnknownData")
        || id == format!("{NS}UnreadableData")
}

fn map_interval(map: &Map, position: u64, size: u64) -> (&str, u64, u64) {
    let next = map.ranges.partition_point(|range| range.end <= position);
    match map.ranges.get(next) {
        Some(range) if range.start <= position => (
            &range.target,
            range.offset + position - range.start,
            range.end,
        ),
        next => (&map.gap, position, next.map_or(size, |range| range.start)),
    }
}