4 * A few endianness-aware macros for reading and writing data from in-memory
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 u8 *get_u8(const u8 *p, u8 *res)
30 static inline const u8 *get_u16(const u8 *p, u16 *res)
32 *res = to_le16(*(u16*)p);
38 static inline const u8 *get_u32(const u8 *p, u32 *res)
40 *res = to_le32(*(u32*)p);
45 static inline const u8 *get_u56(const u8 *p, u64 *res)
47 *res = to_le64(*(u64*)p) & 0x00ffffffffffffff;
52 static inline const u8 *get_u64(const u8 *p, u64 *res)
54 *res = to_le64(*(u64*)p);
58 /* The put_u8, put_u16, put_u32, put_u56, and put_u64 functions take in a
59 * pointer to an output location as the first argument and a value for the
60 * second argument. The value of the second argument is written to the output
61 * location in little-endian format as the data type indicated in the function
62 * name, and a pointer to the output location directory following the bytes
63 * written is returned. */
64 static inline u8 *put_u8(u8 *res, u8 val)
70 static inline u8 *put_u16(u8 *res, u16 val)
72 *(uint16_t*)res = to_le16(val);
76 static inline u8 *put_u32(u8 *res, u32 val)
78 *(uint32_t*)res = to_le32(val);
82 static inline u8 *put_u56(u8 *res, u64 val)
84 const u8 *__p = (const u8*)&val;
85 #ifdef WORDS_BIGENDIAN
99 static inline u8 *put_u64(u8 *res, u64 val)
101 *(u64*)res = to_le64(val);
105 static inline const u8 *get_bytes(const u8 *p, size_t num_bytes, void *res)
107 memcpy(res, p, num_bytes);
108 return p + num_bytes;
111 static inline u8 *put_zeroes(u8 *p, size_t num_bytes)
113 return (u8*)memset(p, 0, num_bytes) + num_bytes;
116 static inline u8 *put_bytes(u8 *p, size_t num_bytes, const u8 *input)
118 return (u8*)memcpy(p, input, num_bytes) + num_bytes;
120 #endif /* _WIMLIB_IO_H */