]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
FIX: I think the dumping of data is correct now in tnp3.lua. MD5 string vs bytearra...
[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
90 -- function convertStringToBytes(str)
91 -- local bytes = {}
92 -- local strLength = string.len(str)
93 -- for i=1,strLength do
94 -- table.insert(bytes, string.byte(str, i))
95 -- end
96
97 -- return bytes
98 -- end
99
100 -- function convertBytesToString(bytes)
101 -- local bytesLength = table.getn(bytes)
102 -- local str = ""
103 -- for i=1,bytesLength do
104 -- str = str .. string.char(bytes[i])
105 -- end
106
107 -- return str
108 -- end
109
110 -- function convertHexStringToBytes(str)
111 -- local bytes = {}
112 -- local strLength = string.len(str)
113 -- for k=2,strLength,2 do
114 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
115 -- table.insert(bytes, hex.to_dec(hexString))
116 -- end
117
118 -- return bytes
119 -- end
120
121 -- function convertBytesToHexString(bytes)
122 -- local str = ""
123 -- local bytesLength = table.getn(bytes)
124 -- for i=1,bytesLength do
125 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
126 -- if string.len(hexString) == 1 then
127 -- hexString = "0" .. hexString
128 -- end
129 -- str = str .. hexString
130 -- end
131
132 -- return str
133 -- end
134
135 }
136 return Utils
Impressum, Datenschutz