diff options
Diffstat (limited to 'src/common/io.h')
-rw-r--r-- | src/common/io.h | 57 |
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); |