2014-08-29 15:09:23 +08:00
|
|
|
--[[
|
|
|
|
This may be moved to a separate library at some point (Holiman)
|
|
|
|
--]]
|
|
|
|
local Utils =
|
|
|
|
{
|
|
|
|
-- Asks the user for Yes or No
|
|
|
|
confirm = function(message, ...)
|
|
|
|
local answer
|
|
|
|
message = message .. " [y/n] ?"
|
|
|
|
repeat
|
|
|
|
io.write(message)
|
|
|
|
io.flush()
|
|
|
|
answer=io.read()
|
|
|
|
if answer == 'Y' or answer == "y" then
|
|
|
|
return true
|
|
|
|
elseif answer == 'N' or answer == 'n' then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
until false
|
|
|
|
end,
|
|
|
|
---
|
|
|
|
-- Asks the user for input
|
|
|
|
input = function (message , default)
|
|
|
|
local answer
|
|
|
|
if default ~= nil then
|
|
|
|
message = message .. " (default: ".. default.. " )"
|
|
|
|
end
|
|
|
|
message = message .." \n > "
|
|
|
|
io.write(message)
|
|
|
|
io.flush()
|
|
|
|
answer=io.read()
|
|
|
|
if answer == '' then answer = default end
|
|
|
|
|
|
|
|
return answer
|
|
|
|
end,
|
|
|
|
--
|
|
|
|
-- Converts DECIMAL to HEX
|
|
|
|
ConvertDec2Hex = function(IN)
|
|
|
|
local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
|
|
|
|
while IN>0 do
|
|
|
|
I=I+1
|
|
|
|
IN,D=math.floor(IN/B),math.mod(IN,B)+1
|
|
|
|
OUT=string.sub(K,D,D)..OUT
|
|
|
|
end
|
|
|
|
return OUT
|
|
|
|
end,
|
|
|
|
---
|
|
|
|
-- Convert Byte array to string of hex
|
2014-11-10 02:29:47 +08:00
|
|
|
ConvertBytes2HexString = function(bytes)
|
|
|
|
if #bytes == 0 then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
local s={}
|
2014-08-29 15:09:23 +08:00
|
|
|
for i = 1, #(bytes) do
|
2014-11-06 02:16:54 +08:00
|
|
|
s[i] = string.format("%02X",bytes[i])
|
2014-08-29 15:09:23 +08:00
|
|
|
end
|
|
|
|
return table.concat(s)
|
|
|
|
end,
|
2014-11-10 02:29:47 +08:00
|
|
|
-- Convert byte array to string with ascii
|
|
|
|
ConvertBytesToAsciiString = function(bytes)
|
|
|
|
if #bytes == 0 then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
local s={}
|
|
|
|
for i = 1, #(bytes) do
|
|
|
|
s[i] = string.char(bytes[i])
|
|
|
|
end
|
|
|
|
return table.concat(s)
|
|
|
|
end,
|
|
|
|
ConvertHexStringToBytes = function(s)
|
2014-11-06 02:16:54 +08:00
|
|
|
local t={}
|
2014-11-10 02:29:47 +08:00
|
|
|
if s == nil then return t end
|
|
|
|
if #s == 0 then return t end
|
2014-11-06 02:16:54 +08:00
|
|
|
for k in s:gmatch"(%x%x)" do
|
|
|
|
table.insert(t,tonumber(k,16))
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end,
|
2014-11-10 02:29:47 +08:00
|
|
|
ConvertAsciiStringToBytes = function(s)
|
|
|
|
local t={}
|
|
|
|
if s == nil then return t end
|
|
|
|
if #s == 0 then return t end
|
|
|
|
|
|
|
|
for k in s:gmatch"(.)" do
|
|
|
|
table.insert(t, string.byte(k))
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end,
|
2014-11-13 06:18:46 +08:00
|
|
|
ConvertHexToAscii = function(s)
|
|
|
|
local t={}
|
|
|
|
if s == nil then return t end
|
|
|
|
if #s == 0 then return t end
|
|
|
|
for k in s:gmatch"(%x%x)" do
|
|
|
|
table.insert(t, string.char(tonumber(k,16)))
|
|
|
|
end
|
|
|
|
return table.concat(t)
|
|
|
|
end,
|
2014-11-06 02:16:54 +08:00
|
|
|
|
|
|
|
-- function convertStringToBytes(str)
|
|
|
|
-- local bytes = {}
|
|
|
|
-- local strLength = string.len(str)
|
|
|
|
-- for i=1,strLength do
|
|
|
|
-- table.insert(bytes, string.byte(str, i))
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- return bytes
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- function convertBytesToString(bytes)
|
|
|
|
-- local bytesLength = table.getn(bytes)
|
|
|
|
-- local str = ""
|
|
|
|
-- for i=1,bytesLength do
|
|
|
|
-- str = str .. string.char(bytes[i])
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- return str
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- function convertHexStringToBytes(str)
|
|
|
|
-- local bytes = {}
|
|
|
|
-- local strLength = string.len(str)
|
|
|
|
-- for k=2,strLength,2 do
|
|
|
|
-- local hexString = "0x" .. string.sub(str, (k - 1), k)
|
|
|
|
-- table.insert(bytes, hex.to_dec(hexString))
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- return bytes
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- function convertBytesToHexString(bytes)
|
|
|
|
-- local str = ""
|
|
|
|
-- local bytesLength = table.getn(bytes)
|
|
|
|
-- for i=1,bytesLength do
|
|
|
|
-- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
|
|
|
|
-- if string.len(hexString) == 1 then
|
|
|
|
-- hexString = "0" .. hexString
|
|
|
|
-- end
|
|
|
|
-- str = str .. hexString
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- return str
|
|
|
|
-- end
|
|
|
|
|
2014-08-29 15:09:23 +08:00
|
|
|
}
|
|
|
|
return Utils
|