]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[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 ------------ FILE READING
38 ReadDumpFile = function (filename)
39
40 filename = filename or 'dumpdata.bin'
41 if #filename == 0 then
42 return nil, 'Filename length is zero'
43 end
44
45 infile = io.open(filename, "rb")
46 if infile == nil then
47 return nil, string.format("Could not read file %s",filename)
48 end
49 local t = infile:read("*all")
50 len = string.len(t)
51 local _,hex = bin.unpack(("H%d"):format(len),t)
52 io.close(infile)
53 return hex
54 end,
55
56 ------------ string split function
57 Split = function( inSplitPattern, outResults )
58 if not outResults then
59 outResults = {}
60 end
61 local start = 1
62 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
63 while splitStart do
64 table.insert( outResults, string.sub( self, start, splitStart-1 ) )
65 start = splitEnd + 1
66 splitStart, splitEnd = string.find( self, inSplitPattern, start )
67 end
68 table.insert( outResults, string.sub( self, start ) )
69 return outResults
70 end,
71
72
73 ------------ CRC-16 ccitt checksums
74 -- Takes a hex string and calculates a crc16
75 Crc16 = function(s)
76 if s == nil then return nil end
77 if #s == 0 then return nil end
78 if type(s) == 'string' then
79 local utils = require('utils')
80 local asc = utils.ConvertHexToAscii(s)
81 local hash = core.crc16(asc)
82 return hash
83 end
84 return nil
85 end,
86
87 ------------ CRC-64 ecma checksums
88 -- Takes a hex string and calculates a crc64 ecma
89 Crc64 = function(s)
90 if s == nil then return nil end
91 if #s == 0 then return nil end
92 if type(s) == 'string' then
93 local utils = require('utils')
94 local asc = utils.ConvertHexToAscii(s)
95 local hash = core.crc64(asc)
96 return hash
97 end
98 return nil
99 end,
100
101 ------------ SHA1 hash
102 -- Takes a string and calculates a SHA1 hash
103 Sha1 = function(s)
104 if s == nil then return nil end
105 if #s == 0 then return nil end
106 if type(s) == 'string' then
107 local utils = require('utils')
108 --local asc = utils.ConvertHexToAscii(s)
109 local hash = core.sha1(s)
110 return hash
111 end
112 return nil
113 end,
114 -- Takes a hex string and calculates a SHA1 hash
115 Sha1Hex = function(s)
116 if s == nil then return nil end
117 if #s == 0 then return nil end
118 if type(s) == 'string' then
119 local utils = require('utils')
120 local asc = utils.ConvertHexToAscii(s)
121 local hash = core.sha1(asc)
122 return hash
123 end
124 return nil
125 end,
126
127
128 -- input parameter is a string
129 -- Swaps the endianess and returns a number,
130 -- IE: 'cd7a' -> '7acd' -> 0x7acd
131 SwapEndianness = function(s, len)
132 if s == nil then return nil end
133 if #s == 0 then return '' end
134 if type(s) ~= 'string' then return nil end
135
136 local retval = 0
137 if len == 16 then
138 local t = s:sub(3,4)..s:sub(1,2)
139 retval = tonumber(t,16)
140 elseif len == 24 then
141 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
142 retval = tonumber(t,16)
143 elseif len == 32 then
144 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
145 retval = tonumber(t,16)
146 end
147 return retval
148 end,
149
150 -- input parameter is a string
151 -- Swaps the endianess and returns a string,
152 -- IE: 'cd7a' -> '7acd' -> 0x7acd
153 SwapEndiannessStr = function(s, len)
154 if s == nil then return nil end
155 if #s == 0 then return '' end
156 if type(s) ~= 'string' then return nil end
157
158 local retval
159 if len == 16 then
160 retval = s:sub(3,4)..s:sub(1,2)
161 elseif len == 24 then
162 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
163 elseif len == 32 then
164 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
165 end
166 return retval
167 end,
168 ------------ CONVERSIONS
169
170 --
171 -- Converts DECIMAL to HEX
172 ConvertDecToHex = function(IN)
173 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
174 while IN>0 do
175 I=I+1
176 IN , D = math.floor(IN/B), math.modf(IN,B)+1
177 OUT = string.sub(K,D,D)..OUT
178 end
179 return OUT
180 end,
181 ---
182 -- Convert Byte array to string of hex
183 ConvertBytesToHex = function(bytes)
184 if #bytes == 0 then
185 return ''
186 end
187 local s={}
188 for i = 1, #(bytes) do
189 s[i] = string.format("%02X",bytes[i])
190 end
191 return table.concat(s)
192 end,
193 -- Convert byte array to string with ascii
194 ConvertBytesToAscii = function(bytes)
195 if #bytes == 0 then
196 return ''
197 end
198 local s={}
199 for i = 1, #(bytes) do
200 s[i] = string.char(bytes[i])
201 end
202 return table.concat(s)
203 end,
204 ConvertHexToBytes = function(s)
205 local t={}
206 if s == nil then return t end
207 if #s == 0 then return t end
208 for k in s:gmatch"(%x%x)" do
209 table.insert(t,tonumber(k,16))
210 end
211 return t
212 end,
213 ConvertAsciiToBytes = function(s, reverse)
214 local t = {}
215 if s == nil then return t end
216 if #s == 0 then return t end
217
218 for k in s:gmatch"(.)" do
219 table.insert(t, string.byte(k))
220 end
221
222 if not reverse then
223 return t
224 end
225
226 local rev = {}
227 if reverse then
228 for i = #t, 1,-1 do
229 table.insert(rev, t[i] )
230 end
231 end
232 return rev
233 end,
234
235 ConvertHexToAscii = function(s)
236 local t={}
237 if s == nil then return t end
238 if #s == 0 then return t end
239 for k in s:gmatch"(%x%x)" do
240 table.insert(t, string.char(tonumber(k,16)))
241 end
242 return table.concat(t)
243 end,
244
245 Chars2num = function(s)
246 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
247 end,
248
249 -- use length of string to determine 8,16,32,64 bits
250 bytes_to_int = function(str,endian,signed)
251 local t={str:byte(1,-1)}
252 if endian=="big" then --reverse bytes
253 local tt={}
254 for k=1,#t do
255 tt[#t-k+1]=t[k]
256 end
257 t=tt
258 end
259 local n=0
260 for k=1,#t do
261 n=n+t[k]*2^((k-1)*8)
262 end
263 if signed then
264 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
265 end
266 return n
267 end,
268
269 -- function convertStringToBytes(str)
270 -- local bytes = {}
271 -- local strLength = string.len(str)
272 -- for i=1,strLength do
273 -- table.insert(bytes, string.byte(str, i))
274 -- end
275
276 -- return bytes
277 -- end
278
279 -- function convertBytesToString(bytes)
280 -- local bytesLength = table.getn(bytes)
281 -- local str = ""
282 -- for i=1,bytesLength do
283 -- str = str .. string.char(bytes[i])
284 -- end
285
286 -- return str
287 -- end
288
289 -- function convertHexStringToBytes(str)
290 -- local bytes = {}
291 -- local strLength = string.len(str)
292 -- for k=2,strLength,2 do
293 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
294 -- table.insert(bytes, hex.to_dec(hexString))
295 -- end
296
297 -- return bytes
298 -- end
299
300 -- function convertBytesToHexString(bytes)
301 -- local str = ""
302 -- local bytesLength = table.getn(bytes)
303 -- for i=1,bytesLength do
304 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
305 -- if string.len(hexString) == 1 then
306 -- hexString = "0" .. hexString
307 -- end
308 -- str = str .. hexString
309 -- end
310
311 -- return str
312 -- end
313
314 }
315 return Utils
Impressum, Datenschutz