node.cpp
Go to the documentation of this file.
1 
11 #include <sstream>
12 #include <iomanip>
13 #include <cstdlib>
14 
15 #include "node.hpp"
16 #include "../verifierutils.hpp"
17 
18 using namespace verifierUtils;
19 
20 
21 
23 {
24 }
25 
26 
28 {
29  for (unsigned int i=0; i<children.size(); i++)
30  free(children[i]);
31 }
32 
33 
34 Node::Node(std::string &s)
35 {
36  if ((s.compare(0,2,"00") != 0) || (s.size()%2 !=0))
37  {
38  std::cout<<"ERROR: in Node(string), input does not "
39  "start with '00' or has an odd length"
40  <<std::endl<<"input: '"<<s<<"'"<<std::endl;
41  exit(1);
42  }
43  else
44  {
45  // reading number of children
46  unsigned int l = octuple2num(s.substr(2,8));
47  children.resize(l);
48  s = s.substr(10,std::string::npos);
49  for (unsigned int i=0; i<l; i++)
51  }
52 }
53 
54 
55 Node::Node(std::vector<uint8_t> &v)
56 {
57  if (v[0] != 0)
58  {
59  std::cout<<"ERROR: in Node(vector), input does not "
60  "start with 0x0.\nv= " <<std::endl;
61  for (unsigned int i=0; i<v.size(); i++)
62  std::cout<<std::hex<<v[i]<<",";
63  std::cout<<std::endl;
64  exit(1);
65  }
66  else
67  {
68  // reading number of children
69  unsigned int l = v[1]*0x1000000
70  + v[2]*0x10000
71  + v[3]*0x100
72  + v[4];
73  children.resize(l);
74  v.assign(v.begin()+5, v.end());
75  for (unsigned int i=0; i<l; i++)
77  }
78 }
79 
80 
81 std::string Node::toString()
82 {
83  std::string result;
84  std::vector<uint8_t> bytes = toVector();
85  for (unsigned int i=0; i<bytes.size(); i++)
86  result += byte2str(bytes[i]);
87  return result;
88 }
89 
90 
91 std::vector<uint8_t> Node::toVector()
92 {
93  std::vector<uint8_t> result;
94  result.push_back(0); // Node starts with a 0x00 byte
95  // Adding the number of children
96  uint32_t byteNumber = children.size();
97  result.push_back((byteNumber>>24) % 0x100);
98  result.push_back((byteNumber>>16) % 0x100);
99  result.push_back((byteNumber>> 8) % 0x100);
100  result.push_back( byteNumber % 0x100);
101  // Adding the byte representation of the children
102  for (unsigned int i=0; i<children.size(); i++)
103  {
104  std::vector<uint8_t> bytesChild = children[i]->toVector();
105  result.insert(result.end(),bytesChild.begin(), bytesChild.end());
106  }
107  return result;
108 }
109 
110 
112 {
113  return true;
114 }
115 
116 
118 {
119  return false;
120 }
121 
122 
123 unsigned int Node::size()
124 {
125  return children.size();
126 }
127 
128 
129 void Node::prettyPrint(std::string indent)
130 {
131  std::cout<<indent<<num2str(children.size())<<std::endl;
132  for (unsigned int i=0; i<children.size(); i++)
133  children[i]->prettyPrint(indent+" ");
134  std::cout<<std::endl;
135 }
136 
137 
139 {
140  children.push_back(bt);
141 }
142 
143 
144 ByteTree * Node::getChild(unsigned int i)
145 {
146  return children[i];
147 }