From: Vsevolod Stakhov Date: Tue, 21 Dec 2021 20:21:06 +0000 (+0000) Subject: [Minor] Add unhex utility X-Git-Tag: 3.2~147 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dd2b7b4c24e34d77382ab7213d21546d8f3d93e7;p=thirdparty%2Frspamd.git [Minor] Add unhex utility --- diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 75d218da35..4b0e501929 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -1473,4 +1473,19 @@ exports.shuffle = function(tbl) return tbl end +-- +local hex_table = {} +for idx = 0, 255 do + hex_table[("%02X"):format(idx)] = string.char(idx) + hex_table[("%02x"):format(idx)] = string.char(idx) +end + +---[[[ +-- @function lua_util.unhex(str) +-- Decode hex encoded string +-- @param {string} str string to decode +-- @return {string} hex decoded string (valid hex pairs are decoded, everything else is printed as is) +--]]] +exports.unhex = function(str) return str:gsub('(..)', hex_table) end + return exports