encoding_asn1/
types.rs

1use crate::common;
2use crate::marshal;
3use crate::marshal::Encoder;
4use crate::unmarshal;
5
6pub type OctetString = Vec<u8>;
7
8#[derive(Debug)]
9pub struct RawValue {
10    pub class: i32,
11    pub tag: i32,
12    pub is_compound: bool,
13    pub bytes: Vec<u8>,
14    pub full_bytes: Vec<u8>, // includes the tag and length
15}
16
17impl marshal::Marshaler for RawValue {
18    fn marshal_with_params(&self, _params: &common::FieldParameters) -> Vec<u8> {
19        if self.full_bytes.len() > 0 {
20            return self.full_bytes.to_vec();
21        }
22
23        let t = marshal::TaggedEncoder {
24            tag: common::TagAndLength {
25                class: self.class,
26                is_compound: self.is_compound,
27                length: self.bytes.len(),
28                tag: self.tag,
29            },
30            body: self.bytes.to_vec(),
31        };
32
33        t.encode()
34    }
35}
36
37impl unmarshal::Unmarshaler<RawValue> for RawValue {
38    fn unmarshal_with_params<'a>(
39        bytes: &'a [u8],
40        _params: &common::FieldParameters,
41    ) -> Result<(RawValue, &'a [u8]), unmarshal::Error> {
42        println!("1111 bytes: {:02X?}", bytes);
43        let (tag_and_length, bytes) = unmarshal::parse_tag_and_length(bytes)?;
44        println!("tag_and_length: {:?}", tag_and_length);
45        println!("bytes: {:02X?}", bytes);
46
47        let rv = RawValue {
48            class: tag_and_length.class,
49            tag: tag_and_length.tag,
50            is_compound: tag_and_length.is_compound,
51            bytes: bytes[bytes.len() - tag_and_length.length..].to_vec(),
52            full_bytes: bytes.to_vec(),
53        };
54
55        Ok((rv, &bytes[(tag_and_length.length as usize)..]))
56    }
57}