|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2018 Universita' di Firenze, Italy |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 2 as |
| 7 |
* published by the Free Software Foundation; |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
| 18 |
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it> |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include "bit-deserializer.h" |
| 23 |
#include "ns3/log.h" |
| 24 |
#include "ns3/assert.h" |
| 25 |
|
| 26 |
namespace ns3 { |
| 27 |
|
| 28 |
NS_LOG_COMPONENT_DEFINE ("BitDeserializer"); |
| 29 |
|
| 30 |
BitDeserializer::BitDeserializer () |
| 31 |
{ |
| 32 |
m_deserializing = false; |
| 33 |
} |
| 34 |
|
| 35 |
void BitDeserializer::PushBytes (std::vector<uint8_t> bytes) |
| 36 |
{ |
| 37 |
NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); |
| 38 |
m_bytesBlob.insert (m_bytesBlob.end (), bytes.begin (), bytes.end ()); |
| 39 |
} |
| 40 |
|
| 41 |
void BitDeserializer::PushBytes (uint8_t* bytes, uint32_t size) |
| 42 |
{ |
| 43 |
NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); |
| 44 |
for (uint32_t index = 0; index < size; index++) |
| 45 |
{ |
| 46 |
m_bytesBlob.push_back (bytes[index]); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void BitDeserializer::PushByte (uint8_t byte) |
| 51 |
{ |
| 52 |
NS_ASSERT_MSG (!m_deserializing, "Can't add bytes after deserialization started"); |
| 53 |
m_bytesBlob.push_back (byte); |
| 54 |
} |
| 55 |
|
| 56 |
uint8_t BitDeserializer::GetBits8 (uint8_t size) |
| 57 |
{ |
| 58 |
uint8_t result = 0; |
| 59 |
PrepareDeserialization (); |
| 60 |
|
| 61 |
NS_ASSERT_MSG (size <= 8, "Number of requested bits exceeds 8"); |
| 62 |
NS_ASSERT_MSG (size <= m_blob.size (), "Number of requested bits exceeds blob size"); |
| 63 |
|
| 64 |
for (uint8_t i = 0; i < size; i++) |
| 65 |
{ |
| 66 |
result <<= 1; |
| 67 |
result |= m_blob.front (); |
| 68 |
m_blob.pop_front (); |
| 69 |
} |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
uint16_t BitDeserializer::GetBits16 (uint8_t size) |
| 74 |
{ |
| 75 |
uint8_t result = 0; |
| 76 |
PrepareDeserialization (); |
| 77 |
|
| 78 |
NS_ASSERT_MSG (size <= 16, "Number of requested bits exceeds 16"); |
| 79 |
NS_ASSERT_MSG (size <= m_blob.size (), "Number of requested bits exceeds blob size"); |
| 80 |
|
| 81 |
for (uint8_t i = 0; i < size; i++) |
| 82 |
{ |
| 83 |
result <<= 1; |
| 84 |
result |= m_blob.front (); |
| 85 |
m_blob.pop_front (); |
| 86 |
} |
| 87 |
return result; |
| 88 |
} |
| 89 |
|
| 90 |
uint32_t BitDeserializer::GetBits32 (uint8_t size) |
| 91 |
{ |
| 92 |
uint8_t result = 0; |
| 93 |
PrepareDeserialization (); |
| 94 |
|
| 95 |
NS_ASSERT_MSG (size <= 32, "Number of requested bits exceeds 32"); |
| 96 |
NS_ASSERT_MSG (size <= m_blob.size (), "Number of requested bits exceeds blob size"); |
| 97 |
|
| 98 |
for (uint8_t i = 0; i < size; i++) |
| 99 |
{ |
| 100 |
result <<= 1; |
| 101 |
result |= m_blob.front (); |
| 102 |
m_blob.pop_front (); |
| 103 |
} |
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
uint64_t BitDeserializer::GetBits64 (uint8_t size) |
| 108 |
{ |
| 109 |
uint8_t result = 0; |
| 110 |
PrepareDeserialization (); |
| 111 |
|
| 112 |
NS_ASSERT_MSG (size <= 64, "Number of requested bits exceeds 64"); |
| 113 |
NS_ASSERT_MSG (size <= m_blob.size (), "Number of requested bits exceeds blob size"); |
| 114 |
|
| 115 |
for (uint8_t i = 0; i < size; i++) |
| 116 |
{ |
| 117 |
result <<= 1; |
| 118 |
result |= m_blob.front (); |
| 119 |
m_blob.pop_front (); |
| 120 |
} |
| 121 |
return result; |
| 122 |
} |
| 123 |
|
| 124 |
void BitDeserializer::PrepareDeserialization () |
| 125 |
{ |
| 126 |
if (m_deserializing == false) |
| 127 |
{ |
| 128 |
m_deserializing = true; |
| 129 |
for (auto index = m_bytesBlob.begin (); index != m_bytesBlob.end (); index++) |
| 130 |
{ |
| 131 |
m_blob.push_back (*index & 0x80); |
| 132 |
m_blob.push_back (*index & 0x40); |
| 133 |
m_blob.push_back (*index & 0x20); |
| 134 |
m_blob.push_back (*index & 0x10); |
| 135 |
m_blob.push_back (*index & 0x8); |
| 136 |
m_blob.push_back (*index & 0x4); |
| 137 |
m_blob.push_back (*index & 0x2); |
| 138 |
m_blob.push_back (*index & 0x1); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace ns3 |