aboutsummaryrefslogtreecommitdiff
path: root/shared/bin.h
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-24 21:56:59 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-24 21:56:59 +0200
commit937a3a736aaf2c468c8c8e8dbc7963a87eae890f (patch)
treedb8d9a717727b907c77c24aff98ed90c82ccb22a /shared/bin.h
parentede8a89706209fa26e151a34a28e64affbffd23d (diff)
move some code to a shared folder
Diffstat (limited to 'shared/bin.h')
-rw-r--r--shared/bin.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/shared/bin.h b/shared/bin.h
new file mode 100644
index 0000000..1c9b951
--- /dev/null
+++ b/shared/bin.h
@@ -0,0 +1,52 @@
+#pragma once
+/**
+ * helper file for binary data
+ *
+ * - fix endianness with functions inspired by UNIX arpa/inet.h
+ * - convert uint16_t and uint32_t to w2_s_bin
+ */
+
+#include <stdint.h>
+
+extern uint8_t g_w2_endianness;
+
+#define W2_T_UINT8_T (0)
+#define W2_T_UINT16_T (1)
+#define W2_T_UINT32_T (2)
+
+enum w2_e_struct_types {
+ W2_ST_UINT8_T,
+ W2_ST_UINT16_T,
+ W2_ST_UINT32_T,
+};
+
+extern const uint8_t W2_STRUCT_T_SIZES[];
+
+typedef struct {
+ uint16_t bytes;
+ uint8_t data[];
+} w2_s_bin;
+
+typedef struct {
+ enum w2_e_struct_types type;
+ uint16_t length;
+ const uint8_t *data;
+} w2_s_struct_property;
+
+typedef struct {
+ uint16_t length;
+ w2_s_struct_property *properties[];
+} w2_s_property_list;
+
+w2_s_bin *w2_bin_from_uint8_t(uint8_t data);
+w2_s_bin *w2_bin_from_uint16_t(uint16_t data);
+w2_s_bin *w2_bin_from_uint32_t(uint32_t data);
+
+/** convert 32-bit value from host endian to network (big-endian) */
+uint32_t w2_bin_hton32(uint32_t h32);
+/** convert 16-bit value from host endian to network (big-endian) */
+uint16_t w2_bin_hton16(uint16_t h16);
+/** convert 32-bit value from network (big-endian) to host endian */
+uint32_t w2_bin_ntoh32(uint32_t n32);
+/** convert 16-bit value from network (big-endian) to host endian */
+uint16_t w2_bin_ntoh16(uint16_t n16);