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 * 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
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("BitSerializer");
21
27
28void
30{
31 NS_LOG_FUNCTION(this);
32 m_padAtEnd = padAtEnd;
33}
34
35void
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
45void
47{
48 uint8_t padding = 8 - (m_blob.size() % 8);
49
50 m_blob.insert(m_blob.end(), padding, false);
51}
52
53void
54BitSerializer::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
75std::vector<uint8_t>
77{
78 NS_LOG_FUNCTION(this);
79
80 std::vector<uint8_t> result;
81
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
99uint8_t
100BitSerializer::GetBytes(uint8_t* buffer, uint32_t size)
101{
102 NS_LOG_FUNCTION(this << buffer << size);
103
104 uint8_t resultLen = 0;
105
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
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: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.