Debian Codegraph @

src/fundamental/memory-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "memory-util.h"
4
5 bool memeqbyte(uint8_t byte, const void *data, size_t length) {
6 assert(data || length == 0);
7
8 /* Does the buffer consist entirely of the same specific byte value?
9 * Copied from https://github.com/systemd/casync/, copied in turn from
10 * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
11 * which is licensed CC-0.
12 */
13
14 const uint8_t *p = data;
15
16 /* Check first 16 bytes manually */
17 for (size_t i = 0; i < 16 && length > 0; i++, length--)
18 if (p[i] != byte)
19 return false;
20
21 if (length == 0)
22 return true;
23
24 /* Now we know first 16 bytes match, memcmp() with self. */
25 return memcmp(data, p + 16, length) == 0;
26 }