• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "os_account_photo_operator.h"
17 
18 #include "account_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AccountSA {
22 namespace {
23 const size_t SIZET_ZERO = 0;
24 const size_t SIZET_ONE = 1;
25 const size_t SIZET_TWO = 2;
26 const size_t SIZET_THREE = 3;
27 const size_t SIZET_FOUR = 4;
28 const size_t SIZET_SIX = 6;
29 const size_t SIZET_SEVEN_SIX = 76;
30 }  // namespace
OsAccountPhotoOperator()31 OsAccountPhotoOperator::OsAccountPhotoOperator()
32 {
33     baseChars_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34                  "abcdefghijklmnopqrstuvwxyz"
35                  "0123456789+/";
36 }
~OsAccountPhotoOperator()37 OsAccountPhotoOperator::~OsAccountPhotoOperator()
38 {}
EnCode(const char * data,size_t dataByte)39 std::string OsAccountPhotoOperator::EnCode(const char *data, size_t dataByte)
40 {
41     std::string strEncode;
42     unsigned char tmpArray[SIZET_FOUR] = {0};
43     size_t LineLength = 0;
44     for (size_t i = 0; i < (dataByte / SIZET_THREE); i++) {
45         tmpArray[SIZET_ONE] = *data++;
46         tmpArray[SIZET_TWO] = *data++;
47         tmpArray[SIZET_THREE] = *data++;
48         strEncode += baseChars_[tmpArray[SIZET_ONE] >> SIZET_TWO];
49         strEncode += baseChars_[((tmpArray[SIZET_ONE] << SIZET_FOUR) | (tmpArray[SIZET_TWO] >> SIZET_FOUR)) & 0x3F];
50         strEncode += baseChars_[((tmpArray[SIZET_TWO] << SIZET_TWO) | (tmpArray[SIZET_THREE] >> SIZET_SIX)) & 0x3F];
51         strEncode += baseChars_[tmpArray[SIZET_THREE] & 0x3F];
52         if (LineLength += SIZET_FOUR, LineLength == SIZET_SEVEN_SIX) {
53             strEncode += "\r\n";
54             LineLength = 0;
55         }
56     }
57     size_t mod = dataByte % SIZET_THREE;
58     if (mod == 1) {
59         tmpArray[SIZET_ONE] = *data++;
60         strEncode += baseChars_[(tmpArray[SIZET_ONE] & 0xFC) >> SIZET_TWO];
61         strEncode += baseChars_[((tmpArray[SIZET_ONE] & 0x03) << SIZET_FOUR)];
62         strEncode += "==";
63     } else if (mod == SIZET_TWO) {
64         tmpArray[SIZET_ONE] = *data++;
65         tmpArray[SIZET_TWO] = *data++;
66         strEncode += baseChars_[(tmpArray[SIZET_ONE] & 0xFC) >> SIZET_TWO];
67         strEncode += baseChars_[((tmpArray[SIZET_ONE] & 0x03) << SIZET_FOUR) |
68             ((tmpArray[SIZET_TWO] & 0xF0) >> SIZET_FOUR)];
69         strEncode += baseChars_[((tmpArray[SIZET_TWO] & 0x0F) << SIZET_TWO)];
70         strEncode += "=";
71     }
72 
73     return strEncode;
74 }
DeCode(std::string const & baseStr)75 std::string OsAccountPhotoOperator::DeCode(std::string const &baseStr)
76 {
77     ACCOUNT_LOGD("OsAccountPhotoOperator DeCode Start");
78     std::string byteStr;
79     size_t in_len = baseStr.size();
80     if (in_len == 0) {
81         ACCOUNT_LOGE("empty input baseStr!");
82         return byteStr;
83     }
84 
85     size_t i = 0;
86     size_t in_ = 0;
87     unsigned char char_array_4[SIZET_FOUR];
88     unsigned char char_array_3[SIZET_THREE];
89 
90     while (in_len-- && (baseStr[in_] != '=') && IsBase(baseStr[in_])) {
91         char_array_4[i++] = baseStr[in_];
92         in_++;
93         if (i == SIZET_FOUR) {
94             for (i = 0; i < SIZET_FOUR; i++)
95                 char_array_4[i] = baseChars_.find(char_array_4[i]);
96 
97             char_array_3[SIZET_ZERO] = (char_array_4[SIZET_ZERO] << SIZET_TWO) +
98                 ((char_array_4[SIZET_ONE] & 0x30) >> SIZET_FOUR);
99             char_array_3[SIZET_ONE] =
100                 ((char_array_4[SIZET_ONE] & 0xf) << SIZET_FOUR) + ((char_array_4[SIZET_TWO] & 0x3c) >> SIZET_TWO);
101             char_array_3[SIZET_TWO] = ((char_array_4[SIZET_TWO] & 0x3) << SIZET_SIX) + char_array_4[SIZET_THREE];
102 
103             for (i = 0; (i < SIZET_THREE); i++)
104                 byteStr += char_array_3[i];
105             i = 0;
106         }
107     }
108 
109     if (i) {
110         size_t j = 0;
111         for (j = i; j < SIZET_FOUR; j++)
112             char_array_4[j] = 0;
113 
114         for (j = 0; j < SIZET_FOUR; j++)
115             char_array_4[j] = baseChars_.find(char_array_4[j]);
116 
117         char_array_3[SIZET_ZERO] = (char_array_4[SIZET_ZERO] << SIZET_TWO) +
118             ((char_array_4[SIZET_ONE] & 0x30) >> SIZET_FOUR);
119         char_array_3[SIZET_ONE] =
120             ((char_array_4[SIZET_ONE] & 0xf) << SIZET_FOUR) + ((char_array_4[SIZET_TWO] & 0x3c) >> SIZET_TWO);
121         char_array_3[SIZET_TWO] = ((char_array_4[SIZET_TWO] & 0x3) << SIZET_SIX) + char_array_4[SIZET_THREE];
122 
123         for (j = 0; (j < i - 1); j++)
124             byteStr += char_array_3[j];
125     }
126     return byteStr;
127 }
IsBase(unsigned char c)128 bool OsAccountPhotoOperator::IsBase(unsigned char c)
129 {
130     return (isalnum(c) || (c == '+') || (c == '/'));
131 }
132 }  // namespace AccountSA
133 }  // namespace OHOS