1 #ifndef IOUTILITIES_BINERYREADER_H
2 #define IOUTILITIES_BINERYREADER_H
4 #include "../conversion/binaryconversion.h"
14 BinaryReader(std::istream *stream,
bool giveOwnership =
false);
19 const std::istream *stream()
const;
20 std::istream *stream();
21 void setStream(std::istream *stream,
bool giveOwnership =
false);
22 bool hasOwnership()
const;
24 void detatchOwnership();
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();
64 std::uint8_t readByte();
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();
72 float readFixed16BE();
73 std::uint32_t readSynchsafeUInt32LE();
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[];
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);
95 void bufferVariableLengthInteger();
97 std::istream *m_stream;
109 , m_ownership(giveOwnership)
118 : m_stream(other.m_stream)
196 return m_stream ? m_stream->fail() :
false;
204 return m_stream && m_stream->eof();
212 return m_stream && m_stream->good();
220 m_stream->read(buffer, length);
228 m_stream->read(
reinterpret_cast<char *
>(buffer), length);
236 buffer.resize(
static_cast<std::vector<char>::size_type
>(length));
237 m_stream->read(buffer.data(), length);
245 m_stream->read(m_buffer,
sizeof(std::int16_t));
246 return BE::toInt16(m_buffer);
254 m_stream->read(m_buffer,
sizeof(std::uint16_t));
255 return BE::toUInt16(m_buffer);
264 m_stream->read(m_buffer + 1, 3);
265 auto val = BE::toInt32(m_buffer);
266 if (val >= 0x800000) {
267 val = -(0x1000000 - val);
278 m_stream->read(m_buffer + 1, 3);
279 return BE::toUInt32(m_buffer);
287 m_stream->read(m_buffer,
sizeof(std::int32_t));
288 return BE::toInt32(m_buffer);
296 m_stream->read(m_buffer,
sizeof(std::uint32_t));
297 return BE::toUInt32(m_buffer);
305 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
306 m_stream->read(m_buffer + 3, 5);
307 auto val = BE::toInt64(m_buffer);
308 if (val >= 0x8000000000) {
309 val = -(0x10000000000 - val);
319 *m_buffer = *(m_buffer + 1) = *(m_buffer + 2) = 0;
320 m_stream->read(m_buffer + 3, 5);
321 return BE::toUInt64(m_buffer);
330 m_stream->read(m_buffer + 1, 7);
331 auto val = BE::toInt64(m_buffer);
332 if (val >= 0x80000000000000) {
333 val = -(0x100000000000000 - val);
344 m_stream->read(m_buffer + 1, 7);
345 return BE::toUInt64(m_buffer);
353 m_stream->read(m_buffer,
sizeof(std::int64_t));
354 return BE::toInt64(m_buffer);
362 m_stream->read(m_buffer,
sizeof(std::uint64_t));
363 return BE::toUInt64(m_buffer);
372 bufferVariableLengthInteger();
373 return BE::toUInt64(m_buffer);
381 m_stream->read(m_buffer,
sizeof(
float));
390 m_stream->read(m_buffer,
sizeof(
double));
391 return BE::toFloat64(m_buffer);
399 m_stream->read(m_buffer,
sizeof(std::int16_t));
400 return LE::toInt16(m_buffer);
408 m_stream->read(m_buffer,
sizeof(std::uint16_t));
409 return LE::toUInt16(m_buffer);
418 m_stream->read(m_buffer, 3);
419 auto val = LE::toInt32(m_buffer);
420 if (val >= 0x800000) {
421 val = -(0x1000000 - val);
432 m_stream->read(m_buffer, 3);
433 return LE::toUInt32(m_buffer);
441 m_stream->read(m_buffer,
sizeof(std::int32_t));
442 return LE::toInt32(m_buffer);
450 m_stream->read(m_buffer,
sizeof(std::uint32_t));
451 return LE::toUInt32(m_buffer);
459 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
460 m_stream->read(m_buffer, 5);
461 auto val = LE::toInt64(m_buffer);
462 if (val >= 0x8000000000) {
463 val = -(0x10000000000 - val);
473 *(m_buffer + 5) = *(m_buffer + 6) = *(m_buffer + 7) = 0;
474 m_stream->read(m_buffer, 5);
475 return LE::toUInt64(m_buffer);
484 m_stream->read(m_buffer, 7);
485 auto val = LE::toInt64(m_buffer);
486 if (val >= 0x80000000000000) {
487 val = -(0x100000000000000 - val);
498 m_stream->read(m_buffer, 7);
499 return LE::toUInt64(m_buffer);
507 m_stream->read(m_buffer,
sizeof(std::int64_t));
508 return LE::toInt64(m_buffer);
516 m_stream->read(m_buffer,
sizeof(std::uint64_t));
517 return LE::toUInt64(m_buffer);
526 bufferVariableLengthInteger();
527 return LE::toUInt64(m_buffer);
535 m_stream->read(m_buffer,
sizeof(
float));
544 m_stream->read(m_buffer,
sizeof(
double));
545 return LE::toFloat64(m_buffer);
553 m_stream->read(m_buffer,
sizeof(
char));
562 m_stream->read(m_buffer,
sizeof(
char));
563 return static_cast<std::uint8_t
>(m_buffer[0]);
738 #endif // IOUTILITIES_BINERYREADER_H