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 "distributed_camera_constants.h" 19 #include "distributed_camera_errno.h" 20 #include "distributed_hardware_log.h" 21 22 #include "softbus_bus_center.h" 23 24 namespace OHOS { 25 namespace DistributedHardware { 26 const int OFFSET2 = 2; 27 const int OFFSET4 = 4; 28 const int OFFSET6 = 6; 29 const int INDEX_FIRST = 0; 30 const int INDEX_SECOND = 1; 31 const int INDEX_THIRD = 2; 32 const int INDEX_FORTH = 3; GetLocalDeviceNetworkId(std::string & networkId)33int32_t GetLocalDeviceNetworkId(std::string& networkId) 34 { 35 NodeBasicInfo basicInfo = { { 0 } }; 36 int32_t ret = GetLocalNodeDeviceInfo(DCAMERA_PKG_NAME.c_str(), &basicInfo); 37 if (ret != DCAMERA_OK) { 38 DHLOGE("GetLocalDeviceNetworkId GetLocalNodeDeviceInfo failed ret: %d", ret); 39 return ret; 40 } 41 42 networkId = std::string(basicInfo.networkId); 43 return DCAMERA_OK; 44 } 45 GetNowTimeStampMs()46int64_t GetNowTimeStampMs() 47 { 48 std::chrono::milliseconds nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( 49 std::chrono::system_clock::now().time_since_epoch()); 50 return nowMs.count(); 51 } 52 GetNowTimeStampUs()53int64_t GetNowTimeStampUs() 54 { 55 std::chrono::microseconds nowUs = std::chrono::duration_cast<std::chrono::microseconds>( 56 std::chrono::system_clock::now().time_since_epoch()); 57 return nowUs.count(); 58 } 59 Base64Encode(const unsigned char * toEncode,unsigned int len)60std::string Base64Encode(const unsigned char *toEncode, unsigned int len) 61 { 62 std::string ret; 63 uint32_t i = 0; 64 unsigned char charArray3[3]; 65 unsigned char charArray4[4]; 66 67 while (len--) { 68 charArray3[i++] = *(toEncode++); 69 if (i == sizeof(charArray3)) { 70 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & 0xfc) >> OFFSET2; 71 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & 0x03) << OFFSET4) + 72 ((charArray3[INDEX_SECOND] & 0xf0) >> OFFSET4); 73 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & 0x0f) << OFFSET2) + 74 ((charArray3[INDEX_THIRD] & 0xc0) >> OFFSET6); 75 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & 0x3f; 76 for (i = 0; i < sizeof(charArray4); i++) { 77 ret += BASE_64_CHARS[charArray4[i]]; 78 } 79 i = 0; 80 } 81 } 82 83 if (i) { 84 uint32_t j = 0; 85 for (j = i; j < sizeof(charArray3); j++) { 86 charArray3[j] = '\0'; 87 } 88 charArray4[INDEX_FIRST] = (charArray3[INDEX_FIRST] & 0xfc) >> OFFSET2; 89 charArray4[INDEX_SECOND] = ((charArray3[INDEX_FIRST] & 0x03) << OFFSET4) + 90 ((charArray3[INDEX_SECOND] & 0xf0) >> OFFSET4); 91 charArray4[INDEX_THIRD] = ((charArray3[INDEX_SECOND] & 0x0f) << OFFSET2) + 92 ((charArray3[INDEX_THIRD] & 0xc0) >> OFFSET6); 93 charArray4[INDEX_FORTH] = charArray3[INDEX_THIRD] & 0x3f; 94 for (j = 0; j < i + 1; j++) { 95 ret += BASE_64_CHARS[charArray4[j]]; 96 } 97 while (i++ < sizeof(charArray3)) { 98 ret += '='; 99 } 100 } 101 return ret; 102 } 103 Base64Decode(const std::string & basicString)104std::string Base64Decode(const std::string& basicString) 105 { 106 std::string ret; 107 uint32_t i = 0; 108 int index = 0; 109 int len = static_cast<int>(basicString.size()); 110 unsigned char charArray3[3]; 111 unsigned char charArray4[4]; 112 113 while (len-- && (basicString[index] != '=') && IsBase64(basicString[index])) { 114 charArray4[i++] = basicString[index]; 115 index++; 116 if (i == sizeof(charArray4)) { 117 for (i = 0; i < sizeof(charArray4); i++) { 118 charArray4[i] = BASE_64_CHARS.find(charArray4[i]); 119 } 120 charArray3[INDEX_FIRST] = (charArray4[INDEX_FIRST] << OFFSET2) + 121 ((charArray4[INDEX_SECOND] & 0x30) >> OFFSET4); 122 charArray3[INDEX_SECOND] = ((charArray4[INDEX_SECOND] & 0xf) << OFFSET4) + 123 ((charArray4[INDEX_THIRD] & 0x3c) >> OFFSET2); 124 charArray3[INDEX_THIRD] = ((charArray4[INDEX_THIRD] & 0x3) << OFFSET6) + charArray4[INDEX_FORTH]; 125 for (i = 0; i < sizeof(charArray3); i++) { 126 ret += charArray3[i]; 127 } 128 i = 0; 129 } 130 } 131 132 if (i) { 133 uint32_t j = 0; 134 for (j = i; j < sizeof(charArray4); j++) { 135 charArray4[j] = 0; 136 } 137 for (j = 0; j < sizeof(charArray4); j++) { 138 charArray4[j] = BASE_64_CHARS.find(charArray4[j]); 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 (j = 0; j < i - 1; j++) { 146 ret += charArray3[j]; 147 } 148 } 149 return ret; 150 } 151 IsBase64(unsigned char c)152bool IsBase64(unsigned char c) 153 { 154 return (isalnum(c) || (c == '+') || (c == '/')); 155 } 156 } 157 }