structured-asic 0.21.0

Original-range ASiC containers and source-bound signature profile composition
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
//! Annex A.7 source-linked archive-manifest coverage and renewal graph.

use crate::{
    error::{Error as ArchiveError, resource},
    layout::{Census, Role},
    manifest::Boolean,
    xml::producer::Facts,
    zip::{Archive, MemberId},
};
use structured_asn1::{
    memory::{OwnedMap, OwnedVec, Work},
    range::RangeSource,
};

mod check;
mod inputs;
mod preservation;
pub use check::{Mathematics, ObjectsState, TimestampCheck, TimestampState};
pub use inputs::{AssertionInput, AssertionScope, InputAssociation, TimestampInputs};
pub use preservation::{ActorCheck, ActorResult, CoverageResult, RequirementsResult};

/// Readable source/coverage problems retain every original occurrence.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindingKind {
    /// The fixed latest filename is absent or ambiguous; no older fallback.
    Latest,
    /// This original archive manifest has no completed producing inspection.
    Uninspected,
    /// Original schema inspection has negative or unassessed findings.
    Syntax,
    /// The one signature reference does not select an actual timestamp member.
    Timestamp,
    /// An object URI is missing, malformed, fragmented or ambiguous.
    Target,
    /// Rootfile does not uniquely identify the immediately preceding manifest.
    Rootfile,
    /// The original predecessor sequence cycles.
    Cycle,
    /// An original archive-manifest occurrence was omitted from the sequence.
    Unreachable,
    /// A later manifest omits a prior object, manifest or timestamp.
    Dropped,
}

/// Original archive occurrence and optional exact instruction/related object.
#[derive(Debug, Clone, Copy)]
pub struct Finding<D, G> {
    /// Owning original archive manifest, absent only for latest-name selection.
    pub manifest: Option<MemberId<D, G>>,
    /// Original `DataObjectReference` ordinal, where applicable.
    pub reference: Option<usize>,
    /// Original missing/dropped/cyclic source occurrence.
    pub related: Option<MemberId<D, G>>,
    /// Completed structural result, never application trust.
    pub kind: FindingKind,
}

/// Caller/processing errors cannot become readable archive conformance faults.
#[derive(Debug)]
pub enum Error<E, D, G> {
    /// Actual source/current processing refused.
    Archive(ArchiveError<E>),
    /// More than one producing reading was supplied for one original occurrence.
    DuplicateFacts(MemberId<D, G>),
    /// A supplied reading belongs to another archive or a non-manifest member.
    ForeignFacts(MemberId<D, G>),
    /// Two actual timestamp receipts were supplied for the same original input.
    DuplicateTimestamp(MemberId<D, G>),
}
type ResultOf<T, S> = Result<
    T,
    Error<<S as RangeSource>::Error, <S as RangeSource>::Document, <S as RangeSource>::Generation>,
>;

/// One original occurrence, including uninspected/unreadable source positions.
pub struct Node<D, G> {
    member: MemberId<D, G>,
    facts: Option<usize>,
    previous: Option<usize>,
    timestamp: Option<MemberId<D, G>>,
    objects: Option<OwnedVec<Option<MemberId<D, G>>>>,
}
impl<D: Copy, G: Copy> Node<D, G> {
    /// Original manifest member identity, not its current filename spelling.
    #[must_use]
    pub fn member(&self) -> MemberId<D, G> {
        self.member
    }
    /// Original producing-facts index; None preserves a missing inspection.
    #[must_use]
    pub fn facts(&self) -> Option<usize> {
        self.facts
    }
    /// Exact predecessor node index, never CD/physical/success order.
    #[must_use]
    pub fn previous(&self) -> Option<usize> {
        self.previous
    }
    /// Exact original timestamp occurrence if the singleton URI resolved.
    #[must_use]
    pub fn timestamp(&self) -> Option<MemberId<D, G>> {
        self.timestamp
    }
    /// Every original object instruction, including unresolved positions.
    #[must_use]
    pub fn objects(&self) -> &[Option<MemberId<D, G>>] {
        match &self.objects {
            Some(objects) => objects,
            None => &[],
        }
    }
}

/// Full original A.7 graph. Positive geometry is independent of timestamp
/// mathematics, validation material, historical trust and algorithm longevity.
/// There is no fallback from an unreadable/bad latest to an earlier success.
pub struct Graph<'v, 's, N, D, G, S: RangeSource> {
    archive: &'v Archive<'s, S>,
    facts: OwnedVec<Facts<'v, 's, N, D, G, S>>,
    nodes: OwnedVec<Node<S::Document, S::Generation>>,
    order: OwnedVec<usize>,
    findings: OwnedVec<Finding<S::Document, S::Generation>>,
    latest: Option<usize>,
    covered: OwnedMap<usize, ()>,
}
impl<'v, 's, N: Copy, D: Copy + Eq, G: Copy + Eq, S: RangeSource> Graph<'v, 's, N, D, G, S> {
    /// Derives the complete sequence from original Rootfile links. A.7(1–2)
    /// requires each renewal to retain prior objects AND prior manifests/TSTs;
    /// renamed manifests may have arbitrary prefixes. Input order is irrelevant.
    /// <https://www.etsi.org/deliver/etsi_en/319100_319199/31916201/01.01.01_60/en_31916201v010101p.pdf>
    ///
    /// # Panics
    /// An internal immutable catalogue invariant fails if a bounded census or
    /// retained original-member ordinal cannot be selected from its catalogue.
    #[expect(
        clippy::too_many_lines,
        reason = "one transactional graph pass keeps source admission, predecessor cycle detection and cumulative prior coverage in their required order"
    )]
    pub fn inspect<W: Work>(
        census: &Census<'v, 's, S>,
        facts: OwnedVec<Facts<'v, 's, N, D, G, S>>,
        work: &W,
    ) -> ResultOf<Self, S> {
        const LATEST: &[u8] = b"META-INF/ASiCArchiveManifest.xml";
        let archive = census.archive();
        archive
            .admit(work, archive.directory_span())
            .map_err(Error::Archive)?;
        let execution = archive.session().query(work);
        let refusal = |cause| Error::Archive(resource(archive.directory_span(), cause));
        // Reject foreign sources before allocating the graph or invoking a
        // deferred target lookup. Independently retained storage is moved.
        for fact in &facts {
            execution.charge_work(1).map_err(refusal)?;
            if !core::ptr::eq(fact.catalogue().base().archive(), archive) {
                return Err(Error::ForeignFacts(fact.catalogue().base().id()));
            }
        }
        let mut by_source = execution.map();
        for (index, fact) in facts.iter().enumerate() {
            let id = fact.catalogue().base().id();
            if !matches!(
                census
                    .occurrence(id.ordinal(), &execution)
                    .map_err(Error::Archive)?
                    .map(|(_, role)| role),
                Some(Role::ArchiveManifest | Role::Manifest)
            ) {
                return Err(Error::ForeignFacts(id));
            }
            if by_source
                .try_insert(id.ordinal(), index)
                .map_err(refusal)?
                .is_some()
            {
                return Err(Error::DuplicateFacts(id));
            }
        }
        let mut nodes = execution.vector();
        let mut by_member = execution.map();
        let mut findings = execution.vector();
        let mut latest = None;
        let mut latest_count = 0usize;
        for ordinal in 0..census.len() {
            let (member, role) = census
                .occurrence(ordinal, &execution)
                .map_err(Error::Archive)?
                .expect("census length bounds its original ordinal");
            if role != Role::ArchiveManifest {
                continue;
            }
            let index = nodes.len();
            let id = member.id();
            execution.charge_work(1).map_err(refusal)?;
            if member.name().len() == LATEST.len() {
                execution
                    .charge_work(LATEST.len() as u64)
                    .map_err(refusal)?;
                if member.name() == LATEST {
                    latest_count += 1;
                    latest = Some(index);
                }
            }
            let fact = by_source
                .get(&ordinal, &execution)
                .map_err(refusal)?
                .copied();
            if fact.is_none() {
                findings
                    .try_push(Finding {
                        manifest: Some(id),
                        reference: None,
                        related: None,
                        kind: FindingKind::Uninspected,
                    })
                    .map_err(refusal)?;
            }
            nodes
                .try_push(Node {
                    member: id,
                    facts: fact,
                    previous: None,
                    timestamp: None,
                    objects: None,
                })
                .map_err(refusal)?;
            by_member.try_insert(ordinal, index).map_err(refusal)?;
        }
        if latest_count != 1 {
            latest = None;
            findings
                .try_push(Finding {
                    manifest: None,
                    reference: None,
                    related: None,
                    kind: FindingKind::Latest,
                })
                .map_err(refusal)?;
        }
        for node in &mut nodes {
            let Some(index) = node.facts else {
                continue;
            };
            let catalogue = facts[index].catalogue();
            let id = node.member;
            if !catalogue.reading().faults().is_empty() {
                findings
                    .try_push(Finding {
                        manifest: Some(id),
                        reference: None,
                        related: None,
                        kind: FindingKind::Syntax,
                    })
                    .map_err(refusal)?;
            }
            if let Some(token) = catalogue
                .signature_member(&execution)
                .map_err(Error::Archive)?
                && census
                    .occurrence(token.id().ordinal(), &execution)
                    .map_err(Error::Archive)?
                    .map(|(_, role)| role)
                    == Some(Role::Timestamp)
            {
                node.timestamp = Some(token.id());
            }
            if node.timestamp.is_none() {
                findings
                    .try_push(Finding {
                        manifest: Some(id),
                        reference: None,
                        related: None,
                        kind: FindingKind::Timestamp,
                    })
                    .map_err(refusal)?;
            }
            let mut objects = execution.vector();
            objects
                .try_reserve_exact(catalogue.objects().len())
                .map_err(refusal)?;
            let mut roots = 0usize;
            for (ordinal, object) in catalogue.reading().objects().iter().enumerate() {
                let target = catalogue
                    .object_member(ordinal, &execution)
                    .map_err(Error::Archive)?
                    .map(|member| member.id());
                objects.try_push(target).map_err(refusal)?;
                if target.is_none() {
                    findings
                        .try_push(Finding {
                            manifest: Some(id),
                            reference: Some(ordinal),
                            related: None,
                            kind: FindingKind::Target,
                        })
                        .map_err(refusal)?;
                }
                match object.rootfile {
                    Boolean::Value(true) => {
                        roots += 1;
                        let predecessor = match target {
                            Some(target) => by_member
                                .get(&target.ordinal(), &execution)
                                .map_err(refusal)?
                                .copied(),
                            None => None,
                        };
                        if predecessor.is_none() {
                            findings
                                .try_push(Finding {
                                    manifest: Some(id),
                                    reference: Some(ordinal),
                                    related: target,
                                    kind: FindingKind::Rootfile,
                                })
                                .map_err(refusal)?;
                        }
                        node.previous = predecessor;
                    }
                    Boolean::Malformed => {
                        findings
                            .try_push(Finding {
                                manifest: Some(id),
                                reference: Some(ordinal),
                                related: target,
                                kind: FindingKind::Rootfile,
                            })
                            .map_err(refusal)?;
                    }
                    Boolean::Absent | Boolean::Value(false) => {}
                }
            }
            if roots > 1 {
                node.previous = None;
                findings
                    .try_push(Finding {
                        manifest: Some(id),
                        reference: None,
                        related: None,
                        kind: FindingKind::Rootfile,
                    })
                    .map_err(refusal)?;
            }
            node.objects = Some(objects.into_owned());
        }
        let mut order = execution.vector();
        let mut visited = execution.map();
        let mut current = latest;
        while let Some(index) = current {
            execution.charge_work(1).map_err(refusal)?;
            if visited.try_insert(index, ()).map_err(refusal)?.is_some() {
                findings
                    .try_push(Finding {
                        manifest: Some(nodes[index].member),
                        reference: None,
                        related: Some(nodes[index].member),
                        kind: FindingKind::Cycle,
                    })
                    .map_err(refusal)?;
                break;
            }
            order.try_push(index).map_err(refusal)?;
            current = nodes[index].previous;
        }
        for (index, node) in nodes.iter().enumerate() {
            if visited.get(&index, &execution).map_err(refusal)?.is_none() {
                findings
                    .try_push(Finding {
                        manifest: Some(node.member),
                        reference: None,
                        related: None,
                        kind: FindingKind::Unreachable,
                    })
                    .map_err(refusal)?;
            }
        }
        execution.charge_work(order.len() as u64).map_err(refusal)?;
        order.reverse();
        let mut required = execution.map();
        let mut covered = execution.map();
        for index in &order {
            let node = &nodes[*index];
            let mut actual = execution.map();
            for target in node.objects().iter().flatten() {
                actual.try_insert(target.ordinal(), ()).map_err(refusal)?;
            }
            for (ordinal, ()) in required.try_iter(&execution).map_err(refusal)? {
                if actual.get(ordinal, &execution).map_err(refusal)?.is_none() {
                    let related = archive
                        .member(*ordinal, &execution)
                        .map_err(Error::Archive)?
                        .expect("required set only contains admitted original members")
                        .id();
                    findings
                        .try_push(Finding {
                            manifest: Some(node.member),
                            reference: None,
                            related: Some(related),
                            kind: FindingKind::Dropped,
                        })
                        .map_err(refusal)?;
                }
            }
            for target in node.objects().iter().flatten() {
                required.try_insert(target.ordinal(), ()).map_err(refusal)?;
            }
            required
                .try_insert(node.member.ordinal(), ())
                .map_err(refusal)?;
            if let Some(token) = node.timestamp {
                required.try_insert(token.ordinal(), ()).map_err(refusal)?;
            }
            covered = actual;
        }
        archive
            .admit(&execution, archive.directory_span())
            .map_err(Error::Archive)?;
        Ok(Self {
            archive,
            facts,
            nodes: nodes.into_owned(),
            order: order.into_owned(),
            findings: findings.into_owned(),
            latest,
            covered: covered.into_owned(),
        })
    }
    /// All original source-derived readings, moved without copying catalogues.
    #[must_use]
    pub fn facts(&self) -> &[Facts<'v, 's, N, D, G, S>] {
        &self.facts
    }
    /// Every original archive-manifest occurrence, including uninspected slots.
    #[must_use]
    pub fn nodes(&self) -> &[Node<S::Document, S::Generation>] {
        &self.nodes
    }
    /// Oldest-to-latest linked node indices. Findings expose any partial graph.
    #[must_use]
    pub fn order(&self) -> &[usize] {
        &self.order
    }
    /// Every original negative geometry/coverage outcome.
    #[must_use]
    pub fn findings(&self) -> &[Finding<S::Document, S::Generation>] {
        &self.findings
    }
    /// Exact fixed latest occurrence, never an earlier-success fallback.
    #[must_use]
    pub fn latest(&self) -> Option<usize> {
        self.latest
    }
    /// Complete source/layout geometry only, NOT mathematics/material/trust.
    #[must_use]
    pub fn geometry_complete(&self) -> bool {
        self.latest.is_some() && self.findings.is_empty()
    }
    /// Whether the latest original manifest declares every requested exact
    /// member. Requested long-term objects are caller input, not inferred from
    /// files introduced after an earlier timestamp. This does not check hashes.
    pub fn declares_all<W: Work>(
        &self,
        requested: &[MemberId<S::Document, S::Generation>],
        work: &W,
    ) -> Result<bool, ArchiveError<S::Error>> {
        let execution = self.archive.session().query(work);
        self.archive
            .admit(&execution, self.archive.directory_span())?;
        for id in requested {
            self.archive.original(*id, &execution)?;
            if self
                .covered
                .get(&id.ordinal(), &execution)
                .map_err(|cause| resource(self.archive.directory_span(), cause))?
                .is_none()
            {
                return Ok(false);
            }
        }
        Ok(self.latest.is_some())
    }
}