structured-xades 0.21.0

XAdES mechanisms over caller-owned XML and explicit resource accounting
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
//! Streaming XML digest checks through the caller's selected provider.
//!
//! Execution is the provider's [`structured_asn1::digest::Digest`]
//! implementation; [`DigestProvider`] adds only the XML method resolution and
//! classification. No independent URI dispatcher or implicit backend is
//! selected by this crate.

use crate::binary::{self, BinaryXml, Location, OwnedBinaryXml};
use crate::certificate::{self, Agreement, DigestProvider};
use crate::xml::{Access, Snapshot, Xml};
use structured_asn1::digest::{Digest, Resolution};
use structured_asn1::memory::{MemoryError, OwnedVec, Work};
use structured_cms::stream::{self, Sink, Source, StreamError};

mod batch;
pub use batch::Batch;

/// Original digest method/value selectors, NOT proof of their signed coverage.
/// The enclosing property mechanism supplies its actual source-bound nodes.
#[derive(Debug, Clone, Copy)]
pub struct Reference<N, D, G> {
    /// Original XML document and generation.
    pub snapshot: Snapshot<D, G>,
    /// Original method including untouched parameters.
    pub method: N,
    /// Original base64 digest-value element.
    pub value: N,
}

/// Execution failure stays independent of completed digest agreement.
#[derive(Debug)]
pub enum Error<SE, PE> {
    /// A prepared XML method was paired with another provider's output.
    ForeignDigest,
    /// The provider resolved an identity but cannot stream its digest state.
    Unstreamable,
    /// Original complete-source acquisition/replay refusal.
    Source(SE),
    /// Original selected-provider failure, including resource/policy refusal.
    Provider(PE),
    /// Driver metadata admission failed before callback execution.
    Resource(MemoryError),
    /// Existing stream driver refused dispatch. Its location is the virtual
    /// input's start (zero), NOT a physical XML byte or decoded ASN.1 offset;
    /// the outer binary error preserves the actual XML value node/generation.
    Stream(structured_cms::Error),
    /// Backend output contradicts its already resolved fixed width. This is
    /// processing failure, not malformed declared bytes or a normal mismatch.
    OutputWidth {
        /// Fixed resolved width.
        expected: usize,
        /// Actual backend output width.
        actual: usize,
    },
}

impl<SE, PE> Error<SE, PE> {
    /// Translates only acquisition causes, preserving provider, processing and
    /// resource classifications without replacing their original locations.
    pub fn map_source<E>(self, map: impl FnOnce(SE) -> E) -> Error<E, PE> {
        match self {
            Self::ForeignDigest => Error::ForeignDigest,
            Self::Unstreamable => Error::Unstreamable,
            Self::Source(cause) => Error::Source(map(cause)),
            Self::Provider(cause) => Error::Provider(cause),
            Self::Resource(cause) => Error::Resource(cause),
            Self::Stream(cause) => Error::Stream(cause),
            Self::OutputWidth { expected, actual } => Error::OutputWidth { expected, actual },
        }
    }
}

struct Updating<'s, 'p, P: Digest<'p>> {
    provider: &'p P,
    state: &'s mut P::State,
}

impl<'p, P: Digest<'p>> Sink for Updating<'_, 'p, P> {
    type Error = P::Error;
    fn write<W: Work>(&mut self, bytes: &[u8], execution: &W) -> Result<(), Self::Error> {
        self.provider.update(self.state, bytes, execution)
    }
}

/// Resolves the actual XML method, validates declared width before source I/O,
/// then runs one exact source pass through the existing sticky stream driver.
/// No whole-input collector, parser, implicit I/O or runtime crypto dependency
/// is introduced. This produces digest agreement only, never signed coverage,
/// policy-document acceptance, timestamp evidence or an attained B level.
pub fn check<'p, X: BinaryXml, P: DigestProvider<'p, X>, S: Source, W: Work>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> binary::BinaryResult<Agreement, X, Error<S::Error, P::Error>> {
    check_with(
        xml,
        reference,
        provider,
        execution,
        |identity, expected, work| {
            run_resolved::<P, S, W>(&identity, expected, source, provider, work)
        },
    )
    .map(|reading| match reading {
        Checked::Answer(answer) | Checked::Value(answer) => answer,
    })
}

/// Same method/width/algorithm checks over a source that may suspend. The XML
/// owner moves its funded decoded value into this scope rather than copying a
/// callback-local digest into a second allocation before awaiting input.
pub async fn check_async<'p, X: OwnedBinaryXml, P: DigestProvider<'p, X>, S, W: Work>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> binary::BinaryResult<Agreement, X, Error<S::Error, P::Error>>
where
    S: stream::AsyncSource,
{
    check_async_with::<true, X, P, S, W>(xml, reference, source, provider, execution).await
}

/// `XMLDSig` digest checking without selecting an `XAdES` baseline profile. The
/// format using this result supplies its own profile requirements; selected
/// provider algorithm/policy validation is still mandatory before source I/O.
pub async fn check_xml_async<'p, X: OwnedBinaryXml, P: DigestProvider<'p, X>, S, W: Work>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> binary::BinaryResult<Agreement, X, Error<S::Error, P::Error>>
where
    S: stream::AsyncSource,
{
    check_async_with::<false, X, P, S, W>(xml, reference, source, provider, execution).await
}

/// Compares an original XML method/value with an actual already completed
/// common digest. Full semantic identity must agree before output comparison;
/// output width or matching bytes alone cannot establish that association.
/// The actual output supplies its own provider. No state, output or identity
/// is cloned, no resolver is replaced and no source is hashed again.
/// This general XML path does not select an `XAdES` baseline algorithm policy.
pub fn check_xml_finished<'p, X: BinaryXml, P: DigestProvider<'p, X>, W: Work>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    actual: structured_cms::provider::Finished<'_, P, P::Identity, P::Output>,
    execution: &W,
) -> binary::BinaryResult<Agreement, X, Error<core::convert::Infallible, P::Error>> {
    let access = Access::new(xml, reference.snapshot, execution);
    if let Some(answer) = preflight(&access, reference)? {
        return Ok(answer);
    }
    let location = Location {
        snapshot: reference.snapshot,
        node: reference.value,
    };
    binary::with_octets(xml, location, execution, |expected| {
        Ok::<_, core::convert::Infallible>(compare_finished(&access, reference, actual, expected))
    })
    .map_err(|error| error.map_callback(|never| match never {}))?
}

fn compare_finished<'p, X: BinaryXml, P: DigestProvider<'p, X>, W: Work>(
    access: &Access<'_, X, W>,
    reference: Reference<X::Id, X::Document, X::Generation>,
    actual: structured_cms::provider::Finished<'_, P, P::Identity, P::Output>,
    expected: &[u8],
) -> binary::BinaryResult<Agreement, X, Error<core::convert::Infallible, P::Error>> {
    let provider = actual.provider();
    let identity = match resolve_method(access, reference, provider)? {
        Resolution::Supported(identity) => identity,
        Resolution::Unsupported => return Ok(Agreement::Unsupported),
        Resolution::Malformed => return Ok(Agreement::MalformedMethod),
    };
    let identity = match certificate::resolved_digest::<false, X, P>(identity, provider) {
        Ok(identity) => identity,
        Err(answer) => return Ok(answer),
    };
    compare_resolved_finished(access, reference, provider, &identity, actual, expected)
}

fn compare_resolved_finished<'p, X: Xml, P: Digest<'p>, W: Work>(
    access: &Access<'_, X, W>,
    reference: Reference<X::Id, X::Document, X::Generation>,
    provider: &P,
    identity: &P::Identity,
    actual: structured_cms::provider::Finished<'_, P, P::Identity, P::Output>,
    expected: &[u8],
) -> binary::BinaryResult<Agreement, X, Error<core::convert::Infallible, P::Error>> {
    let width = provider.width(identity);
    if let Some(width) = width
        && expected.len() != width
    {
        return Ok(Agreement::MalformedValue);
    }
    let location = Location {
        snapshot: reference.snapshot,
        node: reference.value,
    };
    access
        .query_fallible(reference.value, |_, _, work| {
            work.charge_work(1).map_err(Error::Resource)?;
            if provider
                .compare(identity, actual.identity(), work)
                .map_err(Error::Provider)?
                != core::cmp::Ordering::Equal
            {
                return Ok(Agreement::Differs);
            }
            if let Some(width) = width
                && actual.output().as_ref().len() != width
            {
                return Err(Error::OutputWidth {
                    expected: width,
                    actual: actual.output().as_ref().len(),
                });
            }
            let agrees = provider
                .matches(identity, actual.output(), expected, work)
                .map_err(Error::Provider)?;
            Ok(if agrees {
                Agreement::Matches
            } else {
                Agreement::Differs
            })
        })
        .map_err(|cause| match cause {
            crate::xml::AccessError::Xml(cause) => binary::Error::Callback { location, cause },
            crate::xml::AccessError::Changed { node } => binary::Error::Access {
                location,
                cause: crate::xml::AccessError::Changed { node },
            },
            crate::xml::AccessError::Resource { node, cause } => binary::Error::Access {
                location,
                cause: crate::xml::AccessError::Resource { node, cause },
            },
        })
}

enum PreparedValue<I> {
    Answer(Agreement),
    Ready { identity: I, expected: OwnedVec<u8> },
}

/// Original XML method/value admitted and resolved before asynchronous input
/// or provider state creation. Only the declared digest bytes are retained;
/// this is not a finished digest or evidence of signed coverage.
pub struct Prepared<'p, P: Digest<'p>, N, D, G> {
    provider: &'p P,
    reference: Reference<N, D, G>,
    value: PreparedValue<P::Identity>,
}

/// Resolves and checks an original general XML digest instruction before a
/// separate shared all-algorithm pass. Malformed/unsupported readings remain
/// values; XML, policy and resource refusals remain terminal original causes.
/// Baseline algorithm restrictions belong to the enclosing profile.
pub type PreparationResult<'p, P, X> = binary::BinaryResult<
    Prepared<'p, P, <X as Xml>::Id, <X as Xml>::Document, <X as Xml>::Generation>,
    X,
    Error<core::convert::Infallible, <P as Digest<'p>>::Error>,
>;

/// Checks original syntax and provider policy before creating digest state.
pub fn prepare_xml<'p, X: OwnedBinaryXml, P: DigestProvider<'p, X>, W: Work>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    provider: &'p P,
    execution: &W,
) -> PreparationResult<'p, P, X> {
    let access = Access::new(xml, reference.snapshot, execution);
    let value = if let Some(answer) = preflight(&access, reference)? {
        PreparedValue::Answer(answer)
    } else {
        match resolve_method(&access, reference, provider)? {
            Resolution::Unsupported => PreparedValue::Answer(Agreement::Unsupported),
            Resolution::Malformed => PreparedValue::Answer(Agreement::MalformedMethod),
            Resolution::Supported(identity) => {
                match certificate::resolved_digest::<false, X, P>(identity, provider) {
                    Err(answer) => PreparedValue::Answer(answer),
                    Ok(identity) => {
                        let location = Location {
                            snapshot: reference.snapshot,
                            node: reference.value,
                        };
                        let expected = binary::owned(xml, location, execution)
                            .map_err(|cause| binary::Error::Access { location, cause })?
                            .value;
                        if let Some(width) = provider.width(&identity)
                            && expected.len() != width
                        {
                            PreparedValue::Answer(Agreement::MalformedValue)
                        } else {
                            PreparedValue::Ready { identity, expected }
                        }
                    }
                }
            }
        }
    };
    Ok(Prepared {
        provider,
        reference,
        value,
    })
}

impl<'p, P: Digest<'p>, N: Copy + Eq, D: Copy + Eq, G: Copy + Eq> Prepared<'p, P, N, D, G> {
    /// Compares the actual completed shared output without another XML decode,
    /// method resolution, hash state, source pass or identity/output clone.
    /// Reusing a stale XML generation or another provider is processing refusal.
    pub fn check_finished<X: Xml<Id = N, Document = D, Generation = G>, W: Work>(
        &self,
        xml: &X,
        actual: structured_cms::provider::Finished<'_, P, P::Identity, P::Output>,
        execution: &W,
    ) -> binary::BinaryResult<Agreement, X, Error<core::convert::Infallible, P::Error>> {
        let access = Access::new(xml, self.reference.snapshot, execution);
        let location = Location {
            snapshot: self.reference.snapshot,
            node: self.reference.value,
        };
        access
            .inspect(self.reference.value, |_| ())
            .map_err(|cause| binary::Error::Access { location, cause })?;
        if !actual.from_provider(self.provider) {
            return Err(binary::Error::Callback {
                location,
                cause: Error::ForeignDigest,
            });
        }
        match &self.value {
            PreparedValue::Answer(answer) => Ok(*answer),
            PreparedValue::Ready { identity, expected } => compare_resolved_finished(
                &access,
                self.reference,
                self.provider,
                identity,
                actual,
                expected,
            ),
        }
    }
}

async fn check_async_with<
    'p,
    const BASELINE: bool,
    X: OwnedBinaryXml,
    P: DigestProvider<'p, X>,
    S: stream::AsyncSource,
    W: Work,
>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> binary::BinaryResult<Agreement, X, Error<S::Error, P::Error>> {
    let access = Access::new(xml, reference.snapshot, execution);
    if let Some(answer) = preflight::<X, W, S::Error, P::Error>(&access, reference)? {
        return Ok(answer);
    }
    let location = Location {
        snapshot: reference.snapshot,
        node: reference.value,
    };
    let expected = access
        .query(reference.value, |xml, snapshot, work| {
            xml.decode_owned(snapshot, reference.value, work)
        })
        .map_err(|cause| binary::Error::Access { location, cause })?;
    let identity = match resolve_method(&access, reference, provider)? {
        Resolution::Supported(identity) => identity,
        Resolution::Unsupported => return Ok(Agreement::Unsupported),
        Resolution::Malformed => return Ok(Agreement::MalformedMethod),
    };
    let identity = match certificate::resolved_digest::<BASELINE, X, P>(identity, provider) {
        Ok(identity) => identity,
        Err(answer) => return Ok(answer),
    };
    let width = provider.width(&identity);
    if let Some(width) = width
        && expected.len() != width
    {
        return Ok(Agreement::MalformedValue);
    }
    let state = match start_resolved::<P, W, S::Error>(&identity, provider, execution)
        .map_err(|cause| binary::Error::Callback { location, cause })?
    {
        Resolution::Supported(state) => state,
        Resolution::Unsupported => return Ok(Agreement::Unsupported),
        Resolution::Malformed => return Ok(Agreement::MalformedMethod),
    };
    let mut state = state;
    let mut updating = Updating::<P> {
        provider,
        state: &mut state,
    };
    let result = stream::run_async(source, &mut updating, execution, "xades.digestInput", 0).await;
    stream_result(result).map_err(|cause| binary::Error::Callback { location, cause })?;
    // External acquisition may suspend while the XML owner changes generation.
    // Fence finalization/comparison with the same original-node query kernel;
    // failed source/provider causes retain precedence over a later guard.
    access
        .query_fallible(reference.value, |_, _, work| {
            let output = finish_resolved(width, state, provider, work)?;
            let agrees = provider
                .matches(&identity, &output, &expected, work)
                .map_err(Error::Provider)?;
            Ok(if agrees {
                Agreement::Matches
            } else {
                Agreement::Differs
            })
        })
        .map_err(|cause| match cause {
            crate::xml::AccessError::Xml(cause) => binary::Error::Callback { location, cause },
            crate::xml::AccessError::Changed { node } => binary::Error::Access {
                location,
                cause: crate::xml::AccessError::Changed { node },
            },
            crate::xml::AccessError::Resource { node, cause } => binary::Error::Access {
                location,
                cause: crate::xml::AccessError::Resource { node, cause },
            },
        })
}

pub(crate) enum Checked<T> {
    Answer(Agreement),
    Value(T),
}

// Both one-shot source checking and complete material-catalogue checking use
// this one method/declared-value kernel. Memoization cannot bypass syntax,
// width or actual resolved-algorithm classification before provider effects.
pub(crate) fn check_with<'p, X: BinaryXml, P: DigestProvider<'p, X>, W: Work, SE, R>(
    xml: &X,
    reference: Reference<X::Id, X::Document, X::Generation>,
    provider: &'p P,
    execution: &W,
    visit: impl FnOnce(
        certificate::FormatDigest<P::Identity>,
        &[u8],
        &W,
    ) -> Result<R, Error<SE, P::Error>>,
) -> binary::BinaryResult<Checked<R>, X, Error<SE, P::Error>> {
    let access = Access::new(xml, reference.snapshot, execution);
    if let Some(answer) = preflight(&access, reference)? {
        return Ok(Checked::Answer(answer));
    }
    binary::with_octets(
        xml,
        Location {
            snapshot: reference.snapshot,
            node: reference.value,
        },
        execution,
        |expected| {
            Ok::<_, core::convert::Infallible>(check_expected(
                &access, reference, expected, provider, visit,
            ))
        },
    )
    .map_err(|error| error.map_callback(|never| match never {}))?
}

fn preflight<X: BinaryXml, W: Work, SE, PE>(
    access: &Access<'_, X, W>,
    reference: Reference<X::Id, X::Document, X::Generation>,
) -> binary::BinaryResult<Option<Agreement>, X, Error<SE, PE>> {
    certificate::preflight_method(access, reference.method).map_err(|cause| binary::Error::Access {
        location: Location {
            snapshot: reference.snapshot,
            node: reference.method,
        },
        cause,
    })
}

fn check_expected<'p, X: BinaryXml, P: DigestProvider<'p, X>, W: Work, SE, R>(
    access: &Access<'_, X, W>,
    reference: Reference<X::Id, X::Document, X::Generation>,
    expected: &[u8],
    provider: &'p P,
    visit: impl FnOnce(
        certificate::FormatDigest<P::Identity>,
        &[u8],
        &W,
    ) -> Result<R, Error<SE, P::Error>>,
) -> binary::BinaryResult<Checked<R>, X, Error<SE, P::Error>> {
    let execution = access.execution();
    let resolved = resolve_method(access, reference, provider)?;
    let identity = match resolved {
        Resolution::Supported(identity) => identity,
        Resolution::Unsupported => return Ok(Checked::Answer(Agreement::Unsupported)),
        Resolution::Malformed => return Ok(Checked::Answer(Agreement::MalformedMethod)),
    };
    let identity = match certificate::baseline_digest::<X, P>(identity, provider) {
        certificate::Expected::Ready(identity) => identity,
        certificate::Expected::Answer(answer) => return Ok(Checked::Answer(answer)),
    };
    if let Some(width) = provider.width(identity.identity())
        && expected.len() != width
    {
        return Ok(Checked::Answer(Agreement::MalformedValue));
    }
    visit(identity, expected, execution)
        .map(Checked::Value)
        .map_err(|cause| binary::Error::Callback {
            location: Location {
                snapshot: reference.snapshot,
                node: reference.value,
            },
            cause,
        })
}

fn resolve_method<'p, X: Xml, P: DigestProvider<'p, X>, W: Work, SE>(
    access: &Access<'_, X, W>,
    reference: Reference<X::Id, X::Document, X::Generation>,
    provider: &P,
) -> binary::BinaryResult<Resolution<P::Identity>, X, Error<SE, P::Error>> {
    let location = Location {
        snapshot: reference.snapshot,
        node: reference.method,
    };
    access
        .query(reference.method, |_, _, _| {
            Ok(provider.resolve(access, reference.method))
        })
        .map_err(|cause| binary::Error::Access { location, cause })?
        .map_err(|cause| binary::Error::Callback {
            location,
            cause: Error::Provider(cause),
        })
}

pub(crate) fn run_resolved<'p, P: Digest<'p>, S: Source, W: Work>(
    identity: &certificate::FormatDigest<P::Identity>,
    expected: &[u8],
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> Result<Agreement, Error<S::Error, P::Error>> {
    let width = provider.width(identity.identity());
    if let Some(width) = width
        && expected.len() != width
    {
        return Ok(Agreement::MalformedValue);
    }
    let output = match compute_resolved::<P, S, W>(identity, source, provider, execution)? {
        Resolution::Supported(output) => output,
        Resolution::Unsupported => return Ok(Agreement::Unsupported),
        Resolution::Malformed => return Ok(Agreement::MalformedMethod),
    };
    execution.charge_work(1).map_err(Error::Resource)?;
    let agrees = provider
        .matches(identity.identity(), &output, expected, execution)
        .map_err(Error::Provider)?;
    Ok(if agrees {
        Agreement::Matches
    } else {
        Agreement::Differs
    })
}

type Computation<O, SE, PE> = Result<Resolution<O>, Error<SE, PE>>;

pub(crate) fn compute_resolved<'p, P: Digest<'p>, S: Source, W: Work>(
    identity: &certificate::FormatDigest<P::Identity>,
    source: &mut S,
    provider: &'p P,
    execution: &W,
) -> Computation<P::Output, S::Error, P::Error> {
    let width = provider.width(identity.identity());
    let mut state =
        match start_resolved::<P, W, S::Error>(identity.identity(), provider, execution)? {
            Resolution::Supported(state) => state,
            Resolution::Unsupported => return Ok(Resolution::Unsupported),
            Resolution::Malformed => return Ok(Resolution::Malformed),
        };
    let mut updating = Updating::<P> {
        provider,
        state: &mut state,
    };
    stream_result(stream::run(
        source,
        &mut updating,
        execution,
        "xades.digestInput",
        0,
    ))?;
    finish_resolved(width, state, provider, execution).map(Resolution::Supported)
}

fn stream_result<SE, PE>(result: Result<(), StreamError<SE, PE>>) -> Result<(), Error<SE, PE>> {
    match result {
        Ok(()) => {}
        Err(StreamError::Source(cause)) => return Err(Error::Source(cause)),
        Err(StreamError::Callback(cause)) => return Err(Error::Provider(cause)),
        Err(StreamError::Processing(cause)) => return Err(Error::Stream(cause)),
    }
    Ok(())
}

fn start_resolved<'p, P: Digest<'p>, W: Work, SE>(
    identity: &P::Identity,
    provider: &'p P,
    execution: &W,
) -> Result<Resolution<P::State>, Error<SE, P::Error>> {
    execution.charge_work(1).map_err(Error::Resource)?;
    provider.start(identity, execution).map_err(Error::Provider)
}

fn finish_resolved<'p, P: Digest<'p>, W: Work, SE>(
    width: Option<usize>,
    state: P::State,
    provider: &'p P,
    execution: &W,
) -> Result<P::Output, Error<SE, P::Error>> {
    execution.charge_work(1).map_err(Error::Resource)?;
    let output = provider.finish(state, execution).map_err(Error::Provider)?;
    if let Some(expected) = width
        && output.as_ref().len() != expected
    {
        return Err(Error::OutputWidth {
            expected,
            actual: output.as_ref().len(),
        });
    }
    Ok(output)
}