A Discrete-Event Network Simulator
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 * 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
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("BitSerializer");
32
34{
35 NS_LOG_FUNCTION(this);
36 m_padAtEnd = true;
37}
38
39void
41{
42 NS_LOG_FUNCTION(this);
43 m_padAtEnd = padAtEnd;
44}
45
46void
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
56void
58{
59 uint8_t padding = 8 - (m_blob.size() % 8);
60
61 m_blob.insert(m_blob.end(), padding, false);
62}
63
64void
65BitSerializer::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
86std::vector<uint8_t>
88{
89 NS_LOG_FUNCTION(this);
90
91 std::vector<uint8_t> result;
92
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
110uint8_t
111BitSerializer::GetBytes(uint8_t* buffer, uint32_t size)
112{
113 NS_LOG_FUNCTION(this << buffer << size);
114
115 uint8_t resultLen = 0;
116
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
bool m_padAtEnd
True if the padding must be added at the end of the blob.
void PushBits(uint64_t value, uint8_t significantBits)
Pushes a number of bits in the blob.
void PadAtStart()
Add the padding at the start of the blob.
std::vector< uint8_t > GetBytes()
Get the bytes representation of the blob.
std::vector< bool > m_blob
Blob of serialized bits.
void InsertPaddingAtEnd(bool padAtEnd)
Toggles the padding insertion policy.
void PadAtEnd()
Add the padding at the end of the blob.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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.