|
27 | 27 | PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, |
28 | 28 | size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen); |
29 | 29 |
|
30 | | -// Use system-provided endian functions on BSD/macOS, provide our own elsewhere |
| 30 | +// Use system-provided endian functions when available, provide our own elsewhere |
31 | 31 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) |
32 | | -#include <sys/endian.h> |
| 32 | + // BSD/macOS: use sys/endian.h |
| 33 | + #include <sys/endian.h> |
| 34 | +#elif defined(__linux__) || defined(__GLIBC__) |
| 35 | + // Linux: use endian.h and provide BSD-style function names |
| 36 | + #include <endian.h> |
| 37 | + #define le32dec(p) le32toh(*(const uint32_t *)(p)) |
| 38 | + #define le32enc(p, v) do { *(uint32_t *)(p) = htole32(v); } while (0) |
33 | 39 | #else |
34 | | -// Portable inline implementations for little-endian encoding/decoding |
35 | | -static inline uint32_t le32dec(const void *pp) |
36 | | -{ |
37 | | - const uint8_t *p = (uint8_t const *)pp; |
38 | | - return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + |
39 | | - ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); |
40 | | -} |
| 40 | + // Fallback: portable inline implementations for little-endian encoding/decoding |
| 41 | + static inline uint32_t le32dec(const void *pp) |
| 42 | + { |
| 43 | + const uint8_t *p = (uint8_t const *)pp; |
| 44 | + return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + |
| 45 | + ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); |
| 46 | + } |
41 | 47 |
|
42 | | -static inline void le32enc(void *pp, uint32_t x) |
43 | | -{ |
44 | | - uint8_t *p = (uint8_t *)pp; |
45 | | - p[0] = x & 0xff; |
46 | | - p[1] = (x >> 8) & 0xff; |
47 | | - p[2] = (x >> 16) & 0xff; |
48 | | - p[3] = (x >> 24) & 0xff; |
49 | | -} |
| 48 | + static inline void le32enc(void *pp, uint32_t x) |
| 49 | + { |
| 50 | + uint8_t *p = (uint8_t *)pp; |
| 51 | + p[0] = x & 0xff; |
| 52 | + p[1] = (x >> 8) & 0xff; |
| 53 | + p[2] = (x >> 16) & 0xff; |
| 54 | + p[3] = (x >> 24) & 0xff; |
| 55 | + } |
50 | 56 | #endif |
51 | 57 | #endif |
0 commit comments