@@ -7,9 +7,6 @@ use core::{
77 ops:: { Add , Sub } ,
88} ;
99
10- /// Maximum length as a `u32` (256 MiB).
11- const MAX_U32 : u32 = 0xfff_ffff ;
12-
1310/// Octet identifying an indefinite length as described in X.690 Section
1411/// 8.1.3.6.1:
1512///
@@ -18,8 +15,6 @@ const MAX_U32: u32 = 0xfff_ffff;
1815const INDEFINITE_LENGTH_OCTET : u8 = 0b10000000 ; // 0x80
1916
2017/// ASN.1-encoded length.
21- ///
22- /// Maximum length is defined by the [`Length::MAX`] constant (256 MiB).
2318#[ derive( Copy , Clone , Debug , Default , Eq , Hash , PartialEq , PartialOrd , Ord ) ]
2419pub struct Length ( u32 ) ;
2520
@@ -30,8 +25,8 @@ impl Length {
3025 /// Length of `1`
3126 pub const ONE : Self = Self ( 1 ) ;
3227
33- /// Maximum length currently supported: 256 MiB
34- pub const MAX : Self = Self ( MAX_U32 ) ;
28+ /// Maximum length (`u32::MAX`).
29+ pub const MAX : Self = Self ( u32 :: MAX ) ;
3530
3631 /// Maximum number of octets in a DER encoding of a [`Length`] using the
3732 /// rules implemented by this crate.
@@ -94,7 +89,7 @@ impl Length {
9489 0x80 ..=0xFF => Some ( 0x81 ) ,
9590 0x100 ..=0xFFFF => Some ( 0x82 ) ,
9691 0x10000 ..=0xFFFFFF => Some ( 0x83 ) ,
97- 0x1000000 ..=MAX_U32 => Some ( 0x84 ) ,
92+ 0x1000000 ..=0xFFFFFFFF => Some ( 0x84 ) ,
9893 _ => None ,
9994 }
10095 }
@@ -107,7 +102,7 @@ impl Add for Length {
107102 self . 0
108103 . checked_add ( other. 0 )
109104 . ok_or_else ( || ErrorKind :: Overflow . into ( ) )
110- . and_then ( TryInto :: try_into )
105+ . map ( Self )
111106 }
112107}
113108
@@ -131,7 +126,7 @@ impl Add<u32> for Length {
131126 type Output = Result < Self > ;
132127
133128 fn add ( self , other : u32 ) -> Result < Self > {
134- self + Length :: try_from ( other) ?
129+ self + Length :: from ( other)
135130 }
136131}
137132
@@ -158,7 +153,7 @@ impl Sub for Length {
158153 self . 0
159154 . checked_sub ( other. 0 )
160155 . ok_or_else ( || ErrorKind :: Overflow . into ( ) )
161- . and_then ( TryInto :: try_into )
156+ . map ( Self )
162157 }
163158}
164159
@@ -182,21 +177,15 @@ impl From<u16> for Length {
182177 }
183178}
184179
185- impl From < Length > for u32 {
186- fn from ( length : Length ) -> u32 {
187- length . 0
180+ impl From < u32 > for Length {
181+ fn from ( len : u32 ) -> Length {
182+ Length ( len )
188183 }
189184}
190185
191- impl TryFrom < u32 > for Length {
192- type Error = Error ;
193-
194- fn try_from ( len : u32 ) -> Result < Length > {
195- if len <= Self :: MAX . 0 {
196- Ok ( Length ( len) )
197- } else {
198- Err ( ErrorKind :: Overflow . into ( ) )
199- }
186+ impl From < Length > for u32 {
187+ fn from ( length : Length ) -> u32 {
188+ length. 0
200189 }
201190}
202191
@@ -236,7 +225,7 @@ impl<'a> Decode<'a> for Length {
236225 | u32:: from ( reader. read_byte ( ) ?) ;
237226 }
238227
239- let length = Length :: try_from ( decoded_len) ? ;
228+ let length = Length :: from ( decoded_len) ;
240229
241230 // X.690 Section 10.1: DER lengths must be encoded with a minimum
242231 // number of octets
@@ -261,8 +250,7 @@ impl Encode for Length {
261250 0x80 ..=0xFF => Ok ( Length ( 2 ) ) ,
262251 0x100 ..=0xFFFF => Ok ( Length ( 3 ) ) ,
263252 0x10000 ..=0xFFFFFF => Ok ( Length ( 4 ) ) ,
264- 0x1000000 ..=MAX_U32 => Ok ( Length ( 5 ) ) ,
265- _ => Err ( ErrorKind :: Overflow . into ( ) ) ,
253+ 0x1000000 ..=0xFFFFFFFF => Ok ( Length ( 5 ) ) ,
266254 }
267255 }
268256
@@ -311,7 +299,7 @@ impl fmt::Display for Length {
311299#[ cfg( feature = "arbitrary" ) ]
312300impl < ' a > arbitrary:: Arbitrary < ' a > for Length {
313301 fn arbitrary ( u : & mut arbitrary:: Unstructured < ' a > ) -> arbitrary:: Result < Self > {
314- Ok ( Self ( u. int_in_range ( 0 ..= MAX_U32 ) ?) )
302+ Ok ( Self ( u. arbitrary ( ) ?) )
315303 }
316304
317305 fn size_hint ( depth : usize ) -> ( usize , Option < usize > ) {
@@ -454,7 +442,7 @@ mod tests {
454442 ) ;
455443
456444 assert_eq ! (
457- Length :: try_from ( 0x10000u32 ) . unwrap ( ) ,
445+ Length :: from ( 0x10000u32 ) ,
458446 Length :: from_der( & [ 0x83 , 0x01 , 0x00 , 0x00 ] ) . unwrap( )
459447 ) ;
460448 }
@@ -487,8 +475,7 @@ mod tests {
487475
488476 assert_eq ! (
489477 & [ 0x83 , 0x01 , 0x00 , 0x00 ] ,
490- Length :: try_from( 0x10000u32 )
491- . unwrap( )
478+ Length :: from( 0x10000u32 )
492479 . encode_to_slice( & mut buffer)
493480 . unwrap( )
494481 ) ;
@@ -507,10 +494,7 @@ mod tests {
507494 // It also supports definite lengths.
508495 let length = IndefiniteLength :: from_der ( & [ 0x83 , 0x01 , 0x00 , 0x00 ] ) . unwrap ( ) ;
509496 assert ! ( length. is_definite( ) ) ;
510- assert_eq ! (
511- Length :: try_from( 0x10000u32 ) . unwrap( ) ,
512- length. try_into( ) . unwrap( )
513- ) ;
497+ assert_eq ! ( Length :: from( 0x10000u32 ) , length. try_into( ) . unwrap( ) ) ;
514498 }
515499
516500 #[ test]
0 commit comments