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 12a1203
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 145 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
Loading

0 comments on commit 12a1203

Please sign in to comment.