Remove dynamic exceptions
[anna.git] / source / core / util / EncodedData.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/util/EncodedData.hpp>
10 #include <anna/core//DataBlock.hpp>
11
12 #include <anna/xml/Node.hpp>
13 #include <anna/xml/Attribute.hpp>
14
15 #include <anna/core/util/Tokenizer.hpp>
16
17 using namespace std;
18 using namespace anna;
19
20 void EncodedData::initialize(const xml::Node* parent)
21 noexcept(false) {
22   if(fromBCD(parent->find("Key1")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
23     throw RuntimeException("anna::EncodedData::initialize | Key1 no valida", ANNA_FILE_LOCATION);
24
25   anna_memcpy(&a_skey[0], a_value.getData(), sizeof(DES_key_schedule));
26
27   if(fromBCD(parent->find("Key2")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
28     throw RuntimeException("anna::EncodedData::initialize | Key2 no valida", ANNA_FILE_LOCATION);
29
30   anna_memcpy(&a_skey[1], a_value.getData(), sizeof(DES_key_schedule));
31
32   if(fromBCD(parent->find("Key3")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_key_schedule))
33     throw RuntimeException("anna::EncodedData::initialize | Key3 no valida", ANNA_FILE_LOCATION);
34
35   anna_memcpy(&a_skey[2], a_value.getData(), sizeof(DES_key_schedule));
36
37   if(fromBCD(parent->find("IVector")->getAttribute("Value")->getValue(), a_value) != sizeof(DES_cblock))
38     throw RuntimeException("anna::EncodedData::initialize | IVector no valida", ANNA_FILE_LOCATION);
39
40   anna_memcpy(&a_iv, a_value.getData(), sizeof(DES_cblock));
41   const xml::Node* node = parent->find("DataBlock");
42   a_realSize = node->getAttribute("Length")->getIntegerValue();
43   fromBCD(node->getAttribute("Value")->getValue(), a_value);
44 }
45
46 xml::Node* EncodedData::asXML(xml::Node* parent) const
47 noexcept(false) {
48   parent->createAttribute("Mode", "DES3");
49   xml::Node* node;
50   string aux;
51   DataBlock key1((char*) &a_skey[0], sizeof(DES_key_schedule), false);
52   parent->createChild("Key1")->createAttribute("Value", asBCD(key1, aux));
53   DataBlock key2((char*) &a_skey[1], sizeof(DES_key_schedule), false);
54   parent->createChild("Key2")->createAttribute("Value", asBCD(key2, aux));
55   DataBlock key3((char*) &a_skey[2], sizeof(DES_key_schedule), false);
56   parent->createChild("Key3")->createAttribute("Value", asBCD(key3, aux));
57   DataBlock iv((char*) &a_iv, sizeof(DES_cblock), false);
58   parent->createChild("IVector")->createAttribute("Value", asBCD(iv, aux));
59   node = parent->createChild("DataBlock");
60   node->createAttribute("Length", a_realSize);
61   node->createAttribute("Value", asBCD(a_value, aux));
62   return parent;
63 }
64
65 const std::string& EncodedData::asBCD(const DataBlock& source, string& result)
66 {
67   result = "";
68   const char* data = source.getData();
69   int c;
70   int byte;
71
72   for(int len = 0, maxlen = source.getSize(); len < maxlen; len ++, data ++) {
73     c = *data;
74     byte = (c & 0xf0) >> 4;
75     result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
76     byte = c & 0x0f;
77     result += (byte <= 9) ? (byte + '0') : (byte - 0x0a + 'a');
78   }
79
80   return result;
81 }
82
83 int EncodedData::fromBCD(const string& source, DataBlock& result)
84 {
85   result.clear();
86   const char* data = source.c_str();
87   int len = 0, maxlen = source.length();
88   int c;
89   int byte;
90
91   while(len < maxlen) {
92     c = *data ++;
93     byte = (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
94     byte <<= 4;
95
96     if(++ len < maxlen) {
97       c = *data ++;
98       byte |= (c <= '9') ? (c - '0') : (c - 'a' + 0x0a);
99     }
100
101     result += char(byte);
102     len ++;
103   }
104
105   return result.getSize();
106 }
107
108
109