@@ -7,9 +7,6 @@ use core::{
7
7
ops:: { Add , Sub } ,
8
8
} ;
9
9
10
- /// Maximum length as a `u32` (256 MiB).
11
- const MAX_U32 : u32 = 0xfff_ffff ;
12
-
13
10
/// Octet identifying an indefinite length as described in X.690 Section
14
11
/// 8.1.3.6.1:
15
12
///
@@ -18,8 +15,6 @@ const MAX_U32: u32 = 0xfff_ffff;
18
15
const INDEFINITE_LENGTH_OCTET : u8 = 0b10000000 ; // 0x80
19
16
20
17
/// ASN.1-encoded length.
21
- ///
22
- /// Maximum length is defined by the [`Length::MAX`] constant (256 MiB).
23
18
#[ derive( Copy , Clone , Debug , Default , Eq , Hash , PartialEq , PartialOrd , Ord ) ]
24
19
pub struct Length ( u32 ) ;
25
20
@@ -30,8 +25,8 @@ impl Length {
30
25
/// Length of `1`
31
26
pub const ONE : Self = Self ( 1 ) ;
32
27
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 ) ;
35
30
36
31
/// Maximum number of octets in a DER encoding of a [`Length`] using the
37
32
/// rules implemented by this crate.
@@ -94,7 +89,7 @@ impl Length {
94
89
0x80 ..=0xFF => Some ( 0x81 ) ,
95
90
0x100 ..=0xFFFF => Some ( 0x82 ) ,
96
91
0x10000 ..=0xFFFFFF => Some ( 0x83 ) ,
97
- 0x1000000 ..=MAX_U32 => Some ( 0x84 ) ,
92
+ 0x1000000 ..=0xFFFFFFFF => Some ( 0x84 ) ,
98
93
_ => None ,
99
94
}
100
95
}
@@ -107,7 +102,7 @@ impl Add for Length {
107
102
self . 0
108
103
. checked_add ( other. 0 )
109
104
. ok_or_else ( || ErrorKind :: Overflow . into ( ) )
110
- . and_then ( TryInto :: try_into )
105
+ . map ( Self )
111
106
}
112
107
}
113
108
@@ -131,7 +126,7 @@ impl Add<u32> for Length {
131
126
type Output = Result < Self > ;
132
127
133
128
fn add ( self , other : u32 ) -> Result < Self > {
134
- self + Length :: try_from ( other) ?
129
+ self + Length :: from ( other)
135
130
}
136
131
}
137
132
@@ -158,7 +153,7 @@ impl Sub for Length {
158
153
self . 0
159
154
. checked_sub ( other. 0 )
160
155
. ok_or_else ( || ErrorKind :: Overflow . into ( ) )
161
- . and_then ( TryInto :: try_into )
156
+ . map ( Self )
162
157
}
163
158
}
164
159
@@ -182,21 +177,15 @@ impl From<u16> for Length {
182
177
}
183
178
}
184
179
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 )
188
183
}
189
184
}
190
185
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
200
189
}
201
190
}
202
191
@@ -236,7 +225,7 @@ impl<'a> Decode<'a> for Length {
236
225
| u32:: from ( reader. read_byte ( ) ?) ;
237
226
}
238
227
239
- let length = Length :: try_from ( decoded_len) ? ;
228
+ let length = Length :: from ( decoded_len) ;
240
229
241
230
// X.690 Section 10.1: DER lengths must be encoded with a minimum
242
231
// number of octets
@@ -261,8 +250,7 @@ impl Encode for Length {
261
250
0x80 ..=0xFF => Ok ( Length ( 2 ) ) ,
262
251
0x100 ..=0xFFFF => Ok ( Length ( 3 ) ) ,
263
252
0x10000 ..=0xFFFFFF => Ok ( Length ( 4 ) ) ,
264
- 0x1000000 ..=MAX_U32 => Ok ( Length ( 5 ) ) ,
265
- _ => Err ( ErrorKind :: Overflow . into ( ) ) ,
253
+ 0x1000000 ..=0xFFFFFFFF => Ok ( Length ( 5 ) ) ,
266
254
}
267
255
}
268
256
@@ -311,7 +299,7 @@ impl fmt::Display for Length {
311
299
#[ cfg( feature = "arbitrary" ) ]
312
300
impl < ' a > arbitrary:: Arbitrary < ' a > for Length {
313
301
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 ( ) ?) )
315
303
}
316
304
317
305
fn size_hint ( depth : usize ) -> ( usize , Option < usize > ) {
@@ -454,7 +442,7 @@ mod tests {
454
442
) ;
455
443
456
444
assert_eq ! (
457
- Length :: try_from ( 0x10000u32 ) . unwrap ( ) ,
445
+ Length :: from ( 0x10000u32 ) ,
458
446
Length :: from_der( & [ 0x83 , 0x01 , 0x00 , 0x00 ] ) . unwrap( )
459
447
) ;
460
448
}
@@ -487,8 +475,7 @@ mod tests {
487
475
488
476
assert_eq ! (
489
477
& [ 0x83 , 0x01 , 0x00 , 0x00 ] ,
490
- Length :: try_from( 0x10000u32 )
491
- . unwrap( )
478
+ Length :: from( 0x10000u32 )
492
479
. encode_to_slice( & mut buffer)
493
480
. unwrap( )
494
481
) ;
@@ -507,10 +494,7 @@ mod tests {
507
494
// It also supports definite lengths.
508
495
let length = IndefiniteLength :: from_der ( & [ 0x83 , 0x01 , 0x00 , 0x00 ] ) . unwrap ( ) ;
509
496
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( ) ) ;
514
498
}
515
499
516
500
#[ test]
0 commit comments