-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(asn1-parser): implement Null type
- Loading branch information
1 parent
49ebb71
commit 69c87c5
Showing
5 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod boolean; | ||
mod null; | ||
|
||
pub use boolean::Bool; | ||
pub use null::Null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use alloc::borrow::Cow; | ||
use alloc::boxed::Box; | ||
|
||
use crate::length::{read_len, write_len}; | ||
use crate::reader::{read_data, Reader}; | ||
use crate::writer::Writer; | ||
use crate::{Asn1, Asn1Decoder, Asn1Encoder, Asn1Entity, Asn1Result, Asn1Type, Error, RawAsn1EntityData, Tag}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Default)] | ||
pub struct Null; | ||
|
||
impl Null { | ||
pub const TAG: Tag = Tag(5); | ||
} | ||
|
||
impl Asn1Entity for Null { | ||
fn tag(&self) -> Tag { | ||
Self::TAG | ||
} | ||
} | ||
|
||
impl<'data> Asn1Decoder<'data> for Null { | ||
fn compare_tags(tag: &Tag) -> bool { | ||
*tag == Self::TAG | ||
} | ||
|
||
fn decode(reader: &mut Reader<'data>) -> Asn1Result<Self> { | ||
check_tag!(in: reader); | ||
|
||
let (len, _len_range) = read_len(reader)?; | ||
|
||
if len != 0 { | ||
return Err(Error::from("Bool length must be equal to 0")); | ||
} | ||
|
||
Ok(Self) | ||
} | ||
|
||
fn decode_asn1(reader: &mut Reader<'data>) -> Asn1Result<Asn1<'data>> { | ||
let tag_position = reader.position(); | ||
check_tag!(in: reader); | ||
|
||
let (len, len_range) = read_len(reader)?; | ||
|
||
if len != 0 { | ||
return Err(Error::from("Bool length must be equal to 0")); | ||
} | ||
|
||
let (_, data_range) = read_data(reader, len)?; | ||
|
||
Ok(Asn1 { | ||
raw_data: RawAsn1EntityData { | ||
raw_data: Cow::Borrowed(reader.data_in_range(tag_position..data_range.end)?), | ||
tag: tag_position, | ||
length: len_range, | ||
data: data_range, | ||
}, | ||
asn1_type: Box::new(Asn1Type::Null(Self)), | ||
}) | ||
} | ||
} | ||
|
||
impl Asn1Encoder for Null { | ||
fn needed_buf_size(&self) -> usize { | ||
1 /* tag */ + 1 /* length (always zero) */ | ||
} | ||
|
||
fn encode(&self, writer: &mut Writer) -> Asn1Result<()> { | ||
writer.write_byte(Self::TAG.into())?; | ||
write_len(0, writer)?; | ||
|
||
Ok(()) | ||
} | ||
} |