1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef HC_GEN_DECOMPILE_H
10 #define HC_GEN_DECOMPILE_H
11 
12 #include <fstream>
13 #include <string>
14 
15 #include "ast.h"
16 
17 namespace OHOS {
18 namespace Hardware {
19 class Decompile {
20 public:
21     explicit Decompile(const std::string &fileName);
22 
23     ~Decompile() = default;
24 
25     bool DoDecompile();
26 
27 private:
28     bool InitDecompileFile();
29 
30     bool ReadFile(char *buffer, size_t readSize);
31 
GetAlignSize(uint32_t size)32     uint32_t GetAlignSize(uint32_t size) const
33     {
34         if (isAlign_) {
35             return (size + ALIGN_SIZE - 1) & (~(ALIGN_SIZE - 1));
36         } else {
37             return size;
38         }
39     }
40 
41     bool ReadUint8(uint8_t &value);
42 
43     bool ReadUint16(uint16_t &value);
44 
45     bool ReadUint32(uint32_t &value);
46 
47     bool ReadUint64(uint64_t &value);
48 
49     bool ReadString(std::string &value);
50 
51     void SetAlign(bool isAlign);
52 
53     bool VerifyDecompileFile();
54 
55     bool GetNextByteCode(uint32_t &byteCode);
56 
57     std::shared_ptr<AstObject> RebuildObject(uint8_t opCode);
58 
59     std::shared_ptr<AstObject> RebuildNode();
60 
61     std::shared_ptr<AstObject> RebuildTerm();
62 
63     std::shared_ptr<AstObject> RebuildNodeRefObject();
64 
65     std::shared_ptr<AstObject> RebuildNumberObject(uint8_t opCode);
66 
67     std::shared_ptr<AstObject> RebuildArray();
68 
69     std::shared_ptr<AstObject> RebuildStringObject();
70 
71     std::shared_ptr<Ast> RebuildAst();
72 
73     bool isAlign_;
74     std::string fileName_;
75     std::ifstream file_;
76 };
77 } // namespace Hardware
78 } // namespace OHOS
79 #endif // HC_GEN_DECOMPILE_H
80