diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index 3995eae24..15af7e3f4 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -105,8 +105,13 @@ namespace hex::magic { ON_SCOPE_EXIT { magic_close(ctx); }; if (magic_load(ctx, magicFiles->c_str()) == 0) { - if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr) - return wolv::util::replaceStrings(result, "\\012-", "\n-"); + if (auto description = magic_buffer(ctx, data.data(), data.size()); description != nullptr) { + auto result = wolv::util::replaceStrings(description, "\\012-", "\n-"); + if (result.ends_with("- data")) + result = result.substr(0, result.size() - 6); + + return result; + } } } @@ -130,8 +135,13 @@ namespace hex::magic { ON_SCOPE_EXIT { magic_close(ctx); }; if (magic_load(ctx, magicFiles->c_str()) == 0) { - if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr) - return wolv::util::replaceStrings(result, "\\012-", "\n-"); + if (auto mimeType = magic_buffer(ctx, data.data(), data.size()); mimeType != nullptr) { + auto result = wolv::util::replaceStrings(mimeType, "\\012-", "\n-"); + if (result.ends_with("- application/octet-stream")) + result = result.substr(0, result.size() - 26); + + return result; + } } } @@ -162,8 +172,13 @@ namespace hex::magic { ON_SCOPE_EXIT { magic_close(ctx); }; if (magic_load(ctx, magicFiles->c_str()) == 0) { - if (auto result = magic_buffer(ctx, data.data(), data.size()); result != nullptr) - return wolv::util::replaceStrings(result, "\\012-", "\n-"); + if (auto extension = magic_buffer(ctx, data.data(), data.size()); extension != nullptr) { + auto result = wolv::util::replaceStrings(extension, "\\012-", "\n-"); + if (result.ends_with("- ???")) + result = result.substr(0, result.size() - 5); + + return result; + } } } diff --git a/plugins/builtin/source/content/data_information_sections.cpp b/plugins/builtin/source/content/data_information_sections.cpp index 108ccdd37..04dc7ec2e 100644 --- a/plugins/builtin/source/content/data_information_sections.cpp +++ b/plugins/builtin/source/content/data_information_sections.cpp @@ -7,8 +7,12 @@ #include #include +#include + namespace hex::plugin::builtin { + using namespace wolv::literals; + class InformationProvider : public ContentRegistry::DataInformation::InformationSection { public: InformationProvider() : InformationSection("hex.builtin.information_section.provider_information") { } @@ -64,10 +68,22 @@ namespace hex::plugin::builtin { task.update(); - m_dataDescription = magic::getDescription(provider, region.getStartAddress()); - m_dataMimeType = magic::getMIMEType(provider, region.getStartAddress()); - m_dataAppleCreatorType = magic::getAppleCreatorType(provider, region.getStartAddress()); - m_dataExtensions = magic::getExtensions(provider, region.getStartAddress()); + try { + std::vector data(region.getSize()); + provider->read(region.getStartAddress(), data.data(), data.size()); + + m_dataDescription = magic::getDescription(data); + m_dataMimeType = magic::getMIMEType(data); + m_dataAppleCreatorType = magic::getAppleCreatorType(data); + m_dataExtensions = magic::getExtensions(data); + } catch (const std::bad_alloc &) { + hex::log::error("Failed to allocate enough memory for full file magic analysis!"); + + // Retry analysis with only the first 100 KiB + if (region.getSize() != 100_kiB) { + process(task, provider, { region.getStartAddress(), 100_kiB }); + } + } } void drawContent() override {