1 /*
2 * Copyright (C) 2023 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 "parcel_helper.h"
17
18 #include "log.h"
19
20 namespace OHOS {
21 namespace Request {
UnMarshal(MessageParcel & data,TaskInfo & info)22 void ParcelHelper::UnMarshal(MessageParcel &data, TaskInfo &info)
23 {
24 UnMarshalBase(data, info);
25 if (!UnMarshalFormItem(data, info)) {
26 return;
27 }
28 if (!UnMarshalFileSpec(data, info)) {
29 return;
30 }
31 UnMarshalProgress(data, info);
32 if (!UnMarshalMapProgressExtras(data, info)) {
33 return;
34 }
35 if (!UnMarshalMapExtras(data, info)) {
36 return;
37 }
38 info.version = static_cast<Version>(data.ReadUint32());
39 if (!UnMarshalTaskState(data, info)) {
40 return;
41 }
42 }
43
UnMarshalBase(MessageParcel & data,TaskInfo & info)44 void ParcelHelper::UnMarshalBase(MessageParcel &data, TaskInfo &info)
45 {
46 info.gauge = data.ReadBool();
47 info.retry = data.ReadBool();
48 info.action = static_cast<Action>(data.ReadUint32());
49 info.mode = static_cast<Mode>(data.ReadUint32());
50 info.code = static_cast<Reason>(data.ReadUint32());
51 info.tries = data.ReadUint32();
52 info.uid = data.ReadString();
53 info.bundle = data.ReadString();
54 info.url = data.ReadString();
55 info.tid = data.ReadString();
56 info.title = data.ReadString();
57 info.mimeType = data.ReadString();
58 info.ctime = data.ReadUint64();
59 info.mtime = data.ReadUint64();
60 info.data = data.ReadString();
61 info.description = data.ReadString();
62 }
63
UnMarshalFormItem(MessageParcel & data,TaskInfo & info)64 bool ParcelHelper::UnMarshalFormItem(MessageParcel &data, TaskInfo &info)
65 {
66 uint32_t size = data.ReadUint32();
67 if (size > data.GetReadableBytes()) {
68 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
69 return false;
70 }
71 for (uint32_t i = 0; i < size; i++) {
72 FormItem form;
73 form.name = data.ReadString();
74 form.value = data.ReadString();
75 info.forms.push_back(form);
76 }
77 return true;
78 }
79
UnMarshalFileSpec(MessageParcel & data,TaskInfo & info)80 bool ParcelHelper::UnMarshalFileSpec(MessageParcel &data, TaskInfo &info)
81 {
82 uint32_t size = data.ReadUint32();
83 if (size > data.GetReadableBytes()) {
84 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
85 return false;
86 }
87 for (uint32_t i = 0; i < size; i++) {
88 FileSpec file;
89 file.name = data.ReadString();
90 file.uri = data.ReadString();
91 file.filename = data.ReadString();
92 file.type = data.ReadString();
93 info.files.push_back(file);
94 }
95 return true;
96 }
97
UnMarshalProgress(MessageParcel & data,TaskInfo & info)98 void ParcelHelper::UnMarshalProgress(MessageParcel &data, TaskInfo &info)
99 {
100 info.progress.state = static_cast<State>(data.ReadUint32());
101 info.progress.index = data.ReadUint32();
102 info.progress.processed = data.ReadUint64();
103 info.progress.totalProcessed = data.ReadUint64();
104 data.ReadInt64Vector(&info.progress.sizes);
105 }
106
UnMarshalMapProgressExtras(MessageParcel & data,TaskInfo & info)107 bool ParcelHelper::UnMarshalMapProgressExtras(MessageParcel &data, TaskInfo &info)
108 {
109 uint32_t size = data.ReadUint32();
110 if (size > data.GetReadableBytes()) {
111 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
112 return false;
113 }
114 for (uint32_t i = 0; i < size; i++) {
115 std::string key = data.ReadString();
116 info.progress.extras[key] = data.ReadString();
117 }
118 return true;
119 }
120
UnMarshalMapExtras(MessageParcel & data,TaskInfo & info)121 bool ParcelHelper::UnMarshalMapExtras(MessageParcel &data, TaskInfo &info)
122 {
123 uint32_t size = data.ReadUint32();
124 if (size > data.GetReadableBytes()) {
125 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
126 return false;
127 }
128 for (uint32_t i = 0; i < size; i++) {
129 std::string key = data.ReadString();
130 info.extras[key] = data.ReadString();
131 }
132 return true;
133 }
134
UnMarshalTaskState(MessageParcel & data,TaskInfo & info)135 bool ParcelHelper::UnMarshalTaskState(MessageParcel &data, TaskInfo &info)
136 {
137 uint32_t size = data.ReadUint32();
138 if (size > data.GetReadableBytes()) {
139 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
140 return false;
141 }
142 for (uint32_t i = 0; i < size; i++) {
143 TaskState taskState;
144 taskState.path = data.ReadString();
145 taskState.responseCode = data.ReadUint32();
146 taskState.message = data.ReadString();
147 info.taskStates.push_back(taskState);
148 }
149 return true;
150 }
151 } // namespace Request
152 } // namespace OHOS