diff options
author | Plex <thinkplex@riseup.net> | 2021-10-24 22:50:22 +0200 |
---|---|---|
committer | Plex <thinkplex@riseup.net> | 2021-10-24 22:50:22 +0200 |
commit | dd1779c4c7581b0b863886371f891d9d545d6b27 (patch) | |
tree | 721db5e98337f206e3c20ef82db71ecd6fa3fede /mctypes.c | |
parent | a74a07ab26fbda8235011f2819a78a6f314a1ef2 (diff) |
writeVarInt
Diffstat (limited to 'mctypes.c')
-rw-r--r-- | mctypes.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/mctypes.c b/mctypes.c new file mode 100644 index 0000000..ad855e4 --- /dev/null +++ b/mctypes.c @@ -0,0 +1,36 @@ +#include "mctypes.h" +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> + +varint writeVarInt(unsigned int x) +{ + // unsigned int v = x; + varint n = malloc(sizeof(u_int8_t)); + unsigned int size = 1; + while(1) { + n = reallocarray(n, size, sizeof(u_int8_t)); + n[size-1] = x & 0x7F; + x >>= 7; + if(x == 0) + return n; + n[size-1] |= 0b10000000; + size++; + } + return n; +} +/* +writeVarInt(varint x, int s) +{ + while (1) { + if ((x & 0xFFFFFF80) == 0) { + if (send(s, &x, 8, 0) == -1) + exit(EXIT_FAILURE); + return; + } + if (send(s, &x, 8, 0) == -1) + exit(EXIT_FAILURE); + + x >>= 7; + } +} */ |