]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/lualibs/utils.lua
Icemans UL-C Auth dev side fix plus a few other ...
[proxmark3-svn] / client / lualibs / utils.lua
... / ...
CommitLineData
1--[[\r
2 This may be moved to a separate library at some point (Holiman)\r
3--]]\r
4local 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
36 \r
37 ------------ FILE READING\r
38 ReadDumpFile = function (filename)\r
39 \r
40 if filename == nil then \r
41 return nil, 'Filename is empty'\r
42 end\r
43 if #filename == 0 then\r
44 return nil, 'Filename length is zero'\r
45 end\r
46\r
47 infile = io.open(filename, "rb")\r
48 if infile == nil then \r
49 return nil, string.format("Could not read file %s",filename)\r
50 end\r
51 local t = infile:read("*all")\r
52 len = string.len(t)\r
53 local _,hex = bin.unpack(("H%d"):format(len),t)\r
54 io.close(infile)\r
55 return hex\r
56 end,\r
57 \r
58 ------------ string split function\r
59 Split = function( inSplitPattern, outResults )\r
60 if not outResults then\r
61 outResults = {}\r
62 end\r
63 local start = 1\r
64 local splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
65 while splitStart do\r
66 table.insert( outResults, string.sub( self, start, splitStart-1 ) )\r
67 start = splitEnd + 1\r
68 splitStart, splitEnd = string.find( self, inSplitPattern, start )\r
69 end\r
70 table.insert( outResults, string.sub( self, start ) )\r
71 return outResults\r
72 end,\r
73 \r
74 ------------ CRC-16 ccitt checksums\r
75 \r
76 -- Takes a hex string and calculates a crc16\r
77 Crc16 = function(s)\r
78 if s == nil then return nil end\r
79 if #s == 0 then return nil end\r
80 if type(s) == 'string' then\r
81 local utils = require('utils')\r
82 local asc = utils.ConvertHexToAscii(s)\r
83 local hash = core.crc16(asc)\r
84 return hash\r
85 end\r
86 return nil\r
87 end,\r
88\r
89 -- input parameter is a string\r
90 -- Swaps the endianess and returns a number, \r
91 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
92 SwapEndianness = function(s, len)\r
93 if s == nil then return nil end\r
94 if #s == 0 then return '' end\r
95 if type(s) ~= 'string' then return nil end\r
96 \r
97 local retval = 0\r
98 if len == 16 then\r
99 local t = s:sub(3,4)..s:sub(1,2)\r
100 retval = tonumber(t,16)\r
101 elseif len == 24 then\r
102 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
103 retval = tonumber(t,16)\r
104 elseif len == 32 then\r
105 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
106 retval = tonumber(t,16)\r
107 end\r
108 return retval\r
109 end,\r
110 \r
111 -- input parameter is a string\r
112 -- Swaps the endianess and returns a string, \r
113 -- IE: 'cd7a' -> '7acd' -> 0x7acd\r
114 SwapEndiannessStr = function(s, len)\r
115 if s == nil then return nil end\r
116 if #s == 0 then return '' end\r
117 if type(s) ~= 'string' then return nil end\r
118 \r
119 local retval\r
120 if len == 16 then\r
121 retval = s:sub(3,4)..s:sub(1,2)\r
122 elseif len == 24 then\r
123 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
124 elseif len == 32 then\r
125 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
126 end\r
127 return retval\r
128 end, \r
129 ------------ CONVERSIONS\r
130 \r
131 --\r
132 -- Converts DECIMAL to HEX\r
133 ConvertDecToHex = function(IN)\r
134 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
135 while IN>0 do\r
136 I=I+1\r
137 IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
138 OUT=string.sub(K,D,D)..OUT\r
139 end\r
140 return OUT\r
141 end,\r
142 ---\r
143 -- Convert Byte array to string of hex\r
144 ConvertBytesToHex = function(bytes)\r
145 if #bytes == 0 then\r
146 return ''\r
147 end\r
148 local s={}\r
149 for i = 1, #(bytes) do\r
150 s[i] = string.format("%02X",bytes[i]) \r
151 end\r
152 return table.concat(s)\r
153 end, \r
154 -- Convert byte array to string with ascii\r
155 ConvertBytesToAscii = function(bytes)\r
156 if #bytes == 0 then\r
157 return ''\r
158 end\r
159 local s={}\r
160 for i = 1, #(bytes) do\r
161 s[i] = string.char(bytes[i]) \r
162 end\r
163 return table.concat(s) \r
164 end, \r
165 ConvertHexToBytes = function(s)\r
166 local t={}\r
167 if s == nil then return t end\r
168 if #s == 0 then return t end\r
169 for k in s:gmatch"(%x%x)" do\r
170 table.insert(t,tonumber(k,16))\r
171 end\r
172 return t\r
173 end,\r
174 ConvertAsciiToBytes = function(s)\r
175 local t={}\r
176 if s == nil then return t end\r
177 if #s == 0 then return t end\r
178 \r
179 for k in s:gmatch"(.)" do\r
180 table.insert(t, string.byte(k))\r
181 end\r
182 return t\r
183 end,\r
184 ConvertHexToAscii = function(s)\r
185 local t={}\r
186 if s == nil then return t end\r
187 if #s == 0 then return t end\r
188 for k in s:gmatch"(%x%x)" do\r
189 table.insert(t, string.char(tonumber(k,16)))\r
190 end\r
191 return table.concat(t) \r
192 end,\r
193 \r
194 -- function convertStringToBytes(str)\r
195 -- local bytes = {}\r
196 -- local strLength = string.len(str)\r
197 -- for i=1,strLength do\r
198 -- table.insert(bytes, string.byte(str, i))\r
199 -- end\r
200\r
201 -- return bytes\r
202-- end\r
203\r
204-- function convertBytesToString(bytes)\r
205 -- local bytesLength = table.getn(bytes)\r
206 -- local str = ""\r
207 -- for i=1,bytesLength do\r
208 -- str = str .. string.char(bytes[i])\r
209 -- end\r
210\r
211 -- return str\r
212-- end\r
213\r
214-- function convertHexStringToBytes(str)\r
215 -- local bytes = {}\r
216 -- local strLength = string.len(str)\r
217 -- for k=2,strLength,2 do\r
218 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
219 -- table.insert(bytes, hex.to_dec(hexString))\r
220 -- end\r
221\r
222 -- return bytes\r
223-- end\r
224\r
225-- function convertBytesToHexString(bytes)\r
226 -- local str = ""\r
227 -- local bytesLength = table.getn(bytes)\r
228 -- for i=1,bytesLength do\r
229 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
230 -- if string.len(hexString) == 1 then\r
231 -- hexString = "0" .. hexString\r
232 -- end\r
233 -- str = str .. hexString\r
234 -- end\r
235\r
236 -- return str\r
237-- end\r
238\r
239}\r
240return Utils
Impressum, Datenschutz