1 /*
2  * Copyright (c) 2021 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 #include "faultlog_info_ohos.h"
16 
17 #include "hiview_logger.h"
18 #include "parcel.h"
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 DEFINE_LOG_LABEL(0xD002D11, "FaultLogInfoOhos");
Marshalling(Parcel & parcel) const23 bool FaultLogInfoOhos::Marshalling(Parcel& parcel) const
24 {
25     if (!parcel.WriteInt64(time) || !parcel.WriteInt32(uid) ||
26         !parcel.WriteInt32(pid) || !parcel.WriteInt32(faultLogType)) {
27         HIVIEW_LOGE("Parcel failed to write int number.");
28         return false;
29     }
30     if (!parcel.WriteString(module)) {
31         HIVIEW_LOGE("Parcel failed to write module name(%{public}s).", module.c_str());
32         return false;
33     }
34     if (!parcel.WriteString(reason)) {
35         HIVIEW_LOGE("Parcel failed to write reason(%{public}s).", reason.c_str());
36         return false;
37     }
38     if (!parcel.WriteString(summary)) {
39         HIVIEW_LOGE("Parcel failed to write summary(%{public}s).", summary.c_str());
40         return false;
41     }
42     if (!parcel.WriteString(logPath)) {
43         HIVIEW_LOGE("Parcel failed to write log path(%{public}s).", logPath.c_str());
44         return false;
45     }
46     if (!parcel.WriteString(registers)) {
47         HIVIEW_LOGE("Parcel failed to write registers(%{public}s).", registers.c_str());
48         return false;
49     }
50     uint32_t size = sectionMaps.size();
51     if (!parcel.WriteUint32(size)) {
52         return false;
53     }
54     for (const auto& [key, value] : sectionMaps) {
55         if (!parcel.WriteString(key)) {
56             HIVIEW_LOGE("Parcel failed to write key of sectionMaps(%{public}s).", key.c_str());
57             return false;
58         }
59         if (!parcel.WriteString(value)) {
60             HIVIEW_LOGE("Parcel failed to write value of sectionMaps(%{public}s).", value.c_str());
61             return false;
62         }
63     }
64     return true;
65 }
66 
Unmarshalling(Parcel & parcel)67 FaultLogInfoOhos* FaultLogInfoOhos::Unmarshalling(Parcel& parcel)
68 {
69     const uint32_t maxSize = 128;
70     uint32_t size;
71     uint32_t i;
72     FaultLogInfoOhos* ret = new FaultLogInfoOhos();
73     if (!parcel.ReadInt64(ret->time)) {
74         goto error;
75     }
76 
77     if (!parcel.ReadInt32(ret->uid)) {
78         goto error;
79     }
80 
81     if (!parcel.ReadInt32(ret->pid)) {
82         goto error;
83     }
84 
85     if (!parcel.ReadInt32(ret->faultLogType)) {
86         goto error;
87     }
88 
89     if (!parcel.ReadString(ret->module)) {
90         HIVIEW_LOGE("Parcel failed to read module(%{public}s).", ret->module.c_str());
91         goto error;
92     }
93 
94     if (!parcel.ReadString(ret->reason)) {
95         HIVIEW_LOGE("Parcel failed to read reason(%{public}s).", ret->reason.c_str());
96         goto error;
97     }
98 
99     if (!parcel.ReadString(ret->summary)) {
100         HIVIEW_LOGE("Parcel failed to read summary(%{public}s).", ret->summary.c_str());
101         goto error;
102     }
103 
104     if (!parcel.ReadString(ret->logPath)) {
105         HIVIEW_LOGE("Parcel failed to read log path(%{public}s).", ret->logPath.c_str());
106         goto error;
107     }
108 
109     if (!parcel.ReadString(ret->registers)) {
110         HIVIEW_LOGE("Parcel failed to read registers(%{public}s).", ret->registers.c_str());
111         goto error;
112     }
113 
114     if (!parcel.ReadUint32(size)) {
115         goto error;
116     }
117 
118     if (size > maxSize) {
119         goto error;
120     }
121 
122     for (i = 0; i < size; i++) {
123         std::string key;
124         std::string value;
125         if (!parcel.ReadString(key)) {
126             HIVIEW_LOGE("Parcel failed to read key of sectionMaps(%{public}s).", key.c_str());
127             goto error;
128         }
129 
130         if (!parcel.ReadString(value)) {
131             HIVIEW_LOGE("Parcel failed to read value of sectionMaps(%{public}s).", value.c_str());
132             goto error;
133         }
134 
135         ret->sectionMaps[key] = value;
136     }
137     return ret;
138 
139 error:
140     delete ret;
141     ret = nullptr;
142     return nullptr;
143 }
144 }  // namespace hiview
145 }  // namespace OHOS
146