aboutsummaryrefslogtreecommitdiff
path: root/lib/darwin/read.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/darwin/read.c')
-rw-r--r--lib/darwin/read.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/darwin/read.c b/lib/darwin/read.c
new file mode 100644
index 0000000..2e5b746
--- /dev/null
+++ b/lib/darwin/read.c
@@ -0,0 +1,22 @@
+#include "darwin/read.h"
+bool read_leb128(void **ptr, void *end, bool is_signed, uint64_t *out) {
+ uint64_t result = 0;
+ uint8_t *p = *ptr;
+ uint8_t bit;
+ unsigned int shift = 0;
+ do {
+ if (p >= (uint8_t *) end)
+ return false;
+ bit = *p++;
+ uint64_t k = bit & 0x7f;
+ if (shift < sizeof(uint64_t) * 8)
+ result |= k << shift;
+ shift += 7;
+ } while (bit & 0x80);
+ if (is_signed && (bit & 0x40))
+ result |= ~((uint64_t) 0) << shift;
+ *ptr = p;
+ if (out)
+ *out = result;
+ return true;
+}