summaryrefslogtreecommitdiff
path: root/lib/rawDataReader.cpp
blob: c0f32b58fa2e10b6a17cb03a7c58223cdd86a907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "rawDataReader.h"

namespace MyGrate {
	RawDataReader::RawDataReader(const void * const d, std::size_t l) :
		rawdata {static_cast<const std::byte *>(d)}, len {l}
	{
	}

	RawDataReader::RawDataReader(const MARIADB_STRING & str) : RawDataReader {str.str, str.length} { }

	void
	RawDataReader::offsetSizeCheck(size_t l) const
	{
		if (offset + l > len) {
			throw std::range_error("Read beyond end of data");
		}
	}

	void
	RawDataReader::discard(size_t d)
	{
		offset += d;
	}

	template<>
	uint64_t
	RawDataReader::readValue<PackedInteger>()
	{
		const auto byte1 {readValue<uint8_t>()};
		if (byte1 <= 250) {
			return byte1;
		}
		switch (byte1) {
			case 252: // 2 bytes
				return readValue<uint16_t, 2>();
			case 253: // 3 bytes
				return readValue<uint32_t, 3>();
			case 254: // 8 bytes
				return readValue<uint64_t, 8>();
			default:;
		}
		throw std::domain_error("Invalid first byte of packed integer");
	}
}