OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
base64.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, 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 #ifndef BASE_64_H
18 #define BASE_64_H
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 mapd {
26 inline std::string decode_base64(const std::string& val, bool trim_nulls) {
27  // For some strings, signatature particualary it is important no to trim
28  // '\0' characters
29  using namespace boost::archive::iterators;
30  using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
31 
32  if (!trim_nulls) {
33  return std::string(It(val.begin()), It(val.end()));
34  }
35  return boost::algorithm::trim_right_copy_if(
36  std::string(It(std::begin(val)), It(std::end(val))),
37  [](char c) { return c == '\0'; });
38 }
39 
40 inline std::string decode_base64(const std::string& val) {
41  return decode_base64(val, true);
42 }
43 
44 static inline std::string encode_base64(const std::string& val) {
45  using namespace boost::archive::iterators;
46  using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
47  auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
48  return tmp.append((3 - val.size() % 3) % 3, '=');
49 }
50 
51 std::string decode_base64_uri(const std::string& value, bool trim_nulls = true);
52 
53 } // namespace mapd
54 #endif
std::string decode_base64_uri(const std::string &data, bool trim_nulls)
Definition: base64.cpp:20
std::string decode_base64(const std::string &val, bool trim_nulls)
Definition: base64.h:26
static std::string encode_base64(const std::string &val)
Definition: base64.h:44