@@ -66,6 +66,12 @@ macro_rules! buf_get_impl {
66
66
} } ;
67
67
}
68
68
69
+ // https://en.wikipedia.org/wiki/Sign_extension
70
+ fn sign_extend ( val : u64 , nbytes : usize ) -> i64 {
71
+ let shift = ( 8 - nbytes) * 8 ;
72
+ ( val << shift) as i64 >> shift
73
+ }
74
+
69
75
/// Read bytes from a buffer.
70
76
///
71
77
/// A buffer stores bytes in memory such that read operations are infallible.
@@ -923,7 +929,7 @@ pub trait Buf {
923
929
/// This function panics if there is not enough remaining data in `self`, or
924
930
/// if `nbytes` is greater than 8.
925
931
fn get_int ( & mut self , nbytes : usize ) -> i64 {
926
- buf_get_impl ! ( be => self , i64 , nbytes) ;
932
+ sign_extend ( self . get_uint ( nbytes ) , nbytes)
927
933
}
928
934
929
935
/// Gets a signed n-byte integer from `self` in little-endian byte order.
@@ -944,7 +950,7 @@ pub trait Buf {
944
950
/// This function panics if there is not enough remaining data in `self`, or
945
951
/// if `nbytes` is greater than 8.
946
952
fn get_int_le ( & mut self , nbytes : usize ) -> i64 {
947
- buf_get_impl ! ( le => self , i64 , nbytes) ;
953
+ sign_extend ( self . get_uint_le ( nbytes ) , nbytes)
948
954
}
949
955
950
956
/// Gets a signed n-byte integer from `self` in native-endian byte order.
0 commit comments