]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
15b96ee50776eb5b7a40185b3edaa7f5a029cff3
[proxmark3-svn] / client / lualibs / utils.lua
1 --[[
2 This may be moved to a separate library at some point (Holiman)
3 --]]
4 local Utils =
5 {
6 -- Asks the user for Yes or No
7 confirm = function(message, ...)
8 local answer
9 message = message .. " [y/n] ?"
10 repeat
11 io.write(message)
12 io.flush()
13 answer=io.read()
14 if answer == 'Y' or answer == "y" then
15 return true
16 elseif answer == 'N' or answer == 'n' then
17 return false
18 end
19 until false
20 end,
21 ---
22 -- Asks the user for input
23 input = function (message , default)
24 local answer
25 if default ~= nil then
26 message = message .. " (default: ".. default.. " )"
27 end
28 message = message .." \n > "
29 io.write(message)
30 io.flush()
31 answer=io.read()
32 if answer == '' then answer = default end
33
34 return answer
35 end,
36 --
37 -- Converts DECIMAL to HEX
38 ConvertDec2Hex = function(IN)
39 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
40 while IN>0 do
41 I=I+1
42 IN,D=math.floor(IN/B),math.mod(IN,B)+1
43 OUT=string.sub(K,D,D)..OUT
44 end
45 return OUT
46 end,
47 ---
48 -- Convert Byte array to string of hex
49 ConvertBytes2HexString = function(bytes)
50 if #bytes == 0 then
51 return ''
52 end
53 local s={}
54 for i = 1, #(bytes) do
55 s[i] = string.format("%02X",bytes[i])
56 end
57 return table.concat(s)
58 end,
59 -- Convert byte array to string with ascii
60 ConvertBytesToAsciiString = function(bytes)
61 if #bytes == 0 then
62 return ''
63 end
64 local s={}
65 for i = 1, #(bytes) do
66 s[i] = string.char(bytes[i])
67 end
68 return table.concat(s)
69 end,
70 ConvertHexStringToBytes = function(s)
71 local t={}
72 if s == nil then return t end
73 if #s == 0 then return t end
74 for k in s:gmatch"(%x%x)" do
75 table.insert(t,tonumber(k,16))
76 end
77 return t
78 end,
79 ConvertAsciiStringToBytes = function(s)
80 local t={}
81 if s == nil then return t end
82 if #s == 0 then return t end
83
84 for k in s:gmatch"(.)" do
85 table.insert(t, string.byte(k))
86 end
87 return t
88 end,
89 ConvertHexToAscii = function(s)
90 local t={}
91 if s == nil then return t end
92 if #s == 0 then return t end
93 for k in s:gmatch"(%x%x)" do
94 table.insert(t, string.char(tonumber(k,16)))
95 end
96 return table.concat(t)
97 end,
98
99 -- function convertStringToBytes(str)
100 -- local bytes = {}
101 -- local strLength = string.len(str)
102 -- for i=1,strLength do
103 -- table.insert(bytes, string.byte(str, i))
104 -- end
105
106 -- return bytes
107 -- end
108
109 -- function convertBytesToString(bytes)
110 -- local bytesLength = table.getn(bytes)
111 -- local str = ""
112 -- for i=1,bytesLength do
113 -- str = str .. string.char(bytes[i])
114 -- end
115
116 -- return str
117 -- end
118
119 -- function convertHexStringToBytes(str)
120 -- local bytes = {}
121 -- local strLength = string.len(str)
122 -- for k=2,strLength,2 do
123 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
124 -- table.insert(bytes, hex.to_dec(hexString))
125 -- end
126
127 -- return bytes
128 -- end
129
130 -- function convertBytesToHexString(bytes)
131 -- local str = ""
132 -- local bytesLength = table.getn(bytes)
133 -- for i=1,bytesLength do
134 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
135 -- if string.len(hexString) == 1 then
136 -- hexString = "0" .. hexString
137 -- end
138 -- str = str .. hexString
139 -- end
140
141 -- return str
142 -- end
143
144 }
145 return Utils
Impressum, Datenschutz