asn1_codecs/per/aper/decode/
mod.rs

1//! Decode APIs for APER Codec
2
3use bitvec::prelude::*;
4
5#[allow(unused)]
6use crate::per::common::decode::*;
7use crate::per::PerCodecData;
8
9use crate::PerCodecError;
10
11/// Decode a Choice Index.
12///
13/// For an ASN.1 `CHOICE` Type, a CHOICE Index is first decoded. This function is used to `decode`
14/// the choice index. Returns the Index in the 'root' or 'additions' and a flag indicated whether
15/// the value is from the 'root_extensions' or 'addtions'. The caller would then decide the
16/// appropriate `decode` function for the CHOICE variant is called.
17pub fn decode_choice_idx(
18    data: &mut PerCodecData,
19    lb: i128,
20    ub: i128,
21    is_extensible: bool,
22) -> Result<(i128, bool), PerCodecError> {
23    log::trace!(
24        "decode_choice_idx: lb: {}, ub: {}, extensible: {}",
25        lb,
26        ub,
27        is_extensible
28    );
29
30    decode_choice_idx_common(data, lb, ub, is_extensible, true)
31}
32
33/// Decode The Sequence Header
34///
35/// The Sequence Header consists of potentially two fields
36/// 1. Whether `extensions` are present in the encoding
37/// 2. Which of the OPTIONAL fields (if any) are present as a bitmap.
38pub fn decode_sequence_header(
39    data: &mut PerCodecData,
40    is_extensible: bool,
41    optional_count: usize,
42) -> Result<(BitVec<u8, Msb0>, bool), PerCodecError> {
43    log::trace!("decode_sequence_header: extensible: {}", is_extensible);
44
45    decode_sequence_header_common(data, is_extensible, optional_count, true)
46}
47
48/// Decode an Integer
49///
50/// Given an Integer Specification with PER Visible Constraints, decode an Integer Value to obtain
51/// the integer value which will always be returned as an i128 value.
52///
53/// Note: The maximum (and minimum) value to be decoded is limited to an `i128` value. For the
54/// protocols that are currently supported this limit is acceptable.
55///
56/// `lb` and `ub` are upper and lower bounds as determined by the PER Constraints (and hence can be
57/// `None` if no Constraints are not speicifed. `is_extensible` specifies whether the defined type
58/// is extensible (as per PER Constraints). Returned value is the value of the Integer (i128) and
59/// whether the value is outside the extension root (`bool`: `true` if value is outside the
60/// extension root.).
61pub fn decode_integer(
62    data: &mut PerCodecData,
63    lb: Option<i128>,
64    ub: Option<i128>,
65    is_extensible: bool,
66) -> Result<(i128, bool), PerCodecError> {
67    log::trace!(
68        "decode_integer: Lower: {:?} Upper:{:?} Extensible: {}",
69        lb,
70        ub,
71        is_extensible
72    );
73
74    decode_integer_common(data, lb, ub, is_extensible, true)
75}
76
77/// Decode a Boolean
78///
79/// Decode a Boolean value. Returns the decoded value as a `bool`.
80pub fn decode_bool(data: &mut PerCodecData) -> Result<bool, PerCodecError> {
81    log::trace!("decode_bool:");
82
83    decode_bool_common(data, true)
84}
85
86/// Decode a Real
87///
88/// Decode a Boolean value. Returns the decoded value as a `f64`.
89pub fn decode_real(data: &mut PerCodecData) -> Result<f64, PerCodecError> {
90    log::trace!("decode_real:");
91    decode_real_common(data, true)
92}
93
94/// Decode an Enumerated Value
95///
96/// Decodes an Enumerated value as an index into either `root_values` of the ENUMERATED or
97/// `ext_values` of the ENUMERATED and also decodes a flag indicating where the value belongs. If
98/// `false` the value is from the `root_values`, else the value is from the `ext_values` of the
99/// ENUMERATED.
100pub fn decode_enumerated(
101    data: &mut PerCodecData,
102    lb: Option<i128>,
103    ub: Option<i128>,
104    is_extensible: bool,
105) -> Result<(i128, bool), PerCodecError> {
106    log::trace!(
107        "decode_enumerated: lb: {:?}, ub: {:?}, is_extensible: {}",
108        lb,
109        ub,
110        is_extensible
111    );
112
113    decode_enumerated_common(data, lb, ub, is_extensible, true)
114}
115
116/// Decode a Bit String
117///
118/// Decodes the value of the BIT STRING from the Buffer.
119pub fn decode_bitstring(
120    data: &mut PerCodecData,
121    lb: Option<i128>,
122    ub: Option<i128>,
123    is_extensible: bool,
124) -> Result<BitVec<u8, Msb0>, PerCodecError> {
125    log::trace!(
126        "decode_bitstring: lb: {:?}, ub: {:?}, is_extensible: {}",
127        lb,
128        ub,
129        is_extensible
130    );
131
132    decode_bitstring_common(data, lb, ub, is_extensible, true)
133}
134
135/// Decode an OCTET STRING
136///
137/// Decodes the value of the OCTET STRING from the Buffer.
138pub fn decode_octetstring(
139    data: &mut PerCodecData,
140    lb: Option<i128>,
141    ub: Option<i128>,
142    is_extensible: bool,
143) -> Result<Vec<u8>, PerCodecError> {
144    log::trace!(
145        "decode_bitstring: lb: {:?}, ub: {:?}, is_extensible: {}",
146        lb,
147        ub,
148        is_extensible
149    );
150
151    decode_octetstring_common(data, lb, ub, is_extensible, true)
152}
153
154/// Decodes a Length determinent
155pub fn decode_length_determinent(
156    data: &mut PerCodecData,
157    lb: Option<i128>,
158    ub: Option<i128>,
159    normally_small: bool,
160) -> Result<usize, PerCodecError> {
161    log::trace!("decode_length_determinent:");
162
163    decode_length_determinent_common(data, lb, ub, normally_small, true)
164}
165
166mod decode_charstrings;
167pub use decode_charstrings::*;