From 07ad9ed7725a99a70aa8ce1045e81bacb8b86491 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 27 Jul 2021 22:46:37 +0200 Subject: [PATCH] tools: Added UNIX permissions calculator --- .../builtin/source/content/tools_entries.cpp | 60 +++++++++++++++++++ plugins/builtin/source/lang/de_DE.cpp | 6 ++ plugins/builtin/source/lang/en_US.cpp | 6 ++ plugins/builtin/source/lang/it_IT.cpp | 6 ++ source/providers/file_provider.cpp | 10 ++-- 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 665be9970..168b256b0 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -340,6 +340,65 @@ namespace hex::plugin::builtin { } + void drawPermissionsCalculator() { + static bool setuid, setgid, sticky; + static bool r[3], w[3], x[3]; + + ImGui::TextUnformatted("hex.builtin.tools.permissions.perm_bits"_lang); + ImGui::Separator(); + + if (ImGui::BeginTable("Permissions", 4, ImGuiTableFlags_Borders)) { + ImGui::TableSetupScrollFreeze(0, 1); + ImGui::TableSetupColumn("Special", ImGuiTableColumnFlags_NoSort); + ImGui::TableSetupColumn("User", ImGuiTableColumnFlags_NoSort); + ImGui::TableSetupColumn("Group", ImGuiTableColumnFlags_NoSort); + ImGui::TableSetupColumn("Other", ImGuiTableColumnFlags_NoSort); + + ImGui::TableHeadersRow(); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + + ImGui::Checkbox("setuid", &setuid); + ImGui::Checkbox("setgid", &setgid); + ImGui::Checkbox("Sticky bit", &sticky); + + for (u8 i = 0; i < 3; i++) { + ImGui::TableNextColumn(); + + ImGui::PushID(i); + ImGui::Checkbox("Read", &r[i]); + ImGui::Checkbox("Write", &w[i]); + ImGui::Checkbox("Execute", &x[i]); + ImGui::PopID(); + } + + ImGui::EndTable(); + } + + ImGui::NewLine(); + ImGui::TextUnformatted("hex.builtin.tools.permissions.absolute"_lang); + ImGui::Separator(); + + auto result = hex::format("{}{}{}{}", + (setuid << 2) | (setgid << 1) | (sticky << 0), + (r[0] << 2) | (w[0] << 1) | (x[0] << 0), + (r[1] << 2) | (w[1] << 1) | (x[1] << 0), + (r[2] << 2) | (w[2] << 1) | (x[2] << 0)); + ImGui::InputText("##permissions_absolute", result.data(), result.size(), ImGuiInputTextFlags_ReadOnly); + + ImGui::NewLine(); + + static const auto WarningColor = ImColor(0.92F, 0.25F, 0.2F, 1.0F); + if (setuid && !x[0]) + ImGui::TextColored(WarningColor, "hex.builtin.tools.permissions.setuid_error"_lang); + if (setgid && !x[1]) + ImGui::TextColored(WarningColor, "hex.builtin.tools.permissions.setgid_error"_lang); + if (sticky && !x[2]) + ImGui::TextColored(WarningColor, "hex.builtin.tools.permissions.sticky_error"_lang); + + } + void registerToolEntries() { ContentRegistry::Tools::add("hex.builtin.tools.demangler", drawDemangler); ContentRegistry::Tools::add("hex.builtin.tools.ascii_table", drawASCIITable); @@ -347,6 +406,7 @@ namespace hex::plugin::builtin { ContentRegistry::Tools::add("hex.builtin.tools.color", drawColorPicker); ContentRegistry::Tools::add("hex.builtin.tools.calc", drawMathEvaluator); ContentRegistry::Tools::add("hex.builtin.tools.base_converter", drawBaseConverter); + ContentRegistry::Tools::add("hex.builtin.tools.permissions", drawPermissionsCalculator); } } \ No newline at end of file diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index f0b399c15..63675d60e 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -518,6 +518,12 @@ namespace hex::plugin::builtin { { "hex.builtin.tools.base_converter.hex", "HEX" }, { "hex.builtin.tools.base_converter.oct", "OCT" }, { "hex.builtin.tools.base_converter.bin", "BIN" }, + { "hex.builtin.tools.permissions", "UNIX Berechtigungsrechner" }, + { "hex.builtin.tools.permissions.perm_bits", "Berechtigungs bits" }, + { "hex.builtin.tools.permissions.absolute", "Absolute Notation" }, + { "hex.builtin.tools.permissions.setuid_error", "User benötigt execute Rechte, damit setuid bit gilt!" }, + { "hex.builtin.tools.permissions.setgid_error", "Group benötigt execute Rechte, damit setgid bit gilt!" }, + { "hex.builtin.tools.permissions.sticky_error", "Other benötigt execute Rechte, damit sticky bit gilt!" }, { "hex.builtin.setting.imhex", "ImHex" }, { "hex.builtin.setting.imhex.recent_files", "Kürzlich geöffnete Dateien" }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 254c9aad0..726b4fd47 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -518,6 +518,12 @@ namespace hex::plugin::builtin { { "hex.builtin.tools.base_converter.hex", "HEX" }, { "hex.builtin.tools.base_converter.oct", "OCT" }, { "hex.builtin.tools.base_converter.bin", "BIN" }, + { "hex.builtin.tools.permissions", "UNIX Permissions Calculator" }, + { "hex.builtin.tools.permissions.perm_bits", "Permission bits" }, + { "hex.builtin.tools.permissions.absolute", "Absolute Notation" }, + { "hex.builtin.tools.permissions.setuid_error", "User must have execute rights for setuid bit to apply!" }, + { "hex.builtin.tools.permissions.setgid_error", "Group must have execute rights for setgid bit to apply!" }, + { "hex.builtin.tools.permissions.sticky_error", "Other must have execute rights for sticky bit to apply!" }, { "hex.builtin.setting.imhex", "ImHex" }, { "hex.builtin.setting.imhex.recent_files", "Recent Files" }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 14b8cc769..cd9d6e258 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -518,6 +518,12 @@ namespace hex::plugin::builtin { { "hex.builtin.tools.base_converter.hex", "HEX" }, { "hex.builtin.tools.base_converter.oct", "OCT" }, { "hex.builtin.tools.base_converter.bin", "BIN" }, + //{ "hex.builtin.tools.permissions", "UNIX Permissions Calculator" }, + //{ "hex.builtin.tools.permissions.perm_bits", "Permission bits" }, + //{ "hex.builtin.tools.permissions.absolute", "Absolute Notation" }, + //{ "hex.builtin.tools.permissions.setuid_error", "User must have execute rights for setuid bit to apply!" }, + //{ "hex.builtin.tools.permissions.setgid_error", "Group must have execute rights for setgid bit to apply!" }, + //{ "hex.builtin.tools.permissions.sticky_error", "Other must have execute rights for sticky bit to apply!" }, { "hex.builtin.setting.imhex", "ImHex" }, { "hex.builtin.setting.imhex.recent_files", "File recenti" }, diff --git a/source/providers/file_provider.cpp b/source/providers/file_provider.cpp index 433b3e8eb..48a1c1f6b 100644 --- a/source/providers/file_provider.cpp +++ b/source/providers/file_provider.cpp @@ -83,7 +83,7 @@ namespace hex::prv { } void FileProvider::resize(ssize_t newSize) { - close(); + this->close(); #if defined(OS_WINDOWS) std::wstring widePath; @@ -106,12 +106,12 @@ namespace hex::prv { #else auto handle = ::open(this->m_path.data(), 0644); - ftruncate(handle, newSize - 1); + ::ftruncate(handle, newSize - 1); - close(handle); + ::close(handle); #endif - open(); + this->open(); } size_t FileProvider::getActualSize() { @@ -198,7 +198,7 @@ namespace hex::prv { #else this->m_file = ::open(this->m_path.data(), O_RDWR); if (this->m_file == -1) { - this->m_file = ::open(path.data(), O_RDONLY); + this->m_file = ::open(this->m_path.data(), O_RDONLY); this->m_writable = false; }