Vanetza
Loading...
Searching...
No Matches
country_data_reader.cpp
1#include <vanetza/common/byte_order.hpp>
2#include <vanetza/geodesy/country_data_reader.hpp>
3#include <cstring>
4
5namespace vanetza
6{
7namespace geodesy
8{
9namespace detail
10{
11
12uint16_t read_u16le(const uint8_t* p)
13{
14 uint16_t v;
15 std::memcpy(&v, p, sizeof(v));
16 return endian_cast<ByteOrder::LittleEndian>(v).host();
17}
18
19uint32_t read_u32le(const uint8_t* p)
20{
21 uint32_t v;
22 std::memcpy(&v, p, sizeof(v));
23 return endian_cast<ByteOrder::LittleEndian>(v).host();
24}
25
26namespace
27{
28
29static constexpr std::size_t wkb_header_length = 5; /*< byte order (1) and type (4) */
30static constexpr std::size_t wkb_count_length = 4; /*< counters are uint32_t (4) */
31static constexpr std::uint32_t wkb_polygon_type = 3;
32static constexpr std::uint32_t wkb_multipolygon_type = 6;
33
34uint32_t read_u32(const uint8_t* p, bool little_endian)
35{
36 uint32_t v;
37 std::memcpy(&v, p, sizeof(v));
38 return little_endian
39 ? endian_cast<ByteOrder::LittleEndian>(v).host()
40 : endian_cast<ByteOrder::BigEndian>(v).host();
41}
42
43double read_f64(const uint8_t* p, bool little_endian)
44{
45 uint64_t bits;
46 std::memcpy(&bits, p, sizeof(bits));
47 bits = little_endian
48 ? endian_cast<ByteOrder::LittleEndian>(bits).host()
49 : endian_cast<ByteOrder::BigEndian>(bits).host();
50 double v;
51 std::memcpy(&v, &bits, sizeof(v));
52 return v;
53}
54
55// Parse a WKB Polygon body (after byte-order and type fields).
56// On success, `consumed` receives the number of bytes consumed from `data`.
57// Positions in the returned result are relative to `data` (body start).
58CountryReaderResult parse_polygon_body(const uint8_t* data, std::size_t length, bool le, country::Polygon& out)
59{
60 if (length < wkb_count_length) {
61 return CountryReaderResult::failure("truncated polygon: missing ring count", 0);
62 }
63
64 uint32_t num_rings = read_u32(data, le);
65 std::size_t offset = wkb_count_length;
66
67 for (uint32_t r = 0; r < num_rings; ++r) {
68 if (length - offset < wkb_count_length) {
69 return CountryReaderResult::failure("truncated polygon: missing point count", offset);
70 }
71
72 uint32_t num_points = read_u32(data + offset, le);
73 offset += wkb_count_length;
74
75 std::size_t points_bytes = static_cast<std::size_t>(num_points) * 2 * sizeof(double);
76 if (length - offset < points_bytes) {
77 return CountryReaderResult::failure("truncated polygon: missinsg point data", offset);
78 }
79
80 country::Ring ring;
81 ring.reserve(num_points);
82 for (uint32_t i = 0; i < num_points; ++i) {
83 double lon = read_f64(data + offset, le);
84 offset += sizeof(double);
85 double lat = read_f64(data + offset, le);
86 offset += sizeof(double);
87 ring.push_back(country::Point(lon, lat));
88 }
89
90 if (r == 0) {
91 out.outer() = std::move(ring);
92 } else {
93 out.inners().push_back(std::move(ring));
94 }
95 }
96
97 return CountryReaderResult::success(offset);
98}
99
100} // anonymous namespace
101
102CountryReaderResult parse_wkb(const uint8_t* data, std::size_t length, CountryPolygon& out)
103{
104 if (length < wkb_header_length) {
105 return CountryReaderResult::failure("WKB too short for header", 0);
106 }
107
108 bool le = (data[0] == 0x01);
109 uint32_t type = read_u32(data + 1, le);
110
111 if (type == wkb_polygon_type) {
112 country::Polygon polygon;
113 auto inner = parse_polygon_body(data + wkb_header_length, length - wkb_header_length, le, polygon);
114 if (inner.ok()) {
115 out.push_back(std::move(polygon));
116 }
117 return inner.add_offset(wkb_header_length);
118 } else if (type == wkb_multipolygon_type) {
119 if (length < wkb_header_length + wkb_count_length) {
120 return CountryReaderResult::failure("truncated multi-polygon: missing polygon count", wkb_header_length);
121 }
122 uint32_t num_polygons = read_u32(data + wkb_header_length, le);
123 std::size_t offset = wkb_header_length + wkb_count_length;
124
125 for (uint32_t i = 0; i < num_polygons; ++i) {
126 if (length - offset < wkb_header_length) {
127 return CountryReaderResult::failure("truncated multi-polygon: missing polygon header", offset);
128 }
129
130 bool poly_le = (data[offset] == 0x01);
131 uint32_t poly_type = read_u32(data + offset + 1, poly_le);
132 if (poly_type != wkb_polygon_type) {
133 return CountryReaderResult::failure("unexpected geometry type inside multi-polygon", offset + 1);
134 }
135 offset += wkb_header_length;
136
137 country::Polygon polygon;
138 auto inner = parse_polygon_body(data + offset, length - offset, poly_le, polygon);
139 if (inner.ok()) {
140 out.push_back(std::move(polygon));
141 offset += inner.position();
142 } else {
143 return inner.add_offset(offset);
144 }
145 }
146
147 return CountryReaderResult::success(offset);
148 } else {
149 return CountryReaderResult::failure("unsupported WKB geometry type" , 1);
150 }
151}
152
153} // namespace detail
154} // namespace geodesy
155} // namespace vanetza