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
16 #define MLOG_TAG "Media_Cloud_Vo"
17
18 #include "cloud_file_data_vo.h"
19
20 #include <sstream>
21
22 #include "media_itypes_utils.h"
23 #include "media_log.h"
24
25 namespace OHOS::Media::CloudSync {
Unmarshalling(MessageParcel & parcel)26 bool CloudFileDataVo::Unmarshalling(MessageParcel &parcel)
27 {
28 parcel.ReadString(this->fileName);
29 parcel.ReadString(this->filePath);
30 parcel.ReadInt64(this->size);
31 return true;
32 }
Marshalling(MessageParcel & parcel) const33 bool CloudFileDataVo::Marshalling(MessageParcel &parcel) const
34 {
35 parcel.WriteString(this->fileName);
36 parcel.WriteString(this->filePath);
37 parcel.WriteInt64(this->size);
38 return true;
39 }
40
Marshalling(const std::map<std::string,CloudFileDataVo> & result,MessageParcel & parcel)41 bool CloudFileDataVo::Marshalling(const std::map<std::string, CloudFileDataVo> &result, MessageParcel &parcel)
42 {
43 CHECK_AND_RETURN_RET(parcel.WriteInt32(static_cast<int32_t>(result.size())), false);
44 for (const auto &entry : result) {
45 if (!parcel.WriteString(entry.first) || !entry.second.Marshalling(parcel)) {
46 return false;
47 }
48 }
49 return true;
50 }
Unmarshalling(std::map<std::string,CloudFileDataVo> & val,MessageParcel & parcel)51 bool CloudFileDataVo::Unmarshalling(std::map<std::string, CloudFileDataVo> &val, MessageParcel &parcel)
52 {
53 int32_t size = 0;
54 CHECK_AND_RETURN_RET(parcel.ReadInt32(size), false);
55 CHECK_AND_RETURN_RET(size >= 0, false);
56 size_t readAbleSize = parcel.GetReadableBytes();
57 if ((static_cast<size_t>(size) > readAbleSize) || static_cast<size_t>(size) > val.max_size()) {
58 return false;
59 }
60 bool isValid;
61 for (int32_t i = 0; i < size; i++) {
62 std::string key;
63 CHECK_AND_RETURN_RET(parcel.ReadString(key), false);
64 CloudFileDataVo nodeObj;
65 isValid = nodeObj.Unmarshalling(parcel);
66 CHECK_AND_RETURN_RET(isValid, false);
67 val.emplace(key, nodeObj);
68 }
69 return true;
70 }
ToString() const71 std::string CloudFileDataVo::ToString() const
72 {
73 std::stringstream ss;
74 ss << "{"
75 << "\"size\": " << this->size << "}";
76 return ss.str();
77 }
78 } // namespace OHOS::Media::CloudSync