>From 1a75cbbb4db437f35cdf67a60f90fd39692df0de Mon Sep 17 00:00:00 2001 From: Ben Sartor Date: Thu, 22 Jan 2015 18:59:55 +0100 Subject: [PATCH 11/15] selectCommonAlgo now prevents adding the same element twice --- src/cryptoUtils.c | 29 +++++++++++++++++++++-------- test/bzrtpCryptoTest.c | 8 ++++---- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/cryptoUtils.c b/src/cryptoUtils.c index b4f3988..19d06a3 100644 --- a/src/cryptoUtils.c +++ b/src/cryptoUtils.c @@ -424,6 +424,13 @@ int updateCryptoFunctionPointers(bzrtpChannelContext_t *zrtpChannelContext) { return 0; } +#define BITS_PRO_INT 8*sizeof(int) +#define BITMASK_256_SIZE 256/BITS_PRO_INT +#define BITMASK_256_SET_ZERO(bitmask) memset(bitmask, 0, sizeof(int)*BITMASK_256_SIZE) +#define BITMASK_256_SET(bitmask, value) bitmask[value/BITS_PRO_INT] |= 1 << (value % BITS_PRO_INT) +#define BITMASK_256_UNSET(bitmask, value) bitmask[value/BITS_PRO_INT] &= ~(1 << (value % BITS_PRO_INT)) +#define BITMASK_256_CHECK(bitmask, value) (bitmask[value/BITS_PRO_INT] & 1 << (value % BITS_PRO_INT)) + /** * @brief Select common algorithm from the given array where algo are represented by their 4 chars string defined in rfc section 5.1.2 to 5.1.6 * Master array is the one given the preference order @@ -438,17 +445,23 @@ int updateCryptoFunctionPointers(bzrtpChannelContext_t *zrtpChannelContext) { * @return the number of common algorithms found */ uint8_t selectCommonAlgo(uint8_t masterArray[7], uint8_t masterArrayLength, uint8_t slaveArray[7], uint8_t slaveArrayLength, uint8_t commonArray[7]) { - int i,j; + int i; uint8_t commonLength = 0; + int algosBitmap[BITMASK_256_SIZE]; + + BITMASK_256_SET_ZERO(algosBitmap); + for (i=0; i