| 228 | |
| 229 | |
| 230 | int main(int argc, char* argv[]) |
| 231 | { |
| 232 | if (argc < 4) |
| 233 | { |
| 234 | fprintf(stderr, "Usage: generator <header> <output> <output_enum>\n"); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | // Parse API header to get type and function information |
| 239 | map<QualifiedName, Ref<Type>> types, vars, funcs; |
| 240 | string errors; |
| 241 | auto arch = new CoreArchitecture(BNGetNativeTypeParserArchitecture()); |
| 242 | |
| 243 | // Enable ephemeral settings |
| 244 | Settings::Instance()->LoadSettingsFile(""); |
| 245 | Settings::Instance()->Set("analysis.types.parserName", "ClangTypeParser"); |
| 246 | bool ok = arch->GetStandalonePlatform()->ParseTypesFromSourceFile(argv[1], types, vars, funcs, errors); |
| 247 | |
| 248 | if (!ok) { |
| 249 | fprintf(stderr, "Errors: %s\n", errors.c_str()); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | FILE* out = fopen(argv[2], "w"); |
| 254 | FILE* enums = fopen(argv[3], "w"); |
| 255 | |
| 256 | fprintf(out, "import ctypes, os\n\n"); |
| 257 | fprintf(out, "from typing import Optional, AnyStr\n"); |
| 258 | fprintf(out, "from .enums import *"); |
| 259 | |
| 260 | fprintf(enums, "import enum\n"); |
| 261 | |
| 262 | fprintf(out, "# Load core module\n"); |
| 263 | fprintf(out, "import platform\n"); |
| 264 | fprintf(out, "core = None\n"); |
| 265 | fprintf(out, "_base_path = None\n"); |
| 266 | fprintf(out, "core_platform = platform.system()\n"); |
| 267 | fprintf(out, "if core_platform == \"Darwin\":\n"); |
| 268 | fprintf(out, "\t_base_path = os.path.join(os.path.dirname(__file__), \"..\", \"..\", \"..\", \"MacOS\")\n"); |
| 269 | fprintf(out, "\tcore = ctypes.CDLL(os.path.join(_base_path, \"libbinaryninjacore.dylib\"))\n\n"); |
| 270 | fprintf(out, "elif core_platform == \"Linux\":\n"); |
| 271 | fprintf(out, "\t_base_path = os.path.join(os.path.dirname(__file__), \"..\", \"..\")\n"); |
| 272 | fprintf(out, "\tcore = ctypes.CDLL(os.path.join(_base_path, \"libbinaryninjacore.so.1\"))\n\n"); |
| 273 | fprintf(out, "elif (core_platform == \"Windows\") or (core_platform.find(\"CYGWIN_NT\") == 0):\n"); |
| 274 | fprintf(out, "\t_base_path = os.path.join(os.path.dirname(__file__), \"..\", \"..\")\n"); |
| 275 | fprintf(out, "\tcore = ctypes.CDLL(os.path.join(_base_path, \"binaryninjacore.dll\"))\n"); |
| 276 | fprintf(out, "else:\n"); |
| 277 | fprintf(out, "\traise Exception(\"OS not supported\")\n\n\n"); |
| 278 | |
| 279 | fprintf(out, "def cstr(var: Optional[AnyStr]) -> Optional[bytes]:\n"); |
| 280 | fprintf(out, " if var is None:\n"); |
| 281 | fprintf(out, " return None\n"); |
| 282 | fprintf(out, " if isinstance(var, bytes):\n"); |
| 283 | fprintf(out, " return var\n"); |
| 284 | fprintf(out, " return var.encode(\"utf-8\")\n\n\n"); |
| 285 | |
| 286 | fprintf(out, "def pyNativeStr(arg: AnyStr) -> str:\n"); |
| 287 | fprintf(out, " if isinstance(arg, str):\n"); |
nothing calls this directly
no test coverage detected