C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binaryreader.h
Go to the documentation of this file.
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 IoUtilities {
12 
13 public:
14  BinaryReader(std::istream *stream);
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  void read(char *buffer, std::streamsize length);
30  void read(byte *buffer, std::streamsize length);
31  void read(std::vector<char> &buffer, std::streamsize length);
32  int16 readInt16BE();
33  uint16 readUInt16BE();
34  int32 readInt24BE();
35  uint32 readUInt24BE();
36  int32 readInt32BE();
37  uint32 readUInt32BE();
38  int64 readInt40BE();
39  uint64 readUInt40BE();
40  int64 readInt56BE();
41  uint64 readUInt56BE();
42  int64 readInt64BE();
43  uint64 readUInt64BE();
44  uint64 readVariableLengthUIntBE();
45  float32 readFloat32BE();
46  float64 readFloat64BE();
47  int16 readInt16LE();
48  uint16 readUInt16LE();
49  int32 readInt24LE();
50  uint32 readUInt24LE();
51  int32 readInt32LE();
52  uint32 readUInt32LE();
53  int64 readInt40LE();
54  uint64 readUInt40LE();
55  int64 readInt56LE();
56  uint64 readUInt56LE();
57  int64 readInt64LE();
58  uint64 readUInt64LE();
59  uint64 readVariableLengthUIntLE();
60  float32 readFloat32LE();
61  float64 readFloat64LE();
62  char readChar();
63  byte readByte();
64  bool readBool();
65  std::string readLengthPrefixedString();
66  std::string readString(std::size_t length);
67  std::string readTerminatedString(byte termination = 0);
68  std::string readTerminatedString(size_t maxBytesToRead, byte termination = 0);
69  std::string readMultibyteTerminatedStringBE(uint16 termination = 0);
70  std::string readMultibyteTerminatedStringLE(uint16 termination = 0);
71  std::string readMultibyteTerminatedStringBE(std::size_t maxBytesToRead, uint16 termination = 0);
72  std::string readMultibyteTerminatedStringLE(std::size_t maxBytesToRead, uint16 termination = 0);
73  uint32 readSynchsafeUInt32BE();
74  float32 readFixed8BE();
75  float32 readFixed16BE();
76  uint32 readSynchsafeUInt32LE();
77  float32 readFixed8LE();
78  float32 readFixed16LE();
79  uint32 readCrc32(std::size_t length);
80  static uint32 computeCrc32(const char *buffer, std::size_t length);
81  static const uint32 crc32Table[];
82 
83  // declare further overloads for read() to ease use of BinaryReader in templates
84  void read(char &oneCharacter);
85  void read(byte &oneByte);
86  void read(bool &oneBool);
87  void read(std::string &lengthPrefixedString);
88  void read(int16 &one16BitInt);
89  void read(uint16 &one16BitUInt);
90  void read(int32 &one32BitInt);
91  void read(uint32 &one32BitUInt);
92  void read(int64 &one64BitInt);
93  void read(uint64 &one64BitUInt);
94  void read(float32 &one32BitFloat);
95  void read(float64 &one64BitFloat);
96 
97 private:
98  void bufferVariableLengthInteger();
99 
100  std::istream *m_stream;
101  bool m_ownership;
102  char m_buffer[8];
103 };
104 
110 inline std::istream *BinaryReader::stream()
111 {
112  return m_stream;
113 }
114 
120 inline const std::istream *BinaryReader::stream() const
121 {
122  return m_stream;
123 }
124 
132 inline bool BinaryReader::hasOwnership() const
133 {
134  return m_ownership;
135 }
136 
145 {
146  if (m_stream) {
147  m_ownership = true;
148  }
149 }
150 
159 {
160  m_ownership = false;
161 }
162 
166 inline bool BinaryReader::fail() const
167 {
168  return m_stream ? m_stream->fail() : false;
169 }
170 
174 inline bool BinaryReader::eof() const
175 {
176  return m_stream && m_stream->eof();
177 }
178 
182 inline bool BinaryReader::canRead() const
183 {
184  return m_stream && m_stream->good();
185 }
186 
190 inline void BinaryReader::read(char *buffer, std::streamsize length)
191 {
192  m_stream->read(buffer, length);
193 }
194 
198 inline void BinaryReader::read(byte *buffer, std::streamsize length)
199 {
200  m_stream->read(reinterpret_cast<char *>(buffer), length);
201 }
202 
206 inline void BinaryReader::read(std::vector<char> &buffer, std::streamsize length)
207 {
208  buffer.resize(static_cast<std::vector<char>::size_type>(length));
209  m_stream->read(buffer.data(), length);
210 }
211 
216 {
217  m_stream->read(m_buffer, sizeof(int16));
218  return ConversionUtilities::BE::toInt16(m_buffer);
219 }
220 
225 {
226  m_stream->read(m_buffer, sizeof(uint16));
227  return ConversionUtilities::BE::toUInt16(m_buffer);
228 }
229 
234 {
235  *m_buffer = 0;
236  m_stream->read(m_buffer + 1, 3);
237  auto val = ConversionUtilities::BE::toInt32(m_buffer);
238  if (val >= 0x800000) {
239  val = -(0x1000000 - val);
240  }
241  return val;
242 }
243 
248 {
249  *m_buffer = 0;
250  m_stream->read(m_buffer + 1, 3);
251  return ConversionUtilities::BE::toUInt32(m_buffer);
252 }
253 
258 {
259  m_stream->read(m_buffer, sizeof(int32));
260  return ConversionUtilities::BE::toInt32(m_buffer);
261 }
262 
267 {
268  m_stream->read(m_buffer, sizeof(uint32));
269  return ConversionUtilities::BE::toUInt32(m_buffer);
270 }
271 
276 {
277  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
278  m_stream->read(m_buffer + 3, 5);
279  auto val = ConversionUtilities::BE::toInt64(m_buffer);
280  if (val >= 0x8000000000) {
281  val = -(0x10000000000 - val);
282  }
283  return val;
284 }
285 
290 {
291  *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
292  m_stream->read(m_buffer + 3, 5);
293  return ConversionUtilities::BE::toUInt64(m_buffer);
294 }
295 
300 {
301  *m_buffer = 0;
302  m_stream->read(m_buffer + 1, 7);
303  auto val = ConversionUtilities::BE::toInt64(m_buffer);
304  if (val >= 0x80000000000000) {
305  val = -(0x100000000000000 - val);
306  }
307  return val;
308 }
309 
314 {
315  *m_buffer = 0;
316  m_stream->read(m_buffer + 1, 7);
317  return ConversionUtilities::BE::toUInt64(m_buffer);
318 }
319 
324 {
325  m_stream->read(m_buffer, sizeof(int64));
326  return ConversionUtilities::BE::toInt64(m_buffer);
327 }
328 
333 {
334  m_stream->read(m_buffer, sizeof(uint64));
335  return ConversionUtilities::BE::toUInt64(m_buffer);
336 }
337 
343 {
344  bufferVariableLengthInteger();
345  return ConversionUtilities::BE::toUInt64(m_buffer);
346 }
347 
352 {
353  m_stream->read(m_buffer, sizeof(float32));
354  return ConversionUtilities::BE::toFloat32(m_buffer);
355 }
356 
361 {
362  m_stream->read(m_buffer, sizeof(float64));
363  return ConversionUtilities::BE::toFloat64(m_buffer);
364 }
365 
370 {
371  m_stream->read(m_buffer, sizeof(int16));
372  return ConversionUtilities::LE::toInt16(m_buffer);
373 }
374 
379 {
380  m_stream->read(m_buffer, sizeof(uint16));
381  return ConversionUtilities::LE::toUInt16(m_buffer);
382 }
383 
388 {
389  *(m_buffer + 3) = 0;
390  m_stream->read(m_buffer, 3);
391  auto val = ConversionUtilities::LE::toInt32(m_buffer);
392  if (val >= 0x800000) {
393  val = -(0x1000000 - val);
394  }
395  return val;
396 }
397 
402 {
403  *(m_buffer + 3) = 0;
404  m_stream->read(m_buffer, 3);
405  return ConversionUtilities::LE::toUInt32(m_buffer);
406 }
407 
412 {
413  m_stream->read(m_buffer, sizeof(int32));
414  return ConversionUtilities::LE::toInt32(m_buffer);
415 }
416 
421 {
422  m_stream->read(m_buffer, sizeof(uint32));
423  return ConversionUtilities::LE::toUInt32(m_buffer);
424 }
425 
430 {
431  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
432  m_stream->read(m_buffer, 5);
433  auto val = ConversionUtilities::LE::toInt64(m_buffer);
434  if (val >= 0x8000000000) {
435  val = -(0x10000000000 - val);
436  }
437  return val;
438 }
439 
444 {
445  *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
446  m_stream->read(m_buffer, 5);
447  return ConversionUtilities::LE::toUInt64(m_buffer);
448 }
449 
454 {
455  *(m_buffer + 7) = 0;
456  m_stream->read(m_buffer, 7);
457  auto val = ConversionUtilities::LE::toInt64(m_buffer);
458  if (val >= 0x80000000000000) {
459  val = -(0x100000000000000 - val);
460  }
461  return val;
462 }
463 
468 {
469  *(m_buffer + 7) = 0;
470  m_stream->read(m_buffer, 7);
471  return ConversionUtilities::LE::toUInt64(m_buffer);
472 }
473 
478 {
479  m_stream->read(m_buffer, sizeof(int64));
480  return ConversionUtilities::LE::toInt64(m_buffer);
481 }
482 
487 {
488  m_stream->read(m_buffer, sizeof(uint64));
489  return ConversionUtilities::LE::toUInt64(m_buffer);
490 }
491 
497 {
498  bufferVariableLengthInteger();
499  return ConversionUtilities::LE::toUInt64(m_buffer);
500 }
501 
506 {
507  m_stream->read(m_buffer, sizeof(float32));
508  return ConversionUtilities::LE::toFloat32(m_buffer);
509 }
510 
515 {
516  m_stream->read(m_buffer, sizeof(float64));
517  return ConversionUtilities::LE::toFloat64(m_buffer);
518 }
519 
524 {
525  m_stream->read(m_buffer, sizeof(char));
526  return m_buffer[0];
527 }
528 
533 {
534  m_stream->read(m_buffer, sizeof(char));
535  return static_cast<byte>(m_buffer[0]);
536 }
537 
543 {
544  return readByte() != 0;
545 }
546 
553 {
555 }
556 
561 {
563 }
564 
569 {
571 }
572 
579 {
581 }
582 
587 {
589 }
590 
595 {
597 }
598 
602 inline void BinaryReader::read(char &oneCharacter)
603 {
604  oneCharacter = readChar();
605 }
606 
610 inline void BinaryReader::read(byte &oneByte)
611 {
612  oneByte = readByte();
613 }
614 
619 inline void BinaryReader::read(bool &oneBool)
620 {
621  oneBool = readBool();
622 }
623 
630 inline void BinaryReader::read(std::string &lengthPrefixedString)
631 {
632  lengthPrefixedString = readLengthPrefixedString();
633 }
634 
638 inline void BinaryReader::read(int16 &one16BitInt)
639 {
640  one16BitInt = readInt16BE();
641 }
642 
646 inline void BinaryReader::read(uint16 &one16BitUInt)
647 {
648  one16BitUInt = readUInt16BE();
649 }
650 
654 inline void BinaryReader::read(int32 &one32BitInt)
655 {
656  one32BitInt = readInt32BE();
657 }
658 
662 inline void BinaryReader::read(uint32 &one32BitUInt)
663 {
664  one32BitUInt = readUInt32BE();
665 }
666 
670 inline void BinaryReader::read(int64 &one64BitInt)
671 {
672  one64BitInt = readInt64BE();
673 }
674 
678 inline void BinaryReader::read(uint64 &one64BitUInt)
679 {
680  one64BitUInt = readUInt64BE();
681 }
682 
686 inline void BinaryReader::read(float32 &one32BitFloat)
687 {
688  one32BitFloat = readFloat32BE();
689 }
690 
694 inline void BinaryReader::read(float64 &one64BitFloat)
695 {
696  one64BitFloat = readFloat64BE();
697 }
698 } // namespace IoUtilities
699 
700 #endif // IOUTILITIES_BINERYREADER_H
int64 readInt64LE()
Reads a 64-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:477
CPP_UTILITIES_EXPORT constexpr uint16 toUInt16(const char *value)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array.
bool fail() const
Returns an indication whether the fail bit of the assigned stream is set.
Definition: binaryreader.h:166
bool readBool()
Reads a boolean value from the current stream and advances the current position of the stream by one ...
Definition: binaryreader.h:542
CPP_UTILITIES_EXPORT constexpr uint32 toUInt32(const char *value)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array.
float32 readFloat32BE()
Reads a 32-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:351
int64 readInt40BE()
Reads a 40-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:275
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
int16 readInt16LE()
Reads a 16-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:369
Reads primitive data types from a std::istream.
Definition: binaryreader.h:11
float64 readFloat64LE()
Reads a 64-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:514
CPP_UTILITIES_EXPORT constexpr int64 toInt64(const char *value)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array.
int32 readInt32LE()
Reads a 32-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:411
float32 readFloat32LE()
Reads a 32-bit little endian floating point value from the current stream and advances the current po...
Definition: binaryreader.h:505
#define CPP_UTILITIES_EXPORT
uint32 readUInt24BE()
Reads a 24-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:247
int16 readInt16BE()
Reads a 16-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:215
void read(char *buffer, std::streamsize length)
Reads the specified number of characters from the stream in the character array.
Definition: binaryreader.h:190
CPP_UTILITIES_EXPORT constexpr int32 toInt32(const char *value)
Returns a 32-bit signed integer converted from four bytes at a specified position in a char array.
int64 readInt64BE()
Reads a 64-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:323
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
CPP_UTILITIES_EXPORT constexpr int16 toInt16(const char *value)
Returns a 16-bit signed integer converted from two bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr int16 toInt16(const char *value)
Returns a 16-bit signed integer converted from two bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT float32 toFloat32(const char *value)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
float64 readFloat64BE()
Reads a 64-bit big endian floating point value from the current stream and advances the current posit...
Definition: binaryreader.h:360
int64 readInt56BE()
Reads a 56-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:299
uint64 readUInt56LE()
Reads a 56-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:467
bool hasOwnership() const
Returns whether the reader takes ownership over the assigned stream.
Definition: binaryreader.h:132
uint32 readUInt32LE()
Reads a 32-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:420
float32 readFixed16BE()
Reads a 16.16 fixed point big endian representation from the current stream and returns it as 32-bit ...
Definition: binaryreader.h:568
uint32 readUInt32BE()
Reads a 32-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:266
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
CPP_UTILITIES_EXPORT constexpr uint16 toUInt16(const char *value)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array.
uint64 readVariableLengthUIntLE()
Reads an up to 8 byte long little endian unsigned integer from the current stream and advances the cu...
Definition: binaryreader.h:496
uint64 readUInt40BE()
Reads a 40-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:289
CPP_UTILITIES_EXPORT float64 toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
uint64 readUInt64BE()
Reads a 64-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:332
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
uint16 readUInt16LE()
Reads a 16-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:378
void giveOwnership()
The reader will take ownership over the assigned stream.
Definition: binaryreader.h:144
uint16 readUInt16BE()
Reads a 16-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:224
void detatchOwnership()
The reader will not take ownership over the assigned stream.
Definition: binaryreader.h:158
CPP_UTILITIES_EXPORT float64 toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
CPP_UTILITIES_EXPORT constexpr uint64 toUInt64(const char *value)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array.
int32 readInt24LE()
Reads a 24-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:387
CPP_UTILITIES_EXPORT constexpr int64 toInt64(const char *value)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array.
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
CPP_UTILITIES_EXPORT constexpr int32 toInt32(const char *value)
Returns a 32-bit signed integer converted from four bytes at a specified position in a char array.
const std::istream * stream() const
Returns a pointer to the stream the reader will read from when calling one of the read-methods.
Definition: binaryreader.h:120
int32 readInt32BE()
Reads a 32-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:257
int32 readInt24BE()
Reads a 24-bit big endian signed integer from the current stream and advances the current position of...
Definition: binaryreader.h:233
uint32 readSynchsafeUInt32LE()
Reads a 32-bit little endian synchsafe integer from the current stream and advances the current posit...
Definition: binaryreader.h:578
bool canRead() const
Returns an indication whether a stream is assigned the reader can read from.
Definition: binaryreader.h:182
uint64 readVariableLengthUIntBE()
Reads an up to 8 byte long big endian unsigned integer from the current stream and advances the curre...
Definition: binaryreader.h:342
uint32 readUInt24LE()
Reads a 24-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:401
int64 readInt56LE()
Reads a 56-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:453
byte readByte()
Reads a single byte/unsigned character from the current stream and advances the current position of t...
Definition: binaryreader.h:532
CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint16 fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
char readChar()
Reads a single character from the current stream and advances the current position of the stream by o...
Definition: binaryreader.h:523
float32 readFixed8BE()
Reads a 8.8 fixed point big endian representation from the current stream and returns it as 32-bit fl...
Definition: binaryreader.h:560
CPP_UTILITIES_EXPORT constexpr uint64 toUInt64(const char *value)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array.
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
CPP_UTILITIES_EXPORT float32 toFloat32(const char *value)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
CPP_UTILITIES_EXPORT constexpr uint32 toNormalInt(uint32 synchsafeInt)
Returns a normal 32-bit integer converted from a 32-bit synchsafe integer.
uint64 readUInt64LE()
Reads a 64-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:486
float32 readFixed16LE()
Reads a 16.16 fixed point little endian representation from the current stream and returns it as 32-b...
Definition: binaryreader.h:594
bool eof() const
Returns an indication whether the end-of-stream bit of the assigned stream is set.
Definition: binaryreader.h:174
std::string readLengthPrefixedString()
Reads a length prefixed string from the current stream.
float32 readFixed8LE()
Reads a 8.8 fixed point little endian representation from the current stream and returns it as 32-bit...
Definition: binaryreader.h:586
uint64 readUInt56BE()
Reads a 56-bit big endian unsigned integer from the current stream and advances the current position ...
Definition: binaryreader.h:313
int64 readInt40LE()
Reads a 40-bit little endian signed integer from the current stream and advances the current position...
Definition: binaryreader.h:429
uint32 readSynchsafeUInt32BE()
Reads a 32-bit big endian synchsafe integer from the current stream and advances the current position...
Definition: binaryreader.h:552
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
CPP_UTILITIES_EXPORT constexpr uint32 toUInt32(const char *value)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array.
uint64 readUInt40LE()
Reads a 40-bit little endian unsigned integer from the current stream and advances the current positi...
Definition: binaryreader.h:443