gimli-rs / leb128

Read and write DWARF's "Little Endian Base 128" variable length integer encoding
http://gimli-rs.github.io/leb128/leb128/index.html
Apache License 2.0
18 stars 15 forks source link

Support maximal-length encoded integers #6

Open alexcrichton opened 5 years ago

alexcrichton commented 5 years ago

This came up during https://github.com/rustwasm/walrus/pull/30, specifically https://github.com/rustwasm/walrus/pull/30#discussion_r251137366.

The use case here is that sometimes when dealing with leb128 you'll often have a scenario where a leb128 integer denotes how many bytes left in the region are part of a section or unit. When encoding, though, we often don't know the length of the section up-front, so a common trick is to do something like:

This encoding uses the maximal instead of minimal width, using "padding zero bytes" that look like 0x80 to ensure that leb128 eats up all 5 of the bytes reserved.

It'd be neat if this crate supported such a use case (it's sort of like Seek with writers), although I'd be fine just supporting it with slices for the time being!