diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-26 15:39:31 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-26 15:39:31 +0200 |
commit | 60f07661602a5dfe8e39b8038964b38bddcb33a5 (patch) | |
tree | 0b257acda0797a13cd09e7df2d16a6da0a6aef11 /shared/bin.h | |
parent | 333eea840a17d0f8ecf0110d952df2857fea4da0 (diff) | |
parent | f073c9d3848dab915bed4844e9d13684aa5e23eb (diff) |
merge dev into master
Diffstat (limited to 'shared/bin.h')
-rw-r--r-- | shared/bin.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/shared/bin.h b/shared/bin.h new file mode 100644 index 0000000..2b65c95 --- /dev/null +++ b/shared/bin.h @@ -0,0 +1,34 @@ +#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; + +typedef struct { + uint16_t bytes; + uint8_t data[]; +} w2_s_bin; + +/** allocate new w2_s_bin struct and fill with `*data` for `bytes` bytes */ +w2_s_bin *w2_bin_s_alloc(uint16_t bytes, uint8_t *data); +/** concatenate 2 w2_s_bin structs, deallocates `a` and `b` */ +w2_s_bin *w2_bin_s_cat(w2_s_bin *a, w2_s_bin *b); + +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); |