public static function encodeUnsigned(string &$buf, int $x): int
    {
        $offset = $i = strlen($buf);
        while ($x >= 0x80) {
            $buf[$i++] = chr($x | 0x80);
            $x >>= 7;
        }
        $buf[$i++] = chr($x);
        return $i - $offset;
    }