| 1 | #ifndef IOUTILITIES_BINERYREADER_H |
| 2 | #define IOUTILITIES_BINERYREADER_H |
| 3 | |
| 4 | #include "../conversion/binaryconversion.h" |
| 5 | |
| 6 | #include <istream> |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace CppUtilities { |
| 11 | class CPP_UTILITIES_EXPORT BinaryReader { |
| 12 | |
| 13 | public: |
| 14 | BinaryReader(std::istream *stream, bool giveOwnership = false); |
| 15 | BinaryReader(const BinaryReader &other); |
| 16 | BinaryReader &operator=(const BinaryReader &rhs) = delete; |
| 17 | ~BinaryReader(); |
| 18 | |
| 19 | const std::istream *stream() const; |
| 20 | std::istream *stream(); |
| 21 | void setStream(std::istream *stream, bool giveOwnership = false); |
| 22 | bool hasOwnership() const; |
| 23 | void giveOwnership(); |
| 24 | void detatchOwnership(); |
| 25 | bool fail() const; |
| 26 | bool eof() const; |
| 27 | bool canRead() const; |
| 28 | std::istream::pos_type readStreamsize(); |
| 29 | std::istream::pos_type readRemainingBytes(); |
| 30 | void read(char *buffer, std::streamsize length); |
| 31 | void read(std::uint8_t *buffer, std::streamsize length); |
| 32 | void read(std::vector<char> &buffer, std::streamsize length); |
| 33 | std::int16_t readInt16BE(); |
| 34 | std::uint16_t readUInt16BE(); |
| 35 | std::int32_t readInt24BE(); |
| 36 | std::uint32_t readUInt24BE(); |
| 37 | std::int32_t readInt32BE(); |
| 38 | std::uint32_t readUInt32BE(); |
| 39 | std::int64_t readInt40BE(); |
| 40 | std::uint64_t readUInt40BE(); |
| 41 | std::int64_t readInt56BE(); |
| 42 | std::uint64_t readUInt56BE(); |
| 43 | std::int64_t readInt64BE(); |
| 44 | std::uint64_t readUInt64BE(); |
| 45 | std::uint64_t readVariableLengthUIntBE(); |
| 46 | float readFloat32BE(); |
| 47 | double readFloat64BE(); |
| 48 | std::int16_t readInt16LE(); |
| 49 | std::uint16_t readUInt16LE(); |
| 50 | std::int32_t readInt24LE(); |
| 51 | std::uint32_t readUInt24LE(); |
| 52 | std::int32_t readInt32LE(); |
| 53 | std::uint32_t readUInt32LE(); |
| 54 | std::int64_t readInt40LE(); |
| 55 | std::uint64_t readUInt40LE(); |
| 56 | std::int64_t readInt56LE(); |
| 57 | std::uint64_t readUInt56LE(); |
| 58 | std::int64_t readInt64LE(); |
| 59 | std::uint64_t readUInt64LE(); |
| 60 | std::uint64_t readVariableLengthUIntLE(); |
| 61 | float readFloat32LE(); |
| 62 | double readFloat64LE(); |
| 63 | char readChar(); |
| 64 | std::uint8_t readByte(); |
| 65 | bool readBool(); |
| 66 | std::string readLengthPrefixedString(); |
| 67 | std::string readString(std::size_t length); |
| 68 | std::string readTerminatedString(std::uint8_t termination = 0); |
| 69 | std::string readTerminatedString(std::size_t maxBytesToRead, std::uint8_t termination = 0); |
| 70 | std::uint32_t readSynchsafeUInt32BE(); |
| 71 | float readFixed8BE(); |
| 72 | float readFixed16BE(); |
| 73 | std::uint32_t readSynchsafeUInt32LE(); |
| 74 | float readFixed8LE(); |
| 75 | float readFixed16LE(); |
| 76 | std::uint32_t readCrc32(std::size_t length); |
| 77 | static std::uint32_t computeCrc32(const char *buffer, std::size_t length); |
| 78 | static const std::uint32_t crc32Table[]; |
| 79 | |
| 80 | // declare further overloads for read() to ease use of BinaryReader in templates |
| 81 | void read(char &oneCharacter); |
| 82 | void read(std::uint8_t &oneByte); |
| 83 | void read(bool &oneBool); |
| 84 | void read(std::string &lengthPrefixedString); |
| 85 | void read(std::int16_t &one16BitInt); |
| 86 | void read(std::uint16_t &one16BitUInt); |
| 87 | void read(std::int32_t &one32BitInt); |
| 88 | void read(std::uint32_t &one32BitUInt); |
| 89 | void read(std::int64_t &one64BitInt); |
| 90 | void read(std::uint64_t &one64BitUInt); |
| 91 | void read(float &one32BitFloat); |
| 92 | void read(double &one64BitFloat); |
| 93 | |
| 94 | private: |
| 95 | void bufferVariableLengthInteger(); |
| 96 | |
| 97 | std::istream *m_stream; |
| 98 | bool m_ownership; |
| 99 | char m_buffer[8]; |
| 100 | }; |
| 101 | |
| 102 | /*! |
| 103 | * \brief Constructs a new BinaryReader. |
| 104 | * \param stream Specifies the stream to read from. |
| 105 | * \param giveOwnership Specifies whether the reader should take ownership. |
| 106 | */ |
| 107 | inline BinaryReader::BinaryReader(std::istream *stream, bool giveOwnership) |
| 108 | : m_stream(stream) |
| 109 | , m_ownership(giveOwnership) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | /*! |
| 114 | * \brief Copies the specified BinaryReader. |
| 115 | * \remarks The copy will not take ownership over the stream. |
| 116 | */ |
| 117 | inline BinaryReader::BinaryReader(const BinaryReader &other) |
| 118 | : m_stream(other.m_stream) |
| 119 | , m_ownership(false) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | /*! |
| 124 | * \brief Destroys the BinaryReader. |
| 125 | */ |
| 126 | inline BinaryReader::~BinaryReader() |
| 127 | { |
| 128 | if (m_ownership) { |
| 129 | delete m_stream; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /*! |
| 134 | * \brief Returns a pointer to the stream the reader will read from when calling one of the read-methods. |
| 135 | * |
| 136 | * \sa setStream() |
| 137 | */ |
| 138 | inline std::istream *BinaryReader::stream() |
| 139 | { |
| 140 | return m_stream; |
| 141 | } |
| 142 | |
| 143 | /*! |
| 144 | * \brief Returns a pointer to the stream the reader will read from when calling one of the read-methods. |
| 145 | * |
| 146 | * \sa setStream() |
| 147 | */ |
| 148 | inline const std::istream *BinaryReader::stream() const |
| 149 | { |
| 150 | return m_stream; |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | * \brief Returns whether the reader takes ownership over the assigned stream. |
| 155 | * |
| 156 | * \sa setStream() |
| 157 | * \sa giveOwnership() |
| 158 | * \sa detatchOwnership() |
| 159 | */ |
| 160 | inline bool BinaryReader::hasOwnership() const |
| 161 | { |
| 162 | return m_ownership; |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | * \brief The reader will take ownership over the assigned stream. |
| 167 | * |
| 168 | * \sa setStream() |
| 169 | * \sa detatchOwnership() |
| 170 | * \sa hasOwnership() |
| 171 | */ |
| 172 | inline void BinaryReader::giveOwnership() |
| 173 | { |
| 174 | if (m_stream) { |
| 175 | m_ownership = true; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | * \brief The reader will not take ownership over the assigned stream. |
| 181 | * |
| 182 | * \sa setStream() |
| 183 | * \sa giveOwnership() |
| 184 | * \sa hasOwnership() |
| 185 | */ |
| 186 | inline void BinaryReader::detatchOwnership() |
| 187 | { |
| 188 | m_ownership = false; |
| 189 | } |
| 190 | |
| 191 | /*! |
| 192 | * \brief Returns an indication whether the fail bit of the assigned stream is set. |
| 193 | */ |
| 194 | inline bool BinaryReader::fail() const |
| 195 | { |
| 196 | return m_stream ? m_stream->fail() : false; |
| 197 | } |
| 198 | |
| 199 | /*! |
| 200 | * \brief Returns an indication whether the end-of-stream bit of the assigned stream is set. |
| 201 | */ |
| 202 | inline bool BinaryReader::eof() const |
| 203 | { |
| 204 | return m_stream && m_stream->eof(); |
| 205 | } |
| 206 | |
| 207 | /*! |
| 208 | * \brief Returns an indication whether a stream is assigned the reader can read from. |
| 209 | */ |
| 210 | inline bool BinaryReader::canRead() const |
| 211 | { |
| 212 | return m_stream && m_stream->good(); |
| 213 | } |
| 214 | |
| 215 | /*! |
| 216 | * \brief Reads the specified number of characters from the stream in the character array. |
| 217 | */ |
| 218 | inline void BinaryReader::read(char *buffer, std::streamsize length) |
| 219 | { |
| 220 | m_stream->read(buffer, length); |
| 221 | } |
| 222 | |
| 223 | /*! |
| 224 | * \brief Reads the specified number of bytes from the stream in the character array. |
| 225 | */ |
| 226 | inline void BinaryReader::read(std::uint8_t *buffer, std::streamsize length) |
| 227 | { |
| 228 | m_stream->read(reinterpret_cast<char *>(buffer), length); |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | * \brief Reads the specified number of bytes from the stream in the specified \a buffer. |
| 233 | */ |
| 234 | inline void BinaryReader::read(std::vector<char> &buffer, std::streamsize length) |
| 235 | { |
| 236 | buffer.resize(static_cast<std::vector<char>::size_type>(length)); |
| 237 | m_stream->read(buffer.data(), length); |
| 238 | } |
| 239 | |
| 240 | /*! |
| 241 | * \brief Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. |
| 242 | */ |
| 243 | inline std::int16_t BinaryReader::readInt16BE() |
| 244 | { |
| 245 | m_stream->read(m_buffer, sizeof(std::int16_t)); |
| 246 | return BE::toInt<std::int16_t>(m_buffer); |
| 247 | } |
| 248 | |
| 249 | /*! |
| 250 | * \brief Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes. |
| 251 | */ |
| 252 | inline std::uint16_t BinaryReader::readUInt16BE() |
| 253 | { |
| 254 | m_stream->read(m_buffer, sizeof(std::uint16_t)); |
| 255 | return BE::toInt<std::uint16_t>(m_buffer); |
| 256 | } |
| 257 | |
| 258 | /*! |
| 259 | * \brief Reads a 24-bit big endian signed integer from the current stream and advances the current position of the stream by three bytes. |
| 260 | */ |
| 261 | inline std::int32_t BinaryReader::readInt24BE() |
| 262 | { |
| 263 | *m_buffer = 0; |
| 264 | m_stream->read(m_buffer + 1, 3); |
| 265 | auto val = BE::toInt<std::int32_t>(m_buffer); |
| 266 | if (val >= 0x800000) { |
| 267 | val = -(0x1000000 - val); |
| 268 | } |
| 269 | return val; |
| 270 | } |
| 271 | |
| 272 | /*! |
| 273 | * \brief Reads a 24-bit big endian unsigned integer from the current stream and advances the current position of the stream by three bytes. |
| 274 | */ |
| 275 | inline std::uint32_t BinaryReader::readUInt24BE() |
| 276 | { |
| 277 | *m_buffer = 0; |
| 278 | m_stream->read(m_buffer + 1, 3); |
| 279 | return BE::toInt<std::uint32_t>(m_buffer); |
| 280 | } |
| 281 | |
| 282 | /*! |
| 283 | * \brief Reads a 32-bit big endian signed integer from the current stream and advances the current position of the stream by four bytes. |
| 284 | */ |
| 285 | inline std::int32_t BinaryReader::readInt32BE() |
| 286 | { |
| 287 | m_stream->read(m_buffer, sizeof(std::int32_t)); |
| 288 | return BE::toInt<std::int32_t>(m_buffer); |
| 289 | } |
| 290 | |
| 291 | /*! |
| 292 | * \brief Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes. |
| 293 | */ |
| 294 | inline std::uint32_t BinaryReader::readUInt32BE() |
| 295 | { |
| 296 | m_stream->read(m_buffer, sizeof(std::uint32_t)); |
| 297 | return BE::toInt<std::uint32_t>(m_buffer); |
| 298 | } |
| 299 | |
| 300 | /*! |
| 301 | * \brief Reads a 40-bit big endian signed integer from the current stream and advances the current position of the stream by five bytes. |
| 302 | */ |
| 303 | inline std::int64_t BinaryReader::readInt40BE() |
| 304 | { |
| 305 | *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0; |
| 306 | m_stream->read(m_buffer + 3, 5); |
| 307 | auto val = BE::toInt<std::int64_t>(m_buffer); |
| 308 | if (val >= 0x8000000000) { |
| 309 | val = -(0x10000000000 - val); |
| 310 | } |
| 311 | return val; |
| 312 | } |
| 313 | |
| 314 | /*! |
| 315 | * \brief Reads a 40-bit big endian unsigned integer from the current stream and advances the current position of the stream by five bytes. |
| 316 | */ |
| 317 | inline std::uint64_t BinaryReader::readUInt40BE() |
| 318 | { |
| 319 | *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0; |
| 320 | m_stream->read(m_buffer + 3, 5); |
| 321 | return BE::toInt<std::uint64_t>(m_buffer); |
| 322 | } |
| 323 | |
| 324 | /*! |
| 325 | * \brief Reads a 56-bit big endian signed integer from the current stream and advances the current position of the stream by seven bytes. |
| 326 | */ |
| 327 | inline std::int64_t BinaryReader::readInt56BE() |
| 328 | { |
| 329 | *m_buffer = 0; |
| 330 | m_stream->read(m_buffer + 1, 7); |
| 331 | auto val = BE::toInt<std::int64_t>(m_buffer); |
| 332 | if (val >= 0x80000000000000) { |
| 333 | val = -(0x100000000000000 - val); |
| 334 | } |
| 335 | return val; |
| 336 | } |
| 337 | |
| 338 | /*! |
| 339 | * \brief Reads a 56-bit big endian unsigned integer from the current stream and advances the current position of the stream by seven bytes. |
| 340 | */ |
| 341 | inline std::uint64_t BinaryReader::readUInt56BE() |
| 342 | { |
| 343 | *m_buffer = 0; |
| 344 | m_stream->read(m_buffer + 1, 7); |
| 345 | return BE::toInt<std::uint64_t>(m_buffer); |
| 346 | } |
| 347 | |
| 348 | /*! |
| 349 | * \brief Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes. |
| 350 | */ |
| 351 | inline std::int64_t BinaryReader::readInt64BE() |
| 352 | { |
| 353 | m_stream->read(m_buffer, sizeof(std::int64_t)); |
| 354 | return BE::toInt<std::int64_t>(m_buffer); |
| 355 | } |
| 356 | |
| 357 | /*! |
| 358 | * \brief Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. |
| 359 | */ |
| 360 | inline std::uint64_t BinaryReader::readUInt64BE() |
| 361 | { |
| 362 | m_stream->read(m_buffer, sizeof(std::uint64_t)); |
| 363 | return BE::toInt<std::uint64_t>(m_buffer); |
| 364 | } |
| 365 | |
| 366 | /*! |
| 367 | * \brief Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte. |
| 368 | * \throws Throws ConversionException if the size of the integer exceeds the maximum. |
| 369 | */ |
| 370 | inline std::uint64_t BinaryReader::readVariableLengthUIntBE() |
| 371 | { |
| 372 | bufferVariableLengthInteger(); |
| 373 | return BE::toInt<std::uint64_t>(m_buffer); |
| 374 | } |
| 375 | |
| 376 | /*! |
| 377 | * \brief Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes. |
| 378 | */ |
| 379 | inline float BinaryReader::readFloat32BE() |
| 380 | { |
| 381 | m_stream->read(m_buffer, sizeof(float)); |
| 382 | return BE::toFloat32(m_buffer); |
| 383 | } |
| 384 | |
| 385 | /*! |
| 386 | * \brief Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes. |
| 387 | */ |
| 388 | inline double BinaryReader::readFloat64BE() |
| 389 | { |
| 390 | m_stream->read(m_buffer, sizeof(double)); |
| 391 | return BE::toFloat64(m_buffer); |
| 392 | } |
| 393 | |
| 394 | /*! |
| 395 | * \brief Reads a 16-bit little endian signed integer from the current stream and advances the current position of the stream by two bytes. |
| 396 | */ |
| 397 | inline std::int16_t BinaryReader::readInt16LE() |
| 398 | { |
| 399 | m_stream->read(m_buffer, sizeof(std::int16_t)); |
| 400 | return LE::toInt<std::int16_t>(m_buffer); |
| 401 | } |
| 402 | |
| 403 | /*! |
| 404 | * \brief Reads a 16-bit little endian unsigned integer from the current stream and advances the current position of the stream by two bytes. |
| 405 | */ |
| 406 | inline std::uint16_t BinaryReader::readUInt16LE() |
| 407 | { |
| 408 | m_stream->read(m_buffer, sizeof(std::uint16_t)); |
| 409 | return LE::toInt<std::uint16_t>(m_buffer); |
| 410 | } |
| 411 | |
| 412 | /*! |
| 413 | * \brief Reads a 24-bit little endian signed integer from the current stream and advances the current position of the stream by three bytes. |
| 414 | */ |
| 415 | inline std::int32_t BinaryReader::readInt24LE() |
| 416 | { |
| 417 | *(m_buffer + 3) = 0; |
| 418 | m_stream->read(m_buffer, 3); |
| 419 | auto val = LE::toInt<std::int32_t>(m_buffer); |
| 420 | if (val >= 0x800000) { |
| 421 | val = -(0x1000000 - val); |
| 422 | } |
| 423 | return val; |
| 424 | } |
| 425 | |
| 426 | /*! |
| 427 | * \brief Reads a 24-bit little endian unsigned integer from the current stream and advances the current position of the stream by three bytes. |
| 428 | */ |
| 429 | inline std::uint32_t BinaryReader::readUInt24LE() |
| 430 | { |
| 431 | *(m_buffer + 3) = 0; |
| 432 | m_stream->read(m_buffer, 3); |
| 433 | return LE::toInt<std::uint32_t>(m_buffer); |
| 434 | } |
| 435 | |
| 436 | /*! |
| 437 | * \brief Reads a 32-bit little endian signed integer from the current stream and advances the current position of the stream by four bytes. |
| 438 | */ |
| 439 | inline std::int32_t BinaryReader::readInt32LE() |
| 440 | { |
| 441 | m_stream->read(m_buffer, sizeof(std::int32_t)); |
| 442 | return LE::toInt<std::int32_t>(m_buffer); |
| 443 | } |
| 444 | |
| 445 | /*! |
| 446 | * \brief Reads a 32-bit little endian unsigned integer from the current stream and advances the current position of the stream by four bytes. |
| 447 | */ |
| 448 | inline std::uint32_t BinaryReader::readUInt32LE() |
| 449 | { |
| 450 | m_stream->read(m_buffer, sizeof(std::uint32_t)); |
| 451 | return LE::toInt<std::uint32_t>(m_buffer); |
| 452 | } |
| 453 | |
| 454 | /*! |
| 455 | * \brief Reads a 40-bit little endian signed integer from the current stream and advances the current position of the stream by five bytes. |
| 456 | */ |
| 457 | inline std::int64_t BinaryReader::readInt40LE() |
| 458 | { |
| 459 | *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0; |
| 460 | m_stream->read(m_buffer, 5); |
| 461 | auto val = LE::toInt<std::int64_t>(m_buffer); |
| 462 | if (val >= 0x8000000000) { |
| 463 | val = -(0x10000000000 - val); |
| 464 | } |
| 465 | return val; |
| 466 | } |
| 467 | |
| 468 | /*! |
| 469 | * \brief Reads a 40-bit little endian unsigned integer from the current stream and advances the current position of the stream by five bytes. |
| 470 | */ |
| 471 | inline std::uint64_t BinaryReader::readUInt40LE() |
| 472 | { |
| 473 | *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0; |
| 474 | m_stream->read(m_buffer, 5); |
| 475 | return LE::toInt<std::uint64_t>(m_buffer); |
| 476 | } |
| 477 | |
| 478 | /*! |
| 479 | * \brief Reads a 56-bit little endian signed integer from the current stream and advances the current position of the stream by seven bytes. |
| 480 | */ |
| 481 | inline std::int64_t BinaryReader::readInt56LE() |
| 482 | { |
| 483 | *(m_buffer + 7) = 0; |
| 484 | m_stream->read(m_buffer, 7); |
| 485 | auto val = LE::toInt<std::int64_t>(m_buffer); |
| 486 | if (val >= 0x80000000000000) { |
| 487 | val = -(0x100000000000000 - val); |
| 488 | } |
| 489 | return val; |
| 490 | } |
| 491 | |
| 492 | /*! |
| 493 | * \brief Reads a 56-bit little endian unsigned integer from the current stream and advances the current position of the stream by seven bytes. |
| 494 | */ |
| 495 | inline std::uint64_t BinaryReader::readUInt56LE() |
| 496 | { |
| 497 | *(m_buffer + 7) = 0; |
| 498 | m_stream->read(m_buffer, 7); |
| 499 | return LE::toInt<std::uint64_t>(m_buffer); |
| 500 | } |
| 501 | |
| 502 | /*! |
| 503 | * \brief Reads a 64-bit little endian signed integer from the current stream and advances the current position of the stream by eight bytes. |
| 504 | */ |
| 505 | inline std::int64_t BinaryReader::readInt64LE() |
| 506 | { |
| 507 | m_stream->read(m_buffer, sizeof(std::int64_t)); |
| 508 | return LE::toInt<std::int64_t>(m_buffer); |
| 509 | } |
| 510 | |
| 511 | /*! |
| 512 | * \brief Reads a 64-bit little endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. |
| 513 | */ |
| 514 | inline std::uint64_t BinaryReader::readUInt64LE() |
| 515 | { |
| 516 | m_stream->read(m_buffer, sizeof(std::uint64_t)); |
| 517 | return LE::toInt<std::uint64_t>(m_buffer); |
| 518 | } |
| 519 | |
| 520 | /*! |
| 521 | * \brief Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the current position of the stream by one to eight byte. |
| 522 | * \throws Throws ConversionException if the size of the integer exceeds the maximum. |
| 523 | */ |
| 524 | inline std::uint64_t BinaryReader::readVariableLengthUIntLE() |
| 525 | { |
| 526 | bufferVariableLengthInteger(); |
| 527 | return LE::toInt<std::uint64_t>(m_buffer); |
| 528 | } |
| 529 | |
| 530 | /*! |
| 531 | * \brief Reads a 32-bit little endian floating point value from the current stream and advances the current position of the stream by four bytes. |
| 532 | */ |
| 533 | inline float BinaryReader::readFloat32LE() |
| 534 | { |
| 535 | m_stream->read(m_buffer, sizeof(float)); |
| 536 | return LE::toFloat32(m_buffer); |
| 537 | } |
| 538 | |
| 539 | /*! |
| 540 | * \brief Reads a 64-bit little endian floating point value from the current stream and advances the current position of the stream by eight bytes. |
| 541 | */ |
| 542 | inline double BinaryReader::readFloat64LE() |
| 543 | { |
| 544 | m_stream->read(m_buffer, sizeof(double)); |
| 545 | return LE::toFloat64(m_buffer); |
| 546 | } |
| 547 | |
| 548 | /*! |
| 549 | * \brief Reads a single character from the current stream and advances the current position of the stream by one byte. |
| 550 | */ |
| 551 | inline char BinaryReader::readChar() |
| 552 | { |
| 553 | m_stream->read(m_buffer, sizeof(char)); |
| 554 | return m_buffer[0]; |
| 555 | } |
| 556 | |
| 557 | /*! |
| 558 | * \brief Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte. |
| 559 | */ |
| 560 | inline uint8_t BinaryReader::readByte() |
| 561 | { |
| 562 | m_stream->read(m_buffer, sizeof(char)); |
| 563 | return static_cast<std::uint8_t>(m_buffer[0]); |
| 564 | } |
| 565 | |
| 566 | /*! |
| 567 | * \brief Reads a boolean value from the current stream and advances the current position of the stream by one byte. |
| 568 | * \sa BitReader |
| 569 | */ |
| 570 | inline bool BinaryReader::readBool() |
| 571 | { |
| 572 | return readByte() != 0; |
| 573 | } |
| 574 | |
| 575 | /*! |
| 576 | * \brief Reads a length prefixed string from the current stream. |
| 577 | * \remarks Reads the length prefix from the stream and then a string of the denoted length. |
| 578 | * Advances the current position of the stream by the denoted length of the string plus the prefix length. |
| 579 | */ |
| 580 | inline std::string BinaryReader::readLengthPrefixedString() |
| 581 | { |
| 582 | return readString(readVariableLengthUIntBE()); |
| 583 | } |
| 584 | |
| 585 | /*! |
| 586 | * \brief Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position of the stream by four bytes. |
| 587 | * \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file. |
| 588 | * \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a> |
| 589 | */ |
| 590 | inline std::uint32_t BinaryReader::readSynchsafeUInt32BE() |
| 591 | { |
| 592 | return toNormalInt(readUInt32BE()); |
| 593 | } |
| 594 | |
| 595 | /*! |
| 596 | * \brief Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit floating point value. |
| 597 | */ |
| 598 | inline float BinaryReader::readFixed8BE() |
| 599 | { |
| 600 | return toFloat32(readUInt16BE()); |
| 601 | } |
| 602 | |
| 603 | /*! |
| 604 | * \brief Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit floating point value. |
| 605 | */ |
| 606 | inline float BinaryReader::readFixed16BE() |
| 607 | { |
| 608 | return toFloat32(readUInt32BE()); |
| 609 | } |
| 610 | |
| 611 | /*! |
| 612 | * \brief Reads a 32-bit little endian synchsafe integer from the current stream and advances the current position of the stream by four bytes. |
| 613 | * \remarks Synchsafe integers appear in ID3 tags that are attached to an MP3 file. |
| 614 | * \sa <a href="http://id3.org/id3v2.4.0-structure">ID3 tag version 2.4.0 - Main Structure</a> |
| 615 | */ |
| 616 | inline std::uint32_t BinaryReader::readSynchsafeUInt32LE() |
| 617 | { |
| 618 | return toNormalInt(readUInt32LE()); |
| 619 | } |
| 620 | |
| 621 | /*! |
| 622 | * \brief Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit floating point value. |
| 623 | */ |
| 624 | inline float BinaryReader::readFixed8LE() |
| 625 | { |
| 626 | return toFloat32(readUInt16LE()); |
| 627 | } |
| 628 | |
| 629 | /*! |
| 630 | * \brief Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-bit floating point value. |
| 631 | */ |
| 632 | inline float BinaryReader::readFixed16LE() |
| 633 | { |
| 634 | return toFloat32(readUInt32LE()); |
| 635 | } |
| 636 | |
| 637 | /*! |
| 638 | * \brief Reads a single character from the current stream and advances the current position of the stream by one byte. |
| 639 | */ |
| 640 | inline void BinaryReader::read(char &oneCharacter) |
| 641 | { |
| 642 | oneCharacter = readChar(); |
| 643 | } |
| 644 | |
| 645 | /*! |
| 646 | * \brief Reads a single byte/unsigned character from the current stream and advances the current position of the stream by one byte. |
| 647 | */ |
| 648 | inline void BinaryReader::read(std::uint8_t &oneByte) |
| 649 | { |
| 650 | oneByte = readByte(); |
| 651 | } |
| 652 | |
| 653 | /*! |
| 654 | * \brief Reads a boolean value from the current stream and advances the current position of the stream by one byte. |
| 655 | * \sa BitReader |
| 656 | */ |
| 657 | inline void BinaryReader::read(bool &oneBool) |
| 658 | { |
| 659 | oneBool = readBool(); |
| 660 | } |
| 661 | |
| 662 | /*! |
| 663 | * \brief Reads a length prefixed string from the current stream. |
| 664 | * |
| 665 | * \remarks Reads the length prefix from the stream and then a string of the denoted length. |
| 666 | * Advances the current position of the stream by the denoted length of the string plus the prefix length. |
| 667 | */ |
| 668 | inline void BinaryReader::read(std::string &lengthPrefixedString) |
| 669 | { |
| 670 | lengthPrefixedString = readLengthPrefixedString(); |
| 671 | } |
| 672 | |
| 673 | /*! |
| 674 | * \brief Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. |
| 675 | */ |
| 676 | inline void BinaryReader::read(std::int16_t &one16BitInt) |
| 677 | { |
| 678 | one16BitInt = readInt16BE(); |
| 679 | } |
| 680 | |
| 681 | /*! |
| 682 | * \brief Reads a 16-bit big endian unsigned integer from the current stream and advances the current position of the stream by two bytes. |
| 683 | */ |
| 684 | inline void BinaryReader::read(std::uint16_t &one16BitUInt) |
| 685 | { |
| 686 | one16BitUInt = readUInt16BE(); |
| 687 | } |
| 688 | |
| 689 | /*! |
| 690 | * \brief Reads a 16-bit big endian signed integer from the current stream and advances the current position of the stream by two bytes. |
| 691 | */ |
| 692 | inline void BinaryReader::read(std::int32_t &one32BitInt) |
| 693 | { |
| 694 | one32BitInt = readInt32BE(); |
| 695 | } |
| 696 | |
| 697 | /*! |
| 698 | * \brief Reads a 32-bit big endian unsigned integer from the current stream and advances the current position of the stream by four bytes. |
| 699 | */ |
| 700 | inline void BinaryReader::read(std::uint32_t &one32BitUInt) |
| 701 | { |
| 702 | one32BitUInt = readUInt32BE(); |
| 703 | } |
| 704 | |
| 705 | /*! |
| 706 | * \brief Reads a 64-bit big endian signed integer from the current stream and advances the current position of the stream by eight bytes. |
| 707 | */ |
| 708 | inline void BinaryReader::read(std::int64_t &one64BitInt) |
| 709 | { |
| 710 | one64BitInt = readInt64BE(); |
| 711 | } |
| 712 | |
| 713 | /*! |
| 714 | * \brief Reads a 64-bit big endian unsigned integer from the current stream and advances the current position of the stream by eight bytes. |
| 715 | */ |
| 716 | inline void BinaryReader::read(std::uint64_t &one64BitUInt) |
| 717 | { |
| 718 | one64BitUInt = readUInt64BE(); |
| 719 | } |
| 720 | |
| 721 | /*! |
| 722 | * \brief Reads a 32-bit big endian floating point value from the current stream and advances the current position of the stream by four bytes. |
| 723 | */ |
| 724 | inline void BinaryReader::read(float &one32BitFloat) |
| 725 | { |
| 726 | one32BitFloat = readFloat32BE(); |
| 727 | } |
| 728 | |
| 729 | /*! |
| 730 | * \brief Reads a 64-bit big endian floating point value from the current stream and advances the current position of the stream by eight bytes. |
| 731 | */ |
| 732 | inline void BinaryReader::read(double &one64BitFloat) |
| 733 | { |
| 734 | one64BitFloat = readFloat64BE(); |
| 735 | } |
| 736 | } // namespace CppUtilities |
| 737 | |
| 738 | #endif // IOUTILITIES_BINERYREADER_H |