Implements the ByteTree virtual class in the case when the data stored is not other bytetrees but actual bytes.
More...
#include <leaf.hpp>
Public Member Functions |
| Leaf (std::vector< uint8_t > content) |
| Initializes the bytes attribute of a new Leaf instance directly.
|
| Leaf (std::string &s) |
| Reads a leaf from a string containing a byte representation and removes the data read from it.
|
| Leaf (mpz_class number) |
| Turns a number into the Leaf of a ByteTree.
|
| Leaf (mpz_class number, unsigned int size) |
| Turns a number into the Leaf of a ByteTree with a fixed number of bytes.
|
| ~Leaf () |
| Empties the content of bytes during destruction.
|
std::string | toString () |
| Turns this object into a string.
|
std::vector< uint8_t > | toVector () |
| Turns this object into a vector of bytes.
|
mpz_class | toNumber () |
std::vector< bool > | toBoolArray () |
| Turns this object into a vector of boolean.
|
bool | isNode () |
| To know if the instance used is a Node.
|
bool | isLeaf () |
| To know if the instance used is a Leaf.
|
mpz_class | toInteger () |
| Turns this object into an arbitrarily large integer.
|
unsigned int | size () |
| The number of bytes in a Leaf and the number of ByteTrees in a Node.
|
void | prettyPrint (std::string indent) |
| Recursively prints an indented version of the ByteTree (mainly for debugging)
|
bool | compare (std::string s) |
| Compare the bytes contained in this leaf with those in the input.
|
| ByteTree () |
| Empty constructor to please the compiler.
|
| ~ByteTree () |
| Empty destructor to please the compiler.
|
virtual void | addChild (ByteTree *bt) |
| Adds the ByteTree given in the argument to the children attribute in a Node; exits with exit code 1 if the ByteTree is a Leaf.
|
virtual ByteTree * | getChild (unsigned int i) |
| Returns the child of index i if we have a Node; exits with exit code 1 if the ByteTree is a Leaf.
|
Private Attributes |
std::vector< uint8_t > | bytes |
| The actual bytes stored in this leaf.
|
Additional Inherited Members |
static ByteTree * | parseString (std::string &s) |
| Parses a string by constructing a ByteTree of the correct kind.
|
static ByteTree * | parseVector (std::vector< uint8_t > &v) |
| Parses a vector of bytes by constructing a ByteTree of the correct kind; returns Leaf(0) if the vector is empty.
|
static ByteTree * | parseFile (std::string path) |
| Parses a file containing the string representation of a bytetree and returns the said bytetree. If the file does not exist, returns NULL.
|
static ByteTree * | string2ByteTree (std::string s) |
| Turns a string into its ByteTree representation. Not to be mistaken with parseString!
|
Detailed Description
Implements the ByteTree virtual class in the case when the data stored is not other bytetrees but actual bytes.
See the verificatum verifier documentation (page 5) for more details on how this is supposed to work.
- See Also
- btrtest.cpp
Definition at line 28 of file leaf.hpp.
Constructor & Destructor Documentation
Leaf::Leaf |
( |
std::vector< uint8_t > |
content | ) |
|
Initializes the bytes attribute of a new Leaf instance directly.
- Parameters
-
content | The bytes this Leaf must contain. |
Definition at line 23 of file leaf.cpp.
Leaf::Leaf |
( |
std::string & |
s | ) |
|
Reads a leaf from a string containing a byte representation and removes the data read from it.
For s="010000000201010000000001AA", creates a leaf containing two bytes equal to 1 and, at the end of the call, s="010000000001AA". It is assumed that the string is a correctly formatted Leaf. No verifications are made!
- Parameters
-
[in,out] | s | The string to parse. It must start with "01". |
Definition at line 46 of file leaf.cpp.
{
if ((s.compare(0,2,"01") != 0) || (s.size()%2 !=0))
{
std::cout<<"ERROR: in Leaf(string), input does not "
"start with '01' or has an odd length"
<<std::endl<<"input: '"<<s<<"'"<<std::endl;
exit(1);
}
else
{
if ((10+l*2) > s.size())
{
std::cout<<"ERROR in Leaf::Leaf(string): the "
"input is too small! Input: "<<s
<<std::endl;
exit(1);
}
else
{
for (unsigned int i=0; i < l; i++)
s[11+i*2]);
s = s.substr(10+l*2,std::string::npos);
}
}
}
Leaf::Leaf |
( |
mpz_class |
number | ) |
|
Turns a number into the Leaf of a ByteTree.
- Parameters
-
number | The number to store in this leaf |
Definition at line 29 of file leaf.cpp.
Leaf::Leaf |
( |
mpz_class |
number, |
|
|
unsigned int |
size |
|
) |
| |
Turns a number into the Leaf of a ByteTree with a fixed number of bytes.
- Parameters
-
number | The number to store in this leaf. |
size | The number of bytes which will actually be stored. |
Definition at line 35 of file leaf.cpp.
{
for (
int i =
bytes.size()-1; i>=0; i--)
{
bytes[i] = number.get_ui() % 0x100;
number = number / 0x100;
}
}
Empties the content of bytes during destruction.
Definition at line 80 of file leaf.cpp.
Member Function Documentation
bool Leaf::compare |
( |
std::string |
s | ) |
|
|
virtual |
Compare the bytes contained in this leaf with those in the input.
Functions (almost) like std::string.compare(): outputs false if the bytes in the leaf correspond to the string given in the input, true otherwise. For instance, Leaf(65,66,67).compare("ABC") == false.
- Parameters
-
- Returns
- False if the string and the bytes are identical, true otherwise. In particular, always return true if we don't have a Leaf.
Reimplemented from verifierUtils::ByteTree.
Definition at line 156 of file leaf.cpp.
{
bool different = (s.size() !=
bytes.size());
if (different)
return true;
else
{
for (unsigned int i=0; i < s.size(); i++)
if ((uint8_t)s[i] !=
bytes[i])
{
different = true;
break;
}
return different;
}
}
To know if the instance used is a Leaf.
- Returns
- true if the type of this object is an instance of the class Leaf, false otherwise.
Reimplemented from verifierUtils::ByteTree.
Definition at line 117 of file leaf.cpp.
To know if the instance used is a Node.
- Returns
- true if the type of this object is an instance of the class Node, false otherwise.
Reimplemented from verifierUtils::ByteTree.
Definition at line 111 of file leaf.cpp.
void Leaf::prettyPrint |
( |
std::string |
indent | ) |
|
|
virtual |
Recursively prints an indented version of the ByteTree (mainly for debugging)
- Parameters
-
indent | The characters to be printed in the indentation. |
Reimplemented from verifierUtils::ByteTree.
Definition at line 147 of file leaf.cpp.
{
std::cout<<indent;
for (
unsigned int i=0; i<
bytes.size(); i++)
std::cout<<std::endl;
}
unsigned int Leaf::size |
( |
| ) |
|
|
virtual |
std::vector< bool > Leaf::toBoolArray |
( |
| ) |
|
|
virtual |
Turns this object into a vector of boolean.
- Returns
- The boolean stored in the bytes if it is a Leaf, exits with exit code 1 otherwise.
Reimplemented from verifierUtils::ByteTree.
Definition at line 132 of file leaf.cpp.
{
std::vector<bool> result(
bytes.size());
for (
unsigned int i=0; i<
bytes.size(); i++)
result[i] = (
bytes[i] == 1);
return result;
}
mpz_class Leaf::toInteger |
( |
| ) |
|
|
virtual |
Turns this object into an arbitrarily large integer.
- Returns
- The integer stored in the bytes if it is a Leaf; 0 otherwise.
Reimplemented from verifierUtils::ByteTree.
Definition at line 123 of file leaf.cpp.
{
mpz_class result = 0;
for (
unsigned int i=0; i<
bytes.size(); i++)
result = (result * 0x100) +
bytes[i];
return result;
}
mpz_class verifierUtils::Leaf::toNumber |
( |
| ) |
|
std::string Leaf::toString |
( |
| ) |
|
|
virtual |
Turns this object into a string.
- Returns
- The string representation of this instance.
Reimplemented from verifierUtils::ByteTree.
Definition at line 86 of file leaf.cpp.
{
std::string result;
std::vector<uint8_t> byteRepresentation =
toVector();
for (unsigned int i=0; i<byteRepresentation.size(); i++)
result +=
byte2str(byteRepresentation[i]);
return result;
}
std::vector< uint8_t > Leaf::toVector |
( |
| ) |
|
|
virtual |
Turns this object into a vector of bytes.
This aim is to allow an easy hashing of the ByteTree's.
- Returns
- The byte representation of the ByteTree.
Reimplemented from verifierUtils::ByteTree.
Definition at line 96 of file leaf.cpp.
{
std::vector<uint8_t> result;
result.push_back(1);
uint32_t byteNumber =
bytes.size();
result.push_back((byteNumber>>24) % 0x100);
result.push_back((byteNumber>>16) % 0x100);
result.push_back((byteNumber>> 8) % 0x100);
result.push_back( byteNumber % 0x100);
result.insert(result.end(),
bytes.begin(),
bytes.end());
return result;
}
Member Data Documentation
std::vector<uint8_t> verifierUtils::Leaf::bytes |
|
private |
The actual bytes stored in this leaf.
Definition at line 34 of file leaf.hpp.
The documentation for this class was generated from the following files: