@@ -66,6 +66,12 @@ macro_rules! buf_get_impl {
6666 } } ;
6767}
6868
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+
6975/// Read bytes from a buffer.
7076///
7177/// A buffer stores bytes in memory such that read operations are infallible.
@@ -923,7 +929,7 @@ pub trait Buf {
923929 /// This function panics if there is not enough remaining data in `self`, or
924930 /// if `nbytes` is greater than 8.
925931 fn get_int ( & mut self , nbytes : usize ) -> i64 {
926- buf_get_impl ! ( be => self , i64 , nbytes) ;
932+ sign_extend ( self . get_uint ( nbytes ) , nbytes)
927933 }
928934
929935 /// Gets a signed n-byte integer from `self` in little-endian byte order.
@@ -944,7 +950,7 @@ pub trait Buf {
944950 /// This function panics if there is not enough remaining data in `self`, or
945951 /// if `nbytes` is greater than 8.
946952 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)
948954 }
949955
950956 /// Gets a signed n-byte integer from `self` in native-endian byte order.
0 commit comments