1 /*
2 **
3 ** Copyright 2020, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include "base64util.h"
19
20 #include <vector>
21
22 #include "common/libs/utils/base64.h"
23
24 extern "C" {
25
base64_decode(const char * base64input,unsigned char * bindata)26 int base64_decode(const char *base64input, unsigned char *bindata) {
27 if (!base64input || !bindata) {
28 return 0;
29 }
30
31 std::vector<uint8_t> output;
32 std::string input(base64input);
33 bool success = cuttlefish::DecodeBase64(input, &output);
34 if (!success) {
35 return 0;
36 }
37 memcpy(bindata, reinterpret_cast<unsigned char *>(output.data()),
38 output.size());
39 return output.size();
40 }
41
base64_encode(const unsigned char * bindata,char * base64output,int binlength)42 char *base64_encode(const unsigned char *bindata, char *base64output,
43 int binlength) {
44 if (!base64output || !bindata || binlength <= 0) {
45 return NULL;
46 }
47
48 std::string output;
49 bool success = cuttlefish::EncodeBase64(
50 bindata, static_cast<size_t>(binlength), &output);
51
52 if (!success) {
53 return NULL;
54 }
55
56 memcpy(base64output, output.data(), output.size());
57 return base64output;
58 }
59 }
60