diff options
Diffstat (limited to 'mctypes.c')
-rw-r--r-- | mctypes.c | 44 |
1 files changed, 33 insertions, 11 deletions
@@ -25,9 +25,9 @@ int from_varint(varint x) do { if (offset == 5) exit(EXIT_FAILURE); - res |= (x[offset] & 0b01111111) << offset * 7; + res |= (x[offset] & 0x7F) << offset * 7; offset++; - } while ((x[offset-1] & 0b10000000) != 0); + } while ((x[offset-1] & 0x80) != 0); return (int) res; } @@ -45,25 +45,26 @@ size_t serialize_handshake(handshake hs, void **buf) *buf = malloc(size); varint_s = size_varint(hs.protocol_version); - *buf = realloc(*buf, size+varint_s); + *buf = realloc(*buf, size + varint_s); memcpy(*buf + size, hs.protocol_version, varint_s); size += varint_s; varint_s = size_varint(hs.server_address.length); - *buf = realloc(*buf, size+varint_s); + *buf = realloc(*buf, size + varint_s); memcpy(*buf + size, hs.server_address.length, varint_s); size += varint_s; - *buf = realloc(*buf, size+from_varint(hs.server_address.length)); - memcpy(*buf + size, hs.server_address.content, from_varint(hs.server_address.length)); + *buf = realloc(*buf, size + from_varint(hs.server_address.length)); + memcpy(*buf + size, hs.server_address.content, + from_varint(hs.server_address.length)); size += from_varint(hs.server_address.length); - *buf = realloc(*buf, size+sizeof(unsigned short)); + *buf = realloc(*buf, size + sizeof(unsigned short)); memcpy(*buf + size, &hs.server_port, sizeof(unsigned short)); size += sizeof(unsigned short); varint_s = size_varint(hs.next_state); - *buf = realloc(*buf, size+varint_s); + *buf = realloc(*buf, size + varint_s); memcpy(*buf + size, hs.next_state, varint_s); size += varint_s; @@ -73,7 +74,7 @@ size_t serialize_handshake(handshake hs, void **buf) size_t serialize_packet(packet p, void **buf) { size_t v_size = size_varint(p.length); - size_t p_size = from_varint(p.length) + v_size; // total size of packet + size_t p_size = from_varint(p.length) + v_size; // total size of packet size_t size = 0; *buf = malloc(p_size); @@ -81,11 +82,32 @@ size_t serialize_packet(packet p, void **buf) size += v_size; for (v_size = 1; (p.id[v_size - 1] & 0x80) != 0; v_size++); - memcpy(*buf+size, p.id, v_size); + memcpy(*buf + size, p.id, v_size); size += v_size; - memcpy(*buf+size, p.data, from_varint(p.length) - v_size); + memcpy(*buf + size, p.data, from_varint(p.length) - v_size); return p_size; } +varint deserialize_varint(void **raw) +{ + varint v = NULL; + unsigned short i = 0; + do { + v = reallocarray(v, i+1, sizeof(u_int8_t)); + memcpy(v+i, *raw, sizeof(u_int8_t)); + (*raw)++; + i++; + } while ((v[i-1] & 0x80) != 0); + return v; +} + +string deserialize_string(void **raw) +{ + string s; + s.length = deserialize_varint(raw); + s.content = malloc(from_varint(s.length)); + memcpy(s.content, *raw, from_varint(s.length)); + return s; +}; |