Skip to content

Commit

Permalink
revert: encode/decode to working stage
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAppSec committed Sep 7, 2024
1 parent 57c92b5 commit 981cf73
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 74 deletions.
79 changes: 34 additions & 45 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -381,61 +381,50 @@ M.octal_decode = function(text)
end

M.transform_func = function(text, selection_type, encode_or_decode, encoding_type)
-- Helper function for invalid operation
local function invalid_operation()
vim.notify("Hacker Helper: Invalid operation for " .. encoding_type, vim.log.levels.ERROR)
return text
end

-- Encoding functions
if encode_or_decode == "encode" then
if encoding_type == "base64" then
if encoding_type == "base64" then
if encode_or_decode == "encode" then
return M.base64_encode(text)
elseif encoding_type == "url" then
return M.url_encode(text)
elseif encoding_type == "html" then
return M.html_encode(text)
elseif encoding_type == "ascii_hex" then
return M.ascii_hex_encode(text)
elseif encoding_type == "gzip" then
return M.gzip_encode(text)
elseif encoding_type == "binary" then
return M.binary_encode(text)
elseif encoding_type == "octal" then
return M.octal_encode(text)
else
return invalid_operation()
end

-- Decoding functions
elseif encode_or_decode == "decode" then
if encoding_type == "base64" then
elseif encode_or_decode == "decode" then
return M.base64_decode(text)
elseif encoding_type == "url" then
end
elseif encoding_type == "url" then
if encode_or_decode == "encode" then
return M.url_encode(text)
elseif encode_or_decode == "decode" then
return M.url_decode(text)
elseif encoding_type == "html" then
end
elseif encoding_type == "html" then
if encode_or_decode == "encode" then
return M.html_encode(text)
elseif encode_or_decode == "decode" then
return M.html_decode(text)
elseif encoding_type == "ascii_hex" then
end
elseif encoding_type == "ascii_hex" then
if encode_or_decode == "encode" then
return M.ascii_hex_encode(text)
elseif encode_or_decode == "decode" then
return M.ascii_hex_decode(text)
elseif encoding_type == "gzip" then
end
elseif encoding_type == "gzip" then
if encode_or_decode == "encode" then
return M.gzip_encode(text)
elseif encode_or_decode == "decode" then
return M.gzip_decode(text)
elseif encoding_type == "binary" then
end
elseif encoding_type == "binary" then
if encode_or_decode == "encode" then
return M.binary_encode(text)
elseif encode_or_decode == "decode" then
return M.binary_decode(text)
elseif encoding_type == "octal" then
end
elseif encoding_type == "octal" then
if encode_or_decode == "encode" then
return M.octal_encode(text)
elseif encode_or_decode == "decode" then
return M.octal_decode(text)
else
return invalid_operation()
end

-- Hashing functions
elseif encode_or_decode == "hash" then
-- Use the M.hash_text function for hashing algorithms
return M.hash_text(text, encoding_type)

-- If an unsupported encode_or_decode operation is requested
else
return invalid_operation()
end
return text
end

M.hash_text = function(text, algorithm)
Expand Down
42 changes: 13 additions & 29 deletions lua/hacker-helper/selection_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

local M = {}

M.transform_selection = function(transform_func, mode, encoding_type)
-- Default to "decode" if mode is not provided (backward compatibility)
mode = mode or "decode"

-- Utility function to capture the visual selection, apply a transformation, and replace the selection
M.transform_selection = function(transform_func, encode_or_decode, encoding_type)
-- Reselect the current visual block to ensure the latest selection is active
vim.cmd("normal! gv")

Expand Down Expand Up @@ -36,20 +34,16 @@ M.transform_selection = function(transform_func, mode, encoding_type)
vim.notify("Full lines selected: " .. vim.inspect(lines), vim.log.levels.INFO)
-- Apply transformation for full lines
for i, line in ipairs(lines) do
lines[i] = transform_func(line, "full_line", mode, encoding_type)
end

-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, lines)
else
vim.fn.setline(start_line, lines) -- Default: Replace lines for encoding/decoding
lines[i] = transform_func(line, "full_line", encode_or_decode, encoding_type)
end
-- Replace the selected lines with the transformed text
vim.fn.setline(start_line, lines)
else
-- Inline selection (v mode)
if start_line == end_line then
-- Handle inline selection on a single line
local line = lines[1] or ""
-- Ensure start_col and end_col are valid
start_col = math.max(0, start_col)
end_col = math.min(#line, end_col)

Expand All @@ -58,17 +52,11 @@ M.transform_selection = function(transform_func, mode, encoding_type)
vim.notify("Selected part of the line: " .. selection, vim.log.levels.INFO)

-- Transform the selected part
local transformed = transform_func(selection or "", "specific_selection", mode, encoding_type)
local transformed = transform_func(selection or "", "specific_selection", encode_or_decode, encoding_type)

-- Replace the selected part with the transformed text
local new_line = string.sub(line, 1, start_col) .. transformed .. string.sub(line, end_col + 1)

-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, transformed)
else
vim.fn.setline(start_line, new_line)
end
vim.fn.setline(start_line, new_line)
else
-- Handle multi-line partial selection
local first_line = string.sub(lines[1] or "", start_col + 1)
Expand All @@ -78,21 +66,17 @@ M.transform_selection = function(transform_func, mode, encoding_type)

-- Transform first and last lines
lines[1] = string.sub(lines[1] or "", 1, start_col)
.. transform_func(first_line, "multi_line", mode, encoding_type)
lines[#lines] = transform_func(last_line, "multi_line", mode, encoding_type)
.. transform_func(first_line, "multi_line", encode_or_decode, encoding_type)
lines[#lines] = transform_func(last_line, "multi_line", encode_or_decode, encoding_type)
.. string.sub(lines[#lines] or "", end_col + 1)

-- Transform middle lines
for i = 2, #lines - 1 do
lines[i] = transform_func(lines[i], "multi_line", mode, encoding_type)
lines[i] = transform_func(lines[i], "multi_line", encode_or_decode, encoding_type)
end

-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, lines)
else
vim.fn.setline(start_line, lines) -- Default: Replace lines for encoding/decoding
end
-- Replace the selected lines with the transformed text
vim.fn.setline(start_line, lines)
end
end

Expand Down

0 comments on commit 981cf73

Please sign in to comment.