]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
CHG: the call to TurnReadLFOn has a delay / number as parameter. Seems to be gone.
[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 ----ISO14443-B CRC
73 Crc14b = function(s)
74 if s == nil then return nil end
75 if #s == 0 then return nil end
76 if type(s) == 'string' then
77 local utils = require('utils')
78 return utils.ConvertAsciiToHex(
79 core.iso14443b_crc(s)
80 )
81 end
82 return nil
83 end,
84
85 ------------ CRC-8 Legic checksums
86 -- Takes a hex string and calculates a crc8
87 Crc8Legic = function(s)
88 if s == nil then return nil end
89 if #s == 0 then return nil end
90 if type(s) == 'string' then
91 local utils = require('utils')
92 local asc = utils.ConvertHexToAscii(s)
93 local hash = core.crc8legic(asc)
94 return hash
95 end
96 return nil
97 end,
98
99 ------------ CRC-16 ccitt checksums
100 -- Takes a hex string and calculates a crc16
101 Crc16 = 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 local asc = utils.ConvertHexToAscii(s)
107 local hash = core.crc16(asc)
108 return hash
109 end
110 return nil
111 end,
112
113
114 ------------ CRC-64 ecma checksums
115 -- Takes a hex string and calculates a crc64 ecma
116 Crc64 = function(s)
117 if s == nil then return nil end
118 if #s == 0 then return nil end
119 if type(s) == 'string' then
120 local utils = require('utils')
121 local asc = utils.ConvertHexToAscii(s)
122 local hash = core.crc64(asc)
123 return hash
124 end
125 return nil
126 end,
127
128 ------------ SHA1 hash
129 -- Takes a string and calculates a SHA1 hash
130 Sha1 = function(s)
131 if s == nil then return nil end
132 if #s == 0 then return nil end
133 if type(s) == 'string' then
134 local utils = require('utils')
135 --local asc = utils.ConvertHexToAscii(s)
136 local hash = core.sha1(s)
137 return hash
138 end
139 return nil
140 end,
141 -- Takes a hex string and calculates a SHA1 hash
142 Sha1Hex = function(s)
143 if s == nil then return nil end
144 if #s == 0 then return nil end
145 if type(s) == 'string' then
146 local utils = require('utils')
147 local asc = utils.ConvertHexToAscii(s)
148 local hash = core.sha1(asc)
149 return hash
150 end
151 return nil
152 end,
153
154
155 -- input parameter is a string
156 -- Swaps the endianess and returns a number,
157 -- IE: 'cd7a' -> '7acd' -> 0x7acd
158 SwapEndianness = function(s, len)
159 if s == nil then return nil end
160 if #s == 0 then return '' end
161 if type(s) ~= 'string' then return nil end
162
163 local retval = 0
164 if len == 16 then
165 local t = s:sub(3,4)..s:sub(1,2)
166 retval = tonumber(t,16)
167 elseif len == 24 then
168 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
169 retval = tonumber(t,16)
170 elseif len == 32 then
171 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
172 retval = tonumber(t,16)
173 end
174 return retval
175 end,
176
177 -- input parameter is a string
178 -- Swaps the endianess and returns a string,
179 -- IE: 'cd7a' -> '7acd' -> 0x7acd
180 SwapEndiannessStr = function(s, len)
181 if s == nil then return nil end
182 if #s == 0 then return '' end
183 if type(s) ~= 'string' then return nil end
184
185 local retval
186 if len == 16 then
187 retval = s:sub(3,4)..s:sub(1,2)
188 elseif len == 24 then
189 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
190 elseif len == 32 then
191 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
192 end
193 return retval
194 end,
195 ------------ CONVERSIONS
196
197 --
198 -- Converts DECIMAL to HEX
199 ConvertDecToHex = function(IN)
200 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
201 while IN>0 do
202 I=I+1
203 IN , D = math.floor(IN/B), math.modf(IN,B)+1
204 OUT = string.sub(K,D,D)..OUT
205 end
206 return OUT
207 end,
208 ---
209 -- Convert Byte array to string of hex
210 ConvertBytesToHex = function(bytes)
211 if bytes == nil then return '' end
212 if #bytes == 0 then return '' end
213 local s={}
214 for i = 1, #bytes do
215 s[i] = string.format("%02X",bytes[i])
216 end
217 return table.concat(s)
218 end,
219 -- Convert byte array to string with ascii
220 ConvertBytesToAscii = function(bytes)
221 if bytes == nil then return '' end
222 if #bytes == 0 then return '' end
223 local s={}
224 for i = 1, #(bytes) do
225 s[i] = string.char(bytes[i])
226 end
227 return table.concat(s)
228 end,
229 ConvertHexToBytes = function(s)
230 local t={}
231 if s == nil then return t end
232 if #s == 0 then return t end
233 for k in s:gmatch"(%x%x)" do
234 table.insert(t,tonumber(k,16))
235 end
236 return t
237 end,
238 ConvertAsciiToBytes = function(s, reverse)
239 local t = {}
240 if s == nil then return t end
241 if #s == 0 then return t end
242
243 for k in s:gmatch"(.)" do
244 table.insert(t, string.byte(k))
245 end
246
247 if not reverse then
248 return t
249 end
250
251 local rev = {}
252 if reverse then
253 for i = #t, 1,-1 do
254 table.insert(rev, t[i] )
255 end
256 end
257 return rev
258 end,
259
260 ConvertHexToAscii = function(s)
261 if s == nil then return '' end
262 if #s == 0 then return '' end
263 local t={}
264 for k in s:gmatch"(%x%x)" do
265 table.insert(t, string.char(tonumber(k,16)))
266 end
267 return table.concat(t)
268 end,
269
270 ConvertAsciiToHex = function(s)
271 if s == nil then return '' end
272 if #s == 0 then return '' end
273 local t={}
274 for k in s:gmatch"(.)" do
275 table.insert(t, string.format("%02X", string.byte(k)))
276 end
277 return table.concat(t)
278 end,
279
280 Chars2num = function(s)
281 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
282 end,
283
284 -- use length of string to determine 8,16,32,64 bits
285 bytes_to_int = function(str,endian,signed)
286 local t={str:byte(1,-1)}
287 if endian=="big" then --reverse bytes
288 local tt={}
289 for k=1,#t do
290 tt[#t-k+1]=t[k]
291 end
292 t=tt
293 end
294 local n=0
295 for k=1,#t do
296 n=n+t[k]*2^((k-1)*8)
297 end
298 if signed then
299 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
300 end
301 return n
302 end,
303
304 -- a simple implementation of a sleep command. Thanks to Mosci
305 -- takes number of seconds to sleep
306 Sleep = function(n)
307 local clock = os.clock
308 local t0 = clock()
309 while clock() - t0 <= n do end
310 return nil
311 end,
312
313 -- function convertStringToBytes(str)
314 -- local bytes = {}
315 -- local strLength = string.len(str)
316 -- for i=1,strLength do
317 -- table.insert(bytes, string.byte(str, i))
318 -- end
319
320 -- return bytes
321 -- end
322
323 -- function convertBytesToString(bytes)
324 -- local bytesLength = table.getn(bytes)
325 -- local str = ""
326 -- for i=1,bytesLength do
327 -- str = str .. string.char(bytes[i])
328 -- end
329
330 -- return str
331 -- end
332
333 -- function convertHexStringToBytes(str)
334 -- local bytes = {}
335 -- local strLength = string.len(str)
336 -- for k=2,strLength,2 do
337 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
338 -- table.insert(bytes, hex.to_dec(hexString))
339 -- end
340
341 -- return bytes
342 -- end
343
344 -- function convertBytesToHexString(bytes)
345 -- local str = ""
346 -- local bytesLength = table.getn(bytes)
347 -- for i=1,bytesLength do
348 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
349 -- if string.len(hexString) == 1 then
350 -- hexString = "0" .. hexString
351 -- end
352 -- str = str .. hexString
353 -- end
354
355 -- return str
356 -- end
357
358 }
359 return Utils
Impressum, Datenschutz