4 * A few endianness-aware macros for reading and writing data from in-memory
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "endianness.h"
32 /* Note that in the WIM format, integers are always in little-endian format. */
34 /* The get_u8, get_u16, get_u32, get_u56, and get_u64 functions take in a
35 * pointer to an input location as the first argument and a pointer to an output
36 * location as the second argument. The data in the input location is copied to
37 * the output location, with the size indicated in the function name, in little
38 * endian format. A pointer to the input location directly following the bytes
39 * read is returned. */
40 static inline const u8 *get_u8(const u8 *p, u8 *res)
47 static inline const u8 *get_u16(const u8 *p, u16 *res)
49 *res = to_le16(*(u16*)p);
55 static inline const u8 *get_u32(const u8 *p, u32 *res)
57 *res = to_le32(*(u32*)p);
62 static inline const u8 *get_u56(const u8 *p, u64 *res)
64 *res = to_le64(*(u64*)p) & 0x00ffffffffffffff;
69 static inline const u8 *get_u64(const u8 *p, u64 *res)
71 *res = to_le64(*(u64*)p);
75 /* The put_u8, put_u16, put_u32, put_u56, and put_u64 functions take in a
76 * pointer to an output location as the first argument and a value for the
77 * second argument. The value of the second argument is written to the output
78 * location in little-endian format as the data type indicated in the function
79 * name, and a pointer to the output location directory following the bytes
80 * written is returned. */
81 static inline u8 *put_u8(u8 *res, u8 val)
87 static inline u8 *put_u16(u8 *res, u16 val)
89 *(uint16_t*)res = to_le16(val);
93 static inline u8 *put_u32(u8 *res, u32 val)
95 *(uint32_t*)res = to_le32(val);
99 static inline u8 *put_u56(u8 *res, u64 val)
101 const u8 *__p = (const u8*)&val;
102 #ifdef WORDS_BIGENDIAN
116 static inline u8 *put_u64(u8 *res, u64 val)
118 *(u64*)res = to_le64(val);
122 static inline const u8 *get_bytes(const u8 *p, size_t num_bytes, void *res)
124 memcpy(res, p, num_bytes);
125 return p + num_bytes;
128 static inline u8 *put_bytes(u8 *p, size_t num_bytes, const u8 *input)
130 memcpy(p, input, num_bytes);
131 return p + num_bytes;
133 #endif /* _WIMLIB_IO_H */