#include <OverlayKey.h>
Wraps common functions from Gnu MP library.
Public Member Functions | |
OverlayKey () | |
Default constructor. | |
OverlayKey (uint32_t num) | |
Constructs an overlay key initialized with a common integer. | |
OverlayKey (const unsigned char *buffer, uint size) | |
Constructs a key out of a buffer. | |
OverlayKey (const std::string &str, uint base=16) | |
Constructs a key out of a string number. | |
OverlayKey (const OverlayKey &rhs) | |
Copy constructor. | |
~OverlayKey () | |
Default destructor. | |
std::string | toString (uint base=16) const |
Returns a string representation of this key. | |
bool | isUnspecified () const |
Returns true, if the key is unspecified. | |
bool | operator< (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
bool | operator> (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
bool | operator<= (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
bool | operator>= (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
bool | operator== (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
bool | operator!= (const OverlayKey &compKey) const |
compares this to a given OverlayKey | |
int | compareTo (const OverlayKey &compKey) const |
Unifies all compare operations in one method. | |
OverlayKey & | operator= (const OverlayKey &rhs) |
assigns OverlayKey of rhs to this->key | |
OverlayKey & | operator-- () |
substracts 1 from this->key | |
OverlayKey & | operator++ () |
adds 1 to this->key | |
OverlayKey & | operator+= (const OverlayKey &rhs) |
adds rhs->key to this->key | |
OverlayKey & | operator-= (const OverlayKey &rhs) |
substracts rhs->key from this->key | |
OverlayKey | operator+ (const OverlayKey &rhs) const |
adds rhs->key to this->key | |
OverlayKey | operator- (const OverlayKey &rhs) const |
substracts rhs->key from this->key | |
OverlayKey | operator-- (int) |
substracts 1 from this->key | |
OverlayKey | operator++ (int) |
adds 1 to this->key | |
OverlayKey | operator>> (uint num) const |
bitwise shift right | |
OverlayKey | operator<< (uint num) const |
bitwise shift left | |
OverlayKey | operator & (const OverlayKey &rhs) const |
bitwise AND of rhs->key and this->key | |
OverlayKey | operator| (const OverlayKey &rhs) const |
bitwise OR of rhs->key and this->key | |
OverlayKey | operator^ (const OverlayKey &rhs) const |
bitwise XOR of rhs->key and this->key | |
OverlayKey | operator~ () const |
bitwise NOT of this->key | |
OverlayKeyBit | operator[] (uint n) |
returns the n-th bit of this->key | |
OverlayKey & | setBitAt (uint pos, bool value) |
sets a bit of this->key | |
uint32_t | get (uint p, uint n) const |
Returns a sub integer at position p with n-bits. | |
size_t | hash () const |
Returns a hash value for the key. | |
int | log_2 () const |
Returns the position of the msb in this key, which represents just the logarithm to base 2. | |
OverlayKey | randomSuffix (uint pos) const |
Fills the suffix starting at pos with random bits to lsb. | |
OverlayKey | randomPrefix (uint pos) const |
Fills the prefix starting at pos with random bits to msb. | |
uint | sharedPrefixLength (const OverlayKey &compKey) const |
Calculates the number of equal bits from the left with another Key (shared prefix length). | |
bool | isBetween (const OverlayKey &keyA, const OverlayKey &keyB) const |
Returns true, if this key is element of the interval (keyA, keyB) on the ring. | |
bool | isBetweenR (const OverlayKey &keyA, const OverlayKey &keyB) const |
Returns true, if this key is element of the interval (keyA, keyB] on the ring. | |
bool | isBetweenL (const OverlayKey &keyA, const OverlayKey &keyB) const |
Returns true, if this key is element of the interval [keyA, keyB) on the ring. | |
bool | isBetweenLR (const OverlayKey &keyA, const OverlayKey &keyB) const |
Returns true, if this key is element of the interval [keyA, keyB] on the ring. | |
int | bitAtPlace (int step) const __attribute__((deprecated)) |
void | netPack (cCommBuffer *b) |
serializes the object into a buffer | |
void | netUnpack (cCommBuffer *b) |
deserializes the object from a buffer | |
Static Public Member Functions | |
static void | setKeyLength (uint length) |
Set the length of an OverlayKey. | |
static uint | getLength () |
Returns the length in number of bits. | |
static OverlayKey | random () |
Returns a random key. | |
static OverlayKey | max () |
Returns the maximum key, i.e. | |
static OverlayKey | sha1 (const BinaryValue &value) |
Returns a key with the SHA1 cryptographic hash of a BinaryValue. | |
static OverlayKey | pow2 (uint exponent) |
Returns a key 2^exponent. | |
static void | test () |
A pseudo regression test method. | |
Static Public Attributes | |
static const OverlayKey | UNSPECIFIED_KEY |
OverlayKey without defined key. | |
static const OverlayKey | ZERO |
OverlayKey with key initialized as 0. | |
static const OverlayKey | ONE |
OverlayKey with key initialized as 1. | |
Private Member Functions | |
void | trim () |
trims key after key operations | |
void | clear () |
set this->key to 0 and isUnspec to false | |
Private Attributes | |
bool | isUnspec |
is this->key unspecified? | |
mp_limb_t | key [MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)] |
the overlay key this object represents | |
Static Private Attributes | |
static const uint | MAX_KEYLENGTH = 160 |
maximum length of the key | |
static uint | keyLength = MAX_KEYLENGTH |
actual length of the key | |
static uint | aSize |
number of needed machine words to hold the key | |
static mp_limb_t | GMP_MSB_MASK |
bits to fill up if key does not exactly fit in one or more machine words | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const OverlayKey &c) |
Common stdc++ console output method. |
OverlayKey::OverlayKey | ( | ) |
OverlayKey::OverlayKey | ( | uint32_t | num | ) |
OverlayKey::OverlayKey | ( | const unsigned char * | buffer, | |
uint | size | |||
) |
Constructs a key out of a buffer.
buffer | Source buffer | |
size | Buffer size (in bytes) |
00079 { 00080 int trimSize, offset; 00081 clear(); 00082 trimSize = (int)min(aSize * sizeof(mp_limb_t), size); 00083 offset = aSize * sizeof(mp_limb_t) - trimSize; 00084 memcpy( ((char*)key) + offset, buf, trimSize); 00085 trim(); 00086 }
OverlayKey::OverlayKey | ( | const std::string & | str, | |
uint | base = 16 | |||
) |
Constructs a key out of a string number.
00090 { 00091 if ((base < 2) || (base > 16)) { 00092 throw new cRuntimeError("OverlayKey::OverlayKey(): Invalid base!"); 00093 } 00094 00095 string s(str); 00096 clear(); 00097 00098 for (uint i=0; i<s.size(); i++) { 00099 if ((s[i] >= '0') && (s[i] <= '9')) { 00100 s[i] -= '0'; 00101 } else if ((s[i] >= 'a') && (s[i] <= 'f')) { 00102 s[i] -= ('a' - 10); 00103 } else if ((s[i] >= 'A') & (s[i] <= 'F')) { 00104 s[i] -= ('A' - 10); 00105 } else { 00106 throw new cRuntimeError("OverlayKey::OverlayKey(): " 00107 "Invalid charakter in string!"); 00108 } 00109 } 00110 00111 mpn_set_str ((mp_limb_t*)this->key, (const unsigned char*)s.c_str(), 00112 str.size(), base); 00113 trim(); 00114 }
OverlayKey::OverlayKey | ( | const OverlayKey & | rhs | ) |
std::string OverlayKey::toString | ( | uint | base = 16 |
) | const |
Returns a string representation of this key.
00161 { 00162 if ((base < 2) || (base > 16)) { 00163 throw new cRuntimeError("OverlayKey::OverlayKey(): Invalid base!"); 00164 } 00165 00166 if (isUnspec) 00167 return std::string("<unspec>"); 00168 else { 00169 char temp[256]; 00170 #if 0 00171 if (base==16) { 00172 int k=0; 00173 for (int i=(keyLength-1)/4; i>=0; i--, k++) 00174 temp[k] = HEX[this->get 00175 (4*i,4)]; 00176 00177 temp[k] = 0; 00178 return std::string((const char*)temp); 00179 } else if (base==2) { 00180 int k=0; 00181 for (int i=keyLength-1; i>=0; i-=1, k++) 00182 temp[k] = HEX[this->get 00183 (i,1)]; 00184 temp[k] = 0; 00185 return std::string((const char*)temp); 00186 } 00187 #endif 00188 mp_size_t last = mpn_get_str((unsigned char*)temp, base, 00189 (mp_limb_t*)this->key, aSize); 00190 for (int i=0; i<last; i++) { 00191 temp[i] = HEX[temp[i]]; 00192 } 00193 temp[last] = 0; 00194 return std::string((const char*)temp); 00195 } 00196 }
bool OverlayKey::isUnspecified | ( | ) | const |
Returns true, if the key is unspecified.
00156 { 00157 return isUnspec; 00158 }
bool OverlayKey::operator< | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00274 { 00275 return compareTo(compKey) < 0; 00276 }
bool OverlayKey::operator> | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00278 { 00279 return compareTo(compKey) > 0; 00280 }
bool OverlayKey::operator<= | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00282 { 00283 return compareTo(compKey) <=0; 00284 }
bool OverlayKey::operator>= | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00286 { 00287 return compareTo(compKey) >=0; 00288 }
bool OverlayKey::operator== | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00290 { 00291 return compareTo(compKey) ==0; 00292 }
bool OverlayKey::operator!= | ( | const OverlayKey & | compKey | ) | const |
compares this to a given OverlayKey
compKey | the the OverlayKey to compare this to |
00294 { 00295 return compareTo(compKey) !=0; 00296 }
int OverlayKey::compareTo | ( | const OverlayKey & | compKey | ) | const |
Unifies all compare operations in one method.
compKey | key to compare with |
00766 { 00767 if (compKey.isUnspec || isUnspec) 00768 opp_error("OverlayKey::compareTo(): key is unspecified!"); 00769 return mpn_cmp(key,compKey.key,aSize); 00770 }
OverlayKey & OverlayKey::operator= | ( | const OverlayKey & | rhs | ) |
assigns OverlayKey of rhs to this->key
rhs | the OverlayKey with the defined key |
00204 { 00205 isUnspec = rhs.isUnspec; 00206 memcpy( key, rhs.key, aSize*sizeof(mp_limb_t) ); 00207 return *this; 00208 }
OverlayKey & OverlayKey::operator-- | ( | ) |
substracts 1 from this->key
00212 { 00213 return (*this -= ONE); 00214 }
OverlayKey & OverlayKey::operator++ | ( | ) |
OverlayKey & OverlayKey::operator+= | ( | const OverlayKey & | rhs | ) |
adds rhs->key to this->key
rhs | the OverlayKey with the defined key |
00240 { 00241 mpn_add_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize); 00242 trim(); 00243 isUnspec = false; 00244 return *this; 00245 }
OverlayKey & OverlayKey::operator-= | ( | const OverlayKey & | rhs | ) |
substracts rhs->key from this->key
rhs | the OverlayKey with the defined key |
00249 { 00250 mpn_sub_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize); 00251 trim(); 00252 isUnspec = false; 00253 return *this; 00254 }
OverlayKey OverlayKey::operator+ | ( | const OverlayKey & | rhs | ) | const |
adds rhs->key to this->key
rhs | the OverlayKey with the defined key |
00258 { 00259 OverlayKey result = *this; 00260 result += rhs; 00261 return result; 00262 }
OverlayKey OverlayKey::operator- | ( | const OverlayKey & | rhs | ) | const |
substracts rhs->key from this->key
rhs | the OverlayKey with the defined key |
00266 { 00267 OverlayKey result = *this; 00268 result -= rhs; 00269 return result; 00270 }
OverlayKey OverlayKey::operator-- | ( | int | ) |
substracts 1 from this->key
00218 { 00219 OverlayKey clone = *this; 00220 *this -= ONE; 00221 return clone; 00222 }
OverlayKey OverlayKey::operator++ | ( | int | ) |
adds 1 to this->key
00232 { 00233 OverlayKey clone = *this; 00234 *this += ONE; 00235 return clone; 00236 }
OverlayKey OverlayKey::operator>> | ( | uint | num | ) | const |
bitwise shift right
num | number of bits to shift |
00345 { 00346 OverlayKey result = ZERO; 00347 int i = num/GMP_LIMB_BITS; 00348 00349 num %= GMP_LIMB_BITS; 00350 00351 if (i>=(int)aSize) 00352 return result; 00353 00354 for (int j=0; j<(int)aSize-i; j++) { 00355 result.key[j] = key[j+i]; 00356 } 00357 mpn_rshift(result.key,result.key,aSize,num); 00358 result.isUnspec = false; 00359 result.trim(); 00360 00361 return result; 00362 }
OverlayKey OverlayKey::operator<< | ( | uint | num | ) | const |
bitwise shift left
num | number of bits to shift |
00366 { 00367 OverlayKey result = ZERO; 00368 int i = num/GMP_LIMB_BITS; 00369 00370 num %= GMP_LIMB_BITS; 00371 00372 if (i>=(int)aSize) 00373 return result; 00374 00375 for (int j=0; j<(int)aSize-i; j++) { 00376 result.key[j+i] = key[j]; 00377 } 00378 mpn_lshift(result.key,result.key,aSize,num); 00379 result.isUnspec = false; 00380 result.trim(); 00381 00382 return result; 00383 }
OverlayKey OverlayKey::operator & | ( | const OverlayKey & | rhs | ) | const |
bitwise AND of rhs->key and this->key
rhs | the OverlayKey AND is calculated with |
00322 { 00323 OverlayKey result = *this; 00324 for (uint i=0; i<aSize; i++) { 00325 result.key[i] &= rhs.key[i]; 00326 } 00327 00328 return result; 00329 }
OverlayKey OverlayKey::operator| | ( | const OverlayKey & | rhs | ) | const |
bitwise OR of rhs->key and this->key
rhs | the OverlayKey OR is calculated with |
00311 { 00312 OverlayKey result = *this; 00313 for (uint i=0; i<aSize; i++) { 00314 result.key[i] |= rhs.key[i]; 00315 } 00316 00317 return result; 00318 }
OverlayKey OverlayKey::operator^ | ( | const OverlayKey & | rhs | ) | const |
bitwise XOR of rhs->key and this->key
rhs | the OverlayKey XOR is calculated with |
00300 { 00301 OverlayKey result = *this; 00302 for (uint i=0; i<aSize; i++) { 00303 result.key[i] ^= rhs.key[i]; 00304 } 00305 00306 return result; 00307 }
OverlayKey OverlayKey::operator~ | ( | ) | const |
bitwise NOT of this->key
00333 { 00334 OverlayKey result = *this; 00335 for (uint i=0; i<aSize; i++) { 00336 result.key[i] = ~key[i]; 00337 } 00338 result.trim(); 00339 00340 return result; 00341 }
OverlayKeyBit OverlayKey::operator[] | ( | uint | n | ) |
returns the n-th bit of this->key
n | the position of the returned bit |
00387 { 00388 return OverlayKeyBit(get(n, 1), n, this); 00389 }
OverlayKey & OverlayKey::setBitAt | ( | uint | pos, | |
bool | value | |||
) |
sets a bit of this->key
pos | the position of the bit to set | |
value | new value for bit at position pos |
00392 { 00393 mp_limb_t digit = 1; 00394 digit = digit << (pos % GMP_LIMB_BITS); 00395 00396 if (value) { 00397 key[pos / GMP_LIMB_BITS] |= digit; 00398 } else { 00399 //key[pos / GMP_LIMB_BITS] = key[pos / GMP_LIMB_BITS] & ~digit; 00400 key[pos / GMP_LIMB_BITS] &= ~digit; 00401 } 00402 00403 return *this; 00404 };
uint32_t OverlayKey::get | ( | uint | p, | |
uint | n | |||
) | const |
Returns a sub integer at position p with n-bits.
p is counted starting from the least significant bit of the key as bit 0. Bit p of the key becomes bit 0 of the returned integer.
p | the position of the sub-integer | |
n | the number of bits to be returned (max.32) |
00412 { 00413 int i = p / GMP_LIMB_BITS, // index of starting bit 00414 f = p % GMP_LIMB_BITS, // position of starting bit 00415 f2 = f + n - GMP_LIMB_BITS; // how many bits to take from next index 00416 00417 if (p + n > OverlayKey::keyLength) { 00418 opp_error("OverlayKey::get: Invalid range (index too large!)"); 00419 } 00420 00421 return ((key[i] >> f) | // get the bits of key[i] 00422 (f2 > 0 ? (key[i+1] << (GMP_LIMB_BITS - f)) : 0)) & // the extra bits from key[i+1] 00423 (((uint32_t)(~0)) >> (GMP_LIMB_BITS - n)); // delete unused bits 00424 }
size_t OverlayKey::hash | ( | ) | const |
Returns a hash value for the key.
00521 { 00522 return (size_t)key[0]; 00523 }
int OverlayKey::log_2 | ( | ) | const |
Returns the position of the msb in this key, which represents just the logarithm to base 2.
00498 { 00499 int16_t i = aSize-1; 00500 00501 while (i>=0 && key[i]==0) { 00502 i--; 00503 } 00504 00505 if (i<0) { 00506 return -1; 00507 } 00508 00509 mp_limb_t j = key[i]; 00510 i *= GMP_LIMB_BITS; 00511 while (j!=0) { 00512 j >>= 1; 00513 i++; 00514 } 00515 00516 return i-1; 00517 }
OverlayKey OverlayKey::randomSuffix | ( | uint | pos | ) | const |
Fills the suffix starting at pos with random bits to lsb.
pos |
00428 { 00429 OverlayKey newKey = *this; 00430 int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS; 00431 mp_limb_t m = ((mp_limb_t)1 << j)-1; 00432 mp_limb_t rnd; 00433 00434 // mpn_random(&rnd,1); 00435 omnet_random(&rnd,1); 00436 newKey.key[i] &= ~m; 00437 newKey.key[i] |= (rnd&m); 00438 // mpn_random(newKey.key,i); 00439 omnet_random(newKey.key,i); 00440 newKey.trim(); 00441 00442 return newKey; 00443 }
OverlayKey OverlayKey::randomPrefix | ( | uint | pos | ) | const |
Fills the prefix starting at pos with random bits to msb.
pos |
00447 { 00448 OverlayKey newKey = *this; 00449 int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS; 00450 mp_limb_t m = ((mp_limb_t)1 << j)-1; 00451 mp_limb_t rnd; 00452 00453 // mpn_random(&rnd,1); 00454 omnet_random(&rnd,1); 00455 00456 newKey.key[i] &= m; 00457 newKey.key[i] |= (rnd&~m); 00458 for (int k=aSize-1; k!=i; k--) { 00459 // mpn_random( &newKey.key[k], 1 ); 00460 omnet_random( &newKey.key[k], 1 ); 00461 } 00462 newKey.trim(); 00463 00464 return newKey; 00465 }
uint OverlayKey::sharedPrefixLength | ( | const OverlayKey & | compKey | ) | const |
Calculates the number of equal bits from the left with another Key (shared prefix length).
compKey | the Key to compare with |
00469 { 00470 if (compareTo(compKey) == 0) return keyLength; 00471 00472 uint length = 0; 00473 int i; 00474 uint j; 00475 bool msb = true; 00476 00477 // count equal limbs first: 00478 for (i=aSize-1; i>=0; --i) 00479 { 00480 if (this->key[i] != compKey.key[i]) 00481 { 00482 // XOR first differing limb for easy counting of the bits: 00483 mp_limb_t d = this->key[i] ^ compKey.key[i]; 00484 if (msb) d <<= ( GMP_LIMB_BITS - (keyLength % GMP_LIMB_BITS) ); 00485 for (j = GMP_LIMB_BITS-1; d >>= 1; --j); 00486 length += j; 00487 break; 00488 } 00489 length += GMP_LIMB_BITS; 00490 msb = false; 00491 } 00492 00493 return length; 00494 }
bool OverlayKey::isBetween | ( | const OverlayKey & | keyA, | |
const OverlayKey & | keyB | |||
) | const |
Returns true, if this key is element of the interval (keyA, keyB) on the ring.
keyA | The left border of the interval | |
keyB | The right border of the interval |
00528 { 00529 if (isUnspec || keyA.isUnspec || keyB.isUnspec) 00530 return false; 00531 00532 if (*this == keyA) 00533 return false; 00534 else if (keyA < keyB) 00535 return ((*this > keyA) && (*this < keyB)); 00536 else 00537 return ((*this > keyA) || (*this < keyB)); 00538 }
bool OverlayKey::isBetweenR | ( | const OverlayKey & | keyA, | |
const OverlayKey & | keyB | |||
) | const |
Returns true, if this key is element of the interval (keyA, keyB] on the ring.
keyA | The left border of the interval | |
keyB | The right border of the interval |
00543 { 00544 if (isUnspec || keyA.isUnspec || keyB.isUnspec) 00545 return false; 00546 00547 if ((keyA == keyB) && (*this == keyA)) 00548 return true; 00549 else if (keyA <= keyB) 00550 return ((*this > keyA) && (*this <= keyB)); 00551 else 00552 return ((*this > keyA) || (*this <= keyB)); 00553 }
bool OverlayKey::isBetweenL | ( | const OverlayKey & | keyA, | |
const OverlayKey & | keyB | |||
) | const |
Returns true, if this key is element of the interval [keyA, keyB) on the ring.
keyA | The left border of the interval | |
keyB | The right border of the interval |
00558 { 00559 if (isUnspec || keyA.isUnspec || keyB.isUnspec) 00560 return false; 00561 00562 if ((keyA == keyB) && (*this == keyA)) 00563 return true; 00564 else if (keyA <= keyB) 00565 return ((*this >= keyA) && (*this < keyB)); 00566 else 00567 return ((*this >= keyA) || (*this < keyB)); 00568 }
bool OverlayKey::isBetweenLR | ( | const OverlayKey & | keyA, | |
const OverlayKey & | keyB | |||
) | const |
Returns true, if this key is element of the interval [keyA, keyB] on the ring.
keyA | The left border of the interval | |
keyB | The right border of the interval |
00573 { 00574 if (isUnspec || keyA.isUnspec || keyB.isUnspec) 00575 return false; 00576 00577 if ((keyA == keyB) && (*this == keyA)) 00578 return true; 00579 else if (keyA <= keyB) 00580 return ((*this >= keyA) && (*this <= keyB)); 00581 else 00582 return ((*this >= keyA) || (*this <= keyB)); 00583 }
void OverlayKey::setKeyLength | ( | uint | length | ) | [static] |
Set the length of an OverlayKey.
length | keylength in bits |
00131 { 00132 if ((length < 1) || (length > OverlayKey::keyLength)) { 00133 opp_error("OverlayKey::setKeyLength(): length must be <= %i " 00134 "and setKeyLength() must not be called twice " 00135 "with different length!", MAX_KEYLENGTH); 00136 } 00137 00138 keyLength = length; 00139 00140 aSize = keyLength / (8*sizeof(mp_limb_t)) + 00141 (keyLength % (8*sizeof(mp_limb_t))!=0 ? 1 : 0); 00142 00143 GMP_MSB_MASK = (keyLength % GMP_LIMB_BITS) 00144 != 0 ? (((mp_limb_t)1 << (keyLength % GMP_LIMB_BITS))-1) 00145 : (mp_limb_t)-1; 00146 }
uint OverlayKey::getLength | ( | ) | [static] |
Returns the length in number of bits.
00151 { 00152 return OverlayKey::keyLength; 00153 }
OverlayKey OverlayKey::random | ( | ) | [static] |
Returns a random key.
00613 { 00614 OverlayKey newKey = ZERO; 00615 00616 // mpn_random(newKey.key,aSize); 00617 omnet_random(newKey.key,aSize); 00618 00619 newKey.trim(); 00620 00621 return newKey; 00622 }
OverlayKey OverlayKey::max | ( | ) | [static] |
Returns the maximum key, i.e.
a key filled with bit 1
00599 { 00600 OverlayKey newKey; 00601 00602 for (uint i=0; i<aSize; i++) { 00603 newKey.key[i] = ~0; 00604 } 00605 newKey.isUnspec = false; 00606 newKey.trim(); 00607 00608 return newKey; 00609 }
OverlayKey OverlayKey::sha1 | ( | const BinaryValue & | value | ) | [static] |
Returns a key with the SHA1 cryptographic hash of a BinaryValue.
value | A BinaryValue object. |
00626 { 00627 OverlayKey newKey = OverlayKey(); 00628 uint8_t temp[20]; 00629 CSHA1 sha1; 00630 00631 sha1.Reset(); 00632 sha1.Update((uint8_t*)(&(*input.begin())), input.size()); 00633 sha1.Final(); 00634 sha1.GetHash(temp); 00635 mpn_set_str(newKey.key, (const uint8_t*)temp, 00636 (int)min(aSize * sizeof(mp_limb_t), 20), 256); 00637 newKey.isUnspec = false; 00638 newKey.trim(); 00639 00640 return newKey; 00641 }
OverlayKey OverlayKey::pow2 | ( | uint | exponent | ) | [static] |
Returns a key 2^exponent.
exponent | The exponent. |
00645 { 00646 OverlayKey newKey = ZERO; 00647 00648 newKey.key[exponent/GMP_LIMB_BITS] = 00649 (mp_limb_t)1 << (exponent % GMP_LIMB_BITS); 00650 00651 return newKey; 00652 }
void OverlayKey::test | ( | ) | [static] |
A pseudo regression test method.
Outputs report to standard output.
00656 { 00657 // add test 00658 cout << endl << "--- Add test ..." << endl; 00659 OverlayKey key = 123456789; 00660 cout << " key=" << key << endl; 00661 cout << " key += 987654321 = " << (key+=987654321) << endl; 00662 cout << " prefix++ : " << (++key) << endl; 00663 cout << " postfix++ : " << (key++) << endl; 00664 cout << " key=" << key << endl; 00665 00666 OverlayKey k1 = 256, k2 = 10, k3 = 3; 00667 00668 // compare test 00669 cout << endl << "--- Compare test ..." << endl; 00670 cout << " 256 < 10 = "<< (k1 < k2) << " k1="<<k1<<endl; 00671 cout << " 256 > 10 = "<< (k1 > k2) << " k2="<<k2<<endl; 00672 00673 cout << " 10 isBetween(3, 256)=" << k2.isBetween(k3, k1) << endl; 00674 cout << " 3 isBetween(10, 256)=" << k3.isBetween(k2, k1) << endl; 00675 cout << " 256 isBetween(10, 256)=" << k1.isBetween(k2, k1) << endl; 00676 cout << " 256 isBetweenR(10, 256)=" << k1.isBetweenR(k2, k1) << endl; 00677 cout << " max isBetween(max-1,0)=" << OverlayKey::max().isBetween( 00678 OverlayKey::max()-1, OverlayKey::ZERO) << endl; 00679 cout << " max-1 isBetween(max,1)=" << (OverlayKey::max()-1).isBetween( 00680 OverlayKey::max(), OverlayKey::ONE) << endl; 00681 cout << " max-1 isBetweenL(max-1,1)=" << (OverlayKey::max()-1). 00682 isBetweenL(OverlayKey::max()-1, OverlayKey::ONE) << endl; 00683 cout << " 1 isBetweenL(max-1,1)=" << (OverlayKey::ONE).isBetweenL( 00684 OverlayKey::max()-1, OverlayKey::ONE) << endl; 00685 cout << " 1 isBetweenR(max-1,1)=" << OverlayKey::ONE.isBetweenR( 00686 OverlayKey::max()-1, OverlayKey::ONE) << endl; 00687 cout << " 1 isBetween(max-1,1)=" << OverlayKey::ONE.isBetween( 00688 OverlayKey::max()-1, OverlayKey::ONE) << endl; 00689 cout << " 1 isBetween(max-1,0)=" << OverlayKey::ONE.isBetween( 00690 OverlayKey::max()-1, OverlayKey::ZERO) << endl; 00691 cout << " 256 sharedPrefixLength(3)=" << k1.sharedPrefixLength(k3) 00692 << endl; 00693 cout << " 256 sharedPrefixLength(256)=" << k1.sharedPrefixLength(k1) 00694 << endl; 00695 00696 // wrap around test 00697 cout << endl << "--- Warp around test ..." << endl; 00698 00699 k1 = OverlayKey::max(); 00700 cout << "k1=max= " << k1.toString(16) << endl; 00701 cout << "k1+1 = " << (k1 + 1).toString(16) << endl; 00702 cout << "k1+2 = " << (k1 + 2).toString(16) << endl; 00703 00704 k1 = OverlayKey::ZERO; 00705 cout << "k1=0= " << k1.toString(16) << endl; 00706 cout << "k1-1 = " << (k1 - 1).toString(16) << endl; 00707 cout << "k1-2 = " << (k1 - 2).toString(16) << endl; 00708 00709 cout << "max > ONE=" << (OverlayKey::max() > OverlayKey::ONE) << endl; 00710 cout << "max < ONE=" << (OverlayKey::max() < OverlayKey::ONE) << endl; 00711 00712 00713 // suffix and log2 test 00714 cout << endl << "--- RandomSuffix and log2 test ..." << endl; 00715 k1 = OverlayKey::ZERO; 00716 for (uint i=0; i<k1.getLength(); i++) { 00717 k2=k1.randomSuffix(i); 00718 cout << " " << k2.toString(16) << " log2=" << k2.log_2() << endl; 00719 } 00720 cout << endl << "--- RandomPrefix and log2 test ..." << endl; 00721 k1 = OverlayKey::max(); 00722 for (uint i=0; i<k1.getLength(); i++) { 00723 k2=k1.randomPrefix(i); 00724 cout << " " << k2.toString(16) << " log2=" << k2.log_2() << endl; 00725 } 00726 00727 cout << endl << "--- pow2 test..." << endl; 00728 for (uint i=0; i<k1.getLength(); i++) { 00729 k2=pow2(i); 00730 cout << " 2^" << i << " = " << k2.toString(16) << " log2=" 00731 << k2.log_2() << endl; 00732 } 00733 00734 cout << endl << "--- Bits test ..." << endl << " "; 00735 const char* BITS[] = { "000","001","010","011","100","101","110","111" 00736 }; 00737 k1 = OverlayKey::random(); 00738 for (int i=k1.getLength()-1; i>=0; i--) 00739 cout << k1[i]; 00740 cout << " = " << endl << " "; 00741 for (int i=k1.getLength()-3; i>=0; i-=3) 00742 cout << BITS[k1.get(i,3)]; 00743 cout << endl; 00744 00745 cout << endl << "--- SHA1 test ... (verified with test vectors)" << endl; 00746 cout << " Empty string: " << OverlayKey::sha1("").toString(16) 00747 << " = da39a3ee5e6b4b0d3255bfef95601890afd80709" << endl; 00748 cout << " 'Hello World' string: " 00749 << OverlayKey::sha1("Hello World").toString(16) 00750 << " = 0a4d55a8d778e5022fab701977c5d840bbc486d0" << endl; 00751 }
int OverlayKey::bitAtPlace | ( | int | step | ) | const |
00783 { 00784 /* if ((unsigned int)step > keyLength) 00785 { 00786 //cout << "Bit not in Key!" << endl; 00787 return 0; 00788 } 00789 return ((*this).get((*this).getLength() - step, 1));*/ 00790 00791 return get(getLength() - step, 1); // range test done in get() 00792 00793 }
void OverlayKey::trim | ( | ) | [inline, private] |
void OverlayKey::clear | ( | ) | [inline, private] |
void OverlayKey::netPack | ( | cCommBuffer * | b | ) |
serializes the object into a buffer
b | the buffer |
00856 { 00857 doPacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) + 00858 (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0)); 00859 doPacking(b,this->isUnspec); 00860 }
void OverlayKey::netUnpack | ( | cCommBuffer * | b | ) |
deserializes the object from a buffer
b | the buffer |
00863 { 00864 doUnpacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) + 00865 (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0)); 00866 doUnpacking(b,this->isUnspec); 00867 00868 }
std::ostream& operator<< | ( | std::ostream & | os, | |
const OverlayKey & | c | |||
) | [friend] |
const OverlayKey OverlayKey::UNSPECIFIED_KEY [static] |
OverlayKey without defined key.
const OverlayKey OverlayKey::ZERO [static] |
OverlayKey with key initialized as 0.
const OverlayKey OverlayKey::ONE [static] |
OverlayKey with key initialized as 1.
const uint OverlayKey::MAX_KEYLENGTH = 160 [static, private] |
maximum length of the key
uint OverlayKey::keyLength = MAX_KEYLENGTH [static, private] |
actual length of the key
uint OverlayKey::aSize [static, private] |
Initial value:
OverlayKey::keyLength / (8*sizeof(mp_limb_t)) + (OverlayKey::keyLength % (8*sizeof(mp_limb_t)) != 0 ? 1 : 0)
mp_limb_t OverlayKey::GMP_MSB_MASK [static, private] |
Initial value:
(OverlayKey::keyLength % GMP_LIMB_BITS) != 0 ? (((mp_limb_t)1 << (OverlayKey::keyLength % GMP_LIMB_BITS))-1) : (mp_limb_t) - 1
bool OverlayKey::isUnspec [private] |
is this->key unspecified?
mp_limb_t OverlayKey::key[MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)] [private] |
the overlay key this object represents