summaryrefslogtreecommitdiffstats
path: root/vendor/object/src/read/util.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/object/src/read/util.rs
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/object/src/read/util.rs')
-rw-r--r--vendor/object/src/read/util.rs42
1 files changed, 42 insertions, 0 deletions
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<u64, ()> {
+ let mut result = 0;
+ let mut shift = 0;
+
+ loop {
+ let byte = *self.read::<u8>()?;
+ 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<i64, ()> {
+ let mut result = 0;
+ let mut shift = 0;
+
+ loop {
+ let byte = *self.read::<u8>()?;
+ 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`.