crc.hpp
Go to the documentation of this file.
#ifndef OSMIUM_OSM_CRC_HPP
#define OSMIUM_OSM_CRC_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cstdint>
#include <osmium/osm/area.hpp>
#include <osmium/osm/changeset.hpp>
#include <osmium/osm/location.hpp>
#include <osmium/osm/node.hpp>
#include <osmium/osm/node_ref_list.hpp>
#include <osmium/osm/relation.hpp>
#include <osmium/osm/way.hpp>
#include <osmium/util/endian.hpp>
namespace osmium {
namespace util {
inline uint16_t byte_swap_16(uint16_t value) noexcept {
# if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap16(value);
# else
return (value >> 8) | (value << 8);
# endif
}
inline uint32_t byte_swap_32(uint32_t value) noexcept {
# if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap32(value);
# else
return (value >> 24) |
((value >> 8) & 0x0000FF00) |
((value << 8) & 0x00FF0000) |
(value << 24);
# endif
}
inline uint64_t byte_swap_64(uint64_t value) noexcept {
# if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap64(value);
# else
uint64_t val1 = byte_swap_32(value & 0xFFFFFFFF);
uint64_t val2 = byte_swap_32(value >> 32);
return (val1 << 32) | val2;
# endif
}
} // namespace util
template <typename TCRC>
class CRC {
TCRC m_crc;
public:
TCRC& operator()() {
return m_crc;
}
const TCRC& operator()() const {
return m_crc;
}
void update_bool(const bool value) {
m_crc.process_byte(value);
}
void update_int8(const uint8_t value) {
m_crc.process_byte(value);
}
void update_int16(const uint16_t value) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
m_crc.process_bytes(&value, sizeof(uint16_t));
#else
uint16_t v = osmium::util::byte_swap_16(value);
m_crc.process_bytes(&v, sizeof(uint16_t));
#endif
}
void update_int32(const uint32_t value) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
m_crc.process_bytes(&value, sizeof(uint32_t));
#else
uint32_t v = osmium::util::byte_swap_32(value);
m_crc.process_bytes(&v, sizeof(uint32_t));
#endif
}
void update_int64(const uint64_t value) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
m_crc.process_bytes(&value, sizeof(uint64_t));
#else
uint64_t v = osmium::util::byte_swap_64(value);
m_crc.process_bytes(&v, sizeof(uint64_t));
#endif
}
void update_string(const char* str) {
while (*str) {
m_crc.process_byte(*str++);
}
}
void update(const Timestamp& timestamp) {
update_int32(uint32_t(timestamp));
}
void update(const osmium::Location& location) {
update_int32(location.x());
update_int32(location.y());
}
void update(const osmium::Box& box) {
update(box.bottom_left());
update(box.top_right());
}
void update(const NodeRef& node_ref) {
update_int64(node_ref.ref());
}
void update(const NodeRefList& node_refs) {
for (const NodeRef& node_ref : node_refs) {
update(node_ref);
}
}
void update(const TagList& tags) {
for (const Tag& tag : tags) {
update_string(tag.key());
update_string(tag.value());
}
}
void update(const osmium::RelationMember& member) {
update_int64(member.ref());
update_int16(uint16_t(member.type()));
update_string(member.role());
}
void update(const osmium::RelationMemberList& members) {
for (const RelationMember& member : members) {
update(member);
}
}
void update(const osmium::OSMObject& object) {
update_int64(object.id());
update_bool(object.visible());
update_int32(object.version());
update(object.timestamp());
update_int32(object.uid());
update_string(object.user());
update(object.tags());
}
void update(const osmium::Node& node) {
update(static_cast<const osmium::OSMObject&>(node));
update(node.location());
}
void update(const osmium::Way& way) {
update(static_cast<const osmium::OSMObject&>(way));
update(way.nodes());
}
void update(const osmium::Relation& relation) {
update(static_cast<const osmium::OSMObject&>(relation));
update(relation.members());
}
void update(const osmium::Area& area) {
update(static_cast<const osmium::OSMObject&>(area));
for (auto it = area.cbegin(); it != area.cend(); ++it) {
if (it->type() == osmium::item_type::outer_ring ||
it->type() == osmium::item_type::inner_ring) {
update(static_cast<const osmium::NodeRefList&>(*it));
}
}
}
void update(const osmium::ChangesetDiscussion& discussion) {
for (const auto& comment : discussion) {
update(comment.date());
update_int32(comment.uid());
update_string(comment.user());
update_string(comment.text());
}
}
void update(const osmium::Changeset& changeset) {
update_int64(changeset.id());
update(changeset.created_at());
update(changeset.closed_at());
update(changeset.bounds());
update_int32(changeset.num_changes());
update_int32(changeset.num_comments());
update_int32(changeset.uid());
update_string(changeset.user());
update(changeset.tags());
update(changeset.discussion());
}
}; // class CRC
} // namespace osmium
#endif // OSMIUM_OSM_CRC
osmium::Way::nodes
WayNodeList & nodes()
Definition: way.hpp:75
osmium::CRC::update
void update(const osmium::Relation &relation)
Definition: crc.hpp:201
osmium::CRC::update_int8
void update_int8(const uint8_t value)
Definition: crc.hpp:101
osmium::Tag
Definition: tag.hpp:48
osmium::ChangesetDiscussion
Definition: changeset.hpp:128
osmium::CRC::update_int16
void update_int16(const uint16_t value)
Definition: crc.hpp:105
osmium::CRC::update
void update(const osmium::Way &way)
Definition: crc.hpp:196
osmium::item_type::outer_ring
osmium::Changeset::num_comments
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:320
osmium::TagList
Definition: tag.hpp:105
location.hpp
osmium::Changeset::bounds
osmium::Box & bounds() noexcept
Definition: changeset.hpp:340
osmium::Relation::members
RelationMemberList & members()
Definition: relation.hpp:177
osmium::CRC::update
void update(const osmium::Changeset &changeset)
Definition: crc.hpp:225
osmium::item_type::inner_ring
osmium::CRC::update_int64
void update_int64(const uint64_t value)
Definition: crc.hpp:123
osmium::Changeset::uid
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:214
osmium::Changeset::tags
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:359
osmium::Box::top_right
constexpr Location top_right() const noexcept
Definition: box.hpp:178
osmium::Relation
Definition: relation.hpp:165
osmium::OSMObject::cend
const_iterator cend() const
Definition: object.hpp:346
osmium::CRC::update
void update(const Timestamp &timestamp)
Definition: crc.hpp:138
osmium::Changeset::user
const char * user() const
Get user name.
Definition: changeset.hpp:354
osmium::Changeset::id
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:188
node_ref_list.hpp
osmium::Area
Definition: area.hpp:113
osmium::RelationMemberList
Definition: relation.hpp:148
osmium::CRC::update
void update(const osmium::OSMObject &object)
Definition: crc.hpp:181
osmium::CRC::update
void update(const TagList &tags)
Definition: crc.hpp:162
osmium::OSMObject::cbegin
const_iterator cbegin() const
Definition: object.hpp:342
osmium::CRC::m_crc
TCRC m_crc
Definition: crc.hpp:85
osmium::Way
Definition: way.hpp:65
osmium::CRC::update
void update(const osmium::RelationMember &member)
Definition: crc.hpp:169
osmium::Changeset::closed_at
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:267
osmium::NodeRef::ref
constexpr osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:65
way.hpp
osmium::RelationMember
Definition: relation.hpp:54
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
endian.hpp
osmium::Changeset::num_changes
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:304
area.hpp
osmium::CRC::update
void update(const osmium::Area &area)
Definition: crc.hpp:206
osmium::CRC::update
void update(const osmium::Box &box)
Definition: crc.hpp:147
osmium::CRC
Definition: crc.hpp:83
osmium::CRC::update
void update(const osmium::Location &location)
Definition: crc.hpp:142
osmium::Timestamp
Definition: timestamp.hpp:56
relation.hpp
osmium::Location::y
constexpr int32_t y() const noexcept
Definition: location.hpp:167
osmium::CRC::update
void update(const NodeRef &node_ref)
Definition: crc.hpp:152
osmium::CRC::operator()
const TCRC & operator()() const
Definition: crc.hpp:93
osmium::RelationMember::type
item_type type() const noexcept
Definition: relation.hpp:126
osmium::util::byte_swap_32
uint32_t byte_swap_32(uint32_t value) noexcept
Definition: crc.hpp:59
osmium::CRC::update_bool
void update_bool(const bool value)
Definition: crc.hpp:97
osmium::Changeset::created_at
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:257
osmium::Location
Definition: location.hpp:79
osmium::Box
Definition: box.hpp:50
osmium::RelationMember::ref
object_id_type ref() const noexcept
Definition: relation.hpp:108
osmium::Node::location
osmium::Location location() const noexcept
Definition: node.hpp:61
osmium::RelationMember::role
const char * role() const noexcept
Definition: relation.hpp:134
osmium::util::byte_swap_64
uint64_t byte_swap_64(uint64_t value) noexcept
Definition: crc.hpp:70
osmium::Box::bottom_left
constexpr Location bottom_left() const noexcept
Definition: box.hpp:164
osmium::Changeset::discussion
ChangesetDiscussion & discussion()
Definition: changeset.hpp:413
osmium::Node
Definition: node.hpp:47
node.hpp
osmium::NodeRefList
Definition: node_ref_list.hpp:50
osmium::Changeset
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:154
osmium::CRC::update
void update(const osmium::Node &node)
Definition: crc.hpp:191
osmium::CRC::update_int32
void update_int32(const uint32_t value)
Definition: crc.hpp:114
osmium::Location::x
constexpr int32_t x() const noexcept
Definition: location.hpp:163
osmium::CRC::update
void update(const osmium::ChangesetDiscussion &discussion)
Definition: crc.hpp:216
osmium::CRC::update
void update(const osmium::RelationMemberList &members)
Definition: crc.hpp:175
osmium::NodeRef
Definition: node_ref.hpp:50
osmium::CRC::update_string
void update_string(const char *str)
Definition: crc.hpp:132
osmium::util::byte_swap_16
uint16_t byte_swap_16(uint16_t value) noexcept
Definition: crc.hpp:51
changeset.hpp
osmium::CRC::operator()
TCRC & operator()()
Definition: crc.hpp:89
osmium::CRC::update
void update(const NodeRefList &node_refs)
Definition: crc.hpp:156
osmium::OSMObject
Definition: object.hpp:58