summaryrefslogtreecommitdiffstats
path: root/protocol.c
blob: 9ccf9135df7492ba0c280c543c60a423ff92b26e (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
//  protocol.c
//  mcc@exe
//
//  Created by Finn Behrens on 19.02.21.
//
#include <stdlib.h>
#include <string.h>

#include "protocol.h"

int check_addx(int func, int *x)
{
    if (func < 0)
        return func;
    *x += func;
    return 0;
}

// MARK: - READ
// MARK: VAR
int read_var_int(int *dest, uint8_t *src)
{
    int num_read = 0, result = 0;
    uint8_t read;
    do {
        read = *(src + num_read);
        uint8_t value = (read & 0b01111111);
        result |= (value << (7 * num_read));
        
        num_read++;
        if (num_read > 5) {
            return -EINVAL;
        }
    } while ((read & 0b10000000) != 0);
    
    *dest = result;
    return num_read;
}

int read_var_long(long *dest, uint8_t *src)
{
    int num_read = 0;
    long result = 0;
    uint8_t read;
    do {
        read = *(src + num_read);
        uint8_t value = (read & 0b01111111);
        result |= (value << (7 * num_read));
        
        num_read++;
        if(num_read > 10) {
            return -EINVAL;
        }
    } while ((read & 0b10000000) != 0);
    
    return num_read;
}

// MARK: primitives
int read_bool(bool *dest, uint8_t *src)
{
    // True is encoded as 0x01, false as 0x00, so everything else is a hard error
    if (__builtin_expect((*dest != 0x00 && *dest != 0x01), 0)) {
        return -EINVAL;
    }
    *dest = *src == 0x01;
    return 1;
}

int read_int8(int8_t *dest, uint8_t *src)
{
    *dest = (int8_t)*src;
    return 1;
}
int read_uint8(uint8_t *dest, uint8_t *src)
{
    *dest = (uint8_t)*src;
    return 1;
}

// short
int read_int16(int16_t *dest, uint8_t *src)
{
    *dest = BE16_TO_H((int16_t) *src);
    return 2;
}
int read_uint16(uint16_t *dest, uint8_t *src)
{
    *dest = BE16_TO_H((uint16_t) *src);
    return 2;
}

// int
int read_int32(int32_t *dest, uint8_t *src)
{
    *dest = BE32_TO_H((int32_t) *src);
    return 4;
}

int read_uint32(uint32_t *dest, uint8_t *src)
{
    *dest = BE32_TO_H((uint32_t) *src);
    return 4;
}

// long
int read_int64(int64_t *dest, uint8_t *src)
{
    *dest = BE64_TO_H((int64_t) *src);
    return 8;
}

int read_uint64(uint64_t *dest, uint8_t *src)
{
    *dest = BE64_TO_H((uint64_t) *src);
    return 8;
}

// MARK: position
int read_position(position_t *dest, uint8_t *src)
{
    uint64_t position;
    int ret = read_uint64(&position, src);
    
    dest->x = position >> 38;
    dest->y = position & 0xFFF;
    dest->z = (position << 26 >> 38);
    
    return ret;
}

// string
int read_string_malloc(char **dest, uint8_t *src) {
    int counter = 0;
    int length, err;
    
    if ((err = check_add(read_var_int(&length, src)) != 0))
        return err;
    
    *dest = calloc(length+1, sizeof(char));
    if (*dest == NULL) {
        return -ENOMEM;
    }
    
    memcpy(*dest, pos_buffer(src), length);
    
    return counter + length;
}

// MARK: - WRITE
// MARK: VAR
int write_var_int(int value, uint8_t *dest)
{
    int pos = 0;
    unsigned int value_u = (unsigned int) value;
    
    do {
        uint8_t temp = (uint8_t)(value_u & 0b01111111);
        value_u >>= 7;
        if (value_u != 0) {
            temp |= 0b10000000;
        }
        *(dest + pos) = temp;
        pos++;
    } while (value_u != 0);
    
    if (pos > 5) {
        return -EINVAL;
    }
    
    return pos;
}

int write_var_long(long value, uint8_t *dest)
{
    int pos = 0;
    
    do {
        uint8_t temp = (uint8_t)(value & 0b01111111);
        value >>= 7;
        if (value != 0) {
            temp |= 0b10000000;
        }
        *(dest + pos) = temp;
        pos++;
    } while (value != 0);
    
    if (pos > 10) {
        return -EINVAL;
    }
    
    return pos;
}

int write_string(const char *str, uint8_t *dest, int size)
{
    int counter = 0, err;
    
    if ((err =check_add(write_var_int(size, pos_buffer(dest)))) != 0)
        return err;
    memcpy((char *)pos_buffer(dest), str, size);
    
    return counter + size;
}