#include <I3Identifier.h>
Public Member Functions | |
I3Identifier () | |
Constructor. | |
I3Identifier (unsigned char b) | |
Constructs an identifier filled with a byte. | |
I3Identifier (int prefixL, int keyL) | |
Constructor for variable prefix length, key length. | |
I3Identifier (const I3Identifier &id) | |
Copy constructor. | |
I3Identifier (std::string s) | |
Constructs an identifier from the hash of a string. | |
int | getKeyLength () const |
Returns the key length (total length) in bits. | |
int | getPrefixLength () const |
Returns the prefix length in bits. | |
void | clear () |
Sets all bits to 0. | |
int | compareTo (const I3Identifier &) const |
Comparation function. | |
bool | operator< (const I3Identifier &) const |
"Less than" comparation function | |
bool | operator> (const I3Identifier &) const |
"Greater than" comparation function | |
bool | operator== (const I3Identifier &) const |
"Equals" comparation function | |
I3Identifier & | operator= (const I3Identifier &) |
Copy operator. | |
bool | isClear () |
Checks if this identifier has been cleared. | |
bool | isMatch (const I3Identifier &id) const |
Checks if this identifier's first prefixLength bits equal those of id. | |
int | distanceTo (const I3Identifier &id) const |
Returns the "distance to" a identifier. | |
void | createFromHash (const std::string &s, const std::string &o="") |
Creates an identifier from the hash of a string. | |
void | createRandomKey () |
void | createRandomPrefix () |
void | createRandomSuffix () |
int | length () const |
OverlayKey | asOverlayKey () const |
Creates an OverlayKey from an identifier, to be used in the overlay underneath. | |
void | setName (std::string s) |
std::string | getName () |
~I3Identifier () | |
Destructor. | |
Protected Member Functions | |
void | initKey (int prefixL, int keyL) |
Inits the key with a given prefix length and key length. | |
Protected Attributes | |
unsigned char * | key |
Identifier bits. | |
unsigned short | prefixLength |
Size of prefix in bits. | |
unsigned short | keyLength |
Size of identifier in bits. | |
std::string | name |
Friends | |
std::ostream & | operator<< (std::ostream &os, const I3Identifier &id) |
String stream output operator. |
I3Identifier::I3Identifier | ( | ) |
I3Identifier::I3Identifier | ( | unsigned char | b | ) |
Constructs an identifier filled with a byte.
b | Byte to fill with |
I3Identifier::I3Identifier | ( | int | prefixL, | |
int | keyL | |||
) |
Constructor for variable prefix length, key length.
prefixL | Prefix length | |
keyL | Key length |
00039 { 00040 initKey(prefixL, keyL); 00041 }
I3Identifier::I3Identifier | ( | const I3Identifier & | id | ) |
Copy constructor.
id | Identifier to copy |
00044 { 00045 initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); 00046 *this = id; 00047 }
I3Identifier::I3Identifier | ( | std::string | s | ) |
Constructs an identifier from the hash of a string.
s | String to be hashed |
00050 { 00051 initKey(DEFAULT_PREFIX_LENGTH, DEFAULT_KEY_LENGTH); 00052 createFromHash(s); 00053 }
I3Identifier::~I3Identifier | ( | ) |
int I3Identifier::getKeyLength | ( | ) | const |
int I3Identifier::getPrefixLength | ( | ) | const |
void I3Identifier::clear | ( | ) |
int I3Identifier::compareTo | ( | const I3Identifier & | id | ) | const |
bool I3Identifier::operator< | ( | const I3Identifier & | id | ) | const |
bool I3Identifier::operator> | ( | const I3Identifier & | id | ) | const |
bool I3Identifier::operator== | ( | const I3Identifier & | id | ) | const |
I3Identifier & I3Identifier::operator= | ( | const I3Identifier & | id | ) |
bool I3Identifier::isClear | ( | ) |
bool I3Identifier::isMatch | ( | const I3Identifier & | id | ) | const |
Checks if this identifier's first prefixLength bits equal those of id.
id | Identifier to be matched against |
00106 { 00107 return memcmp(key, id.key, prefixLength / 8) == 0; 00108 }
int I3Identifier::distanceTo | ( | const I3Identifier & | id | ) | const |
Returns the "distance to" a identifier.
This is used when many triggers match an identifier, to check which is the biggest prefix match. The distance is defined as the index of the byte (counting from the end) which is the first that is not equal, multiplied by 256, then added the XOR'ing of the differing byte. That way an earlier differing bits are considered "further" from later differing ones (notice that this distance is not a real metric!)
id | Identifier to be compared to |
00111 { 00112 int index; 00113 00114 for (index = 0; index < keyLength; index++) { 00115 if (key[index] != id.key[index]) break; 00116 } 00117 00118 return (keyLength - index) * 256 + (key[index] ^ id.key[index]); 00119 }
void I3Identifier::createFromHash | ( | const std::string & | s, | |
const std::string & | o = "" | |||
) |
Creates an identifier from the hash of a string.
s | String to be hashed to form the prefix | |
o | String to be hashed to form the remaining bits |
00127 { 00128 uint8_t temp[20]; 00129 CSHA1 sha1; 00130 int size1, size2; 00131 00132 sha1.Reset(); 00133 sha1.Update((uint8_t*)p.c_str(), p.size()); 00134 sha1.Final(); 00135 sha1.GetHash(temp); 00136 00137 clear(); 00138 size1 = prefixLength / 8; 00139 if (size1 > 20) size1 = 20; 00140 memcpy(key, temp, size1); 00141 00142 name = p + ":0"; 00143 00144 if (o.size() == 0) return; 00145 00146 name = p + ":" + o; 00147 00148 sha1.Reset(); 00149 sha1.Update((uint8_t*)o.c_str(), o.size()); 00150 sha1.Final(); 00151 sha1.GetHash(temp); 00152 00153 clear(); 00154 size2 = (keyLength - prefixLength) / 8; 00155 if (size2 > 20) size2 = 20; 00156 memcpy(key + size1, temp, size2); 00157 }
void I3Identifier::createRandomKey | ( | ) |
void I3Identifier::createRandomPrefix | ( | ) |
00166 { 00167 for (int i = 0; i < prefixLength / 8; i++) { 00168 key[i] = intrand(256); 00169 } 00170 }
void I3Identifier::createRandomSuffix | ( | ) |
00173 { 00174 for (int i = prefixLength / 8; i < keyLength / 8; i++) { 00175 key[i] = intrand(256); 00176 } 00177 }
int I3Identifier::length | ( | ) | const |
OverlayKey I3Identifier::asOverlayKey | ( | ) | const |
Creates an OverlayKey from an identifier, to be used in the overlay underneath.
No hashing is done, the first min(OverlayKey::keySize, keyLength) bits are copied directly.
00122 { 00123 return OverlayKey(key, prefixLength / 8); 00124 }
void I3Identifier::setName | ( | std::string | s | ) |
std::string I3Identifier::getName | ( | ) |
void I3Identifier::initKey | ( | int | prefixL, | |
int | keyL | |||
) | [protected] |
Inits the key with a given prefix length and key length.
prefixL | Prefix length | |
keyL | Key (identifier) length |
00027 { 00028 prefixLength = prefixL; 00029 keyLength = keyL; 00030 key = new unsigned char[keyLength / 8]; 00031 }
std::ostream& operator<< | ( | std::ostream & | os, | |
const I3Identifier & | id | |||
) | [friend] |
String stream output operator.
os | String stream | |
id | I3Identifier to be output |
00193 { 00194 bool allzeros; 00195 const char hex[] = "0123456789abcdef"; 00196 string s0, s1; 00197 00198 if (id.name.length() != 0) { 00199 os << "(" << id.name << ") "; 00200 } 00201 00202 for (int i = 0; i < id.prefixLength / 8; i++) { 00203 os << hex[id.key[i] >> 4]; 00204 os << hex[id.key[i] & 0xf]; 00205 } 00206 os << ':'; 00207 00208 allzeros = true; 00209 for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) { 00210 if (id.key[i] != 0) { 00211 allzeros = false; 00212 break; 00213 } 00214 } 00215 if (allzeros) { 00216 os << "0..."; 00217 } else { 00218 for (int i = id.prefixLength / 8; i < id.keyLength / 8; i++) { 00219 os << hex[id.key[i] >> 4]; 00220 os << hex[id.key[i] & 0xf]; 00221 } 00222 } 00223 return os; 00224 }
unsigned char* I3Identifier::key [protected] |
Identifier bits.
unsigned short I3Identifier::prefixLength [protected] |
Size of prefix in bits.
unsigned short I3Identifier::keyLength [protected] |
Size of identifier in bits.
std::string I3Identifier::name [protected] |