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