@@ -10,6 +10,9 @@ use core::{
10
10
ops:: { Add , Sub } ,
11
11
} ;
12
12
13
+ /// Maximum length as a `u32`.
14
+ const MAX_U32 : u32 = u32:: MAX - 1 ;
15
+
13
16
/// Octet identifying an indefinite length as described in X.690 Section
14
17
/// 8.1.3.6.1:
15
18
///
@@ -29,7 +32,7 @@ impl Length {
29
32
pub const ONE : Self = Self ( 1 ) ;
30
33
31
34
/// Maximum length (`u32::MAX` - 1).
32
- pub const MAX : Self = Self ( u32 :: MAX - 1 ) ;
35
+ pub const MAX : Self = Self ( MAX_U32 ) ;
33
36
34
37
/// Maximum number of octets in a DER encoding of a [`Length`] using the
35
38
/// rules implemented by this crate.
@@ -47,7 +50,7 @@ impl Length {
47
50
/// This function is const-safe and therefore useful for [`Length`] constants.
48
51
#[ allow( clippy:: cast_possible_truncation) ]
49
52
pub ( crate ) const fn new_usize ( len : usize ) -> Result < Self > {
50
- if len > ( u32 :: MAX as usize ) - 1 {
53
+ if len > Self :: MAX . 0 as usize {
51
54
Err ( Error :: from_kind ( ErrorKind :: Overflow ) )
52
55
} else {
53
56
Ok ( Length ( len as u32 ) )
@@ -74,7 +77,12 @@ impl Length {
74
77
75
78
/// Perform saturating addition of two lengths.
76
79
pub fn saturating_add ( self , rhs : Self ) -> Self {
77
- Self ( self . 0 . saturating_add ( rhs. 0 ) )
80
+ let sum = self . 0 . saturating_add ( rhs. 0 ) ;
81
+ if sum < Self :: MAX . 0 {
82
+ Self ( sum)
83
+ } else {
84
+ Self :: MAX
85
+ }
78
86
}
79
87
80
88
/// Perform saturating subtraction of two lengths.
@@ -99,7 +107,7 @@ impl Length {
99
107
0x80 ..=0xFF => Some ( 0x81 ) ,
100
108
0x100 ..=0xFFFF => Some ( 0x82 ) ,
101
109
0x10000 ..=0xFFFFFF => Some ( 0x83 ) ,
102
- 0x1000000 ..=0xFFFFFFFE => Some ( 0x84 ) ,
110
+ 0x1000000 ..=MAX_U32 => Some ( 0x84 ) ,
103
111
_ => None ,
104
112
}
105
113
}
@@ -112,7 +120,7 @@ impl Add for Length {
112
120
self . 0
113
121
. checked_add ( other. 0 )
114
122
. ok_or_else ( || ErrorKind :: Overflow . into ( ) )
115
- . map ( Self )
123
+ . and_then ( TryInto :: try_into )
116
124
}
117
125
}
118
126
@@ -136,7 +144,7 @@ impl Add<u32> for Length {
136
144
type Output = Result < Self > ;
137
145
138
146
fn add ( self , other : u32 ) -> Result < Self > {
139
- self + Length :: from ( other)
147
+ self + Length :: try_from ( other) ?
140
148
}
141
149
}
142
150
@@ -187,9 +195,21 @@ impl From<u16> for Length {
187
195
}
188
196
}
189
197
190
- impl From < u32 > for Length {
191
- fn from ( len : u32 ) -> Length {
192
- Length ( len)
198
+ // impl From<u32> for Length {
199
+ // fn from(len: u32) -> Length {
200
+ // Length(len)
201
+ // }
202
+ // }
203
+
204
+ impl TryFrom < u32 > for Length {
205
+ type Error = Error ;
206
+
207
+ fn try_from ( len : u32 ) -> Result < Length > {
208
+ if len <= Self :: MAX . 0 {
209
+ Ok ( Length ( len) )
210
+ } else {
211
+ Err ( ErrorKind :: Overflow . into ( ) )
212
+ }
193
213
}
194
214
}
195
215
@@ -238,7 +258,7 @@ impl<'a> Decode<'a> for Length {
238
258
| u32:: from ( reader. read_byte ( ) ?) ;
239
259
}
240
260
241
- let length = Length :: from ( decoded_len) ;
261
+ let length = Length :: try_from ( decoded_len) ? ;
242
262
243
263
// X.690 Section 10.1: DER lengths must be encoded with a minimum
244
264
// number of octets
@@ -312,7 +332,7 @@ impl fmt::Display for Length {
312
332
#[ cfg( feature = "arbitrary" ) ]
313
333
impl < ' a > arbitrary:: Arbitrary < ' a > for Length {
314
334
fn arbitrary ( u : & mut arbitrary:: Unstructured < ' a > ) -> arbitrary:: Result < Self > {
315
- Ok ( Self ( u. arbitrary ( ) ?) )
335
+ Ok ( Self ( u. int_in_range ( 0 ..= MAX_U32 ) ?) )
316
336
}
317
337
318
338
fn size_hint ( depth : usize ) -> ( usize , Option < usize > ) {
@@ -349,12 +369,16 @@ mod tests {
349
369
) ;
350
370
351
371
assert_eq ! (
352
- Length :: from ( 0x10000u32 ) ,
372
+ Length :: try_from ( 0x10000u32 ) . unwrap ( ) ,
353
373
Length :: from_der( & [ 0x83 , 0x01 , 0x00 , 0x00 ] ) . unwrap( )
354
374
) ;
355
375
assert_eq ! (
356
- Length :: from( 0xFFFFFFFFu32 ) ,
357
- Length :: from_der( & [ 0x84 , 0xFF , 0xFF , 0xFF , 0xFF ] ) . unwrap( )
376
+ Length :: try_from( 0xFFFFFFFEu32 ) . unwrap( ) ,
377
+ Length :: from_der( & [ 0x84 , 0xFF , 0xFF , 0xFF , 0xFE ] ) . unwrap( )
378
+ ) ;
379
+ assert_eq ! (
380
+ Length :: from_der( & [ 0x84 , 0xFF , 0xFF , 0xFF , 0xFF ] ) ,
381
+ Err ( ErrorKind :: Overflow . into( ) )
358
382
) ;
359
383
}
360
384
@@ -386,13 +410,15 @@ mod tests {
386
410
387
411
assert_eq ! (
388
412
& [ 0x83 , 0x01 , 0x00 , 0x00 ] ,
389
- Length :: from( 0x10000u32 )
413
+ Length :: try_from( 0x10000u32 )
414
+ . unwrap( )
390
415
. encode_to_slice( & mut buffer)
391
416
. unwrap( )
392
417
) ;
393
418
assert_eq ! (
394
- & [ 0x84 , 0xFF , 0xFF , 0xFF , 0xFF ] ,
395
- Length :: from( 0xFFFFFFFFu32 )
419
+ & [ 0x84 , 0xFF , 0xFF , 0xFF , 0xFE ] ,
420
+ Length :: try_from( 0xFFFFFFFEu32 )
421
+ . unwrap( )
396
422
. encode_to_slice( & mut buffer)
397
423
. unwrap( )
398
424
) ;
0 commit comments