#ifndef OBJECT_ENCODER_HPP #define OBJECT_ENCODER_HPP #include #include #include #include #include #include #include #include #include #include namespace osrm { namespace engine { namespace detail { static_assert(CHAR_BIT == 8u, "we assume a byte holds 8 bits"); static_assert(sizeof(char) == 1u, "we assume a char is one byte large"); using Base64FromBinary = boost::archive::iterators::base64_from_binary< boost::archive::iterators::transform_width>; using BinaryFromBase64 = boost::archive::iterators::transform_width< boost::archive::iterators::binary_from_base64, 8, // get a view of 8 bit 6 // from a sequence of 6 bit >; } // ns detail template std::string encodeBase64(const T &x) { #if not defined __GNUC__ or __GNUC__ > 4 static_assert(std::is_trivially_copyable::value, "requires a trivially copyable type"); #endif std::vector bytes{reinterpret_cast(&x), reinterpret_cast(&x) + sizeof(T)}; BOOST_ASSERT(!bytes.empty()); const auto next_divisible_by_three = ((bytes.size() / 3u) + 1u) * 3u; BOOST_ASSERT(next_divisible_by_three >= bytes.size()); const auto bytes_to_pad = next_divisible_by_three - bytes.size(); BOOST_ASSERT(bytes_to_pad == 0 || bytes_to_pad == 1 || bytes_to_pad == 2); bytes.insert(end(bytes), bytes_to_pad, 0x00); BOOST_ASSERT_MSG(0 == bytes.size() % 3, "base64 input data size is not a multiple of 3"); std::string encoded{detail::Base64FromBinary{bytes.data()}, detail::Base64FromBinary{bytes.data() + (bytes.size() - bytes_to_pad)}}; std::replace(begin(encoded), end(encoded), '+', '-'); std::replace(begin(encoded), end(encoded), '/', '_'); return encoded; } template T decodeBase64(std::string encoded) { #if not defined __GNUC__ or __GNUC__ > 4 static_assert(std::is_trivially_copyable::value, "requires a trivially copyable type"); #endif std::replace(begin(encoded), end(encoded), '-', '+'); std::replace(begin(encoded), end(encoded), '_', '/'); T rv; std::copy(detail::BinaryFromBase64{begin(encoded)}, detail::BinaryFromBase64{begin(encoded) + encoded.length()}, reinterpret_cast(&rv)); return rv; } } // ns engine } // ns osrm #endif /* OBJECT_ENCODER_HPP */