• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #define LOG_TAG "ITypesUtil"
17 
18 #include "itypes_util.h"
19 #include "iremote_object.h"
20 #include "log_print.h"
21 
22 namespace OHOS {
23 namespace DistributedKv {
Marshal(MessageParcel & data)24 bool ITypesUtil::Marshal(MessageParcel &data)
25 {
26     return true;
27 }
28 
Unmarshal(MessageParcel & data)29 bool ITypesUtil::Unmarshal(MessageParcel &data)
30 {
31     return true;
32 }
33 
Marshalling(uint32_t input,MessageParcel & data)34 bool ITypesUtil::Marshalling(uint32_t input, MessageParcel &data)
35 {
36     return data.WriteUint32(input);
37 }
38 
Unmarshalling(uint32_t & output,MessageParcel & data)39 bool ITypesUtil::Unmarshalling(uint32_t &output, MessageParcel &data)
40 {
41     return data.ReadUint32(output);
42 }
43 
Marshalling(int32_t input,MessageParcel & data)44 bool ITypesUtil::Marshalling(int32_t input, MessageParcel &data)
45 {
46     return data.WriteInt32(input);
47 }
48 
Unmarshalling(int32_t & output,MessageParcel & data)49 bool ITypesUtil::Unmarshalling(int32_t &output, MessageParcel &data)
50 {
51     return data.ReadInt32(output);
52 }
53 
Marshalling(uint64_t input,MessageParcel & data)54 bool ITypesUtil::Marshalling(uint64_t input, MessageParcel &data)
55 {
56     return data.WriteUint64(input);
57 }
58 
Unmarshalling(uint64_t & output,MessageParcel & data)59 bool ITypesUtil::Unmarshalling(uint64_t &output, MessageParcel &data)
60 {
61     return data.ReadUint64(output);
62 }
63 
Marshalling(IRemoteObject * input,MessageParcel & data)64 bool ITypesUtil::Marshalling(IRemoteObject* input, MessageParcel &data)
65 {
66     return Marshalling(sptr<IRemoteObject>(input), data);
67 }
68 
Marshalling(const std::monostate & input,MessageParcel & data)69 bool ITypesUtil::Marshalling(const std::monostate &input, MessageParcel &data)
70 {
71     return true;
72 }
73 
Unmarshalling(std::monostate & output,MessageParcel & data)74 bool ITypesUtil::Unmarshalling(std::monostate &output, MessageParcel &data)
75 {
76     return true;
77 }
78 
Marshalling(const std::string & input,MessageParcel & data)79 bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data)
80 {
81     return data.WriteString(input);
82 }
83 
Unmarshalling(std::string & output,MessageParcel & data)84 bool ITypesUtil::Unmarshalling(std::string &output, MessageParcel &data)
85 {
86     return data.ReadString(output);
87 }
88 
Marshalling(const std::vector<uint8_t> & input,MessageParcel & data)89 bool ITypesUtil::Marshalling(const std::vector<uint8_t> &input, MessageParcel &data)
90 {
91     return data.WriteUInt8Vector(input);
92 }
93 
Unmarshalling(std::vector<uint8_t> & output,MessageParcel & data)94 bool ITypesUtil::Unmarshalling(std::vector<uint8_t> &output, MessageParcel &data)
95 {
96     return data.ReadUInt8Vector(&output);
97 }
98 
Marshalling(const Blob & blob,MessageParcel & data)99 bool ITypesUtil::Marshalling(const Blob &blob, MessageParcel &data)
100 {
101     return data.WriteUInt8Vector(blob.Data());
102 }
103 
Unmarshalling(Blob & output,MessageParcel & data)104 bool ITypesUtil::Unmarshalling(Blob &output, MessageParcel &data)
105 {
106     std::vector<uint8_t> blob;
107     bool result = data.ReadUInt8Vector(&blob);
108     output = blob;
109     return result;
110 }
111 
Marshalling(const Entry & entry,MessageParcel & data)112 bool ITypesUtil::Marshalling(const Entry &entry, MessageParcel &data)
113 {
114     if (!Marshalling(entry.key, data)) {
115         return false;
116     }
117     return Marshalling(entry.value, data);
118 }
119 
Unmarshalling(Entry & output,MessageParcel & data)120 bool ITypesUtil::Unmarshalling(Entry &output, MessageParcel &data)
121 {
122     if (!Unmarshalling(output.key, data)) {
123         return false;
124     }
125     return Unmarshalling(output.value, data);
126 }
127 
Marshalling(const DeviceInfo & entry,MessageParcel & data)128 bool ITypesUtil::Marshalling(const DeviceInfo &entry, MessageParcel &data)
129 {
130     if (!data.WriteString(entry.deviceId)) {
131         return false;
132     }
133     if (!data.WriteString(entry.deviceName)) {
134         return false;
135     }
136     return data.WriteString(entry.deviceType);
137 }
138 
Unmarshalling(DeviceInfo & output,MessageParcel & data)139 bool ITypesUtil::Unmarshalling(DeviceInfo &output, MessageParcel &data)
140 {
141     if (!data.ReadString(output.deviceId)) {
142         return false;
143     }
144     if (!data.ReadString(output.deviceName)) {
145         return false;
146     }
147     return data.ReadString(output.deviceType);
148 }
149 
Marshalling(const ChangeNotification & notification,MessageParcel & parcel)150 bool ITypesUtil::Marshalling(const ChangeNotification &notification, MessageParcel &parcel)
151 {
152     if (!Marshalling(notification.GetInsertEntries(), parcel)) {
153         return false;
154     }
155 
156     if (!Marshalling(notification.GetUpdateEntries(), parcel)) {
157         return false;
158     }
159 
160     if (!Marshalling(notification.GetDeleteEntries(), parcel)) {
161         return false;
162     }
163     if (!parcel.WriteString(notification.GetDeviceId())) {
164         ZLOGE("WriteString deviceId_ failed.");
165         return false;
166     }
167 
168     return parcel.WriteBool(notification.IsClear());
169 }
170 
Unmarshalling(ChangeNotification & output,MessageParcel & parcel)171 bool ITypesUtil::Unmarshalling(ChangeNotification &output, MessageParcel &parcel)
172 {
173     std::vector<Entry> insertEntries;
174     if (!Unmarshalling(insertEntries, parcel)) {
175         return false;
176     }
177     std::vector<Entry> updateEntries;
178     if (!Unmarshalling(updateEntries, parcel)) {
179         return false;
180     }
181     std::vector<Entry> deleteEntries;
182     if (!Unmarshalling(deleteEntries, parcel)) {
183         return false;
184     }
185     std::string deviceId;
186     if (!parcel.ReadString(deviceId)) {
187         ZLOGE("WriteString deviceId_ failed.");
188         return false;
189     }
190     bool isClear = false;
191     if (!parcel.ReadBool(isClear)) {
192         ZLOGE("WriteString deviceId_ failed.");
193         return false;
194     }
195     output = ChangeNotification(
196         std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), deviceId, isClear);
197     return true;
198 }
199 
Marshalling(const DistributedRdb::RdbSyncerParam & param,MessageParcel & parcel)200 bool ITypesUtil::Marshalling(const DistributedRdb::RdbSyncerParam &param, MessageParcel &parcel)
201 {
202     if (!parcel.WriteString(param.bundleName_)) {
203         ZLOGE("RdbStoreParam write bundle name failed");
204         return false;
205     }
206     if (!parcel.WriteString(param.hapName_)) {
207         ZLOGE("RdbStoreParam write directory failed");
208         return false;
209     }
210     if (!parcel.WriteString(param.storeName_)) {
211         ZLOGE("RdbStoreParam write store name failed");
212         return false;
213     }
214     if (!parcel.WriteInt32(param.area_)) {
215         ZLOGE("RdbStoreParam write security level failed");
216         return false;
217     }
218     if (!parcel.WriteInt32(param.level_)) {
219         ZLOGE("RdbStoreParam write security level failed");
220         return false;
221     }
222     if (!parcel.WriteInt32(param.type_)) {
223         ZLOGE("RdbStoreParam write type failed");
224         return false;
225     }
226     if (!parcel.WriteUInt8Vector(param.password_)) {
227         ZLOGE("RdbStoreParam write password failed");
228         return false;
229     }
230     if (!parcel.WriteBool(param.isAutoSync_)) {
231         ZLOGE("RdbStoreParam write auto sync failed");
232         return false;
233     }
234     if (!parcel.WriteBool(param.isEncrypt_)) {
235         ZLOGE("RdbStoreParam write encrypt sync failed");
236         return false;
237     }
238     return true;
239 }
240 
Unmarshalling(DistributedRdb::RdbSyncerParam & param,MessageParcel & parcel)241 bool ITypesUtil::Unmarshalling(DistributedRdb::RdbSyncerParam &param, MessageParcel &parcel)
242 {
243     if (!parcel.ReadString(param.bundleName_)) {
244         ZLOGE("RdbStoreParam read bundle name failed");
245         return false;
246     }
247     if (!parcel.ReadString(param.hapName_)) {
248         ZLOGE("RdbStoreParam read directory failed");
249         return false;
250     }
251     if (!parcel.ReadString(param.storeName_)) {
252         ZLOGE("RdbStoreParam read store name failed");
253         return false;
254     }
255     if (!parcel.ReadInt32(param.area_)) {
256         ZLOGE("RdbStoreParam read security level failed");
257         return false;
258     }
259     if (!parcel.ReadInt32(param.level_)) {
260         ZLOGE("RdbStoreParam read security level failed");
261         return false;
262     }
263     if (!parcel.ReadInt32(param.type_)) {
264         ZLOGE("RdbStoreParam read type failed");
265         return false;
266     }
267     if (!parcel.ReadUInt8Vector(&(param.password_))) {
268         ZLOGE("RdbStoreParam read password failed");
269         return false;
270     }
271     if (!parcel.ReadBool(param.isAutoSync_)) {
272         ZLOGE("RdbStoreParam read auto sync failed");
273         return false;
274     }
275     if (!parcel.ReadBool(param.isEncrypt_)) {
276         ZLOGE("RdbStoreParam read encrypt sync failed");
277         return false;
278     }
279     return true;
280 }
281 
Marshalling(const DistributedRdb::SyncOption & option,MessageParcel & parcel)282 bool ITypesUtil::Marshalling(const DistributedRdb::SyncOption &option, MessageParcel &parcel)
283 {
284     if (!parcel.WriteInt32(option.mode)) {
285         ZLOGE("SyncOption write mode failed");
286         return false;
287     }
288     if (!parcel.WriteBool(option.isBlock)) {
289         ZLOGE("SyncOption write isBlock failed");
290         return false;
291     }
292     return true;
293 }
294 
Unmarshalling(DistributedRdb::SyncOption & option,MessageParcel & parcel)295 bool ITypesUtil::Unmarshalling(DistributedRdb::SyncOption &option, MessageParcel &parcel)
296 {
297     int32_t mode;
298     if (!parcel.ReadInt32(mode)) {
299         ZLOGE("SyncOption read mode failed");
300         return false;
301     }
302     option.mode = static_cast<DistributedRdb::SyncMode>(mode);
303     if (!parcel.ReadBool(option.isBlock)) {
304         ZLOGE("SyncOption read isBlock failed");
305         return false;
306     }
307     return true;
308 }
309 
Marshalling(const DistributedRdb::RdbPredicates & predicates,MessageParcel & parcel)310 bool ITypesUtil::Marshalling(const DistributedRdb::RdbPredicates &predicates, MessageParcel &parcel)
311 {
312     if (!parcel.WriteString(predicates.table_)) {
313         ZLOGE("predicate write table failed");
314         return false;
315     }
316     if (!parcel.WriteStringVector(predicates.devices_)) {
317         ZLOGE("predicate write devices failed");
318         return false;
319     }
320     if (!parcel.WriteUint32(predicates.operations_.size())) {
321         ZLOGE("predicate write operation size failed");
322         return false;
323     }
324     for (const auto &operation : predicates.operations_) {
325         if (!parcel.WriteInt32(operation.operator_)) {
326             ZLOGE("predicate write operator failed");
327             return false;
328         }
329         if (!parcel.WriteString(operation.field_)) {
330             ZLOGE("predicate write field failed");
331             return false;
332         }
333         if (!parcel.WriteStringVector(operation.values_)) {
334             ZLOGE("predicate write values failed");
335             return false;
336         }
337     }
338     return true;
339 }
340 
Unmarshalling(DistributedRdb::RdbPredicates & predicates,MessageParcel & parcel)341 bool ITypesUtil::Unmarshalling(DistributedRdb::RdbPredicates &predicates, MessageParcel &parcel)
342 {
343     if (!parcel.ReadString(predicates.table_)) {
344         ZLOGE("predicate read table failed");
345         return false;
346     }
347     if (!parcel.ReadStringVector(&predicates.devices_)) {
348         ZLOGE("predicate read devices failed");
349         return false;
350     }
351     uint32_t size = 0;
352     if (!parcel.ReadUint32(size)) {
353         ZLOGE("predicate read operation size failed");
354         return false;
355     }
356     for (uint32_t i = 0; i < size; i++) {
357         int32_t op;
358         if (!parcel.ReadInt32(op)) {
359             ZLOGE("predicate read operator failed");
360             return false;
361         }
362         DistributedRdb::RdbPredicateOperation operation;
363         operation.operator_ = static_cast<DistributedRdb::RdbPredicateOperator>(op);
364         if (!parcel.ReadString(operation.field_)) {
365             ZLOGE("predicate read field failed");
366             return false;
367         }
368         if (!parcel.ReadStringVector(&operation.values_)) {
369             ZLOGE("predicate read values failed");
370             return false;
371         }
372         predicates.operations_.push_back(std::move(operation));
373     }
374     return true;
375 }
376 
Marshalling(const Options & input,MessageParcel & data)377 bool ITypesUtil::Marshalling(const Options &input, MessageParcel &data)
378 {
379     if (!data.WriteString(input.schema)) {
380         ZLOGE("schema is failed");
381         return false;
382     }
383 
384     if (!data.WriteString(input.hapName)) {
385         ZLOGE("hapName is failed");
386         return false;
387     }
388 
389     if (!Marshalling(input.policies, data)) {
390         ZLOGE("write policies failed");
391         return false;
392     }
393 
394     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(sizeof(input));
395     Options *target = reinterpret_cast<Options *>(buffer.get());
396     target->createIfMissing = input.createIfMissing;
397     target->encrypt = input.encrypt;
398     target->persistent = input.persistent;
399     target->backup = input.backup;
400     target->autoSync = input.autoSync;
401     target->syncable = input.syncable;
402     target->securityLevel = input.securityLevel;
403     target->area = input.area;
404     target->kvStoreType = input.kvStoreType;
405     return data.WriteRawData(buffer.get(), sizeof(input));
406 }
407 
Unmarshalling(Options & output,MessageParcel & data)408 bool ITypesUtil::Unmarshalling(Options &output, MessageParcel &data)
409 {
410     if (!data.ReadString(output.schema)) {
411         ZLOGE("read schema failed");
412         return false;
413     }
414 
415     if (!data.ReadString(output.hapName)) {
416         ZLOGE("read hapName failed");
417         return false;
418     }
419 
420     if (!Unmarshalling(output.policies, data)) {
421         ZLOGE("read policies failed");
422         return false;
423     }
424 
425     const Options *source = reinterpret_cast<const Options *>(data.ReadRawData(sizeof(output)));
426     if (source == nullptr) {
427         return false;
428     }
429     output.createIfMissing = source->createIfMissing;
430     output.encrypt = source->encrypt;
431     output.persistent = source->persistent;
432     output.backup = source->backup;
433     output.autoSync = source->autoSync;
434     output.securityLevel = source->securityLevel;
435     output.area = source->area;
436     output.kvStoreType = source->kvStoreType;
437     output.syncable = source->syncable;
438     return true;
439 }
440 
Marshalling(const SyncPolicy & input,MessageParcel & data)441 bool ITypesUtil::Marshalling(const SyncPolicy &input, MessageParcel &data)
442 {
443     if (!data.WriteUint32(input.type)) {
444         ZLOGE("write policy type failed");
445         return false;
446     }
447     if (!Marshalling(input.value, data)) {
448         ZLOGE("write policy value failed");
449         return false;
450     }
451     return true;
452 }
453 
Unmarshalling(SyncPolicy & output,MessageParcel & data)454 bool ITypesUtil::Unmarshalling(SyncPolicy &output, MessageParcel &data)
455 {
456     if (!data.ReadUint32(output.type)) {
457         ZLOGE("read policy type failed");
458         return false;
459     }
460     if (!Unmarshalling(output.value, data)) {
461         ZLOGE("read policy value failed");
462         return false;
463     }
464     return true;
465 }
466 
Marshalling(const KVDBService::DevBrief & input,MessageParcel & data)467 bool ITypesUtil::Marshalling(const KVDBService::DevBrief &input, MessageParcel &data)
468 {
469     return ITypesUtil::Marshal(data, input.uuid, input.networkId);
470 }
471 
Unmarshalling(KVDBService::DevBrief & output,MessageParcel & data)472 bool ITypesUtil::Unmarshalling(KVDBService::DevBrief &output, MessageParcel &data)
473 {
474     return ITypesUtil::Unmarshal(data, output.uuid, output.networkId);
475 }
476 
Marshalling(const sptr<IRemoteObject> & input,MessageParcel & data)477 bool ITypesUtil::Marshalling(const sptr<IRemoteObject> &input, MessageParcel &data)
478 {
479     return data.WriteRemoteObject(input);
480 }
481 
Unmarshalling(sptr<IRemoteObject> & output,MessageParcel & data)482 bool ITypesUtil::Unmarshalling(sptr<IRemoteObject> &output, MessageParcel &data)
483 {
484     output = data.ReadRemoteObject();
485     return true;
486 }
487 
Unmarshalling(DataShare::DataSharePredicates & predicates,MessageParcel & parcel)488 bool ITypesUtil::Unmarshalling(DataShare::DataSharePredicates &predicates, MessageParcel &parcel)
489 {
490     ZLOGD("Unmarshalling DataSharePredicates Start");
491     std::list<DataShare::OperationItem> operations{};
492     std::string whereClause = "";
493     std::vector<std::string> whereArgs;
494     std::string order = "";
495     int64_t mode = DataShare::INVALID_MODE;
496     size_t size = static_cast<size_t>(parcel.ReadInt32());
497     if (static_cast<int32_t>(size) < 0) {
498         ZLOGE("predicate read listSize failed");
499         return false;
500     }
501     if ((size > parcel.GetReadableBytes()) || (operations.max_size() < size)) {
502         ZLOGE("Read operations failed, size : %{public}zu", size);
503         return false;
504     }
505     operations.clear();
506     for (size_t i = 0; i < size; i++) {
507         DataShare::OperationItem listitem{};
508         if (!Unmarshalling(listitem, parcel)) {
509             ZLOGE("operations read OperationItem failed");
510             return false;
511         }
512         operations.push_back(listitem);
513     }
514     if (!parcel.ReadString(whereClause)) {
515         ZLOGE("predicate read whereClause failed");
516         return false;
517     }
518     if (!parcel.ReadStringVector(&whereArgs)) {
519         ZLOGE("predicate read whereArgs failed");
520         return false;
521     }
522     if (!parcel.ReadString(order)) {
523         ZLOGE("predicate read order failed");
524         return false;
525     }
526     if (!parcel.ReadInt64(mode)) {
527         ZLOGE("predicate read mode failed");
528         return false;
529     }
530     DataShare::DataSharePredicates tmpPredicates(operations);
531     tmpPredicates.SetWhereClause(whereClause);
532     tmpPredicates.SetWhereArgs(whereArgs);
533     tmpPredicates.SetOrder(order);
534     tmpPredicates.SetSettingMode(static_cast<DataShare::SettingMode>(mode));
535     predicates = tmpPredicates;
536     return true;
537 }
538 
Unmarshalling(DataShare::DataShareValuesBucket & valuesBucket,MessageParcel & parcel)539 bool ITypesUtil::Unmarshalling(DataShare::DataShareValuesBucket &valuesBucket, MessageParcel &parcel)
540 {
541     int len = parcel.ReadInt32();
542     if (len < 0) {
543         ZLOGE("valuesBucket read mapSize failed");
544         return false;
545     }
546     size_t size = static_cast<size_t>(len);
547     if ((size > parcel.GetReadableBytes()) || (valuesBucket.valuesMap.max_size() < size)) {
548         ZLOGE("Read valuesMap failed, size : %{public}zu", size);
549         return false;
550     }
551     valuesBucket.valuesMap.clear();
552     for (size_t i = 0; i < size; i++) {
553         std::string key = parcel.ReadString();
554         DataShare::DataShareValueObject value{};
555         if (!Unmarshalling(value, parcel)) {
556             ZLOGE("valuesBucket read value failed");
557             return false;
558         }
559         valuesBucket.valuesMap.insert(std::make_pair(key, value));
560     }
561     return true;
562 }
563 
Unmarshalling(DataShare::OperationItem & operationItem,MessageParcel & parcel)564 bool ITypesUtil::Unmarshalling(DataShare::OperationItem &operationItem, MessageParcel &parcel)
565 {
566     operationItem.operation = static_cast<DataShare::OperationType>(parcel.ReadInt64());
567     if (operationItem.operation <DataShare::OperationType::INVALID_OPERATION) {
568         ZLOGE("operationItem read operation failed");
569         return false;
570     }
571     if (!Unmarshalling(operationItem.singleParams, parcel)) {
572         ZLOGE("Unmarshalling singleParams failed");
573         return false;
574     }
575     if (!Unmarshalling(operationItem.multiParams, parcel)) {
576         ZLOGE("Unmarshalling multiParams failed");
577         return false;
578     }
579     return true;
580 }
581 
Unmarshalling(DataShare::DataSharePredicatesObject & predicatesObject,MessageParcel & parcel)582 bool ITypesUtil::Unmarshalling(DataShare::DataSharePredicatesObject &predicatesObject, MessageParcel &parcel)
583 {
584     int16_t type = parcel.ReadInt16();
585     if (type < (int16_t)DataShare::DataSharePredicatesObjectType::TYPE_NULL) {
586         ZLOGE("predicatesObject read type failed");
587         return false;
588     }
589     predicatesObject.type = static_cast<DataShare::DataSharePredicatesObjectType>(type);
590     switch (predicatesObject.type) {
591         case DataShare::DataSharePredicatesObjectType::TYPE_INT: {
592             predicatesObject.value = parcel.ReadInt32();
593             break;
594         }
595         case DataShare::DataSharePredicatesObjectType::TYPE_LONG: {
596             predicatesObject.value = parcel.ReadInt64();
597             break;
598         }
599         case DataShare::DataSharePredicatesObjectType::TYPE_DOUBLE: {
600             predicatesObject.value = parcel.ReadDouble();
601             break;
602         }
603         case DataShare::DataSharePredicatesObjectType::TYPE_STRING: {
604             predicatesObject.value = parcel.ReadString();
605             break;
606         }
607         case DataShare::DataSharePredicatesObjectType::TYPE_BOOL: {
608             predicatesObject.value = parcel.ReadBool();
609             break;
610         }
611         default:
612             break;
613     }
614     return true;
615 }
616 
Unmarshalling(DataShare::DataSharePredicatesObjects & predicatesObject,MessageParcel & parcel)617 bool ITypesUtil::Unmarshalling(DataShare::DataSharePredicatesObjects &predicatesObject, MessageParcel &parcel)
618 {
619     int16_t type = parcel.ReadInt16();
620     if (type < (int16_t)DataShare::DataSharePredicatesObjectsType::TYPE_NULL) {
621         ZLOGE("predicatesObject read type failed");
622         return false;
623     }
624     predicatesObject.type = static_cast<DataShare::DataSharePredicatesObjectsType>(type);
625     switch (predicatesObject.type) {
626         case DataShare::DataSharePredicatesObjectsType::TYPE_INT_VECTOR: {
627             std::vector<int> intval{};
628             if (!parcel.ReadInt32Vector(&intval)) {
629                 ZLOGE("predicatesObject ReadInt32Vector value failed");
630                 return false;
631             }
632             predicatesObject.value = intval;
633             break;
634         }
635         case DataShare::DataSharePredicatesObjectsType::TYPE_LONG_VECTOR: {
636             std::vector<int64_t> int64val{};
637             if (!parcel.ReadInt64Vector(&int64val)) {
638                 ZLOGE("predicatesObject ReadInt64Vector value failed");
639                 return false;
640             }
641             predicatesObject.value = int64val;
642             break;
643         }
644         case DataShare::DataSharePredicatesObjectsType::TYPE_DOUBLE_VECTOR: {
645             std::vector<double> doubleval{};
646             if (!parcel.ReadDoubleVector(&doubleval)) {
647                 ZLOGE("predicatesObject ReadDoubleVector value failed");
648                 return false;
649             }
650             predicatesObject.value = doubleval;
651             break;
652         }
653         case DataShare::DataSharePredicatesObjectsType::TYPE_STRING_VECTOR: {
654             std::vector<std::string> stringval{};
655             if (!parcel.ReadStringVector(&stringval)) {
656                 ZLOGE("predicatesObject ReadDoubReadStringVectorleVector value failed");
657                 return false;
658             }
659             predicatesObject.value = stringval;
660             break;
661         }
662         default:
663             break;
664     }
665     return true;
666 }
667 
Unmarshalling(DataShare::DataShareValueObject & valueObject,MessageParcel & parcel)668 bool ITypesUtil::Unmarshalling(DataShare::DataShareValueObject &valueObject, MessageParcel &parcel)
669 {
670     int16_t type = parcel.ReadInt16();
671     if (type < (int16_t)DataShare::DataShareValueObjectType::TYPE_NULL) {
672         ZLOGE("valueObject read type failed");
673         return false;
674     }
675     valueObject.type = static_cast<DataShare::DataShareValueObjectType>(type);
676     switch (valueObject.type) {
677         case DataShare::DataShareValueObjectType::TYPE_INT: {
678             valueObject.value = parcel.ReadInt64();
679             break;
680         }
681         case DataShare::DataShareValueObjectType::TYPE_DOUBLE: {
682             valueObject.value = parcel.ReadDouble();
683             break;
684         }
685         case DataShare::DataShareValueObjectType::TYPE_STRING: {
686             valueObject.value = parcel.ReadString();
687             break;
688         }
689         case DataShare::DataShareValueObjectType::TYPE_BLOB: {
690             std::vector<uint8_t> val;
691             if (!parcel.ReadUInt8Vector(&val)) {
692                 ZLOGE("valueObject ReadUInt8Vector value failed");
693                 return false;
694             }
695             valueObject.value = val;
696             break;
697         }
698         case DataShare::DataShareValueObjectType::TYPE_BOOL: {
699             valueObject.value = parcel.ReadBool();
700             break;
701         }
702         default:
703             break;
704     }
705     return true;
706 }
707 
GetTotalSize(const std::vector<Entry> & entries)708 int64_t ITypesUtil::GetTotalSize(const std::vector<Entry> &entries)
709 {
710     int64_t bufferSize = 1;
711     for (const auto &item : entries) {
712         if (item.key.Size() > Entry::MAX_KEY_LENGTH || item.value.Size() > Entry::MAX_VALUE_LENGTH) {
713             return -bufferSize;
714         }
715         bufferSize += item.key.RawSize() + item.value.RawSize();
716     }
717     return bufferSize - 1;
718 }
719 
GetTotalSize(const std::vector<Key> & entries)720 int64_t ITypesUtil::GetTotalSize(const std::vector<Key> &entries)
721 {
722     int64_t bufferSize = 1;
723     for (const auto &item : entries) {
724         if (item.Size() > Entry::MAX_KEY_LENGTH) {
725             return -bufferSize;
726         }
727         bufferSize += item.RawSize();
728     }
729     return bufferSize - 1;
730 }
731 } // namespace DistributedKv
732 } // namespace OHOS