Markopy
Utilizing Markov Models for brute forcing attacks
edge.h
Go to the documentation of this file.
1 /** @file edge.h
2  * @brief Edge class template
3  * @authors Ata Hakçıl, Osman Ömer Yıldıztugay
4  *
5  * @copydoc Markov::Edge
6  */
7 
8 #pragma once
9 #include <cstdint>
10 #include <cstddef>
11 
12 namespace Markov {
13 
14  template <typename NodeStorageType>
15  class Node;
16 
17  /** @brief Edge class used to link nodes in the model together.
18  *
19  Has LeftNode, RightNode, and EdgeWeight of the edge.
20  Edges are *UNIDIRECTIONAL* in this model. They can only be traversed LeftNode to RightNode.
21  */
22  template <typename NodeStorageType>
23  class Edge {
24  public:
25 
26  /** @brief Default constructor.
27  */
28  Edge<NodeStorageType>();
29 
30  /** @brief Constructor. Initialize edge with given RightNode and LeftNode
31  * @param _left - Left node of this edge.
32  * @param _right - Right node of this edge.
33  *
34  * @b Example @b Use: Construct edge
35  * @code{.cpp}
36  * Markov::Node<unsigned char>* src = new Markov::Node<unsigned char>('a');
37  * Markov::Node<unsigned char>* target1 = new Markov::Node<unsigned char>('b');
38  * Markov::Edge<unsigned char>* e1 = new Markov::Edge<unsigned char>(src, target1);
39  * @endcode
40  *
41  */
42  Edge<NodeStorageType>(Node<NodeStorageType>* _left, Node<NodeStorageType>* _right);
43 
44  /** @brief Adjust the edge EdgeWeight with offset.
45  * Adds the offset parameter to the edge EdgeWeight.
46  * @param offset - NodeValue to be added to the EdgeWeight
47  *
48  * @b Example @b Use: Construct edge
49  * @code{.cpp}
50  * Markov::Node<unsigned char>* src = new Markov::Node<unsigned char>('a');
51  * Markov::Node<unsigned char>* target1 = new Markov::Node<unsigned char>('b');
52  * Markov::Edge<unsigned char>* e1 = new Markov::Edge<unsigned char>(src, target1);
53  *
54  * e1->AdjustEdge(25);
55  *
56  * @endcode
57  */
58  void AdjustEdge(long int offset);
59 
60  /** @brief Traverse this edge to RightNode.
61  * @return Right node. If this is a terminator node, return NULL
62  *
63  *
64  * @b Example @b Use: Traverse a node
65  * @code{.cpp}
66  * Markov::Node<unsigned char>* src = new Markov::Node<unsigned char>('a');
67  * Markov::Node<unsigned char>* target1 = new Markov::Node<unsigned char>('b');
68  * Markov::Edge<unsigned char>* e1 = new Markov::Edge<unsigned char>(src, target1);
69  *
70  * e1->AdjustEdge(25);
71  * Markov::Edge<unsigned char>* e2 = e1->traverseNode();
72  * @endcode
73  *
74  */
75  inline Node<NodeStorageType>* TraverseNode();
76 
77  /** @brief Set LeftNode of this edge.
78  * @param node - Node to be linked with.
79  */
80  void SetLeftEdge (Node<NodeStorageType>*);
81  /** @brief Set RightNode of this edge.
82  * @param node - Node to be linked with.
83  */
84  void SetRightEdge(Node<NodeStorageType>*);
85 
86  /** @brief return edge's EdgeWeight.
87  * @return edge's EdgeWeight.
88  */
89  inline uint64_t EdgeWeight();
90 
91  /** @brief return edge's LeftNode
92  * @return edge's LeftNode.
93  */
94  Node<NodeStorageType>* LeftNode();
95 
96  /** @brief return edge's RightNode
97  * @return edge's RightNode.
98  */
99  inline Node<NodeStorageType>* RightNode();
100 
101  private:
102  /**
103  @brief source node
104  */
105  Node<NodeStorageType>* _left;
106 
107  /**
108  @brief target node
109  */
110  Node<NodeStorageType>* _right;
111 
112  /** @brief
113  Edge Edge Weight
114  */
115  long int _weight;
116  };
117 
118 
119 };
120 
121 //default constructor of edge
122 template <typename NodeStorageType>
123 Markov::Edge<NodeStorageType>::Edge() {
124  this->_left = NULL;
125  this->_right = NULL;
126  this->_weight = 0;
127 }
128 //constructor of edge
129 template <typename NodeStorageType>
130 Markov::Edge<NodeStorageType>::Edge(Markov::Node<NodeStorageType>* _left, Markov::Node<NodeStorageType>* _right) {
131  this->_left = _left;
132  this->_right = _right;
133  this->_weight = 0;
134 }
135 //to AdjustEdge the edges by the edge with its offset
136 template <typename NodeStorageType>
137 void Markov::Edge<NodeStorageType>::AdjustEdge(long int offset) {
138  this->_weight += offset;
139  this->LeftNode()->UpdateTotalVerticeWeight(offset);
140 }
141 //to TraverseNode the node
142 template <typename NodeStorageType>
143 inline Markov::Node<NodeStorageType>* Markov::Edge<NodeStorageType>::TraverseNode() {
144  if (this->RightNode()->NodeValue() == 0xff) //terminator node
145  return NULL;
146  return _right;
147 }
148 //to set the LeftNode of the node
149 template <typename NodeStorageType>
150 void Markov::Edge<NodeStorageType>::SetLeftEdge(Markov::Node<NodeStorageType>* n) {
151  this->_left = n;
152 }
153 //to set the RightNode of the node
154 template <typename NodeStorageType>
155 void Markov::Edge<NodeStorageType>::SetRightEdge(Markov::Node<NodeStorageType>* n) {
156  this->_right = n;
157 }
158 //to get the EdgeWeight of the node
159 template <typename NodeStorageType>
160 inline uint64_t Markov::Edge<NodeStorageType>::EdgeWeight() {
161  return this->_weight;
162 }
163 //to get the LeftNode of the node
164 template <typename NodeStorageType>
165 Markov::Node<NodeStorageType>* Markov::Edge<NodeStorageType>::LeftNode() {
166  return this->_left;
167 }
168 //to get the RightNode of the node
169 template <typename NodeStorageType>
170 inline Markov::Node<NodeStorageType>* Markov::Edge<NodeStorageType>::RightNode() {
171  return this->_right;
172 }