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
//! Decode APIs for APER Codec

use bitvec::prelude::*;

#[allow(unused)]
use crate::per::common::decode::*;
use crate::per::PerCodecData;

use crate::PerCodecError;

/// Decode a Choice Index.
///
/// For an ASN.1 `CHOICE` Type, a CHOICE Index is first decoded. This function is used to `decode`
/// the choice index. Returns the Index in the 'root' or 'additions' and a flag indicated whether
/// the value is from the 'root_extensions' or 'addtions'. The caller would then decide the
/// appropriate `decode` function for the CHOICE variant is called.
pub fn decode_choice_idx(
    data: &mut PerCodecData,
    lb: i128,
    ub: i128,
    is_extensible: bool,
) -> Result<(i128, bool), PerCodecError> {

    decode_choice_idx_common(data, lb, ub, is_extensible, true)
}

/// Decode The Sequence Header
///
/// The Sequence Header consists of potentially two fields
/// 1. Whether `extensions` are present in the encoding
/// 2. Which of the OPTIONAL fields (if any) are present as a bitmap.
pub fn decode_sequence_header(
    data: &mut PerCodecData,
    is_extensible: bool,
    optional_count: usize,
) -> Result<(BitVec<u8, Msb0>, bool), PerCodecError> {

    decode_sequence_header_common(data, is_extensible, optional_count, true)
}

/// Decode an Integer
///
/// Given an Integer Specification with PER Visible Constraints, decode an Integer Value to obtain
/// the integer value which will always be returned as an i128 value.
///
/// Note: The maximum (and minimum) value to be decoded is limited to an `i128` value. For the
/// protocols that are currently supported this limit is acceptable.
///
/// `lb` and `ub` are upper and lower bounds as determined by the PER Constraints (and hence can be
/// `None` if no Constraints are not speicifed. `is_extensible` specifies whether the defined type
/// is extensible (as per PER Constraints). Returned value is the value of the Integer (i128) and
/// whether the value is outside the extension root (`bool`: `true` if value is outside the
/// extension root.).
pub fn decode_integer(
    data: &mut PerCodecData,
    lb: Option<i128>,
    ub: Option<i128>,
    is_extensible: bool,
) -> Result<(i128, bool), PerCodecError> {

    decode_integer_common(data, lb, ub, is_extensible, true)
}

/// Decode a Boolean
///
/// Decode a Boolean value. Returns the decoded value as a `bool`.
pub fn decode_bool(data: &mut PerCodecData) -> Result<bool, PerCodecError> {

    decode_bool_common(data, true)
}

/// Decode an Enumerated Value
///
/// Decodes an Enumerated value as an index into either `root_values` of the ENUMERATED or
/// `ext_values` of the ENUMERATED and also decodes a flag indicating where the value belongs. If
/// `false` the value is from the `root_values`, else the value is from the `ext_values` of the
/// ENUMERATED.
pub fn decode_enumerated(
    data: &mut PerCodecData,
    lb: Option<i128>,
    ub: Option<i128>,
    is_extensible: bool,
) -> Result<(i128, bool), PerCodecError> {

    decode_enumerated_common(data, lb, ub, is_extensible, true)
}

/// Decode a Bit String
///
/// Decodes the value of the BIT STRING from the Buffer.
pub fn decode_bitstring(
    data: &mut PerCodecData,
    lb: Option<i128>,
    ub: Option<i128>,
    is_extensible: bool,
) -> Result<BitVec<u8, Msb0>, PerCodecError> {

    decode_bitstring_common(data, lb, ub, is_extensible, true)
}

/// Decode an OCTET STRING
///
/// Decodes the value of the OCTET STRING from the Buffer.
pub fn decode_octetstring(
    data: &mut PerCodecData,
    lb: Option<i128>,
    ub: Option<i128>,
    is_extensible: bool,
) -> Result<Vec<u8>, PerCodecError> {

    decode_octetstring_common(data, lb, ub, is_extensible, true)
}

/// Decodes a Length determinent
pub fn decode_length_determinent(
    data: &mut PerCodecData,
    lb: Option<i128>,
    ub: Option<i128>,
    normally_small: bool,
) -> Result<usize, PerCodecError> {

    decode_length_determinent_common(data, lb, ub, normally_small, true)
}

mod decode_charstrings;
pub use decode_charstrings::*;