summaryrefslogtreecommitdiff
path: root/src/map/pbf.hpp
blob: 29c252e1151ff5c64f1a7c9a59e3ba207f265d62 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once

/*
 * Some parts are from upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2008-2011 Google Inc.  See LICENSE for details.
 * Author: Josh Haberman <jhaberman@gmail.com>
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
// #include <cassert>

namespace llmr {

struct pbf {
	inline pbf(const unsigned char *data, uint32_t length);
	inline pbf(const char *data, uint32_t length);
	// inline pbf(const std::string& buffer);

	inline bool next();
	template <typename T = uint32_t> inline T varint();
	inline int64_t svarint();
	inline std::string string();
	inline float float32();
	inline double float64();
	inline int64_t int64();
	inline bool boolean();

	template <typename T, typename... Args>
	inline T *message(const Args&... args);

	inline void skip();
	inline void skipValue(uint32_t val);
	inline void skipBytes(uint32_t bytes);

	const uint8_t *data;
	const uint8_t *end;
	uint32_t value;
	uint32_t tag;
};

pbf::pbf(const unsigned char *data, uint32_t length)
	: data(data),
	  end(data + length)
{}

pbf::pbf(const char *data, uint32_t length)
	: data((const unsigned char *)data),
	  end((const unsigned char *)data + length)
{}

// pbf::pbf(const std::string& buffer)
// 	: data((const unsigned char *)buffer.data()),
// 	  end((const unsigned char *)buffer.data() + buffer.size())
// {}

bool pbf::next()
{
	if (data < end) {
		value = (uint32_t)varint();
		tag = value >> 3;
		return true;
	}
	return false;
}


template <typename T>
T pbf::varint()
{
	uint8_t byte = 0x80;
	T result = 0;
	int bitpos;
	for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
		if (data >= end) {
			fprintf(stderr, "unterminated varint, unexpected end of buffer");
			exit(1);
		}
		result |= ((T)(byte = *data) & 0x7F) << bitpos;

		data++;
	}
	if (bitpos == 70 && (byte & 0x80)) {
		fprintf(stderr, "unterminated varint (too long)");
		exit(1);
	}

	return result;
}

int64_t pbf::svarint()
{
	uint64_t n = varint();
	return (n >> 1) ^ -(int64_t)(n & 1);
}

std::string pbf::string()
{
	uint32_t bytes = (uint32_t)varint();
	const char *string = (const char *)data;
	skipBytes(bytes);
	return std::string(string, bytes);
}

float pbf::float32()
{
	skipBytes(4);
	float result;
	memcpy(&result, data - 4, 4);
	return result;
}
double pbf::float64()
{
	skipBytes(8);
	double result;
	memcpy(&result, data - 8, 8);
	return result;
}

int64_t pbf::int64()
{
	return (int64_t)varint();
}

bool pbf::boolean()
{
	skipBytes(1);
	return *(bool *)(data - 1);
}


template <typename T, typename... Args> T *pbf::message(const Args&... args)
{
	uint32_t bytes = (uint32_t)varint();
    T *result = new T(data, bytes, args...);
    skipBytes(bytes);
    return result;
}

void pbf::skip()
{
	skipValue(value);
}

void pbf::skipValue(uint32_t val)
{
	switch (val & 0x7) {
		case 0: // varint
			varint();
			break;
		case 1: // 64 bit
			skipBytes(8);
			break;
		case 2: // string/message
			skipBytes((uint32_t)varint());
			break;
		case 5: // 32 bit
			skipBytes(4);
			break;
		default:
			fprintf(stderr, "cannot skip unknown type %d", val & 0x7);
			exit(1);
	}
}

void pbf::skipBytes(uint32_t bytes)
{
	data += bytes;
	if (data > end) {
		fprintf(stderr, "unexpected end of buffer");
		exit(1);
	}
}

} // end namespace llmr