A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
16
namespace
ns3
17
{
18
19
NS_LOG_COMPONENT_DEFINE
(
"BitDeserializer"
);
20
21
BitDeserializer::BitDeserializer
()
22
{
23
NS_LOG_FUNCTION
(
this
);
24
m_deserializing
=
false
;
25
}
26
27
void
28
BitDeserializer::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
35
void
36
BitDeserializer::PushBytes
(uint8_t* bytes,
uint32_t
size)
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
46
void
47
BitDeserializer::PushByte
(uint8_t
byte
)
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
54
uint64_t
55
BitDeserializer::GetBits
(uint8_t size)
56
{
57
NS_LOG_FUNCTION
(
this
<< +size);
58
uint8_t result = 0;
59
PrepareDeserialization
();
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
73
void
74
BitDeserializer::PrepareDeserialization
()
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
bit-deserializer.h
ns3::BitDeserializer::PushBytes
void PushBytes(std::vector< uint8_t > bytes)
Pushes some bytes into the blob to be deserialized.
Definition
bit-deserializer.cc:28
ns3::BitDeserializer::m_bytesBlob
std::vector< uint8_t > m_bytesBlob
Blob of bytes to be deserialized.
Definition
bit-deserializer.h:75
ns3::BitDeserializer::PrepareDeserialization
void PrepareDeserialization()
Prepare the byte array to the deserialization.
Definition
bit-deserializer.cc:74
ns3::BitDeserializer::PushByte
void PushByte(uint8_t byte)
Pushes one byte into the blob to be deserialized.
Definition
bit-deserializer.cc:47
ns3::BitDeserializer::m_deserializing
bool m_deserializing
True if the deserialization did start already.
Definition
bit-deserializer.h:76
ns3::BitDeserializer::m_blob
std::deque< bool > m_blob
Blob of bits ready to be deserialized.
Definition
bit-deserializer.h:74
ns3::BitDeserializer::BitDeserializer
BitDeserializer()
Definition
bit-deserializer.cc:21
ns3::BitDeserializer::GetBits
uint64_t GetBits(uint8_t size)
Pops a given number of bits from the blob front.
Definition
bit-deserializer.cc:55
uint32_t
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition
abort.h:97
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:229
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
network
utils
bit-deserializer.cc
Generated on Fri Nov 8 2024 13:59:05 for ns-3 by
1.11.0