encapsulate base64 encoding into class to remove static functions from global namespace
This commit is contained in:
parent
60987e6b9b
commit
0047040af9
@ -25,8 +25,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef OBJECTTOBASE64_H_
|
#ifndef OBJECT_TO_BASE64_H_
|
||||||
#define OBJECTTOBASE64_H_
|
#define OBJECT_TO_BASE64_H_
|
||||||
|
|
||||||
#include "../Util/StringUtil.h"
|
#include "../Util/StringUtil.h"
|
||||||
|
|
||||||
@ -39,61 +39,56 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
typedef
|
struct ObjectEncoder
|
||||||
boost::archive::iterators::base64_from_binary<
|
{
|
||||||
boost::archive::iterators::transform_width<const char *, 6, 8>
|
using base64_t = boost::archive::iterators::base64_from_binary<
|
||||||
> base64_t;
|
boost::archive::iterators::transform_width<const char *, 6, 8>>;
|
||||||
|
|
||||||
typedef
|
using binary_t = boost::archive::iterators::transform_width<
|
||||||
boost::archive::iterators::transform_width<
|
boost::archive::iterators::binary_from_base64<std::string::const_iterator>,
|
||||||
boost::archive::iterators::binary_from_base64<
|
8,
|
||||||
std::string::const_iterator>, 8, 6
|
6>;
|
||||||
> binary_t;
|
|
||||||
|
|
||||||
template<class ObjectT>
|
template <class ObjectT>
|
||||||
static void EncodeObjectToBase64(const ObjectT & object, std::string& encoded) {
|
static void EncodeToBase64(const ObjectT &object, std::string &encoded)
|
||||||
const char * char_ptr_to_object = (const char *)&object;
|
{
|
||||||
std::vector<unsigned char> data(sizeof(object));
|
const char *char_ptr_to_object = (const char *)&object;
|
||||||
std::copy(
|
std::vector<unsigned char> data(sizeof(object));
|
||||||
char_ptr_to_object,
|
std::copy(char_ptr_to_object, char_ptr_to_object + sizeof(ObjectT), data.begin());
|
||||||
char_ptr_to_object + sizeof(ObjectT),
|
|
||||||
data.begin()
|
|
||||||
);
|
|
||||||
|
|
||||||
unsigned char number_of_padded_chars = 0; // is in {0,1,2};
|
unsigned char number_of_padded_chars = 0; // is in {0,1,2};
|
||||||
while(data.size() % 3 != 0) {
|
while (data.size() % 3 != 0)
|
||||||
++number_of_padded_chars;
|
{
|
||||||
data.push_back(0x00);
|
++number_of_padded_chars;
|
||||||
|
data.push_back(0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_ASSERT_MSG(0 == data.size() % 3, "base64 input data size is not a multiple of 3!");
|
||||||
|
encoded.resize(sizeof(ObjectT));
|
||||||
|
encoded.assign(base64_t(&data[0]),
|
||||||
|
base64_t(&data[0] + (data.size() - number_of_padded_chars)));
|
||||||
|
replaceAll(encoded, "+", "-");
|
||||||
|
replaceAll(encoded, "/", "_");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_ASSERT_MSG(
|
template <class ObjectT>
|
||||||
0 == data.size() % 3,
|
static void DecodeFromBase64(const std::string &input, ObjectT &object)
|
||||||
"base64 input data size is not a multiple of 3!"
|
{
|
||||||
);
|
try
|
||||||
encoded.resize(sizeof(ObjectT));
|
{
|
||||||
encoded.assign(
|
std::string encoded(input);
|
||||||
base64_t( &data[0] ),
|
// replace "-" with "+" and "_" with "/"
|
||||||
base64_t( &data[0] + (data.size() - number_of_padded_chars) )
|
replaceAll(encoded, "-", "+");
|
||||||
);
|
replaceAll(encoded, "_", "/");
|
||||||
replaceAll(encoded, "+", "-");
|
|
||||||
replaceAll(encoded, "/", "_");
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ObjectT>
|
std::copy(binary_t(encoded.begin()),
|
||||||
static void DecodeObjectFromBase64(const std::string& input, ObjectT & object) {
|
binary_t(encoded.begin() + encoded.length() - 1),
|
||||||
try {
|
(char *)&object);
|
||||||
std::string encoded(input);
|
}
|
||||||
//replace "-" with "+" and "_" with "/"
|
catch (...)
|
||||||
replaceAll(encoded, "-", "+");
|
{
|
||||||
replaceAll(encoded, "_", "/");
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
std::copy (
|
#endif /* OBJECT_TO_BASE64_H_ */
|
||||||
binary_t( encoded.begin() ),
|
|
||||||
binary_t( encoded.begin() + encoded.length() - 1),
|
|
||||||
(char *)&object
|
|
||||||
);
|
|
||||||
|
|
||||||
} catch(...) { }
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* OBJECTTOBASE64_H_ */
|
|
||||||
|
@ -283,10 +283,10 @@ template <class DataFacadeT> class JSONDescriptor : public BaseDescriptor<DataFa
|
|||||||
std::string hint;
|
std::string hint;
|
||||||
for (const auto i : osrm::irange<std::size_t>(0, raw_route.segment_end_coordinates.size()))
|
for (const auto i : osrm::irange<std::size_t>(0, raw_route.segment_end_coordinates.size()))
|
||||||
{
|
{
|
||||||
EncodeObjectToBase64(raw_route.segment_end_coordinates[i].source_phantom, hint);
|
ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates[i].source_phantom, hint);
|
||||||
json_location_hint_array.values.push_back(hint);
|
json_location_hint_array.values.push_back(hint);
|
||||||
}
|
}
|
||||||
EncodeObjectToBase64(raw_route.segment_end_coordinates.back().target_phantom, hint);
|
ObjectEncoder::EncodeToBase64(raw_route.segment_end_coordinates.back().target_phantom, hint);
|
||||||
json_location_hint_array.values.push_back(hint);
|
json_location_hint_array.values.push_back(hint);
|
||||||
json_hint_object.values["locations"] = json_location_hint_array;
|
json_hint_object.values["locations"] = json_location_hint_array;
|
||||||
json_result.values["hint_data"] = json_hint_object;
|
json_result.values["hint_data"] = json_hint_object;
|
||||||
|
@ -100,7 +100,7 @@ template <class DataFacadeT> class DistanceTablePlugin : public BasePlugin
|
|||||||
!route_parameters.hints[i].empty())
|
!route_parameters.hints[i].empty())
|
||||||
{
|
{
|
||||||
PhantomNode current_phantom_node;
|
PhantomNode current_phantom_node;
|
||||||
DecodeObjectFromBase64(route_parameters.hints[i], current_phantom_node);
|
ObjectEncoder::DecodeFromBase64(route_parameters.hints[i], current_phantom_node);
|
||||||
if (current_phantom_node.isValid(facade->GetNumberOfNodes()))
|
if (current_phantom_node.isValid(facade->GetNumberOfNodes()))
|
||||||
{
|
{
|
||||||
phantom_node_vector[i].emplace_back(std::move(current_phantom_node));
|
phantom_node_vector[i].emplace_back(std::move(current_phantom_node));
|
||||||
|
@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "BasePlugin.h"
|
#include "BasePlugin.h"
|
||||||
|
|
||||||
#include "../Algorithms/ObjectToBase64.h"
|
#include "../Algorithms/ObjectToBase64.h"
|
||||||
|
|
||||||
#include "../DataStructures/QueryEdge.h"
|
#include "../DataStructures/QueryEdge.h"
|
||||||
#include "../DataStructures/SearchEngine.h"
|
#include "../DataStructures/SearchEngine.h"
|
||||||
#include "../Descriptors/BaseDescriptor.h"
|
#include "../Descriptors/BaseDescriptor.h"
|
||||||
@ -99,7 +98,7 @@ template <class DataFacadeT> class ViaRoutePlugin : public BasePlugin
|
|||||||
if (checksum_OK && i < route_parameters.hints.size() &&
|
if (checksum_OK && i < route_parameters.hints.size() &&
|
||||||
!route_parameters.hints[i].empty())
|
!route_parameters.hints[i].empty())
|
||||||
{
|
{
|
||||||
DecodeObjectFromBase64(route_parameters.hints[i], phantom_node_vector[i]);
|
ObjectEncoder::DecodeFromBase64(route_parameters.hints[i], phantom_node_vector[i]);
|
||||||
if (phantom_node_vector[i].isValid(facade->GetNumberOfNodes()))
|
if (phantom_node_vector[i].isValid(facade->GetNumberOfNodes()))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
Reference in New Issue
Block a user