A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
bit-serializer.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-serializer.h
"
21
22
#include "ns3/abort.h"
23
#include "ns3/assert.h"
24
#include "ns3/log.h"
25
26
#include <iostream>
27
28
namespace
ns3
29
{
30
31
NS_LOG_COMPONENT_DEFINE
(
"BitSerializer"
);
32
33
BitSerializer::BitSerializer
()
34
{
35
NS_LOG_FUNCTION
(
this
);
36
m_padAtEnd
=
true
;
37
}
38
39
void
40
BitSerializer::InsertPaddingAtEnd
(
bool
padAtEnd)
41
{
42
NS_LOG_FUNCTION
(
this
);
43
m_padAtEnd
= padAtEnd;
44
}
45
46
void
47
BitSerializer::PadAtStart
()
48
{
49
NS_LOG_FUNCTION
(
this
);
50
51
uint8_t padding = 8 - (
m_blob
.size() % 8);
52
53
m_blob
.insert(
m_blob
.begin(), padding,
false
);
54
}
55
56
void
57
BitSerializer::PadAtEnd
()
58
{
59
uint8_t padding = 8 - (
m_blob
.size() % 8);
60
61
m_blob
.insert(
m_blob
.end(), padding,
false
);
62
}
63
64
void
65
BitSerializer::PushBits
(uint64_t value, uint8_t significantBits)
66
{
67
NS_LOG_FUNCTION
(
this
<< value << +significantBits);
68
69
uint64_t mask = 1;
70
mask <<= significantBits - 1;
71
72
for
(uint8_t i = 0; i < significantBits; i++)
73
{
74
if
(value & mask)
75
{
76
m_blob
.push_back(
true
);
77
}
78
else
79
{
80
m_blob
.push_back(
false
);
81
}
82
mask >>= 1;
83
}
84
}
85
86
std::vector<uint8_t>
87
BitSerializer::GetBytes
()
88
{
89
NS_LOG_FUNCTION
(
this
);
90
91
std::vector<uint8_t> result;
92
93
m_padAtEnd
?
PadAtEnd
() :
PadAtStart
();
94
95
for
(
auto
it =
m_blob
.begin(); it !=
m_blob
.end();)
96
{
97
uint8_t tmp = 0;
98
for
(uint8_t i = 0; i < 8; ++i)
99
{
100
tmp <<= 1;
101
tmp |= (*it & 1);
102
it++;
103
}
104
result.push_back(tmp);
105
}
106
m_blob
.clear();
107
return
result;
108
}
109
110
uint8_t
111
BitSerializer::GetBytes
(uint8_t* buffer, uint32_t size)
112
{
113
NS_LOG_FUNCTION
(
this
<< buffer << size);
114
115
uint8_t resultLen = 0;
116
117
m_padAtEnd
?
PadAtEnd
() :
PadAtStart
();
118
119
NS_ABORT_MSG_IF
(
m_blob
.size() <= 8 * size,
120
"Target buffer is too short, "
<<
m_blob
.size() / 8 <<
" bytes needed"
);
121
122
for
(
auto
it =
m_blob
.begin(); it !=
m_blob
.end();)
123
{
124
uint8_t tmp = 0;
125
for
(uint8_t i = 0; i < 8; ++i)
126
{
127
tmp <<= 1;
128
tmp |= (*it & 1);
129
it++;
130
}
131
buffer[resultLen] = tmp;
132
resultLen++;
133
}
134
m_blob
.clear();
135
return
resultLen;
136
}
137
138
}
// 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:113
ns3::BitSerializer::PushBits
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
Definition:
bit-serializer.cc:65
ns3::BitSerializer::BitSerializer
BitSerializer()
Definition:
bit-serializer.cc:33
ns3::BitSerializer::PadAtStart
void PadAtStart()
Add the padding at the start of the blob.
Definition:
bit-serializer.cc:47
ns3::BitSerializer::GetBytes
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
Definition:
bit-serializer.cc:87
ns3::BitSerializer::m_blob
std::vector< bool > m_blob
Blob of serialized bits.
Definition:
bit-serializer.h:112
ns3::BitSerializer::InsertPaddingAtEnd
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
Definition:
bit-serializer.cc:40
ns3::BitSerializer::PadAtEnd
void PadAtEnd()
Add the padding at the end of the blob.
Definition:
bit-serializer.cc:57
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:108
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:202
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:240
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
network
utils
bit-serializer.cc
Generated on Sun Mar 3 2024 17:11:05 for ns-3 by
1.9.1