summaryrefslogtreecommitdiff
path: root/include/mbgl/util/pbf.hpp
blob: 1f78a0072f2f1d1d62ca0be3e0159f07bc8b92d6 (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
180
181
182
183
184
#ifndef MBGL_UTIL_PBF
#define MBGL_UTIL_PBF

/*
 * 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 <string>
#include <cstring>

namespace mbgl {

struct pbf {
    struct exception : std::exception { const char *what() const noexcept { return "pbf exception"; } };
    struct unterminated_varint_exception : exception { const char *what() const noexcept { return "pbf unterminated varint exception"; } };
    struct varint_too_long_exception : exception { const char *what() const noexcept { return "pbf varint too long exception"; } };
    struct unknown_field_type_exception : exception { const char *what() const noexcept { return "pbf unknown field type exception"; } };
    struct end_of_buffer_exception : exception { const char *what() const noexcept { return "pbf end of buffer exception"; } };

    inline pbf(const unsigned char *data, size_t length);
    inline pbf();

    inline operator bool() const;

    inline bool next();
    inline bool next(uint32_t tag);
    template <typename T = uint32_t> inline T varint();
    template <typename T = uint32_t> inline T svarint();

    template <typename T = uint32_t, int bytes = 4> inline T fixed();
    inline float float32();
    inline double float64();

    inline std::string string();
    inline bool boolean();

    inline pbf message();

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

    const uint8_t *data = nullptr;
    const uint8_t *end = nullptr;
    uint32_t value = 0;
    uint32_t tag = 0;
};

pbf::pbf(const unsigned char *data, size_t length)
    : data(data),
      end(data + length),
      value(0),
      tag(0) {
}

pbf::pbf()
    : data(nullptr),
      end(nullptr),
      value(0),
      tag(0) {
}


pbf::operator bool() const {
    return data < end;
}

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

bool pbf::next(uint32_t requested_tag) {
    while (next()) {
        if (tag == requested_tag) {
            return true;
        } else {
            skip();
        }
    }
    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) {
            throw unterminated_varint_exception();
        }
        result |= ((T)(byte = *data) & 0x7F) << bitpos;

        data++;
    }
    if (bitpos == 70 && (byte & 0x80)) {
        throw varint_too_long_exception();
    }

    return result;
}

template <typename T>
T pbf::svarint() {
    T n = varint<T>();
    return (n >> 1) ^ -(T)(n & 1);
}

template <typename T, int bytes>
T pbf::fixed() {
    skipBytes(bytes);
    T result;
    memcpy(&result, data - bytes, bytes);
    return result;
}

float pbf::float32() {
    return fixed<float, 4>();
}

double pbf::float64() {
    return fixed<double, 8>();
}

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

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

pbf pbf::message() {
    uint32_t bytes = static_cast<uint32_t>(varint());
    const uint8_t *pos = data;
    skipBytes(bytes);
    return pbf(pos, bytes);
}

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(static_cast<uint32_t>(varint()));
            break;
        case 5: // 32 bit
            skipBytes(4);
            break;
        default:
            throw unknown_field_type_exception();
    }
}

void pbf::skipBytes(uint32_t bytes) {
    if (data + bytes > end) {
        throw end_of_buffer_exception();
    }
    data += bytes;
}

} // end namespace mbgl

#endif