OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
base64.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 #include "Shared/base64.h"
18 
19 namespace mapd {
20 std::string decode_base64_uri(const std::string& data, bool trim_nulls) {
21  // Allocate a string large enough to hold exta '=' as padding
22  std::string uri_dec;
23  size_t data_len = data.length();
24  // base64_uri encoding removes '=' padding at the end of the string.
25  size_t padding = 4 - (data_len % 4);
26  if (padding == 4) {
27  padding = 0;
28  }
29  uri_dec.resize(data_len + padding);
30 
31  // base64_uri encoding replaces all '+' and '/' with '-' and '_' respectively.
32  std::transform(
33  data.begin(), data.end(), uri_dec.begin(), [](unsigned char c) -> unsigned char {
34  switch (c) {
35  case '-':
36  return '+';
37  case '_':
38  return '/';
39  default:
40  return c;
41  }
42  });
43  if (padding != 0) {
44  uri_dec.replace(uri_dec.begin() + data_len, uri_dec.end(), padding, '=');
45  }
46  // in the case of a signature from a JWT trim_nulls should be false
47  return mapd::decode_base64(uri_dec, trim_nulls);
48 }
49 } // namespace mapd
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