• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("GetLocalDeviceNetworkId 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     uint32_t i = 0;
83     unsigned char charArray3[3];
84     unsigned char charArray4[4];
85 
86     while (len--) {
87         charArray3[i++] = *(toEncode++);
88         if (i == sizeof(charArray3)) {
89             charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
90             charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
91                 ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
92             charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
93                 ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
94             charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
95             for (i = 0; i < sizeof(charArray4); i++) {
96                 ret += BASE_64_CHARS[charArray4[i]];
97             }
98             i = 0;
99         }
100     }
101 
102     if (i > 0) {
103         uint32_t j = 0;
104         for (j = i; j < sizeof(charArray3); j++) {
105             charArray3[j] = '\0';
106         }
107         charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & PARAM_FC) >> OFFSET2;
108         charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & PARAM_03) << OFFSET4) +
109             ((charArray3[INDEX_SECOND] & PARAM_F0) >> OFFSET4);
110         charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & PARAM_0F) << OFFSET2) +
111             ((charArray3[INDEX_THIRD] & PARAM_C0) >> OFFSET6);
112         charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & PARAM_3F;
113         for (j = 0; j < i + 1; j++) {
114             ret += BASE_64_CHARS[charArray4[j]];
115         }
116         while (i++ < sizeof(charArray3)) {
117             ret += '=';
118         }
119     }
120     return ret;
121 }
122 
Base64Decode(const std::string & basicString)123 std::string Base64Decode(const std::string& basicString)
124 {
125     std::string ret;
126     uint32_t i = 0;
127     int index = 0;
128     int len = static_cast<int>(basicString.size());
129     unsigned char charArray3[3];
130     unsigned char charArray4[4];
131 
132     while (len-- && (basicString[index] != '=') && IsBase64(basicString[index])) {
133         charArray4[i++] = basicString[index];
134         index++;
135         if (i == sizeof(charArray4)) {
136             for (i = 0; i < sizeof(charArray4); i++) {
137                 charArray4[i] = BASE_64_CHARS.find(charArray4[i]);
138             }
139             charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
140                 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
141             charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
142                 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
143             charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
144             for (i = 0; i < sizeof(charArray3); i++) {
145                 ret += charArray3[i];
146             }
147             i = 0;
148         }
149     }
150 
151     if (i > 0) {
152         uint32_t j = 0;
153         for (j = i; j < sizeof(charArray4); j++) {
154             charArray4[j] = 0;
155         }
156         for (j = 0; j < sizeof(charArray4); j++) {
157             charArray4[j] = BASE_64_CHARS.find(charArray4[j]);
158         }
159         charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) +
160             ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4);
161         charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) +
162             ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2);
163         charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH];
164         for (j = 0; j < i - 1; j++) {
165             ret += charArray3[j];
166         }
167     }
168     return ret;
169 }
170 
IsBase64(unsigned char c)171 bool IsBase64(unsigned char c)
172 {
173     return (isalnum(c) || (c == '+') || (c == '/'));
174 }
175 } // namespace DistributedHardware
176 } // namespace OHOS
177