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 "hiview_file_info.h"
17
18 #include "hiview_logger.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace HiviewDFX {
23 DEFINE_LOG_TAG("LOGLIBRARY_FILE_INFO");
GetData(uint32_t & dataSize) const24 void* HiviewFileInfo::GetData(uint32_t& dataSize) const
25 {
26 Parcel outParcel;
27 if (!outParcel.WriteString(name)) {
28 return nullptr;
29 }
30 if (!outParcel.WriteInt64(mtime)) {
31 return nullptr;
32 }
33 if (!outParcel.WriteUint64(size)) {
34 return nullptr;
35 }
36 size_t size = outParcel.GetDataSize();
37 const size_t maxSize = 1024;
38 if (size == 0 || size > maxSize) {
39 HIVIEW_LOGE("invalid size.");
40 return nullptr;
41 }
42 void* data = malloc(size);
43 if (data == nullptr) {
44 HIVIEW_LOGE("invalid data.");
45 return nullptr;
46 }
47 memset_s(data, size, 0, size);
48 if (memcpy_s(data, size, reinterpret_cast<void*>(outParcel.GetData()), size) != 0) {
49 HIVIEW_LOGE("copy failed.");
50 free(data);
51 return nullptr;
52 }
53 dataSize = size;
54 return data;
55 }
56
ParseData(const char * data,const uint32_t dataSize)57 HiviewFileInfo HiviewFileInfo::ParseData(const char* data, const uint32_t dataSize)
58 {
59 if (data == nullptr) {
60 HIVIEW_LOGE("param invalid.");
61 return HiviewFileInfo("", 0, 0);
62 }
63 Parcel inParcel;
64 inParcel.WriteBuffer(data, dataSize);
65 std::string name;
66 if (!inParcel.ReadString(name)) {
67 return HiviewFileInfo("", 0, 0);
68 }
69 time_t mtime;
70 if (!inParcel.ReadInt64(mtime)) {
71 return HiviewFileInfo("", 0, 0);
72 }
73 uint64_t size;
74 if (!inParcel.ReadUint64(size)) {
75 return HiviewFileInfo("", 0, 0);
76 }
77 return HiviewFileInfo(name, mtime, size);
78 }
79 } // namespace HiviewDFX
80 } // namespace OHOS