Skip to content

Commit c9a1956

Browse files
authored
Batch 3: Add data_length implementations (#555)
- Add data_length to X509GeneralizedTime (15 bytes for YYYYMMDDHHMMSSZ) - Add data_length to GeneralizedTime (variable length due to fractional seconds)
1 parent 809ba21 commit c9a1956

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ impl SimpleAsn1Writable for X509GeneralizedTime {
12231223

12241224
dest.push_byte(b'Z')
12251225
}
1226+
1227+
fn data_length(&self) -> Option<usize> {
1228+
Some(15) // YYYYMMDDHHMMSSZ
1229+
}
12261230
}
12271231

12281232
/// Used for parsing and writing ASN.1 `GENERALIZED TIME` values,
@@ -1342,6 +1346,19 @@ impl SimpleAsn1Writable for GeneralizedTime {
13421346

13431347
dest.push_byte(b'Z')
13441348
}
1349+
1350+
fn data_length(&self) -> Option<usize> {
1351+
let base_len = 15; // YYYYMMDDHHMMSSZ
1352+
if let Some(nanoseconds) = self.nanoseconds() {
1353+
let mut buf = itoa::Buffer::new();
1354+
let nanos = buf.format(nanoseconds);
1355+
let pad = 9 - nanos.len();
1356+
let nanos = nanos.trim_end_matches('0');
1357+
Some(base_len + 1 + pad + nanos.len()) // . + padded nanos
1358+
} else {
1359+
Some(base_len)
1360+
}
1361+
}
13451362
}
13461363

13471364
/// An ASN.1 `ENUMERATED` value.

0 commit comments

Comments
 (0)