C Base128 Function -


what c function use encode/decode number in leb128 format? not find simple documentation or examples.

it might interest readers why leb128 useful. provides kind of compression representing numbers if of time magnitude of numbers relatively small. on random input, on average use 5 out of 8 bytes represent 64 bit number (although worst case, use 10 bytes).

below implementations encode , decode unsigned 64 bit numbers. i'll leave signed version exercise interested reader.

size_t fwrite_uleb128 (file *out, uint64_t x) {     unsigned char buf[10];     size_t bytes = 0;     {         buf[bytes] = x & 0x7fu;         if (x >>= 7) buf[bytes] |= 0x80u;         ++bytes;     } while (x);     return fwrite(buf, bytes, 1, out); }  size_t fread_uleb128 (file *in, uint64_t *x) {     unsigned char buf;     size_t bytes = 0;     while (fread(&buf, 1, 1, in)) {         if (bytes == 0) *x = 0;         *x |= (buf & 0x7full) << (7 * bytes++);         if (!(buf & 0x80u)) break;     }     return !!bytes; } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -