⬢ DragonFlyBSD Kernel Audit
DF-5701 / bcmp_fixed.c
← back to finding ↓ download raw
/* Plain fixed bcmp (fix.diff applied) for guard/trunc validation runs. */
#include <string.h>
#include <machine/endian.h>

typedef	const unsigned char	*ustring;
typedef unsigned long		ul;
typedef const unsigned long	*culp;

int
bcmp_fixed(const void *b1, const void *b2, size_t length)
{
	size_t	len = length;
	ustring	p1 = b1, p2 = b2;

	if (len == 0)
		return (0);

	while (((long)p1 | (long)p2) & (sizeof(long) - 1)) {
		if (*p1++ != *p2++)
			return (1);
		if (--len <= 0)
			return (0);
	}

	while (len >= sizeof(long)) {
		if (*(culp)p1 != *(culp)p2)
			return (1);
		p1 += sizeof(long);
		p2 += sizeof(long);
		len -= sizeof(long);
	}

	while (len--) {
		if (*p1++ != *p2++)
			return (1);
	}
	return (0);
}