diff options
Diffstat (limited to 'lib/isc/safe.c')
-rw-r--r-- | lib/isc/safe.c | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/lib/isc/safe.c b/lib/isc/safe.c index fd2768718872..8ac4a5b9193f 100644 --- a/lib/isc/safe.c +++ b/lib/isc/safe.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 2013, 2015 Internet Systems Consortium, Inc. ("ISC") * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,8 +14,6 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id$ */ - /*! \file */ #include <config.h> @@ -28,7 +26,7 @@ #endif isc_boolean_t -isc_safe_memcmp(const void *s1, const void *s2, size_t n) { +isc_safe_memequal(const void *s1, const void *s2, size_t n) { isc_uint8_t acc = 0; if (n != 0U) { @@ -40,3 +38,30 @@ isc_safe_memcmp(const void *s1, const void *s2, size_t n) { } return (ISC_TF(acc == 0)); } + + +int +isc_safe_memcompare(const void *b1, const void *b2, size_t len) { + const unsigned char *p1 = b1, *p2 = b2; + size_t i; + int res = 0, done = 0; + + for (i = 0; i < len; i++) { + /* lt is -1 if p1[i] < p2[i]; else 0. */ + int lt = (p1[i] - p2[i]) >> CHAR_BIT; + + /* gt is -1 if p1[i] > p2[i]; else 0. */ + int gt = (p2[i] - p1[i]) >> CHAR_BIT; + + /* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */ + int cmp = lt - gt; + + /* set res = cmp if !done. */ + res |= cmp & ~done; + + /* set done if p1[i] != p2[i]. */ + done |= lt | gt; + } + + return (res); +} |