forked from hoffoo/itch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
58 lines (43 loc) · 1013 Bytes
/
reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "itch.h"
static char* data;
long read_all(char* filename) {
FILE *f = fopen(filename, "rb");
if (errno != 0) {
exit(errno);
}
fseek(f, 0, SEEK_END);
long len = ftell(f);
rewind(f);
data = realloc(data, len + 1);
fread(data, len, 1, f);
fclose(f);
return len;
}
void read_message(itch_map* m, long len, long offset) {
// done reading
if (offset >= len) {
free(data);
return;
}
if (data[offset] == 0x00) {
offset++;
read_message(m, len, offset);
}
int msg_len = data[offset];
if (msg_len < 1) {
printf("got bad message len, %i\n", data[offset]);
}
char* msg = malloc(msg_len);
for (int i = 0; i < msg_len; i++) {
msg[i] = data[offset+i];
}
itch_emit(m, msg);
free(msg);
offset += msg_len + 1;;
read_message(m, len, offset);
}