• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.overwrite = data.ReadBool();
158     config.network = static_cast<Network>(data.ReadUint32());
159     config.metered = data.ReadBool();
160     config.roaming = data.ReadBool();
161     config.retry = data.ReadBool();
162     config.redirect = data.ReadBool();
163     config.index = data.ReadUint32();
164     config.begins = data.ReadInt64();
165     config.ends = data.ReadInt64();
166     config.gauge = data.ReadBool();
167     config.precise = data.ReadBool();
168     config.priority = data.ReadUint32();
169     config.background = data.ReadBool();
170     config.bundleName = data.ReadString();
171     config.url = data.ReadString();
172     config.title = data.ReadString();
173     config.description = data.ReadString();
174     config.method = data.ReadString();
175     // read headers
176     if (!UnMarshalConfigHeaders(data, config)) {
177         return;
178     }
179     config.data = data.ReadString();
180     config.token = data.ReadString();
181     // read extras
182     if (!UnMarshalConfigExtras(data, config)) {
183         return;
184     }
185     config.version = static_cast<Version>(data.ReadUint32());
186     // read form_items
187     if (!UnMarshalConfigFormItem(data, config)) {
188         return;
189     }
190     // read file_specs
191     if (!UnMarshalConfigFileSpec(data, config)) {
192         return;
193     }
194     // read body_file_names
195     if (!UnMarshalConfigBodyFileName(data, config)) {
196         return;
197     }
198 }
199 
UnMarshalConfigHeaders(MessageParcel & data,Config & config)200 bool ParcelHelper::UnMarshalConfigHeaders(MessageParcel &data, Config &config)
201 {
202     uint32_t headerLen = data.ReadUint32();
203     if (headerLen > data.GetReadableBytes()) {
204         REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", headerLen);
205         return false;
206     }
207     for (uint32_t i = 0; i < headerLen; i++) {
208         std::string key = data.ReadString();
209         config.headers[key] = data.ReadString();
210     }
211     return true;
212 }
213 
UnMarshalConfigExtras(MessageParcel & data,Config & config)214 bool ParcelHelper::UnMarshalConfigExtras(MessageParcel &data, Config &config)
215 {
216     uint32_t extraLen = data.ReadUint32();
217     if (extraLen > data.GetReadableBytes()) {
218         REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", extraLen);
219         return false;
220     }
221     for (uint32_t i = 0; i < extraLen; i++) {
222         std::string key = data.ReadString();
223         config.extras[key] = data.ReadString();
224     }
225     return true;
226 }
227 
UnMarshalConfigFormItem(MessageParcel & data,Config & config)228 bool ParcelHelper::UnMarshalConfigFormItem(MessageParcel &data, Config &config)
229 {
230     uint32_t size = data.ReadUint32();
231     if (size > data.GetReadableBytes()) {
232         REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
233         return false;
234     }
235     for (uint32_t i = 0; i < size; i++) {
236         FormItem form;
237         form.name = data.ReadString();
238         form.value = data.ReadString();
239         config.forms.push_back(form);
240     }
241     return true;
242 }
243 
UnMarshalConfigFileSpec(MessageParcel & data,Config & config)244 bool ParcelHelper::UnMarshalConfigFileSpec(MessageParcel &data, Config &config)
245 {
246     uint32_t size = data.ReadUint32();
247     if (size > data.GetReadableBytes()) {
248         REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
249         return false;
250     }
251     for (uint32_t i = 0; i < size; i++) {
252         FileSpec file;
253         file.name = data.ReadString();
254         file.uri = data.ReadString();
255         file.filename = data.ReadString();
256         file.type = data.ReadString();
257         config.files.push_back(file);
258     }
259     return true;
260 }
261 
UnMarshalConfigBodyFileName(MessageParcel & data,Config & config)262 bool ParcelHelper::UnMarshalConfigBodyFileName(MessageParcel &data, Config &config)
263 {
264     uint32_t size = data.ReadUint32();
265     if (size > data.GetReadableBytes()) {
266         REQUEST_HILOGE("Size exceeds the upper limit, size = %{public}u", size);
267         return false;
268     }
269     for (uint32_t i = 0; i < size; i++) {
270         std::string name = data.ReadString();
271         config.bodyFds.push_back(0);
272         config.bodyFileNames.push_back(name);
273     }
274     return true;
275 }
276 } // namespace Request
277 } // namespace OHOS