xmlconfig.cpp
Go to the documentation of this file.
1 
13 #include <cstring>
14 
15 #include "xmlconfig.hpp"
16 
17 
18 
19 const char * XmlConfig::retrieveText(std::string node, TiXmlHandle hdl)
20 {
21  TiXmlElement *elem;
22  elem = hdl.FirstChildElement("protocol").FirstChildElement(node.c_str()).Element();
23  if (!elem)
24  {
25  std::cout<<"error: no '"<<node<<"' node in the config file. Aborting."<<std::endl;
26  exit(1);
27  }
28  else
29  {
30  const char * cont = elem->GetText();
31  return cont;
32  }
33 }
34 
35 
36 int XmlConfig::toInteger(std::string node, TiXmlHandle hdl)
37 {
38  int number = atoi(retrieveText(node,hdl));
39  return number;
40 }
41 
42 
43 cryptoTools::SHAx * XmlConfig::string2hash(const char * hashRepresentation)
44 {
45  if (strcmp(hashRepresentation,"SHA-256") == 0)
46  return new cryptoTools::SHA256();
47  else if (strcmp(hashRepresentation,"SHA-384") == 0)
48  return new cryptoTools::SHA384();
49  else if (strcmp(hashRepresentation,"SHA-512") == 0)
50  return new cryptoTools::SHA512();
51  else
52  {
53  std::cout<<"ERROR: in XmlCOnfig.string2hash,"
54  <<"Incorrect hash type: "
55  <<hashRepresentation<<std::endl;
56  exit(1);
57  }
58 }
59 
60 
61 arithm::Group * XmlConfig::unmarshalPGroup(const char * fieldRepresentation)
62 {
63  std::string fieldRepr(fieldRepresentation);
64  unsigned int index=0;
65  std::string fieldByteTree = "";
66  // getting rid of the comment
67  while (fieldRepr[index] != ':')
68  index++;
69  index += 2; // taking care of the second ':'
70 
71  // reading the ByteTree corresponding to the group
72  while (index < fieldRepr.size())
73  {
74  // to suppress spaces
75  if (fieldRepr[index]!=' ')
76  fieldByteTree.push_back(fieldRepr[index]);
77  index++;
78  }
81 
82  // returning a field of the correct type
83  if (bt->getChild(0)->compare("verificatum.arithm.ModPGroup") == 0)
84  return new arithm::ModPGrp(bt->getChild(1));
85  else
86  {
87  std::cout<<"ERROR: in xmlConfig.unmarshalPGroup"
88  <<"pgroup type unkown: "
89  <<bt->getChild(0)->toString()<<"."
90  <<"Aborting."<<std::endl;
91  exit(1);
92  }
93 }
94 
95 
96 XmlConfig::XmlConfig(std::string path2file)
97 {
98  TiXmlDocument doc(path2file.c_str());
99  // Checking if we can open it; otherwise we stop everything.
100  if(!doc.LoadFile())
101  {
102  std::cout<<"Error when loading file"<<std::endl;
103  std::cout<<"error #"<<doc.ErrorId()<<" : "<<doc.ErrorDesc()<<std::endl;
104  exit(1);
105  }
106  // Reading every first level nodes of the file and assigning
107  // the instance's sttribute accordingly.
108  TiXmlHandle hdl(&doc); // it is apparently better to use
109  // handles than pointers and I am a man
110  // of peace, so I do.
111 
112  // reading simple constants
113  nopart = toInteger("nopart" ,hdl);
114  thres = toInteger("thres" ,hdl);
115  maxciph = toInteger("maxciph" ,hdl);
116  vbitlenro = toInteger("vbitlenro",hdl);
117  statdist = toInteger("statdist",hdl);
118  cbitlenro = toInteger("cbitlenro",hdl);
119 
120  // reading rohash
121  const char * ro = retrieveText("rohash",hdl);
122  rohash = string2hash(ro);
123 
124  // reading prghash
125  const char * prgType = retrieveText("prg",hdl);
126  prghash = string2hash(prgType);
127 
128  // reading pgroup
129  const char * fieldRepr = retrieveText("pgroup",hdl);
130  pgroup = unmarshalPGroup(fieldRepr);
131 }
132 
133 
134 unsigned int XmlConfig::getNopart()
135 {
136  return nopart;
137 }
138 
139 
140 unsigned int XmlConfig::getThres()
141 {
142  return thres;
143 }
144 
145 
146 unsigned int XmlConfig::getMaxciph()
147 {
148  return maxciph;
149 }
150 
151 
153 {
154  return vbitlenro;
155 }
156 
157 
159 {
160  return statdist;
161 }
162 
163 
165 {
166  return cbitlenro;
167 }
168 
169 
171 {
172  return rohash;
173 }
174 
175 
177 {
178  return prghash;
179 }
180 
181 
183 {
184  return pgroup;
185 }
186 
187