]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
9dcb49c7b53abbbf35785089d2de71b6a4dadffa
[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 ------------ FILE WRITING (EML)
57 --- Writes an eml-file.
58 -- @param uid - the uid of the tag. Used in filename
59 -- @param blockData. Assumed to be on the format {'\0\1\2\3,'\b\e\e\f' ...,
60 -- that is, blockData[row] contains a string with the actual data, not ascii hex representation
61 -- return filename if all went well,
62 -- @reurn nil, error message if unsuccessfulls
63 WriteDumpFile = function(uid, blockData)
64 local destination = string.format("%s.eml", uid)
65 local file = io.open(destination, "w")
66 if file == nil then
67 return nil, string.format("Could not write to file %s", destination)
68 end
69 local rowlen = string.len(blockData[1])
70
71 for i,block in ipairs(blockData) do
72 if rowlen ~= string.len(block) then
73 prlog(string.format("WARNING: Dumpdata seems corrupted, line %d was not the same length as line 1",i))
74 end
75
76 local formatString = string.format("H%d", string.len(block))
77 local _,hex = bin.unpack(formatString,block)
78 file:write(hex.."\n")
79 end
80 file:close()
81 return destination
82 end,
83
84 ------------ string split function
85 Split = function( inSplitPattern, outResults )
86 if not outResults then
87 outResults = {}
88 end
89 local start = 1
90 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
91 while splitStart do
92 table.insert( outResults, string.sub( self, start, splitStart-1 ) )
93 start = splitEnd + 1
94 splitStart, splitEnd = string.find( self, inSplitPattern, start )
95 end
96 table.insert( outResults, string.sub( self, start ) )
97 return outResults
98 end,
99
100 ----ISO14443-B CRC
101 Crc14b = function(s)
102 if s == nil then return nil end
103 if #s == 0 then return nil end
104 if type(s) == 'string' then
105 local utils = require('utils')
106 return utils.ConvertAsciiToHex(
107 core.iso14443b_crc(s)
108 )
109 end
110 return nil
111 end,
112
113 ------------ CRC-8 Legic checksums
114 -- Takes a hex string and calculates a crc8
115 Crc8Legic = 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.crc8legic(asc)
122 return hash
123 end
124 return nil
125 end,
126
127 ------------ CRC-16 ccitt checksums
128 -- Takes a hex string and calculates a crc16
129 Crc16 = function(s)
130 if s == nil then return nil end
131 if #s == 0 then return nil end
132 if type(s) == 'string' then
133 local utils = require('utils')
134 local asc = utils.ConvertHexToAscii(s)
135 local hash = core.crc16(asc)
136 return hash
137 end
138 return nil
139 end,
140
141
142 ------------ CRC-64 ecma checksums
143 -- Takes a hex string and calculates a crc64 ecma
144 Crc64 = function(s)
145 if s == nil then return nil end
146 if #s == 0 then return nil end
147 if type(s) == 'string' then
148 local utils = require('utils')
149 local asc = utils.ConvertHexToAscii(s)
150 local hash = core.crc64(asc)
151 return hash
152 end
153 return nil
154 end,
155
156 ------------ SHA1 hash
157 -- Takes a string and calculates a SHA1 hash
158 Sha1 = function(s)
159 if s == nil then return nil end
160 if #s == 0 then return nil end
161 if type(s) == 'string' then
162 local utils = require('utils')
163 --local asc = utils.ConvertHexToAscii(s)
164 local hash = core.sha1(s)
165 return hash
166 end
167 return nil
168 end,
169 -- Takes a hex string and calculates a SHA1 hash
170 Sha1Hex = function(s)
171 if s == nil then return nil end
172 if #s == 0 then return nil end
173 if type(s) == 'string' then
174 local utils = require('utils')
175 local asc = utils.ConvertHexToAscii(s)
176 local hash = core.sha1(asc)
177 return hash
178 end
179 return nil
180 end,
181
182
183 -- input parameter is a string
184 -- Swaps the endianess and returns a number,
185 -- IE: 'cd7a' -> '7acd' -> 0x7acd
186 SwapEndianness = function(s, len)
187 if s == nil then return nil end
188 if #s == 0 then return '' end
189 if type(s) ~= 'string' then return nil end
190
191 local retval = 0
192 if len == 16 then
193 local t = s:sub(3,4)..s:sub(1,2)
194 retval = tonumber(t,16)
195 elseif len == 24 then
196 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
197 retval = tonumber(t,16)
198 elseif len == 32 then
199 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
200 retval = tonumber(t,16)
201 end
202 return retval
203 end,
204
205 -- input parameter is a string
206 -- Swaps the endianess and returns a string,
207 -- IE: 'cd7a' -> '7acd' -> 0x7acd
208 SwapEndiannessStr = function(s, len)
209 if s == nil then return nil end
210 if #s == 0 then return '' end
211 if type(s) ~= 'string' then return nil end
212
213 local retval
214 if len == 16 then
215 retval = s:sub(3,4)..s:sub(1,2)
216 elseif len == 24 then
217 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
218 elseif len == 32 then
219 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
220 end
221 return retval
222 end,
223 ------------ CONVERSIONS
224
225 --
226 -- Converts DECIMAL to HEX
227 ConvertDecToHex = function(IN)
228 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
229 while IN>0 do
230 I=I+1
231 IN , D = math.floor(IN/B), math.modf(IN,B)+1
232 OUT = string.sub(K,D,D)..OUT
233 end
234 return OUT
235 end,
236 ---
237 -- Convert Byte array to string of hex
238 ConvertBytesToHex = function(bytes)
239 if bytes == nil then return '' end
240 if #bytes == 0 then return '' end
241 local s={}
242 for i = 1, #bytes do
243 s[i] = string.format("%02X",bytes[i])
244 end
245 return table.concat(s)
246 end,
247 -- Convert byte array to string with ascii
248 ConvertBytesToAscii = function(bytes)
249 if bytes == nil then return '' end
250 if #bytes == 0 then return '' end
251 local s={}
252 for i = 1, #(bytes) do
253 s[i] = string.char(bytes[i])
254 end
255 return table.concat(s)
256 end,
257 ConvertHexToBytes = function(s)
258 local t={}
259 if s == nil then return t end
260 if #s == 0 then return t end
261 for k in s:gmatch"(%x%x)" do
262 table.insert(t,tonumber(k,16))
263 end
264 return t
265 end,
266 ConvertAsciiToBytes = function(s, reverse)
267 local t = {}
268 if s == nil then return t end
269 if #s == 0 then return t end
270
271 for k in s:gmatch"(.)" do
272 table.insert(t, string.byte(k))
273 end
274
275 if not reverse then
276 return t
277 end
278
279 local rev = {}
280 if reverse then
281 for i = #t, 1,-1 do
282 table.insert(rev, t[i] )
283 end
284 end
285 return rev
286 end,
287
288 ConvertHexToAscii = function(s)
289 if s == nil then return '' end
290 if #s == 0 then return '' end
291 local t={}
292 for k in s:gmatch"(%x%x)" do
293 local n = tonumber(k,16)
294 local c
295 if (n < 32) or (n == 127) then
296 c = '.';
297 else
298 c = string.char(n)
299 end
300 table.insert(t,c)
301 end
302 return table.concat(t)
303 end,
304
305 ConvertAsciiToHex = function(s)
306 if s == nil then return '' end
307 if #s == 0 then return '' end
308 local t={}
309 for k in s:gmatch"(.)" do
310 table.insert(t, string.format("%02X", string.byte(k)))
311 end
312 return table.concat(t)
313 end,
314
315 Chars2num = function(s)
316 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
317 end,
318
319 -- use length of string to determine 8,16,32,64 bits
320 bytes_to_int = function(str,endian,signed)
321 local t={str:byte(1,-1)}
322 if endian=="big" then --reverse bytes
323 local tt={}
324 for k=1,#t do
325 tt[#t-k+1]=t[k]
326 end
327 t=tt
328 end
329 local n=0
330 for k=1,#t do
331 n=n+t[k]*2^((k-1)*8)
332 end
333 if signed then
334 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
335 end
336 return n
337 end,
338
339 -- a simple implementation of a sleep command. Thanks to Mosci
340 -- takes number of seconds to sleep
341 Sleep = function(n)
342 local clock = os.clock
343 local t0 = clock()
344 while clock() - t0 <= n do end
345 return nil
346 end,
347
348 -- function convertStringToBytes(str)
349 -- local bytes = {}
350 -- local strLength = string.len(str)
351 -- for i=1,strLength do
352 -- table.insert(bytes, string.byte(str, i))
353 -- end
354
355 -- return bytes
356 -- end
357
358 -- function convertBytesToString(bytes)
359 -- local bytesLength = table.getn(bytes)
360 -- local str = ""
361 -- for i=1,bytesLength do
362 -- str = str .. string.char(bytes[i])
363 -- end
364
365 -- return str
366 -- end
367
368 -- function convertHexStringToBytes(str)
369 -- local bytes = {}
370 -- local strLength = string.len(str)
371 -- for k=2,strLength,2 do
372 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
373 -- table.insert(bytes, hex.to_dec(hexString))
374 -- end
375
376 -- return bytes
377 -- end
378
379 -- function convertBytesToHexString(bytes)
380 -- local str = ""
381 -- local bytesLength = table.getn(bytes)
382 -- for i=1,bytesLength do
383 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
384 -- if string.len(hexString) == 1 then
385 -- hexString = "0" .. hexString
386 -- end
387 -- str = str .. hexString
388 -- end
389
390 -- return str
391 -- end
392
393 }
394 return Utils
Impressum, Datenschutz