OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
base64.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <boost/algorithm/string.hpp>
20 #include <boost/archive/iterators/base64_from_binary.hpp>
21 #include <boost/archive/iterators/binary_from_base64.hpp>
22 #include <boost/archive/iterators/transform_width.hpp>
23 #include <string>
24 
25 namespace shared {
26 
27 inline std::string decode_base64(const std::string& val, bool trim_nulls) {
28  // For some strings, signatature particualary it is important no to trim
29  // '\0' characters
30  using namespace boost::archive::iterators;
31  using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
32 
33  if (!trim_nulls) {
34  return std::string(It(val.begin()), It(val.end()));
35  }
36  return boost::algorithm::trim_right_copy_if(
37  std::string(It(std::begin(val)), It(std::end(val))),
38  [](char c) { return c == '\0'; });
39 }
40 
41 inline std::string decode_base64(const std::string& val) {
42  return decode_base64(val, true);
43 }
44 
45 static inline std::string encode_base64(const std::string& val) {
46  using namespace boost::archive::iterators;
47  using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
48  auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
49  return tmp.append((3 - val.size() % 3) % 3, '=');
50 }
51 
52 std::string decode_base64_uri(const std::string& value, bool trim_nulls = true);
53 
54 } // namespace shared
std::string decode_base64_uri(const std::string &data, bool trim_nulls)
Definition: base64.cpp:21
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:27
static std::string encode_base64(const std::string &val)
Definition: base64.h:45