| 59 | namespace LIEF::py { |
| 60 | |
| 61 | typing::OutputParser from_stream(std::unique_ptr<BinaryStream> stream) { |
| 62 | if (stream == nullptr) { |
| 63 | return nb::none(); |
| 64 | } |
| 65 | |
| 66 | #if LIEF_PE_SUPPORT |
| 67 | if (LIEF::PE::is_pe(*stream)) { |
| 68 | return nb::cast(LIEF::PE::Parser::parse(std::move(stream))); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #if LIEF_ELF_SUPPORT |
| 73 | if (LIEF::ELF::is_elf(*stream)) { |
| 74 | return nb::cast(LIEF::ELF::Parser::parse(std::move(stream))); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | #if LIEF_MACHO_SUPPORT |
| 79 | if (LIEF::MachO::is_macho(*stream)) { |
| 80 | std::unique_ptr<LIEF::MachO::FatBinary> fat = |
| 81 | LIEF::MachO::Parser::parse(std::move(stream)); |
| 82 | |
| 83 | // NOTE(romain): We could nb::cast the fat binary object but |
| 84 | // to avoid a breaking change in the logic of parse() we return a |
| 85 | // `LIEF::MachO::Binary` as we did since the beginning. |
| 86 | // |
| 87 | // We could introduce this breaking change later though. |
| 88 | if (fat == nullptr || fat->empty()) { |
| 89 | return nb::none(); |
| 90 | } |
| 91 | return nb::cast(fat->take(0)); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | #if LIEF_COFF_SUPPORT |
| 96 | if (LIEF::COFF::is_coff(*stream)) { |
| 97 | return nb::cast(LIEF::COFF::Parser::parse(std::move(stream))); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | return nb::none(); |
| 102 | } |
| 103 | |
| 104 | template<> |
| 105 | void create<Parser>(nb::module_& m) { |