From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/object/src/read/util.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'vendor/object/src/read/util.rs') diff --git a/vendor/object/src/read/util.rs b/vendor/object/src/read/util.rs index 842bd6ca1..7c3c65ec9 100644 --- a/vendor/object/src/read/util.rs +++ b/vendor/object/src/read/util.rs @@ -165,6 +165,48 @@ impl<'data> Bytes<'data> { self.skip(offset)?; self.read_string() } + + /// Read an unsigned LEB128 number. + pub fn read_uleb128(&mut self) -> Result { + let mut result = 0; + let mut shift = 0; + + loop { + let byte = *self.read::()?; + if shift == 63 && byte != 0x00 && byte != 0x01 { + return Err(()); + } + result |= u64::from(byte & 0x7f) << shift; + shift += 7; + + if byte & 0x80 == 0 { + return Ok(result); + } + } + } + + /// Read a signed LEB128 number. + pub fn read_sleb128(&mut self) -> Result { + let mut result = 0; + let mut shift = 0; + + loop { + let byte = *self.read::()?; + if shift == 63 && byte != 0x00 && byte != 0x7f { + return Err(()); + } + result |= i64::from(byte & 0x7f) << shift; + shift += 7; + + if byte & 0x80 == 0 { + if shift < 64 && (byte & 0x40) != 0 { + // Sign extend the result. + result |= !0 << shift; + } + return Ok(result); + } + } + } } // Only for Debug impl of `Bytes`. -- cgit v1.2.3