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 #ifndef DISTRIBUTEDDB_TYPES_EXPORT_H
17 #define DISTRIBUTEDDB_TYPES_EXPORT_H
18 
19 #include <climits>
20 #include <cstdint>
21 #include <functional>
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 namespace DistributedDB {
27 #ifdef _WIN32
28     #ifdef DB_DLL_EXPORT
29         #define DB_API __declspec(dllexport)
30     #else
31         #define DB_API
32     #endif
33 #else
34     #define DB_API __attribute__ ((visibility ("default")))
35 #endif
36 
37 #define DB_SYMBOL DB_API
38 
39 using Key = std::vector<uint8_t>;
40 using Value = std::vector<uint8_t>;
41 
42 struct Entry {
43     Key key;
44     Value value;
45 };
46 
47 enum class CipherType {
48     DEFAULT,
49     AES_256_GCM, // AES-256-GCM
50 };
51 
52 class CipherPassword final {
53 public:
54     enum ErrorCode {
55         OK = 0,
56         OVERSIZE,
57         INVALID_INPUT,
58         SECUREC_ERROR,
59     };
60 
61     DB_API CipherPassword();
62     DB_API ~CipherPassword();
63 
64     DB_API bool operator==(const CipherPassword &input) const;
65     DB_API bool operator!=(const CipherPassword &input) const;
66 
67     DB_API size_t GetSize() const;
68     DB_API const uint8_t *GetData() const;
69     DB_API int SetValue(const uint8_t *inputData, size_t inputSize);
70     DB_API int Clear();
71 
72 private:
73     static const size_t MAX_PASSWORD_SIZE = 128;
74     uint8_t data_[MAX_PASSWORD_SIZE] = {UCHAR_MAX};
75     size_t size_ = 0;
76 };
77 
78 using PragmaData = void *;
79 
80 struct PragmaEntryDeviceIdentifier {
81     Key key;
82     bool origDevice = true;
83     std::string deviceIdentifier;
84 };
85 
86 using KvStoreNbPublishAction = std::function<void (const Entry &local, const Entry *sync, bool isLocalLastest)>;
87 
88 struct PragmaDeviceIdentifier {
89     std::string deviceID;
90     std::string deviceIdentifier;
91 };
92 
93 enum WipePolicy {
94     RETAIN_STALE_DATA = 1, // remote stale data will be retained in syncing when remote db rebuiled.
95     WIPE_STALE_DATA // remote stale data will be wiped when in syncing remote db rebuiled.
96 };
97 
98 // We don't parse, read or modify the array type, so there are not a corresponding array value
99 // The leaf object is empty, an internal object always composed by other type values.
100 struct FieldValue {
101     union {
102         bool boolValue;
103         int32_t integerValue;
104         int64_t longValue = 0;
105         double doubleValue;
106     };
107     std::string stringValue;
108 };
109 
110 enum PermissionCheckFlag {
111     CHECK_FLAG_SEND = 1, // send
112     CHECK_FLAG_RECEIVE = 2, // receive
113     CHECK_FLAG_AUTOSYNC = 4, // autosync flag
114     CHECK_FLAG_SPONSOR = 8, // sync sponsor
115 };
116 
117 struct PermissionCheckParam {
118     std::string userId;
119     std::string appId;
120     std::string storeId;
121     std::string deviceId;
122     int32_t instanceId = 0;
123     std::map<std::string, std::string> extraConditions;
124 };
125 
126 struct ActivationCheckParam {
127     std::string userId;
128     std::string appId;
129     std::string storeId;
130     int32_t instanceId = 0;
131 };
132 
133 struct PermissionConditionParam {
134     std::string userId;
135     std::string appId;
136     std::string storeId;
137     int32_t instanceId = 0;
138 };
139 
140 using PermissionCheckCallback = std::function<bool (const std::string &userId, const std::string &appId,
141     const std::string &storeId, uint8_t flag)>;
142 
143 using PermissionCheckCallbackV2 = std::function<bool (const std::string &userId, const std::string &appId,
144     const std::string &storeId, const std::string &deviceId, uint8_t flag)>;
145 
146 using PermissionCheckCallbackV3 = std::function<bool (const PermissionCheckParam &param, uint8_t flag)>;
147 
148 using StoreStatusNotifier = std::function<void (std::string userId, std::string appId, std::string storeId,
149     const std::string deviceId, bool onlineStatus)>; // status, 1: online, 0: offline
150 
151 using SyncActivationCheckCallback = std::function<bool (const std::string &userId, const std::string &appId,
152     const std::string &storeId)>;
153 
154 using SyncActivationCheckCallbackV2 = std::function<bool (const ActivationCheckParam &param)>;
155 
156 using PermissionConditionCallback =
157     std::function<std::map<std::string, std::string> (const PermissionConditionParam &param)>;
158 
159 enum AutoLaunchStatus {
160     WRITE_OPENED = 1,
161     WRITE_CLOSED = 2,
162     INVALID_PARAM = 3, // AutoLaunchRequestCallback, if param check failed
163 };
164 
165 using AutoLaunchNotifier = std::function<void (const std::string &userId,
166     const std::string &appId, const std::string &storeId, AutoLaunchStatus status)>;
167 
168 enum SecurityLabel : int {
169     INVALID_SEC_LABEL = -1,
170     NOT_SET = 0,
171     S0,
172     S1,
173     S2,
174     S3,
175     S4
176 };
177 
178 // security flag type
179 enum SecurityFlag : int {
180     INVALID_SEC_FLAG = -1,
181     ECE = 0,
182     SECE
183 };
184 
185 struct SecurityOption {
186     int securityLabel = 0; // the securityLabel is the class of data sensitive, see enum SecurityLabel
187     int securityFlag = 0;  // the securityFlag is the encryption method of the file only used for S3 like 0:ECE, 1:SECE
188     bool operator==(const SecurityOption &rhs) const
189     {
190         return securityLabel == rhs.securityLabel && securityFlag == rhs.securityFlag;
191     }
192 
193     bool operator!=(const SecurityOption &rhs) const
194     {
195         return !(*this == rhs);
196     }
197 };
198 
199 enum class ResultSetCacheMode : int {
200     CACHE_FULL_ENTRY = 0,       // Ordinary mode efficient when sequential access, the default mode
201     CACHE_ENTRY_ID_ONLY = 1,    // Special mode efficient when random access
202 };
203 
204 struct RemotePushNotifyInfo {
205     std::string deviceId;
206 };
207 using RemotePushFinishedNotifier = std::function<void (const RemotePushNotifyInfo &info)>;
208 using RemotePushFinisheNotifier = RemotePushFinishedNotifier; // To correct spelling errors in the previous version
209 
210 enum class CompressAlgorithm : uint8_t {
211     NONE = 0,
212     ZLIB = 1
213 };
214 
215 struct StoreInfo {
216     std::string userId;
217     std::string appId;
218     std::string storeId;
219 };
220 using TranslateToDeviceIdCallback = std::function<std::string (const std::string &oriDevId, const StoreInfo &info)>;
221 } // namespace DistributedDB
222 #endif // DISTRIBUTEDDB_TYPES_EXPORT_H
223