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