OverlayKey Class Reference

#include <OverlayKey.h>

List of all members.


Detailed Description

A common overlay key class.

Wraps common functions from Gnu MP library.

Author:
Sebastian Mies.


Public Member Functions

 OverlayKey ()
 Default constructor.
 OverlayKey (uint32_t num)
 Constructs an overlay key initialized with a common integer.
 OverlayKey (const std::string &str, uint base=16)
 Constructs a key out of a string number.
 OverlayKey (const OverlayKey &rhs)
 Copy contructor.
 ~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.
OverlayKeyoperator= (const OverlayKey &rhs)
 assigns OverlayKey of rhs to this->key
OverlayKeyoperator-- ()
 substracts 1 from this->key
OverlayKeyoperator++ ()
 adds 1 to this->key
OverlayKeyoperator+= (const OverlayKey &rhs)
 adds rhs->key to this->key
OverlayKeyoperator-= (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
bool operator[] (uint n) const
 returns the n-th 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
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
 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.


Constructor & Destructor Documentation

OverlayKey::OverlayKey (  ) 

Default constructor.

Contructs an unspecified overlay key

00060 {
00061     isUnspec = true;
00062 }

OverlayKey::OverlayKey ( uint32_t  num  ) 

Constructs an overlay key initialized with a common integer.

Parameters:
num The integer to initialize this key with
00066 {
00067     clear();
00068     key[0] = num;
00069     trim();
00070 }

OverlayKey::OverlayKey ( const std::string &  str,
uint  base = 16 
)

Constructs a key out of a string number.

00074 {
00075     throw("Not implemented yet!");
00076     mpn_set_str ((mp_limb_t*)this->key, (const unsigned char*)str.c_str(),
00077                  str.size(), base);
00078 }

OverlayKey::OverlayKey ( const OverlayKey rhs  ) 

Copy contructor.

Parameters:
rhs The key to copy.
00082 {
00083     (*this) = rhs;
00084 }

OverlayKey::~OverlayKey (  ) 

Default destructor.

Does nothing ATM.

00088 {}


Member Function Documentation

std::string OverlayKey::toString ( uint  base = 16  )  const

Returns a string representation of this key.

Returns:
String representation of this key
00125 {
00126     if (isUnspec)
00127         return std::string("<unspec>");
00128     else {
00129         char temp[256];
00130 
00131         if (base==16) {
00132             int k=0;
00133             for (int i=(keyLength-1)/4; i>=0; i--, k++)
00134                 temp[k] = HEX[this->get
00135                               (4*i,4)];
00136 
00137             temp[k] = 0;
00138             return std::string((const char*)temp);
00139         } else if (base==2) {
00140             int k=0;
00141             for (int i=keyLength-1; i>=0; i-=1, k++)
00142                 temp[k] = HEX[this->get
00143                               (i,1)];
00144             temp[k] = 0;
00145             return std::string((const char*)temp);
00146         }
00147 
00148         mp_size_t last = mpn_get_str((unsigned char*)temp, base,
00149                                      (mp_limb_t*)this->key, aSize);
00150         for (int i=0; i<last; i++) {
00151             temp[i] = HEX[temp[i]];
00152         }
00153         temp[last] = 0;
00154         return std::string((const char*)temp);
00155     }
00156 }

bool OverlayKey::isUnspecified (  )  const

Returns true, if the key is unspecified.

Returns:
Returns true, if key is unspecified
00120 {
00121     return isUnspec;
00122 }

bool OverlayKey::operator< ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is smaller than this->key, else false
00234 {
00235     return compareTo(compKey) < 0;
00236 }

bool OverlayKey::operator> ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is greater than this->key, else false
00238 {
00239     return compareTo(compKey) > 0;
00240 }

bool OverlayKey::operator<= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is smaller than or equal to this->key, else false
00242 {
00243     return compareTo(compKey) <=0;
00244 }

bool OverlayKey::operator>= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is greater than or equal to this->key, else false
00246 {
00247     return compareTo(compKey) >=0;
00248 }

bool OverlayKey::operator== ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is equal to this->key, else false
00250 {
00251     return compareTo(compKey) ==0;
00252 }

bool OverlayKey::operator!= ( const OverlayKey compKey  )  const

compares this to a given OverlayKey

Parameters:
compKey the the OverlayKey to compare this to
Returns:
true if compKey->key is not equal to this->key, else false
00254 {
00255     return compareTo(compKey) !=0;
00256 }

int OverlayKey::compareTo ( const OverlayKey compKey  )  const

Unifies all compare operations in one method.

Parameters:
compKey key to compare with
Returns:
int -1 if smaller, 0 if equal, 1 if greater
00708 {
00709     if (compKey.isUnspec || isUnspec)
00710         opp_error("OverlayKey::compareTo(): key is unspecified!");
00711     return mpn_cmp(key,compKey.key,aSize);
00712 }

OverlayKey & OverlayKey::operator= ( const OverlayKey rhs  ) 

assigns OverlayKey of rhs to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00164 {
00165     isUnspec = rhs.isUnspec;
00166     memcpy( key, rhs.key, aSize*sizeof(mp_limb_t) );
00167     return *this;
00168 }

OverlayKey & OverlayKey::operator-- (  ) 

substracts 1 from this->key

Returns:
this OverlayKey object
00172 {
00173     return (*this -= ONE);
00174 }

OverlayKey & OverlayKey::operator++ (  ) 

adds 1 to this->key

Returns:
this OverlayKey object
00186 {
00187     return (*this += ONE);
00188 }

OverlayKey & OverlayKey::operator+= ( const OverlayKey rhs  ) 

adds rhs->key to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00200 {
00201     mpn_add_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00202     trim();
00203     isUnspec = false;
00204     return *this;
00205 }

OverlayKey & OverlayKey::operator-= ( const OverlayKey rhs  ) 

substracts rhs->key from this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00209 {
00210     mpn_sub_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00211     trim();
00212     isUnspec = false;
00213     return *this;
00214 }

OverlayKey OverlayKey::operator+ ( const OverlayKey rhs  )  const

adds rhs->key to this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00218 {
00219     OverlayKey result = *this;
00220     result += rhs;
00221     return result;
00222 }

OverlayKey OverlayKey::operator- ( const OverlayKey rhs  )  const

substracts rhs->key from this->key

Parameters:
rhs the OverlayKey with the defined key
Returns:
this OverlayKey object
00226 {
00227     OverlayKey result = *this;
00228     result -= rhs;
00229     return result;
00230 }

OverlayKey OverlayKey::operator-- ( int   ) 

substracts 1 from this->key

Returns:
this OverlayKey object
00178 {
00179     OverlayKey clone = *this;
00180     *this -= ONE;
00181     return clone;
00182 }

OverlayKey OverlayKey::operator++ ( int   ) 

adds 1 to this->key

Returns:
this OverlayKey object
00192 {
00193     OverlayKey clone = *this;
00194     *this += ONE;
00195     return clone;
00196 }

OverlayKey OverlayKey::operator>> ( uint  num  )  const

bitwise shift right

Parameters:
num number of bits to shift
Returns:
this OverlayKey object
00305 {
00306     OverlayKey result = ZERO;
00307     int i = num/GMP_LIMB_BITS;
00308 
00309     num %= GMP_LIMB_BITS;
00310 
00311     if (i>=(int)aSize)
00312         return result;
00313 
00314     for (int j=0; j<(int)aSize-i; j++) {
00315         result.key[j] = key[j+i];
00316     }
00317     mpn_rshift(result.key,result.key,aSize,num);
00318     result.isUnspec = false;
00319     result.trim();
00320 
00321     return result;
00322 }

OverlayKey OverlayKey::operator<< ( uint  num  )  const

bitwise shift left

Parameters:
num number of bits to shift
Returns:
this OverlayKey object
00326 {
00327     OverlayKey result = ZERO;
00328     int i = num/GMP_LIMB_BITS;
00329 
00330     num %= GMP_LIMB_BITS;
00331 
00332     if (i>=(int)aSize)
00333         return result;
00334 
00335     for (int j=0; j<(int)aSize-i; j++) {
00336         result.key[j+i] = key[j];
00337     }
00338     mpn_lshift(result.key,result.key,aSize,num);
00339     result.isUnspec = false;
00340     result.trim();
00341 
00342     return result;
00343 }

OverlayKey OverlayKey::operator & ( const OverlayKey rhs  )  const

bitwise AND of rhs->key and this->key

Parameters:
rhs the OverlayKey AND is calculated with
Returns:
this OverlayKey object
00282 {
00283     OverlayKey result = *this;
00284     for (uint i=0; i<aSize; i++) {
00285         result.key[i] &= rhs.key[i];
00286     }
00287 
00288     return result;
00289 }

OverlayKey OverlayKey::operator| ( const OverlayKey rhs  )  const

bitwise OR of rhs->key and this->key

Parameters:
rhs the OverlayKey OR is calculated with
Returns:
this OverlayKey object
00271 {
00272     OverlayKey result = *this;
00273     for (uint i=0; i<aSize; i++) {
00274         result.key[i] |= rhs.key[i];
00275     }
00276 
00277     return result;
00278 }

OverlayKey OverlayKey::operator^ ( const OverlayKey rhs  )  const

bitwise XOR of rhs->key and this->key

Parameters:
rhs the OverlayKey XOR is calculated with
Returns:
this OverlayKey object
00260 {
00261     OverlayKey result = *this;
00262     for (uint i=0; i<aSize; i++) {
00263         result.key[i] ^= rhs.key[i];
00264     }
00265 
00266     return result;
00267 }

OverlayKey OverlayKey::operator~ (  )  const

bitwise NOT of this->key

Returns:
this OverlayKey object
00293 {
00294     OverlayKey result = *this;
00295     for (uint i=0; i<aSize; i++) {
00296         result.key[i] = ~key[i];
00297     }
00298     result.trim();
00299 
00300     return result;
00301 }

bool OverlayKey::operator[] ( uint  n  )  const

returns the n-th bit of this->key

Parameters:
n the position of the returned bit
Returns:
the bit on position n in this->key
00347 {
00348     return (key[n / GMP_LIMB_BITS] >> (n % GMP_LIMB_BITS)) & 1;
00349 }

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.

Parameters:
p the position of the sub-integer
n the number of bits to be returned (max.32)
Returns:
The sub-integer.
00358     {
00359         // easy ...
00360         int i = p / GMP_LIMB_BITS, f = p % GMP_LIMB_BITS,
00361             f2 = n+f-GMP_LIMB_BITS;
00362         // ... and nasty stuff ... :)
00363 
00364         return ((key[i] >> f) | (f2>0 ? (key[i+1]<<(GMP_LIMB_BITS-f)) : 0)) &
00365                (((uint32_t)(~0))>>(GMP_LIMB_BITS-n));
00366     }

size_t OverlayKey::hash (  )  const

Returns a hash value for the key.

Returns:
size_t The hash value
00463 {
00464     return (size_t)key[0];
00465 }

int OverlayKey::log_2 (  )  const

Returns the position of the msb in this key, which represents just the logarithm to base 2.

Returns:
The logarithm to base 2 of this key.
00440 {
00441     int16_t i = aSize-1;
00442 
00443     while (i>=0 && key[i]==0) {
00444         i--;
00445     }
00446 
00447     if (i<0) {
00448         return -1;
00449     }
00450 
00451     mp_limb_t j = key[i];
00452     i *= GMP_LIMB_BITS;
00453     while (j!=0) {
00454         j >>= 1;
00455         i++;
00456     }
00457 
00458     return i-1;
00459 }

OverlayKey OverlayKey::randomSuffix ( uint  pos  )  const

Fills the suffix starting at pos with random bits to lsb.

Parameters:
pos 
Returns:
OverlayKey
00370 {
00371     OverlayKey newKey = *this;
00372     int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS;
00373     mp_limb_t m = ((mp_limb_t)1 << j)-1;
00374     mp_limb_t rnd;
00375 
00376     //  mpn_random(&rnd,1);
00377     omnet_random(&rnd,1);
00378     newKey.key[i] &= ~m;
00379     newKey.key[i] |= (rnd&m);
00380     //  mpn_random(newKey.key,i);
00381     omnet_random(newKey.key,i);
00382     newKey.trim();
00383 
00384     return newKey;
00385 }

OverlayKey OverlayKey::randomPrefix ( uint  pos  )  const

Fills the prefix starting at pos with random bits to msb.

Parameters:
pos 
Returns:
OverlayKey
00389 {
00390     OverlayKey newKey = *this;
00391     int i = pos/GMP_LIMB_BITS, j = pos%GMP_LIMB_BITS;
00392     mp_limb_t m = ((mp_limb_t)1 << j)-1;
00393     mp_limb_t rnd;
00394 
00395     //  mpn_random(&rnd,1);
00396     omnet_random(&rnd,1);
00397 
00398     newKey.key[i] &= m;
00399     newKey.key[i] |= (rnd&~m);
00400     for (int k=aSize-1; k!=i; k--) {
00401         //        mpn_random( &newKey.key[k], 1 );
00402         omnet_random( &newKey.key[k], 1 );
00403     }
00404     newKey.trim();
00405 
00406     return newKey;
00407 }

uint OverlayKey::sharedPrefixLength ( const OverlayKey compKey  )  const

Calculates the number of equal bits from the left with another Key (shared prefix length).

Parameters:
compKey the Key to compare with
Returns:
length of shared prefix
00411 {
00412     if (compareTo(compKey) == 0) return keyLength;
00413 
00414     uint length = 0;
00415     int i;
00416     uint j;
00417     bool msb = true;
00418 
00419     // count equal limbs first:
00420     for (i=aSize-1; i>=0; --i)
00421     {
00422         if (this->key[i] != compKey.key[i])
00423         {
00424             // XOR first differing limb for easy counting of the bits:
00425             mp_limb_t d = this->key[i] ^ compKey.key[i];
00426             if (msb) d <<= ( GMP_LIMB_BITS - (keyLength % GMP_LIMB_BITS) );
00427             for (j = GMP_LIMB_BITS-1; d >>= 1; --j);
00428             length += j;
00429             break;
00430         }
00431         length += GMP_LIMB_BITS;
00432         msb = false;
00433     }
00434     
00435     return length;
00436 }

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.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval (keyA, keyB)
00470 {
00471     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00472         return false;
00473 
00474     if (*this == keyA)
00475         return false;
00476     else if (keyA < keyB)
00477         return ((*this > keyA) && (*this < keyB));
00478     else
00479         return ((*this > keyA) || (*this < keyB));
00480 }

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.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval (keyA, keyB]
00485 {
00486     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00487         return false;
00488 
00489     if ((keyA == keyB) && (*this == keyA))
00490         return true;
00491     else if (keyA <= keyB)
00492         return ((*this > keyA) && (*this <= keyB));
00493     else
00494         return ((*this > keyA) || (*this <= keyB));
00495 }

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.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval [keyA, keyB)
00500 {
00501     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00502         return false;
00503 
00504     if ((keyA == keyB) && (*this == keyA))
00505         return true;
00506     else if (keyA <= keyB)
00507         return ((*this >= keyA) && (*this < keyB));
00508     else
00509         return ((*this >= keyA) || (*this < keyB));
00510 }

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.

Parameters:
keyA The left border of the interval
keyB The right border of the interval
Returns:
True, if the key is element of the interval [keyA, keyB]
00515 {
00516     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00517         return false;
00518 
00519     if ((keyA == keyB) && (*this == keyA))
00520         return true;
00521     else if (keyA <= keyB)
00522         return ((*this >= keyA) && (*this <= keyB));
00523     else
00524         return ((*this >= keyA) || (*this <= keyB));
00525 }

void OverlayKey::setKeyLength ( uint  length  )  [static]

Set the length of an OverlayKey.

Parameters:
length keylength in bits
00095 {
00096     if ((length < 1) || (length > OverlayKey::keyLength)) {
00097         opp_error("OverlayKey::setKeyLength(): length must be <= %i "
00098                   "and setKeyLength() must not be called twice "
00099                   "with different length!", MAX_KEYLENGTH);
00100     }
00101 
00102     keyLength = length;
00103 
00104     aSize = keyLength / (8*sizeof(mp_limb_t)) +
00105             (keyLength % (8*sizeof(mp_limb_t))!=0 ? 1 : 0);
00106 
00107     GMP_MSB_MASK = (keyLength % GMP_LIMB_BITS)
00108         != 0 ? (((mp_limb_t)1 << (keyLength % GMP_LIMB_BITS))-1)
00109         : (mp_limb_t)-1;
00110 }

uint OverlayKey::getLength (  )  [static]

Returns the length in number of bits.

Returns:
The length in number of bits.
00115 {
00116     return OverlayKey::keyLength;
00117 }

OverlayKey OverlayKey::random (  )  [static]

Returns a random key.

Returns:
A random key.
00555 {
00556     OverlayKey newKey = ZERO;
00557 
00558     //    mpn_random(newKey.key,aSize);
00559     omnet_random(newKey.key,aSize);
00560 
00561     newKey.trim();
00562 
00563     return newKey;
00564 }

OverlayKey OverlayKey::max (  )  [static]

Returns the maximum key, i.e.

a key filled with bit 1

Returns:
The maximum key, i.e. a key filled with bit 1
00541 {
00542     OverlayKey newKey;
00543 
00544     for (uint i=0; i<aSize; i++) {
00545         newKey.key[i] = ~0;
00546     }
00547     newKey.isUnspec = false;
00548     newKey.trim();
00549 
00550     return newKey;
00551 }

OverlayKey OverlayKey::sha1 ( const BinaryValue value  )  [static]

Returns a key with the SHA1 cryptographic hash of a BinaryValue.

Parameters:
value A BinaryValue object.
Returns:
SHA1 of value
00568 {
00569     OverlayKey newKey = OverlayKey();
00570     uint8_t temp[20];
00571     CSHA1 sha1;
00572 
00573     sha1.Reset();
00574     sha1.Update((uint8_t*)(&(*input.begin())), input.size());
00575     sha1.Final();
00576     sha1.GetHash(temp);
00577     mpn_set_str(newKey.key, (const uint8_t*)temp,
00578                 (int)min(aSize * sizeof(mp_limb_t), 20), 256);
00579     newKey.isUnspec = false;
00580     newKey.trim();
00581 
00582     return newKey;
00583 }

OverlayKey OverlayKey::pow2 ( uint  exponent  )  [static]

Returns a key 2^exponent.

Parameters:
exponent The exponent.
Returns:
Key=2^exponent.
00587 {
00588     OverlayKey newKey = ZERO;
00589 
00590     newKey.key[exponent/GMP_LIMB_BITS] =
00591         (mp_limb_t)1 << (exponent % GMP_LIMB_BITS);
00592 
00593     return newKey;
00594 }

void OverlayKey::test (  )  [static]

A pseudo regression test method.

Outputs report to standard output.

00598 {
00599     // add test
00600     cout << endl << "--- Add test ..." << endl;
00601     OverlayKey key = 123456789;
00602     cout << "    key=" << key << endl;
00603     cout << "    key += 987654321 = " << (key+=987654321) << endl;
00604     cout << "    prefix++  : " << (++key) << endl;
00605     cout << "    postfix++ : " << (key++) << endl;
00606     cout << "    key=" << key << endl;
00607 
00608     OverlayKey k1 = 256, k2 = 10, k3 = 3;
00609 
00610     // compare test
00611     cout << endl << "--- Compare test ..." << endl;
00612     cout << "    256 < 10 = "<< (k1 < k2) << " k1="<<k1<<endl;
00613     cout << "    256 > 10 = "<< (k1 > k2) << " k2="<<k2<<endl;
00614 
00615     cout << "    10 isBetween(3, 256)=" << k2.isBetween(k3, k1) << endl;
00616     cout << "    3 isBetween(10, 256)=" << k3.isBetween(k2, k1) << endl;
00617     cout << "    256 isBetween(10, 256)=" << k1.isBetween(k2, k1) << endl;
00618     cout << "    256 isBetweenR(10, 256)=" << k1.isBetweenR(k2, k1) << endl;
00619     cout << "    max isBetween(max-1,0)=" << OverlayKey::max().isBetween(
00620         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00621     cout << "    max-1 isBetween(max,1)=" << (OverlayKey::max()-1).isBetween(
00622         OverlayKey::max(), OverlayKey::ONE) << endl;
00623     cout << "    max-1 isBetweenL(max-1,1)=" << (OverlayKey::max()-1).
00624     isBetweenL(OverlayKey::max()-1, OverlayKey::ONE) << endl;
00625     cout << "    1 isBetweenL(max-1,1)=" << (OverlayKey::ONE).isBetweenL(
00626         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00627     cout << "    1 isBetweenR(max-1,1)=" << OverlayKey::ONE.isBetweenR(
00628         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00629     cout << "    1 isBetween(max-1,1)=" << OverlayKey::ONE.isBetween(
00630         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00631     cout << "    1 isBetween(max-1,0)=" << OverlayKey::ONE.isBetween(
00632         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00633     cout << "    256 sharedPrefixLength(3)=" << k1.sharedPrefixLength(k3)
00634         << endl;
00635     cout << "    256 sharedPrefixLength(256)=" << k1.sharedPrefixLength(k1)
00636         << endl;
00637 
00638     // wrap around test
00639     cout << endl << "--- Warp around test ..." << endl;
00640 
00641     k1 = OverlayKey::max();
00642     cout << "k1=max= " << k1.toString(16) << endl;
00643     cout << "k1+1 = " << (k1 + 1).toString(16) << endl;
00644     cout << "k1+2 = " << (k1 + 2).toString(16) << endl;
00645 
00646     k1 = OverlayKey::ZERO;
00647     cout << "k1=0= " << k1.toString(16) << endl;
00648     cout << "k1-1 = " << (k1 - 1).toString(16) << endl;
00649     cout << "k1-2 = " << (k1 - 2).toString(16) << endl;
00650 
00651     cout << "max > ONE=" << (OverlayKey::max() > OverlayKey::ONE) << endl;
00652     cout << "max < ONE=" << (OverlayKey::max() < OverlayKey::ONE) << endl;
00653 
00654 
00655     // suffix and log2 test
00656     cout << endl << "--- RandomSuffix and log2 test ..." << endl;
00657     k1 = OverlayKey::ZERO;
00658     for (uint i=0; i<k1.getLength(); i++) {
00659         k2=k1.randomSuffix(i);
00660         cout << "    " << k2.toString(16) << " log2=" << k2.log_2() << endl;
00661     }
00662     cout << endl << "--- RandomPrefix and log2 test ..." << endl;
00663     k1 = OverlayKey::max();
00664     for (uint i=0; i<k1.getLength(); i++) {
00665         k2=k1.randomPrefix(i);
00666         cout << "    " << k2.toString(16) << " log2=" << k2.log_2() << endl;
00667     }
00668 
00669     cout << endl << "--- pow2 test..." << endl;
00670     for (uint i=0; i<k1.getLength(); i++) {
00671         k2=pow2(i);
00672         cout << " 2^" << i << " = " << k2.toString(16) << "   log2="
00673         << k2.log_2() << endl;
00674     }
00675 
00676     cout << endl << "--- Bits test ..." << endl << "    ";
00677     const char* BITS[] = { "000","001","010","011","100","101","110","111"
00678                          };
00679     k1 = OverlayKey::random();
00680     for (int i=k1.getLength()-1; i>=0; i--)
00681         cout << k1[i];
00682     cout << " = " << endl << "    ";
00683     for (int i=k1.getLength()-3; i>=0; i-=3)
00684         cout << BITS[k1.get(i,3)];
00685     cout << endl;
00686 
00687     cout << endl << "--- SHA1 test ... (verified with test vectors)" << endl;
00688     cout << "    Empty string: " << OverlayKey::sha1("").toString(16)
00689     << " = da39a3ee5e6b4b0d3255bfef95601890afd80709" << endl;
00690     cout << "    'Hello World' string: "
00691     << OverlayKey::sha1("Hello World").toString(16)
00692     << " = 0a4d55a8d778e5022fab701977c5d840bbc486d0" << endl;
00693 }

int OverlayKey::bitAtPlace ( int  step  )  const

Todo:
get rid of this function (Koorde)
00726 {
00727   if ((unsigned int)step > keyLength)
00728   {
00729     //cout << "Bit not in Key!" << endl;
00730     return 0;
00731   }
00732   return ((*this).get((*this).getLength()  - step, 1));
00733 }

void OverlayKey::trim (  )  [inline, private]

trims key after key operations

00701 {
00702     key[aSize-1] &= GMP_MSB_MASK;
00703 }

void OverlayKey::clear (  )  [inline, private]

set this->key to 0 and isUnspec to false

00716 {
00717     memset( key, 0, aSize * sizeof(mp_limb_t) );
00718     isUnspec = false;
00719 }

void OverlayKey::netPack ( cCommBuffer *  b  ) 

serializes the object into a buffer

Parameters:
b the buffer
00797 {
00798     doPacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00799             (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00800     doPacking(b,this->isUnspec);
00801 }

void OverlayKey::netUnpack ( cCommBuffer *  b  ) 

deserializes the object from a buffer

Parameters:
b the buffer
00804 {
00805     doUnpacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00806               (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00807     doUnpacking(b,this->isUnspec);
00808 
00809 }


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const OverlayKey c 
) [friend]

Common stdc++ console output method.

00534 {
00535     os << c.toString(16);
00536     return os;
00537 };


Member Data Documentation

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 [static, private]

actual length of the key

uint OverlayKey::aSize [static, private]

number of needed machine words to hold the key

mp_limb_t OverlayKey::GMP_MSB_MASK [static, private]

bits to fill up if key does not exactly fit in one or more machine words

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


The documentation for this class was generated from the following files:
Generated on Wed Sep 26 12:13:01 2007 for ITM OverSim by  doxygen 1.5.1