feat: add signed leb128 decoding

This commit is contained in:
Phireh 2023-09-24 17:13:44 +02:00
commit 22a7fd432b
Signed by: Phireh
GPG key ID: DD169F1BA658A5E5

View file

@ -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;