Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #192, add APER ExtensiblePersonnelRecord with bug fixes #199

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/aper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,30 @@ mod tests {
&[0x01]
);
}
#[test]
fn issue_192() {
// https://github.com/XAMPPRocky/rasn/issues/192
use crate as rasn;

use rasn::AsnType;

#[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>,
}

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

let msg = Message::Updates(Updates { updates: vec![1] });

round_trip!(aper, Message, msg, &[0, 1, 1]);
}
}
3 changes: 3 additions & 0 deletions 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
15 changes: 14 additions & 1 deletion src/per/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ impl Encoder {
.iter()
.map(|field| (field.tag_tree.smallest_tag(), (field.presence, false)))
.collect();
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 @@ -130,6 +132,7 @@ impl Encoder {
.values()
.filter(|(presence, _)| presence.is_optional_or_default())
.count();

output_length += self.parent_output_length.unwrap_or_default();

if self.options.set_encoding {
Expand Down Expand Up @@ -435,7 +438,6 @@ impl Encoder {
range,
&(effective_length as u32).to_be_bytes(),
);

if is_large_string {
self.pad_to_alignment(buffer);
}
Expand Down Expand Up @@ -964,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 @@ -1070,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
21 changes: 17 additions & 4 deletions tests/personnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl rasn::Encode for PersonnelRecord {
let children = &self.children;
encoder
.encode_set::<Self, _>(tag, |encoder| {
dbg!(self.name.encode(encoder)?);
self.name.encode(encoder)?;
encoder.encode_explicit_prefix(
rasn::Tag::new(rasn::types::Class::Context, 0),
&self.title,
Expand Down Expand Up @@ -212,7 +212,7 @@ pub struct ExtensiblePersonnelRecord {
#[rasn(tag(explicit(2)))]
pub name_of_spouse: ExtensibleName,
#[rasn(tag(3), default, size(2, extensible))]
pub children: Vec<ExtensibleChildInformation>,
pub children: Option<Vec<ExtensibleChildInformation>>,
}

impl Default for ExtensiblePersonnelRecord {
Expand All @@ -223,10 +223,10 @@ impl Default for ExtensiblePersonnelRecord {
number: ExtensibleEmployeeNumber(51.into()),
date_of_hire: ExtensibleDate(VisibleString::try_from("19710917").unwrap()),
name_of_spouse: Name::mary().into(),
children: vec![
children: Some(vec![
ChildInformation::ralph().into(),
ExtensibleChildInformation::susan(),
],
]),
}
}
}
Expand Down Expand Up @@ -548,4 +548,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, 0x01, 0x40
];
}