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 a overlay key initialized with a common integer.
 OverlayKey (const char *str, uint radix=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
 Common compare operators.
bool operator> (const OverlayKey &compKey) const
bool operator<= (const OverlayKey &compKey) const
bool operator>= (const OverlayKey &compKey) const
bool operator== (const OverlayKey &compKey) const
bool operator!= (const OverlayKey &compKey) const
int compareTo (const OverlayKey &compKey) const
 Unifies all compare operations in one method.
OverlayKeyoperator= (const OverlayKey &rhs)
 Assign and arithmetic operators.
OverlayKeyoperator-- ()
OverlayKeyoperator++ ()
OverlayKeyoperator+= (const OverlayKey &rhs)
OverlayKeyoperator-= (const OverlayKey &rhs)
OverlayKey operator+ (const OverlayKey &rhs) const
OverlayKey operator- (const OverlayKey &rhs) const
OverlayKey operator-- (int)
OverlayKey operator++ (int)
OverlayKey operator>> (uint num) const
 Bit operators.
OverlayKey operator<< (uint num) const
OverlayKey operator & (const OverlayKey &rhs) const
OverlayKey operator| (const OverlayKey &rhs) const
OverlayKey operator^ (const OverlayKey &rhs) const
OverlayKey operator~ () const
bool operator[] (uint n) const
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 log2 () 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)
void netUnpack (cCommBuffer *b)

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 char *str)
 Returns a key with the SHA1 cryptographic hash of a null-terminated string.
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
static const OverlayKey ZERO
static const OverlayKey ONE

Private Member Functions

void trim ()
void clear ()

Private Attributes

bool isUnspec
mp_limb_t key [MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)]

Static Private Attributes

static const uint MAX_KEYLENGTH = 160
static uint keyLength = MAX_KEYLENGTH
static uint aSize
static mp_limb_t GMP_MSB_MASK

Friends

std::ostream & operator<< (std::ostream &os, const OverlayKey &c)
 Common stdc++ console output method.


Constructor & Destructor Documentation

OverlayKey::OverlayKey (  ) 

Default constructor.

Contructs a unspecified overlay key

00060 {
00061     isUnspec = true;
00062 }

OverlayKey::OverlayKey ( uint32_t  num  ) 

Constructs a 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 char *  str,
uint  radix = 16 
)

Constructs a key out of a string number.

00074 {
00075     throw "NOT IMPLEMENTED YET!!";
00076 }

OverlayKey::OverlayKey ( const OverlayKey rhs  ) 

Copy contructor.

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

OverlayKey::~OverlayKey (  ) 

Default destructor.

Does nothing ATM.

00086 {}


Member Function Documentation

int OverlayKey::bitAtPlace ( int  step  )  const

Todo:
get rid of this function (Koorde)
00719 {
00720   if ((unsigned int)step > keyLength)
00721   {
00722     //cout << "Bit not in Key!" << endl;
00723     return 0;
00724   }
00725   return ((*this).get((*this).getLength()  - step, 1));
00726 }

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

00709 {
00710     memset( key, 0, aSize * sizeof(mp_limb_t) );
00711     isUnspec = false;
00712 }

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
00701 {
00702     if (compKey.isUnspec || isUnspec)
00703         opp_error("OverlayKey::compareTo(): key is unspecified!");
00704     return mpn_cmp(key,compKey.key,aSize);
00705 }

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.
00356     {
00357         // easy ...
00358         int i = p / GMP_LIMB_BITS, f = p % GMP_LIMB_BITS,
00359             f2 = n+f-GMP_LIMB_BITS;
00360         // ... and nasty stuff ... :)
00361 
00362         return ((key[i] >> f) | (f2>0 ? (key[i+1]<<(GMP_LIMB_BITS-f)) : 0)) &
00363                (((uint32_t)(~0))>>(GMP_LIMB_BITS-n));
00364     }

uint OverlayKey::getLength (  )  [static]

Returns the length in number of bits.

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

size_t OverlayKey::hash (  )  const

Returns a hash value for the key.

Returns:
size_t The hash value
00456 {
00457     return (size_t)key[0];
00458 }

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)
00463 {
00464     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00465         return false;
00466 
00467     if (*this == keyA)
00468         return false;
00469     else if (keyA < keyB)
00470         return ((*this > keyA) && (*this < keyB));
00471     else
00472         return ((*this > keyA) || (*this < keyB));
00473 }

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)
00493 {
00494     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00495         return false;
00496 
00497     if ((keyA == keyB) && (*this == keyA))
00498         return true;
00499     else if (keyA <= keyB)
00500         return ((*this >= keyA) && (*this < keyB));
00501     else
00502         return ((*this >= keyA) || (*this < keyB));
00503 }

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]
00508 {
00509     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00510         return false;
00511 
00512     if ((keyA == keyB) && (*this == keyA))
00513         return true;
00514     else if (keyA <= keyB)
00515         return ((*this >= keyA) && (*this <= keyB));
00516     else
00517         return ((*this >= keyA) || (*this <= keyB));
00518 }

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]
00478 {
00479     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00480         return false;
00481 
00482     if ((keyA == keyB) && (*this == keyA))
00483         return true;
00484     else if (keyA <= keyB)
00485         return ((*this > keyA) && (*this <= keyB));
00486     else
00487         return ((*this > keyA) || (*this <= keyB));
00488 }

bool OverlayKey::isUnspecified (  )  const

Returns true, if the key is unspecified.

Returns:
Returns true, if the key is unspecified
00118 {
00119     return isUnspec;
00120 }

int OverlayKey::log2 (  )  const

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

Returns:
The logartithm to base 2 of this key.
00433 {
00434     int16_t i = aSize-1;
00435 
00436     while (i>=0 && key[i]==0) {
00437         i--;
00438     }
00439 
00440     if (i<0) {
00441         return -1;
00442     }
00443 
00444     mp_limb_t j = key[i];
00445     i *= GMP_LIMB_BITS;
00446     while (j!=0) {
00447         j >>= 1;
00448         i++;
00449     }
00450 
00451     return i-1;
00452 }

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
00534 {
00535     OverlayKey newKey;
00536 
00537     for (uint i=0; i<aSize; i++) {
00538         newKey.key[i] = ~0;
00539     }
00540     newKey.isUnspec = false;
00541     newKey.trim();
00542 
00543     return newKey;
00544 }

void OverlayKey::netPack ( cCommBuffer *  b  ) 

00790 {
00791     doPacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00792             (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00793     doPacking(b,this->isUnspec);
00794 }

void OverlayKey::netUnpack ( cCommBuffer *  b  ) 

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

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

00280 {
00281     OverlayKey result = *this;
00282     for (uint i=0; i<aSize; i++) {
00283         result.key[i] &= rhs.key[i];
00284     }
00285 
00286     return result;
00287 }

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

00252 {
00253     return compareTo(compKey) !=0;
00254 }

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

00216 {
00217     OverlayKey result = *this;
00218     result += rhs;
00219     return result;
00220 }

OverlayKey OverlayKey::operator++ ( int   ) 

00190 {
00191     OverlayKey clone = *this;
00192     *this += ONE;
00193     return clone;
00194 }

OverlayKey & OverlayKey::operator++ (  ) 

00184 {
00185     return (*this += ONE);
00186 }

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

00198 {
00199     mpn_add_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00200     trim();
00201     isUnspec = false;
00202     return *this;
00203 }

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

00224 {
00225     OverlayKey result = *this;
00226     result -= rhs;
00227     return result;
00228 }

OverlayKey OverlayKey::operator-- ( int   ) 

00176 {
00177     OverlayKey clone = *this;
00178     *this -= ONE;
00179     return clone;
00180 }

OverlayKey & OverlayKey::operator-- (  ) 

00170 {
00171     return (*this -= ONE);
00172 }

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

00207 {
00208     mpn_sub_n((mp_limb_t*)key, (mp_limb_t*)key, (mp_limb_t*)rhs.key, aSize);
00209     trim();
00210     isUnspec = false;
00211     return *this;
00212 }

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

Common compare operators.

00232 {
00233     return compareTo(compKey) < 0;
00234 }

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

00324 {
00325     OverlayKey result = ZERO;
00326     int i = num/GMP_LIMB_BITS;
00327 
00328     num %= GMP_LIMB_BITS;
00329 
00330     if (i>=(int)aSize)
00331         return result;
00332 
00333     for (int j=0; j<(int)aSize-i; j++) {
00334         result.key[j+i] = key[j];
00335     }
00336     mpn_lshift(result.key,result.key,aSize,num);
00337     result.isUnspec = false;
00338     result.trim();
00339 
00340     return result;
00341 }

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

00240 {
00241     return compareTo(compKey) <=0;
00242 }

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

Assign and arithmetic operators.

00162 {
00163     isUnspec = rhs.isUnspec;
00164     memcpy( key, rhs.key, aSize*sizeof(mp_limb_t) );
00165     return *this;
00166 }

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

00248 {
00249     return compareTo(compKey) ==0;
00250 }

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

00236 {
00237     return compareTo(compKey) > 0;
00238 }

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

00244 {
00245     return compareTo(compKey) >=0;
00246 }

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

Bit operators.

00303 {
00304     OverlayKey result = ZERO;
00305     int i = num/GMP_LIMB_BITS;
00306 
00307     num %= GMP_LIMB_BITS;
00308 
00309     if (i>=(int)aSize)
00310         return result;
00311 
00312     for (int j=0; j<(int)aSize-i; j++) {
00313         result.key[j] = key[j+i];
00314     }
00315     mpn_rshift(result.key,result.key,aSize,num);
00316     result.isUnspec = false;
00317     result.trim();
00318 
00319     return result;
00320 }

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

00345 {
00346     return (key[n / GMP_LIMB_BITS] >> (n % GMP_LIMB_BITS)) & 1;
00347 }

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

00258 {
00259     OverlayKey result = *this;
00260     for (uint i=0; i<aSize; i++) {
00261         result.key[i] ^= rhs.key[i];
00262     }
00263 
00264     return result;
00265 }

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

00269 {
00270     OverlayKey result = *this;
00271     for (uint i=0; i<aSize; i++) {
00272         result.key[i] |= rhs.key[i];
00273     }
00274 
00275     return result;
00276 }

OverlayKey OverlayKey::operator~ (  )  const

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

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

Returns a key 2^exponent.

Parameters:
exponent The exponent.
Returns:
Key=2^exponent.
00580 {
00581     OverlayKey newKey = ZERO;
00582 
00583     newKey.key[exponent/GMP_LIMB_BITS] =
00584         (mp_limb_t)1 << (exponent % GMP_LIMB_BITS);
00585 
00586     return newKey;
00587 }

OverlayKey OverlayKey::random (  )  [static]

Returns a random key.

Returns:
A random key.
00548 {
00549     OverlayKey newKey = ZERO;
00550 
00551     //    mpn_random(newKey.key,aSize);
00552     omnet_random(newKey.key,aSize);
00553 
00554     newKey.trim();
00555 
00556     return newKey;
00557 }

OverlayKey OverlayKey::randomPrefix ( uint  pos  )  const

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

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

OverlayKey OverlayKey::randomSuffix ( uint  pos  )  const

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

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

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

Set the length of an OverlayKey.

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

OverlayKey OverlayKey::sha1 ( const char *  str  )  [static]

Returns a key with the SHA1 cryptographic hash of a null-terminated string.

Parameters:
str A null-terminated string.
Returns:
SHA1 of str
00561 {
00562     OverlayKey newKey = OverlayKey();
00563     uint8_t temp[20];
00564     CSHA1 sha1;
00565 
00566     sha1.Reset();
00567     sha1.Update((uint8_t*)input, strlen(input));
00568     sha1.Final();
00569     sha1.GetHash(temp);
00570     mpn_set_str(newKey.key, (const uint8_t*)temp,
00571                 (int)min(aSize * sizeof(mp_limb_t), 20), 256);
00572     newKey.isUnspec = false;
00573     newKey.trim();
00574 
00575     return newKey;
00576 }

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
00409 {
00410     uint length = 0;
00411     int i;
00412     uint j;
00413 
00414     // count equal limbs first:
00415     for (i=aSize-1; i>=0; --i)
00416     {
00417         if (this->key[i] != compKey.key[i])
00418         {
00419             // XOR first differing limb for easy counting of the bits:
00420             mp_limb_t d = this->key[i] ^ compKey.key[i];
00421             for (j = GMP_LIMB_BITS-1; d >>= 1; --j);
00422             length += j;
00423             break;
00424         }
00425         length += GMP_LIMB_BITS;
00426     }
00427     
00428     return length;
00429 }

void OverlayKey::test (  )  [static]

A pseudo regression test method.

Outputs report to standard output.

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

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

Returns a string representation of this key.

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

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

00694 {
00695     key[aSize-1] &= GMP_MSB_MASK;
00696 }


Friends And Related Function Documentation

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

Common stdc++ console output method.

00527 {
00528     os << c.toString(16);
00529     return os;
00530 };


Member Data Documentation

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]

mp_limb_t OverlayKey::key[MAX_KEYLENGTH/(8 *sizeof(mp_limb_t))+(MAX_KEYLENGTH%(8 *sizeof(mp_limb_t))!=0?1:0)] [private]

uint OverlayKey::keyLength = MAX_KEYLENGTH [static, private]

const uint OverlayKey::MAX_KEYLENGTH = 160 [static, private]

const OverlayKey OverlayKey::ONE [static]

const OverlayKey OverlayKey::UNSPECIFIED_KEY [static]

const OverlayKey OverlayKey::ZERO [static]


The documentation for this class was generated from the following files:
Generated on Fri May 11 14:52:40 2007 for ITM OverSim by  doxygen 1.4.7