Let helpers in portable_io.h receive void * arguments.
authorAndre Noll <maan@tuebingen.mpg.de>
Sat, 15 Jul 2017 13:19:32 +0000 (15:19 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 23 Jul 2017 16:01:10 +0000 (18:01 +0200)
The functions have to convert the bytes to uint8_t (aka unsigned char)
anyway, so allow to pass any pointer type.

portable_io.h

index 4e10c2e3386d1511ac6f71bbfc47210d02852e3a..00704b29309a2edf1125221530940789d51c0053 100644 (file)
@@ -6,61 +6,63 @@
 
 /** \file portable_io.h Inline functions for binary IO. */
 
 
 /** \file portable_io.h Inline functions for binary IO. */
 
-static inline uint64_t read_portable(unsigned bits, const char *buf)
+static inline uint64_t read_portable(unsigned bits, const void *buf)
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
+       const uint8_t *p = (typeof(p))buf;
 
        for (i = 0; i < num_bytes; i++) {
 
        for (i = 0; i < num_bytes; i++) {
-               unsigned char c = buf[i];
+               unsigned char c = p[i];
                ret += ((uint64_t)c << (8 * i));
        }
        return ret;
 }
 
                ret += ((uint64_t)c << (8 * i));
        }
        return ret;
 }
 
-static inline uint64_t read_portable_be(unsigned bits, const char *buf)
+static inline uint64_t read_portable_be(unsigned bits, const void *buf)
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
+       const uint8_t *p = (typeof(p))buf;
 
        for (i = 0; i < num_bytes; i++) {
 
        for (i = 0; i < num_bytes; i++) {
-               unsigned char c = buf[i];
+               unsigned char c = p[i];
                ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
        }
        return ret;
 }
 
                ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
        }
        return ret;
 }
 
-static inline uint64_t read_u64(const char *buf)
+static inline uint64_t read_u64(const void *buf)
 {
        return read_portable(64, buf);
 }
 
 {
        return read_portable(64, buf);
 }
 
-static inline uint32_t read_u32(const char *buf)
+static inline uint32_t read_u32(const void *buf)
 {
        return read_portable(32, buf);
 }
 
 {
        return read_portable(32, buf);
 }
 
-static inline uint16_t read_u16(const char *buf)
+static inline uint16_t read_u16(const void *buf)
 {
        return read_portable(16, buf);
 }
 
 {
        return read_portable(16, buf);
 }
 
-static inline uint8_t read_u8(const char *buf)
+static inline uint8_t read_u8(const void *buf)
 {
        return read_portable(8, buf);
 }
 
 {
        return read_portable(8, buf);
 }
 
-static inline uint64_t read_u64_be(const char *buf)
+static inline uint64_t read_u64_be(const void *buf)
 {
        return read_portable_be(64, buf);
 }
 
 {
        return read_portable_be(64, buf);
 }
 
-static inline uint32_t read_u32_be(const char *buf)
+static inline uint32_t read_u32_be(const void *buf)
 {
        return read_portable_be(32, buf);
 }
 
 {
        return read_portable_be(32, buf);
 }
 
-static inline uint16_t read_u16_be(const char *buf)
+static inline uint16_t read_u16_be(const void *buf)
 {
        return read_portable_be(16, buf);
 }
 {
        return read_portable_be(16, buf);
 }
@@ -68,8 +70,10 @@ static inline uint16_t read_u16_be(const char *buf)
 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
+       uint8_t *p = (typeof(p))buf;
+
        for (i = 0; i < num_bytes; i++) {
        for (i = 0; i < num_bytes; i++) {
-               buf[i] = val & 0xff;
+               p[i] = val & 0xff;
                val = val >> 8;
        }
 }
                val = val >> 8;
        }
 }
@@ -77,43 +81,45 @@ static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
 static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
+       uint8_t *p = (typeof(p))buf;
+
        for (i = 0; i < num_bytes; i++) {
        for (i = 0; i < num_bytes; i++) {
-               buf[num_bytes - i - 1] = val & 0xff;
+               p[num_bytes - i - 1] = val & 0xff;
                val = val >> 8;
        }
 }
 
                val = val >> 8;
        }
 }
 
-static inline void write_u64(char *buf, uint64_t val)
+static inline void write_u64(void *buf, uint64_t val)
 {
        write_portable(64, buf, val);
 }
 
 {
        write_portable(64, buf, val);
 }
 
-static inline void write_u32(char *buf, uint32_t val)
+static inline void write_u32(void *buf, uint32_t val)
 {
        write_portable(32, buf, (uint64_t) val);
 }
 
 {
        write_portable(32, buf, (uint64_t) val);
 }
 
-static inline void write_u16(char *buf, uint16_t val)
+static inline void write_u16(void *buf, uint16_t val)
 {
        write_portable(16, buf, (uint64_t) val);
 }
 
 {
        write_portable(16, buf, (uint64_t) val);
 }
 
-static inline void write_u8(char *buf, uint8_t val)
+static inline void write_u8(void *buf, uint8_t val)
 {
        write_portable(8, buf, (uint64_t) val);
 }
 
 {
        write_portable(8, buf, (uint64_t) val);
 }
 
-static inline void write_u64_be(char *buf, uint64_t val)
+static inline void write_u64_be(void *buf, uint64_t val)
 {
        write_portable_be(64, buf, val);
 }
 
 {
        write_portable_be(64, buf, val);
 }
 
-static inline void write_u32_be(char *buf, uint32_t val)
+static inline void write_u32_be(void *buf, uint32_t val)
 {
        write_portable_be(32, buf, (uint64_t) val);
 }
 
 {
        write_portable_be(32, buf, (uint64_t) val);
 }
 
-static inline void write_u16_be(char *buf, uint16_t val)
+static inline void write_u16_be(void *buf, uint16_t val)
 {
        write_portable_be(16, buf, (uint64_t) val);
 }
 {
        write_portable_be(16, buf, (uint64_t) val);
 }