asn1_codecs/per/uper/decode/
mod.rs

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