diff --git a/src/common.h b/src/common.h index 9d73b43..d180344 100644 --- a/src/common.h +++ b/src/common.h @@ -16,6 +16,8 @@ uint64_t htonll(uint64_t); uint64_t ntohll(uint64_t); +uint64_t unpack_bytes(const uint8_t*, uint8_t); + #define SPAWN_UNDEF 0 #define SPAWN_CLIENT 1 #define SPAWN_SERVER 2 diff --git a/src/packet.c b/src/packet.c index eaacd14..0dd95b3 100644 --- a/src/packet.c +++ b/src/packet.c @@ -3,6 +3,8 @@ typedef struct packet_ctx_t packet_ctx_t; struct packet_ctx_t { uint8_t iter_start; + uint32_t iter_length; + uint8_t region_count; uint16_t *region_lengths; uint32_t length; @@ -12,12 +14,19 @@ struct { uint8_t count; packet_ctx_t *c2s; packet_ctx_t *s2c; + + packet_ctx_t *in; + packet_ctx_t *out; } static ctx; void packet_context_init(uint8_t count) { ctx.count = count; ctx.c2s = (packet_ctx_t*)malloc(count * sizeof(packet_ctx_t)); ctx.s2c = (packet_ctx_t*)malloc(count * sizeof(packet_ctx_t)); + + uint8_t spawn_type = get_spawn_type(); + ctx.in = spawn_type == SPAWN_CLIENT ? ctx.s2c : ctx.c2s; + ctx.out = spawn_type == SPAWN_CLIENT ? ctx.c2s : ctx.s2c; } void packet_context_register(uint8_t direction, uint8_t id, uint8_t iter_pt, uint8_t count, ...) { @@ -53,4 +62,57 @@ void packet_context_free() { for(i = 0; i < ctx.count; ++i) free(ctx.s2c[i].region_lengths); free(ctx.s2c); +} + +static void packet_fill_regions(packet_t *packet) { + uint8_t *ptr = packet->raw; + int i; + + packet->regions = (uint8_t**)malloc(packet->region_count * sizeof(uint8_t*)); + for(i = 0; i < packet->region_count; ++i) { + packet->regions // TOD Ofisnibh THIS +} + +packet_t* packet_init_in(uint8_t *raw) { + if(raw[0] != 0xDE || raw[1] != 0xAD) + return NULL; + if(raw[2] > ctx.count) + return NULL; + + packet_ctx_t *in = &(ctx.in[raw[2]]); + + packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); + packet->id = raw[2]; + packet->raw = raw; + packet->length = ntohl(*((uint32_t*)raw[3])); + packet->region_lengths = in->region_lengths; + packet->iterator = 0; + packet->next_append = 0xFFFF; + packet_fill_regions(packet); + + return packet; +} + +packet_t* packet_init_out(uint8_t id, uint16_t iterations) { + if(id > ctx.count) + return NULL; + + packet_ctx_t *out = &(ctx.out[id]); + int regions = out->region_count; + int iterable_regions = out->region_count - out->iter_start - 1; + if(out->iter_start != 0) + regions = out->iter_start + (iterations * iterable_regions); + + packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); + + packet->id = id; + packet->iterator = 0xFFFF; + packet->next_append = 0; + packet->region_count = regions; + packet->region_lengths = out->region_lengths; + packet->length = (length - iter_length) + iter_length * iterations; + packet->raw = (uint8_t*)malloc(packet->length * sizeof(uint8_t)); + packet_fill_regions(packet); + + return packet; } \ No newline at end of file diff --git a/src/packet.h b/src/packet.h index 7410333..2fe8e53 100644 --- a/src/packet.h +++ b/src/packet.h @@ -17,11 +17,13 @@ typedef struct packet_t packet_t; struct packet_t { uint8_t id; - uint32_t length; + uint8_t *raw; uint8_t **regions; const uint16_t *region_lengths; - uint8_t region_count; - uint8_t iterator; + uint32_t length; + uint16_t region_count; + uint16_t iterator; + uint16_t next_append; }; void packet_context_init(uint8_t); @@ -29,9 +31,32 @@ void packet_context_register(uint8_t, uint8_t, uint8_t, uint8_t, ...); void packet_context_free(); packet_t* packet_init_in(uint8_t*); -packet_t* packet_init_out(uint8_t); +packet_t* packet_init_out(uint8_t, uint16_t); +uint8_t* packet_get (packet_t*, uint8_t, int*); +uint8_t packet_get_uint8 (packet_t*, uint8_t); +uint16_t packet_get_uint16 (packet_t*, uint8_t); +uint32_t packet_get_uint32 (packet_t*, uint8_t); +uint64_t packet_get_uint64 (packet_t*, uint8_t); +uint8_t* packet_get_raw (packet_t*, int*); +uint8_t* packet_iter_get (packet_t*, int*); +uint8_t packet_iter_get_uint8 (packet_t*); +uint16_t packet_iter_get_uint16 (packet_t*); +uint32_t packet_iter_get_uint32 (packet_t*); +uint64_t packet_iter_get_uint64 (packet_t*); + +void packet_append (uint8_t*, int); +void packet_append_string (uint8_t*, int); +void packet_append_uint8 (uint8_t); +void packet_append_uint16 (uint16_t); +void packet_append_uint32 (uint32_t); +void packet_append_uint64 (uint64_t); + +uint16_t packet_region_length (uint8_t, uint8_t); +uint32_t packet_pre_iter_length (uint8_t); +uint32_t packet_iter_length (uint8_t); +BOOL packet_is_length_valid (uint8_t, uint32_t); void packet_free(packet_t*);