2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

Move and extend the uint8_t low-endian to uint{32,64}t to endian.h

Move the U8TO{32,64}_LE and U{32,64}TO8_LE macros to endian.h and extend
the macros for 16-bit and Big-Endian variants.

Use the macros both in isc_siphash (LE) and isc_buffer (BE) units.
This commit is contained in:
Ondřej Surý
2022-12-15 12:58:31 +01:00
parent aea251f3bc
commit a1d45685e6
2 changed files with 110 additions and 30 deletions

View File

@@ -164,3 +164,101 @@
#endif /* WORDS_BIGENDIAN */
#endif /* !htobe64 */
/*
* Macros to convert uint8_t arrays to integers.
*/
/* Low-Endian */
#define ISC_U16TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8);
#define ISC_U8TO16_LE(p) (((uint16_t)((p)[0])) | ((uint16_t)((p)[1]) << 8))
#define ISC_U32TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24);
#define ISC_U8TO32_LE(p) \
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
#define ISC_U48TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24); \
(p)[4] = (uint8_t)((v) >> 32); \
(p)[5] = (uint8_t)((v) >> 40);
#define ISC_U8TO48_LE(p) \
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40))
#define ISC_U64TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24); \
(p)[4] = (uint8_t)((v) >> 32); \
(p)[5] = (uint8_t)((v) >> 40); \
(p)[6] = (uint8_t)((v) >> 48); \
(p)[7] = (uint8_t)((v) >> 56);
#define ISC_U8TO64_LE(p) \
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
/* Big-Endian */
#define ISC_U16TO8_BE(p, v) \
(p)[0] = (uint8_t)((v) >> 8); \
(p)[1] = (uint8_t)((v));
#define ISC_U8TO16_BE(p) (((uint16_t)((p)[0]) << 8) | ((uint16_t)((p)[1])))
#define ISC_U32TO8_BE(p, v) \
(p)[0] = (uint8_t)((v) >> 24); \
(p)[1] = (uint8_t)((v) >> 16); \
(p)[2] = (uint8_t)((v) >> 8); \
(p)[3] = (uint8_t)((v));
#define ISC_U8TO32_BE(p) \
(((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \
((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3])))
#define ISC_U48TO8_BE(p, v) \
(p)[0] = (uint8_t)((v) >> 40); \
(p)[1] = (uint8_t)((v) >> 32); \
(p)[2] = (uint8_t)((v) >> 24); \
(p)[3] = (uint8_t)((v) >> 16); \
(p)[4] = (uint8_t)((v) >> 8); \
(p)[5] = (uint8_t)((v));
#define ISC_U8TO48_BE(p) \
(((uint64_t)((p)[0]) << 40) | ((uint64_t)((p)[1]) << 32) | \
((uint64_t)((p)[2]) << 24) | ((uint64_t)((p)[3]) << 16) | \
((uint64_t)((p)[4]) << 8) | ((uint64_t)((p)[5])))
#define ISC_U64TO8_BE(p, v) \
(p)[0] = (uint8_t)((v) >> 56); \
(p)[1] = (uint8_t)((v) >> 48); \
(p)[2] = (uint8_t)((v) >> 40); \
(p)[3] = (uint8_t)((v) >> 32); \
(p)[4] = (uint8_t)((v) >> 24); \
(p)[5] = (uint8_t)((v) >> 16); \
(p)[6] = (uint8_t)((v) >> 8); \
(p)[7] = (uint8_t)((v));
#define ISC_U8TO64_BE(p) \
(((uint64_t)((p)[0]) << 56) | ((uint64_t)((p)[1]) << 48) | \
((uint64_t)((p)[2]) << 40) | ((uint64_t)((p)[3]) << 32) | \
((uint64_t)((p)[4]) << 24) | ((uint64_t)((p)[5]) << 16) | \
((uint64_t)((p)[6]) << 8) | ((uint64_t)((p)[7])))

View File

@@ -67,29 +67,9 @@
#define HALFSIPROUND FULL_ROUND32
#define U32TO8_LE(p, v) \
(p)[0] = (uint8_t)((v)); \
(p)[1] = (uint8_t)((v) >> 8); \
(p)[2] = (uint8_t)((v) >> 16); \
(p)[3] = (uint8_t)((v) >> 24);
#define U8TO32_LE(p) \
(((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
#define U8TO32_ONE(case_sensitive, byte) \
(uint32_t)(case_sensitive ? byte : isc__ascii_tolower1(byte))
#define U64TO8_LE(p, v) \
U32TO8_LE((p), (uint32_t)((v))); \
U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
#define U8TO64_LE(p) \
(((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
#define U8TO64_ONE(case_sensitive, byte) \
(uint64_t)(case_sensitive ? byte : isc__ascii_tolower1(byte))
@@ -99,8 +79,8 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
REQUIRE(k != NULL);
REQUIRE(out != NULL);
uint64_t k0 = U8TO64_LE(k);
uint64_t k1 = U8TO64_LE(k + 8);
uint64_t k0 = ISC_U8TO64_LE(k);
uint64_t k1 = ISC_U8TO64_LE(k + 8);
uint64_t v0 = UINT64_C(0x736f6d6570736575) ^ k0;
uint64_t v1 = UINT64_C(0x646f72616e646f6d) ^ k1;
@@ -113,8 +93,9 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
const size_t left = inlen & 7;
for (; in != end; in += 8) {
uint64_t m = case_sensitive ? U8TO64_LE(in)
: isc_ascii_tolower8(U8TO64_LE(in));
uint64_t m = case_sensitive
? ISC_U8TO64_LE(in)
: isc_ascii_tolower8(ISC_U8TO64_LE(in));
v3 ^= m;
@@ -169,7 +150,7 @@ isc_siphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
b = v0 ^ v1 ^ v2 ^ v3;
U64TO8_LE(out, b);
ISC_U64TO8_LE(out, b);
}
void
@@ -178,8 +159,8 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
REQUIRE(k != NULL);
REQUIRE(out != NULL);
uint32_t k0 = U8TO32_LE(k);
uint32_t k1 = U8TO32_LE(k + 4);
uint32_t k0 = ISC_U8TO32_LE(k);
uint32_t k1 = ISC_U8TO32_LE(k + 4);
uint32_t v0 = UINT32_C(0x00000000) ^ k0;
uint32_t v1 = UINT32_C(0x00000000) ^ k1;
@@ -192,8 +173,9 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
const int left = inlen & 3;
for (; in != end; in += 4) {
uint32_t m = case_sensitive ? U8TO32_LE(in)
: isc_ascii_tolower4(U8TO32_LE(in));
uint32_t m = case_sensitive
? ISC_U8TO32_LE(in)
: isc_ascii_tolower4(ISC_U8TO32_LE(in));
v3 ^= m;
@@ -235,5 +217,5 @@ isc_halfsiphash24(const uint8_t *k, const uint8_t *in, const size_t inlen,
}
b = v1 ^ v3;
U32TO8_LE(out, b);
ISC_U32TO8_LE(out, b);
}