const_byteptr decode_leb128(Uint& result, const_byteptr p, const_byteptr end) { // TODO: test Uint x = 0; for (;;) { byte const c = *p; byte const sig = c & 0x7f; x <<= 7; x |= sig; ++p; if (c == sig) { result = x; return p; } if (p == end) throw leb128error(); } }
byteptr encode_leb128(byteptr p, byteptr e, Uint x) { if (leb128_max_bytes(x) + p > e) throw leb128error(); return encode_leb128(p, x); }
virtual byteptr encode(byteptr p, const_byteptr end, Value const& x) const { if (max_bytes + p > end) throw leb128error(); /// override this if you want to be more precise return encode(p, x); }
byteptr encode(byteptr p, const_byteptr end, Uint x) const { if (p + fixed_bytes > end) throw leb128error(); return encode(p, x); }
const_byteptr decode(Uint& x, const_byteptr p, const_byteptr end) const { if (p + fixed_bytes > end) throw leb128error(); return decode(x, p); }
static byteptr encode(byteptr p, const_byteptr end, Uint x) { if (p + sizeof(Uint) > end) throw leb128error(); *(Uint*)p = x; return p + sizeof(Uint); }
static const_byteptr decode(Uint &x, const_byteptr p, const_byteptr end) { if (p + sizeof(Uint) > end) throw leb128error(); x = *(Uint*)p; return p + sizeof(Uint); }