summaryrefslogtreecommitdiff
path: root/src/common/io.h
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2024-11-03 14:00:29 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2024-11-03 14:00:29 (GMT)
commit54d05927811a59db294af8a24258c0b837360492 (patch)
tree527064eea471829b12f3b0f5a5b2278cbe4524c2 /src/common/io.h
parentc24b37fd74ae12a90669731f150ac99213fad5b6 (diff)
Load and store various values.
Diffstat (limited to 'src/common/io.h')
-rw-r--r--src/common/io.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/common/io.h b/src/common/io.h
index 7fe9d9d..1932b12 100644
--- a/src/common/io.h
+++ b/src/common/io.h
@@ -39,12 +39,69 @@
/* Lit des données depuis un flux local. */
bool safe_read(int, void *, size_t);
+#define load_u8(f, v) \
+ safe_read(f, v, sizeof(uint8_t));
+
+#define load_u16(f, v, e) \
+ ({ \
+ bool __ret; \
+ uint16_t __val; \
+ __ret = safe_read(f, &__val, sizeof(uint16_t)); \
+ __val = swap_u16(&__val, e); \
+ *(v) = __val; \
+ __ret; \
+ })
+
+#define load_u32(f, v, e) \
+ ({ \
+ bool __ret; \
+ uint32_t __val; \
+ __ret = safe_read(f, &__val, sizeof(uint32_t)); \
+ __val = swap_u32(&__val, e); \
+ *(v) = __val; \
+ __ret; \
+ })
+
+#define load_u64(f, v, e) \
+ ({ \
+ bool __ret; \
+ uint64_t __val; \
+ __ret = safe_read(f, &__val, sizeof(uint64_t)); \
+ __val = swap_u64(&__val, e); \
+ *(v) = __val; \
+ __ret; \
+ })
+
/* Lit des données depuis un flux local. */
ssize_t safe_read_partial(int, void *, size_t);
/* Ecrit des données dans un flux local. */
bool safe_write(int, const void *, size_t);
+#define store_u8(f, v) \
+ safe_write(f, (const uint8_t []){ v }, sizeof(uint8_t));
+
+#define store_u16(f, v, e) \
+ ({ \
+ uint16_t __val; \
+ __val = swap_u16((const uint16_t []){ v }, e); \
+ safe_write(f, &__val, sizeof(uint16_t)); \
+ })
+
+#define store_u32(f, v, e) \
+ ({ \
+ uint32_t __val; \
+ __val = swap_u32((const uint32_t []){ v }, e); \
+ safe_write(f, &__val, sizeof(uint32_t)); \
+ })
+
+#define store_u64(f, v, e) \
+ ({ \
+ uint64_t __val; \
+ __val = swap_u64((const uint64_t []){ v }, e); \
+ safe_write(f, &__val, sizeof(uint64_t)); \
+ })
+
/* Réceptionne des données depuis un flux réseau. */
bool safe_recv(int, void *, size_t, int);