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 "dcamera.h"
17 #include <chrono>
18 #include "constants.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 const uint32_t OFFSET2 = 2;
23 const uint32_t OFFSET4 = 4;
24 const uint32_t OFFSET6 = 6;
25 const uint8_t PARAM_FC = 0xfc;
26 const uint8_t PARAM_03 = 0x03;
27 const uint8_t PARAM_F0 = 0xf0;
28 const uint8_t PARAM_0F = 0x0f;
29 const uint8_t PARAM_C0 = 0xc0;
30 const uint8_t PARAM_3F = 0x3f;
31 const int INDEX_FIRST = 0;
32 const int INDEX_SECOND = 1;
33 const int INDEX_THIRD = 2;
34 const int INDEX_FORTH = 3;
MapToExternalRetCode(DCamRetCode retCode)35 CamRetCode MapToExternalRetCode(DCamRetCode retCode)
36 {
37 switch (retCode) {
38 case DCamRetCode::SUCCESS:
39 return CamRetCode::NO_ERROR;
40 case DCamRetCode::CAMERA_BUSY:
41 return CamRetCode::CAMERA_BUSY;
42 case DCamRetCode::INVALID_ARGUMENT:
43 return CamRetCode::INVALID_ARGUMENT;
44 case DCamRetCode::METHOD_NOT_SUPPORTED:
45 return CamRetCode::METHOD_NOT_SUPPORTED;
46 case DCamRetCode::CAMERA_OFFLINE:
47 return CamRetCode::CAMERA_CLOSED;
48 case DCamRetCode::EXCEED_MAX_NUMBER:
49 return CamRetCode::INSUFFICIENT_RESOURCES;
50 case DCamRetCode::FAILED:
51 return CamRetCode::DEVICE_ERROR;
52 default:
53 break;
54 }
55 return CamRetCode::DEVICE_ERROR;
56 }
57
GetCurrentLocalTimeStamp()58 uint64_t GetCurrentLocalTimeStamp()
59 {
60 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
61 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
62 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
63 return static_cast<uint64_t>(tmp.count());
64 }
65
SplitString(const std::string & str,std::vector<std::string> & tokens,const std::string & delimiters)66 void SplitString(const std::string &str, std::vector<std::string> &tokens, const std::string &delimiters)
67 {
68 std::string::size_type lastPos = 0;
69 std::string::size_type pos = str.find(delimiters);
70 while (std::string::npos != pos) {
71 tokens.push_back(str.substr(lastPos, pos - lastPos));
72 lastPos = pos + delimiters.size();
73 pos = str.find(delimiters, lastPos);
74 }
75 if (lastPos != str.length()) {
76 tokens.push_back(str.substr(lastPos));
77 }
78 }
79
Base64Encode(const unsigned char * toEncode,unsigned int len)80 std::string Base64Encode(const unsigned char *toEncode, unsigned int len)
81 {
82 std::string ret;
83 uint32_t i = 0;
84 unsigned char charArray3[3];
85 unsigned char charArray4[4];
86
87 while (len--) {
88 charArray3[i++] = *(toEncode++);
89 if (i == sizeof(charArray3)) {
90 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
91 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
92 ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
93 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
94 ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
95 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
96 for (i = 0; i < sizeof(charArray4); i++) {
97 ret += BASE_64_CHARS[charArray4[i]];
98 }
99 i = 0;
100 }
101 }
102
103 if (i) {
104 uint32_t j = 0;
105 for (j = i; j < sizeof(charArray3); j++) {
106 charArray3[j] = '\0';
107 }
108 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
109 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
110 ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
111 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
112 ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
113 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
114 for (j = 0; j < i + 1; j++) {
115 ret += BASE_64_CHARS[charArray4[j]];
116 }
117 while (i++ < sizeof(charArray3)) {
118 ret += '=';
119 }
120 }
121 return ret;
122 }
123
Base64Decode(const std::string & basicString)124 std::string Base64Decode(const std::string& basicString)
125 {
126 std::string ret;
127 uint32_t i = 0;
128 int index = 0;
129 int len = static_cast<int>(basicString.size());
130 unsigned char charArray3[3];
131 unsigned char charArray4[4];
132
133 while (len-- && (basicString[index] != '=') && IsBase64(basicString[index])) {
134 charArray4[i++] = basicString[index];
135 index++;
136 if (i == sizeof(charArray4)) {
137 for (i = 0; i < sizeof(charArray4); i++) {
138 charArray4[i] = BASE_64_CHARS.find(charArray4[i]);
139 }
140 charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
141 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
142 charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
143 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
144 charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
145 for (i = 0; i < sizeof(charArray3); i++) {
146 ret += charArray3[i];
147 }
148 i = 0;
149 }
150 }
151
152 if (i) {
153 uint32_t j = 0;
154 for (j = i; j < sizeof(charArray4); j++) {
155 charArray4[j] = 0;
156 }
157 for (j = 0; j < sizeof(charArray4); j++) {
158 charArray4[j] = BASE_64_CHARS.find(charArray4[j]);
159 }
160 charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
161 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
162 charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
163 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
164 charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
165 for (j = 0; j < i - 1; j++) {
166 ret += charArray3[j];
167 }
168 }
169 return ret;
170 }
171
IsBase64(unsigned char c)172 bool IsBase64(unsigned char c)
173 {
174 return (isalnum(c) || (c == '+') || (c == '/'));
175 }
176
IsDhBaseInfoInvalid(const DHBase & dhBase)177 bool IsDhBaseInfoInvalid(const DHBase& dhBase)
178 {
179 return dhBase.deviceId_.empty() || (dhBase.deviceId_.length() > DEVID_MAX_LENGTH) ||
180 dhBase.dhId_.empty() || (dhBase.dhId_.length() > DHID_MAX_LENGTH);
181 }
182 } // namespace DistributedHardware
183 } // namespace OHOS
184