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