| 1 |
/* |
| 2 |
* Copyright (C) 2018-2022 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "binary_container.hpp" |
| 27 |
#include <algorithm> |
| 28 |
#include <iterator> |
| 29 |
#include <utility> |
| 30 |
#include <stdexcept> |
| 31 |
|
| 32 |
namespace io |
| 33 |
{ |
| 34 |
BinaryContainer::BinaryContainer() |
| 35 |
: isLE_(true) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
BinaryContainer::BinaryContainer(const std::vector<uint8_t>& buf) |
| 40 |
: isLE_(true) |
| 41 |
{ |
| 42 |
std::copy(buf.cbegin(), buf.cend(), std::back_inserter(buf_)); |
| 43 |
} |
| 44 |
|
| 45 |
BinaryContainer::BinaryContainer(std::vector<uint8_t>&& buf) |
| 46 |
: isLE_(true) |
| 47 |
{ |
| 48 |
std::move(buf.begin(), buf.end(), std::back_inserter(buf_)); |
| 49 |
} |
| 50 |
|
| 51 |
void BinaryContainer::clear() |
| 52 |
{ |
| 53 |
buf_.clear(); |
| 54 |
buf_.shrink_to_fit(); |
| 55 |
} |
| 56 |
|
| 57 |
void BinaryContainer::appendInt8(int8_t v) |
| 58 |
{ |
| 59 |
buf_.push_back(static_cast<uint8_t>(v)); |
| 60 |
} |
| 61 |
|
| 62 |
void BinaryContainer::appendUint8(uint8_t v) |
| 63 |
{ |
| 64 |
buf_.push_back(v); |
| 65 |
} |
| 66 |
|
| 67 |
void BinaryContainer::appendInt16(int16_t v) |
| 68 |
{ |
| 69 |
append({ |
| 70 |
static_cast<uint8_t>(0x00ff & v), |
| 71 |
static_cast<uint8_t>(v >> 8), |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
void BinaryContainer::appendUint16(uint16_t v) |
| 76 |
{ |
| 77 |
append({ |
| 78 |
static_cast<uint8_t>(0x00ff & v), |
| 79 |
static_cast<uint8_t>(v >> 8), |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
void BinaryContainer::appendInt32(int32_t v) |
| 84 |
{ |
| 85 |
append({ |
| 86 |
static_cast<uint8_t>(0x000000ff & v), |
| 87 |
static_cast<uint8_t>(0x0000ff & (v >> 8)), |
| 88 |
static_cast<uint8_t>(0x00ff & (v >> 16)), |
| 89 |
static_cast<uint8_t>(v >> 24), |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
void BinaryContainer::appendUint32(uint32_t v) |
| 94 |
{ |
| 95 |
append({ |
| 96 |
static_cast<uint8_t>(0x000000ff & v), |
| 97 |
static_cast<uint8_t>(0x0000ff & (v >> 8)), |
| 98 |
static_cast<uint8_t>(0x00ff & (v >> 16)), |
| 99 |
static_cast<uint8_t>(v >> 24), |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
void BinaryContainer::appendChar(char c) |
| 104 |
{ |
| 105 |
buf_.push_back(static_cast<uint8_t>(c)); |
| 106 |
} |
| 107 |
|
| 108 |
void BinaryContainer::appendString(const std::string& str) |
| 109 |
{ |
| 110 |
std::copy(str.cbegin(), str.cend(), std::back_inserter(buf_)); |
| 111 |
} |
| 112 |
|
| 113 |
void BinaryContainer::appendArray(const uint8_t* array, size_type size) |
| 114 |
{ |
| 115 |
std::copy(array, array + size, std::back_inserter(buf_)); |
| 116 |
} |
| 117 |
|
| 118 |
void BinaryContainer::appendVector(const std::vector<uint8_t>& vec) |
| 119 |
{ |
| 120 |
std::copy(vec.cbegin(), vec.cend(), std::back_inserter(buf_)); |
| 121 |
} |
| 122 |
|
| 123 |
void BinaryContainer::appendVector(std::vector<uint8_t>&& vec) |
| 124 |
{ |
| 125 |
std::move(vec.begin(), vec.end(), std::back_inserter(buf_)); |
| 126 |
} |
| 127 |
|
| 128 |
void BinaryContainer::appendBinaryContainer(const BinaryContainer& bc) |
| 129 |
{ |
| 130 |
std::copy(bc.cbegin(), bc.cend(), std::back_inserter(buf_)); |
| 131 |
} |
| 132 |
|
| 133 |
void BinaryContainer::appendBinaryContainer(BinaryContainer&& bc) |
| 134 |
{ |
| 135 |
std::move(bc.begin(), bc.end(), std::back_inserter(buf_)); |
| 136 |
} |
| 137 |
|
| 138 |
void BinaryContainer::writeInt8(size_type offset, int8_t v) |
| 139 |
{ |
| 140 |
buf_.at(offset) = static_cast<uint8_t>(v); |
| 141 |
} |
| 142 |
|
| 143 |
void BinaryContainer::writeUint8(size_type offset, uint8_t v) |
| 144 |
{ |
| 145 |
buf_.at(offset) = v; |
| 146 |
} |
| 147 |
|
| 148 |
void BinaryContainer::writeInt16(size_type offset, int16_t v) |
| 149 |
{ |
| 150 |
write(offset, { |
| 151 |
static_cast<uint8_t>(0x00ff & v), |
| 152 |
static_cast<uint8_t>(v >> 8), |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
void BinaryContainer::writeUint16(size_type offset, uint16_t v) |
| 157 |
{ |
| 158 |
write(offset, { |
| 159 |
static_cast<uint8_t>(0x00ff & v), |
| 160 |
static_cast<uint8_t>(v >> 8), |
| 161 |
}); |
| 162 |
} |
| 163 |
|
| 164 |
void BinaryContainer::writeInt32(size_type offset, int32_t v) |
| 165 |
{ |
| 166 |
write(offset, { |
| 167 |
static_cast<uint8_t>(0x000000ff & v), |
| 168 |
static_cast<uint8_t>(0x0000ff & (v >> 8)), |
| 169 |
static_cast<uint8_t>(0x00ff & (v >> 16)), |
| 170 |
static_cast<uint8_t>(v >> 24), |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
void BinaryContainer::writeUint32(size_type offset, uint32_t v) |
| 175 |
{ |
| 176 |
write(offset, { |
| 177 |
static_cast<uint8_t>(0x000000ff & v), |
| 178 |
static_cast<uint8_t>(0x0000ff & (v >> 8)), |
| 179 |
static_cast<uint8_t>(0x00ff & (v >> 16)), |
| 180 |
static_cast<uint8_t>(v >> 24), |
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
void BinaryContainer::writeChar(size_type offset, char c) |
| 185 |
{ |
| 186 |
buf_.at(offset) = static_cast<uint8_t>(c); |
| 187 |
} |
| 188 |
|
| 189 |
void BinaryContainer::writeString(size_type offset, const std::string& str) |
| 190 |
{ |
| 191 |
if (buf_.size() <= offset || buf_.size() < offset + str.length()) |
| 192 |
throw std::out_of_range("Invalid buffer range in binary container"); |
| 193 |
|
| 194 |
std::copy(str.cbegin(), str.cend(), buf_.begin() + static_cast<int>(offset)); |
| 195 |
} |
| 196 |
|
| 197 |
int8_t BinaryContainer::readInt8(size_type offset) const |
| 198 |
{ |
| 199 |
return static_cast<int8_t>(buf_.at(offset)); |
| 200 |
} |
| 201 |
|
| 202 |
uint8_t BinaryContainer::readUint8(size_type offset) const |
| 203 |
{ |
| 204 |
return static_cast<uint8_t>(buf_.at(offset)); |
| 205 |
} |
| 206 |
|
| 207 |
int16_t BinaryContainer::readInt16(size_type offset) const |
| 208 |
{ |
| 209 |
std::vector<uint8_t> data = read(offset, 2); |
| 210 |
return static_cast<int16_t>(data[0] | (data[1] << 8)); |
| 211 |
} |
| 212 |
|
| 213 |
uint16_t BinaryContainer::readUint16(size_type offset) const |
| 214 |
{ |
| 215 |
std::vector<uint8_t> data = read(offset, 2); |
| 216 |
return static_cast<uint16_t>(data[0] | (data[1] << 8)); |
| 217 |
} |
| 218 |
|
| 219 |
int32_t BinaryContainer::readInt32(size_type offset) const |
| 220 |
{ |
| 221 |
std::vector<uint8_t> data = read(offset, 4); |
| 222 |
return static_cast<int32_t>(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); |
| 223 |
} |
| 224 |
|
| 225 |
uint32_t BinaryContainer::readUint32(size_type offset) const |
| 226 |
{ |
| 227 |
std::vector<uint8_t> data = read(offset, 4); |
| 228 |
return static_cast<uint32_t>(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); |
| 229 |
} |
| 230 |
|
| 231 |
char BinaryContainer::readChar(size_type offset) const |
| 232 |
{ |
| 233 |
return buf_.at(offset); |
| 234 |
} |
| 235 |
|
| 236 |
std::string BinaryContainer::readString(size_type offset, size_type length) const |
| 237 |
{ |
| 238 |
if (buf_.size() <= offset || buf_.size() < offset + length) |
| 239 |
throw std::out_of_range("Invalid buffer range in binary container"); |
| 240 |
|
| 241 |
return std::string(buf_.begin() + static_cast<int>(offset), buf_.begin() + static_cast<int>(offset + length)); |
| 242 |
} |
| 243 |
|
| 244 |
BinaryContainer BinaryContainer::getSubcontainer(size_type offset, size_type length) const |
| 245 |
{ |
| 246 |
if (buf_.size() <= offset || buf_.size() < offset + length) |
| 247 |
throw std::out_of_range("Invalid buffer range in binary container"); |
| 248 |
|
| 249 |
BinaryContainer sub; |
| 250 |
std::copy_n(buf_.begin() + static_cast<int>(offset), length, std::back_inserter(sub)); |
| 251 |
return sub; |
| 252 |
} |
| 253 |
|
| 254 |
std::vector<uint8_t> BinaryContainer::toVector() const |
| 255 |
{ |
| 256 |
std::vector<uint8_t> vec; |
| 257 |
vec.reserve(buf_.size()); |
| 258 |
std::copy(buf_.cbegin(), buf_.cend(), std::back_inserter(vec)); |
| 259 |
return vec; |
| 260 |
} |
| 261 |
|
| 262 |
void BinaryContainer::append(const std::vector<uint8_t>&& a) |
| 263 |
{ |
| 264 |
if (isLE_) |
| 265 |
std::copy(a.cbegin(), a.cend(), std::back_inserter(buf_)); |
| 266 |
else |
| 267 |
std::reverse_copy(a.cbegin(), a.cend(), std::back_inserter(buf_)); |
| 268 |
} |
| 269 |
|
| 270 |
void BinaryContainer::write(size_t offset, const std::vector<uint8_t>&& a) |
| 271 |
{ |
| 272 |
if (buf_.size() <= offset || buf_.size() < offset + a.size()) |
| 273 |
throw std::out_of_range("Invalid buffer range in binary container"); |
| 274 |
|
| 275 |
if (isLE_) |
| 276 |
std::copy(a.cbegin(), a.cend(), buf_.begin() + static_cast<int>(offset)); |
| 277 |
else |
| 278 |
std::reverse_copy(a.cbegin(), a.cend(), buf_.begin() + static_cast<int>(offset)); |
| 279 |
} |
| 280 |
|
| 281 |
std::vector<uint8_t> BinaryContainer::read(size_type offset, size_type size) const |
| 282 |
{ |
| 283 |
if (buf_.size() <= offset || buf_.size() < offset + size) |
| 284 |
throw std::out_of_range("Invalid buffer range in binary container"); |
| 285 |
|
| 286 |
std::vector<uint8_t> data; |
| 287 |
if (isLE_) |
| 288 |
std::copy(buf_.cbegin() + static_cast<int>(offset), buf_.cbegin() + static_cast<int>(offset + size), std::back_inserter(data)); |
| 289 |
else |
| 290 |
std::reverse_copy(buf_.cbegin() + static_cast<int>(offset), buf_.cbegin() + static_cast<int>(offset + size), std::back_inserter(data)); |
| 291 |
return data; |
| 292 |
} |
| 293 |
} |