diff --git a/main.cpp b/main.cpp index 750aed0..6dd1732 100644 --- a/main.cpp +++ b/main.cpp @@ -87,6 +87,25 @@ inline int decode_leb128(uint8_t *src, uint64_t *dest) // return the number of bytes that we should move the src pointer return shift / 8 + (shift % 8 != 0); } +// TODO: Deal with cases where size of LEB128 > word size +inline int decode_sleb128(uint8_t *src, int64_t *dest) +{ + *dest = 0; + int shift = 0; + uint8_t val; + do { + val = *src++; + *dest |= (val & 0x7f) << shift; + shift += 7; + } while (val & 0x80); + + // deal with signedness + if ((shift < 64) && (val & 0x40)) + for (int i = 63; i < shift; ++i) + *dest |= (1 << shift); // sign extend + // return the number of bytes that we should move the src pointer + return shift / 8 + (shift % 8 != 0); +} // globals uint64_t cu_header_offset = 0;