verifierUtils Namespace Reference

Contains several classes and functions used throughout the whole verifier, in particular the ByteTree. More...

Classes

class  ByteTree
 A virtual class providing a partial common interface for Node and Leaf. More...
class  Leaf
 Implements the ByteTree virtual class in the case when the data stored is not other bytetrees but actual bytes. More...
class  Node
 Inherits from the ByteTree and implements the case where the ByteTree has children. More...

Functions

std::string num2str (unsigned int number)
 Turns a number into a string of length 8 containing its hexdecimal representation. It is filled on the left with '0'.
std::string byte2str (uint8_t b)
 Turns a byte into a two characters string.
uint8_t doublon2byte (char b1, char b2)
 Turns two char representing a byte into the corresponding unsigned byte.
uint32_t octuple2num (std::string s)
 Turns a string of length 8 into a number of bitlength 32.
std::vector< uint8_t > largeNum2byteVector (mpz_class number)
 Turns an arbitrary large number into a vector containing bytes representing it. For instance, returns [0x1,0x0] if given 256.
std::vector< uint8_t > file2bytes (std::string path)
 Returns the content of a file as a vector of bytes.

Detailed Description

Contains several classes and functions used throughout the whole verifier, in particular the ByteTree.

Classes in this module are of general utility but do not have much in common apart from that. See their respective documentation for more information.

Function Documentation

std::string verifierUtils::byte2str ( uint8_t  b)

Turns a byte into a two characters string.

Parameters
bThe byte to transform.
Returns
The string representation of the byte.

Definition at line 33 of file verifierutils.cpp.

{
std::string r;
switch ((b/0x10) % 0x10)
{
case (0x0): { r = '0'; break; }
case (0x1): { r = '1'; break; }
case (0x2): { r = '2'; break; }
case (0x3): { r = '3'; break; }
case (0x4): { r = '4'; break; }
case (0x5): { r = '5'; break; }
case (0x6): { r = '6'; break; }
case (0x7): { r = '7'; break; }
case (0x8): { r = '8'; break; }
case (0x9): { r = '9'; break; }
case (0xa): { r = 'a'; break; }
case (0xb): { r = 'b'; break; }
case (0xc): { r = 'c'; break; }
case (0xd): { r = 'd'; break; }
case (0xe): { r = 'e'; break; }
case (0xf): { r = 'f'; break; }
}
switch (b%0x10)
{
case (0x0): { r += '0'; break; }
case (0x1): { r += '1'; break; }
case (0x2): { r += '2'; break; }
case (0x3): { r += '3'; break; }
case (0x4): { r += '4'; break; }
case (0x5): { r += '5'; break; }
case (0x6): { r += '6'; break; }
case (0x7): { r += '7'; break; }
case (0x8): { r += '8'; break; }
case (0x9): { r += '9'; break; }
case (0xa): { r += 'a'; break; }
case (0xb): { r += 'b'; break; }
case (0xc): { r += 'c'; break; }
case (0xd): { r += 'd'; break; }
case (0xe): { r += 'e'; break; }
case (0xf): { r += 'f'; break; }
}
return r;
}
uint8_t verifierUtils::doublon2byte ( char  b1,
char  b2 
)

Turns two char representing a byte into the corresponding unsigned byte.

For instance:

  • doublon2byte("01") = 1
  • doublon2byte("10") = 16
Parameters
b1The first char two read.
b2The second to read.
Returns
The unsigned byte stored in the bytes b1 and b2.

Definition at line 78 of file verifierutils.cpp.

{
uint8_t r = 0;
switch (b2)
{
case ('0'): { r = 0x0 ; break; }
case ('1'): { r = 0x1 ; break; }
case ('2'): { r = 0x2 ; break; }
case ('3'): { r = 0x3 ; break; }
case ('4'): { r = 0x4 ; break; }
case ('5'): { r = 0x5 ; break; }
case ('6'): { r = 0x6 ; break; }
case ('7'): { r = 0x7 ; break; }
case ('8'): { r = 0x8 ; break; }
case ('9'): { r = 0x9 ; break; }
case ('a'): { r = 0xa; break; }
case ('A'): { r = 0xa; break; }
case ('b'): { r = 0xb; break; }
case ('B'): { r = 0xb; break; }
case ('c'): { r = 0xc; break; }
case ('C'): { r = 0xc; break; }
case ('d'): { r = 0xd; break; }
case ('D'): { r = 0xd; break; }
case ('e'): { r = 0xe; break; }
case ('E'): { r = 0xe; break; }
case ('f'): { r = 0xf; break; }
case ('F'): { r = 0xf; break; }
default:
{
std::cout<<"ERROR: in verifierUtils::doublon2byte(): "
"unknown character ("<<b1<<","<<b2<<"). "
"Aborting."<<std::endl;
exit(1);
}
} // end switch on b2
switch (b1)
{
case ('0'): { r += 0x00 ; break; }
case ('1'): { r += 0x10 ; break; }
case ('2'): { r += 0x20 ; break; }
case ('3'): { r += 0x30 ; break; }
case ('4'): { r += 0x40 ; break; }
case ('5'): { r += 0x50 ; break; }
case ('6'): { r += 0x60 ; break; }
case ('7'): { r += 0x70 ; break; }
case ('8'): { r += 0x80 ; break; }
case ('9'): { r += 0x90 ; break; }
case ('a'): { r += 0xa0 ; break; }
case ('A'): { r += 0xa0 ; break; }
case ('b'): { r += 0xb0 ; break; }
case ('B'): { r += 0xb0 ; break; }
case ('c'): { r += 0xc0 ; break; }
case ('C'): { r += 0xc0 ; break; }
case ('d'): { r += 0xd0 ; break; }
case ('D'): { r += 0xd0 ; break; }
case ('e'): { r += 0xe0 ; break; }
case ('E'): { r += 0xe0 ; break; }
case ('f'): { r += 0xf0 ; break; }
case ('F'): { r += 0xf0 ; break; }
default:
{
std::cout<<"ERROR: in verifierUtils::doublon2byte(): "
"unknown character ("<<b1<<","<<b2<<"). "
"Aborting."<<std::endl;
exit(1);
}
} // end switch on b1
return r;
}
std::vector< uint8_t > verifierUtils::file2bytes ( std::string  path)

Returns the content of a file as a vector of bytes.

Parameters
pathThe path to the file to read.
Returns
A byte representation of the content of the file.

Definition at line 182 of file verifierutils.cpp.

{
std::ifstream content(path);
uint8_t character;
std::vector<uint8_t> result;
while (content.good())
{
character = content.get();
if (content.good())
result.push_back(character);
}
content.close();
return result;
}
std::vector< uint8_t > verifierUtils::largeNum2byteVector ( mpz_class  number)

Turns an arbitrary large number into a vector containing bytes representing it. For instance, returns [0x1,0x0] if given 256.

Parameters
numberThe number to turn into bytes.
Returns
The byte array corresponding to number.

Definition at line 167 of file verifierutils.cpp.

{
std::vector<uint8_t> bytes;
mpz_class prov = number;
bytes.resize(((unsigned int)mpz_sizeinbase(prov.get_mpz_t(),16)+1)/2);
for (int i = bytes.size()-1; i>=0; i--)
{
bytes[i] = prov.get_ui() % 0x100;
prov = prov / 0x100;
}
return bytes;
}
std::string verifierUtils::num2str ( unsigned int  number)

Turns a number into a string of length 8 containing its hexdecimal representation. It is filled on the left with '0'.

Parameters
numberThe number to turn into a string.
Returns
The string representation of number.

Definition at line 21 of file verifierutils.cpp.

{
std::string result;
std::ostringstream oss;
oss<<std::hex<<number;
result = oss.str();
for (unsigned int i=result.size(); i<8; i++)
result = "0"+result;
return result;
}
uint32_t verifierUtils::octuple2num ( std::string  s)

Turns a string of length 8 into a number of bitlength 32.

Parameters
sAn hexadecimal representation of a number of length 8.
Returns
The number represented in the string.

Definition at line 149 of file verifierutils.cpp.

{
if (s.size() != 8)
{
std::cout<<"ERROR in verifierUtils::octople2num: input"
" is not of length 8: "<<s<<std::endl;
exit(1);
}
else
{
uint32_t l = (doublon2byte(s[0],s[1]) * 0x1000000)
+ (doublon2byte(s[2],s[3]) * 0x10000)
+ (doublon2byte(s[4],s[5]) * 0x100)
+ doublon2byte(s[6],s[7]);
return l;
}
}