A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
bit-deserializer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "bit-deserializer.h"
10
11#include "ns3/abort.h"
12#include "ns3/log.h"
13
14#include <iostream>
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("BitDeserializer");
20
26
27void
28BitDeserializer::PushBytes(std::vector<uint8_t> bytes)
29{
30 NS_LOG_FUNCTION(this << bytes);
31 NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
32 m_bytesBlob.insert(m_bytesBlob.end(), bytes.begin(), bytes.end());
33}
34
35void
37{
38 NS_LOG_FUNCTION(this << bytes << size);
39 NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
40 for (uint32_t index = 0; index < size; index++)
41 {
42 m_bytesBlob.push_back(bytes[index]);
43 }
44}
45
46void
48{
49 NS_LOG_FUNCTION(this << +byte);
50 NS_ABORT_MSG_IF(m_deserializing, "Can't add bytes after deserialization started");
51 m_bytesBlob.push_back(byte);
52}
53
54uint64_t
56{
57 NS_LOG_FUNCTION(this << +size);
58 uint8_t result = 0;
60
61 NS_ABORT_MSG_IF(size > 64, "Number of requested bits exceeds 64");
62 NS_ABORT_MSG_IF(size > m_blob.size(), "Number of requested bits exceeds blob size");
63
64 for (uint8_t i = 0; i < size; i++)
65 {
66 result <<= 1;
67 result |= m_blob.front();
68 m_blob.pop_front();
69 }
70 return result;
71}
72
73void
75{
76 NS_LOG_FUNCTION(this);
77 if (!m_deserializing)
78 {
79 m_deserializing = true;
80 for (auto index = m_bytesBlob.begin(); index != m_bytesBlob.end(); index++)
81 {
82 m_blob.push_back(*index & 0x80);
83 m_blob.push_back(*index & 0x40);
84 m_blob.push_back(*index & 0x20);
85 m_blob.push_back(*index & 0x10);
86 m_blob.push_back(*index & 0x8);
87 m_blob.push_back(*index & 0x4);
88 m_blob.push_back(*index & 0x2);
89 m_blob.push_back(*index & 0x1);
90 }
91 }
92}
93
94} // 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:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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.