leaf.cpp
Go to the documentation of this file.
1 
11 #include <iomanip>
12 #include <iostream>
13 #include <sstream>
14 #include <cstdlib>
15 
16 #include "leaf.hpp"
17 #include "../verifierutils.hpp"
18 
19 
20 using namespace verifierUtils;
21 
22 
23 Leaf::Leaf(std::vector<uint8_t> content)
24 {
25  bytes = content;
26 }
27 
28 
29 Leaf::Leaf(mpz_class number)
30 {
31  bytes = largeNum2byteVector(number);
32 }
33 
34 
35 Leaf::Leaf(mpz_class number, unsigned int size)
36 {
37  bytes.resize(size);
38  for (int i = bytes.size()-1; i>=0; i--)
39  {
40  bytes[i] = number.get_ui() % 0x100;
41  number = number / 0x100;
42  }
43 }
44 
45 
46 Leaf::Leaf(std::string &s)
47 {
48  if ((s.compare(0,2,"01") != 0) || (s.size()%2 !=0))
49  {
50  std::cout<<"ERROR: in Leaf(string), input does not "
51  "start with '01' or has an odd length"
52  <<std::endl<<"input: '"<<s<<"'"<<std::endl;
53  exit(1);
54  }
55  else
56  {
57  // reading number of bytes
58  unsigned int l = octuple2num(s.substr(2,8));
59  if ((10+l*2) > s.size())
60  {
61  std::cout<<"ERROR in Leaf::Leaf(string): the "
62  "input is too small! Input: "<<s
63  <<std::endl;
64  exit(1);
65  }
66  else
67  {
68  bytes.resize(l);
69  // reading actual bytes
70  for (unsigned int i=0; i < l; i++)
71  bytes[i] = doublon2byte(s[10+i*2],
72  s[11+i*2]);
73  // cutting what we just read from the string
74  s = s.substr(10+l*2,std::string::npos);
75  }
76  }
77 }
78 
79 
81 {
82  bytes.clear();
83 }
84 
85 
86 std::string Leaf::toString()
87 {
88  std::string result;
89  std::vector<uint8_t> byteRepresentation = toVector();
90  for (unsigned int i=0; i<byteRepresentation.size(); i++)
91  result += byte2str(byteRepresentation[i]);
92  return result;
93 }
94 
95 
96 std::vector<uint8_t> Leaf::toVector()
97 {
98  std::vector<uint8_t> result;
99  result.push_back(1); // Node starts with a 0x01 byte
100  // Adding the number of bytes
101  uint32_t byteNumber = bytes.size();
102  result.push_back((byteNumber>>24) % 0x100);
103  result.push_back((byteNumber>>16) % 0x100);
104  result.push_back((byteNumber>> 8) % 0x100);
105  result.push_back( byteNumber % 0x100);
106  // Adding the bytes contained in the leaf
107  result.insert(result.end(),bytes.begin(), bytes.end());
108  return result;
109 }
110 
112 {
113  return false;
114 }
115 
116 
118 {
119  return true;
120 }
121 
122 
123 mpz_class Leaf::toInteger()
124 {
125  mpz_class result = 0;
126  for (unsigned int i=0; i<bytes.size(); i++)
127  result = (result * 0x100) + bytes[i];
128  return result;
129 }
130 
131 
132 std::vector<bool> Leaf::toBoolArray()
133 {
134  std::vector<bool> result(bytes.size());
135  for (unsigned int i=0; i<bytes.size(); i++)
136  result[i] = (bytes[i] == 1);
137  return result;
138 }
139 
140 
141 unsigned int Leaf::size()
142 {
143  return bytes.size();
144 }
145 
146 
147 void Leaf::prettyPrint(std::string indent)
148 {
149  std::cout<<indent;
150  for (unsigned int i=0; i<bytes.size(); i++)
151  std::cout<<verifierUtils::byte2str(bytes[i]);
152  std::cout<<std::endl;
153 }
154 
155 
156 bool Leaf::compare(std::string s)
157 {
158  bool different = (s.size() != bytes.size());
159  if (different)
160  return true;
161  else
162  {
163  for (unsigned int i=0; i < s.size(); i++)
164  if ((uint8_t)s[i] != bytes[i])
165  {
166  different = true;
167  break;
168  }
169  return different;
170  }
171 }
172