1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "ITypesUtil::KvTypesUtil"
16 #include "kv_types_util.h"
17 #include "log_print.h"
18 namespace OHOS::ITypesUtil {
19 using namespace DistributedKv;
20 template<>
Marshalling(const Blob & blob,MessageParcel & data)21 bool Marshalling(const Blob &blob, MessageParcel &data)
22 {
23 return data.WriteUInt8Vector(blob.Data());
24 }
25
26 template<>
Unmarshalling(Blob & output,MessageParcel & data)27 bool Unmarshalling(Blob &output, MessageParcel &data)
28 {
29 std::vector<uint8_t> blob;
30 bool result = data.ReadUInt8Vector(&blob);
31 output = blob;
32 return result;
33 }
34
35 template<>
Marshalling(const AppId & input,MessageParcel & data)36 bool Marshalling(const AppId &input, MessageParcel &data)
37 {
38 return ITypesUtil::Marshalling(input.appId, data);
39 }
40
41 template<>
Unmarshalling(AppId & output,MessageParcel & data)42 bool Unmarshalling(AppId &output, MessageParcel &data)
43 {
44 return ITypesUtil::Unmarshalling(output.appId, data);
45 }
46
47 template<>
Marshalling(const StoreId & input,MessageParcel & data)48 bool Marshalling(const StoreId &input, MessageParcel &data)
49 {
50 return ITypesUtil::Marshalling(input.storeId, data);
51 }
52
53 template<>
Unmarshalling(StoreId & output,MessageParcel & data)54 bool Unmarshalling(StoreId &output, MessageParcel &data)
55 {
56 return ITypesUtil::Unmarshalling(output.storeId, data);
57 }
58
59 template<>
Marshalling(const Entry & entry,MessageParcel & data)60 bool Marshalling(const Entry &entry, MessageParcel &data)
61 {
62 return ITypesUtil::Marshal(data, entry.key, entry.value);
63 }
64
65 template<>
Unmarshalling(Entry & output,MessageParcel & data)66 bool Unmarshalling(Entry &output, MessageParcel &data)
67 {
68 return ITypesUtil::Unmarshal(data, output.key, output.value);
69 }
70
71 template<>
Marshalling(const DeviceInfo & entry,MessageParcel & data)72 bool Marshalling(const DeviceInfo &entry, MessageParcel &data)
73 {
74 return ITypesUtil::Marshal(data, entry.deviceId, entry.deviceName, entry.deviceType);
75 }
76
77 template<>
Unmarshalling(DeviceInfo & output,MessageParcel & data)78 bool Unmarshalling(DeviceInfo &output, MessageParcel &data)
79 {
80 return ITypesUtil::Unmarshal(data, output.deviceId, output.deviceName, output.deviceType);
81 }
82
83 template<>
Marshalling(const ChangeNotification & notification,MessageParcel & parcel)84 bool Marshalling(const ChangeNotification ¬ification, MessageParcel &parcel)
85 {
86 return ITypesUtil::Marshal(parcel, notification.GetInsertEntries(), notification.GetUpdateEntries(),
87 notification.GetDeleteEntries(), notification.GetDeviceId(), notification.IsClear());
88 }
89
90 template<>
Unmarshalling(ChangeNotification & output,MessageParcel & parcel)91 bool Unmarshalling(ChangeNotification &output, MessageParcel &parcel)
92 {
93 std::vector<Entry> inserts;
94 std::vector<Entry> updates;
95 std::vector<Entry> deletes;
96 std::string deviceId;
97 bool isClear = false;
98 if (!ITypesUtil::Unmarshal(parcel, inserts, updates, deletes, deviceId, isClear)) {
99 return false;
100 }
101 output = ChangeNotification(std::move(inserts), std::move(updates), std::move(deletes), deviceId, isClear);
102 return true;
103 }
104
105 template<>
Marshalling(const Options & input,MessageParcel & data)106 bool Marshalling(const Options &input, MessageParcel &data)
107 {
108 if (!ITypesUtil::Marshal(data, input.schema, input.hapName, input.policies, input.cloudConfig.enableCloud,
109 input.cloudConfig.autoSync, input.authType)) {
110 ZLOGE("write policies failed");
111 return false;
112 }
113
114 std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(sizeof(input));
115 Options *target = reinterpret_cast<Options *>(buffer.get());
116 target->createIfMissing = input.createIfMissing;
117 target->encrypt = input.encrypt;
118 target->persistent = input.persistent;
119 target->backup = input.backup;
120 target->autoSync = input.autoSync;
121 target->syncable = input.syncable;
122 target->securityLevel = input.securityLevel;
123 target->area = input.area;
124 target->kvStoreType = input.kvStoreType;
125 target->isNeedCompress = input.isNeedCompress;
126 target->dataType = input.dataType;
127 target->isPublic = input.isPublic;
128 return data.WriteRawData(buffer.get(), sizeof(input));
129 }
130
131 template<>
Unmarshalling(Options & output,MessageParcel & data)132 bool Unmarshalling(Options &output, MessageParcel &data)
133 {
134 if (!ITypesUtil::Unmarshal(data, output.schema, output.hapName, output.policies, output.cloudConfig.enableCloud,
135 output.cloudConfig.autoSync, output.authType)) {
136 ZLOGE("read policies failed");
137 return false;
138 }
139
140 const Options *source = reinterpret_cast<const Options *>(data.ReadRawData(sizeof(output)));
141 if (source == nullptr) {
142 return false;
143 }
144 output.createIfMissing = source->createIfMissing;
145 output.encrypt = source->encrypt;
146 output.persistent = source->persistent;
147 output.backup = source->backup;
148 output.autoSync = source->autoSync;
149 output.securityLevel = source->securityLevel;
150 output.area = source->area;
151 output.kvStoreType = source->kvStoreType;
152 output.syncable = source->syncable;
153 output.isNeedCompress = source->isNeedCompress;
154 output.dataType = source->dataType;
155 output.isPublic = source->isPublic;
156 return true;
157 }
158
159 template<>
Marshalling(const AuthType & input,MessageParcel & data)160 bool Marshalling(const AuthType &input, MessageParcel &data)
161 {
162 return ITypesUtil::Marshal(data, static_cast<int32_t>(input));
163 }
164
165 template<>
Unmarshalling(AuthType & output,MessageParcel & data)166 bool Unmarshalling(AuthType &output, MessageParcel &data)
167 {
168 int32_t authType;
169 if (!ITypesUtil::Unmarshal(data, authType)) {
170 return false;
171 }
172 output = static_cast<AuthType>(authType);
173 return true;
174 }
175
176 template<>
Marshalling(const SyncPolicy & input,MessageParcel & data)177 bool Marshalling(const SyncPolicy &input, MessageParcel &data)
178 {
179 return ITypesUtil::Marshal(data, input.type, input.value);
180 }
181
182 template<>
Unmarshalling(SyncPolicy & output,MessageParcel & data)183 bool Unmarshalling(SyncPolicy &output, MessageParcel &data)
184 {
185 return ITypesUtil::Unmarshal(data, output.type, output.value);
186 }
187
188 template<>
Marshalling(const SwitchData & input,MessageParcel & data)189 bool Marshalling(const SwitchData &input, MessageParcel &data)
190 {
191 return ITypesUtil::Marshal(data, input.value, input.length);
192 }
193
194 template<>
Unmarshalling(SwitchData & output,MessageParcel & data)195 bool Unmarshalling(SwitchData &output, MessageParcel &data)
196 {
197 return ITypesUtil::Unmarshal(data, output.value, output.length);
198 }
199
200 template<>
Marshalling(const Status & input,MessageParcel & data)201 bool Marshalling(const Status &input, MessageParcel &data)
202 {
203 return ITypesUtil::Marshal(data, static_cast<int32_t>(input));
204 }
205
206 template<>
Unmarshalling(Status & output,MessageParcel & data)207 bool Unmarshalling(Status &output, MessageParcel &data)
208 {
209 int32_t status;
210 if (!ITypesUtil::Unmarshal(data, status)) {
211 return false;
212 }
213 output = static_cast<Status>(status);
214 return true;
215 }
216
217 template<>
Marshalling(const Notification & input,MessageParcel & data)218 bool Marshalling(const Notification &input, MessageParcel &data)
219 {
220 return ITypesUtil::Marshal(
221 data, input.data.value, input.data.length, input.deviceId, static_cast<int32_t>(input.state));
222 }
223
224 template<>
Unmarshalling(Notification & output,MessageParcel & data)225 bool Unmarshalling(Notification &output, MessageParcel &data)
226 {
227 int32_t state;
228 if (!ITypesUtil::Unmarshal(data, output.data.value, output.data.length, output.deviceId, state)) {
229 return false;
230 }
231 output.state = static_cast<SwitchState>(state);
232 return true;
233 }
234
235 template<>
Marshalling(const ProgressDetail & input,MessageParcel & data)236 bool Marshalling(const ProgressDetail &input, MessageParcel &data)
237 {
238 return Marshal(data, input.progress, input.code, input.details);
239 }
240 template<>
Unmarshalling(ProgressDetail & output,MessageParcel & data)241 bool Unmarshalling(ProgressDetail &output, MessageParcel &data)
242 {
243 return Unmarshal(data, output.progress, output.code, output.details);
244 }
245 template<>
Marshalling(const TableDetail & input,MessageParcel & data)246 bool Marshalling(const TableDetail &input, MessageParcel &data)
247 {
248 return Marshal(data, input.upload, input.download);
249 }
250 template<>
Unmarshalling(TableDetail & output,MessageParcel & data)251 bool Unmarshalling(TableDetail &output, MessageParcel &data)
252 {
253 return Unmarshal(data, output.upload, output.download);
254 }
255 template<>
Marshalling(const Statistic & input,MessageParcel & data)256 bool Marshalling(const Statistic &input, MessageParcel &data)
257 {
258 return Marshal(data, input.total, input.success, input.failed, input.untreated);
259 }
260 template<>
Unmarshalling(Statistic & output,MessageParcel & data)261 bool Unmarshalling(Statistic &output, MessageParcel &data)
262 {
263 return Unmarshal(data, output.total, output.success, output.failed, output.untreated);
264 }
265
266 template<>
Marshalling(const CloudConfig & input,MessageParcel & data)267 bool Marshalling(const CloudConfig &input, MessageParcel &data)
268 {
269 return Marshal(data, input.enableCloud, input.autoSync);
270 }
271 template<>
Unmarshalling(CloudConfig & output,MessageParcel & data)272 bool Unmarshalling(CloudConfig &output, MessageParcel &data)
273 {
274 return Unmarshal(data, output.enableCloud, output.autoSync);
275 }
276
277 template<>
Marshalling(const StoreConfig & input,MessageParcel & data)278 bool Marshalling(const StoreConfig &input, MessageParcel &data)
279 {
280 return Marshal(data, input.cloudConfig);
281 }
282 template<>
Unmarshalling(StoreConfig & output,MessageParcel & data)283 bool Unmarshalling(StoreConfig &output, MessageParcel &data)
284 {
285 return Unmarshal(data, output.cloudConfig);
286 }
287
GetTotalSize(const std::vector<Entry> & entries)288 int64_t GetTotalSize(const std::vector<Entry> &entries)
289 {
290 int64_t bufferSize = 1;
291 for (const auto &item : entries) {
292 if (item.key.Size() > Entry::MAX_KEY_LENGTH || item.value.Size() > Entry::MAX_VALUE_LENGTH) {
293 return -bufferSize;
294 }
295 bufferSize += item.key.RawSize() + item.value.RawSize();
296 }
297 return bufferSize - 1;
298 }
299
GetTotalSize(const std::vector<Key> & entries)300 int64_t GetTotalSize(const std::vector<Key> &entries)
301 {
302 int64_t bufferSize = 1;
303 for (const auto &item : entries) {
304 if (item.Size() > Entry::MAX_KEY_LENGTH) {
305 return -bufferSize;
306 }
307 bufferSize += item.RawSize();
308 }
309 return bufferSize - 1;
310 }
311 }