Skip to content

Commit 8b22c71

Browse files
authored
Implement data_length for many types (#552)
1 parent f3bd344 commit 8b22c71

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/types.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ impl SimpleAsn1Writable for Null {
261261
fn write_data(&self, _dest: &mut WriteBuf) -> WriteResult {
262262
Ok(())
263263
}
264+
265+
fn data_length(&self) -> Option<usize> {
266+
Some(0)
267+
}
264268
}
265269

266270
impl SimpleAsn1Readable<'_> for bool {
@@ -357,6 +361,10 @@ impl<T: Asn1Writable> SimpleAsn1Writable for OctetStringEncoded<T> {
357361
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
358362
self.0.write(&mut Writer::new(dest))
359363
}
364+
365+
fn data_length(&self) -> Option<usize> {
366+
self.0.encoded_length()
367+
}
360368
}
361369

362370
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -428,6 +436,10 @@ impl SimpleAsn1Writable for PrintableString<'_> {
428436
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
429437
dest.push_slice(self.0.as_bytes())
430438
}
439+
440+
fn data_length(&self) -> Option<usize> {
441+
Some(self.0.len())
442+
}
431443
}
432444

433445
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -477,6 +489,10 @@ impl SimpleAsn1Writable for IA5String<'_> {
477489
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
478490
dest.push_slice(self.0.as_bytes())
479491
}
492+
493+
fn data_length(&self) -> Option<usize> {
494+
Some(self.0.len())
495+
}
480496
}
481497

482498
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -510,6 +526,10 @@ impl SimpleAsn1Writable for Utf8String<'_> {
510526
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
511527
dest.push_slice(self.0.as_bytes())
512528
}
529+
530+
fn data_length(&self) -> Option<usize> {
531+
Some(self.0.len())
532+
}
513533
}
514534

515535
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -565,6 +585,10 @@ impl SimpleAsn1Writable for VisibleString<'_> {
565585
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
566586
dest.push_slice(self.0.as_bytes())
567587
}
588+
589+
fn data_length(&self) -> Option<usize> {
590+
Some(self.0.len())
591+
}
568592
}
569593

570594
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -615,6 +639,10 @@ impl SimpleAsn1Writable for BMPString<'_> {
615639
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
616640
dest.push_slice(self.as_utf16_be_bytes())
617641
}
642+
643+
fn data_length(&self) -> Option<usize> {
644+
Some(self.as_utf16_be_bytes().len())
645+
}
618646
}
619647

620648
/// Type for use with `Parser.read_element` and `Writer.write_element` for
@@ -665,6 +693,10 @@ impl SimpleAsn1Writable for UniversalString<'_> {
665693
fn write_data(&self, dest: &mut WriteBuf) -> WriteResult {
666694
dest.push_slice(self.as_utf32_be_bytes())
667695
}
696+
697+
fn data_length(&self) -> Option<usize> {
698+
Some(self.as_utf32_be_bytes().len())
699+
}
668700
}
669701

670702
const fn validate_integer(data: &[u8], signed: bool) -> ParseResult<()> {
@@ -1182,6 +1214,10 @@ impl SimpleAsn1Writable for UtcTime {
11821214

11831215
dest.push_byte(b'Z')
11841216
}
1217+
1218+
fn data_length(&self) -> Option<usize> {
1219+
Some(13)
1220+
}
11851221
}
11861222

11871223
/// Used for parsing and writing ASN.1 `GENERALIZED TIME` values used in X.509.

src/writer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,14 @@ mod tests {
633633
.unwrap(),
634634
b"\x30\x03\x01\x01\xff"
635635
);
636+
assert_eq!(
637+
write(|w| {
638+
w.write_element(&SequenceWriter::new(&|w: &mut Writer<'_>| {
639+
w.write_element(&b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
640+
}))
641+
}).unwrap(),
642+
b"\x30\x81\x84\x04\x81\x81aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
643+
);
636644

637645
assert_writes(&[(
638646
parse_single::<Sequence<'_>>(b"\x30\x06\x01\x01\xff\x02\x01\x06").unwrap(),

0 commit comments

Comments
 (0)