@@ -299,15 +299,41 @@ where
299
299
Ok ( ( ) )
300
300
}
301
301
302
- pub ( crate ) fn read_signed_int ( & mut self ) -> Result < i64 > {
303
- todo ! ( )
302
+ pub ( crate ) fn read_signed_int ( & mut self , element_length : u64 ) -> Result < i64 > {
303
+ // https://www.rfc-editor.org/rfc/rfc8794.html#section-7.1
304
+ // A Signed Integer Element MUST declare a length from zero to eight octets
305
+ if element_length > 8 {
306
+ decode_err ! ( @BAIL Ebml , "Invalid size for signed int element" )
307
+ }
308
+
309
+ let mut buf = [ 0 ; 8 ] ;
310
+ self . reader
311
+ . read_exact ( & mut buf[ 8 - element_length as usize ..] ) ?;
312
+ let value = u64:: from_be_bytes ( buf) ;
313
+
314
+ // Signed Integers are stored with two's complement notation with the leftmost bit being the sign bit.
315
+ let value_width = element_length * 8 ;
316
+ let shift = ( 64 - value_width) as u32 ;
317
+ Ok ( ( value. wrapping_shl ( shift) as i64 ) . wrapping_shr ( shift) )
304
318
}
305
319
306
- pub ( crate ) fn read_unsigned_int ( & mut self ) -> Result < u64 > {
307
- todo ! ( )
320
+ pub ( crate ) fn read_unsigned_int ( & mut self , element_length : u64 ) -> Result < u64 > {
321
+ // https://www.rfc-editor.org/rfc/rfc8794.html#section-7.2
322
+ // An Unsigned Integer Element MUST declare a length from zero to eight octets
323
+ if element_length > 8 {
324
+ decode_err ! ( @BAIL Ebml , "Invalid size for unsigned int element" )
325
+ }
326
+
327
+ let mut buf = [ 0 ; 8 ] ;
328
+ self . reader
329
+ . read_exact ( & mut buf[ 8 - element_length as usize ..] ) ?;
330
+ Ok ( u64:: from_be_bytes ( buf) )
308
331
}
309
332
310
333
pub ( crate ) fn read_float ( & mut self , element_length : u64 ) -> Result < f64 > {
334
+ // https://www.rfc-editor.org/rfc/rfc8794.html#section-7.3
335
+ // A Float Element MUST declare a length of either zero octets (0 bit),
336
+ // four octets (32 bit), or eight octets (64 bit)
311
337
Ok ( match element_length {
312
338
0 => 0.0 ,
313
339
4 => f64:: from ( self . reader . read_f32 :: < BigEndian > ( ) ?) ,
0 commit comments