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 #include "datashare_itypes_utils.h"
17
18 #include <cstdint>
19 #include <iomanip>
20 #include <sstream>
21 #include <variant>
22 #include "dataproxy_handle_common.h"
23 #include "datashare_log.h"
24 #include "datashare_value_object.h"
25 #include "itypes_util.h"
26
27 namespace OHOS::ITypesUtil {
28 using namespace OHOS::DataShare;
29
30 // Maximum value of IPC shared memory
31 static const size_t MAX_IPC_SIZE = 128 * 1024 * 1024;
32
33 template<>
Marshalling(const Predicates & predicates,MessageParcel & parcel)34 bool Marshalling(const Predicates &predicates, MessageParcel &parcel)
35 {
36 const auto &operations = predicates.GetOperationList();
37 int16_t mode = predicates.GetSettingMode();
38 return ITypesUtil::Marshal(parcel, operations, predicates.GetWhereClause(), predicates.GetWhereArgs(),
39 predicates.GetOrder(), mode);
40 }
41
42 template<>
Unmarshalling(Predicates & predicates,MessageParcel & parcel)43 bool Unmarshalling(Predicates &predicates, MessageParcel &parcel)
44 {
45 std::vector<Operation> operations{};
46 std::string whereClause = "";
47 std::vector<std::string> whereArgs;
48 std::string order = "";
49 int16_t mode = DataShare::INVALID_MODE;
50 if (!ITypesUtil::Unmarshal(parcel, operations, whereClause, whereArgs, order, mode)) {
51 return false;
52 }
53 DataShare::DataSharePredicates tmpPredicates(operations);
54 tmpPredicates.SetWhereClause(whereClause);
55 tmpPredicates.SetWhereArgs(whereArgs);
56 tmpPredicates.SetOrder(order);
57 tmpPredicates.SetSettingMode(mode);
58 predicates = tmpPredicates;
59 return true;
60 }
61
62 template<>
Marshalling(const BatchUpdateResult & result,MessageParcel & parcel)63 bool Marshalling(const BatchUpdateResult &result, MessageParcel &parcel)
64 {
65 return ITypesUtil::Marshal(parcel, result.uri, result.codes);
66 }
67
68 template<>
Unmarshalling(BatchUpdateResult & result,MessageParcel & parcel)69 bool Unmarshalling(BatchUpdateResult &result, MessageParcel &parcel)
70 {
71 return ITypesUtil::Unmarshal(parcel, result.uri, result.codes);
72 }
73
74 template<>
Marshalling(const UpdateOperation & operation,MessageParcel & parcel)75 bool Marshalling(const UpdateOperation &operation, MessageParcel &parcel)
76 {
77 return ITypesUtil::Marshal(parcel, operation.valuesBucket, operation.predicates);
78 }
79
80 template<>
Unmarshalling(UpdateOperation & operation,MessageParcel & parcel)81 bool Unmarshalling(UpdateOperation &operation, MessageParcel &parcel)
82 {
83 return ITypesUtil::Unmarshal(parcel, operation.valuesBucket, operation.predicates);
84 }
85
86 template<>
Marshalling(const Operation & operation,MessageParcel & parcel)87 bool Marshalling(const Operation &operation, MessageParcel &parcel)
88 {
89 return ITypesUtil::Marshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
90 }
91
92 template<>
Unmarshalling(Operation & operation,MessageParcel & parcel)93 bool Unmarshalling(Operation &operation, MessageParcel &parcel)
94 {
95 return ITypesUtil::Unmarshal(parcel, operation.operation, operation.singleParams, operation.multiParams);
96 }
97
98 template<>
Unmarshalling(PublishedDataItem & dataItem,MessageParcel & parcel)99 bool Unmarshalling(PublishedDataItem &dataItem, MessageParcel &parcel)
100 {
101 return ITypesUtil::Unmarshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
102 }
103
104 template<>
Marshalling(const PublishedDataItem & dataItem,MessageParcel & parcel)105 bool Marshalling(const PublishedDataItem &dataItem, MessageParcel &parcel)
106 {
107 return ITypesUtil::Marshal(parcel, dataItem.key_, dataItem.subscriberId_, dataItem.value_);
108 }
109
110 template<>
Marshalling(const Data & data,MessageParcel & parcel)111 bool Marshalling(const Data &data, MessageParcel &parcel)
112 {
113 return ITypesUtil::Marshal(parcel, data.datas_, data.version_);
114 }
115
116 template<>
Unmarshalling(Data & data,MessageParcel & parcel)117 bool Unmarshalling(Data &data, MessageParcel &parcel)
118 {
119 return ITypesUtil::Unmarshal(parcel, data.datas_, data.version_);
120 }
121
122 template<>
Unmarshalling(TemplateId & templateId,MessageParcel & parcel)123 bool Unmarshalling(TemplateId &templateId, MessageParcel &parcel)
124 {
125 return ITypesUtil::Unmarshal(parcel, templateId.subscriberId_, templateId.bundleName_);
126 }
127
128 template<>
Marshalling(const TemplateId & templateId,MessageParcel & parcel)129 bool Marshalling(const TemplateId &templateId, MessageParcel &parcel)
130 {
131 return ITypesUtil::Marshal(parcel, templateId.subscriberId_, templateId.bundleName_);
132 }
133
134 template<>
Marshalling(const PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)135 bool Marshalling(const PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
136 {
137 return ITypesUtil::Marshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
138 }
139
140 template<>
Unmarshalling(PredicateTemplateNode & predicateTemplateNode,MessageParcel & parcel)141 bool Unmarshalling(PredicateTemplateNode &predicateTemplateNode, MessageParcel &parcel)
142 {
143 return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.selectSql_);
144 }
145
146 template<>
Marshalling(const RdbChangeNode & changeNode,MessageParcel & parcel)147 bool Marshalling(const RdbChangeNode &changeNode, MessageParcel &parcel)
148 {
149 bool firstPart = ITypesUtil::Marshal(
150 parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_, changeNode.isSharedMemory_);
151 if (!firstPart) {
152 return false;
153 }
154 if (changeNode.isSharedMemory_) {
155 if (changeNode.memory_ == nullptr) {
156 LOG_ERROR("Used shared memory but ashmem is nullptr.");
157 return false;
158 }
159 if (!parcel.WriteAshmem(changeNode.memory_)) {
160 return false;
161 }
162 }
163 return ITypesUtil::Marshal(parcel, changeNode.size_);
164 }
165
166 template<>
Unmarshalling(RdbChangeNode & changeNode,MessageParcel & parcel)167 bool Unmarshalling(RdbChangeNode &changeNode, MessageParcel &parcel)
168 {
169 bool firstPart = ITypesUtil::Unmarshal(
170 parcel, changeNode.uri_, changeNode.templateId_, changeNode.data_, changeNode.isSharedMemory_);
171 if (!firstPart) {
172 return false;
173 }
174 if (changeNode.isSharedMemory_) {
175 changeNode.memory_ = parcel.ReadAshmem();
176 } else {
177 changeNode.memory_ = nullptr;
178 }
179 return ITypesUtil::Unmarshal(parcel, changeNode.size_);
180 }
181
182 template<>
Marshalling(const PublishedDataChangeNode & changeNode,MessageParcel & parcel)183 bool Marshalling(const PublishedDataChangeNode &changeNode, MessageParcel &parcel)
184 {
185 return ITypesUtil::Marshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
186 }
187
188 template<>
Unmarshalling(PublishedDataChangeNode & changeNode,MessageParcel & parcel)189 bool Unmarshalling(PublishedDataChangeNode &changeNode, MessageParcel &parcel)
190 {
191 return ITypesUtil::Unmarshal(parcel, changeNode.ownerBundleName_, changeNode.datas_);
192 }
193
194 template<>
Marshalling(const OperationResult & operationResult,MessageParcel & parcel)195 bool Marshalling(const OperationResult &operationResult, MessageParcel &parcel)
196 {
197 return ITypesUtil::Marshal(parcel, operationResult.key_, operationResult.errCode_);
198 }
199
200 template<>
Unmarshalling(OperationResult & predicateTemplateNode,MessageParcel & parcel)201 bool Unmarshalling(OperationResult &predicateTemplateNode, MessageParcel &parcel)
202 {
203 return ITypesUtil::Unmarshal(parcel, predicateTemplateNode.key_, predicateTemplateNode.errCode_);
204 }
205
206 template<>
Unmarshalling(AshmemNode & node,MessageParcel & parcel)207 bool Unmarshalling(AshmemNode &node, MessageParcel &parcel)
208 {
209 node.isManaged = true;
210 node.ashmem = parcel.ReadAshmem();
211 return true;
212 }
213
214 template<>
Marshalling(const AshmemNode & node,MessageParcel & parcel)215 bool Marshalling(const AshmemNode &node, MessageParcel &parcel)
216 {
217 return parcel.WriteAshmem(node.ashmem);
218 }
219
220 template<>
Marshalling(const Uri & node,MessageParcel & parcel)221 bool Marshalling(const Uri &node, MessageParcel &parcel)
222 {
223 return parcel.WriteParcelable(&node);
224 }
225
226 template<>
Unmarshalling(Uri & node,MessageParcel & parcel)227 bool Unmarshalling(Uri &node, MessageParcel &parcel)
228 {
229 auto uri = std::shared_ptr<Uri>(parcel.ReadParcelable<Uri>());
230 if (uri == nullptr) {
231 return false;
232 }
233 node = *uri;
234 return true;
235 }
236
237 template<>
Marshalling(const DataShareValuesBucket & bucket,MessageParcel & parcel)238 bool Marshalling(const DataShareValuesBucket &bucket, MessageParcel &parcel)
239 {
240 return ITypesUtil::Marshal(parcel, bucket.valuesMap);
241 }
242
243 template<>
Unmarshalling(DataShareValuesBucket & bucket,MessageParcel & parcel)244 bool Unmarshalling(DataShareValuesBucket &bucket, MessageParcel &parcel)
245 {
246 return ITypesUtil::Unmarshal(parcel, bucket.valuesMap);
247 }
248
249 template<>
Marshalling(const OperationStatement & operationStatement,MessageParcel & parcel)250 bool Marshalling(const OperationStatement &operationStatement, MessageParcel &parcel)
251 {
252 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(operationStatement.operationType),
253 operationStatement.uri, operationStatement.valuesBucket, operationStatement.predicates);
254 }
255
256 template<>
Unmarshalling(OperationStatement & operationStatement,MessageParcel & parcel)257 bool Unmarshalling(OperationStatement &operationStatement, MessageParcel &parcel)
258 {
259 int type;
260 if (!ITypesUtil::Unmarshalling(type, parcel)) {
261 return false;
262 }
263 operationStatement.operationType = static_cast<DataShare::Operation>(type);
264 return ITypesUtil::Unmarshal(parcel, operationStatement.uri,
265 operationStatement.valuesBucket, operationStatement.predicates);
266 }
267
268 template<>
Marshalling(const ExecResult & execResult,MessageParcel & parcel)269 bool Marshalling(const ExecResult &execResult, MessageParcel &parcel)
270 {
271 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResult.operationType), execResult.code,
272 execResult.message);
273 }
274
275 template<>
Unmarshalling(ExecResult & execResult,MessageParcel & parcel)276 bool Unmarshalling(ExecResult &execResult, MessageParcel &parcel)
277 {
278 int type;
279 if (!ITypesUtil::Unmarshalling(type, parcel)) {
280 return false;
281 }
282 execResult.operationType = static_cast<DataShare::Operation>(type);
283 return ITypesUtil::Unmarshal(parcel, execResult.code, execResult.message);
284 }
285
286 template<>
Marshalling(const ExecResultSet & execResultSet,MessageParcel & parcel)287 bool Marshalling(const ExecResultSet &execResultSet, MessageParcel &parcel)
288 {
289 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(execResultSet.errorCode), execResultSet.results);
290 }
291
292 template<>
Unmarshalling(ExecResultSet & execResultSet,MessageParcel & parcel)293 bool Unmarshalling(ExecResultSet &execResultSet, MessageParcel &parcel)
294 {
295 int errorCode;
296 if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
297 return false;
298 }
299 execResultSet.errorCode = static_cast<DataShare::ExecErrorCode>(errorCode);
300 return ITypesUtil::Unmarshal(parcel, execResultSet.results);
301 }
302
303 template<>
Marshalling(const DataShareProxyData & proxyData,MessageParcel & parcel)304 bool Marshalling(const DataShareProxyData &proxyData, MessageParcel &parcel)
305 {
306 return ITypesUtil::Marshal(parcel, proxyData.uri_,
307 proxyData.value_, proxyData.isValueUndefined, proxyData.allowList_, proxyData.isAllowListUndefined);
308 }
309
310 template<>
Unmarshalling(DataShareProxyData & proxyData,MessageParcel & parcel)311 bool Unmarshalling(DataShareProxyData &proxyData, MessageParcel &parcel)
312 {
313 return ITypesUtil::Unmarshal(parcel, proxyData.uri_,
314 proxyData.value_, proxyData.isValueUndefined, proxyData.allowList_, proxyData.isAllowListUndefined);
315 }
316
317 template<>
Marshalling(const DataProxyConfig & config,MessageParcel & parcel)318 bool Marshalling(const DataProxyConfig &config, MessageParcel &parcel)
319 {
320 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(config.type_));
321 }
322
323 template<>
Unmarshalling(DataProxyConfig & config,MessageParcel & parcel)324 bool Unmarshalling(DataProxyConfig &config, MessageParcel &parcel)
325 {
326 int type;
327 if (!ITypesUtil::Unmarshalling(type, parcel)) {
328 return false;
329 }
330 config.type_ = static_cast<DataProxyType>(type);
331 return true;
332 }
333
334 template<>
Marshalling(const DataProxyResult & result,MessageParcel & parcel)335 bool Marshalling(const DataProxyResult &result, MessageParcel &parcel)
336 {
337 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(result.result_), result.uri_);
338 }
339
340 template<>
Unmarshalling(DataProxyResult & result,MessageParcel & parcel)341 bool Unmarshalling(DataProxyResult &result, MessageParcel &parcel)
342 {
343 int errorCode;
344 if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
345 return false;
346 }
347 result.result_ = static_cast<DataProxyErrorCode>(errorCode);
348 return ITypesUtil::Unmarshal(parcel, result.uri_);
349 }
350
351 template<>
Marshalling(const DataShareValueObject & value,MessageParcel & parcel)352 bool Marshalling(const DataShareValueObject &value, MessageParcel &parcel)
353 {
354 if (!ITypesUtil::Marshal(parcel, value.value.index())) {
355 return false;
356 }
357 switch (value.value.index()) {
358 case DataShareValueObjectType::TYPE_INT: {
359 return ITypesUtil::Marshal(parcel, std::get<int64_t>(value.value));
360 }
361 case DataShareValueObjectType::TYPE_DOUBLE: {
362 return ITypesUtil::Marshal(parcel, std::get<double>(value.value));
363 }
364 case DataShareValueObjectType::TYPE_STRING: {
365 return ITypesUtil::Marshal(parcel, std::get<std::string>(value.value));
366 }
367 case DataShareValueObjectType::TYPE_BOOL: {
368 return ITypesUtil::Marshal(parcel, std::get<bool>(value.value));
369 }
370 default: {
371 LOG_ERROR("Marshal ValueObject: unknown typeId");
372 return false;
373 }
374 }
375 }
376
377 template<>
Unmarshalling(DataShareValueObject & value,MessageParcel & parcel)378 bool Unmarshalling(DataShareValueObject &value, MessageParcel &parcel)
379 {
380 int32_t index;
381 if (!ITypesUtil::Unmarshal(parcel, index)) {
382 return false;
383 }
384 bool ret = true;
385 switch (index) {
386 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_INT): {
387 int64_t val;
388 ret = ITypesUtil::Unmarshal(parcel, val);
389 value = val;
390 break;
391 }
392 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_DOUBLE): {
393 double val;
394 ret = ITypesUtil::Unmarshal(parcel, val);
395 value = val;
396 break;
397 }
398 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_STRING): {
399 std::string val;
400 ret = ITypesUtil::Unmarshal(parcel, val);
401 value = val;
402 break;
403 }
404 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_BOOL): {
405 bool val;
406 ret = ITypesUtil::Unmarshal(parcel, val);
407 value = val;
408 break;
409 }
410 default: {
411 LOG_ERROR("Unmarshal ValueObject: unknown typeId");
412 ret = false;
413 }
414 }
415 return ret;
416 }
417
418 template<>
Marshalling(const DataProxyValue & value,MessageParcel & parcel)419 bool Marshalling(const DataProxyValue &value, MessageParcel &parcel)
420 {
421 int32_t index = value.index();
422 if (!ITypesUtil::Marshal(parcel, index)) {
423 return false;
424 }
425 switch (value.index()) {
426 case static_cast<uint8_t>(DataProxyValueType::VALUE_INT): {
427 return ITypesUtil::Marshal(parcel, std::get<int64_t>(value));
428 }
429 case static_cast<uint8_t>(DataProxyValueType::VALUE_DOUBLE): {
430 return ITypesUtil::Marshal(parcel, std::get<double>(value));
431 }
432 case static_cast<uint8_t>(DataProxyValueType::VALUE_STRING): {
433 return ITypesUtil::Marshal(parcel, std::get<std::string>(value));
434 }
435 case static_cast<uint8_t>(DataProxyValueType::VALUE_BOOL): {
436 return ITypesUtil::Marshal(parcel, std::get<bool>(value));
437 }
438 default: {
439 LOG_ERROR("Marshal ValueObject: unknown typeId");
440 return false;
441 }
442 }
443 }
444
445 template<>
Unmarshalling(DataProxyValue & value,MessageParcel & parcel)446 bool Unmarshalling(DataProxyValue &value, MessageParcel &parcel)
447 {
448 int32_t index;
449 if (!ITypesUtil::Unmarshal(parcel, index)) {
450 return false;
451 }
452 bool ret = true;
453 switch (index) {
454 case static_cast<uint8_t>(DataProxyValueType::VALUE_INT): {
455 int64_t val;
456 ret = ITypesUtil::Unmarshal(parcel, val);
457 value = val;
458 break;
459 }
460 case static_cast<uint8_t>(DataProxyValueType::VALUE_DOUBLE): {
461 double val;
462 ret = ITypesUtil::Unmarshal(parcel, val);
463 value = val;
464 break;
465 }
466 case static_cast<uint8_t>(DataProxyValueType::VALUE_STRING): {
467 std::string val;
468 ret = ITypesUtil::Unmarshal(parcel, val);
469 value = val;
470 break;
471 }
472 case static_cast<uint8_t>(DataProxyValueType::VALUE_BOOL): {
473 bool val;
474 ret = ITypesUtil::Unmarshal(parcel, val);
475 value = val;
476 break;
477 }
478 default: {
479 LOG_ERROR("Unmarshal DataProxyValue: unknown typeId");
480 ret = false;
481 }
482 }
483 return ret;
484 }
485
486 template<>
Marshalling(const DataProxyGetResult & result,MessageParcel & parcel)487 bool Marshalling(const DataProxyGetResult &result, MessageParcel &parcel)
488 {
489 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(result.result_),
490 result.uri_, result.value_, result.allowList_);
491 }
492
493 template<>
Unmarshalling(DataProxyGetResult & result,MessageParcel & parcel)494 bool Unmarshalling(DataProxyGetResult &result, MessageParcel &parcel)
495 {
496 int errorCode;
497 if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
498 return false;
499 }
500 result.result_ = static_cast<DataProxyErrorCode>(errorCode);
501 return ITypesUtil::Unmarshal(parcel, result.uri_, result.value_, result.allowList_);
502 }
503
504 template<>
Marshalling(const DataProxyChangeInfo & changeInfo,MessageParcel & parcel)505 bool Marshalling(const DataProxyChangeInfo &changeInfo, MessageParcel &parcel)
506 {
507 return ITypesUtil::Marshal(parcel, static_cast<int32_t>(changeInfo.changeType_),
508 changeInfo.uri_, changeInfo.value_);
509 }
510
511 template<>
Unmarshalling(DataProxyChangeInfo & changeInfo,MessageParcel & parcel)512 bool Unmarshalling(DataProxyChangeInfo &changeInfo, MessageParcel &parcel)
513 {
514 int errorCode;
515 if (!ITypesUtil::Unmarshalling(errorCode, parcel)) {
516 return false;
517 }
518 changeInfo.changeType_ = static_cast<DataShareObserver::ChangeType>(errorCode);
519 return ITypesUtil::Unmarshal(parcel, changeInfo.uri_, changeInfo.value_);
520 }
521
522 template<>
Marshalling(const UriInfo & result,MessageParcel & parcel)523 bool Marshalling(const UriInfo &result, MessageParcel &parcel)
524 {
525 return ITypesUtil::Marshal(parcel, result.uri, result.extUri, result.option.timeout);
526 }
527
528 template<>
Unmarshalling(UriInfo & result,MessageParcel & parcel)529 bool Unmarshalling(UriInfo &result, MessageParcel &parcel)
530 {
531 return ITypesUtil::Unmarshal(parcel, result.uri, result.extUri, result.option.timeout);
532 }
533
534 template <typename T>
MarshalBasicTypeToBuffer(std::ostringstream & oss,const T & value)535 bool MarshalBasicTypeToBuffer(std::ostringstream &oss, const T &value)
536 {
537 oss.write(reinterpret_cast<const char *>(&value), sizeof(value));
538 return oss.good();
539 }
540
541 template <typename T>
MarshalBasicTypeVecToBuffer(std::ostringstream & oss,const std::vector<T> & values)542 bool MarshalBasicTypeVecToBuffer(std::ostringstream &oss, const std::vector<T> &values)
543 {
544 size_t valSize = values.size();
545 if (!MarshalBasicTypeToBuffer(oss, valSize)) {
546 return false;
547 }
548 if (valSize > 0)
549 oss.write(reinterpret_cast<const char *>(values.data()), valSize * sizeof(T));
550 return oss.good();
551 }
552
MarshalStringToBuffer(std::ostringstream & oss,const std::string & value)553 bool MarshalStringToBuffer(std::ostringstream &oss, const std::string &value)
554 {
555 // write string length
556 size_t len = value.length();
557 if (!MarshalBasicTypeToBuffer(oss, len)) {
558 return false;
559 }
560 // write string data
561 if (len > 0) {
562 oss.write(value.data(), len);
563 }
564 return oss.good();
565 }
566
MarshalStringVecToBuffer(std::ostringstream & oss,const std::vector<std::string> & values)567 bool MarshalStringVecToBuffer(std::ostringstream &oss, const std::vector<std::string> &values)
568 {
569 // write vector size
570 size_t len = values.size();
571 if (!MarshalBasicTypeToBuffer(oss, len)) {
572 return false;
573 }
574 for (const auto &it : values) {
575 if (!MarshalStringToBuffer(oss, it)) {
576 return false;
577 }
578 }
579 return oss.good();
580 }
581
MarshalSingleTypeToBuffer(std::ostringstream & oss,const SingleValue::Type & value)582 bool MarshalSingleTypeToBuffer(std::ostringstream &oss, const SingleValue::Type &value)
583 {
584 // write typeId
585 uint8_t typeId = value.index();
586 if (!MarshalBasicTypeToBuffer(oss, typeId)) {
587 return false;
588 }
589 switch (typeId) {
590 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_NULL): {
591 return oss.good();
592 }
593 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_INT): {
594 int val = std::get<int>(value);
595 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
596 break;
597 }
598 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_DOUBLE): {
599 double val = std::get<double>(value);
600 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
601 break;
602 }
603 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_STRING): {
604 std::string val = std::get<std::string>(value);
605 if (!MarshalStringToBuffer(oss, val)) { return false; }
606 break;
607 }
608 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_BOOL): {
609 bool val = std::get<bool>(value);
610 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
611 break;
612 }
613 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_LONG): {
614 int64_t val = std::get<int64_t>(value);
615 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
616 break;
617 }
618 default:
619 LOG_ERROR("MarshalSingleTypeToBuffer: unknown typeId");
620 return false;
621 }
622 return oss.good();
623 }
624
MarshalSingleTypeVecToBuffer(std::ostringstream & oss,const std::vector<SingleValue::Type> & values)625 bool MarshalSingleTypeVecToBuffer(std::ostringstream &oss, const std::vector<SingleValue::Type> &values)
626 {
627 // write vector size
628 size_t len = values.size();
629 if (!MarshalBasicTypeToBuffer(oss, len)) {
630 return false;
631 }
632 for (const auto &it : values) {
633 if (!MarshalSingleTypeToBuffer(oss, it)) {
634 return false;
635 }
636 }
637 return oss.good();
638 }
639
MarshalMultiTypeToBuffer(std::ostringstream & oss,const MutliValue::Type & value)640 bool MarshalMultiTypeToBuffer(std::ostringstream &oss, const MutliValue::Type &value)
641 {
642 uint8_t typeId = value.index();
643 if (!MarshalBasicTypeToBuffer(oss, typeId)) {
644 return false;
645 }
646 // add offset of TYPE_NULL
647 typeId += static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_NULL);
648 switch (typeId) {
649 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_NULL): {
650 return oss.good();
651 }
652 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_INT_VECTOR): {
653 std::vector<int> val = std::get<std::vector<int>>(value);
654 if (!MarshalBasicTypeVecToBuffer(oss, val)) { return false; }
655 break;
656 }
657 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_DOUBLE_VECTOR): {
658 std::vector<double> val = std::get<std::vector<double>>(value);
659 if (!MarshalBasicTypeVecToBuffer(oss, val)) { return false; }
660 break;
661 }
662 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_LONG_VECTOR): {
663 std::vector<int64_t> val = std::get<std::vector<int64_t>>(value);
664 if (!MarshalBasicTypeVecToBuffer(oss, val)) { return false; }
665 break;
666 }
667 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_STRING_VECTOR): {
668 auto val = std::get<std::vector<std::string>>(value);
669 if (!MarshalStringVecToBuffer(oss, val)) { return false; }
670 break;
671 }
672 default:
673 LOG_ERROR("MarshalMultiTypeToBuffer: unknown typeId");
674 return false;
675 }
676 return oss.good();
677 }
678
MarshalMultiTypeVecToBuffer(std::ostringstream & oss,const std::vector<MutliValue::Type> & values)679 bool MarshalMultiTypeVecToBuffer(std::ostringstream &oss, const std::vector<MutliValue::Type> &values)
680 {
681 size_t len = values.size();
682 if (!MarshalBasicTypeToBuffer(oss, len)) {
683 return false;
684 }
685 for (const auto &it : values) {
686 if (!MarshalMultiTypeToBuffer(oss, it)) {
687 return false;
688 }
689 }
690 return oss.good();
691 }
692
MarshalOperationItemToBuffer(std::ostringstream & oss,const OperationItem & value)693 bool MarshalOperationItemToBuffer(std::ostringstream &oss, const OperationItem &value)
694 {
695 int32_t operation = value.operation;
696 std::vector<SingleValue::Type> singleParams = value.singleParams;
697 std::vector<MutliValue::Type> multiParams = value.multiParams;
698
699 // Serialize operation
700 if (!MarshalBasicTypeToBuffer(oss, operation)) {
701 return false;
702 }
703 // Serialize singleParams
704 if (!MarshalSingleTypeVecToBuffer(oss, singleParams)) {
705 return false;
706 }
707 // Serialize multiParams
708 if (!MarshalMultiTypeVecToBuffer(oss, multiParams)) {
709 return false;
710 }
711 return oss.good();
712 }
713
MarshalOperationItemVecToBuffer(std::ostringstream & oss,const std::vector<OperationItem> & values)714 bool MarshalOperationItemVecToBuffer(std::ostringstream &oss, const std::vector<OperationItem> &values)
715 {
716 size_t len = values.size();
717 if (!MarshalBasicTypeToBuffer(oss, len)) {
718 return false;
719 }
720 for (const auto &it : values) {
721 if (!MarshalOperationItemToBuffer(oss, it)) {
722 return false;
723 }
724 }
725 return oss.good();
726 }
727
MarshalPredicatesToBuffer(std::ostringstream & oss,const DataSharePredicates & predicates)728 bool MarshalPredicatesToBuffer(std::ostringstream &oss, const DataSharePredicates &predicates)
729 {
730 // Extract all members of predicates
731 const std::vector<OperationItem> &operations = predicates.GetOperationList();
732 std::string whereClause = predicates.GetWhereClause();
733 std::vector<std::string> whereArgs = predicates.GetWhereArgs();
734 std::string order = predicates.GetOrder();
735 short mode = predicates.GetSettingMode();
736
737 // Serialize operations
738 if (!MarshalOperationItemVecToBuffer(oss, operations)) {
739 return false;
740 }
741 // Serialize whereClause
742 if (!MarshalStringToBuffer(oss, whereClause)) {
743 return false;
744 }
745 // Serialize whereArgs
746 if (!MarshalStringVecToBuffer(oss, whereArgs)) {
747 return false;
748 }
749 // Serialize order
750 if (!MarshalStringToBuffer(oss, order)) {
751 return false;
752 }
753 // Serialize mode
754 if (!MarshalBasicTypeToBuffer(oss, mode)) {
755 return false;
756 }
757 return oss.good();
758 }
759
MarshalValuesBucketToBuffer(std::ostringstream & oss,const DataShareValuesBucket & bucket)760 bool MarshalValuesBucketToBuffer(std::ostringstream &oss, const DataShareValuesBucket &bucket)
761 {
762 for (const auto &[key, value] : bucket.valuesMap) {
763 // write key
764 if (!MarshalStringToBuffer(oss, key)) { return false; }
765 // write typeId
766 uint8_t typeId = value.index();
767 if (!MarshalBasicTypeToBuffer(oss, typeId)) { return false; }
768 switch (typeId) {
769 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_NULL): {
770 continue;
771 }
772 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_INT): {
773 int64_t val = std::get<int64_t>(value);
774 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
775 break;
776 }
777 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_DOUBLE): {
778 double val = std::get<double>(value);
779 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
780 break;
781 }
782 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_STRING): {
783 std::string val = std::get<std::string>(value);
784 if (!MarshalStringToBuffer(oss, val)) { return false; }
785 break;
786 }
787 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_BOOL): {
788 bool val = std::get<bool>(value);
789 if (!MarshalBasicTypeToBuffer(oss, val)) { return false; }
790 break;
791 }
792 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_BLOB): {
793 std::vector<uint8_t> val = std::get<std::vector<uint8_t>>(value);
794 if (!MarshalBasicTypeVecToBuffer(oss, val)) { return false; }
795 break;
796 }
797 default:
798 LOG_ERROR("MarshalValuesBucketToBuffer: unknown typeId");
799 return false;
800 }
801 }
802 return oss.good();
803 }
804
MarshalValuesBucketVecToBuffer(std::ostringstream & oss,const std::vector<DataShareValuesBucket> & values)805 bool MarshalValuesBucketVecToBuffer(std::ostringstream &oss, const std::vector<DataShareValuesBucket> &values)
806 {
807 size_t size = values.size();
808 if (!MarshalBasicTypeToBuffer(oss, size)) {
809 return false;
810 }
811 for (const auto &bucket : values) {
812 size_t mapSize = bucket.valuesMap.size();
813 if (!MarshalBasicTypeToBuffer(oss, mapSize)) {
814 return false;
815 }
816 if (!MarshalValuesBucketToBuffer(oss, bucket)) {
817 return false;
818 }
819 }
820 return oss.good();
821 }
822
823 template <typename T>
UnmarshalBasicTypeToBuffer(std::istringstream & iss,T & value)824 bool UnmarshalBasicTypeToBuffer(std::istringstream &iss, T &value)
825 {
826 iss.read(reinterpret_cast<char *>(&value), sizeof(value));
827 return iss.good();
828 }
829
830 template <typename T>
UnmarshalBasicTypeVecToBuffer(std::istringstream & iss,std::vector<T> & values)831 bool UnmarshalBasicTypeVecToBuffer(std::istringstream &iss, std::vector<T> &values)
832 {
833 size_t valSize = 0;
834 if (!UnmarshalBasicTypeToBuffer(iss, valSize)) {
835 return false;
836 }
837 if (valSize > 0) {
838 values.resize(valSize);
839 iss.read(reinterpret_cast<char *>(values.data()), valSize * sizeof(T));
840 }
841 return iss.good();
842 }
843
UnmarshalStringToBuffer(std::istringstream & iss,std::string & value)844 bool UnmarshalStringToBuffer(std::istringstream &iss, std::string &value)
845 {
846 // Get string length
847 size_t len;
848 if (!UnmarshalBasicTypeToBuffer(iss, len)) {
849 return false;
850 }
851 // Get string content
852 if (len > 0) {
853 value.resize(len, '\0');
854 iss.read(value.data(), len);
855 }
856 return iss.good();
857 }
858
UnmarshalStringVecToBuffer(std::istringstream & iss,std::vector<std::string> & values)859 bool UnmarshalStringVecToBuffer(std::istringstream &iss, std::vector<std::string> &values)
860 {
861 // Get vec length
862 size_t len;
863 if (!UnmarshalBasicTypeToBuffer(iss, len)) {
864 return false;
865 }
866 for (size_t i = 0; i < len; i++) {
867 std::string value;
868 if (!UnmarshalStringToBuffer(iss, value)) {
869 return false;
870 }
871 values.push_back(value);
872 }
873 return iss.good();
874 }
875
UnmarshalSingleTypeToBuffer(std::istringstream & iss,SingleValue::Type & value)876 bool UnmarshalSingleTypeToBuffer(std::istringstream &iss, SingleValue::Type &value)
877 {
878 // Get type of value
879 uint8_t typeId;
880 if (!UnmarshalBasicTypeToBuffer(iss, typeId)) {
881 return false;
882 }
883 // Deserialize according to the type
884 switch (typeId) {
885 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_NULL): {
886 return iss.good();
887 }
888 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_INT): {
889 int intVal;
890 if (!UnmarshalBasicTypeToBuffer(iss, intVal)) { return false; }
891 value = intVal;
892 break;
893 }
894 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_DOUBLE): {
895 double doubleVal;
896 if (!UnmarshalBasicTypeToBuffer(iss, doubleVal)) { return false; }
897 value = doubleVal;
898 break;
899 }
900 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_STRING): {
901 std::string str;
902 if (!UnmarshalStringToBuffer(iss, str)) { return false; }
903 value = str;
904 break;
905 }
906 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_BOOL): {
907 bool val;
908 if (!UnmarshalBasicTypeToBuffer(iss, val)) { return false; }
909 value = val;
910 break;
911 }
912 case static_cast<uint8_t>(DataSharePredicatesObjectType::TYPE_LONG): {
913 int64_t longVal;
914 if (!UnmarshalBasicTypeToBuffer(iss, longVal)) { return false; }
915 value = longVal;
916 break;
917 }
918 default:
919 LOG_ERROR("UnmarshalSingleTypeToBuffer: unknown typeId");
920 return false;
921 }
922 return iss.good();
923 }
924
UnmarshalSingleTypeVecToBuffer(std::istringstream & iss,std::vector<SingleValue::Type> & values)925 bool UnmarshalSingleTypeVecToBuffer(std::istringstream &iss, std::vector<SingleValue::Type> &values)
926 {
927 // Get vec length
928 size_t len;
929 if (!UnmarshalBasicTypeToBuffer(iss, len)) {
930 return false;
931 }
932 for (size_t i = 0; i < len; i++) {
933 SingleValue::Type value;
934 if (!UnmarshalSingleTypeToBuffer(iss, value)) {
935 return false;
936 }
937 values.push_back(value);
938 }
939 return iss.good();
940 }
941
UnmarshalMultiTypeToBuffer(std::istringstream & iss,MutliValue::Type & value)942 bool UnmarshalMultiTypeToBuffer(std::istringstream &iss, MutliValue::Type &value)
943 {
944 // Get type of value
945 uint8_t typeId;
946 if (!UnmarshalBasicTypeToBuffer(iss, typeId)) {
947 return false;
948 }
949 // add offset of TYPE_NULL
950 typeId += static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_NULL);
951 switch (typeId) {
952 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_NULL): {
953 return iss.good();
954 }
955 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_INT_VECTOR): {
956 std::vector<int> intVector;
957 if (!UnmarshalBasicTypeVecToBuffer(iss, intVector)) { return false; }
958 value = intVector;
959 break;
960 }
961 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_LONG_VECTOR): {
962 std::vector<int64_t> longVector;
963 if (!UnmarshalBasicTypeVecToBuffer(iss, longVector)) { return false; }
964 value = longVector;
965 break;
966 }
967 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_DOUBLE_VECTOR): {
968 std::vector<double> doubleVector;
969 if (!UnmarshalBasicTypeVecToBuffer(iss, doubleVector)) { return false; }
970 value = doubleVector;
971 break;
972 }
973 case static_cast<uint8_t>(DataSharePredicatesObjectsType::TYPE_STRING_VECTOR): {
974 std::vector<std::string> strVector;
975 if (!UnmarshalStringVecToBuffer(iss, strVector)) { return false; }
976 value = strVector;
977 break;
978 }
979 default:
980 LOG_ERROR("UnmarshalMultiTypeToBuffer: unknown typeId");
981 return false;
982 }
983 return iss.good();
984 }
985
UnmarshalMultiTypeVecToBuffer(std::istringstream & iss,std::vector<MutliValue::Type> & values)986 bool UnmarshalMultiTypeVecToBuffer(std::istringstream &iss, std::vector<MutliValue::Type> &values)
987 {
988 size_t len;
989 if (!UnmarshalBasicTypeToBuffer(iss, len)) {
990 return false;
991 }
992 for (size_t i = 0; i < len; i++) {
993 MutliValue::Type typ;
994 if (!UnmarshalMultiTypeToBuffer(iss, typ)) {
995 return false;
996 }
997 values.push_back(typ);
998 }
999 return iss.good();
1000 }
1001
UnmarshalOperationItemToBuffer(std::istringstream & iss,OperationItem & value)1002 bool UnmarshalOperationItemToBuffer(std::istringstream &iss, OperationItem &value)
1003 {
1004 // Deserialize operation
1005 if (!UnmarshalBasicTypeToBuffer(iss, value.operation)) {
1006 return false;
1007 }
1008 // Deserialize singleParams
1009 if (!UnmarshalSingleTypeVecToBuffer(iss, value.singleParams)) {
1010 return false;
1011 }
1012 // Deserialize multiParams
1013 if (!UnmarshalMultiTypeVecToBuffer(iss, value.multiParams)) {
1014 return false;
1015 }
1016
1017 return iss.good();
1018 }
1019
UnmarshalOperationItemVecToBuffer(std::istringstream & iss,std::vector<OperationItem> & values)1020 bool UnmarshalOperationItemVecToBuffer(std::istringstream &iss, std::vector<OperationItem> &values)
1021 {
1022 size_t len;
1023 if (!UnmarshalBasicTypeToBuffer(iss, len)) {
1024 return false;
1025 }
1026 for (size_t i = 0; i < len; i++) {
1027 OperationItem item;
1028 if (!UnmarshalOperationItemToBuffer(iss, item)) {
1029 return false;
1030 }
1031 values.push_back(item);
1032 }
1033 return iss.good();
1034 }
1035
UnmarshalPredicatesToBuffer(std::istringstream & iss,DataSharePredicates & predicates)1036 bool UnmarshalPredicatesToBuffer(std::istringstream &iss, DataSharePredicates &predicates)
1037 {
1038 std::vector<OperationItem> operations = {};
1039 std::string whereClause = "";
1040 std::vector<std::string> whereArgs = {};
1041 std::string order = "";
1042 short mode = 0;
1043
1044 // Deserialize operations
1045 if (!UnmarshalOperationItemVecToBuffer(iss, operations)) {
1046 return false;
1047 }
1048 // Deserialize whereClause
1049 if (!UnmarshalStringToBuffer(iss, whereClause)) {
1050 return false;
1051 }
1052 // Deserialize whereArgs
1053 if (!UnmarshalStringVecToBuffer(iss, whereArgs)) {
1054 return false;
1055 }
1056 // Deserialize order
1057 if (!UnmarshalStringToBuffer(iss, order)) {
1058 return false;
1059 }
1060 // Deserialize mode
1061 if (!UnmarshalBasicTypeToBuffer(iss, mode)) {
1062 return false;
1063 }
1064
1065 predicates.SetOperationList(operations);
1066 predicates.SetWhereClause(whereClause);
1067 predicates.SetWhereArgs(whereArgs);
1068 predicates.SetOrder(order);
1069 predicates.SetSettingMode(mode);
1070
1071 return iss.good();
1072 }
1073
UnmarshalValuesMapToBuffer(std::istringstream & iss,std::map<std::string,DataShareValueObject::Type> & valuesMap)1074 bool UnmarshalValuesMapToBuffer(std::istringstream &iss,
1075 std::map<std::string, DataShareValueObject::Type> &valuesMap)
1076 {
1077 std::string key;
1078 UnmarshalStringToBuffer(iss, key);
1079 uint8_t typeId;
1080 UnmarshalBasicTypeToBuffer(iss, typeId);
1081 DataShareValueObject::Type value;
1082 switch (typeId) {
1083 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_NULL): { return iss.good(); }
1084 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_INT): {
1085 int64_t val;
1086 UnmarshalBasicTypeToBuffer(iss, val);
1087 value = val;
1088 break;
1089 }
1090 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_DOUBLE): {
1091 double val;
1092 UnmarshalBasicTypeToBuffer(iss, val);
1093 value = val;
1094 break;
1095 }
1096 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_STRING): {
1097 std::string val;
1098 UnmarshalStringToBuffer(iss, val);
1099 value = val;
1100 break;
1101 }
1102 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_BOOL): {
1103 bool val;
1104 UnmarshalBasicTypeToBuffer(iss, val);
1105 value = val;
1106 break;
1107 }
1108 case static_cast<uint8_t>(DataShareValueObjectType::TYPE_BLOB): {
1109 std::vector<uint8_t> val;
1110 UnmarshalBasicTypeVecToBuffer(iss, val);
1111 value = val;
1112 break;
1113 }
1114 default:
1115 LOG_ERROR("UnmarshalValuesMapToBuffer: unknown typeId");
1116 return false;
1117 }
1118 valuesMap.insert(std::make_pair(key, value));
1119 return iss.good();
1120 }
1121
UnmarshalValuesBucketVecToBuffer(std::istringstream & iss,std::vector<DataShareValuesBucket> & values)1122 bool UnmarshalValuesBucketVecToBuffer(std::istringstream &iss, std::vector<DataShareValuesBucket> &values)
1123 {
1124 size_t size;
1125 if (!UnmarshalBasicTypeToBuffer(iss, size)) { return false; }
1126 for (size_t i = 0; i < size; i++) {
1127 size_t mapSize;
1128 if (!UnmarshalBasicTypeToBuffer(iss, mapSize)) { return false; }
1129 std::map<std::string, DataShareValueObject::Type> valuesMap;
1130 for (size_t j = 0; j < mapSize; j++) {
1131 if (!UnmarshalValuesMapToBuffer(iss, valuesMap)) {
1132 return false;
1133 }
1134 }
1135 DataShareValuesBucket value(valuesMap);
1136 values.push_back(value);
1137 }
1138 return iss.good();
1139 }
1140
MarshalPredicates(const Predicates & predicates,MessageParcel & parcel)1141 bool MarshalPredicates(const Predicates &predicates, MessageParcel &parcel)
1142 {
1143 std::ostringstream oss;
1144 if (!MarshalPredicatesToBuffer(oss, predicates)) {
1145 LOG_ERROR("MarshalPredicatesToBuffer failed.");
1146 return false;
1147 }
1148 std::string str = oss.str();
1149 size_t size = str.length();
1150 if (size > MAX_IPC_SIZE) {
1151 LOG_ERROR("Size of predicates is too large.");
1152 return false;
1153 }
1154 if (!parcel.WriteInt32(size)) {
1155 LOG_ERROR("Write size failed.");
1156 return false;
1157 }
1158 return parcel.WriteRawData(reinterpret_cast<const void *>(str.data()), size);
1159 }
1160
UnmarshalPredicates(Predicates & predicates,MessageParcel & parcel)1161 bool UnmarshalPredicates(Predicates &predicates, MessageParcel &parcel)
1162 {
1163 int32_t length = parcel.ReadInt32();
1164 if (length < 1) {
1165 LOG_ERROR("Length of predicates is invalid.");
1166 return false;
1167 }
1168 const char *buffer = reinterpret_cast<const char *>(parcel.ReadRawData(static_cast<size_t>(length)));
1169 if (buffer == nullptr) {
1170 LOG_ERROR("ReadRawData failed.");
1171 return false;
1172 }
1173 std::istringstream iss(std::string(buffer, length));
1174 return UnmarshalPredicatesToBuffer(iss, predicates);
1175 }
1176
MarshalValuesBucketVec(const std::vector<DataShareValuesBucket> & values,MessageParcel & parcel)1177 bool MarshalValuesBucketVec(const std::vector<DataShareValuesBucket> &values, MessageParcel &parcel)
1178 {
1179 std::ostringstream oss;
1180 if (!MarshalValuesBucketVecToBuffer(oss, values)) {
1181 LOG_ERROR("MarshalValuesBucketVecToBuffer failed.");
1182 return false;
1183 }
1184 std::string str = oss.str();
1185 size_t size = str.length();
1186 if (size > MAX_IPC_SIZE) {
1187 LOG_ERROR("Size of ValuesBucketVec is too large.");
1188 return false;
1189 }
1190 if (!parcel.WriteInt32(size)) {
1191 LOG_ERROR("Write size failed.");
1192 return false;
1193 }
1194 return parcel.WriteRawData(reinterpret_cast<const void *>(str.data()), size);
1195 }
1196
UnmarshalValuesBucketVec(std::vector<DataShareValuesBucket> & values,MessageParcel & parcel)1197 bool UnmarshalValuesBucketVec(std::vector<DataShareValuesBucket> &values, MessageParcel &parcel)
1198 {
1199 int32_t length = parcel.ReadInt32();
1200 if (length < 1) {
1201 LOG_ERROR("Length of ValuesBucketVec is invalid.");
1202 return false;
1203 }
1204 const char *buffer = reinterpret_cast<const char *>(parcel.ReadRawData(static_cast<size_t>(length)));
1205 if (buffer == nullptr) {
1206 LOG_ERROR("ReadRawData failed.");
1207 return false;
1208 }
1209 std::istringstream iss(std::string(buffer, length));
1210 return UnmarshalValuesBucketVecToBuffer(iss, values);
1211 }
1212
1213 template<>
Marshalling(const RegisterOption & option,MessageParcel & parcel)1214 bool Marshalling(const RegisterOption &option, MessageParcel &parcel)
1215 {
1216 return ITypesUtil::Marshal(parcel, option.isReconnect);
1217 }
1218
1219 template<>
Unmarshalling(RegisterOption & option,MessageParcel & parcel)1220 bool Unmarshalling(RegisterOption &option, MessageParcel &parcel)
1221 {
1222 return ITypesUtil::Unmarshal(parcel, option.isReconnect);
1223 }
1224
1225 } // namespace OHOS::ITypesUtil