From 35adefaa9d1ecf5e8c09a787dbd6bda1d1a49325 Mon Sep 17 00:00:00 2001 From: Peter LaFosse Date: Wed, 5 Apr 2023 13:54:44 -0400 Subject: Add APIs for dumping the contents of TypeLibraries --- binaryninjaapi.h | 17 ++++++++++++++++- binaryninjacore.h | 2 ++ python/typelibrary.py | 21 +++++++++++++++++++++ typelibrary.cpp | 15 +++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 5f2987bf..1a3dcb53 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -14907,10 +14907,25 @@ namespace BinaryNinja { */ TypeLibrary(Ref arch, const std::string& name); + /*! Decompresses a type library from a file + + \param path + \return The string contents of the decompressed type library + */ + std::string Decompress(const std::string& path); + + /*! Decompresses a type library from a file + + \param path + \param output + \return True if the type library was successfully decompressed + */ + static bool DecompressToFile(const std::string& path, const std::string& output); + /*! Loads a finalized type library instance from file \param path - \return + \return True if the type library was successfully loaded */ static Ref LoadFromFile(const std::string& path); diff --git a/binaryninjacore.h b/binaryninjacore.h index 3b9fb33f..afdaa6cb 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -5212,6 +5212,8 @@ extern "C" BINARYNINJACOREAPI BNTypeLibrary* BNNewTypeLibraryReference(BNTypeLibrary* lib); BINARYNINJACOREAPI BNTypeLibrary* BNDuplicateTypeLibrary(BNTypeLibrary* lib); BINARYNINJACOREAPI BNTypeLibrary* BNLoadTypeLibraryFromFile(const char* path); + BINARYNINJACOREAPI bool BNTypeLibraryDecompressToFile(const char* file, const char* output); + BINARYNINJACOREAPI char* BNTypeLibraryDecompressToString(const char* file); BINARYNINJACOREAPI void BNFreeTypeLibrary(BNTypeLibrary* lib); BINARYNINJACOREAPI BNTypeLibrary* BNLookupTypeLibraryByName(BNArchitecture* arch, const char* name); diff --git a/python/typelibrary.py b/python/typelibrary.py index 63e7c33b..adc3919d 100644 --- a/python/typelibrary.py +++ b/python/typelibrary.py @@ -54,6 +54,27 @@ class TypeLibrary: """ return TypeLibrary(core.BNNewTypeLibrary(arch.handle, name)) + @staticmethod + def decompress_to_file(path: str, output: str) -> bool: + """ + Decompresses a type library file to a file on disk. + + :param str path: + :param str output: + :rtype: bool + """ + return core.BNTypeLibraryDecompressToFile(path, output) + + @staticmethod + def decompress(path: str) -> str: + """ + Decompresses a type library file to a string. + + :param str path: + :rtype: str + """ + return core.BNTypeLibraryDecompressToString(path) + @staticmethod def load_from_file(path: str) -> Optional['TypeLibrary']: """ diff --git a/typelibrary.cpp b/typelibrary.cpp index 4a794805..5c81705f 100644 --- a/typelibrary.cpp +++ b/typelibrary.cpp @@ -14,6 +14,21 @@ TypeLibrary::TypeLibrary(Ref arch, const std::string& name) } +bool TypeLibrary::DecompressToFile(const std::string& path, const std::string& output) +{ + return BNTypeLibraryDecompressToFile(path.c_str(), output.c_str()); +} + + +std::string TypeLibrary::Decompress(const std::string& path) +{ + auto str = BNTypeLibraryDecompressToString(path.c_str()); + std::string result = str; + BNFreeString(str); + return result; +} + + Ref TypeLibrary::LoadFromFile(const std::string& path) { return new TypeLibrary(BNLoadTypeLibraryFromFile(path.c_str())); -- cgit v1.3.1