summaryrefslogtreecommitdiffstats
path: root/protocol.h
blob: a73b4cf3417fa4766e4a135ad79bbd73b9dee596 (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
//
//  protocol.h
//  mcc@exe
//
//  Created by Finn Behrens on 19.02.21.
//

#ifndef protocol_h
#define protocol_h

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>

// FIXME: find real version number
#define PROTOCOL_VERSION 1073741839

// MARK: - Helpers
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define bswap_16(x) OSSwapInt16(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#else
#include <byteswap.h>
#endif

#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define BE16_TO_H(x) (x)
#define BE32_TO_H(x) (x)
#define BE64_TO_H(x) (x)

#define H_TO_BE16(x) (x)
#define H_TO_BE32(x) (x)
#define H_TO_BE64(x) (x)
#else
#define BE16_TO_H(x) bswap_16(x)
#define BE32_TO_H(x) bswap_32(x)
#define BE64_TO_H(x) bswap_64(x)

#define H_TO_BE16(x) bswap_16(x)
#define H_TO_BE32(x) bswap_32(x)
#define H_TO_BE64(x) bswap_64(x)
#endif

#define pos_bufferx(x, y) ((x + y))
#define pos_buffer(x) pos_bufferx(x, counter)
/*#define check_addx(func, x)  \
    if (func < 0)           \
        return func;        \
    x += func;*/
#define check_add(func) check_addx(func, &counter)
int check_addx(int func, int *x);


// MARK: - READ
// MARK: VAR
int read_var_int(int *dest, uint8_t *src);
int read_var_long(long *dest, uint8_t *src);

// MARK: primitives
int read_bool(bool *dest, uint8_t *src);

// byte
int read_int8(int8_t *dest, uint8_t *src);
int read_uint8(uint8_t *dest, uint8_t *src);

// short
int read_int16(int16_t *dest, uint8_t *src);
int read_uint16(uint16_t *dest, uint8_t *src);

// int
int read_int32(int32_t *dest, uint8_t *src);
int read_uint32(uint32_t *dest, uint8_t *src);

// long
int read_int64(int64_t *dest, uint8_t *src);
int read_uint64(uint64_t *dest, uint8_t *src);

// MARK: Position
typedef struct {
    int32_t x;
    int32_t z;
    int16_t y;
} position_t;

int read_position(position_t *dest, uint8_t *src);

// string
int read_string_malloc(char **dest, uint8_t *src);

// MARK: - WRITE
// MARK: VAR
int write_var_int(int value, uint8_t *dest);
int write_var_long(long value, uint8_t *dest);

// MARK: primitives

// string
int write_string(const char *str, uint8_t *dest, int size);

#endif /* protocol_h */