Skip to main content

aff4_image/
verify_all.rs

1//! Complete inventory of checks; unsupported constructions remain visible.
2use super::integrity::{absent, compare};
3use super::*;
4use sha2::Sha512;
5
6/// Byte provenance for one fully traversed resource. Counts sum to its size.
7#[derive(Debug, Clone, Default, serde::Serialize)]
8pub struct ByteCoverage {
9    /// Bytes read from ZIP members or ImageStreams.
10    pub stored: u64,
11    /// Bytes represented by inline metadata or explicit symbolic ranges.
12    pub described: u64,
13    /// Bytes supplied by a map's default stream for unmapped addresses.
14    pub gap_filled: u64,
15}
16
17/// Content traversal of one resource, kept separate from metadata checks.
18#[derive(Debug, Clone, serde::Serialize)]
19pub struct ResourceVerification {
20    /// Selected image, file, map, or storage stream.
21    pub resource: String,
22    /// Linear digests, when every byte was read successfully.
23    pub verification: Option<Verification>,
24    /// Provenance of the logical bytes; absent if traversal could not finish.
25    pub coverage: Option<ByteCoverage>,
26    /// Diagnostic for a traversal that could not complete.
27    pub error: Option<String>,
28}
29
30/// Container-wide inventory. No single matching digest substitutes for coverage.
31#[derive(Debug, Clone, serde::Serialize)]
32pub struct ContainerVerification {
33    /// Metadata integrity report when its hash files can be interpreted.
34    pub metadata: Option<MetadataVerification>,
35    /// Metadata verification failure, if any.
36    pub metadata_error: Option<String>,
37    /// Every supported data-bearing resource, including files without hashes.
38    pub resources: Vec<ResourceVerification>,
39    /// Recorded linear, structural, block, and composite references.
40    pub checks: Vec<IntegrityCheck>,
41}
42
43impl ContainerVerification {
44    /// True only with matching metadata and all inventoried content checks.
45    /// Metadata-only containers have no content checks to perform.
46    /// Does not establish authenticity without independently trusted references.
47    pub fn all_match(&self) -> bool {
48        self.metadata
49            .as_ref()
50            .is_some_and(MetadataVerification::all_match)
51            && self.metadata_error.is_none()
52            && self
53                .resources
54                .iter()
55                .all(|r| r.verification.is_some() && r.coverage.is_some())
56            && self.checks.iter().all(|c| c.outcome == CheckOutcome::Match)
57    }
58}
59
60pub(super) fn algorithm(p: &Property) -> &str {
61    let datatype = p.datatype.as_deref().unwrap_or("");
62    datatype.strip_prefix(NS).unwrap_or(datatype)
63}
64
65pub(super) fn declined(id: &str, p: &Property, error: &Error) -> IntegrityCheck {
66    IntegrityCheck {
67        resource: id.into(),
68        reference_source: p.predicate.clone(),
69        algorithm: algorithm(p).into(),
70        expected: Some(p.value.clone()),
71        computed: None,
72        outcome: if matches!(error, Error::Unsupported(_)) {
73            CheckOutcome::Unsupported
74        } else {
75            CheckOutcome::Unreadable
76        },
77        detail: Some(error.to_string()),
78    }
79}
80
81impl Container {
82    /// Checks metadata and all data resources, continuing after individual failures.
83    /// Progress cancellation aborts the entire operation. Unknown digest semantics
84    /// are reported as unsupported, never inferred from the digest length.
85    pub fn verify_all(
86        &mut self,
87        expected_metadata_sha256: Option<&str>,
88        mut progress: impl FnMut(&str, u64, u64) -> ControlFlow<()>,
89    ) -> Result<ContainerVerification> {
90        if progress("metadata", 0, 0).is_break() {
91            return Err(Error::Aborted);
92        }
93        let (metadata, metadata_error) = match self.verify_metadata(expected_metadata_sha256) {
94            Ok(value) => (Some(value), None),
95            Err(e) => (None, Some(e.to_string())),
96        };
97        let mut report = ContainerVerification {
98            metadata,
99            metadata_error,
100            resources: Vec::new(),
101            checks: Vec::new(),
102        };
103        let ids: Vec<_> = self
104            .streams()?
105            .into_iter()
106            .filter(|s| !self.has_type(&s.id, "Folder") && !self.has_type(&s.id, "FolderImage"))
107            .map(|s| s.id)
108            .collect();
109        let mut remaining = self.limits.verification_bytes;
110        let mut verified_backings = BTreeMap::<String, usize>::new();
111        let mut verified_blocks = BTreeMap::<String, PairedBlockDigests>::new();
112        for id in &ids {
113            let mut backing = None;
114            let value = self.size(id).and_then(|size| {
115                remaining = remaining
116                    .checked_sub(size)
117                    .ok_or_else(|| malformed("verification byte limit exceeded"))?;
118                let key = self.linear_backing(id)?;
119                backing = Some(key.clone());
120                if let Some(computed) = verified_backings
121                    .get(&key)
122                    .and_then(|index| report.resources[*index].verification.clone())
123                {
124                    // Direct dataStream wrappers have identical bytes, but each
125                    // resource still needs its own metadata validation, progress,
126                    // linear-reference comparisons, and coverage report.
127                    if progress(id, 0, size).is_break() {
128                        return Err(Error::Aborted);
129                    }
130                    self.read_inner(id, &mut [], 0, &mut Vec::new(), false)?;
131                    if progress(id, size, size).is_break() {
132                        return Err(Error::Aborted);
133                    }
134                    self.with_linear_references(id, computed)
135                } else {
136                    if let Some(block_size) = self.paired_block_size(&key) {
137                        let (verification, blocks) =
138                            self.verify_with_paired_blocks(id, block_size, |done, size| {
139                                progress(id, done, size)
140                            })?;
141                        verified_blocks.insert(key, blocks);
142                        Ok(verification)
143                    } else {
144                        self.verify(id, |done, size| progress(id, done, size))
145                    }
146                }
147            });
148            let (verification, coverage, error) = match value {
149                Err(Error::Aborted) => return Err(Error::Aborted),
150                Err(e) => (None, None, Some(e.to_string())),
151                Ok(value) => match self.byte_coverage(id, 0, value.bytes_verified, &mut Vec::new())
152                {
153                    Ok(coverage) => (Some(value), Some(coverage), None),
154                    Err(e) => (Some(value), None, Some(e.to_string())),
155                },
156            };
157            let hashes: Vec<_> = self
158                .properties(id)
159                .filter(|p| is_property(&p.predicate, "hash"))
160                .cloned()
161                .collect();
162            for p in &hashes {
163                if algorithm(p).starts_with("blockMapHash") {
164                    continue;
165                }
166                let computed = verification.as_ref().and_then(|v| match algorithm(p) {
167                    "MD5" => Some(&v.md5),
168                    "SHA1" => Some(&v.sha1),
169                    "SHA256" => Some(&v.sha256),
170                    "SHA512" => Some(&v.sha512),
171                    "Blake2b" | "blake2b" => Some(&v.blake2b),
172                    _ => None,
173                });
174                let mut check =
175                    declined(id, p, &Error::Unsupported("unknown linear digest".into()));
176                if let Some(value) = computed {
177                    check.computed = Some(value.clone());
178                    check.detail = None;
179                    check.outcome = if p.value.len() != value.len()
180                        || !p.value.bytes().all(|b| b.is_ascii_hexdigit())
181                    {
182                        CheckOutcome::Unreadable
183                    } else if value.eq_ignore_ascii_case(&p.value) {
184                        CheckOutcome::Match
185                    } else {
186                        CheckOutcome::Mismatch
187                    };
188                } else if verification.is_none() {
189                    check.outcome = CheckOutcome::Unreadable;
190                    check.detail = error.clone();
191                }
192                report.checks.push(check);
193            }
194            // Do not require a redundant linear hash on a Map or an ImageStream
195            // when a composite protects it, but do retain missing references on files.
196            if hashes.is_empty()
197                && (self.has_type(id, "FileImage") || self.has_type(id, "FileSubStream"))
198                && self.inline_data(id)?.is_none()
199            {
200                report.checks.push(absent(id, "linear file digest"));
201            }
202            let index = report.resources.len();
203            if coverage.is_some()
204                && let Some(key) = backing
205            {
206                verified_backings.entry(key).or_insert(index);
207            }
208            report.resources.push(ResourceVerification {
209                resource: id.clone(),
210                verification,
211                coverage,
212                error,
213            });
214        }
215        let subjects: Vec<_> = self.graph.keys().cloned().collect();
216        for id in subjects {
217            if progress(&id, 0, 0).is_break() {
218                return Err(Error::Aborted);
219            }
220            let properties = self.graph[&id].clone();
221            let block_hash_object = self.has_type(&id, "BlockHashes");
222            for p in properties.iter().filter(|p| {
223                is_property(&p.predicate, "hash")
224                    && (block_hash_object || algorithm(p).starts_with("blockMapHash"))
225                    || [
226                        "mapHash",
227                        "mapIdxHash",
228                        "mapPointHash",
229                        "mapPathHash",
230                        "blockMapHash",
231                        "imageStreamHash",
232                        "imageStreamIndexHash",
233                    ]
234                    .iter()
235                    .any(|key| is_property(&p.predicate, key))
236            }) {
237                let check = match self.structural_input(&id, p) {
238                    Ok((bytes, alg)) => compare(&id, &p.predicate, &alg, &p.value, &bytes),
239                    Err(e) => declined(&id, p, &e),
240                };
241                report.checks.push(check);
242            }
243            if self.has_type(&id, "Map")
244                && !properties.iter().any(|p| {
245                    is_property(&p.predicate, "hash")
246                        || is_property(&p.predicate, "mapHash")
247                        || is_property(&p.predicate, "blockMapHash")
248                })
249            {
250                let path = self.path(&id)?;
251                for (member, key) in [
252                    ("map", "mapPointHash"),
253                    ("idx", "mapIdxHash"),
254                    ("mapPath", "mapPathHash"),
255                ] {
256                    if (member != "mapPath"
257                        || self
258                            .archive
259                            .index_for_name(&format!("{path}/{member}"))
260                            .is_some())
261                        && !properties.iter().any(|p| is_property(&p.predicate, key))
262                    {
263                        report.checks.push(absent(&id, key));
264                    }
265                }
266            }
267            if self.has_type(&id, "ImageStream") {
268                match self.check_blocks(
269                    &id,
270                    &mut remaining,
271                    &mut progress,
272                    verified_blocks.get(&id),
273                ) {
274                    Ok(checks) => {
275                        if checks.is_empty()
276                            && !self
277                                .properties(&id)
278                                .any(|p| is_property(&p.predicate, "hash"))
279                        {
280                            report
281                                .checks
282                                .push(absent(&id, "linear or block content digest"));
283                        }
284                        report.checks.extend(checks);
285                    }
286                    Err(Error::Aborted) => return Err(Error::Aborted),
287                    Err(e) => {
288                        let p = Property {
289                            predicate: "block hashes".into(),
290                            value: String::new(),
291                            datatype: None,
292                            language: None,
293                        };
294                        report.checks.push(declined(&id, &p, &e));
295                    }
296                }
297            }
298        }
299        Ok(report)
300    }
301
302    fn byte_coverage(
303        &mut self,
304        id: &str,
305        offset: u64,
306        length: u64,
307        visited: &mut Vec<String>,
308    ) -> Result<ByteCoverage> {
309        enter(id, visited)?;
310        let result = (|| {
311            let mut count = ByteCoverage::default();
312            if id == format!("{NS}Zero")
313                || id.starts_with(&format!("{NS}SymbolicStream"))
314                || self.inline_data(id)?.is_some()
315            {
316                count.described = length;
317            } else if let Some(target) = self.value(id, "dataStream")? {
318                return self.byte_coverage(&target, offset, length, visited);
319            } else if self.has_type(id, "Map") {
320                self.load_map(id)?;
321                let map = self.maps[id].clone();
322                let mut position = offset;
323                let end = offset
324                    .checked_add(length)
325                    .ok_or_else(|| malformed("coverage overflow"))?;
326                while position < end {
327                    let next = map.ranges.partition_point(|r| r.end <= position);
328                    if let Some(range) = map.ranges.get(next).filter(|r| r.start <= position) {
329                        let take = (range.end - position).min(end - position);
330                        let child = self.byte_coverage(
331                            &range.target,
332                            range.offset + position - range.start,
333                            take,
334                            visited,
335                        )?;
336                        count.stored += child.stored;
337                        count.described += child.described;
338                        count.gap_filled += child.gap_filled;
339                        position += take;
340                    } else {
341                        let take =
342                            map.ranges.get(next).map_or(end, |r| r.start.min(end)) - position;
343                        count.gap_filled += take;
344                        position += take;
345                    }
346                }
347            } else {
348                count.stored = length;
349            }
350            Ok(count)
351        })();
352        visited.pop();
353        result
354    }
355
356    fn block_names(&self, id: &str, suffix: &str) -> Result<Vec<String>> {
357        let prefix = format!("{}/", self.path(id)?);
358        let mut names: BTreeMap<u64, String> = BTreeMap::new();
359        for name in self.archive.file_names() {
360            let Some(tail) = name.strip_prefix(&prefix) else {
361                continue;
362            };
363            let ordinal = tail
364                .strip_suffix(&format!(".blockHash.{suffix}"))
365                .or_else(|| tail.strip_suffix(&format!(".{suffix}")));
366            if let Some(number) =
367                ordinal.filter(|n| n.len() == 8 && n.bytes().all(|b| b.is_ascii_digit()))
368            {
369                let number = number
370                    .parse()
371                    .map_err(|_| malformed("invalid block bevy"))?;
372                if names.insert(number, name.into()).is_some() {
373                    return Err(malformed("ambiguous block-hash segment aliases"));
374                }
375            }
376        }
377        Ok(names.into_values().collect())
378    }
379
380    fn join_members(&mut self, names: &[String]) -> Result<Vec<u8>> {
381        let mut result = Vec::new();
382        for name in names {
383            let limit = self.limits.member_bytes.saturating_sub(result.len() as u64);
384            result.extend_from_slice(&member(&mut self.archive, name, limit)?);
385        }
386        Ok(result)
387    }
388
389    pub(super) fn structural_input(&mut self, id: &str, p: &Property) -> Result<(Vec<u8>, String)> {
390        self.structural_input_scoped(id, p, None)
391    }
392
393    pub(super) fn structural_input_scoped(
394        &mut self,
395        id: &str,
396        p: &Property,
397        local: Option<&BTreeSet<String>>,
398    ) -> Result<(Vec<u8>, String)> {
399        let alg = algorithm(p);
400        if self.has_type(id, "BlockHashes") {
401            let (stream, suffix) = id
402                .rsplit_once("/blockhash.")
403                .ok_or_else(|| malformed("invalid BlockHashes resource"))?;
404            let names = self.block_names(stream, suffix)?;
405            if names.is_empty() {
406                return Err(malformed("missing block hash members"));
407            }
408            return Ok((self.join_members(&names)?, alg.into()));
409        }
410        if is_property(&p.predicate, "imageStreamIndexHash") {
411            let size = self.number(id, "size")?;
412            let chunk = self.number(id, "chunkSize")?;
413            let per = self.number(id, "chunksInSegment")?;
414            if chunk == 0 || per == 0 {
415                return Err(malformed("invalid chunk geometry"));
416            }
417            let count = size.div_ceil(chunk).div_ceil(per);
418            if count > self.archive.len() as u64 {
419                return Err(malformed("missing bevies"));
420            }
421            let path = self.path(id)?;
422            let names: Vec<_> = (0..count).map(|n| format!("{path}/{n:08}.index")).collect();
423            return Ok((self.join_members(&names)?, alg.into()));
424        }
425        if is_property(&p.predicate, "imageStreamHash") {
426            return Err(Error::Unsupported(
427                "imageStreamHash semantics require an independent vector".into(),
428            ));
429        }
430        let map = if self.has_type(id, "Map") {
431            id.to_owned()
432        } else {
433            self.value(id, "dataStream")?
434                .ok_or_else(|| malformed("missing image map"))?
435        };
436        if !self.has_type(&map, "Map") {
437            return Err(Error::Unsupported("composite digest requires a Map".into()));
438        }
439        let path = self.path(&map)?;
440        let points = member(
441            &mut self.archive,
442            &format!("{path}/map"),
443            self.limits.member_bytes,
444        )?;
445        let index = member(
446            &mut self.archive,
447            &format!("{path}/idx"),
448            self.limits.member_bytes,
449        )?;
450        let path_name = format!("{path}/mapPath");
451        let order = if self.archive.index_for_name(&path_name).is_some() {
452            member(&mut self.archive, &path_name, self.limits.member_bytes)?
453        } else {
454            Vec::new()
455        };
456        for (key, bytes) in [
457            ("mapPointHash", &points),
458            ("mapIdxHash", &index),
459            ("mapPathHash", &order),
460        ] {
461            if is_property(&p.predicate, key) {
462                return Ok((bytes.clone(), alg.into()));
463            }
464        }
465        if is_property(&p.predicate, "mapHash") {
466            return Ok(([points, index, order].concat(), alg.into()));
467        }
468        let outer = match alg {
469            "blockMapHashSHA512" | "SHA512" => "SHA512",
470            "blockMapHashSHA256" | "SHA256" => "SHA256",
471            _ => return Err(Error::Unsupported("block map digest algorithm".into())),
472        };
473        let digest = |bytes: &[u8]| -> Vec<u8> {
474            if outer == "SHA512" {
475                Sha512::digest(bytes).to_vec()
476            } else {
477                Sha256::digest(bytes).to_vec()
478            }
479        };
480        let targets = std::str::from_utf8(&index).map_err(|_| malformed("map index UTF8"))?;
481        let mut input = Vec::new();
482        let mut seen = BTreeSet::new();
483        for target in targets.lines() {
484            if !self.has_type(target, "ImageStream") {
485                if target == format!("{NS}Zero")
486                    || target.starts_with(&format!("{NS}SymbolicStream"))
487                {
488                    continue;
489                }
490                return Err(Error::Unsupported(
491                    "nested or external block map target".into(),
492                ));
493            }
494            if local.is_some_and(|streams| !streams.contains(target)) {
495                continue;
496            }
497            if self
498                .value(target, "stored")?
499                .is_some_and(|owner| owner != self.volume)
500            {
501                return Err(Error::Unsupported(
502                    "cross-volume composite block map digest".into(),
503                ));
504            }
505            if !seen.insert(target) {
506                continue;
507            }
508            for suffix in ["md5", "sha1", "sha256", "sha512", "blake2b"] {
509                let names = self.block_names(target, suffix)?;
510                if !names.is_empty() {
511                    input.extend(digest(&self.join_members(&names)?));
512                }
513            }
514        }
515        input.extend(digest(&points));
516        input.extend(digest(&index));
517        // Canonical v1 images include H(empty) when mapPath is absent.
518        input.extend(digest(&order));
519        Ok((input, outer.into()))
520    }
521
522    fn check_blocks(
523        &mut self,
524        id: &str,
525        remaining: &mut u64,
526        progress: &mut impl FnMut(&str, u64, u64) -> ControlFlow<()>,
527        verified: Option<&PairedBlockDigests>,
528    ) -> Result<Vec<IntegrityCheck>> {
529        self.check_blocks_from(id, None, remaining, progress, verified)
530    }
531
532    pub(super) fn check_blocks_from(
533        &mut self,
534        id: &str,
535        mut source: Option<&mut Self>,
536        remaining: &mut u64,
537        progress: &mut impl FnMut(&str, u64, u64) -> ControlFlow<()>,
538        verified: Option<&PairedBlockDigests>,
539    ) -> Result<Vec<IntegrityCheck>> {
540        let mut checks = Vec::new();
541        let geometry = source.as_deref().unwrap_or(self);
542        let size = geometry.number(id, "size")?;
543        let chunk = geometry.number(id, "chunkSize")?;
544        let per = geometry.number(id, "chunksInSegment")?;
545        if chunk == 0 || chunk > self.limits.chunk_bytes || per == 0 {
546            return Err(malformed("invalid block hash geometry"));
547        }
548        let count = size.div_ceil(chunk);
549        let mut buffer = chunk_buffer(chunk as usize)?;
550        // Writer-produced streams carry these two block digests. Compare both
551        // while each decoded chunk is in memory instead of decoding it twice.
552        let md5_names = self.block_names(id, "md5")?;
553        let sha256_names = self.block_names(id, "sha256")?;
554        if !md5_names.is_empty()
555            && !sha256_names.is_empty()
556            && ["sha1", "sha512", "blake2b"].iter().all(|suffix| {
557                self.block_names(id, suffix)
558                    .is_ok_and(|names| names.is_empty())
559            })
560            && count
561                .min(per)
562                .checked_mul(16 + 32)
563                .is_some_and(|bytes| bytes <= self.limits.member_bytes)
564        {
565            return self.check_md5_sha256_blocks(
566                id,
567                source,
568                remaining,
569                progress,
570                size,
571                chunk,
572                per,
573                count,
574                &mut buffer,
575                &md5_names,
576                &sha256_names,
577                verified,
578            );
579        }
580        for (suffix, algorithm, width) in [
581            ("md5", "MD5", 16),
582            ("sha1", "SHA1", 20),
583            ("sha256", "SHA256", 32),
584            ("sha512", "SHA512", 64),
585            ("blake2b", "Blake2b", 64),
586        ] {
587            let names = self.block_names(id, suffix)?;
588            if names.is_empty() {
589                continue;
590            }
591            if names.len() as u64 != count.div_ceil(per) {
592                return Err(malformed("incomplete block hash bevy set"));
593            }
594            for (bevy, name) in names.iter().enumerate() {
595                let ordinal = name.rsplit('/').next().unwrap()[..8]
596                    .parse::<u64>()
597                    .map_err(|_| malformed("block bevy number"))?;
598                if ordinal != bevy as u64 {
599                    return Err(malformed("block hash bevy gap"));
600                }
601                let recorded = member(&mut self.archive, name, self.limits.member_bytes)?;
602                let first = bevy as u64 * per;
603                let blocks = (count - first).min(per);
604                if recorded.len() as u64 != blocks * width {
605                    return Err(malformed("block hash count mismatch"));
606                }
607                let mut check = absent(id, name);
608                check.algorithm = algorithm.into();
609                check.outcome = CheckOutcome::Match;
610                check.detail = None;
611                check.expected = Some(format!("{blocks} block digests"));
612                check.computed = check.expected.clone();
613                for n in 0..blocks {
614                    let offset = (first + n) * chunk;
615                    if progress(id, offset, size).is_break() {
616                        return Err(Error::Aborted);
617                    }
618                    let take = (size - offset).min(chunk) as usize;
619                    *remaining = remaining
620                        .checked_sub(take as u64)
621                        .ok_or_else(|| malformed("verification byte limit exceeded"))?;
622                    if let Some(reader) = source.as_deref_mut() {
623                        reader.read_at_uncached(id, &mut buffer[..take], offset)?;
624                    } else {
625                        self.read_at_uncached(id, &mut buffer[..take], offset)?;
626                    }
627                    let start = (n * width) as usize;
628                    let expected = hex(&recorded[start..start + width as usize]);
629                    let value = compare(id, name, algorithm, &expected, &buffer[..take]);
630                    if value.outcome != CheckOutcome::Match {
631                        check.outcome = value.outcome;
632                        if check.detail.is_none() {
633                            check.detail = Some(format!(
634                                "first mismatch at chunk {} (offset {offset})",
635                                first + n
636                            ));
637                        }
638                    }
639                }
640                checks.push(check);
641            }
642        }
643        Ok(checks)
644    }
645
646    fn paired_block_size(&mut self, id: &str) -> Option<usize> {
647        if !self.has_type(id, "ImageStream") {
648            return None;
649        }
650        let size = self.number(id, "size").ok()?;
651        let chunk = self.number(id, "chunkSize").ok()?;
652        let per = self.number(id, "chunksInSegment").ok()?;
653        if chunk == 0 || chunk > self.limits.chunk_bytes || per == 0 {
654            return None;
655        }
656        if self.block_names(id, "md5").ok()?.is_empty()
657            || self.block_names(id, "sha256").ok()?.is_empty()
658            || ["sha1", "sha512", "blake2b"].iter().any(|suffix| {
659                !self
660                    .block_names(id, suffix)
661                    .is_ok_and(|names| names.is_empty())
662            })
663            || size
664                .div_ceil(chunk)
665                .min(per)
666                .checked_mul(16 + 32)
667                .is_none_or(|bytes| bytes > self.limits.member_bytes)
668        {
669            return None;
670        }
671        usize::try_from(chunk).ok()
672    }
673
674    #[allow(clippy::too_many_arguments)]
675    fn check_md5_sha256_blocks(
676        &mut self,
677        id: &str,
678        mut source: Option<&mut Self>,
679        remaining: &mut u64,
680        progress: &mut impl FnMut(&str, u64, u64) -> ControlFlow<()>,
681        size: u64,
682        chunk: u64,
683        per: u64,
684        count: u64,
685        buffer: &mut [u8],
686        md5_names: &[String],
687        sha256_names: &[String],
688        verified: Option<&PairedBlockDigests>,
689    ) -> Result<Vec<IntegrityCheck>> {
690        let bevies = count.div_ceil(per);
691        if md5_names.len() as u64 != bevies || sha256_names.len() as u64 != bevies {
692            return Err(malformed("incomplete block hash bevy set"));
693        }
694        if verified.is_some_and(|verified| {
695            verified.md5.len() as u64 != count || verified.sha256.len() as u64 != count
696        }) {
697            return Err(malformed("verified block digest count mismatch"));
698        }
699        let mut md5_checks = Vec::with_capacity(md5_names.len());
700        let mut sha256_checks = Vec::with_capacity(sha256_names.len());
701        for bevy in 0..bevies {
702            let first = bevy * per;
703            let blocks = (count - first).min(per);
704            let mut recorded = Vec::with_capacity(2);
705            for (names, algorithm, width) in [
706                (md5_names, "MD5", 16usize),
707                (sha256_names, "SHA256", 32usize),
708            ] {
709                let name = &names[bevy as usize];
710                let ordinal = name.rsplit('/').next().unwrap()[..8]
711                    .parse::<u64>()
712                    .map_err(|_| malformed("block bevy number"))?;
713                if ordinal != bevy {
714                    return Err(malformed("block hash bevy gap"));
715                }
716                let bytes = member(&mut self.archive, name, self.limits.member_bytes)?;
717                if bytes.len() as u64 != blocks * width as u64 {
718                    return Err(malformed("block hash count mismatch"));
719                }
720                let mut check = absent(id, name);
721                check.algorithm = algorithm.into();
722                check.outcome = CheckOutcome::Match;
723                check.detail = None;
724                check.expected = Some(format!("{blocks} block digests"));
725                check.computed = check.expected.clone();
726                recorded.push((name, algorithm, width, bytes, check));
727            }
728            for n in 0..blocks {
729                let offset = (first + n) * chunk;
730                let take = (size - offset).min(chunk) as usize;
731                // Preserve the existing work limit and cancellation points for
732                // both logical block checks even though only one read occurs.
733                for _ in 0..2 {
734                    if progress(id, offset, size).is_break() {
735                        return Err(Error::Aborted);
736                    }
737                    *remaining = remaining
738                        .checked_sub(take as u64)
739                        .ok_or_else(|| malformed("verification byte limit exceeded"))?;
740                }
741                if verified.is_none() {
742                    if let Some(reader) = source.as_deref_mut() {
743                        reader.read_at_uncached(id, &mut buffer[..take], offset)?;
744                    } else {
745                        self.read_at_uncached(id, &mut buffer[..take], offset)?;
746                    }
747                }
748                for (name, algorithm, width, bytes, check) in &mut recorded {
749                    let start = n as usize * *width;
750                    let outcome = if let Some(verified) = verified {
751                        let index = usize::try_from(first + n)
752                            .map_err(|_| malformed("block index does not fit memory"))?;
753                        let computed: &[u8] = if *algorithm == "MD5" {
754                            &verified.md5[index]
755                        } else {
756                            &verified.sha256[index]
757                        };
758                        if bytes[start..start + *width] == *computed {
759                            CheckOutcome::Match
760                        } else {
761                            CheckOutcome::Mismatch
762                        }
763                    } else {
764                        let expected = hex(&bytes[start..start + *width]);
765                        compare(id, name, algorithm, &expected, &buffer[..take]).outcome
766                    };
767                    if outcome != CheckOutcome::Match {
768                        check.outcome = outcome;
769                        if check.detail.is_none() {
770                            check.detail = Some(format!(
771                                "first mismatch at chunk {} (offset {offset})",
772                                first + n
773                            ));
774                        }
775                    }
776                }
777            }
778            md5_checks.push(recorded.remove(0).4);
779            sha256_checks.push(recorded.remove(0).4);
780        }
781        md5_checks.extend(sha256_checks);
782        Ok(md5_checks)
783    }
784}