Search code examples
parsingd

ULEB128 variable-length integer encoding


I'm trying to write a ULEB 128 parser that will take an array of unsigned bytes and return the value and shift required. If understand correctly if I have a ubyte[] list = [0x1b, 0x02, 0x03] then what I do is look at the 0x1b and here the high bit is greater than 0, so then I've to read the next byte. In the end it should return 0x20b with a shift of 2 without including 0x03, as the second byte high bit is 0. If my logic is correct, my main issue is byte manipulation in D i.e. how to check the high or low bits.


Solution

  • If my logic is correct, my main issue is byte manipulation in D i.e. how to check the high or low bits.

    Like you would do in C. You can use bitwise and (&) and binary shift (<<). If you need an example to go by, druntime does this: https://github.com/dlang/druntime/blob/0dfc0ce5aef1fde00713b56e9be99dcdfb04d171/src/rt/backtrace/dwarf.d#L490-L534