#!/usr/bin/lua --[===[ #(c) 2010 Adam Pridgen adam@praetoriangrp.com, adam.pridgen@thecoverofnight.com # hexlify.lua: # Provides a LUA command script to perform hexlification and unhexlification # # # GPLv3 License # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ]===] local todo = arg[1] local infile = arg[2] local outfile = arg[3] if (todo == nil or infile == nil or outfile == nil) then print ("usage:\n" .. arg[0] .. " [-u] [-h] infile outfile\n") os.exit(-1) end if (todo == "-h") then print ("Performing hexlify\n") local f = io.open(infile, 'rb') local bytes = f:read('*all') f:close() local g = io.open(outfile, 'w') for b in string.gfind(bytes, ".") do g:write(string.format('%02X', string.byte(b))) end g:close() elseif (todo == "-u") then print ("Performing unhexlify\n") local f = io.open(infile, 'r') local bytes = f:read('*all') f:close() local g = io.open(outfile, 'wb') for b in string.gfind(bytes, "..") do g:write(string.char(tonumber(b,16))) end g:close() else print ("Bad todo string:\nusage:" .. arg[0] .. " [-u] [-h] infile outfile\n") os.exit(-1) end print('Done reading ' .. infile .. ' and ' .. outfile .. ' was written!\n') os.exit(0)