2021-06-16 08:10:52 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020 Universita' di Firenze, Italy
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "bit-serializer.h"
|
2022-10-07 20:08:35 +00:00
|
|
|
|
2021-06-16 08:10:52 +00:00
|
|
|
#include "ns3/abort.h"
|
2022-10-07 20:08:35 +00:00
|
|
|
#include "ns3/assert.h"
|
|
|
|
|
#include "ns3/log.h"
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
#include <iostream>
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
namespace ns3
|
|
|
|
|
{
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_COMPONENT_DEFINE("BitSerializer");
|
|
|
|
|
|
|
|
|
|
BitSerializer::BitSerializer()
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
m_padAtEnd = true;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
BitSerializer::InsertPaddingAtEnd(bool padAtEnd)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
|
|
|
|
m_padAtEnd = padAtEnd;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
BitSerializer::PadAtStart()
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t padding = 8 - (m_blob.size() % 8);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.insert(m_blob.begin(), padding, false);
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
BitSerializer::PadAtEnd()
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t padding = 8 - (m_blob.size() % 8);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.insert(m_blob.end(), padding, false);
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
void
|
|
|
|
|
BitSerializer::PushBits(uint64_t value, uint8_t significantBits)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this << value << +significantBits);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
uint64_t mask = 1;
|
|
|
|
|
mask <<= significantBits - 1;
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
for (uint8_t i = 0; i < significantBits; i++)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
if (value & mask)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.push_back(true);
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
else
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.push_back(false);
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
mask >>= 1;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
std::vector<uint8_t>
|
|
|
|
|
BitSerializer::GetBytes()
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
std::vector<uint8_t> result;
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_padAtEnd ? PadAtEnd() : PadAtStart();
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
for (auto it = m_blob.begin(); it != m_blob.end();)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t tmp = 0;
|
|
|
|
|
for (uint8_t i = 0; i < 8; ++i)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
tmp <<= 1;
|
|
|
|
|
tmp |= (*it & 1);
|
|
|
|
|
it++;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
result.push_back(tmp);
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.clear();
|
|
|
|
|
return result;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t
|
|
|
|
|
BitSerializer::GetBytes(uint8_t* buffer, uint32_t size)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_LOG_FUNCTION(this << buffer << size);
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t resultLen = 0;
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
m_padAtEnd ? PadAtEnd() : PadAtStart();
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
NS_ABORT_MSG_IF(m_blob.size() <= 8 * size,
|
|
|
|
|
"Target buffer is too short, " << m_blob.size() / 8 << " bytes needed");
|
2021-06-16 08:10:52 +00:00
|
|
|
|
2022-10-07 20:08:35 +00:00
|
|
|
for (auto it = m_blob.begin(); it != m_blob.end();)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
uint8_t tmp = 0;
|
|
|
|
|
for (uint8_t i = 0; i < 8; ++i)
|
2021-06-16 08:10:52 +00:00
|
|
|
{
|
2022-10-07 20:08:35 +00:00
|
|
|
tmp <<= 1;
|
|
|
|
|
tmp |= (*it & 1);
|
|
|
|
|
it++;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
buffer[resultLen] = tmp;
|
|
|
|
|
resultLen++;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
2022-10-07 20:08:35 +00:00
|
|
|
m_blob.clear();
|
|
|
|
|
return resultLen;
|
2021-06-16 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ns3
|