Motivation: allow bits=0 and consequently 0.0.0.0/0 matches in view
and renumber modules.
https://gitter.im/CZ-NIC/knot-resolver?at=
58c940c721d548df2cdfda5e
We shouldn't mix up error codes with valid results from memcmp;
let's just segfault if someone passes a NULL, just as memcmp() itself.
int kr_bitcmp(const char *a, const char *b, int bits)
{
- if (!a || !b || bits == 0) {
- return kr_error(ENOMEM);
- }
+ assert(a && b && bits >= 0 || bits == 0);
/* Compare part byte-divisible part. */
const size_t chunk = bits / 8;
int ret = memcmp(a, b, chunk);
* @warning 'dst' must be at least `sizeof(struct in6_addr)` long. */
KR_EXPORT
int kr_straddr_subnet(void *dst, const char *addr);
-/** Compare memory bitwise. */
+
+/** Compare memory bitwise. The semantics is "the same" as for memcmp().
+ * The partial byte is considered with more-significant bits first,
+ * so this is e.g. suitable for comparing IP prefixes. */
KR_EXPORT KR_PURE
int kr_bitcmp(const char *a, const char *b, int bits);