4 * A few endianness-aware macros for reading and writing data from in-memory
8 #ifndef _WIMLIB_BUFFER_IO_H
9 #define _WIMLIB_BUFFER_IO_H
12 #include "endianness.h"
15 /* Note that in the WIM format, integers are always in little-endian format. */
17 /* The get_u8, get_u16, get_u32, get_u56, and get_u64 functions take in a
18 * pointer to an input location as the first argument and a pointer to an output
19 * location as the second argument. The data in the input location is copied to
20 * the output location, with the size indicated in the function name, in little
21 * endian format. A pointer to the input location directly following the bytes
22 * read is returned. */
23 static inline const void *
24 get_u8(const void *p, u8 *res)
30 static inline const void *
31 get_u16(const void *p, u16 *res)
33 *res = le16_to_cpu(*(const u16*)p);
39 static inline const void *
40 get_u32(const void *p, u32 *res)
42 *res = le32_to_cpu(*(const u32*)p);
47 static inline const void *
48 get_u56(const void *p, u64 *res)
50 *res = le64_to_cpu(*(const u64*)p) & 0x00ffffffffffffff;
55 static inline const void *
56 get_u64(const void *p, u64 *res)
58 *res = le64_to_cpu(*(const u64*)p);
62 /* The put_u8, put_u16, put_u32, put_u56, and put_u64 functions take in a
63 * pointer to an output location as the first argument and a value for the
64 * second argument. The value of the second argument is written to the output
65 * location in little-endian format as the data type indicated in the function
66 * name, and a pointer to the output location directory following the bytes
67 * written is returned. */
69 put_u8(void *res, u8 val)
76 put_u16(void *res, u16 val)
78 *(uint16_t*)res = cpu_to_le16(val);
83 put_u32(void *res, u32 val)
85 *(uint32_t*)res = cpu_to_le32(val);
90 put_u56(void *res, u64 val)
92 const u8 *__p = (const u8*)&val;
93 #ifdef WORDS_BIGENDIAN
108 put_u64(void *res, u64 val)
110 *(u64*)res = cpu_to_le64(val);
114 static inline const void *
115 get_bytes(const void *p, size_t num_bytes, void *res)
117 memcpy(res, p, num_bytes);
118 return p + num_bytes;
122 put_zeroes(void *p, size_t num_bytes)
124 return (u8*)memset(p, 0, num_bytes) + num_bytes;
128 put_bytes(void *p, size_t num_bytes, const void *input)
130 return (u8*)memcpy(p, input, num_bytes) + num_bytes;
132 #endif /* _WIMLIB_BUFFER_IO_H */