Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'release/0.0.4'

  • Loading branch information...
commit 4bee145115d6adb0881172fed4a413a03c26247e 2 parents 4b9674b + 6122dd5
@deoxxa authored
View
21 Makefile
@@ -2,20 +2,27 @@ CFLAGS += -I ./include -std=c99 -Wall -Werror -Wextra -pedantic -O0 -g
all: bin/example
-obj/read.o: src/read.c
+prepare:
+ if [ ! -e obj ]; then mkdir obj; fi;
+ if [ ! -e bin ]; then mkdir bin; fi;
+
+obj/read.o: prepare src/read.c
$(CC) $(CFLAGS) $(LDFLAGS) -c -o obj/read.o src/read.c
-obj/parser.o: src/parser.c
+obj/parser.o: prepare src/parser.c
$(CC) $(CFLAGS) $(LDFLAGS) -c -o obj/parser.o src/parser.c
-obj/metadata.o: src/metadata.c
+obj/metadata.o: prepare src/metadata.c
$(CC) $(CFLAGS) $(LDFLAGS) -c -o obj/metadata.o src/metadata.c
-obj/example.o: example.c
+obj/slot.o: prepare src/slot.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -c -o obj/slot.o src/slot.c
+
+obj/example.o: prepare example.c
$(CC) $(CFLAGS) $(LDFLAGS) -c -o obj/example.o example.c
-bin/example: obj/read.o obj/parser.o obj/metadata.o obj/example.o
- $(CC) $(CFLAGS) $(LDFLAGS) -o bin/example obj/example.o obj/read.o obj/parser.o obj/metadata.o
+bin/example: prepare obj/read.o obj/parser.o obj/metadata.o obj/slot.o obj/example.o
+ $(CC) $(CFLAGS) $(LDFLAGS) -o bin/example obj/example.o obj/read.o obj/parser.o obj/metadata.o obj/slot.o
clean:
- rm -f obj/* bin/*
+ rm -rf obj bin
View
0  bin/.gitkeep
No changes.
View
76 example.c
@@ -5,18 +5,22 @@
#define PACKET(id, code) case 0x##id: { mcnet_packet_##id##_t* tmp = (mcnet_packet_##id##_t*)packet; printf("Packet ID: 0x%02x\n", tmp->pid); code break; };
-#define BOOL(name) printf(" [bool] %c\n", tmp->name ? 'T' : 'F');
-#define BYTE(name) printf(" [byte] %d\n", tmp->name);
-#define UBYTE(name) printf(" [ubyte] %u\n", tmp->name);
-#define SHORT(name) printf(" [short] %d\n", tmp->name);
-#define INT(name) printf(" [int] %d\n", tmp->name);
-#define LONG(name) printf(" [long] %lld\n", tmp->name);
-#define FLOAT(name) printf(" [float] %f\n", tmp->name);
-#define DOUBLE(name) printf(" [double] %g\n", tmp->name);
-#define STRING16(name) printf(" [string16] %d -> ", tmp->name##_len); for (int i = 0; i < tmp->name##_len; ++i) { printf("%c", tmp->name[i*2+1]); } printf("\n");
-#define STRING8(name) printf(" [string8] %d -> ", tmp->name##_len); for (int i = 0; i < tmp->name##_len; ++i) { printf("%c", tmp->name[i]); } printf("\n");
-#define BLOB(name, length) printf(" [blob] %d -> ", length); for (int i = 0; i < length; ++i) { printf("%02x", tmp->name[i]); if (i != length) { printf(":"); } } printf("\n");
-#define METADATA(name) printf(" [metadata] %d\n", tmp->name##_len);
+#define CODE(data)
+#define BOOL(name) printf(" [bool] %c\n", tmp->name ? 'T' : 'F');
+#define BYTE(name) printf(" [byte] %d\n", tmp->name);
+#define UBYTE(name) printf(" [ubyte] %u\n", tmp->name);
+#define SHORT(name) printf(" [short] %d\n", tmp->name);
+#define USHORT(name) printf(" [ushort] %u\n", tmp->name);
+#define INT(name) printf(" [int] %d\n", tmp->name);
+#define LONG(name) printf(" [long] %lld\n", tmp->name);
+#define FLOAT(name) printf(" [float] %f\n", tmp->name);
+#define DOUBLE(name) printf(" [double] %g\n", tmp->name);
+#define STRING16(name) printf(" [string16] %d -> ", tmp->name##_len); for (int i = 0; i < tmp->name##_len; ++i) { printf("%c", tmp->name[i*2+1]); } printf("\n");
+#define STRING8(name) printf(" [string8] %d -> ", tmp->name##_len); for (int i = 0; i < tmp->name##_len; ++i) { printf("%c", tmp->name[i]); } printf("\n");
+#define BLOB(name, length) printf(" [blob] %d -> ", tmp->length); for (int i = 0; i < tmp->length; ++i) { printf("%02x", tmp->name[i]); if (i != tmp->length) { printf(":"); } } printf("\n");
+#define METADATA(name) printf(" [metadata] %d\n", tmp->name##_len);
+#define SLOT(name) printf(" [slot] %d\n", tmp->name##_len);
+#define SLOTS(name, length) printf(" [slots] %d\n", tmp->name##_len);
void on_packet(mcnet_parser_t* parser, mcnet_packet_t* packet) {
// This gets around the unused variable warning. Most of the time, you'd keep
@@ -30,16 +34,20 @@ void on_packet(mcnet_parser_t* parser, mcnet_packet_t* packet) {
}
}
+#undef CODE
#undef BOOL
#undef BYTE
#undef UBYTE
#undef SHORT
+#undef USHORT
#undef INT
#undef LONG
#undef FLOAT
#undef DOUBLE
#undef STRING16
#undef METADATA
+#undef SLOT
+#undef SLOTS
#undef PACKET
@@ -52,10 +60,43 @@ int main() {
mcnet_parser_settings_t settings = { .on_packet = on_packet, .on_error = on_error };
uint8_t data[] = {
- 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x00, 0x00, 0x00, 0x00, 0x02,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x61, 0x00, 0x73, 0x00, 0x64, 0x00, 0x66, 0x00, 0x01, 0x00, 0x65, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x04, 0x05,
- 0x17, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09
+ 0x00,
+ 0x00, 0x00, 0x00, 0x01,
+
+ 0x00,
+ 0x00, 0x00, 0x00, 0x02,
+
+ 0x01,
+ 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x04, 0x00, 0x61, 0x00, 0x73, 0x00, 0x64, 0x00, 0x66,
+ 0x00, 0x01, 0x00, 0x65,
+ 0x00, 0x00, 0x00, 0x02,
+ 0x00, 0x00, 0x00, 0x03,
+ 0x04,
+ 0x05,
+ 0x06,
+
+ 0x17,
+ 0x00, 0x00, 0x00, 0x01,
+ 0x02,
+ 0x00, 0x00, 0x00, 0x03,
+ 0x00, 0x00, 0x00, 0x04,
+ 0x00, 0x00, 0x00, 0x05,
+ 0x00, 0x00, 0x00, 0x06,
+ 0x00, 0x07,
+ 0x00, 0x08,
+ 0x00, 0x09,
+
+ 0x17,
+ 0x00, 0x00, 0x00, 0x01,
+ 0x02,
+ 0x00, 0x00, 0x00, 0x03,
+ 0x00, 0x00, 0x00, 0x04,
+ 0x00, 0x00, 0x00, 0x05,
+ 0x00, 0x00, 0x00, 0x00,
+
+ 0x00,
+ 0x00, 0x00, 0x00, 0x03
};
size_t nparsed = 0, offset = 0;
@@ -71,8 +112,9 @@ int main() {
break;
}
- printf("parsed %d bytes\n", nparsed);
offset += nparsed;
+
+ printf("parsed %d bytes and %d total\n", nparsed, offset);
}
return 0;
View
2  include/mcnet/metadata.h
@@ -59,11 +59,13 @@ typedef struct mcnet_metadata_entry_iii_s {
struct mcnet_metadata_parser_s;
typedef void (*mcnet_metadata_parser_entry_cb)(struct mcnet_metadata_parser_s* parser, mcnet_metadata_entry_t* entry);
+typedef void (*mcnet_metadata_parser_complete_cb)(struct mcnet_metadata_parser_s* parser);
typedef void (*mcnet_metadata_parser_error_cb)(struct mcnet_metadata_parser_s* parser, int error);
typedef struct mcnet_metadata_parser_s {
void* data;
mcnet_metadata_parser_entry_cb on_entry;
+ mcnet_metadata_parser_complete_cb on_complete;
mcnet_metadata_parser_error_cb on_error;
} mcnet_metadata_parser_t;
View
22 include/mcnet/packets.h
@@ -25,7 +25,7 @@ PACKET(13, INT(eid) BYTE(action)) \
PACKET(14, INT(eid) STRING16(name) INT(x) INT(y) INT(z) BYTE(yaw) BYTE(pitch) SHORT(current_item)) \
PACKET(15, INT(eid) SHORT(item_id) BYTE(count) SHORT(damage) INT(x) INT(y) INT(z) BYTE(rotation) BYTE(pitch) BYTE(roll)) \
PACKET(16, INT(eid) INT(collected_by)) \
-PACKET(17, INT(eid) BYTE(type) INT(x) INT(y) INT(z) INT(thrower) SHORT(speed_x) SHORT(speed_y) SHORT(speed_z)) \
+PACKET(17, INT(eid) BYTE(type) INT(x) INT(y) INT(z) INT(thrower) CODE(if (packet.thrower == 0) { packet.speed_x = 0; packet.speed_y = 0; packet.speed_z = 0; goto done; }) SHORT(speed_x) SHORT(speed_y) SHORT(speed_z) CODE(done:)) \
PACKET(18, INT(eid) BYTE(type) INT(x) INT(y) INT(z) BYTE(yaw) BYTE(pitch) BYTE(head_yaw) METADATA(metadata)) \
PACKET(19, INT(eid) STRING16(title) INT(x) INT(y) INT(z) INT(direction)) \
PACKET(1A, INT(eid) INT(x) INT(y) INT(z) SHORT(count)) \
@@ -44,30 +44,30 @@ PACKET(29, INT(eid) BYTE(effect) BYTE(amplification) SHORT(duration)) \
PACKET(2A, INT(eid) BYTE(effect)) \
PACKET(2B, FLOAT(experience_bar) SHORT(level) SHORT(experience)) \
PACKET(32, INT(x) INT(z) BOOL(mode)) \
-/*PACKET(33, INT(x) INT(z) BOOL(solid) USHORT(primary_bitmap) USHORT(add_bitmap) INT(data_len) INT(unused) BLOB(data, data_len))*/ \
-/*PACKET(34, INT(x) INT(z) SHORT(count) INT(data_len) BLOB(data, data_len))*/ \
+PACKET(33, INT(x) INT(z) BOOL(solid) USHORT(primary_bitmap) USHORT(add_bitmap) INT(data_len) INT(unused) BLOB(data, data_len)) \
+PACKET(34, INT(x) INT(z) SHORT(count) INT(data_len) BLOB(data, data_len)) \
PACKET(35, INT(x) BYTE(y) INT(z) BYTE(type) BYTE(meta)) \
-PACKET(36, INT(x) BYTE(y) INT(z) BYTE(one) BYTE(two)) \
-/*PACKET(3C, DOUBLE(x) DOUBLE(y) DOUBLE(z) FLOAT(radius) INT(count) BLOB(data, count * 3))*/ \
+PACKET(36, INT(x) SHORT(y) INT(z) BYTE(one) BYTE(two)) \
+PACKET(3C, DOUBLE(x) DOUBLE(y) DOUBLE(z) FLOAT(radius) INT(count) BLOB(data, count * 3)) \
PACKET(3D, INT(effect_id) INT(x) BYTE(y) INT(z) INT(data)) \
PACKET(46, BYTE(reason) BYTE(mode)) \
PACKET(47, INT(eid) BOOL(unknown) INT(x) INT(y) INT(z)) \
PACKET(64, BYTE(window) BYTE(type) STRING16(title) BYTE(slots)) \
PACKET(65, BYTE(window)) \
-/*PACKET(66, BYTE(window) SHORT(slot) BYTE(right_click) SHORT(action) BOOL(shift) SLOT(item))*/ \
-/*PACKET(67, BYTE(window) SHORT(slot) SLOT(item))*/ \
-/*PACKET(68, BYTE(window) SHORT(count) SLOTS(items))*/ \
+PACKET(66, BYTE(window) SHORT(slot) BYTE(right_click) SHORT(action) BOOL(shift) SLOT(slot_data)) \
+PACKET(67, BYTE(window) SHORT(slot) SLOT(slot_data)) \
+PACKET(68, BYTE(window) SHORT(count) SLOTS(slot_data, count)) \
PACKET(69, BYTE(window) SHORT(property) SHORT(value)) \
PACKET(6A, BYTE(window) SHORT(action) BOOL(accepted)) \
-/*PACKET(6B, SHORT(slot) SLOT(item)) \*/ \
+PACKET(6B, SHORT(slot) SLOT(slot_data)) \
PACKET(6C, BYTE(window) BYTE(echantment)) \
PACKET(82, INT(x) SHORT(y) INT(z) STRING16(line1) STRING16(line2) STRING16(line3) STRING16(line4)) \
-/*PACKET(83, SHORT(type) SHORT(id) UBYTE(data_len) BLOB(data, data_len))*/ \
+PACKET(83, SHORT(type) SHORT(id) UBYTE(data_len) BLOB(data, data_len)) \
PACKET(84, INT(x) SHORT(y) INT(z) BYTE(action) INT(custom1) INT(custom2) INT(custom3)) \
PACKET(C8, INT(statistic) BYTE(amount)) \
PACKET(C9, STRING16(name) BOOL(online) SHORT(ping)) \
PACKET(CA, BOOL(invincible) BOOL(flying) BOOL(can_fly) BOOL(instant_destroy)) \
-/*PACKET(FA, STRING16(channel) SHORT(data_length) BLOB(data, length))*/ \
+PACKET(FA, STRING16(channel) SHORT(data_length) BLOB(data, data_length)) \
PACKET(FE, ) \
PACKET(FF, STRING16(reason))
View
3  include/mcnet/read.h
@@ -3,8 +3,11 @@
#include <stdint.h>
+int8_t mcnet_read_bool(uint8_t* in);
int8_t mcnet_read_int8(uint8_t* in);
+uint8_t mcnet_read_uint8(uint8_t* in);
int16_t mcnet_read_int16(uint8_t* in);
+uint16_t mcnet_read_uint16(uint8_t* in);
int32_t mcnet_read_int32(uint8_t* in);
int64_t mcnet_read_int64(uint8_t* in);
float mcnet_read_float(uint8_t* in);
View
24 include/mcnet/slot.h
@@ -0,0 +1,24 @@
+#ifndef MCNET_SLOT_H
+#define MCNET_SLOT_H
+
+typedef struct mcnet_slot_s {
+ int16_t item;
+ int8_t count;
+ int16_t meta;
+ int16_t data_len;
+ uint8_t* data;
+} mcnet_slot_t;
+
+struct mcnet_slot_parser_s;
+typedef void (*mcnet_slot_parser_complete_cb)(struct mcnet_slot_parser_s* parser, mcnet_slot_t* slot);
+typedef void (*mcnet_slot_parser_error_cb)(struct mcnet_slot_parser_s* parser, int error);
+
+typedef struct mcnet_slot_parser_s {
+ void* data;
+ mcnet_slot_parser_complete_cb on_complete;
+ mcnet_slot_parser_error_cb on_error;
+} mcnet_slot_parser_t;
+
+size_t mcnet_slot_parser_parse(mcnet_slot_parser_t* parser, uint8_t* data, size_t len);
+
+#endif
View
13 include/mcnet/structs.h
@@ -4,20 +4,22 @@
#include <stdint.h>
#include "packets.h"
-#include "metadata.h"
#define BOOL(name) int8_t name;
#define BYTE(name) int8_t name;
#define UBYTE(name) uint8_t name;
#define SHORT(name) int16_t name;
+#define USHORT(name) uint16_t name;
#define INT(name) int32_t name;
#define LONG(name) int64_t name;
#define FLOAT(name) float name;
#define DOUBLE(name) double name;
-#define STRING8(name) int16_t name##_len; uint8_t* name;
-#define STRING16(name) STRING8(name)
#define BLOB(name, length) uint8_t* name;
-#define METADATA(name) int16_t name##_len; mcnet_metadata_t* name;
+#define STRING8(name) int16_t name##_len; BLOB(name, )
+#define STRING16(name) STRING8(name)
+#define METADATA(name) SHORT(name##_len) BLOB(name, )
+#define SLOT(name) SHORT(name##_len) BLOB(name, )
+#define SLOTS(name, count) SHORT(name##_len) BLOB(name, )
#define CODE(code)
#define PACKET(id, code) typedef struct mcnet_packet_##id##_s { \
@@ -37,6 +39,7 @@ PACKETS
#undef BYTE
#undef UBYTE
#undef SHORT
+#undef USHORT
#undef INT
#undef LONG
#undef FLOAT
@@ -45,6 +48,8 @@ PACKETS
#undef STRING16
#undef BLOB
#undef METADATA
+#undef SLOT
+#undef SLOTS
#undef CODE
#endif
View
0  obj/.gitkeep
No changes.
View
2  src/metadata.c
@@ -18,6 +18,8 @@ size_t mcnet_metadata_parser_parse(mcnet_metadata_parser_t* parser, uint8_t* dat
nparsed += this_round;
if (this_round == 1) {
+ if (parser && parser->on_complete) { parser->on_complete(parser); }
+
break;
}
}
View
54 src/parser.c
@@ -1,4 +1,3 @@
-#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
@@ -6,6 +5,7 @@
#include "../include/mcnet/packets.h"
#include "../include/mcnet/structs.h"
#include "../include/mcnet/metadata.h"
+#include "../include/mcnet/slot.h"
#include "../include/mcnet/read.h"
#include "../include/mcnet/parser.h"
@@ -20,20 +20,41 @@
return nparsed; \
}
-#define BOOL(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = *((int8_t*)(data + nparsed)) ? 1 : 0; nparsed += 1;
-#define BYTE(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = *((int8_t*)(data + nparsed)); nparsed += 1;
-#define UBYTE(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = *((uint8_t*)(data + nparsed)); nparsed += 1;
-#define SHORT(name) if (data_len < nparsed + 2) { return MCNET_EAGAIN; } packet.name = mcnet_read_int16(data + nparsed); nparsed += 2;
-#define INT(name) if (data_len < nparsed + 4) { return MCNET_EAGAIN; } packet.name = mcnet_read_int32(data + nparsed); nparsed += 4;
-#define LONG(name) if (data_len < nparsed + 8) { return MCNET_EAGAIN; } packet.name = mcnet_read_int64(data + nparsed); nparsed += 8;
-#define FLOAT(name) if (data_len < nparsed + 4) { return MCNET_EAGAIN; } packet.name = mcnet_read_float(data + nparsed); nparsed += 4;
-#define DOUBLE(name) if (data_len < nparsed + 8) { return MCNET_EAGAIN; } packet.name = mcnet_read_double(data + nparsed); nparsed += 8;
-#define BLOB(name, length) if (data_len < nparsed + length) { return MCNET_EAGAIN; } packet.name = data + nparsed; nparsed += length;
-#define STRING8(name) SHORT(name##_len) BLOB(name, packet.name##_len)
-#define STRING16(name) SHORT(name##_len) BLOB(name, packet.name##_len * 2)
+#define CODE(data) data
+#define BOOL(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = mcnet_read_bool(data + nparsed); nparsed += 1;
+#define BYTE(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = mcnet_read_int8(data + nparsed); nparsed += 1;
+#define UBYTE(name) if (data_len < nparsed + 1) { return MCNET_EAGAIN; } packet.name = mcnet_read_uint8(data + nparsed); nparsed += 1;
+#define SHORT(name) if (data_len < nparsed + 2) { return MCNET_EAGAIN; } packet.name = mcnet_read_int16(data + nparsed); nparsed += 2;
+#define USHORT(name) if (data_len < nparsed + 2) { return MCNET_EAGAIN; } packet.name = mcnet_read_uint16(data + nparsed); nparsed += 2;
+#define INT(name) if (data_len < nparsed + 4) { return MCNET_EAGAIN; } packet.name = mcnet_read_int32(data + nparsed); nparsed += 4;
+#define LONG(name) if (data_len < nparsed + 8) { return MCNET_EAGAIN; } packet.name = mcnet_read_int64(data + nparsed); nparsed += 8;
+#define FLOAT(name) if (data_len < nparsed + 4) { return MCNET_EAGAIN; } packet.name = mcnet_read_float(data + nparsed); nparsed += 4;
+#define DOUBLE(name) if (data_len < nparsed + 8) { return MCNET_EAGAIN; } packet.name = mcnet_read_double(data + nparsed); nparsed += 8;
+#define BLOB(name, length) if (data_len < nparsed + packet.length) { return MCNET_EAGAIN; } packet.name = data + nparsed; nparsed += packet.length;
+#define STRING8(name) SHORT(name##_len) BLOB(name, name##_len)
+#define STRING16(name) SHORT(name##_len) BLOB(name, name##_len * 2)
#define METADATA(name) \
size_t name = mcnet_metadata_parser_parse(NULL, data + nparsed, data_len); \
if ((name == MCNET_EAGAIN) || (name == MCNET_EINVALID)) { return name; } \
+ packet.name##_len = name; \
+ packet.name = data + nparsed; \
+ nparsed += name;
+#define SLOT(name) \
+ size_t name = mcnet_slot_parser_parse(NULL, data + nparsed, data_len); \
+ if ((name == MCNET_EAGAIN) || (name == MCNET_EINVALID)) { return name; } \
+ packet.name##_len = name; \
+ packet.name = data + nparsed; \
+ nparsed += name;
+#define SLOTS(name, len) \
+ size_t name = 0; \
+ int i = 0; \
+ for (i = 0; i < packet.len; ++i) { \
+ size_t tmp = mcnet_slot_parser_parse(NULL, data + nparsed, data_len); \
+ if ((name == MCNET_EAGAIN) || (name == MCNET_EINVALID)) { return name; } \
+ name += tmp; \
+ } \
+ packet.name##_len = name; \
+ packet.name = data + nparsed; \
nparsed += name;
PACKETS
@@ -42,6 +63,7 @@ PACKETS
#undef BYTE
#undef UBYTE
#undef SHORT
+#undef USHORT
#undef INT
#undef LONG
#undef FLOAT
@@ -49,6 +71,8 @@ PACKETS
#undef STRING8
#undef STRING16
#undef METADATA
+#undef SLOT
+#undef SLOTS
#undef PACKET
@@ -63,13 +87,11 @@ size_t mcnet_parser_execute(mcnet_parser_t* parser, mcnet_parser_settings_t* set
PACKETS
default: {
- printf("Unknown packet: %02x\n", data[0]);
-
if (settings->on_error != NULL) {
- settings->on_error(parser, -1);
+ settings->on_error(parser, MCNET_EINVALID);
}
- return MCNET_EAGAIN;
+ return MCNET_EINVALID;
}
}
View
19 src/read.c
@@ -2,10 +2,18 @@
#include "../include/mcnet/read.h"
+int8_t mcnet_read_bool(uint8_t* in) {
+ return (*((int8_t*)(in)) == 0) ? 0 : 1;
+}
+
int8_t mcnet_read_int8(uint8_t* in) {
return *((int8_t*)(in));
}
+uint8_t mcnet_read_uint8(uint8_t* in) {
+ return *((uint8_t*)(in));
+}
+
int16_t mcnet_read_int16(uint8_t* in) {
int16_t out;
@@ -17,6 +25,17 @@ int16_t mcnet_read_int16(uint8_t* in) {
return out;
}
+uint16_t mcnet_read_uint16(uint8_t* in) {
+ uint16_t out;
+
+ uint8_t* t = (uint8_t*)&out;
+
+ t[0] = in[1];
+ t[1] = in[0];
+
+ return out;
+}
+
int32_t mcnet_read_int32(uint8_t* in) {
int32_t out;
View
57 src/slot.c
@@ -0,0 +1,57 @@
+#include <stdint.h>
+#include <stddef.h>
+
+#include "../include/mcnet/error.h"
+#include "../include/mcnet/read.h"
+#include "../include/mcnet/slot.h"
+
+size_t mcnet_slot_parser_parse(mcnet_slot_parser_t* parser, uint8_t* data, size_t len) {
+ mcnet_slot_t slot;
+ size_t nparsed = 0;
+
+ if (len < nparsed + 2) {
+ return MCNET_EAGAIN;
+ }
+
+ slot.item = mcnet_read_int16(data + nparsed);
+ nparsed += 2;
+
+ if (slot.item == -1) {
+ if (parser && parser->on_complete) { parser->on_complete(parser, &slot); }
+
+ return nparsed;
+ }
+
+ if (len < nparsed + 3) {
+ return MCNET_EAGAIN;
+ }
+
+ slot.count = mcnet_read_int8(data + nparsed);
+ nparsed += 1;
+ slot.meta = mcnet_read_int16(data + nparsed);
+ nparsed += 2;
+
+ if (!((slot.item == 256) || (slot.item == 257) || (slot.item == 258) || (slot.item == 259) || (slot.item == 261) || (slot.item == 267) || (slot.item == 268) || (slot.item == 269) || (slot.item == 270) || (slot.item == 271) || (slot.item == 272) || (slot.item == 273) || (slot.item == 274) || (slot.item == 275) || (slot.item == 276) || (slot.item == 277) || (slot.item == 278) || (slot.item == 279) || (slot.item == 283) || (slot.item == 284) || (slot.item == 285) || (slot.item == 286) || (slot.item == 290) || (slot.item == 291) || (slot.item == 292) || (slot.item == 293) || (slot.item == 294) || (slot.item == 298) || (slot.item == 299) || (slot.item == 300) || (slot.item == 301) || (slot.item == 302) || (slot.item == 303) || (slot.item == 304) || (slot.item == 305) || (slot.item == 306) || (slot.item == 307) || (slot.item == 308) || (slot.item == 309) || (slot.item == 310) || (slot.item == 311) || (slot.item == 312) || (slot.item == 313) || (slot.item == 314) || (slot.item == 315) || (slot.item == 316) || (slot.item == 317) || (slot.item == 346) || (slot.item == 359))) {
+ if (parser && parser->on_complete) { parser->on_complete(parser, &slot); }
+
+ return nparsed;
+ }
+
+ if (len < nparsed + 2) {
+ return MCNET_EAGAIN;
+ }
+
+ slot.data_len = mcnet_read_int16(data + nparsed);
+ nparsed += 2;
+
+ if (len < nparsed + slot.data_len) {
+ return MCNET_EAGAIN;
+ }
+
+ slot.data = data + nparsed;
+ nparsed += slot.data_len;
+
+ if (parser && parser->on_complete) { parser->on_complete(parser, &slot); }
+
+ return nparsed;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.