verifierUtils::ByteTree Class Reference

A virtual class providing a partial common interface for Node and Leaf. More...

#include <bytetree.hpp>

+ Inheritance diagram for verifierUtils::ByteTree:
+ Collaboration diagram for verifierUtils::ByteTree:

Public Member Functions

 ByteTree ()
 Empty constructor to please the compiler.
 ~ByteTree ()
 Empty destructor to please the compiler.
virtual bool isLeaf ()
 To know if the instance used is a Leaf.
virtual bool isNode ()
 To know if the instance used is a Node.
virtual std::string toString ()
 Turns this object into a string.
virtual std::vector< uint8_t > toVector ()
 Turns this object into a vector of bytes.
virtual mpz_class toInteger ()
 Turns this object into an arbitrarily large integer.
virtual std::vector< bool > toBoolArray ()
 Turns this object into a vector of boolean.
virtual unsigned int size ()
 The number of bytes in a Leaf and the number of ByteTrees in a Node.
virtual void prettyPrint (std::string indent)
 Recursively prints an indented version of the ByteTree (mainly for debugging)
virtual bool compare (std::string s)
 Compare the bytes contained in this leaf with those in the input.
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 ByteTreegetChild (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.

Static Public Member Functions

static ByteTreeparseString (std::string &s)
 Parses a string by constructing a ByteTree of the correct kind.
static ByteTreeparseVector (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 ByteTreeparseFile (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 ByteTreestring2ByteTree (std::string s)
 Turns a string into its ByteTree representation. Not to be mistaken with parseString!

Detailed Description

A virtual class providing a partial common interface for Node and Leaf.

Look at the verificatum verifier specification (page 5) for a detailed, well, specification of how the ByteTree should behave. To stick close to the spec', we use to distinct classes for the nodes and the leaves but, in order for them to be easily used, we provide a common interface for them via the virtual ByteTree class.

See Also
Node
Leaf
btrtest.cpp

Definition at line 39 of file bytetree.hpp.

Constructor & Destructor Documentation

ByteTree::ByteTree ( )

Empty constructor to please the compiler.

Definition at line 21 of file bytetree.cpp.

{
}
ByteTree::~ByteTree ( )

Empty destructor to please the compiler.

Definition at line 26 of file bytetree.cpp.

{
}

Member Function Documentation

void ByteTree::addChild ( ByteTree bt)
virtual

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.

Parameters
btThe ByteTree to add.

Reimplemented in verifierUtils::Node.

Definition at line 165 of file bytetree.cpp.

{
std::cout<<"ERROR: in ByteTree::addChild(): trying to access "
"the children of a non-Node ByteTree. Aborting."
<<std::endl;
std::cout<<std::endl;
exit(1);
}
bool ByteTree::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
sThe string to compare.
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 in verifierUtils::Leaf.

Definition at line 159 of file bytetree.cpp.

{
return true;
}
ByteTree * ByteTree::getChild ( unsigned int  i)
virtual

Returns the child of index i if we have a Node; exits with exit code 1 if the ByteTree is a Leaf.

Returns
The ith element of the children attribute.

Reimplemented in verifierUtils::Node.

Definition at line 176 of file bytetree.cpp.

{
std::cout<<"ERROR: in ByteTree::getChild(): trying to access "
"the children of a non-Node ByteTree. Aborting."
<<std::endl;
std::cout<<std::endl;
exit(1);
}
bool ByteTree::isLeaf ( )
virtual

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 in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 31 of file bytetree.cpp.

{
return false;
}
bool ByteTree::isNode ( )
virtual

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 in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 37 of file bytetree.cpp.

{
return false;
}
ByteTree * ByteTree::parseFile ( std::string  path)
static

Parses a file containing the string representation of a bytetree and returns the said bytetree. If the file does not exist, returns NULL.

Parameters
pathThe path to the file storing the ByteTree.

Definition at line 121 of file bytetree.cpp.

{
std::vector<uint8_t> content;
std::ifstream file(path);
uint8_t c;
while (file.good())
{
c = file.get();
if (file.good())
content.push_back(c);
}
if (content.size() == 0)
return NULL;
else
return parseVector(content);
}
ByteTree * ByteTree::parseString ( std::string &  s)
static

Parses a string by constructing a ByteTree of the correct kind.

Returns a pointer to a Node if the input starts with "00" or a pointer to a Leaf otherwise. The data actually read from the string is removed from it, the idea being to easily go through it recursively.

Parameters
[in,out]sThe string to parse.
Returns
A pointer to a ByteTree containing the same information as in the input.

Definition at line 74 of file bytetree.cpp.

{
ByteTree * res;
if (s.compare(0,2,"01") == 0)
res = new Leaf(s);
else if (s.compare(0,2,"00") == 0)
res = new Node(s);
else
{
std::cout<<"ERROR in ByteTree::parseString(): string "
"does not correspond to a bytetree! Here is the"
" problematic string: "<<s<<std::endl;
exit(1);
}
return res;
}
ByteTree * ByteTree::parseVector ( std::vector< uint8_t > &  v)
static

Parses a vector of bytes by constructing a ByteTree of the correct kind; returns Leaf(0) if the vector is empty.

Returns a pointer to a Node if the input starts with 0x0 or a pointer to a Leaf otherwise. The data actually read from the vector is removed from it, the idea being to easily go through it recursively.

Parameters
[in,out]vThe vector to parse.
Returns
A pointer to a ByteTree containing the same information as in the input.

Definition at line 92 of file bytetree.cpp.

{
ByteTree * res;
if (v[0] == 1)
{
unsigned int l = v[1] * 0x1000000
+ v[2] * 0x10000
+ v[3] * 0x100
+ v[4];
std::vector<uint8_t> content(v.begin()+5,v.begin()+l+5);
res = new Leaf(content);
v.assign(v.begin()+l+5,v.end());
}
else if (v[0] == 0)
res = new Node(v);
else
{
std::cout<<"ERROR in ByteTree::parseVector(): vector "
"does not correspond to a bytetree! Here is the"
" problematic vector:\nv= ";
for (unsigned int i=0; i<v.size(); i++)
std::cout<<v[i]<<", ";
std::cout<<std::endl;
exit(1);
}
return res;
}
void ByteTree::prettyPrint ( std::string  indent)
virtual

Recursively prints an indented version of the ByteTree (mainly for debugging)

Parameters
indentThe characters to be printed in the indentation.

Reimplemented in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 154 of file bytetree.cpp.

{
}
unsigned int ByteTree::size ( )
virtual

The number of bytes in a Leaf and the number of ByteTrees in a Node.

Returns
The size of this instance.

Reimplemented in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 148 of file bytetree.cpp.

{
return 0;
}
ByteTree * ByteTree::string2ByteTree ( std::string  s)
static

Turns a string into its ByteTree representation. Not to be mistaken with parseString!

Returns a pointer to Leaf containing the bytes of the string. For instance, string2ByteTree("ABCD") returns a leaf containing [0x65,0x66,0x67,0x68].

Parameters
sThe string to turn into a ByteTree
Returns
A pointer to the ByteTree representing s.

Definition at line 139 of file bytetree.cpp.

{
std::vector<uint8_t> content(s.size());
for (unsigned int i=0; i<s.size(); i++)
content[i] = s[i];
return new Leaf(content);
}
std::vector< bool > ByteTree::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 in verifierUtils::Leaf.

Definition at line 63 of file bytetree.cpp.

{
std::cout<<"ERROR: in ByteTree::toBoolArray(): trying to turn a"
<<" non-leaf into an array of booleans.\nAborting."
<<std::endl;
std::cout<<std::endl;
exit(1);
}
mpz_class ByteTree::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 in verifierUtils::Leaf.

Definition at line 50 of file bytetree.cpp.

{
return 0;
}
std::string ByteTree::toString ( )
virtual

Turns this object into a string.

Returns
The string representation of this instance.

Reimplemented in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 43 of file bytetree.cpp.

{
std::string emptyString;
return emptyString;
}
std::vector< uint8_t > ByteTree::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 in verifierUtils::Leaf, and verifierUtils::Node.

Definition at line 56 of file bytetree.cpp.

{
std::vector<uint8_t> emptyVector;
return emptyVector;
}

The documentation for this class was generated from the following files: