• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef RAWHEAP_TRANSLATE_UTILS_H
17 #define RAWHEAP_TRANSLATE_UTILS_H
18 
19 #include <iostream>
20 #include <iomanip>
21 #include <functional>
22 #include <cstdint>
23 #include <fstream>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include <memory>
27 #include <string>
28 #include <securec.h>
29 #include <sstream>
30 
31 namespace rawheap_translate {
32 #define LOG_INFO_ Logger(0) << std::left << std::setw(24) << __func__
33 #define LOG_ERROR_ Logger(1) << std::left << std::setw(24) << __func__
34 #ifdef PATH_MAX
35 #undef PATH_MAX
36 #endif
37 #define PATH_MAX 4096
38 #define MAX_FILE_SIZE (4 * 1024 * 1024 * 1024ULL) // 4 * 1024 * 1024 * 1024 : file size bigger than 4GB
39 #define MAX_OBJ_SIZE (MAX_FILE_SIZE >> 1)
40 
41 bool RealPath(const std::string &filename, std::string &realpath);
42 
43 bool GenerateDumpFileName(std::string &filename);
44 
45 bool EndsWith(const std::string &str, const std::string &suffix);
46 
47 bool IsLittleEndian();
48 
49 uint16_t ByteToU16(char *data);
50 
51 uint32_t ByteToU32(char *data);
52 
53 uint64_t ByteToU64(char *data);
54 
55 void ByteToU32Array(char *data, uint32_t *array, uint32_t size);
56 
57 void ByteToU64Array(char *data, uint64_t *array, uint32_t size);
58 
59 class Logger {
60 public:
Logger(int level)61     Logger(int level) : level_(level) {}
~Logger()62     ~Logger()
63     {
64         switch (level_) {
65             case 1:
66                 // 8: level width format
67                 std::cout << std::left << std::setw(8) << "[ERROR] " << ss.str() << std::endl;
68                 break;
69             default:
70                 // 8: level width format
71                 std::cout << std::left << std::setw(8) << "[INFO ] " << ss.str() << std::endl;
72                 break;
73         }
74     }
75 
76     template<typename T>
77     Logger& operator<<(const T& value)
78     {
79         ss << value;
80         return *this;
81     }
82 
83 private:
84     int level_ {0};
85     std::stringstream ss;
86 };
87 
88 class FileReader {
89 public:
90     FileReader() = default;
91     ~FileReader() = default;
92 
93     bool Initialize(const std::string &path);
94     bool Read(char *buf, uint32_t size);
95     bool Seek(uint32_t offset);
96     bool ReadArray(std::vector<uint32_t> &array, uint32_t size);
97     bool ReadArray(std::vector<uint64_t> &array, uint32_t size);
98     bool CheckAndGetHeaderAt(uint32_t offset, uint32_t assertNum);
99 
GetHeaderLeft()100     uint32_t GetHeaderLeft()
101     {
102         return left_;
103     }
104 
GetHeaderRight()105     uint32_t GetHeaderRight()
106     {
107         return right_;
108     }
109 
GetFileSize()110     uint32_t GetFileSize()
111     {
112         return fileSize_;
113     }
114 
115     static uint32_t GetFileSize(const std::string &path);
116 
117 private:
118     std::ifstream file_;
119     uint32_t left_ {0};
120     uint32_t right_ {0};
121     uint32_t fileSize_ {0};
122 };
123 
124 class Version {
125 public:
Version(int major,int minor,int build)126     Version(int major, int minor, int build) : major_(major), minor_(minor), build_(build) {}
127     Version() = default;
128     ~Version() = default;
129 
130     bool Parse(const std::string &version);
131 
132     bool operator<(const Version &version) const
133     {
134         if (major_ > version.major_) {
135             return false;
136         }
137 
138         if (major_ == version.major_ && minor_ > version.minor_) {
139             return false;
140         }
141 
142         if (major_ == version.major_ && minor_ == version.minor_ && build_ >= version.build_) {
143             return false;
144         }
145 
146         return true;
147     }
148 
ToString()149     std::string ToString() const
150     {
151         return std::to_string(major_) + '.' + std::to_string(minor_) + '.' + std::to_string(build_);
152     }
153 
154 private:
155     int major_ {0};
156     int minor_ {0};
157     int build_ {0};
158 };
159 
160 static const Version VERSION(2, 0, 0);
161 }  // namespace rawheap_translate
162 #endif
163