Skip to content

Commit a69902c

Browse files
committed
Avoid including endian.h in fastpbkdf2.c
Rewrite utility functions write32_be and write64_be in fastpbkdf2.c to use macros defined by SQLite
1 parent d4074d5 commit a69902c

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/fastpbkdf2.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
#include <assert.h>
1818
#include <string.h>
19-
#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__clang__) && !defined(__QNX__)
20-
#include <endian.h>
21-
#endif
2219

2320
#include "sha1.h"
2421
#include "sha2.h"
@@ -37,23 +34,41 @@
3734

3835
static inline void write32_be(uint32_t n, uint8_t out[4])
3936
{
40-
#if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
41-
*(uint32_t *)(out) = __builtin_bswap32(n);
37+
#if SQLITE_BYTEORDER==4321
38+
memcpy(out, &n, 4);
39+
#elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
40+
u32 x = __builtin_bswap32(n);
41+
memcpy(out, &x, 4);
42+
#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
43+
u32 x = _byteswap_ulong(n);
44+
memcpy(out, &x, 4);
4245
#else
43-
out[0] = (n >> 24) & 0xff;
44-
out[1] = (n >> 16) & 0xff;
45-
out[2] = (n >> 8) & 0xff;
46-
out[3] = n & 0xff;
46+
out[0] = (n >> 24) & 0xFF;
47+
out[1] = (n >> 16) & 0xFF;
48+
out[2] = (n >> 8) & 0xFF;
49+
out[3] = (n >> 0) & 0xFF;
4750
#endif
4851
}
4952

5053
static inline void write64_be(uint64_t n, uint8_t out[8])
5154
{
52-
#if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
53-
*(uint64_t *)(out) = __builtin_bswap64(n);
55+
#if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
56+
n = __builtin_bswap64(n);
57+
memcpy(out, &n, 8);
58+
#elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
59+
n = _byteswap_uint64(n);
60+
memcpy(out, &n, 8);
61+
#elif SQLITE_BYTEORDER==4321
62+
memcpy(out, &n, 8);
5463
#else
55-
write32_be((n >> 32) & 0xffffffff, out);
56-
write32_be(n & 0xffffffff, out + 4);
64+
out[0] = (n >> 56) & 0xFF;
65+
out[1] = (n >> 48) & 0xFF;
66+
out[2] = (n >> 40) & 0xFF;
67+
out[3] = (n >> 32) & 0xFF;
68+
out[4] = (n >> 24) & 0xFF;
69+
out[5] = (n >> 16) & 0xFF;
70+
out[6] = (n >> 8) & 0xFF;
71+
out[7] = (n >> 0) & 0xFF;
5772
#endif
5873
}
5974

0 commit comments

Comments
 (0)