|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# CodeHawk Binary Analyzer |
| 3 | +# Author: Henny Sipma |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2025 Aarno Labs LLC |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in all |
| 17 | +# copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +# SOFTWARE. |
| 26 | +# ------------------------------------------------------------------------------ |
| 27 | + |
| 28 | +import os |
| 29 | +import subprocess |
| 30 | +import sys |
| 31 | + |
| 32 | +from typing import List |
| 33 | + |
| 34 | +from chb.util.Config import Config |
| 35 | + |
| 36 | + |
| 37 | +class ASTCParseManager: |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + pass |
| 41 | + |
| 42 | + def check_cparser(self) -> bool: |
| 43 | + return os.path.isfile(Config().cparser) |
| 44 | + |
| 45 | + def preprocess_file_with_gcc( |
| 46 | + self, cfilename: str, moreoptions: List[str] = []) -> str: |
| 47 | + |
| 48 | + ifilename = cfilename[:-1] + "i" |
| 49 | + cmd = [ |
| 50 | + "gcc", |
| 51 | + "-fno-inline", |
| 52 | + "-fno-builtin", |
| 53 | + "-E", |
| 54 | + "-g", |
| 55 | + "-o", |
| 56 | + ifilename, |
| 57 | + cfilename] |
| 58 | + cmd = cmd[:1] + moreoptions + cmd[1:] |
| 59 | + |
| 60 | + subprocess.call( |
| 61 | + cmd, |
| 62 | + cwd=os.getcwd(), |
| 63 | + stdout=open(os.devnull, "w"), |
| 64 | + stderr=subprocess.STDOUT, |
| 65 | + ) |
| 66 | + return ifilename |
| 67 | + |
| 68 | + def parse_ifile(self, ifilename: str) -> int: |
| 69 | + cwd = os.getcwd() |
| 70 | + ifilename = os.path.join(cwd, ifilename) |
| 71 | + cmd = [Config().cparser, "-projectpath", cwd, "-targetdirectory", cwd] |
| 72 | + cmd.append(ifilename) |
| 73 | + p = subprocess.call(cmd, stderr=subprocess.STDOUT) |
| 74 | + sys.stdout.flush() |
| 75 | + return p |
| 76 | + |
0 commit comments