1
0
mirror of https://github.com/p2r3/bareiron.git synced 2025-10-02 07:35:08 +02:00
Files
bareiron/src/varnum.c
2025-08-12 03:51:57 +03:00

50 lines
893 B
C

#include <stdint.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "varnum.h"
#include "globals.h"
#include "tools.h"
int32_t readVarInt (int client_fd) {
int32_t value = 0;
int position = 0;
uint8_t byte;
while (true) {
byte = readByte(client_fd);
if (recv_count != 1) return VARNUM_ERROR;
value |= (byte & SEGMENT_BITS) << position;
if ((byte & CONTINUE_BIT) == 0) break;
position += 7;
if (position >= 32) return VARNUM_ERROR;
}
return value;
}
int sizeVarInt (uint32_t value) {
int size = 1;
while ((value & ~SEGMENT_BITS) != 0) {
value >>= 7;
size ++;
}
return size;
}
void writeVarInt (int client_fd, uint32_t value) {
while (true) {
if ((value & ~SEGMENT_BITS) == 0) {
writeByte(client_fd, value);
return;
}
writeByte(client_fd, (value & SEGMENT_BITS) | CONTINUE_BIT);
value >>= 7;
}
}