@@ -995,8 +995,11 @@ pub fn read_value(buf: &[u8], serial_type: &SerialType) -> Result<(OwnedValue, u
995
995
if buf. len ( ) < 3 {
996
996
crate :: bail_corrupt_error!( "Invalid BEInt24 value" ) ;
997
997
}
998
+ let sign_extension = if buf[ 0 ] <= 127 { 0 } else { 255 } ;
998
999
Ok ( (
999
- OwnedValue :: Integer ( i32:: from_be_bytes ( [ 0 , buf[ 0 ] , buf[ 1 ] , buf[ 2 ] ] ) as i64 ) ,
1000
+ OwnedValue :: Integer (
1001
+ i32:: from_be_bytes ( [ sign_extension, buf[ 0 ] , buf[ 1 ] , buf[ 2 ] ] ) as i64
1002
+ ) ,
1000
1003
3 ,
1001
1004
) )
1002
1005
}
@@ -1013,9 +1016,17 @@ pub fn read_value(buf: &[u8], serial_type: &SerialType) -> Result<(OwnedValue, u
1013
1016
if buf. len ( ) < 6 {
1014
1017
crate :: bail_corrupt_error!( "Invalid BEInt48 value" ) ;
1015
1018
}
1019
+ let sign_extension = if buf[ 0 ] <= 127 { 0 } else { 255 } ;
1016
1020
Ok ( (
1017
1021
OwnedValue :: Integer ( i64:: from_be_bytes ( [
1018
- 0 , 0 , buf[ 0 ] , buf[ 1 ] , buf[ 2 ] , buf[ 3 ] , buf[ 4 ] , buf[ 5 ] ,
1022
+ sign_extension,
1023
+ sign_extension,
1024
+ buf[ 0 ] ,
1025
+ buf[ 1 ] ,
1026
+ buf[ 2 ] ,
1027
+ buf[ 3 ] ,
1028
+ buf[ 4 ] ,
1029
+ buf[ 5 ] ,
1019
1030
] ) ) ,
1020
1031
6 ,
1021
1032
) )
@@ -1419,6 +1430,18 @@ mod tests {
1419
1430
#[ case( & [ ] , SerialType :: ConstInt1 , OwnedValue :: Integer ( 1 ) ) ]
1420
1431
#[ case( & [ 1 , 2 , 3 ] , SerialType :: Blob ( 3 ) , OwnedValue :: Blob ( vec![ 1 , 2 , 3 ] . into( ) ) ) ]
1421
1432
#[ case( & [ 65 , 66 , 67 ] , SerialType :: String ( 3 ) , OwnedValue :: build_text( "ABC" ) ) ]
1433
+ #[ case( & [ 0x80 ] , SerialType :: Int8 , OwnedValue :: Integer ( -128 ) ) ]
1434
+ #[ case( & [ 0x80 , 0 ] , SerialType :: BEInt16 , OwnedValue :: Integer ( -32768 ) ) ]
1435
+ #[ case( & [ 0x80 , 0 , 0 ] , SerialType :: BEInt24 , OwnedValue :: Integer ( -8388608 ) ) ]
1436
+ #[ case( & [ 0x80 , 0 , 0 , 0 ] , SerialType :: BEInt32 , OwnedValue :: Integer ( -2147483648 ) ) ]
1437
+ #[ case( & [ 0x80 , 0 , 0 , 0 , 0 , 0 ] , SerialType :: BEInt48 , OwnedValue :: Integer ( -140737488355328 ) ) ]
1438
+ #[ case( & [ 0x80 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , SerialType :: BEInt64 , OwnedValue :: Integer ( -9223372036854775808 ) ) ]
1439
+ #[ case( & [ 0x7f ] , SerialType :: Int8 , OwnedValue :: Integer ( 127 ) ) ]
1440
+ #[ case( & [ 0x7f , 0xff ] , SerialType :: BEInt16 , OwnedValue :: Integer ( 32767 ) ) ]
1441
+ #[ case( & [ 0x7f , 0xff , 0xff ] , SerialType :: BEInt24 , OwnedValue :: Integer ( 8388607 ) ) ]
1442
+ #[ case( & [ 0x7f , 0xff , 0xff , 0xff ] , SerialType :: BEInt32 , OwnedValue :: Integer ( 2147483647 ) ) ]
1443
+ #[ case( & [ 0x7f , 0xff , 0xff , 0xff , 0xff , 0xff ] , SerialType :: BEInt48 , OwnedValue :: Integer ( 140737488355327 ) ) ]
1444
+ #[ case( & [ 0x7f , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff ] , SerialType :: BEInt64 , OwnedValue :: Integer ( 9223372036854775807 ) ) ]
1422
1445
fn test_read_value (
1423
1446
#[ case] buf : & [ u8 ] ,
1424
1447
#[ case] serial_type : SerialType ,
0 commit comments