11//! Length calculations for encoded ASN.1 DER values
22
3+ #[ cfg( feature = "ber" ) ]
34pub ( crate ) mod indefinite;
45
5- use self :: indefinite:: INDEFINITE_LENGTH_OCTET ;
6+ /// Octet identifying an indefinite length as described in X.690 Section
7+ /// 8.1.3.6.1:
8+ ///
9+ /// > The single octet shall have bit 8 set to one, and bits 7 to
10+ /// > 1 set to zero.
11+ pub ( super ) const INDEFINITE_LENGTH_OCTET : u8 = 0b10000000 ; // 0x80
12+
613use crate :: { Decode , DerOrd , Encode , EncodingRules , Error , ErrorKind , Reader , Result , Tag , Writer } ;
714use core:: {
815 cmp:: Ordering ,
@@ -20,6 +27,7 @@ pub struct Length {
2027 /// Flag bit which specifies whether the length was indeterminate when decoding ASN.1 BER.
2128 ///
2229 /// This should always be false when working with DER.
30+ #[ cfg( feature = "ber" ) ]
2331 indefinite : bool ,
2432}
2533
@@ -34,6 +42,7 @@ impl Length {
3442 pub const MAX : Self = Self :: new ( u32:: MAX ) ;
3543
3644 /// Length of end-of-content octets (i.e. `00 00`).
45+ #[ cfg( feature = "ber" ) ]
3746 pub ( crate ) const EOC_LEN : Self = Self :: new ( 2 ) ;
3847
3948 /// Maximum number of octets in a DER encoding of a [`Length`] using the
@@ -46,6 +55,8 @@ impl Length {
4655 pub const fn new ( value : u32 ) -> Self {
4756 Self {
4857 inner : value,
58+
59+ #[ cfg( feature = "ber" ) ]
4960 indefinite : false ,
5061 }
5162 }
@@ -68,6 +79,7 @@ impl Length {
6879 }
6980
7081 /// Was this length decoded from an indefinite length when decoding BER?
82+ #[ cfg( feature = "ber" ) ]
7183 pub ( crate ) const fn is_indefinite ( self ) -> bool {
7284 self . indefinite
7385 }
@@ -94,6 +106,7 @@ impl Length {
94106 /// Otherwise (as should always be the case with DER), the length is unchanged.
95107 ///
96108 /// This method notably preserves the `indefinite` flag when performing arithmetic.
109+ #[ cfg( feature = "ber" ) ]
97110 pub ( crate ) fn sans_eoc ( self ) -> Self {
98111 if self . indefinite {
99112 // We expect EOC to be present when this is called.
@@ -104,6 +117,7 @@ impl Length {
104117 indefinite : true ,
105118 }
106119 } else {
120+ // Return DER length
107121 self
108122 }
109123 }
@@ -250,6 +264,7 @@ impl<'a> Decode<'a> for Length {
250264 // Note: per X.690 Section 8.1.3.6.1 the byte 0x80 encodes indefinite lengths
251265 INDEFINITE_LENGTH_OCTET => match reader. encoding_rules ( ) {
252266 // Indefinite lengths are allowed when decoding BER
267+ #[ cfg( feature = "ber" ) ]
253268 EncodingRules :: Ber => indefinite:: decode_indefinite_length ( & mut reader. clone ( ) ) ,
254269 // Indefinite lengths are disallowed when decoding DER
255270 EncodingRules :: Der => Err ( reader. error ( ErrorKind :: IndefiniteLength ) ) ,
@@ -333,11 +348,12 @@ impl DerOrd for Length {
333348
334349impl fmt:: Debug for Length {
335350 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
351+ #[ cfg( feature = "ber" ) ]
336352 if self . indefinite {
337- write ! ( f, "Length({self} [indefinite])" )
338- } else {
339- f. debug_tuple ( "Length" ) . field ( & self . inner ) . finish ( )
353+ return write ! ( f, "Length([indefinite])" ) ;
340354 }
355+
356+ f. debug_tuple ( "Length" ) . field ( & self . inner ) . finish ( )
341357 }
342358}
343359
0 commit comments