srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/include/wireframe/byteio.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/wireframe/byteio.hpp')
-rw-r--r--include/wireframe/byteio.hpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/include/wireframe/byteio.hpp b/include/wireframe/byteio.hpp
new file mode 100644
index 0000000..c37c29e
--- /dev/null
+++ b/include/wireframe/byteio.hpp
@@ -0,0 +1,22 @@
+#pragma once
+
+#include <cstdint>
+#include <span>
+
+// Manual big-endian reads instead of reinterpret_cast onto a packed
+// struct: network buffers from pcap aren't guaranteed aligned for
+// multi-byte integer types, so casting would be undefined behavior.
+namespace wireframe {
+
+inline std::uint16_t read_be16(std::span<const unsigned char> bytes, std::size_t offset) {
+ return static_cast<std::uint16_t>((bytes[offset] << 8) | bytes[offset + 1]);
+}
+
+inline std::uint32_t read_be32(std::span<const unsigned char> bytes, std::size_t offset) {
+ return (static_cast<std::uint32_t>(bytes[offset]) << 24) |
+ (static_cast<std::uint32_t>(bytes[offset + 1]) << 16) |
+ (static_cast<std::uint32_t>(bytes[offset + 2]) << 8) |
+ static_cast<std::uint32_t>(bytes[offset + 3]);
+}
+
+} // namespace wireframe