A Discrete-Event Network Simulator
API
bit-deserializer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Universita' di Firenze, Italy
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "bit-deserializer.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/log.h"
24 
25 #include <iostream>
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("BitDeserializer");
31 
33 {
34  NS_LOG_FUNCTION(this);
35  m_deserializing = false;
36 }
37 
38 void
39 BitDeserializer::PushBytes(std::vector<uint8_t> bytes)
40 {
41  NS_LOG_FUNCTION(this << bytes);
42  NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
43  m_bytesBlob.insert(m_bytesBlob.end(), bytes.begin(), bytes.end());
44 }
45 
46 void
47 BitDeserializer::PushBytes(uint8_t* bytes, uint32_t size)
48 {
49  NS_LOG_FUNCTION(this << bytes << size);
50  NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
51  for (uint32_t index = 0; index < size; index++)
52  {
53  m_bytesBlob.push_back(bytes[index]);
54  }
55 }
56 
57 void
59 {
60  NS_LOG_FUNCTION(this << +byte);
61  NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
62  m_bytesBlob.push_back(byte);
63 }
64 
65 uint64_t
67 {
68  NS_LOG_FUNCTION(this << +size);
69  uint8_t result = 0;
71 
72  NS_ABORT_MSG_IF(size > 64, "Number of requested bits exceeds 64");
73  NS_ABORT_MSG_IF(size > m_blob.size(), "Number of requested bits exceeds blob size");
74 
75  for (uint8_t i = 0; i < size; i++)
76  {
77  result <<= 1;
78  result |= m_blob.front();
79  m_blob.pop_front();
80  }
81  return result;
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION(this);
88  if (!m_deserializing)
89  {
90  m_deserializing = true;
91  for (auto index = m_bytesBlob.begin(); index != m_bytesBlob.end(); index++)
92  {
93  m_blob.push_back(*index & 0x80);
94  m_blob.push_back(*index & 0x40);
95  m_blob.push_back(*index & 0x20);
96  m_blob.push_back(*index & 0x10);
97  m_blob.push_back(*index & 0x8);
98  m_blob.push_back(*index & 0x4);
99  m_blob.push_back(*index & 0x2);
100  m_blob.push_back(*index & 0x1);
101  }
102  }
103 }
104 
105 } // namespace ns3
void PushBytes(std::vector< uint8_t > bytes)
Pushes some bytes into the blob to be deserialized.
std::vector< uint8_t > m_bytesBlob
Blob of bytes to be deserialized.
void PrepareDeserialization()
Prepare the byte array to the deserialization.
void PushByte(uint8_t byte)
Pushes one byte into the blob to be deserialized.
bool m_deserializing
True if the deserialization did start already.
std::deque< bool > m_blob
Blob of bits ready to be deserialized.
uint64_t GetBits(uint8_t size)
Pops a given number of bits from the blob front.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.