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

Improve Bool parsing #74

Merged
merged 1 commit into from
Aug 23, 2024
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
10 changes: 7 additions & 3 deletions crates/asn1-parser/src/primitives/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ impl<'data> Asn1ValueDecoder<'data> for Bool {
fn decode(_: Tag, reader: &mut Reader<'data>) -> Asn1Result<Self> {
let data = reader.remaining();

if data.len() != 1 {
if data.is_empty() {
warn!("Bool data length is 0. Processing with the default value: `true`");

Ok(Bool::from_byte(1))
} else if data.len() == 1 {
Ok(Bool::from_byte(data[0]))
} else {
return Err(Error::from("Bool data len should be equal to 1"));
}

Ok(Bool::from_byte(data[0]))
}

fn compare_tags(tag: Tag) -> bool {
Expand Down
65 changes: 65 additions & 0 deletions crates/asn1-parser/tests/decode_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,68 @@ fn issue_68_1() {
let asn1 = Asn1::decode_buff(raw).unwrap();
println!("{:?}", asn1);
}

#[test]
fn test_1() {
init_logging();

let raw = &[
96, 130, 5, 35, 6, 10, 42, 134, 72, 134, 247, 18, 1, 2, 2, 3, 1, 0, 110, 130, 5, 17, 48, 130, 5, 13, 160, 3, 2,
1, 5, 161, 3, 2, 1, 14, 162, 7, 3, 5, 0, 96, 0, 0, 0, 163, 130, 4, 42, 97, 130, 4, 38, 48, 130, 4, 34, 160, 3,
2, 1, 5, 161, 9, 27, 7, 84, 66, 84, 46, 67, 79, 77, 162, 45, 48, 43, 160, 3, 2, 1, 2, 161, 36, 48, 34, 27, 7,
84, 69, 82, 77, 83, 82, 86, 27, 23, 68, 69, 83, 75, 84, 79, 80, 45, 56, 70, 51, 51, 82, 70, 72, 46, 116, 98,
116, 46, 99, 111, 109, 163, 130, 3, 223, 48, 130, 3, 219, 160, 3, 2, 1, 18, 162, 130, 3, 210, 4, 130, 3, 206,
158, 158, 244, 176, 66, 8, 178, 207, 41, 230, 24, 88, 30, 16, 83, 38, 203, 59, 153, 73, 96, 76, 210, 66, 72,
97, 246, 44, 194, 42, 204, 55, 162, 231, 206, 155, 158, 112, 244, 74, 96, 81, 82, 185, 79, 236, 126, 64, 3,
130, 228, 86, 107, 174, 41, 64, 249, 79, 171, 3, 58, 48, 178, 141, 44, 191, 136, 148, 30, 3, 241, 140, 238,
211, 156, 166, 83, 67, 138, 179, 162, 13, 70, 5, 226, 122, 60, 114, 26, 98, 207, 242, 51, 89, 105, 145, 167,
66, 105, 240, 105, 4, 134, 16, 201, 21, 136, 181, 72, 79, 217, 15, 35, 219, 71, 22, 26, 127, 134, 86, 115, 107,
93, 180, 120, 147, 222, 177, 159, 8, 35, 153, 83, 75, 149, 196, 219, 161, 253, 107, 9, 96, 103, 200, 221, 147,
75, 130, 119, 161, 20, 250, 232, 217, 143, 248, 91, 167, 35, 27, 247, 131, 64, 41, 105, 222, 101, 187, 95, 229,
226, 99, 128, 204, 165, 113, 2, 116, 159, 199, 165, 212, 38, 40, 97, 31, 62, 144, 62, 0, 151, 103, 3, 206, 81,
230, 10, 144, 110, 113, 250, 247, 95, 127, 114, 131, 167, 5, 189, 201, 42, 95, 8, 209, 85, 145, 163, 132, 178,
198, 222, 196, 196, 26, 64, 97, 91, 70, 69, 110, 201, 113, 177, 27, 26, 173, 142, 49, 30, 3, 121, 252, 90, 71,
236, 24, 137, 72, 97, 48, 194, 234, 6, 38, 5, 152, 126, 238, 244, 219, 198, 148, 239, 27, 230, 234, 41, 28,
200, 162, 226, 69, 21, 234, 209, 19, 103, 82, 6, 74, 45, 100, 175, 183, 104, 186, 18, 76, 12, 250, 198, 43, 30,
180, 119, 133, 202, 147, 123, 20, 192, 85, 99, 223, 226, 183, 2, 208, 163, 154, 146, 60, 246, 249, 53, 112, 38,
30, 75, 214, 187, 30, 193, 254, 132, 232, 91, 222, 189, 189, 185, 89, 156, 207, 53, 10, 108, 92, 87, 194, 129,
130, 140, 44, 85, 93, 7, 19, 150, 127, 231, 52, 134, 173, 232, 151, 215, 71, 59, 244, 236, 122, 88, 117, 110,
20, 148, 234, 34, 44, 131, 128, 117, 230, 181, 110, 226, 33, 160, 218, 181, 95, 75, 77, 159, 21, 14, 249, 228,
85, 85, 8, 100, 0, 89, 15, 132, 166, 171, 206, 22, 176, 38, 20, 0, 67, 76, 99, 22, 215, 98, 72, 43, 226, 152,
52, 121, 128, 49, 68, 250, 247, 66, 185, 34, 102, 111, 247, 13, 177, 15, 37, 101, 226, 216, 51, 14, 30, 90,
222, 85, 208, 28, 116, 56, 158, 35, 154, 32, 253, 211, 101, 13, 207, 233, 53, 211, 193, 79, 171, 174, 56, 210,
142, 84, 55, 4, 231, 118, 149, 175, 51, 229, 7, 161, 250, 56, 92, 222, 67, 35, 139, 150, 162, 214, 1, 110, 127,
113, 162, 11, 110, 6, 87, 75, 204, 234, 101, 205, 194, 208, 45, 197, 223, 89, 33, 228, 165, 65, 95, 178, 154,
187, 5, 98, 110, 163, 90, 241, 165, 60, 215, 0, 196, 183, 207, 108, 136, 35, 225, 187, 188, 133, 13, 253, 223,
34, 25, 85, 192, 255, 150, 168, 188, 188, 108, 9, 92, 145, 204, 170, 111, 79, 89, 193, 224, 224, 115, 71, 38,
171, 245, 137, 147, 35, 73, 174, 252, 106, 107, 7, 139, 78, 131, 173, 209, 6, 159, 239, 229, 104, 18, 77, 68,
132, 162, 170, 31, 151, 14, 58, 4, 107, 221, 46, 120, 123, 102, 247, 95, 129, 31, 253, 194, 15, 93, 205, 9, 42,
110, 44, 126, 132, 214, 24, 54, 125, 226, 21, 32, 63, 69, 175, 3, 160, 105, 251, 200, 87, 121, 94, 162, 159,
212, 153, 2, 141, 193, 255, 199, 45, 246, 190, 239, 72, 162, 193, 92, 92, 225, 57, 107, 192, 196, 233, 185,
164, 78, 230, 10, 183, 185, 14, 75, 163, 16, 72, 203, 242, 123, 14, 149, 180, 227, 23, 238, 148, 247, 117, 39,
168, 240, 129, 193, 86, 59, 205, 214, 139, 98, 20, 254, 19, 173, 210, 60, 65, 82, 126, 221, 190, 43, 57, 220,
102, 253, 136, 87, 107, 16, 196, 192, 241, 176, 188, 254, 199, 190, 22, 224, 23, 254, 236, 8, 234, 169, 217,
109, 238, 97, 7, 121, 253, 129, 255, 148, 35, 71, 114, 36, 7, 240, 118, 28, 235, 64, 70, 110, 172, 47, 1, 64,
53, 187, 188, 222, 48, 119, 36, 111, 64, 23, 85, 119, 59, 50, 89, 10, 80, 43, 47, 27, 241, 59, 143, 217, 230,
202, 73, 202, 21, 124, 207, 134, 35, 246, 172, 216, 149, 223, 125, 101, 124, 76, 112, 85, 64, 118, 143, 36,
118, 223, 186, 219, 197, 232, 247, 16, 59, 181, 111, 103, 52, 195, 128, 234, 111, 159, 37, 117, 37, 115, 21,
102, 173, 197, 175, 169, 138, 237, 185, 44, 165, 155, 49, 22, 83, 138, 57, 47, 156, 35, 222, 65, 205, 27, 158,
16, 100, 126, 255, 227, 56, 11, 41, 125, 129, 207, 246, 83, 20, 127, 0, 49, 74, 183, 243, 64, 63, 86, 206, 56,
37, 194, 20, 231, 223, 136, 204, 54, 97, 197, 31, 103, 74, 35, 104, 13, 67, 23, 43, 148, 43, 136, 230, 122,
239, 63, 34, 114, 45, 167, 145, 33, 56, 164, 184, 106, 198, 96, 7, 13, 220, 177, 243, 222, 27, 249, 252, 121,
66, 168, 205, 31, 44, 176, 94, 100, 45, 104, 103, 176, 233, 50, 49, 191, 55, 244, 107, 64, 153, 29, 181, 213,
1, 201, 142, 246, 181, 114, 93, 141, 30, 143, 40, 30, 163, 60, 12, 169, 53, 89, 131, 254, 62, 4, 161, 161, 73,
62, 133, 75, 63, 37, 164, 129, 201, 48, 129, 198, 160, 3, 2, 1, 18, 162, 129, 190, 4, 129, 187, 86, 189, 100,
52, 67, 93, 247, 236, 239, 108, 242, 195, 180, 144, 1, 161, 13, 24, 16, 244, 175, 190, 119, 19, 7, 90, 1, 39,
243, 220, 245, 18, 38, 242, 80, 81, 174, 62, 28, 90, 136, 13, 8, 37, 240, 80, 120, 192, 158, 132, 242, 107,
249, 63, 131, 88, 221, 76, 62, 185, 214, 174, 251, 14, 72, 157, 184, 142, 4, 150, 128, 40, 74, 58, 6, 202, 78,
162, 183, 19, 28, 192, 212, 202, 10, 85, 102, 219, 201, 73, 104, 18, 229, 16, 25, 94, 74, 149, 74, 184, 235,
199, 151, 5, 240, 30, 83, 110, 69, 216, 47, 177, 38, 199, 63, 145, 0, 135, 51, 8, 43, 196, 235, 140, 114, 71,
37, 89, 220, 122, 14, 120, 85, 183, 6, 150, 243, 147, 20, 45, 201, 54, 127, 76, 215, 79, 79, 94, 166, 237, 1,
141, 68, 201, 203, 211, 73, 21, 67, 121, 15, 86, 218, 109, 224, 120, 170, 147, 159, 68, 74, 172, 80, 38, 14,
173, 236, 149, 60, 97, 13, 39, 194, 59, 114, 52, 36,
];

let asn1 = Asn1::decode_buff(raw).unwrap();
println!("{:?}", asn1);
}