1 /*
2 * Copyright (C) 2025 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 #ifndef HDC_CREDENTIAL_BASE_H
16 #define HDC_CREDENTIAL_BASE_H
17
18 #include <chrono>
19 #include <dirent.h>
20 #include <securec.h>
21 #include <set>
22 #include <sys/stat.h>
23 #include <sys/un.h>
24 #include <thread>
25 #include <unistd.h>
26
27 #include "log.h"
28
29 #ifdef HDC_HILOG
30 #ifdef LOG_DOMAIN
31 #undef LOG_DOMAIN
32 #endif // LOG_DOMAIN
33
34 #define LOG_DOMAIN 0xD002D13
35 #ifdef LOG_TAG
36 #undef LOG_TAG
37 #endif // LOG_TAG
38 #define LOG_TAG "HDC_LOG"
39 #endif // HDC_HILOG
40
41 namespace HdcCredentialBase {
42 // 0x10000000 is 1.0.0a
43 constexpr uint32_t CREDENTIAL_VERSION_NUMBER = 0x10000000;
44 constexpr size_t SOCKET_CLIENT_NUMS = 1;
45
46 const std::string HDC_PRIVATE_KEY_FILE_PWD_KEY_ALIAS = "hdc_private_key_file_pwd_key_alias";
47 constexpr size_t PASSWORD_LENGTH = 10;
48
49 constexpr uint32_t MAX_SIZE_IOBUF_STABLE = 60 * 1024; // 60KB, compatible with previous version
50 const std::string HDC_CREDENTIAL_SOCKET_REAL_PATH =
51 "/data/service/el1/public/hdc_server/hdc_common/hdc_credential.socket";
52 constexpr uint8_t CMD_ARG1_COUNT = 2;
53 constexpr int MIN_USER_ID = 100;
54 constexpr int MAX_USER_ID = 10736;
55
56 int RemoveDir(const std::string& dir);
57 int RemovePath(const std::string& path);
58 const std::string StringFormat(const char* const formater, ...);
59 const std::string StringFormat(const char* const formater, va_list& vaArgs);
60 char GetPathSep();
61 bool CreatePathWithMode(const char* path, mode_t mode);
62 bool IsUserDir(const std::string& dir);
63
64 /* Calculate diff: return elements in vector a but not in vector b */
65 template<typename T>
Substract(const std::vector<T> & a,const std::vector<T> & b)66 std::vector<T> Substract(const std::vector<T>& a, const std::vector<T>& b)
67 {
68 std::set<T> aSet(a.begin(), a.end());
69 std::set<T> bSet(b.begin(), b.end());
70 std::vector<T> diff;
71 std::set_difference(aSet.begin(), aSet.end(), bSet.begin(), bSet.end(), std::back_inserter(diff));
72 return diff;
73 }
74 } // namespace HdcCredentialBase
75
76 #endif // HDC_CREDENTIAL_BASE_H