summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClement Sibille <clements+git@lisible.xyz>2024-05-20 00:52:17 +0900
committerClement Sibille <clements+git@lisible.xyz>2024-05-20 01:28:21 +0900
commit2d813cc4c26af142bcfaabdf654371e7801f6491 (patch)
treecbbf8cbb2af9554d7215e96a63fcc213d05c853a /src
parentbd22b018c841cddf4eb8e1dae48eb14eef850523 (diff)
Add bytes module
Diffstat (limited to 'src')
-rw-r--r--src/lisiblestd/bytes.c24
-rw-r--r--src/lisiblestd/bytes.h11
2 files changed, 35 insertions, 0 deletions
diff --git a/src/lisiblestd/bytes.c b/src/lisiblestd/bytes.c
new file mode 100644
index 0000000..0860800
--- /dev/null
+++ b/src/lisiblestd/bytes.c
@@ -0,0 +1,24 @@
+#include "bytes.h"
+#include "assert.h"
+#include <string.h>
+
+u32 u32_from_bytes(const u8 *bytes) {
+ LSTD_ASSERT(bytes != NULL);
+ return (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
+}
+u32 u32_from_bytes_le(const u8 *bytes) {
+ LSTD_ASSERT(bytes != NULL);
+ return (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];
+}
+
+u16 u16_from_bytes_le(const u8 *bytes) {
+ LSTD_ASSERT(bytes != NULL);
+ return (bytes[1] << 8) + bytes[0];
+}
+
+float float_from_bytes_le(const u8 *bytes) {
+ LSTD_ASSERT(bytes != NULL);
+ float result;
+ memcpy(&result, bytes, sizeof(float));
+ return result;
+}
diff --git a/src/lisiblestd/bytes.h b/src/lisiblestd/bytes.h
new file mode 100644
index 0000000..8af109b
--- /dev/null
+++ b/src/lisiblestd/bytes.h
@@ -0,0 +1,11 @@
+#ifndef LSTD_BYTES_H
+#define LSTD_BYTES_H
+
+#include "types.h"
+
+u32 u32_from_bytes(const u8 *bytes);
+u32 u32_from_bytes_le(const u8 *bytes);
+u16 u16_from_bytes_le(const u8 *bytes);
+float float_from_bytes_le(const u8 *bytes);
+
+#endif // LSTD_BYTES_H
Go back to lisible.xyz