safe.c revision 0d63efe476bd07c8ea9a98dc8c9da7b4b114b701
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews/*
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews *
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * purpose with or without fee is hereby granted, provided that the above
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * copyright notice and this permission notice appear in all copies.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews *
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews * PERFORMANCE OF THIS SOFTWARE.
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews */
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews/*! \file */
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews#include <config.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <isc/safe.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#include <isc/util.h>
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#ifdef _MSC_VER
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#pragma optimize("", off)
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrews#endif
8b61d2012063306528286680bd9f086fa868d86eMark Andrews
34ee961fa2f0f5f2ee3cff40fdb4d7d7b48b7728Mark Andrewsisc_boolean_t
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellingtonisc_safe_memequal(const void *s1, const void *s2, size_t n) {
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington isc_uint8_t acc = 0;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence if (n != 0U) {
19d1b1667d073850d4366352aaf8319efc5debeeBrian Wellington const isc_uint8_t *p1 = s1, *p2 = s2;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews do {
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews acc |= *p1++ ^ *p2++;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews } while (--n != 0U);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews }
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrews return (ISC_TF(acc == 0));
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrewsint
78da321b437bbb690ef570ccf17dcc8583a5a4a0Mark Andrewsisc_safe_memcompare(const void *b1, const void *b2, size_t len) {
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews const unsigned char *p1 = b1, *p2 = b2;
440164d3e36353a4b9801fcc05fe66b6cf1fb8ceMark Andrews size_t i;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews int res = 0, done = 0;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff for (i = 0; i < len; i++) {
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* lt is -1 if p1[i] < p2[i]; else 0. */
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews int lt = (p1[i] - p2[i]) >> CHAR_BIT;
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* gt is -1 if p1[i] > p2[i]; else 0. */
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff int gt = (p2[i] - p1[i]) >> CHAR_BIT;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
46993e1d9d18410a5852b7d990338b70b158855cMichael Graff int cmp = lt - gt;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* set res = cmp if !done. */
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff res |= cmp & ~done;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* set done if p1[i] != p2[i]. */
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff done |= lt | gt;
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff }
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews return (res);
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews}
b54630c4518a1a173fee3478f4bf51dff450b6dcMark Andrews