structured-pades 0.18.2

Original-source PDF signatures and provider-neutral PAdES baseline validation
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Structural stream extents and bounded decoding, independent of rendering.

use crate::arena::{Arena, NodeId};
use crate::error::{Error, ErrorKind, ReadFailure, Syntax};
use crate::model::admit;
use crate::range::{RangeReader, RangeSource};
use crate::syntax::{Cursor, read_into};
use structured_asn1::{
    Span,
    memory::{Memory, OwnedVec, Work},
};
pub(crate) mod plan;

#[derive(Clone, Copy)]
pub(crate) struct Indirect {
    pub(crate) number: u64,
    pub(crate) generation: u16,
    pub(crate) value: NodeId,
    pub(crate) start: u64,
    pub(crate) stream: Option<u64>,
}

pub(crate) async fn open<S: RangeSource>(
    cursor: &mut Cursor<'_, S>,
    arena: Arena<Memory>,
) -> Result<(Arena<Memory>, Indirect), ReadFailure<S::Error>> {
    let start = cursor.at;
    let number = cursor.natural().await?;
    if number == 0 {
        return Err(cursor.failure(Syntax::Object, start));
    }
    let gap = cursor.at;
    cursor.skip().await?;
    if gap == cursor.at {
        return Err(cursor.failure(Syntax::Object, gap));
    }
    let generation = u16::try_from(cursor.natural().await?)
        .map_err(|_| cursor.failure(Syntax::Object, cursor.at))?;
    let gap = cursor.at;
    cursor.skip().await?;
    if gap == cursor.at {
        return Err(cursor.failure(Syntax::Object, gap));
    }
    cursor.keyword(b"obj").await?;
    let session = cursor.reader.session();
    let (arena, value) = read_into(cursor, session, arena).await?;
    cursor.skip().await?;
    let stream = if cursor.looking(b"stream").await? {
        cursor.keyword(b"stream").await?;
        // ISO 32000-1 §7.3.8.1 requires LF or CRLF after stream. Bare CR
        // is not a substitute, and payload leading whitespace is not skipped.
        match cursor.take().await? {
            Some(b'\n') => {}
            Some(b'\r') if cursor.take().await? == Some(b'\n') => {}
            _ => return Err(cursor.failure(Syntax::Stream, cursor.at)),
        }
        Some(cursor.at)
    } else {
        cursor.keyword(b"endobj").await?;
        None
    };
    Ok((
        arena,
        Indirect {
            number,
            generation,
            value,
            start,
            stream,
        },
    ))
}

pub(crate) async fn close<S: RangeSource>(
    cursor: &mut Cursor<'_, S>,
    start: u64,
    length: u64,
) -> Result<Span, ReadFailure<S::Error>> {
    let end = start
        .checked_add(length)
        .ok_or_else(|| cursor.failure(Syntax::Stream, start))?;
    if end > cursor.reader.stamp().length {
        return Err(cursor.failure(Syntax::Stream, start));
    }
    cursor.at = end;
    if cursor.peek().await? == Some(b'\r') {
        cursor.at += 1;
    }
    if cursor.peek().await? == Some(b'\n') {
        cursor.at += 1;
    }
    cursor.keyword(b"endstream").await?;
    cursor.skip().await?;
    cursor.keyword(b"endobj").await?;
    Ok(Span { start, length })
}

#[derive(Clone, Copy)]
pub(crate) enum Filter {
    Hex,
    Base85,
    RunLength,
    Lzw { early: u8 },
    Flate,
}

#[derive(Clone, Copy)]
enum Prediction {
    None,
    Tiff {
        row: usize,
        colors: usize,
        samples: usize,
        bits: usize,
    },
    Png {
        row: usize,
        bpp: usize,
    },
}

pub(crate) struct Step {
    filter: Filter,
    prediction: Prediction,
}

pub(crate) async fn decoded<S: RangeSource, W: Work>(
    reader: &mut RangeReader<'_, S>,
    body: Span,
    arena: &Arena<W>,
    dictionary: NodeId,
    revision: usize,
) -> Result<OwnedVec<u8, Memory>, ReadFailure<S::Error>> {
    decoded_extent(reader, body, arena, dictionary, revision, true)
        .await
        .map(|(bytes, _)| bytes)
}

pub(crate) async fn decoded_extent<S: RangeSource, W: Work>(
    reader: &mut RangeReader<'_, S>,
    body: Span,
    arena: &Arena<W>,
    dictionary: NodeId,
    revision: usize,
    bounded: bool,
) -> Result<(OwnedVec<u8, Memory>, u64), ReadFailure<S::Error>> {
    let input = plan::Direct {
        arena,
        root: dictionary,
    };
    let mut builder = plan::Builder::new(
        &reader.execution(),
        reader.location(body.start, body.length),
    );
    builder
        .advance(&input, &reader.execution())
        .map_err(|error| match error {
            plan::Failure::Fault(error) => ReadFailure::Format(error),
            plan::Failure::Need { .. } => unreachable!("direct controls refuse references"),
        })?;
    execute(reader, body, &builder.finish(), revision, bounded).await
}

pub(crate) async fn execute<S: RangeSource>(
    reader: &mut RangeReader<'_, S>,
    body: Span,
    plans: &[Step],
    revision: usize,
    bounded: bool,
) -> Result<(OwnedVec<u8, Memory>, u64), ReadFailure<S::Error>> {
    let work = reader.execution();
    let location = reader.location(body.start, body.length);
    if plans.is_empty() {
        let mut output = work.vector();
        reader.session().decode(body.length, location)?;
        for offset in body.start..body.start + body.length {
            let byte = reader.byte_at(offset).await?.ok_or(Error {
                kind: ErrorKind::Syntax(Syntax::Stream),
                location,
            })?;
            output.try_push(byte).map_err(|cause| Error {
                kind: ErrorKind::Resource(cause),
                location,
            })?;
        }
        return Ok((output.into_owned(), body.length));
    }
    let mut previous: Option<OwnedVec<u8, Memory>> = None;
    let mut consumed = body.length;
    for (ordinal, plan) in plans.iter().enumerate() {
        let mut output = if let Some(prior) = previous.take() {
            let slice = crate::range::Slice::new(&prior, reader.stamp().snapshot);
            let mut nested = RangeReader::in_source(
                &slice,
                reader.session(),
                &work,
                crate::source::Source::Decoded {
                    stream: body,
                    revision,
                    stage: ordinal,
                },
            )
            .map_err(|error| crate::document::resolve::convert(ReadFailure::Input(error)))?;
            decode(
                &mut nested,
                Span {
                    start: 0,
                    length: prior.len() as u64,
                },
                plan.filter,
                true,
            )
            .await
            .map_err(crate::document::resolve::convert)?
            .0
        } else {
            let (output, extent) = decode(reader, body, plan.filter, bounded).await?;
            consumed = extent;
            output
        };
        predictor(&mut output, plan.prediction, &work, location)?;
        previous = Some(output);
    }
    Ok((previous.expect("nonempty filter array"), consumed))
}

fn prediction_shape(
    prediction: u64,
    colors: u64,
    bits: u64,
    columns: u64,
    location: crate::source::Location,
) -> crate::Result<Prediction> {
    let malformed = || Error {
        kind: ErrorKind::Syntax(Syntax::Stream),
        location,
    };
    if colors == 0 || columns == 0 || !matches!(bits, 1 | 2 | 4 | 8 | 16) {
        return Err(malformed());
    }
    let samples = colors.checked_mul(columns).ok_or_else(malformed)?;
    let row_bits = samples.checked_mul(bits).ok_or_else(malformed)?;
    let row = usize::try_from(row_bits.checked_add(7).ok_or_else(malformed)? / 8)
        .map_err(|_| malformed())?;
    let bpp = usize::try_from(
        colors
            .checked_mul(bits)
            .and_then(|value| value.checked_add(7))
            .ok_or_else(malformed)?
            / 8,
    )
    .map_err(|_| malformed())?;
    if prediction == 2 {
        let colors = usize::try_from(colors).map_err(|_| malformed())?;
        let samples = usize::try_from(samples).map_err(|_| malformed())?;
        let bits = usize::try_from(bits).expect("bounded sample width");
        return Ok(Prediction::Tiff {
            row,
            colors,
            samples,
            bits,
        });
    }
    if !(10..=15).contains(&prediction) {
        return Err(malformed());
    }
    row.checked_add(1).ok_or_else(malformed)?;
    Ok(Prediction::Png { row, bpp })
}

fn predictor(
    output: &mut OwnedVec<u8, Memory>,
    prediction: Prediction,
    work: &impl Work,
    location: crate::source::Location,
) -> crate::Result<()> {
    let malformed = || Error {
        kind: ErrorKind::Syntax(Syntax::Stream),
        location,
    };
    let (row, bpp) = match prediction {
        Prediction::None => return Ok(()),
        Prediction::Tiff {
            row,
            colors,
            samples,
            bits,
        } => {
            if !output.len().is_multiple_of(row) {
                return Err(malformed());
            }
            for start in (0..output.len()).step_by(row) {
                admit(work, location, samples as u64 * bits as u64 * 2)?;
                for sample in colors..samples {
                    let bit = sample * bits;
                    let prior = (sample - colors) * bits;
                    let mut value = 0_u32;
                    let mut left = 0_u32;
                    for index in 0..bits {
                        value = (value << 1)
                            | u32::from(
                                (output[start + (bit + index) / 8] >> (7 - (bit + index) % 8)) & 1,
                            );
                        left = (left << 1)
                            | u32::from(
                                (output[start + (prior + index) / 8] >> (7 - (prior + index) % 8))
                                    & 1,
                            );
                    }
                    let value = (value + left) & ((1_u32 << bits) - 1);
                    for index in 0..bits {
                        let shift = 7 - (bit + index) % 8;
                        let byte = &mut output[start + (bit + index) / 8];
                        *byte = (*byte & !(1 << shift))
                            | (u8::try_from((value >> (bits - 1 - index)) & 1).expect("bit")
                                << shift);
                    }
                }
            }
            return Ok(());
        }
        Prediction::Png { row, bpp } => (row, bpp),
    };
    let stride = row + 1;
    if !output.len().is_multiple_of(stride) {
        return Err(malformed());
    }
    let mut read = 0;
    let mut write = 0;
    while read < output.len() {
        admit(work, location, row as u64 * 8 + 1)?;
        let filter = output[read];
        read += 1;
        // ISO32000-1 §7.4.4.4/Table10 limits 10..14 on ENCODING only;
        // decoding all PNG predictors follows each row's explicit tag.
        // https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
        if filter > 4 {
            return Err(malformed());
        }
        for column in 0..row {
            let left = if column >= bpp {
                output[write + column - bpp]
            } else {
                0
            };
            let above = if write >= row {
                output[write + column - row]
            } else {
                0
            };
            let upper_left = if write >= row && column >= bpp {
                output[write + column - row - bpp]
            } else {
                0
            };
            let predicted = match filter {
                0 => 0,
                1 => left,
                2 => above,
                3 => left.midpoint(above),
                4 => paeth(left, above, upper_left),
                _ => unreachable!("checked filter"),
            };
            output[write + column] = output[read + column].wrapping_add(predicted);
        }
        read += row;
        write += row;
    }
    admit(work, location, (output.len() - write) as u64)?;
    while output.len() > write {
        output.pop().expect("nonempty compacted tail");
    }
    Ok(())
}

fn paeth(left: u8, above: u8, upper_left: u8) -> u8 {
    let prediction = i32::from(left) + i32::from(above) - i32::from(upper_left);
    let a = (prediction - i32::from(left)).abs();
    let b = (prediction - i32::from(above)).abs();
    let c = (prediction - i32::from(upper_left)).abs();
    if a <= b && a <= c {
        left
    } else if b <= c {
        above
    } else {
        upper_left
    }
}

struct Input<'a, 'q, S: RangeSource> {
    reader: &'a mut RangeReader<'q, S>,
    at: u64,
    end: u64,
    bounded: bool,
}

impl<S: RangeSource> Input<'_, '_, S> {
    async fn next(&mut self) -> Result<Option<u8>, ReadFailure<S::Error>> {
        if self.at == self.end {
            return Ok(None);
        }
        let byte = self
            .reader
            .byte_at(self.at)
            .await?
            .ok_or_else(|| self.error())?;
        self.at += 1;
        Ok(Some(byte))
    }

    fn error(&self) -> Error {
        Error {
            kind: ErrorKind::Syntax(Syntax::Stream),
            location: self.reader.location(self.at, 0),
        }
    }

    fn put<W: Work>(&self, output: &mut OwnedVec<u8, W>, byte: u8) -> crate::Result<()> {
        let location = self.reader.location(self.at, 0);
        self.reader.session().decode(1, location)?;
        output.try_push(byte).map_err(|cause| Error {
            kind: ErrorKind::Resource(cause),
            location,
        })
    }

    async fn finish(&mut self) -> Result<(), ReadFailure<S::Error>> {
        if !self.bounded {
            return Ok(());
        }
        while let Some(byte) = self.next().await? {
            if !crate::bytes::is_space(byte) {
                return Err(self.error().into());
            }
        }
        Ok(())
    }
}

pub(crate) async fn decode<S: RangeSource>(
    reader: &mut RangeReader<'_, S>,
    body: Span,
    filter: Filter,
    bounded: bool,
) -> Result<(OwnedVec<u8, Memory>, u64), ReadFailure<S::Error>> {
    let location = reader.location(body.start, body.length);
    let end = body.start.checked_add(body.length).ok_or(Error {
        kind: ErrorKind::Foreign,
        location,
    })?;
    if end > reader.stamp().length {
        return Err(Error {
            kind: ErrorKind::Foreign,
            location,
        }
        .into());
    }
    let work = reader.execution();
    let mut output = work.vector();
    let mut input = Input {
        reader,
        at: body.start,
        end,
        bounded,
    };
    match filter {
        Filter::Hex => {
            let mut high = None;
            let mut closed = false;
            while let Some(byte) = input.next().await? {
                if crate::bytes::is_space(byte) {
                    continue;
                }
                if byte == b'>' {
                    closed = true;
                    break;
                }
                let value = match byte {
                    b'0'..=b'9' => byte - b'0',
                    b'a'..=b'f' => byte - b'a' + 10,
                    b'A'..=b'F' => byte - b'A' + 10,
                    _ => return Err(input.error().into()),
                };
                if let Some(first) = high.take() {
                    input.put(&mut output, first * 16 + value)?;
                } else {
                    high = Some(value);
                }
            }
            if !closed {
                return Err(input.error().into());
            }
            if let Some(first) = high {
                input.put(&mut output, first * 16)?;
            }
            input.finish().await?;
        }
        Filter::Base85 => base85(&mut input, &mut output).await?,
        Filter::RunLength => loop {
            let length = input.next().await?.ok_or_else(|| input.error())?;
            if length == 128 {
                input.finish().await?;
                break;
            }
            if length < 128 {
                for _ in 0..=length {
                    let byte = input.next().await?.ok_or_else(|| input.error())?;
                    input.put(&mut output, byte)?;
                }
            } else {
                let byte = input.next().await?.ok_or_else(|| input.error())?;
                for _ in 0..257 - u16::from(length) {
                    input.put(&mut output, byte)?;
                }
            }
        },
        Filter::Lzw { early } => lzw(&mut input, &mut output, early).await?,
        Filter::Flate => flate(&mut input, &mut output).await?,
    }
    Ok((output.into_owned(), input.at - body.start))
}

async fn lzw<S: RangeSource, W: Work>(
    input: &mut Input<'_, '_, S>,
    output: &mut OwnedVec<u8, W>,
    early: u8,
) -> Result<(), ReadFailure<S::Error>> {
    if early > 1 {
        return Err(input.error().into());
    }
    let work = input.reader.execution();
    let location = input.reader.location(input.at, 0);
    let mut prefixes = work.vector();
    let mut suffixes = work.vector();
    let mut stack = work.vector();
    prefixes
        .try_extend_from_slice(&[0_u16; 4096])
        .map_err(|cause| Error {
            kind: ErrorKind::Resource(cause),
            location,
        })?;
    suffixes
        .try_extend_from_slice(&[0_u8; 4096])
        .map_err(|cause| Error {
            kind: ErrorKind::Resource(cause),
            location,
        })?;
    let mut bits = 0_u32;
    let mut count = 0_u8;
    let mut width = 9_u8;
    let mut next = 258_u16;
    let mut previous = None;
    loop {
        while count < width {
            bits = (bits << 8) | u32::from(input.next().await?.ok_or_else(|| input.error())?);
            count += 8;
        }
        count -= width;
        let code = u16::try_from((bits >> count) & ((1_u32 << width) - 1)).expect("12-bit code");
        if code == 256 {
            width = 9;
            next = 258;
            previous = None;
            continue;
        }
        if code == 257 {
            input.finish().await?;
            return Ok(());
        }
        if code > next || (code == next && previous.is_none()) {
            return Err(input.error().into());
        }
        let mut cursor = if code == next {
            previous.expect("checked previous")
        } else {
            code
        };
        while cursor >= 258 {
            admit(&work, location, 1)?;
            if cursor >= next {
                return Err(input.error().into());
            }
            stack
                .try_push(suffixes[usize::from(cursor)])
                .map_err(|cause| Error {
                    kind: ErrorKind::Resource(cause),
                    location,
                })?;
            cursor = prefixes[usize::from(cursor)];
        }
        if cursor > 255 {
            return Err(input.error().into());
        }
        let first = u8::try_from(cursor).expect("literal code");
        input.put(output, first)?;
        while let Some(byte) = stack.pop() {
            input.put(output, byte)?;
        }
        if code == next {
            input.put(output, first)?;
        }
        if let Some(old) = previous
            && next < 4096
        {
            admit(&work, location, 1)?;
            prefixes[usize::from(next)] = old;
            suffixes[usize::from(next)] = first;
            next += 1;
            if width < 12 && next + u16::from(early) == 1_u16 << width {
                width += 1;
            }
        }
        previous = Some(code);
    }
}

async fn base85<S: RangeSource, W: Work>(
    input: &mut Input<'_, '_, S>,
    output: &mut OwnedVec<u8, W>,
) -> Result<(), ReadFailure<S::Error>> {
    let mut value = 0_u64;
    let mut digits = 0_u8;
    loop {
        let byte = input.next().await?.ok_or_else(|| input.error())?;
        if crate::bytes::is_space(byte) {
            continue;
        }
        if byte == b'~' {
            if input.next().await? != Some(b'>') || digits == 1 {
                return Err(input.error().into());
            }
            if digits != 0 {
                let count = digits - 1;
                for _ in digits..5 {
                    value = value * 85 + 84;
                }
                let decoded = u32::try_from(value)
                    .map_err(|_| input.error())?
                    .to_be_bytes();
                for &byte in &decoded[..usize::from(count)] {
                    input.put(output, byte)?;
                }
            }
            input.finish().await?;
            break;
        }
        if byte == b'z' {
            if digits != 0 {
                return Err(input.error().into());
            }
            for _ in 0..4 {
                input.put(output, 0)?;
            }
            continue;
        }
        if !(b'!'..=b'u').contains(&byte) {
            return Err(input.error().into());
        }
        value = value * 85 + u64::from(byte - b'!');
        digits += 1;
        if digits == 5 {
            for byte in u32::try_from(value)
                .map_err(|_| input.error())?
                .to_be_bytes()
            {
                input.put(output, byte)?;
            }
            value = 0;
            digits = 0;
        }
    }
    Ok(())
}

async fn flate<S: RangeSource, W: Work>(
    input: &mut Input<'_, '_, S>,
    output: &mut OwnedVec<u8, W>,
) -> Result<(), ReadFailure<S::Error>> {
    use structured_asn1::inflate::{Envelope, Inflater, Progress};
    let work = input.reader.execution();
    let location = input.reader.location(input.at, 0);
    let mut inflater =
        Inflater::new(Envelope::Zlib, &work).map_err(|cause| inflate_error(cause, location))?;
    let mut chunk = [0_u8; 4096];
    let mut filled = 0;
    let mut consumed = 0;
    loop {
        if consumed == filled {
            filled = 0;
            consumed = 0;
            while filled < chunk.len() {
                let Some(byte) = input.next().await? else {
                    break;
                };
                chunk[filled] = byte;
                filled += 1;
            }
        }
        let maximum = usize::try_from(input.reader.session().decoded_remaining().min(4096))
            .expect("bounded output allowance");
        let allowance = inflater.output_capacity(maximum);
        output.try_reserve(allowance).map_err(|cause| Error {
            kind: ErrorKind::Resource(cause),
            location,
        })?;
        let step = match inflater.step(&chunk[consumed..filled], input.at < input.end, maximum) {
            Ok(step) => step,
            Err(cause) => {
                if let structured_asn1::inflate::Error::Malformed { produced } = cause {
                    input.reader.session().decode(produced as u64, location)?;
                    return Err(input.error().into());
                }
                return Err(inflate_error(cause, location).into());
            }
        };
        // No source/provider callback or await occurs between the allowance
        // check and this commit. The decoder cannot change session counters.
        input
            .reader
            .session()
            .decode(step.output.len() as u64, location)?;
        consumed += step.consumed;
        output
            .try_extend_from_slice(step.output)
            .map_err(|cause| Error {
                kind: ErrorKind::Resource(cause),
                location,
            })?;
        match step.progress {
            Progress::Done => {
                input.at -= (filled - consumed) as u64;
                input.finish().await?;
                return Ok(());
            }
            Progress::Input if consumed != filled => return Err(input.error().into()),
            Progress::Input | Progress::Block | Progress::Output => {}
        }
    }
}

fn inflate_error(
    cause: structured_asn1::inflate::Error,
    location: crate::source::Location,
) -> Error {
    use structured_asn1::inflate::Error as InflateError;
    let kind = match cause {
        InflateError::Resource(cause) => ErrorKind::Resource(cause),
        InflateError::OutputLimit => ErrorKind::Limit(crate::error::Limit::Decoded),
        InflateError::Malformed { .. } | InflateError::State => ErrorKind::Syntax(Syntax::Stream),
    };
    Error { kind, location }
}