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-serializer.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-serializer.h
"
10
11
#include "ns3/abort.h"
12
#include "ns3/assert.h"
13
#include "ns3/log.h"
14
15
#include <iostream>
16
17
namespace
ns3
18
{
19
20
NS_LOG_COMPONENT_DEFINE
(
"BitSerializer"
);
21
22
BitSerializer::BitSerializer
()
23
{
24
NS_LOG_FUNCTION
(
this
);
25
m_padAtEnd
=
true
;
26
}
27
28
void
29
BitSerializer::InsertPaddingAtEnd
(
bool
padAtEnd
)
30
{
31
NS_LOG_FUNCTION
(
this
);
32
m_padAtEnd
=
padAtEnd
;
33
}
34
35
void
36
BitSerializer::PadAtStart
()
37
{
38
NS_LOG_FUNCTION
(
this
);
39
40
uint8_t
padding
= 8 - (
m_blob
.size() % 8);
41
42
m_blob
.insert(
m_blob
.begin(),
padding
,
false
);
43
}
44
45
void
46
BitSerializer::PadAtEnd
()
47
{
48
uint8_t
padding
= 8 - (
m_blob
.size() % 8);
49
50
m_blob
.insert(
m_blob
.end(),
padding
,
false
);
51
}
52
53
void
54
BitSerializer::PushBits
(uint64_t
value
, uint8_t
significantBits
)
55
{
56
NS_LOG_FUNCTION
(
this
<<
value
<< +
significantBits
);
57
58
uint64_t mask = 1;
59
mask <<=
significantBits
- 1;
60
61
for
(uint8_t
i
= 0;
i
<
significantBits
;
i
++)
62
{
63
if
(
value
& mask)
64
{
65
m_blob
.push_back(
true
);
66
}
67
else
68
{
69
m_blob
.push_back(
false
);
70
}
71
mask >>= 1;
72
}
73
}
74
75
std::vector<uint8_t>
76
BitSerializer::GetBytes
()
77
{
78
NS_LOG_FUNCTION
(
this
);
79
80
std::vector<uint8_t> result;
81
82
m_padAtEnd
?
PadAtEnd
() :
PadAtStart
();
83
84
for
(
auto
it
=
m_blob
.begin();
it
!=
m_blob
.end();)
85
{
86
uint8_t
tmp
= 0;
87
for
(uint8_t
i
= 0;
i
< 8; ++
i
)
88
{
89
tmp
<<= 1;
90
tmp
|= (*
it
& 1);
91
it
++;
92
}
93
result.push_back(
tmp
);
94
}
95
m_blob
.clear();
96
return
result;
97
}
98
99
uint8_t
100
BitSerializer::GetBytes
(uint8_t* buffer,
uint32_t
size)
101
{
102
NS_LOG_FUNCTION
(
this
<< buffer << size);
103
104
uint8_t
resultLen
= 0;
105
106
m_padAtEnd
?
PadAtEnd
() :
PadAtStart
();
107
108
NS_ABORT_MSG_IF
(
m_blob
.size() <= 8 * size,
109
"Target buffer is too short, "
<<
m_blob
.size() / 8 <<
" bytes needed"
);
110
111
for
(
auto
it
=
m_blob
.begin();
it
!=
m_blob
.end();)
112
{
113
uint8_t
tmp
= 0;
114
for
(uint8_t
i
= 0;
i
< 8; ++
i
)
115
{
116
tmp
<<= 1;
117
tmp
|= (*
it
& 1);
118
it
++;
119
}
120
buffer[
resultLen
] =
tmp
;
121
resultLen
++;
122
}
123
m_blob
.clear();
124
return
resultLen
;
125
}
126
127
}
// namespace ns3
bit-serializer.h
ns3::BitSerializer::m_padAtEnd
bool m_padAtEnd
True if the padding must be added at the end of the blob.
Definition
bit-serializer.h:102
ns3::BitSerializer::PushBits
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
Definition
bit-serializer.cc:54
ns3::BitSerializer::BitSerializer
BitSerializer()
Definition
bit-serializer.cc:22
ns3::BitSerializer::PadAtStart
void PadAtStart()
Add the padding at the start of the blob.
Definition
bit-serializer.cc:36
ns3::BitSerializer::GetBytes
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
Definition
bit-serializer.cc:76
ns3::BitSerializer::m_blob
std::vector< bool > m_blob
Blob of serialized bits.
Definition
bit-serializer.h:101
ns3::BitSerializer::InsertPaddingAtEnd
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
Definition
bit-serializer.cc:29
ns3::BitSerializer::PadAtEnd
void PadAtEnd()
Add the padding at the end of the blob.
Definition
bit-serializer.cc:46
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::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::value
static unsigned int value(char c)
Definition
qkd-encryptor.cc:267
src
network
utils
bit-serializer.cc
Generated on Mon Dec 15 2025 15:22:01 for ns-3 by
1.9.8