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 long form tag decoding #54

Merged
merged 1 commit into from
Nov 23, 2021
Merged
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
15 changes: 10 additions & 5 deletions src/ber/de/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,19 @@ impl Appendable for crate::types::BitString {
/// Concatenates a series of 7 bit numbers delimited by `1`'s and
/// ended by a `0` in the 8th bit.
fn concat_number(body: &[u8], start: u8) -> Integer {
let mut number = Integer::from(start);
let start = Integer::from(start);
if body.is_empty() {
return start;
}

let mut number = Integer::from(body[0] & 0x7F);

for byte in body.iter().rev() {
for byte in body[1..].iter() {
number <<= 7usize;
number |= Integer::from(byte & 0x7F);
}

number
(number << 7usize) | start
}

fn take_contents(input: &[u8], length: u8) -> IResult<&[u8], &[u8]> {
Expand Down Expand Up @@ -237,9 +242,9 @@ mod tests {

#[test]
fn long_tag() {
let (_, identifier) = parse_identifier_octet([0xFF, 0x80, 0x7F][..].into()).unwrap();
let (_, identifier) = parse_identifier_octet([0xFF, 0x83, 0x7F][..].into()).unwrap();
assert!(identifier.is_constructed);
assert_eq!(Tag::new(Class::Private, 16256), identifier.tag);
assert_eq!(Tag::new(Class::Private, 511), identifier.tag);
}

#[test]
Expand Down