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