asn1rs 0.2.0-alpha2

ASN.1 to Rust, Protobuf and SQL compiler/code generator. Supports ASN.1 UPER
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use crate::io::per::err::Error;
use crate::io::per::unaligned::buffer::BitBuffer;
use crate::io::per::unaligned::BitRead;
use crate::io::per::unaligned::BitWrite;
use crate::io::per::PackedRead;
use crate::io::per::PackedWrite;
use crate::prelude::*;
use std::ops::Range;

/// This ist enum is the main reason, the new impl is about ~10% slower (2020-09) than the previous/
/// legacy implementation. This dynamic state tracking at runtime could be avoided by passing all
/// values as const generics on each `read_`*/`write_`* call [RFC 2000]. Maybe getting rid of all
/// `mem::replace` calls would also be sufficient.
///
/// [RFC 2000]: https://github.com/rust-lang/rust/issues/44580    
pub enum Scope {
    OptBitField(Range<usize>),
    AllBitField(Range<usize>),
    /// According to ITU-TX.691 | ISO/IEC 8825-2:2015, an extensible struct is built as
    ///  - part1
    ///    - `eo`: flag for whether the struct serializes/has payload with extended fields
    ///    - flags for optional fields (only for the non-extended fields!)
    ///    - fields serialized 'inline' (only for the non-extended fields!)
    ///  - part2
    ///    - `eo`: number of extended fields (as normally-small-int)
    ///    - `eo`: presence-flag for each extended field (only OPTIONAL fields seem to
    ///            influence these flags!?)
    ///    - `eo`: fields serialized as
    ///      - length-determinant
    ///      - sub-buffer with actual content
    ///
    /// `eo` for `extensible only` attributes
    ///
    /// To find the beginning of part2 - and thus to be able to insert the secondary-header - one
    /// needs to keep track of the current field number. Also, the position of where to write
    /// the presence flags to must be updated as well.
    ExtensibleSequence {
        opt_bit_field: Option<Range<usize>>,
        calls_until_ext_bitfield: usize,
        number_of_ext_fields: usize,
    },
}

impl Scope {
    #[inline]
    pub const fn exhausted(&self) -> bool {
        match self {
            Scope::OptBitField(range) => range.start == range.end,
            Scope::AllBitField(range) => range.start == range.end,
            Scope::ExtensibleSequence { .. } => false,
        }
    }

    #[inline]
    pub const fn encode_as_open_type_field(&self) -> bool {
        matches!(self, Scope::AllBitField(_))
    }

    #[inline]
    pub fn write_into_field(
        &mut self,
        buffer: &mut BitBuffer,
        is_opt: bool,
        is_present: bool,
    ) -> Result<(), Error> {
        match self {
            Scope::OptBitField(range) => {
                if is_opt {
                    let result =
                        buffer.with_write_position_at(range.start, |b| b.write_bit(is_present));
                    range.start += 1;
                    result
                } else {
                    Ok(())
                }
            }
            Scope::AllBitField(range) => {
                let result =
                    buffer.with_write_position_at(range.start, |b| b.write_bit(is_present));
                range.start += 1;
                result
            }
            Scope::ExtensibleSequence {
                opt_bit_field,
                calls_until_ext_bitfield,
                number_of_ext_fields,
            } => {
                if *calls_until_ext_bitfield == 0 {
                    // when we reach this point, there is never zero numbers of ext-fields
                    buffer.write_normally_small_non_negative_whole_number(
                        *number_of_ext_fields as u64 - 1,
                    )?;
                    let pos = buffer.write_position;
                    for _ in 0..*number_of_ext_fields {
                        if let Err(e) = buffer.write_bit(true) {
                            buffer.write_position = pos;
                            return Err(e);
                        }
                    }
                    let range = pos..buffer.write_position;
                    // buffer.write_int(range.len() as i64, (1, range.end as i64))?;
                    *self = Scope::AllBitField(range);
                    self.write_into_field(buffer, is_opt, is_present)
                } else {
                    *calls_until_ext_bitfield = calls_until_ext_bitfield.saturating_sub(1);
                    if let Some(range) = opt_bit_field {
                        if is_opt {
                            let result = buffer
                                .with_write_position_at(range.start, |b| b.write_bit(is_present));
                            range.start += 1;
                            result
                        } else {
                            Ok(())
                        }
                    } else {
                        Ok(())
                    }
                }
            }
        }
    }

    #[inline]
    pub fn read_from_field(
        &mut self,
        buffer: &mut BitBuffer,
        is_opt: bool,
    ) -> Result<Option<bool>, Error> {
        match self {
            Scope::OptBitField(range) => {
                if is_opt {
                    let result =
                        buffer.with_read_position_at(range.start, |buffer| buffer.read_bit());
                    range.start += 1;
                    Some(result).transpose()
                } else {
                    Ok(None)
                }
            }
            Scope::AllBitField(range) => {
                let result = buffer.with_read_position_at(range.start, |buffer| buffer.read_bit());
                range.start += 1;
                Some(result).transpose()
            }
            Scope::ExtensibleSequence {
                opt_bit_field,
                calls_until_ext_bitfield,
                number_of_ext_fields,
            } => {
                if *calls_until_ext_bitfield == 0 {
                    let read_number_of_ext_fields =
                        buffer.read_normally_small_length()? as usize + 1;
                    if read_number_of_ext_fields != *number_of_ext_fields {
                        return Err(Error::UnsupportedOperation(format!(
                            "Expected {} extended fields but got {}",
                            number_of_ext_fields, read_number_of_ext_fields
                        )));
                    }
                    let range = buffer.read_position..buffer.read_position + *number_of_ext_fields;
                    buffer.read_position = range.end; // skip bit-field
                    *self = Scope::AllBitField(range);
                    self.read_from_field(buffer, is_opt)
                } else {
                    *calls_until_ext_bitfield = calls_until_ext_bitfield.saturating_sub(1);
                    opt_bit_field
                        .as_mut()
                        .filter(|_| is_opt)
                        .map(|range| {
                            let result = buffer
                                .with_read_position_at(range.start, |buffer| buffer.read_bit());
                            range.start += 1;
                            result
                        })
                        .transpose()
                }
            }
        }
    }
}

#[derive(Default)]
pub struct UperWriter {
    buffer: BitBuffer,
    scope: Option<Scope>,
}

impl UperWriter {
    pub fn with_capacity(capacity_bytes: usize) -> Self {
        Self {
            buffer: BitBuffer::with_capacity(capacity_bytes),
            ..Default::default()
        }
    }

    pub fn byte_content(&self) -> &[u8] {
        self.buffer.content()
    }

    pub const fn bit_len(&self) -> usize {
        self.buffer.bit_len()
    }

    pub fn into_bytes_vec(self) -> Vec<u8> {
        self.buffer.into()
    }

    pub fn into_reader(self) -> UperReader {
        let bits = self.bit_len();
        let bytes = self.into_bytes_vec();
        UperReader::from_bits(bytes, bits)
    }

    #[inline]
    pub fn scope_pushed<R, F: FnOnce(&mut Self) -> R>(&mut self, scope: Scope, f: F) -> R {
        let original = core::mem::replace(&mut self.scope, Some(scope));
        let result = f(self);
        let scope = core::mem::replace(&mut self.scope, original);
        // save because this is supposed to be the original from above
        debug_assert!(scope.unwrap().exhausted());
        result
    }

    #[inline]
    pub fn scope_stashed<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
        let scope = self.scope.take();
        let result = f(self);
        self.scope = scope;
        result
    }

    #[inline]
    pub fn write_bit_field_entry(&mut self, is_opt: bool, is_present: bool) -> Result<(), Error> {
        if let Some(scope) = &mut self.scope {
            scope.write_into_field(&mut self.buffer, is_opt, is_present)
        } else if is_opt {
            self.buffer.write_bit(is_present)
        } else {
            Ok(())
        }
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    pub fn with_buffer<T, F: FnOnce(&mut Self) -> Result<T, Error>>(
        &mut self,
        f: F,
    ) -> Result<T, Error> {
        if const_map_or!(self.scope, Scope::encode_as_open_type_field, false) {
            let mut writer = UperWriter::with_capacity(512);
            let result = f(&mut writer)?;
            self.buffer
                .write_octetstring(None, None, false, writer.buffer.content())?;
            Ok(result)
        } else {
            f(self)
        }
    }
}

impl Writer for UperWriter {
    type Error = Error;

    #[inline]
    fn write_sequence<C: sequence::Constraint, F: Fn(&mut Self) -> Result<(), Self::Error>>(
        &mut self,
        f: F,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            if let Some(extension_after) = C::EXTENDED_AFTER_FIELD {
                w.buffer.write_bit(C::FIELD_COUNT > extension_after)?;
            }

            // In UPER the values for all OPTIONAL flags are written before any field
            // value is written. This remembers their position, so a later call of `write_opt`
            // can write them to the buffer
            let write_pos = w.buffer.write_position;
            let range = write_pos..write_pos + C::STD_OPTIONAL_FIELDS as usize;
            for _ in 0..C::STD_OPTIONAL_FIELDS {
                // insert in reverse order so that a simple pop() in `write_opt` retrieves
                // the relevant position
                if let Err(e) = w.buffer.write_bit(false) {
                    w.buffer.write_position = write_pos; // undo write_bits
                    return Err(e);
                }
            }

            if let Some(extension_after) = C::EXTENDED_AFTER_FIELD {
                w.scope_pushed(
                    Scope::ExtensibleSequence {
                        opt_bit_field: Some(range),
                        calls_until_ext_bitfield: (extension_after + 1) as usize,
                        number_of_ext_fields: (C::FIELD_COUNT - (extension_after + 1)) as usize,
                    },
                    f,
                )
            } else {
                w.scope_pushed(Scope::OptBitField(range), f)
            }
        })
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    fn write_sequence_of<C: sequenceof::Constraint, T: WritableType>(
        &mut self,
        slice: &[T::Type],
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.scope_stashed(|w| {
            const MAX: u64 = i64::MAX as u64;
            let min = const_unwrap_or!(C::MIN, 0);
            let max = const_unwrap_or!(C::MAX, MAX);
            let len = slice.len() as u64;
            let out_of_range = len < min || len > max;

            if C::EXTENSIBLE {
                w.buffer.write_bit(out_of_range)?;
            }

            if out_of_range {
                if !C::EXTENSIBLE {
                    return Err(Error::SizeNotInRange(len, min, max));
                } else {
                    w.buffer.write_length_determinant(None, None, len)?;
                }
            } else {
                w.buffer.write_length_determinant(C::MIN, C::MAX, len)?;
            }
            w.scope_stashed(|w| {
                for value in slice {
                    T::write_value(w, value)?;
                }
                Ok(())
            })
        })
    }

    #[inline]
    fn write_enumerated<C: enumerated::Constraint>(
        &mut self,
        enumerated: &C,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            w.buffer.write_enumeration_index(
                C::STD_VARIANT_COUNT,
                C::EXTENSIBLE,
                enumerated.to_choice_index(),
            )
        })
    }

    #[inline]
    fn write_choice<C: choice::Constraint>(&mut self, choice: &C) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.scope_stashed(|w| {
            let index = choice.to_choice_index();

            // this fails if the index is out of range
            w.buffer
                .write_choice_index(C::STD_VARIANT_COUNT, C::EXTENSIBLE, index)?;

            if index >= C::STD_VARIANT_COUNT {
                // TODO performance
                let mut writer = UperWriter::with_capacity(512);
                choice.write_content(&mut writer)?;
                w.buffer
                    .write_octetstring(None, None, false, writer.byte_content())
            } else {
                choice.write_content(w)
            }
        })
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    fn write_opt<T: WritableType>(
        &mut self,
        value: Option<&<T as WritableType>::Type>,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(true, const_is_some!(value))?;
        if let Some(value) = value {
            self.scope_stashed(|w| T::write_value(w, value))
        } else {
            Ok(())
        }
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    fn write_number<T: numbers::Number, C: numbers::Constraint<T>>(
        &mut self,
        value: T,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        let value = value.to_i64();

        let max_fn = if C::EXTENSIBLE {
            let min = const_unwrap_or!(C::MIN, 0);
            let max = const_unwrap_or!(C::MAX, i64::MAX);
            value < min || value > max
        } else {
            const_is_none!(C::MIN) && const_is_none!(C::MAX)
        };

        if max_fn {
            self.with_buffer(|w| {
                if C::EXTENSIBLE {
                    w.buffer.write_bit(true)?;
                }
                w.buffer.write_unconstrained_whole_number(value)
            })
        } else {
            self.with_buffer(|w| {
                if C::EXTENSIBLE {
                    w.buffer.write_bit(false)?;
                }
                w.buffer.write_constrained_whole_number(
                    const_unwrap_or!(C::MIN, 0),
                    const_unwrap_or!(C::MAX, i64::MAX),
                    value,
                )
            })
        }
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    fn write_utf8string<C: utf8string::Constraint>(
        &mut self,
        value: &str,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            if !C::EXTENSIBLE {
                let chars = value.chars().count() as u64;
                let min = const_unwrap_or!(C::MIN, 0);
                let max = const_unwrap_or!(C::MAX, u64::MAX);
                if chars < min || chars > max {
                    return Err(Error::SizeNotInRange(chars, min, max));
                }
            }

            // ITU-TX.691 | ISO/IEC 8825-2:2015, chapter 30.3
            // For 'known-multiplier character string types' there is no min/max in the encoding
            w.buffer
                .write_octetstring(None, None, false, value.as_bytes())
        })
    }

    fn write_ia5string<C: ia5string::Constraint>(
        &mut self,
        value: &str,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            if value.chars().any(|c| c as u32 >= 128) {
                return Err(Error::InvalidIa5String);
            }

            let chars = value.chars().count() as u64;
            let min = const_unwrap_or!(C::MIN, 0);
            let max = const_unwrap_or!(C::MAX, u64::MAX);
            let out_of_range = chars < min || chars > max;

            if C::EXTENSIBLE {
                w.buffer.write_bit(out_of_range)?;
            }

            if out_of_range {
                if !C::EXTENSIBLE {
                    return Err(Error::SizeNotInRange(chars, min, max));
                } else {
                    w.buffer.write_length_determinant(None, None, chars)?;
                }
            } else {
                w.buffer.write_length_determinant(C::MIN, C::MAX, chars)?;
            }

            for char in value.chars().map(|c| c as u8) {
                // 7 bits
                w.buffer.write_bits_with_offset(&[char], 1)?;
            }

            Ok(())
        })
    }

    #[inline]
    fn write_octet_string<C: octetstring::Constraint>(
        &mut self,
        value: &[u8],
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            w.buffer
                .write_octetstring(C::MIN, C::MAX, C::EXTENSIBLE, value)
        })
    }

    #[inline]
    fn write_bit_string<C: bitstring::Constraint>(
        &mut self,
        value: &[u8],
        bit_len: u64,
    ) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| {
            w.buffer
                .write_bitstring(C::MIN, C::MAX, C::EXTENSIBLE, value, 0, bit_len)
        })
    }

    #[inline]
    fn write_boolean<C: boolean::Constraint>(&mut self, value: bool) -> Result<(), Self::Error> {
        self.write_bit_field_entry(false, true)?;
        self.with_buffer(|w| w.buffer.write_bit(value))
    }
}

pub struct UperReader {
    buffer: BitBuffer,
    scope: Option<Scope>,
}

impl UperReader {
    pub fn from_bits<I: Into<Vec<u8>>>(bytes: I, bit_len: usize) -> Self {
        Self {
            buffer: BitBuffer::from_bits(bytes.into(), bit_len),
            scope: Default::default(),
        }
    }

    #[inline]
    pub const fn bits_remaining(&self) -> usize {
        self.buffer.write_position - self.buffer.read_position
    }

    #[inline]
    pub fn scope_pushed<R, F: FnOnce(&mut Self) -> R>(&mut self, scope: Scope, f: F) -> R {
        let original = core::mem::replace(&mut self.scope, Some(scope));
        let result = f(self);
        let scope = core::mem::replace(&mut self.scope, original);
        let scope = scope.unwrap(); // save because this is the original from above
        debug_assert!(scope.exhausted());
        result
    }

    #[inline]
    pub fn scope_stashed<R, F: FnOnce(&mut Self) -> R>(&mut self, f: F) -> R {
        let scope = self.scope.take();
        let result = f(self);
        self.scope = scope;
        result
    }

    #[inline]
    pub fn read_whole_sub_slice<T, E, F: FnOnce(&mut Self) -> Result<T, E>>(
        &mut self,
        length_bytes: usize,
        f: F,
    ) -> Result<T, E> {
        let write_position = self.buffer.read_position + (length_bytes * 8);
        let write_original = core::mem::replace(&mut self.buffer.write_position, write_position);
        let result = f(self);
        // extend to original position
        self.buffer.write_position = write_original;
        if result.is_ok() {
            // on successful read, skip the slice
            self.buffer.read_position = write_position;
        }
        result
    }

    #[inline]
    pub fn read_bit_field_entry(&mut self, is_opt: bool) -> Result<Option<bool>, Error> {
        if let Some(scope) = &mut self.scope {
            scope.read_from_field(&mut self.buffer, is_opt)
        } else if is_opt {
            Some(self.buffer.read_bit()).transpose()
        } else {
            Ok(None)
        }
    }

    #[inline]
    pub fn with_buffer<T, F: FnOnce(&mut Self) -> Result<T, Error>>(
        &mut self,
        f: F,
    ) -> Result<T, Error> {
        if self
            .scope
            .as_ref()
            .map(Scope::encode_as_open_type_field)
            .unwrap_or(false)
        {
            let len = self.buffer.read_length_determinant(None, None)?;
            self.read_whole_sub_slice(len as usize, f)
        } else {
            f(self)
        }
    }

    pub fn reset_read_position(&mut self) {
        self.buffer.reset_read_position()
    }
}

impl Reader for UperReader {
    type Error = Error;

    #[inline]
    fn read_sequence<
        C: sequence::Constraint,
        S: Sized,
        F: Fn(&mut Self) -> Result<S, Self::Error>,
    >(
        &mut self,
        f: F,
    ) -> Result<S, Self::Error> {
        let _ = self.read_bit_field_entry(false);
        self.with_buffer(|r| {
            if let Some(extension_after) = C::EXTENDED_AFTER_FIELD {
                let has_extension = r.buffer.read_bit()?;
                let expects_extension = C::FIELD_COUNT > extension_after;
                if has_extension != expects_extension {
                    return Err(Error::InvalidExtensionConstellation(
                        expects_extension,
                        has_extension,
                    ));
                }
            }

            // In UPER the values for all OPTIONAL flags are written before any field
            // value is written. This remembers their position, so a later call of `read_opt`
            // can retrieve them from the buffer
            let range =
                r.buffer.read_position..r.buffer.read_position + C::STD_OPTIONAL_FIELDS as usize;
            if r.buffer.bit_len() < range.end {
                return Err(Error::EndOfStream);
            }
            r.buffer.read_position = range.end; // skip optional

            if let Some(extension_after) = C::EXTENDED_AFTER_FIELD {
                r.scope_pushed(
                    Scope::ExtensibleSequence {
                        opt_bit_field: Some(range),
                        calls_until_ext_bitfield: (extension_after + 1) as usize,
                        number_of_ext_fields: (C::FIELD_COUNT - (extension_after + 1)) as usize,
                    },
                    f,
                )
            } else {
                r.scope_pushed(Scope::OptBitField(range), f)
            }
        })
    }

    #[inline]
    fn read_sequence_of<C: sequenceof::Constraint, T: ReadableType>(
        &mut self,
    ) -> Result<Vec<T::Type>, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| {
            let len = if C::EXTENSIBLE && r.buffer.read_bit()? {
                r.buffer.read_length_determinant(None, None)?
            } else {
                r.buffer.read_length_determinant(C::MIN, C::MAX)?
            };
            r.scope_stashed(|r| {
                let mut vec = Vec::with_capacity(len as usize);
                for _ in 0..len {
                    vec.push(T::read_value(r)?);
                }
                Ok(vec)
            })
        })
    }

    #[inline]
    fn read_enumerated<C: enumerated::Constraint>(&mut self) -> Result<C, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| {
            r.buffer
                .read_enumeration_index(C::STD_VARIANT_COUNT, C::EXTENSIBLE)
        })
        .and_then(|index| {
            C::from_choice_index(index)
                .ok_or_else(|| Error::InvalidChoiceIndex(index, C::VARIANT_COUNT))
        })
    }

    #[inline]
    fn read_choice<C: choice::Constraint>(&mut self) -> Result<C, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.scope_stashed(|r| {
            let index = r
                .buffer
                .read_choice_index(C::STD_VARIANT_COUNT, C::EXTENSIBLE)?;
            if index >= C::STD_VARIANT_COUNT {
                let length = r.buffer.read_length_determinant(None, None)?;
                r.read_whole_sub_slice(length as usize, |r| Ok((index, C::read_content(index, r)?)))
            } else {
                Ok((index, C::read_content(index, r)?))
            }
            .and_then(|(index, content)| {
                content.ok_or_else(|| Error::InvalidChoiceIndex(index, C::VARIANT_COUNT))
            })
        })
    }

    #[inline]
    fn read_opt<T: ReadableType>(
        &mut self,
    ) -> Result<Option<<T as ReadableType>::Type>, Self::Error> {
        // unwrap: as opt-field this must and will return some value
        if self.read_bit_field_entry(true)?.unwrap() {
            self.scope_stashed(T::read_value).map(Some)
        } else {
            Ok(None)
        }
    }

    #[inline]
    #[allow(clippy::redundant_pattern_matching)] // allow for const_*!
    fn read_number<T: numbers::Number, C: numbers::Constraint<T>>(
        &mut self,
    ) -> Result<T, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| {
            let unconstrained = if C::EXTENSIBLE {
                r.buffer.read_bit()?
            } else {
                const_is_none!(C::MIN) && const_is_none!(C::MAX)
            };

            if unconstrained {
                r.buffer.read_unconstrained_whole_number().map(T::from_i64)
            } else {
                r.buffer
                    .read_constrained_whole_number(
                        const_unwrap_or!(C::MIN, 0),
                        const_unwrap_or!(C::MAX, i64::MAX),
                    )
                    .map(T::from_i64)
            }
        })
    }

    #[inline]
    fn read_utf8string<C: utf8string::Constraint>(&mut self) -> Result<String, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| {
            // ITU-TX.691 | ISO/IEC 8825-2:2015, chapter 30.3
            // For 'known-multiplier character string types' there is no min/max in the encoding
            let octets = r.buffer.read_octetstring(None, None, false)?;
            String::from_utf8(octets).map_err(|_| Self::Error::InvalidUtf8String)
        })
    }

    fn read_ia5string<C: ia5string::Constraint>(&mut self) -> Result<String, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| {
            let len = if C::EXTENSIBLE && r.buffer.read_bit()? {
                r.buffer.read_length_determinant(None, None)?
            } else {
                r.buffer.read_length_determinant(C::MIN, C::MAX)?
            };

            let mut buffer = vec![0u8; len as usize];
            for i in 0..len as usize {
                r.buffer.read_bits_with_offset(&mut buffer[i..i + 1], 1)?;
            }

            String::from_utf8(buffer).map_err(|_| Error::InvalidIa5String)
        })
    }

    #[inline]
    fn read_octet_string<C: octetstring::Constraint>(&mut self) -> Result<Vec<u8>, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| r.buffer.read_octetstring(C::MIN, C::MAX, C::EXTENSIBLE))
    }

    #[inline]
    fn read_bit_string<C: bitstring::Constraint>(&mut self) -> Result<(Vec<u8>, u64), Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| r.buffer.read_bitstring(C::MIN, C::MAX, C::EXTENSIBLE))
    }

    #[inline]
    fn read_boolean<C: boolean::Constraint>(&mut self) -> Result<bool, Self::Error> {
        let _ = self.read_bit_field_entry(false)?;
        self.with_buffer(|r| r.buffer.read_boolean())
    }
}