asn1rs/io/per/
mod.rs

1//! This module contains defines traits to encode and decode basic ASN.1 primitives and types of
2//! which the encoding/decoding depends on the UNALIGNED flag.
3//! The idea is to provide all building blocks to composite the more complex types on top of the
4//! traits without caring about the representation being ALIGNED or UNALIGNED.
5
6pub mod err;
7pub mod unaligned;
8
9pub use err::Error;
10pub use err::ErrorKind;
11
12/// According to ITU-T X.691 | ISO/IEC 8825-2:2015
13pub trait PackedRead {
14    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 12, the boolean type is represented
15    /// through a single bit, where 1 represents `true` and 0 represents `false`.
16    fn read_boolean(&mut self) -> Result<bool, Error>;
17
18    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.7, value that can be a negative,
19    /// zero or positive whole number and has no lower- or upper-bound constraints
20    fn read_2s_compliment_binary_integer(&mut self, bit_len: u64) -> Result<i64, Error>;
21
22    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.7, a constrained whole number
23    /// is a whole number with a lower- and upper-bound constrained
24    fn read_constrained_whole_number(
25        &mut self,
26        lower_bound: i64,
27        upper_bound: i64,
28    ) -> Result<i64, Error>;
29
30    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.17, the length determinant is
31    /// a number used to count bits, octets (bytes), characters or components
32    fn read_length_determinant(
33        &mut self,
34        lower_bound: Option<u64>,
35        upper_bound: Option<u64>,
36    ) -> Result<u64, Error>;
37
38    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.19, a number without constrains
39    /// and is likely to be small. It is used where small lengths are more likely than large values.
40    fn read_normally_small_length(&mut self) -> Result<u64, Error>;
41
42    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.18, an unconstrained integer
43    /// where small numbers appear more often the large numbers.
44    fn read_normally_small_non_negative_whole_number(&mut self) -> Result<u64, Error>;
45
46    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.20,
47    fn read_non_negative_binary_integer(
48        &mut self,
49        lower_bound: Option<u64>,
50        upper_bound: Option<u64>,
51    ) -> Result<u64, Error>;
52
53    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.24, a semi constrained whole
54    /// number is a whole number with a lower-bound constrained but no upper-bound constrained
55    fn read_semi_constrained_whole_number(&mut self, lower_bound: i64) -> Result<i64, Error>;
56
57    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.27, a semi constrained whole
58    /// number is a whole number with a lower-bound constrained but no upper-bound constrained
59    fn read_unconstrained_whole_number(&mut self) -> Result<i64, Error>;
60
61    fn read_bitstring(
62        &mut self,
63        lower_bound_size: Option<u64>,
64        upper_bound_size: Option<u64>,
65        extensible: bool,
66    ) -> Result<(Vec<u8>, u64), Error>;
67
68    fn read_octetstring(
69        &mut self,
70        lower_bound_size: Option<u64>,
71        upper_bound_size: Option<u64>,
72        extensible: bool,
73    ) -> Result<Vec<u8>, Error>;
74
75    fn read_choice_index(&mut self, std_variants: u64, extensible: bool) -> Result<u64, Error>;
76
77    fn read_enumeration_index(&mut self, std_variants: u64, extensible: bool)
78        -> Result<u64, Error>;
79}
80
81/// According to ITU-T X.691 | ISO/IEC 8825-2:2015
82pub trait PackedWrite {
83    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 12, the boolean type is represented
84    /// through a single bit, where 1 represents `true` and 0 represents `false`.
85    fn write_boolean(&mut self, boolean: bool) -> Result<(), Error>;
86
87    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.7, value that can be a negative,
88    /// zero or positive whole number and has no lower- or upper-bound constraints
89    fn write_2s_compliment_binary_integer(&mut self, bit_len: u64, value: i64)
90        -> Result<(), Error>;
91
92    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.7, a constrained whole number
93    /// is a whole number with a lower- and upper-bound constrained
94    fn write_constrained_whole_number(
95        &mut self,
96        lower_bound: i64,
97        upper_bound: i64,
98        value: i64,
99    ) -> Result<(), Error>;
100
101    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.17, the length determinant is
102    /// a number used to count bits, octets (bytes), characters or components.
103    /// Returns `Some`-value, if the transmitted value differs from the given length (for example
104    /// when the length exceeds  the non-fragmented size)
105    fn write_length_determinant(
106        &mut self,
107        lower_bound: Option<u64>,
108        upper_bound: Option<u64>,
109        length: u64,
110    ) -> Result<Option<u64>, Error>;
111
112    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.19, a number without constrains
113    /// and is likely to be small. It is used where small lengths are more likely than large values.
114    fn write_normally_small_length(&mut self, value: u64) -> Result<(), Error>;
115
116    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.18, an unconstrained integer
117    /// where small numbers appear more often the large numbers.
118    fn write_normally_small_non_negative_whole_number(&mut self, value: u64) -> Result<(), Error>;
119
120    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.20,
121    fn write_non_negative_binary_integer(
122        &mut self,
123        lower_bound: Option<u64>,
124        upper_bound: Option<u64>,
125        value: u64,
126    ) -> Result<(), Error>;
127
128    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.24, a semi constrained whole
129    /// number is a whole number with a lower-bound constrained but no upper-bound constrained
130    fn write_semi_constrained_whole_number(
131        &mut self,
132        lower_bound: i64,
133        value: i64,
134    ) -> Result<(), Error>;
135
136    /// According to ITU-T X.691 | ISO/IEC 8825-2:2015, chapter 3.7.27, a semi constrained whole
137    /// number is a whole number with a lower-bound constrained but no upper-bound constrained
138    fn write_unconstrained_whole_number(&mut self, value: i64) -> Result<(), Error>;
139
140    fn write_bitstring(
141        &mut self,
142        lower_bound_size: Option<u64>,
143        upper_bound_size: Option<u64>,
144        extensible: bool,
145        src: &[u8],
146        offset: u64,
147        len: u64,
148    ) -> Result<(), Error>;
149
150    fn write_octetstring(
151        &mut self,
152        lower_bound_size: Option<u64>,
153        upper_bound_size: Option<u64>,
154        extensible: bool,
155        src: &[u8],
156    ) -> Result<(), Error>;
157
158    fn write_choice_index(
159        &mut self,
160        std_variants: u64,
161        extensible: bool,
162        index: u64,
163    ) -> Result<(), Error>;
164
165    fn write_enumeration_index(
166        &mut self,
167        std_variants: u64,
168        extensible: bool,
169        index: u64,
170    ) -> Result<(), Error>;
171}