+/* Read an 8bit value */
+static int fread_8(uint8_t *buf, FILE *fd)
+{
+ return (fread(buf, sizeof(*buf), 1, fd) == 1) ? 0 : -1;
+}
+
+/* Read a 32bit little endian value and convert to host endianness. */
+static int fread_le32(uint32_t *buf, FILE *fd)
+{
+ size_t count;
+ uint8_t tmp[4];
+ unsigned int i;
+
+ if (fread(tmp, sizeof(tmp), 1, fd) != 1)
+ return -1;
+ *buf = 0;
+ for (i = 0; i < ARRAY_SIZE(tmp); i++)
+ *buf |= (uint32_t)(tmp[i]) << (i * 8);
+
+ return 0;
+}
+
+/* Read a 64bit little endian value and convert to host endianness. */
+static int fread_le64(uint64_t *buf, FILE *fd)
+{
+ size_t count;
+ uint8_t tmp[8];
+ unsigned int i;
+
+ if (fread(tmp, sizeof(tmp), 1, fd) != 1)
+ return -1;
+ *buf = 0;
+ for (i = 0; i < ARRAY_SIZE(tmp); i++)
+ *buf |= (uint64_t)(tmp[i]) << (i * 8);
+
+ return 0;
+}
+
+/* Write an 8bit value */
+static int fwrite_8(uint8_t buf, FILE *fd)
+{
+ return (fwrite(&buf, sizeof(buf), 1, fd) == 1) ? 0 : -1;
+}
+
+/* Convert to little endian and write a 32bit value */
+static int fwrite_le32(uint32_t buf, FILE *fd)
+{
+ size_t count;
+ uint8_t tmp[4];
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(tmp); i++)
+ tmp[i] = buf >> (i * 8);
+ if (fwrite(tmp, sizeof(tmp), 1, fd) != 1)
+ return -1;
+
+ return 0;
+}
+