1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 #ifndef OHOS_SHARING_UTILS_H 17 #define OHOS_SHARING_UTILS_H 18 19 #include <regex> 20 #include <string> 21 #include <vector> 22 23 namespace OHOS { 24 namespace Sharing { 25 26 class RpcKeyParser { 27 public: 28 RpcKeyParser() = default; 29 GetRpcKey(std::string bundleName,std::string abilityName,std::string deviceId,std::string className)30 std::string GetRpcKey(std::string bundleName, std::string abilityName, std::string deviceId, std::string className) 31 32 { 33 bundleName_ = std::move(bundleName); 34 abilityName_ = std::move(abilityName); 35 deviceId_ = std::move(deviceId); 36 className_ = std::move(className); 37 return bundleName_ + "+" + abilityName_ + "+" + deviceId_ + "+" + className_; 38 } 39 ParseKey(std::string key)40 bool ParseKey(std::string key) 41 { 42 std::regex reg{"\\+"}; 43 std::vector<std::string> out{std::sregex_token_iterator(key.begin(), key.end(), reg, -1), 44 std::sregex_token_iterator()}; 45 if (out.size() != 4) { // 4: rpckey length 46 return false; 47 } 48 bundleName_ = std::move(out[0]); 49 abilityName_ = std::move(out[1]); 50 deviceId_ = std::move(out[2]); // 2: rpckey parse 51 className_ = std::move(out[3]); // 3: rpckey parse 52 return true; 53 } 54 GetBundleName()55 std::string GetBundleName() 56 { 57 return bundleName_; 58 } 59 GetAbilityName()60 std::string GetAbilityName() 61 { 62 return abilityName_; 63 } 64 GetDeviceId()65 std::string GetDeviceId() 66 { 67 return deviceId_; 68 } 69 GetClassName()70 std::string GetClassName() 71 { 72 return className_; 73 } 74 75 private: 76 std::string deviceId_; 77 std::string className_; 78 std::string bundleName_; 79 std::string abilityName_; 80 }; 81 82 unsigned long long GetThreadId(); 83 uint64_t GetCurrentMillisecond(); 84 85 std::string ChangeCase(const std::string &value, bool lowerCase); 86 std::string Trim(std::string &&s, const std::string &chars = " \r\n\t"); 87 std::string &Trim(std::string &s, const std::string &chars = " \r\n\t"); 88 89 std::vector<std::string> Split(const std::string &s, const char *delim); 90 std::vector<std::string> SplitOnce(const std::string &s, const char *delim); 91 92 void SaveFile(const char *data, int32_t dataSize, const std::string &fileName); 93 94 #if defined(__ARM_NEON__) 95 void NeonMemcpy(volatile unsigned char *dst, volatile unsigned char *src, int32_t size); 96 #endif 97 98 uint16_t SwapEndian16(uint16_t value); 99 uint32_t SwapEndian32(uint32_t value); 100 uint64_t SwapEndian64(uint64_t value); 101 102 uint16_t LoadBE16(const uint8_t *p); 103 uint32_t LoadBE24(const uint8_t *p); 104 uint32_t LoadBE32(const uint8_t *p); 105 uint64_t LoadBE64(const uint8_t *p); 106 107 void SetBE24(void *p, uint32_t val); 108 void SetBE32(void *p, uint32_t val); 109 void SetLE32(void *p, uint32_t val); 110 111 std::string GetAnonyString(const std::string &value); 112 } // namespace Sharing 113 } // namespace OHOS 114 #endif