• 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 #define LOG_TAG "KvStoreUtils"
17 
18 #include "kvstore_utils.h"
19 #include "permission_validator.h"
20 
21 namespace OHOS {
22 namespace DistributedKv {
23 constexpr int32_t HEAD_SIZE = 3;
24 constexpr int32_t END_SIZE = 3;
25 constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3;
26 constexpr const char *REPLACE_CHAIN = "***";
27 constexpr const char *DEFAULT_ANONYMOUS = "******";
28 std::atomic<uint64_t> KvStoreUtils::sequenceId_{ 0 };
ToBeAnonymous(const std::string & name)29 std::string KvStoreUtils::ToBeAnonymous(const std::string &name)
30 {
31     if (name.length() <= HEAD_SIZE) {
32         return DEFAULT_ANONYMOUS;
33     }
34 
35     if (name.length() < MIN_SIZE) {
36         return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
37     }
38 
39     return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN + name.substr(name.length() - END_SIZE, END_SIZE));
40 }
41 
GetProviderInstance()42 AppDistributedKv::CommunicationProvider &KvStoreUtils::GetProviderInstance()
43 {
44 #ifdef CONFIG_PUBLIC_VERSION
45     return AppDistributedKv::CommunicationProvider::GetInstance();
46 #else
47     return *(AppDistributedKv::CommunicationProvider::MakeCommunicationProvider().get());
48 #endif
49 }
50 
GenerateSequenceId()51 uint64_t KvStoreUtils::GenerateSequenceId()
52 {
53     return ++sequenceId_;
54 }
55 } // namespace DistributedKv
56 } // namespace OHOS
57