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