11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#import "objfw-defs.h"
#include <stddef.h>
#include <stdint.h>
#ifdef __GNUC__
#define OF_INLINE inline __attribute__((always_inline))
#define OF_LIKELY(cond) __builtin_expect(!!(cond), 1)
#define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#else
#define OF_INLINE inline
#define OF_LIKELY(cond) cond
#define OF_UNLIKELY(cond) cond
#endif
/* Required to build universal binaries on OS X */
#if __BIG_ENDIAN__ || __LITTLE_ENDIAN__
#if __BIG_ENDIAN__ && __LITTLE_ENDIAN__
#error __BIG_ENDIAN__ and __LITTLE_ENDIAN__ defined!
#endif
#undef OF_BIG_ENDIAN
#if __BIG_ENDIAN__
#define OF_BIG_ENDIAN
#endif
#endif
#ifdef __GNUC__
#if defined(__amd64__) || defined(__x86_64__)
#define OF_AMD64_ASM
#elif defined(__i386__)
#define OF_X86_ASM
#elif defined(__ppc__) || defined(__PPC__)
#define OF_PPC_ASM
#elif defined(__arm__) || defined(__ARM__)
#define OF_ARM_ASM
#endif
#endif
#define OF_BSWAP16_CONST(i) \
(((uint16_t)i & UINT16_C(0xFF00)) >> 8 | \
((uint16_t)i & UINT16_C(0x00FF)) << 8)
#define OF_BSWAP32_CONST(i) \
(((uint32_t)i & UINT32_C(0xFF000000)) >> 24 | \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#import "objfw-defs.h"
#include <stddef.h>
#include <stdint.h>
#ifdef __GNUC__
# define OF_INLINE inline __attribute__((always_inline))
# define OF_LIKELY(cond) __builtin_expect(!!(cond), 1)
# define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#else
# define OF_INLINE inline
# define OF_LIKELY(cond) cond
# define OF_UNLIKELY(cond) cond
#endif
/* Required to build universal binaries on OS X */
#if __BIG_ENDIAN__ || __LITTLE_ENDIAN__
# if __BIG_ENDIAN__ && __LITTLE_ENDIAN__
# error __BIG_ENDIAN__ and __LITTLE_ENDIAN__ defined!
# endif
# undef OF_BIG_ENDIAN
# if __BIG_ENDIAN__
# define OF_BIG_ENDIAN
# endif
#endif
#ifdef __GNUC__
# if defined(__amd64__) || defined(__x86_64__)
# define OF_AMD64_ASM
# elif defined(__i386__)
# define OF_X86_ASM
# elif defined(__ppc__) || defined(__PPC__)
# define OF_PPC_ASM
# elif defined(__arm__) || defined(__ARM__)
# define OF_ARM_ASM
# endif
#endif
#define OF_BSWAP16_CONST(i) \
(((uint16_t)i & UINT16_C(0xFF00)) >> 8 | \
((uint16_t)i & UINT16_C(0x00FF)) << 8)
#define OF_BSWAP32_CONST(i) \
(((uint32_t)i & UINT32_C(0xFF000000)) >> 24 | \
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
i = (uint64_t)OF_BSWAP32_NONCONST(i & 0xFFFFFFFF) << 32 |
OF_BSWAP32_NONCONST(i >> 32);
#endif
return i;
}
#ifdef __GNUC__
#define OF_BSWAP16(i) \
(__builtin_constant_p(i) ? OF_BSWAP16_CONST(i) : OF_BSWAP16_NONCONST(i))
#define OF_BSWAP32(i) \
(__builtin_constant_p(i) ? OF_BSWAP32_CONST(i) : OF_BSWAP32_NONCONST(i))
#define OF_BSWAP64(i) \
(__builtin_constant_p(i) ? OF_BSWAP64_CONST(i) : OF_BSWAP64_NONCONST(i))
#else
#define OF_BSWAP16(i) OF_BSWAP16_CONST(i)
#define OF_BSWAP32(i) OF_BSWAP32_CONST(i)
#define OF_BSWAP64(i) OF_BSWAP64_CONST(i)
#endif
static OF_INLINE void
OF_BSWAP32_V(uint32_t *buf, size_t len)
{
while (len--) {
*buf = OF_BSWAP32(*buf);
buf++;
}
}
#ifdef OF_BIG_ENDIAN
#define OF_BSWAP16_IF_BE(i) OF_BSWAP16(i)
#define OF_BSWAP32_IF_BE(i) OF_BSWAP32(i)
#define OF_BSWAP64_IF_BE(i) OF_BSWAP64(i)
#define OF_BSWAP16_IF_LE(i) i
#define OF_BSWAP32_IF_LE(i) i
#define OF_BSWAP64_IF_LE(i) i
#define OF_BSWAP32_V_IF_BE(buf, len) OF_BSWAP32_V(buf, len)
#else
#define OF_BSWAP16_IF_BE(i) i
#define OF_BSWAP32_IF_BE(i) i
#define OF_BSWAP64_IF_BE(i) i
#define OF_BSWAP16_IF_LE(i) OF_BSWAP16(i)
#define OF_BSWAP32_IF_LE(i) OF_BSWAP32(i)
#define OF_BSWAP64_IF_LE(i) OF_BSWAP64(i)
#define OF_BSWAP32_V_IF_BE(buf, len)
#endif
#define OF_ROL(val, bits) \
(((val) << (bits)) | ((val) >> (32 - (bits))))
#define OF_HASH_INIT(hash) hash = 0
#define OF_HASH_ADD(hash, byte) \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
i = (uint64_t)OF_BSWAP32_NONCONST(i & 0xFFFFFFFF) << 32 |
OF_BSWAP32_NONCONST(i >> 32);
#endif
return i;
}
#ifdef __GNUC__
# define OF_BSWAP16(i) \
(__builtin_constant_p(i) ? OF_BSWAP16_CONST(i) : OF_BSWAP16_NONCONST(i))
# define OF_BSWAP32(i) \
(__builtin_constant_p(i) ? OF_BSWAP32_CONST(i) : OF_BSWAP32_NONCONST(i))
# define OF_BSWAP64(i) \
(__builtin_constant_p(i) ? OF_BSWAP64_CONST(i) : OF_BSWAP64_NONCONST(i))
#else
# define OF_BSWAP16(i) OF_BSWAP16_CONST(i)
# define OF_BSWAP32(i) OF_BSWAP32_CONST(i)
# define OF_BSWAP64(i) OF_BSWAP64_CONST(i)
#endif
static OF_INLINE void
OF_BSWAP32_V(uint32_t *buf, size_t len)
{
while (len--) {
*buf = OF_BSWAP32(*buf);
buf++;
}
}
#ifdef OF_BIG_ENDIAN
# define OF_BSWAP16_IF_BE(i) OF_BSWAP16(i)
# define OF_BSWAP32_IF_BE(i) OF_BSWAP32(i)
# define OF_BSWAP64_IF_BE(i) OF_BSWAP64(i)
# define OF_BSWAP16_IF_LE(i) i
# define OF_BSWAP32_IF_LE(i) i
# define OF_BSWAP64_IF_LE(i) i
# define OF_BSWAP32_V_IF_BE(buf, len) OF_BSWAP32_V(buf, len)
#else
# define OF_BSWAP16_IF_BE(i) i
# define OF_BSWAP32_IF_BE(i) i
# define OF_BSWAP64_IF_BE(i) i
# define OF_BSWAP16_IF_LE(i) OF_BSWAP16(i)
# define OF_BSWAP32_IF_LE(i) OF_BSWAP32(i)
# define OF_BSWAP64_IF_LE(i) OF_BSWAP64(i)
# define OF_BSWAP32_V_IF_BE(buf, len)
#endif
#define OF_ROL(val, bits) \
(((val) << (bits)) | ((val) >> (32 - (bits))))
#define OF_HASH_INIT(hash) hash = 0
#define OF_HASH_ADD(hash, byte) \
|