summaryrefslogtreecommitdiff
path: root/lib/libutils/ext/buf_compare_ct.c
blob: 3394a9aa00108e89465f2721bb738713df20b803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 */
#include <string_ext.h>

int buf_compare_ct(const void *s1, const void *s2, size_t n)
{
	int res = 0;
	unsigned char *c1 = (unsigned char *)s1;
	unsigned char *c2 = (unsigned char *)s2;

	while (n--) {
		res |= (*c1 ^ *c2);
		c1++;
		c2++;
	}

	res |= res >> 4;
	res |= res >> 2;
	res |= res >> 1;
	res &= 1;

	return res;
}