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