1#include <vanetza/common/byte_order.hpp>
2#include <vanetza/geodesy/country_data_reader.hpp>
12uint16_t read_u16le(
const uint8_t* p)
15 std::memcpy(&v, p,
sizeof(v));
16 return endian_cast<ByteOrder::LittleEndian>(v).host();
19uint32_t read_u32le(
const uint8_t* p)
22 std::memcpy(&v, p,
sizeof(v));
23 return endian_cast<ByteOrder::LittleEndian>(v).host();
29static constexpr std::size_t wkb_header_length = 5;
30static constexpr std::size_t wkb_count_length = 4;
31static constexpr std::uint32_t wkb_polygon_type = 3;
32static constexpr std::uint32_t wkb_multipolygon_type = 6;
34uint32_t read_u32(
const uint8_t* p,
bool little_endian)
37 std::memcpy(&v, p,
sizeof(v));
39 ? endian_cast<ByteOrder::LittleEndian>(v).host()
40 : endian_cast<ByteOrder::BigEndian>(v).host();
43double read_f64(
const uint8_t* p,
bool little_endian)
46 std::memcpy(&bits, p,
sizeof(bits));
48 ? endian_cast<ByteOrder::LittleEndian>(bits).host()
49 : endian_cast<ByteOrder::BigEndian>(bits).host();
51 std::memcpy(&v, &bits,
sizeof(v));
58CountryReaderResult parse_polygon_body(
const uint8_t* data, std::size_t length,
bool le, country::Polygon& out)
60 if (length < wkb_count_length) {
64 uint32_t num_rings = read_u32(data, le);
65 std::size_t offset = wkb_count_length;
67 for (uint32_t r = 0; r < num_rings; ++r) {
68 if (length - offset < wkb_count_length) {
72 uint32_t num_points = read_u32(data + offset, le);
73 offset += wkb_count_length;
75 std::size_t points_bytes =
static_cast<std::size_t>(num_points) * 2 *
sizeof(
double);
76 if (length - offset < points_bytes) {
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));
91 out.outer() = std::move(ring);
93 out.inners().push_back(std::move(ring));
104 if (length < wkb_header_length) {
108 bool le = (data[0] == 0x01);
109 uint32_t type = read_u32(data + 1, le);
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);
115 out.push_back(std::move(polygon));
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);
122 uint32_t num_polygons = read_u32(data + wkb_header_length, le);
123 std::size_t offset = wkb_header_length + wkb_count_length;
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);
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);
135 offset += wkb_header_length;
137 country::Polygon polygon;
138 auto inner = parse_polygon_body(data + offset, length - offset, poly_le, polygon);
140 out.push_back(std::move(polygon));
141 offset += inner.position();
143 return inner.add_offset(offset);