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.
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.
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 (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

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

00681 {
00682     memset( key, 0, aSize * sizeof(mp_limb_t) );
00683     isUnspec = false;
00684 }

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
00673 {
00674     if (compKey.isUnspec || isUnspec)
00675         opp_error("OverlayKey::compareTo(): key is unspecified!");
00676     return mpn_cmp(key,compKey.key,aSize);
00677 }

uint32_t OverlayKey::get ( uint  p,
uint  n 
) const

Returns a sub integer at position p with n-bits.

Parameters:
p the position of the sub-integer
n the number of bits to be returned (max.32)
Returns:
The substring.
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
00432 {
00433     return (size_t)key[0];
00434 }

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)
00439 {
00440     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00441         return false;
00442 
00443     if (*this == keyA)
00444         return false;
00445     else if (keyA < keyB)
00446         return ((*this > keyA) && (*this < keyB));
00447     else
00448         return ((*this > keyA) || (*this < keyB));
00449 }

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)
00469 {
00470     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00471         return false;
00472 
00473     if ((keyA == keyB) && (*this == keyA))
00474         return true;
00475     else if (keyA <= keyB)
00476         return ((*this >= keyA) && (*this < keyB));
00477     else
00478         return ((*this >= keyA) || (*this < keyB));
00479 }

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]
00484 {
00485     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00486         return false;
00487 
00488     if ((keyA == keyB) && (*this == keyA))
00489         return true;
00490     else if (keyA <= keyB)
00491         return ((*this >= keyA) && (*this <= keyB));
00492     else
00493         return ((*this >= keyA) || (*this <= keyB));
00494 }

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]
00454 {
00455     if (isUnspec || keyA.isUnspec || keyB.isUnspec)
00456         return false;
00457 
00458     if ((keyA == keyB) && (*this == keyA))
00459         return true;
00460     else if (keyA <= keyB)
00461         return ((*this > keyA) && (*this <= keyB));
00462     else
00463         return ((*this > keyA) || (*this <= keyB));
00464 }

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.
00409 {
00410     int16_t i = aSize-1;
00411 
00412     while (i>=0 && key[i]==0) {
00413         i--;
00414     }
00415 
00416     if (i<0) {
00417         return -1;
00418     }
00419 
00420     mp_limb_t j = key[i];
00421     i *= GMP_LIMB_BITS;
00422     while (j!=0) {
00423         j >>= 1;
00424         i++;
00425     }
00426 
00427     return i-1;
00428 }

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
00510 {
00511     OverlayKey newKey;
00512 
00513     for (uint i=0; i<aSize; i++) {
00514         newKey.key[i] = ~0;
00515     }
00516     newKey.isUnspec = false;
00517     newKey.trim();
00518 
00519     return newKey;
00520 }

void OverlayKey::netPack ( cCommBuffer *  b  ) 

00747 {
00748     doPacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00749             (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00750     doPacking(b,this->isUnspec);
00751 }

void OverlayKey::netUnpack ( cCommBuffer *  b  ) 

00754 {
00755     doUnpacking(b,(GMP_TYPE*)this->key, MAX_KEYLENGTH / (8*sizeof(mp_limb_t)) +
00756               (MAX_KEYLENGTH % (8*sizeof(mp_limb_t))!=0 ? 1 : 0));
00757     doUnpacking(b,this->isUnspec);
00758 
00759 }

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.
00556 {
00557     OverlayKey newKey = ZERO;
00558 
00559     newKey.key[exponent/GMP_LIMB_BITS] =
00560         (mp_limb_t)1 << (exponent % GMP_LIMB_BITS);
00561 
00562     return newKey;
00563 }

OverlayKey OverlayKey::random (  )  [static]

Returns a random key.

Returns:
A random key.
00524 {
00525     OverlayKey newKey = ZERO;
00526 
00527     //    mpn_random(newKey.key,aSize);
00528     omnet_random(newKey.key,aSize);
00529 
00530     newKey.trim();
00531 
00532     return newKey;
00533 }

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 ( 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
00537 {
00538     OverlayKey newKey = OverlayKey();
00539     uint8_t temp[20];
00540     CSHA1 sha1;
00541 
00542     sha1.Reset();
00543     sha1.Update((uint8_t*)input, strlen(input));
00544     sha1.Final();
00545     sha1.GetHash(temp);
00546     mpn_set_str(newKey.key, (const uint8_t*)temp,
00547                 (int)min(aSize * sizeof(mp_limb_t), 20), 256);
00548     newKey.isUnspec = false;
00549     newKey.trim();
00550 
00551     return newKey;
00552 }

void OverlayKey::test (  )  [static]

A pseudo regression test method.

Outputs report to standard output.

00567 {
00568     // add test
00569     cout << endl << "--- Add test ..." << endl;
00570     OverlayKey key = 123456789;
00571     cout << "    key=" << key << endl;
00572     cout << "    key += 987654321 = " << (key+=987654321) << endl;
00573     cout << "    prefix++  : " << (++key) << endl;
00574     cout << "    postfix++ : " << (key++) << endl;
00575     cout << "    key=" << key << endl;
00576 
00577     OverlayKey k1 = 256, k2 = 10, k3 = 3;
00578 
00579     // compare test
00580     cout << endl << "--- Compare test ..." << endl;
00581     cout << "    256 < 10 = "<< (k1 < k2) << " k1="<<k1<<endl;
00582     cout << "    256 > 10 = "<< (k1 > k2) << " k2="<<k2<<endl;
00583 
00584     cout << "    10 isBetween(3, 256)=" << k2.isBetween(k3, k1) << endl;
00585     cout << "    3 isBetween(10, 256)=" << k3.isBetween(k2, k1) << endl;
00586     cout << "    256 isBetween(10, 256)=" << k1.isBetween(k2, k1) << endl;
00587     cout << "    256 isBetweenR(10, 256)=" << k1.isBetweenR(k2, k1) << endl;
00588     cout << "    max isBetween(max-1,0)=" << OverlayKey::max().isBetween(
00589         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00590     cout << "    max-1 isBetween(max,1)=" << (OverlayKey::max()-1).isBetween(
00591         OverlayKey::max(), OverlayKey::ONE) << endl;
00592     cout << "    max-1 isBetweenL(max-1,1)=" << (OverlayKey::max()-1).
00593     isBetweenL(OverlayKey::max()-1, OverlayKey::ONE) << endl;
00594     cout << "    1 isBetweenL(max-1,1)=" << (OverlayKey::ONE).isBetweenL(
00595         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00596     cout << "    1 isBetweenR(max-1,1)=" << OverlayKey::ONE.isBetweenR(
00597         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00598     cout << "    1 isBetween(max-1,1)=" << OverlayKey::ONE.isBetween(
00599         OverlayKey::max()-1, OverlayKey::ONE) << endl;
00600     cout << "    1 isBetween(max-1,0)=" << OverlayKey::ONE.isBetween(
00601         OverlayKey::max()-1, OverlayKey::ZERO) << endl;
00602 
00603     // wrap around test
00604     cout << endl << "--- Warp around test ..." << endl;
00605 
00606     k1 = OverlayKey::max();
00607     cout << "k1=max= " << k1.toString(16) << endl;
00608     cout << "k1+1 = " << (k1 + 1).toString(16) << endl;
00609     cout << "k1+2 = " << (k1 + 2).toString(16) << endl;
00610 
00611     k1 = OverlayKey::ZERO;
00612     cout << "k1=0= " << k1.toString(16) << endl;
00613     cout << "k1-1 = " << (k1 - 1).toString(16) << endl;
00614     cout << "k1-2 = " << (k1 - 2).toString(16) << endl;
00615 
00616     cout << "max > ONE=" << (OverlayKey::max() > OverlayKey::ONE) << endl;
00617     cout << "max < ONE=" << (OverlayKey::max() < OverlayKey::ONE) << endl;
00618 
00619 
00620     // suffix and log2 test
00621     cout << endl << "--- RandomSuffix and log2 test ..." << endl;
00622     k1 = OverlayKey::ZERO;
00623     for (uint i=0; i<k1.getLength(); i++) {
00624         k2=k1.randomSuffix(i);
00625         cout << "    " << k2.toString(16) << " log2=" << k2.log2() << endl;
00626     }
00627     cout << endl << "--- RandomPrefix and log2 test ..." << endl;
00628     k1 = OverlayKey::max();
00629     for (uint i=0; i<k1.getLength(); i++) {
00630         k2=k1.randomPrefix(i);
00631         cout << "    " << k2.toString(16) << " log2=" << k2.log2() << endl;
00632     }
00633 
00634     cout << endl << "--- pow2 test..." << endl;
00635     for (uint i=0; i<k1.getLength(); i++) {
00636         k2=pow2(i);
00637         cout << " 2^" << i << " = " << k2.toString(16) << "   log2="
00638         << k2.log2() << endl;
00639     }
00640 
00641     cout << endl << "--- Bits test ..." << endl << "    ";
00642     const char* BITS[] = { "000","001","010","011","100","101","110","111"
00643                          };
00644     k1 = OverlayKey::random();
00645     for (int i=k1.getLength()-1; i>=0; i--)
00646         cout << k1[i];
00647     cout << " = " << endl << "    ";
00648     for (int i=k1.getLength()-3; i>=0; i-=3)
00649         cout << BITS[k1.get(i,3)];
00650     cout << endl;
00651 
00652     cout << endl << "--- SHA1 test ... (verified with test vectors)" << endl;
00653     cout << "    Empty string: " << OverlayKey::sha1("").toString(16)
00654     << " = da39a3ee5e6b4b0d3255bfef95601890afd80709" << endl;
00655     cout << "    'Hello World' string: "
00656     << OverlayKey::sha1("Hello World").toString(16)
00657     << " = 0a4d55a8d778e5022fab701977c5d840bbc486d0" << endl;
00658 }

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]

00666 {
00667     key[aSize-1] &= GMP_MSB_MASK;
00668 }


Friends And Related Function Documentation

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

Common stdc++ console output method.

00503 {
00504     os << c.toString(16);
00505     return os;
00506 };


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 Wed Apr 4 13:37:06 2007 for ITM OverSim by  doxygen 1.4.7