1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "base64_helper.h"
17 #include <vector>
18 #include "log_print.h"
19 
20 namespace OHOS {
21 namespace NativePreferences {
22 static const uint8_t base64Encoder[] =
23     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
24 
25 static const uint8_t base64Decoder[] = {
26     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62,  -1, -1, -1, 63,
29     52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  -1, -1, -1, -1, -1, -1,
30     -1, 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,
31     15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  -1, -1, -1, -1, -1,
32     -1, 26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
33     41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  -1, -1, -1, -1, -1,
34     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
42 };
43 
44 const uint32_t BASE64_DEST_UNIT_SIZE = 4;
45 const uint32_t BASE64_SRC_UNIT_SIZE = 3;
46 const uint32_t BASE64_ONE_PADDING = 1;
47 const uint32_t BASE64_TWO_PADDING = 2;
48 const uint8_t BASE64_INVALID = 0xFF;
49 const uint8_t BASE64_MASK1 = 0x03;
50 const uint8_t BASE64_MASK2 = 0x0F;
51 const uint8_t BASE64_MASK3 = 0x3F;
52 const uint8_t BASE64_SHIFT_HIBYTE = 2;
53 const uint8_t BASE64_SHIFT = 4;
54 const uint8_t BASE64_SHIFT_LOBYTE = 6;
55 
Encode(const std::vector<uint8_t> & input)56 std::string Base64Helper::Encode(const std::vector<uint8_t> &input)
57 {
58     std::string result = "";
59     uint32_t index = 0;
60     for (uint32_t len = input.size(); len > 0; len -= BASE64_SRC_UNIT_SIZE) {
61         result += base64Encoder[input.at(index) >> BASE64_SHIFT_HIBYTE];
62         uint8_t code = (input.at(index++) & BASE64_MASK1) << BASE64_SHIFT;
63         if (len > BASE64_ONE_PADDING) {
64             result += base64Encoder[code | (input.at(index) >> BASE64_SHIFT)];
65             code = (input.at(index++) & BASE64_MASK2) << BASE64_SHIFT_HIBYTE;
66             if (len > BASE64_TWO_PADDING) {
67                 result += base64Encoder[code | (input.at(index) >> BASE64_SHIFT_LOBYTE)];
68                 result += base64Encoder[input.at(index++) & BASE64_MASK3];
69             } else {
70                 result += base64Encoder[code];
71                 result += "=";
72                 break;
73             }
74         } else {
75             result += base64Encoder[code];
76             result += "==";
77             break;
78         }
79     }
80     return result;
81 }
82 
Decode(const std::string & input,std::vector<uint8_t> & output)83 bool Base64Helper::Decode(const std::string &input, std::vector<uint8_t> &output)
84 {
85     if (input.length() % BASE64_DEST_UNIT_SIZE != 0) {
86         return false;
87     }
88     uint32_t index = 0;
89     std::vector<uint8_t> result {};
90     while (index < input.length()) {
91         uint8_t ch1 = base64Decoder[static_cast<uint8_t>(input.at(index++))];
92         uint8_t ch2 = base64Decoder[static_cast<uint8_t>(input.at(index++))];
93         if (ch1 == BASE64_INVALID || ch2 == BASE64_INVALID) {
94             return false;
95         }
96         result.emplace_back((ch1 << BASE64_SHIFT_HIBYTE) | (ch2 >> BASE64_SHIFT));
97         if (input.at(index) == '=') {
98             break;
99         }
100         uint8_t ch3 = base64Decoder[static_cast<uint8_t>(input.at(index++))];
101         if (ch3 == BASE64_INVALID) {
102             return false;
103         }
104         result.emplace_back((ch2 << BASE64_SHIFT) | (ch3 >> BASE64_SHIFT_HIBYTE));
105         if (input.at(index) == '=') {
106             break;
107         }
108         uint8_t ch4 = base64Decoder[static_cast<uint8_t>(input.at(index++))];
109         if (ch4 == BASE64_INVALID) {
110             return false;
111         }
112         result.emplace_back((ch3 << BASE64_SHIFT_LOBYTE) | ch4);
113     }
114     output = result;
115     return true;
116 }
117 } // End of namespace NativePreferences
118 } // End of namespace OHOS
119