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 info.priority = data.ReadUint32();
63 if (info.code != Reason::REASON_OK) {
64 info.faults = CommonUtils::GetFaultByReason(info.code);
65 info.reason = CommonUtils::GetMsgByReason(info.code);
66 }
67 }
68
UnMarshalFormItem(MessageParcel & data,TaskInfo & info)69 bool ParcelHelper::UnMarshalFormItem(MessageParcel &data, TaskInfo &info)
70 {
71 uint32_t size = data.ReadUint32();
72 if (size > data.GetReadableBytes()) {
73 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
74 return false;
75 }
76 for (uint32_t i = 0; i < size; i++) {
77 FormItem form;
78 form.name = data.ReadString();
79 form.value = data.ReadString();
80 info.forms.push_back(form);
81 }
82 return true;
83 }
84
UnMarshalFileSpec(MessageParcel & data,TaskInfo & info)85 bool ParcelHelper::UnMarshalFileSpec(MessageParcel &data, TaskInfo &info)
86 {
87 uint32_t size = data.ReadUint32();
88 if (size > data.GetReadableBytes()) {
89 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
90 return false;
91 }
92 for (uint32_t i = 0; i < size; i++) {
93 FileSpec file;
94 file.name = data.ReadString();
95 file.uri = data.ReadString();
96 file.filename = data.ReadString();
97 file.type = data.ReadString();
98 info.files.push_back(file);
99 }
100 return true;
101 }
102
UnMarshalProgress(MessageParcel & data,TaskInfo & info)103 void ParcelHelper::UnMarshalProgress(MessageParcel &data, TaskInfo &info)
104 {
105 info.progress.state = static_cast<State>(data.ReadUint32());
106 info.progress.index = data.ReadUint32();
107 info.progress.processed = data.ReadUint64();
108 info.progress.totalProcessed = data.ReadUint64();
109 data.ReadInt64Vector(&info.progress.sizes);
110 }
111
UnMarshalMapProgressExtras(MessageParcel & data,TaskInfo & info)112 bool ParcelHelper::UnMarshalMapProgressExtras(MessageParcel &data, TaskInfo &info)
113 {
114 uint32_t size = data.ReadUint32();
115 if (size > data.GetReadableBytes()) {
116 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
117 return false;
118 }
119 for (uint32_t i = 0; i < size; i++) {
120 std::string key = data.ReadString();
121 info.progress.extras[key] = data.ReadString();
122 }
123 return true;
124 }
125
UnMarshalMapExtras(MessageParcel & data,TaskInfo & info)126 bool ParcelHelper::UnMarshalMapExtras(MessageParcel &data, TaskInfo &info)
127 {
128 uint32_t size = data.ReadUint32();
129 if (size > data.GetReadableBytes()) {
130 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
131 return false;
132 }
133 for (uint32_t i = 0; i < size; i++) {
134 std::string key = data.ReadString();
135 info.extras[key] = data.ReadString();
136 }
137 return true;
138 }
139
UnMarshalTaskState(MessageParcel & data,TaskInfo & info)140 bool ParcelHelper::UnMarshalTaskState(MessageParcel &data, TaskInfo &info)
141 {
142 uint32_t size = data.ReadUint32();
143 if (size > data.GetReadableBytes()) {
144 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
145 return false;
146 }
147 for (uint32_t i = 0; i < size; i++) {
148 TaskState taskState;
149 taskState.path = data.ReadString();
150 taskState.responseCode = data.ReadUint32();
151 taskState.message = data.ReadString();
152 info.taskStates.push_back(taskState);
153 }
154 return true;
155 }
156
UnMarshalConfig(MessageParcel & data,Config & config)157 void ParcelHelper::UnMarshalConfig(MessageParcel &data, Config &config)
158 {
159 config.action = static_cast<Action>(data.ReadUint32());
160 config.mode = static_cast<Mode>(data.ReadUint32());
161 config.bundleType = data.ReadUint32();
162 config.overwrite = data.ReadBool();
163 config.network = static_cast<Network>(data.ReadUint32());
164 config.metered = data.ReadBool();
165 config.roaming = data.ReadBool();
166 config.retry = data.ReadBool();
167 config.redirect = data.ReadBool();
168 config.index = data.ReadUint32();
169 config.begins = data.ReadInt64();
170 config.ends = data.ReadInt64();
171 config.gauge = data.ReadBool();
172 config.precise = data.ReadBool();
173 config.priority = data.ReadUint32();
174 config.background = data.ReadBool();
175 config.multipart = data.ReadBool();
176 config.bundleName = data.ReadString();
177 config.url = data.ReadString();
178 config.title = data.ReadString();
179 config.description = data.ReadString();
180 config.method = data.ReadString();
181 // read headers
182 if (!UnMarshalConfigHeaders(data, config)) {
183 return;
184 }
185 config.data = data.ReadString();
186 config.token = data.ReadString();
187 // read extras
188 if (!UnMarshalConfigExtras(data, config)) {
189 return;
190 }
191 config.version = static_cast<Version>(data.ReadUint32());
192 // read form_items
193 if (!UnMarshalConfigFormItem(data, config)) {
194 return;
195 }
196 // read file_specs
197 if (!UnMarshalConfigFileSpec(data, config)) {
198 return;
199 }
200 // read body_file_names
201 if (!UnMarshalConfigBodyFileName(data, config)) {
202 return;
203 }
204 // read min speed
205 config.minSpeed.speed = data.ReadInt64();
206 config.minSpeed.duration = data.ReadInt64();
207 }
208
UnMarshalConfigHeaders(MessageParcel & data,Config & config)209 bool ParcelHelper::UnMarshalConfigHeaders(MessageParcel &data, Config &config)
210 {
211 uint32_t headerLen = data.ReadUint32();
212 if (headerLen > data.GetReadableBytes()) {
213 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", headerLen);
214 return false;
215 }
216 for (uint32_t i = 0; i < headerLen; i++) {
217 std::string key = data.ReadString();
218 config.headers[key] = data.ReadString();
219 }
220 return true;
221 }
222
UnMarshalConfigExtras(MessageParcel & data,Config & config)223 bool ParcelHelper::UnMarshalConfigExtras(MessageParcel &data, Config &config)
224 {
225 uint32_t extraLen = data.ReadUint32();
226 if (extraLen > data.GetReadableBytes()) {
227 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", extraLen);
228 return false;
229 }
230 for (uint32_t i = 0; i < extraLen; i++) {
231 std::string key = data.ReadString();
232 config.extras[key] = data.ReadString();
233 }
234 return true;
235 }
236
UnMarshalConfigFormItem(MessageParcel & data,Config & config)237 bool ParcelHelper::UnMarshalConfigFormItem(MessageParcel &data, Config &config)
238 {
239 uint32_t size = data.ReadUint32();
240 if (size > data.GetReadableBytes()) {
241 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
242 return false;
243 }
244 for (uint32_t i = 0; i < size; i++) {
245 FormItem form;
246 form.name = data.ReadString();
247 form.value = data.ReadString();
248 config.forms.push_back(form);
249 }
250 return true;
251 }
252
UnMarshalConfigFileSpec(MessageParcel & data,Config & config)253 bool ParcelHelper::UnMarshalConfigFileSpec(MessageParcel &data, Config &config)
254 {
255 uint32_t size = data.ReadUint32();
256 if (size > data.GetReadableBytes()) {
257 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
258 return false;
259 }
260 for (uint32_t i = 0; i < size; i++) {
261 FileSpec file;
262 file.name = data.ReadString();
263 file.uri = data.ReadString();
264 file.filename = data.ReadString();
265 file.type = data.ReadString();
266 config.files.push_back(file);
267 }
268 return true;
269 }
270
UnMarshalConfigBodyFileName(MessageParcel & data,Config & config)271 bool ParcelHelper::UnMarshalConfigBodyFileName(MessageParcel &data, Config &config)
272 {
273 uint32_t size = data.ReadUint32();
274 if (size > data.GetReadableBytes()) {
275 REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
276 return false;
277 }
278 for (uint32_t i = 0; i < size; i++) {
279 std::string name = data.ReadString();
280 config.bodyFileNames.push_back(name);
281 }
282 return true;
283 }
284 } // namespace Request
285 } // namespace OHOS