1 /*
2 * Copyright (c) 2021 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_utils_tools.h"
17
18 #include <chrono>
19 #include <string>
20
21 #include "distributed_camera_constants.h"
22 #include "distributed_camera_errno.h"
23 #include "distributed_hardware_log.h"
24
25 #include "softbus_bus_center.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 const uint32_t OFFSET2 = 2;
30 const uint32_t OFFSET4 = 4;
31 const uint32_t OFFSET6 = 6;
32 const uint8_t PARAM_FC = 0xfc;
33 const uint8_t PARAM_03 = 0x03;
34 const uint8_t PARAM_F0 = 0xf0;
35 const uint8_t PARAM_0F = 0x0f;
36 const uint8_t PARAM_C0 = 0xc0;
37 const uint8_t PARAM_3F = 0x3f;
38 const int INDEX_FIRST = 0;
39 const int INDEX_SECOND = 1;
40 const int INDEX_THIRD = 2;
41 const int INDEX_FORTH = 3;
GetLocalDeviceNetworkId(std::string & networkId)42 int32_t GetLocalDeviceNetworkId(std::string& networkId)
43 {
44 NodeBasicInfo basicInfo = { { 0 } };
45 int32_t ret = GetLocalNodeDeviceInfo(DCAMERA_PKG_NAME.c_str(), &basicInfo);
46 if (ret != DCAMERA_OK) {
47 DHLOGE("GetLocalNodeDeviceInfo failed ret: %d", ret);
48 return ret;
49 }
50
51 networkId = std::string(basicInfo.networkId);
52 return DCAMERA_OK;
53 }
54
GetNowTimeStampMs()55 int64_t GetNowTimeStampMs()
56 {
57 std::chrono::milliseconds nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
58 std::chrono::system_clock::now().time_since_epoch());
59 return nowMs.count();
60 }
61
GetNowTimeStampUs()62 int64_t GetNowTimeStampUs()
63 {
64 std::chrono::microseconds nowUs = std::chrono::duration_cast<std::chrono::microseconds>(
65 std::chrono::system_clock::now().time_since_epoch());
66 return nowUs.count();
67 }
68
GetAlignedHeight(int32_t width)69 int32_t GetAlignedHeight(int32_t width)
70 {
71 int32_t alignedBits = 32;
72 int32_t alignedHeight = width;
73 if (alignedHeight % alignedBits != 0) {
74 alignedHeight = ((alignedHeight / alignedBits) + 1) * alignedBits;
75 }
76 return alignedHeight;
77 }
78
Base64Encode(const unsigned char * toEncode,unsigned int len)79 std::string Base64Encode(const unsigned char *toEncode, unsigned int len)
80 {
81 std::string ret = "";
82 if (len == 0 || toEncode == nullptr) {
83 DHLOGE("toEncode is null or len is zero.");
84 return ret;
85 }
86 int32_t length = len;
87 uint32_t i = 0;
88 unsigned char charArray3[3];
89 unsigned char charArray4[4];
90
91 while (length--) {
92 charArray3[i++] = *(toEncode++);
93 if (i == sizeof(charArray3)) {
94 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
95 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
96 ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
97 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
98 ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
99 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
100 for (i = 0; i < sizeof(charArray4); i++) {
101 ret += BASE_64_CHARS[charArray4[i]];
102 }
103 i = 0;
104 }
105 }
106
107 if (i > 0) {
108 uint32_t j = 0;
109 for (j = i; j < sizeof(charArray3); j++) {
110 charArray3[j] = '\0';
111 }
112 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
113 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
114 ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
115 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
116 ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
117 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
118 for (j = 0; j < i + 1; j++) {
119 ret += BASE_64_CHARS[charArray4[j]];
120 }
121 while (i++ < sizeof(charArray3)) {
122 ret += '=';
123 }
124 }
125 return ret;
126 }
127
Base64Decode(const std::string & basicString)128 std::string Base64Decode(const std::string& basicString)
129 {
130 std::string ret = "";
131 if (basicString.empty()) {
132 DHLOGE("basicString is empty.");
133 return ret;
134 }
135 uint32_t i = 0;
136 int index = 0;
137 int len = static_cast<int>(basicString.size());
138 unsigned char charArray3[3];
139 unsigned char charArray4[4];
140
141 while (len-- && (basicString[index] != '=') && IsBase64(basicString[index])) {
142 charArray4[i++] = basicString[index];
143 index++;
144 if (i == sizeof(charArray4)) {
145 for (i = 0; i < sizeof(charArray4); i++) {
146 charArray4[i] = BASE_64_CHARS.find(charArray4[i]);
147 }
148 charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
149 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
150 charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
151 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
152 charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
153 for (i = 0; i < sizeof(charArray3); i++) {
154 ret += charArray3[i];
155 }
156 i = 0;
157 }
158 }
159
160 if (i > 0) {
161 uint32_t j = 0;
162 for (j = i; j < sizeof(charArray4); j++) {
163 charArray4[j] = 0;
164 }
165 for (j = 0; j < sizeof(charArray4); j++) {
166 charArray4[j] = BASE_64_CHARS.find(charArray4[j]);
167 }
168 charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
169 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
170 charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
171 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
172 charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
173 for (j = 0; j < i - 1; j++) {
174 ret += charArray3[j];
175 }
176 }
177 return ret;
178 }
179
IsBase64(unsigned char c)180 bool IsBase64(unsigned char c)
181 {
182 return (isalnum(c) || (c == '+') || (c == '/'));
183 }
184
DumpBufferToFile(std::string fileName,uint8_t * buffer,size_t bufSize)185 void DumpBufferToFile(std::string fileName, uint8_t *buffer, size_t bufSize)
186 {
187 if (fileName.empty() || buffer == nullptr) {
188 DHLOGE("dumpsaving : input param err.");
189 return;
190 }
191 std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app);
192 if (!ofs.is_open()) {
193 DHLOGE("dumpsaving : open file failed.");
194 return;
195 }
196 ofs.write(reinterpret_cast<const char*>(buffer), bufSize);
197 ofs.close();
198 return;
199 }
200
IsUnderDumpMaxSize(std::string fileName)201 int32_t IsUnderDumpMaxSize(std::string fileName)
202 {
203 if (fileName.empty()) {
204 DHLOGE("dumpsaving : input fileName empty.");
205 return DCAMERA_INIT_ERR;
206 }
207 std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app);
208 if (!ofs.is_open()) {
209 DHLOGE("dumpsaving : open file failed.");
210 return DCAMERA_INIT_ERR;
211 }
212 ofs.seekp(0, std::ios::end);
213 std::ofstream::pos_type fileSize = ofs.tellp();
214 if (fileSize < 0) {
215 DHLOGE("filesize get err");
216 fileSize = 0;
217 return DCAMERA_INIT_ERR;
218 }
219 ofs.close();
220 if (static_cast<int32_t>(fileSize) <= DUMP_FILE_MAX_SIZE) {
221 return DCAMERA_OK;
222 } else {
223 return DCAMERA_BAD_VALUE;
224 }
225 }
226 } // namespace DistributedHardware
227 } // namespace OHOS
228