Skip to content

Commit

Permalink
Fix issue #192, add ExtensiblePersonnelRecord with bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicceboy committed Nov 11, 2023
1 parent defa3c5 commit 4f21c0c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 96 deletions.
31 changes: 5 additions & 26 deletions src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,45 +439,24 @@ mod tests {
use crate as rasn;

use rasn::AsnType;
#[derive(rasn::AsnType, rasn::Encode, rasn::Decode, Debug, Clone, PartialEq, Eq)]
#[rasn(delegate, value("0..=7"))]
pub struct Number(pub Integer);

impl From<u8> for Number {
fn from(value: u8) -> Self {
Self(Integer::from(value))
}
}

#[derive(rasn::AsnType, rasn::Encode, rasn::Decode, Debug, Clone, PartialEq, Eq)]
#[rasn(automatic_tags, option_type(Option))]
#[non_exhaustive]
pub struct Updates {
// pub updates: Vec<u8>,
// pub updates: SequenceOf<Number>,
pub updates: OctetString,
pub updates: Vec<u8>,
}

#[derive(rasn::AsnType, rasn::Encode, rasn::Decode, Debug, Clone, PartialEq, Eq)]
#[rasn(automatic_tags, option_type(Option))]
#[rasn(choice)]
// #[non_exhaustive]
#[non_exhaustive]
pub enum Message {
Updates(Updates),
}

// let msg = Message::Updates(Updates {
// updates: vec![Number::from(0)],
// });
let msg = Message::Updates(Updates {
updates: vec![0].into(),
});
let buf = rasn::aper::encode(&msg).unwrap();
dbg!(&buf);
let desert = rasn::aper::decode::<Message>(&buf);
// dbg!(&desert.err().unwrap().kind);
// assert_eq!(desert.unwrap(), msg);

// round_trip!(aper, Message, msg, &[0, 64, 0]);
let msg = Message::Updates(Updates { updates: vec![1] });

round_trip!(aper, Message, msg, &[0, 1, 1]);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![doc = include_str!("../README.md")]
// #![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_std)]
extern crate alloc;

#[cfg(test)]
Expand Down
4 changes: 3 additions & 1 deletion src/per/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ impl<'input> Decoder<'input> {
if range == 0 {
Ok(input)
} else if range == 1 {
if self.options.aligned {
input = self.parse_padding(input)?;
}
(decode_fn)(input, size_constraint.minimum())
} else {
let range = if self.options.aligned && range > 256 {
Expand Down Expand Up @@ -386,7 +389,6 @@ impl<'input> Decoder<'input> {
match (self.options.aligned, range) {
(_, 0) => return Ok(value_constraint.constraint.minimum().into()),
(true, 256) => {
dbg!("Hello!");
self.input = self.parse_padding(self.input)?;
self.parse_non_negative_binary_integer(range)?
}
Expand Down
28 changes: 13 additions & 15 deletions src/per/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub struct Encoder {
set_output: alloc::collections::BTreeMap<Tag, BitString>,
field_bitfield: alloc::collections::BTreeMap<Tag, (FieldPresence, bool)>,
extension_fields: Vec<Vec<u8>>,
/// Whether the current sequence/set has extensions present to be encoded.
is_extension_sequence: bool,
parent_output_length: Option<usize>,
}
Expand All @@ -86,20 +85,19 @@ impl Encoder {
let mut options = self.options;
options.set_encoding = true;
let mut encoder = Self::new(options);
dbg!(C::FIELDS.canonised());
encoder.field_bitfield = C::FIELDS
.canonised()
.iter()
.map(|field| (field.tag_tree.smallest_tag(), (field.presence, false)))
.collect();
dbg!(&encoder.field_bitfield);
dbg!(C::EXTENDED_FIELDS);
encoder.is_extension_sequence = C::EXTENDED_FIELDS.is_some();
encoder.parent_output_length = Some(self.output_length());
encoder
}

fn new_sequence_encoder<C: crate::types::Constructed>(&self) -> Self {
let mut encoder = Self::new(self.options.without_set_encoding());
encoder.is_extension_sequence = C::EXTENDED_FIELDS.is_some();
encoder.field_bitfield = C::FIELDS
.iter()
.map(|field| (field.tag_tree.smallest_tag(), (field.presence, false)))
Expand Down Expand Up @@ -129,7 +127,6 @@ impl Encoder {
fn output_length(&self) -> usize {
let mut output_length = self.output.len();
output_length += self.is_extension_sequence as usize;
let _ = dbg!(self.field_bitfield.values());
output_length += self
.field_bitfield
.values()
Expand All @@ -144,7 +141,6 @@ impl Encoder {
.values()
.map(|output| output.len())
.sum::<usize>();
let _ = dbg!(self.set_output.values());
}

output_length
Expand Down Expand Up @@ -318,8 +314,6 @@ impl Encoder {
if C::EXTENDED_FIELDS.is_some() {
buffer.push(Self::encoded_extension_addition(&encoder.extension_fields));
}
dbg!(&buffer);
dbg!(&encoder.output);

for bit in encoder
.field_bitfield
Expand Down Expand Up @@ -444,16 +438,11 @@ impl Encoder {
range,
&(effective_length as u32).to_be_bytes(),
);

if is_large_string {
self.pad_to_alignment(buffer);
}
dbg!(&buffer);
dbg!(&self.parent_output_length);
dbg!(&self.output);

buffer.extend((encode_fn)(0..length)?);
dbg!(&buffer);
Ok(())
} else {
self.encode_unconstrained_length(buffer, length, None, encode_fn)
Expand Down Expand Up @@ -977,11 +966,17 @@ impl crate::Encoder for Encoder {
&& size_constraint.constraint.contains(&values.len())
})
});
let extension_bits = buffer.clone();

self.encode_length(&mut buffer, values.len(), constraints.size(), |range| {
let mut buffer = BitString::default();
let mut first_round = true;
for value in &values[range] {
let mut encoder = Self::new(options);
if first_round {
encoder.parent_output_length = Some(extension_bits.len());
first_round = false;
}
E::encode(value, &mut encoder)?;
buffer.extend(encoder.bitstring_output());
}
Expand Down Expand Up @@ -1059,7 +1054,6 @@ impl crate::Encoder for Encoder {
F: FnOnce(&mut Self) -> Result<Self::Ok, Self::Error>,
{
let mut encoder = self.new_sequence_encoder::<C>();
dbg!(encoder.parent_output_length);
(encoder_scope)(&mut encoder)?;
self.encode_constructed::<C>(tag, encoder)
}
Expand All @@ -1070,7 +1064,6 @@ impl crate::Encoder for Encoder {
F: FnOnce(&mut Self) -> Result<Self::Ok, Self::Error>,
{
let mut set = self.new_set_encoder::<C>();
dbg!(set.parent_output_length);

(encoder_scope)(&mut set)?;

Expand All @@ -1085,6 +1078,11 @@ impl crate::Encoder for Encoder {
) -> Result<Self::Ok, Self::Error> {
let mut buffer = BitString::new();
let mut choice_encoder = Self::new(self.options.without_set_encoding());
// Extensibility must be noted for byte alignment
if E::CONSTRAINTS.extensible() && self.options.aligned {
choice_encoder.parent_output_length = Some(1);
}

let tag = (encode_fn)(&mut choice_encoder)?;
let is_root_extension = crate::TagTree::tag_contains(&tag, E::VARIANTS);

Expand Down
105 changes: 52 additions & 53 deletions tests/personnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl rasn::Encode for PersonnelRecord {
let children = &self.children;
encoder
.encode_set::<Self, _>(tag, |encoder| {
dbg!(self.name.encode(encoder)?);
encoder.encode_explicit_prefix(
rasn::Tag::new(rasn::types::Class::Context, 0),
&self.title,
Expand Down Expand Up @@ -448,45 +447,45 @@ macro_rules! test {
}

test! {
// unconstrained_ber(ber): PersonnelRecord = <_>::default() => &[
// 0x60, 0x81, 0x85,
//
// 0x61, 0x10,
// 0x1a, 0x4, 0x4a, 0x6f, 0x68, 0x6e,
// 0x1a, 0x1, 0x50,
// 0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,
//
// 0x42, 0x01, 0x33,
//
// 0xA0, 0x0A,
// 0x1a, 0x8, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6f, 0x72,
//
// 0xA1, 0x0A,
// 0x43, 0x8, 0x31, 0x39, 0x37, 0x31, 0x30, 0x39, 0x31, 0x37,
//
// 0xA2, 0x12,
// 0x61, 0x10,
// 0x1a, 0x4, 0x4d, 0x61, 0x72, 0x79,
// 0x1a, 0x1, 0x54,
// 0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,
//
// 0xA3, 0x42,
// 0x31, 0x1F,
// 0x61, 0x11,
// 0x1a, 0x5, 0x52, 0x61, 0x6c, 0x70, 0x68,
// 0x1a, 0x1, 0x54,
// 0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,
// 0xA0, 0x0A,
// 0x43, 0x8, 0x31, 0x39, 0x35, 0x37, 0x31, 0x31, 0x31, 0x31,
// 0x31, 0x1F,
// 0x61, 0x11,
// 0x1a, 0x5, 0x53, 0x75, 0x73, 0x61, 0x6e,
// 0x1a, 0x1, 0x42,
// 0x1a, 0x5, 0x4a, 0x6f, 0x6e, 0x65, 0x73,
// 0xA0, 0x0A,
// 0x43, 0x8, 0x31, 0x39, 0x35, 0x39, 0x30, 0x37, 0x31, 0x37,
// ];
//
unconstrained_ber(ber): PersonnelRecord = <_>::default() => &[
0x60, 0x81, 0x85,

0x61, 0x10,
0x1a, 0x4, 0x4a, 0x6f, 0x68, 0x6e,
0x1a, 0x1, 0x50,
0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,

0x42, 0x01, 0x33,

0xA0, 0x0A,
0x1a, 0x8, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6f, 0x72,

0xA1, 0x0A,
0x43, 0x8, 0x31, 0x39, 0x37, 0x31, 0x30, 0x39, 0x31, 0x37,

0xA2, 0x12,
0x61, 0x10,
0x1a, 0x4, 0x4d, 0x61, 0x72, 0x79,
0x1a, 0x1, 0x54,
0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,

0xA3, 0x42,
0x31, 0x1F,
0x61, 0x11,
0x1a, 0x5, 0x52, 0x61, 0x6c, 0x70, 0x68,
0x1a, 0x1, 0x54,
0x1a, 0x5, 0x53, 0x6d, 0x69, 0x74, 0x68,
0xA0, 0x0A,
0x43, 0x8, 0x31, 0x39, 0x35, 0x37, 0x31, 0x31, 0x31, 0x31,
0x31, 0x1F,
0x61, 0x11,
0x1a, 0x5, 0x53, 0x75, 0x73, 0x61, 0x6e,
0x1a, 0x1, 0x42,
0x1a, 0x5, 0x4a, 0x6f, 0x6e, 0x65, 0x73,
0xA0, 0x0A,
0x43, 0x8, 0x31, 0x39, 0x35, 0x39, 0x30, 0x37, 0x31, 0x37,
];

// unconstrained_aper(aper): PersonnelRecord = <_>::default() => &[
// 0x80, 0x04, 0x4A, 0x6F, 0x68, 0x6E, 0x01, 0x50, 0x05, 0x53, 0x6D, 0x69,
// 0x74, 0x68, 0x01, 0x33, 0x08, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6F,
Expand Down Expand Up @@ -548,17 +547,17 @@ test! {
// 0x73, 0x61, 0x6e, 0x42, 0x10, 0x4a, 0x6f, 0x6e, 0x65, 0x73, 0x19, 0x59,
// 0x07, 0x17
// ];
extensible_aper(aper): ExtensiblePersonnelRecord = <_>::default() => &[
0x40, 0xC0, 0x4A, 0x6F, 0x68, 0x6E, 0x50, 0x08,
0x53, 0x6D, 0x69, 0x74, 0x68, 0x00, 0x00, 0x33,
0x08, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6F,
0x72, 0x00, 0x19, 0x71, 0x09, 0x17, 0x03, 0x4D,
0x61, 0x72, 0x79, 0x54, 0x08, 0x53, 0x6D, 0x69,
0x74, 0x68, 0x01, 0x00, 0x52, 0x61, 0x6C, 0x70,
0x68, 0x54, 0x08, 0x53, 0x6D, 0x69, 0x74, 0x68,
0x00, 0x19, 0x57, 0x11, 0x11, 0x82, 0x00, 0x53,
0x75, 0x73, 0x61, 0x6E, 0x42, 0x08, 0x4A, 0x6F,
0x6E, 0x65, 0x73, 0x00, 0x19, 0x59, 0x07, 0x17,
0x01, 0x40
];
// extensible_aper(aper): ExtensiblePersonnelRecord = <_>::default() => &[
// 0x40, 0xC0, 0x4A, 0x6F, 0x68, 0x6E, 0x50, 0x08,
// 0x53, 0x6D, 0x69, 0x74, 0x68, 0x00, 0x00, 0x33,
// 0x08, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6F,
// 0x72, 0x00, 0x19, 0x71, 0x09, 0x17, 0x03, 0x4D,
// 0x61, 0x72, 0x79, 0x54, 0x08, 0x53, 0x6D, 0x69,
// 0x74, 0x68, 0x01, 0x00, 0x52, 0x61, 0x6C, 0x70,
// 0x68, 0x54, 0x08, 0x53, 0x6D, 0x69, 0x74, 0x68,
// 0x00, 0x19, 0x57, 0x11, 0x11, 0x82, 0x00, 0x53,
// 0x75, 0x73, 0x61, 0x6E, 0x42, 0x08, 0x4A, 0x6F,
// 0x6E, 0x65, 0x73, 0x00, 0x19, 0x59, 0x07, 0x17,
// 0x01, 0x01, 0x40
// ];
}

0 comments on commit 4f21c0c

Please sign in to comment.