# HG changeset patch # User Tommaso Pecorella # Date 1531488755 -7200 # Parent 3f4e2e37cdc86bd3bfd7bad8118884bc6b874aea New Bit[Des,S]erializer classes diff --git a/src/network/utils/bit-deserializer.cc b/src/network/utils/bit-deserializer.cc new file mode 100644 --- /dev/null +++ b/src/network/utils/bit-deserializer.cc @@ -0,0 +1,142 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 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 + */ + +#include +#include "bit-deserializer.h" +#include "ns3/log.h" +#include "ns3/assert.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("BitDeserializer"); + +BitDeserializer::BitDeserializer() { + m_deserializing = false; +} + +void BitDeserializer::PushBytes (std::vector bytes) +{ + NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); + m_bytesBlob.insert (m_bytesBlob.end (), bytes.begin (), bytes.end ()); +} + +void BitDeserializer::PushBytes (uint8_t* bytes, uint32_t size) +{ + NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); + for (uint32_t index=0; index < size; index++) + { + m_bytesBlob.push_back (bytes[index]); + } +} + +void BitDeserializer::PushByte (uint8_t byte) +{ + NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); + m_bytesBlob.push_back (byte); +} + +uint8_t BitDeserializer::GetBits8 (uint8_t size) +{ + uint8_t result = 0; + PrepareDeserialization (); + + NS_ASSERT_MSG (size <= 8, "Number of requested bits exceeds 8"); + NS_ASSERT_MSG (size <= m_blob.size (), "Number of requested bits exceeds blob size"); + + for (uint8_t i=0; i + */ + +#ifndef BITDESERIALIZER_H_ +#define BITDESERIALIZER_H_ + +#include +#include + +namespace ns3 { + +class BitDeserializer { +public: + BitDeserializer(); + + void PushBytes (std::vector bytes); + void PushBytes (uint8_t* bytes, uint32_t size); + void PushByte (uint8_t byte); + + uint8_t GetBits8 (uint8_t size); + uint16_t GetBits16 (uint8_t size); + uint32_t GetBits32 (uint8_t size); + uint64_t GetBits64 (uint8_t size); + +private: + void PrepareDeserialization (); + + std::deque m_blob; + std::vector m_bytesBlob; + bool m_deserializing; +}; + +} // namespace ns3 + +#endif /* BITDESERIALIZER_H_ */ diff --git a/src/network/utils/bit-serializer.cc b/src/network/utils/bit-serializer.cc new file mode 100644 --- /dev/null +++ b/src/network/utils/bit-serializer.cc @@ -0,0 +1,118 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 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 + */ + +#include +#include "bit-serializer.h" +#include "ns3/log.h" +#include "ns3/assert.h" + +namespace ns3 { + +NS_LOG_COMPONENT_DEFINE ("BitSerializer"); + +BitSerializer::BitSerializer() { + m_padAtEnd = true; +} + +void BitSerializer::InsertPaddingAtEnd (bool padAtEnd) +{ + m_padAtEnd = padAtEnd; +} + +void BitSerializer::PadAtStart () +{ + uint8_t padding = 8 - (m_blob.size ()%8) ; + + m_blob.insert( m_blob.begin (), padding, false); +} + +void BitSerializer::PadAtEnd () +{ + uint8_t padding = 8 - (m_blob.size ()%8) ; + + m_blob.insert( m_blob.end (), padding, false); +} + +void BitSerializer::PushBits (uint64_t value, uint8_t significantBits) +{ + uint64_t mask = 1; + mask <<= significantBits-1; + + for (uint8_t i=0; i>= 1; + } +} + +std::vector BitSerializer::GetBytes () +{ + std::vector result; + + if (m_padAtEnd) + PadAtEnd (); + else + PadAtStart (); + + for (auto it = m_blob.begin(); it != m_blob.end();) + { + uint8_t tmp = 0; + for (uint8_t i = 0; i < 8; ++i) + { + tmp <<= 1; + tmp |= (*it & 1); + it++; + } + result.push_back (tmp); + } + return result; +} + +uint8_t BitSerializer::GetBytes (uint8_t *buffer, uint32_t size) +{ + uint8_t resultLen=0; + + if (m_padAtEnd) + PadAtEnd (); + else + PadAtStart (); + + NS_ASSERT_MSG (m_blob.size () <= 8*size, "Target buffer is too short, " << m_blob.size ()/8 << " bytes needed" ); + + for (auto it = m_blob.begin(); it != m_blob.end();) + { + uint8_t tmp = 0; + for (uint8_t i = 0; i < 8; ++i) + { + tmp <<= 1; + tmp |= (*it & 1); + it++; + } + buffer[resultLen] = tmp; + resultLen++; + + } + return resultLen; +} + +} // namespace ns3 diff --git a/src/network/utils/bit-serializer.h b/src/network/utils/bit-serializer.h new file mode 100644 --- /dev/null +++ b/src/network/utils/bit-serializer.h @@ -0,0 +1,50 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2018 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 + */ + +#ifndef BITSERIALIZER_H_ +#define BITSERIALIZER_H_ + +#include + +namespace ns3 { + +class BitSerializer { +public: + BitSerializer(); + + void InsertPaddingAtEnd (bool padAtEnd); + + void PushBits (uint64_t value, uint8_t significantBits); + + std::vector GetBytes (); + uint8_t GetBytes (uint8_t *buffer, uint32_t size); + +private: + void PadAtStart (); + + void PadAtEnd (); + + std::vector m_blob; + bool m_padAtEnd; +}; + +} // namespace ns3 + +#endif /* BITSERIALIZER_H_ */ diff --git a/src/network/wscript b/src/network/wscript --- a/src/network/wscript +++ b/src/network/wscript @@ -25,6 +25,8 @@ 'model/trailer.cc', 'utils/address-utils.cc', 'utils/ascii-file.cc', + 'utils/bit-deserializer.cc', + 'utils/bit-serializer.cc', 'utils/crc32.cc', 'utils/data-rate.cc', 'utils/drop-tail-queue.cc', @@ -113,6 +115,8 @@ 'utils/address-utils.h', 'utils/ascii-file.h', 'utils/ascii-test.h', + 'utils/bit-deserializer.h', + 'utils/bit-serializer.h', 'utils/crc32.h', 'utils/data-rate.h', 'utils/drop-tail-queue.h',