Update copyright year to 2017.
[paraslash.git] / portable_io.h
index 7371038a57cd7ef4450f02ddaf0add12c09050ca..4e10c2e3386d1511ac6f71bbfc47210d02852e3a 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Copyright (C) 2007-2014 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file portable_io.h Inline functions for endian-independent binary IO. */
+/** \file portable_io.h Inline functions for binary IO. */
 
 static inline uint64_t read_portable(unsigned bits, const char *buf)
 {
@@ -18,6 +18,18 @@ static inline uint64_t read_portable(unsigned bits, const char *buf)
        return ret;
 }
 
+static inline uint64_t read_portable_be(unsigned bits, const char *buf)
+{
+       uint64_t ret = 0;
+       int i, num_bytes = bits / 8;
+
+       for (i = 0; i < num_bytes; i++) {
+               unsigned char c = buf[i];
+               ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
+       }
+       return ret;
+}
+
 static inline uint64_t read_u64(const char *buf)
 {
        return read_portable(64, buf);
@@ -38,13 +50,35 @@ static inline uint8_t read_u8(const char *buf)
        return read_portable(8, buf);
 }
 
+static inline uint64_t read_u64_be(const char *buf)
+{
+       return read_portable_be(64, buf);
+}
+
+static inline uint32_t read_u32_be(const char *buf)
+{
+       return read_portable_be(32, buf);
+}
+
+static inline uint16_t read_u16_be(const char *buf)
+{
+       return read_portable_be(16, buf);
+}
+
 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
-//     fprintf(stderr, "val: %lu\n", val);
        for (i = 0; i < num_bytes; i++) {
                buf[i] = val & 0xff;
-//             fprintf(stderr, "buf[%d]=%x\n", i, buf[i]);
+               val = val >> 8;
+       }
+}
+
+static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
+{
+       int i, num_bytes = bits / 8;
+       for (i = 0; i < num_bytes; i++) {
+               buf[num_bytes - i - 1] = val & 0xff;
                val = val >> 8;
        }
 }
@@ -68,3 +102,18 @@ static inline void write_u8(char *buf, uint8_t val)
 {
        write_portable(8, buf, (uint64_t) val);
 }
+
+static inline void write_u64_be(char *buf, uint64_t val)
+{
+       write_portable_be(64, buf, val);
+}
+
+static inline void write_u32_be(char *buf, uint32_t val)
+{
+       write_portable_be(32, buf, (uint64_t) val);
+}
+
+static inline void write_u16_be(char *buf, uint16_t val)
+{
+       write_portable_be(16, buf, (uint64_t) val);
+}