feat: add signed leb128 decoding
This commit is contained in:
parent
b98b2b2a71
commit
22a7fd432b
1 changed files with 19 additions and 0 deletions
19
main.cpp
19
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue