aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-04 18:11:21 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-04 18:13:57 +0200
commit536a98c12f37e6b1afaad2379ada7ab3e3b9af38 (patch)
treed135d68592616dc7ea70fb143a5b8e76c751df4f
parentallowedips: use native endian on lookup (diff)
downloadWireGuard-jd/endianness-trick.tar.xz
WireGuard-jd/endianness-trick.zip
allowedips: single expression endian choicejd/endianness-trick
This will upset millions of curious onlookers, and people will begin to doubt my judgement and sanity. But this really is cleaner to express as a single statement.
-rw-r--r--src/allowedips.c5
1 files changed, 1 insertions, 4 deletions
diff --git a/src/allowedips.c b/src/allowedips.c
index c1d9bb8..0f1fed2 100644
--- a/src/allowedips.c
+++ b/src/allowedips.c
@@ -31,10 +31,7 @@ static __always_inline void swap_endian(u8 *dst, const u8 *src, u8 bits)
static void copy_and_assign_cidr(struct allowedips_node *node, const u8 *src, u8 cidr, u8 bits)
{
node->cidr = cidr;
- node->bit_at_a = cidr / 8;
-#ifdef __LITTLE_ENDIAN
- node->bit_at_a ^= (bits / 8 - 1) % 8;
-#endif
+ node->bit_at_a = (cidr / 8) ^ (((bits / 8 - 1) % 8) * *(u8 *)((u16 []){ 1 }));
node->bit_at_b = 7 - (cidr % 8);
memcpy(node->bits, src, bits / 8);
}