-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathendian.c
71 lines (57 loc) · 1.28 KB
/
endian.c
1
2
3
4
5
6
7
8
9
10
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
uint64_t xle64dec(const uint8_t *d) {
return OSReadLittleInt64(d, 0);
}
void xle64enc(uint8_t *d, uint64_t n) {
OSWriteLittleInt64(d, 0, n);
}
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__midipix__)
#include "config.h"
#include <stdint.h>
#include <string.h>
#if defined(__linux__) || defined(__midipix__)
#include <endian.h>
#include <byteswap.h>
#else
#include <sys/endian.h>
#endif
#if !HAVE_DECL_HTOLE64
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htole64(x) (x)
# else
# define htole64(x) bswap_64 (x)
# endif
#endif
#if !HAVE_DECL_LE64TOH
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define le64toh(x) (x)
# else
# define le64toh(x) bswap_64 (x)
# endif
#endif
uint64_t xle64dec(const uint8_t *d) {
uint64_t tmp;
memcpy(&tmp, d, sizeof(tmp));
return le64toh(tmp);
}
void xle64enc(uint8_t *d, uint64_t n) {
uint64_t tmp = htole64(n);
memcpy(d, &tmp, sizeof(tmp));
}
#else
// Platform independent
#include <stdint.h>
uint64_t xle64dec(const uint8_t *d) {
uint64_t r = 0;
for (const uint8_t *p = d + sizeof(r) - 1; p >= d; --p)
r = (r << 8) + *p;
return r;
}
void xle64enc(uint8_t *d, uint64_t n) {
for (uint8_t *p = d; p < d + sizeof(n); ++p) {
*p = n & 0xff;
n >>= 8;
}
}
#endif