]> git.zerfleddert.de Git - proxmark3-svn/blame - client/lualibs/utils.lua
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / lualibs / utils.lua
CommitLineData
5b1311fb
MHS
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
f91f0ebb 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
5149e37e 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
f91f0ebb 129 ------------ CONVERSIONS\r
130 \r
5b1311fb
MHS
131 --\r
132 -- Converts DECIMAL to HEX\r
f91f0ebb 133 ConvertDecToHex = function(IN)\r
5b1311fb
MHS
134 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
135 while IN>0 do\r
136 I=I+1\r
5149e37e 137 IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
3ac59c7f 138 OUT = string.sub(K,D,D)..OUT\r
5b1311fb
MHS
139 end\r
140 return OUT\r
141 end,\r
142 ---\r
143 -- Convert Byte array to string of hex\r
f91f0ebb 144 ConvertBytesToHex = function(bytes)\r
cd5767d4 145 if #bytes == 0 then\r
146 return ''\r
147 end\r
148 local s={}\r
5b1311fb 149 for i = 1, #(bytes) do\r
9b989c45 150 s[i] = string.format("%02X",bytes[i]) \r
5b1311fb
MHS
151 end\r
152 return table.concat(s)\r
153 end, \r
cd5767d4 154 -- Convert byte array to string with ascii\r
f91f0ebb 155 ConvertBytesToAscii = function(bytes)\r
cd5767d4 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
f91f0ebb 165 ConvertHexToBytes = function(s)\r
9b989c45 166 local t={}\r
cd5767d4 167 if s == nil then return t end\r
168 if #s == 0 then return t end\r
9b989c45 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
f91f0ebb 174 ConvertAsciiToBytes = function(s)\r
cd5767d4 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
47cbb2d4 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
9b989c45 193 \r
3ac59c7f 194 Chars2num = function(s)\r
195 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
196 end,\r
197 \r
198 -- use length of string to determine 8,16,32,64 bits\r
199 bytes_to_int = function(str,endian,signed) \r
200 local t={str:byte(1,-1)}\r
201 if endian=="big" then --reverse bytes\r
202 local tt={}\r
203 for k=1,#t do\r
204 tt[#t-k+1]=t[k]\r
205 end\r
206 t=tt\r
207 end\r
208 local n=0\r
209 for k=1,#t do\r
210 n=n+t[k]*2^((k-1)*8)\r
211 end\r
212 if signed then\r
213 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
214 end\r
215 return n\r
216 end,\r
217 \r
9b989c45 218 -- function convertStringToBytes(str)\r
219 -- local bytes = {}\r
220 -- local strLength = string.len(str)\r
221 -- for i=1,strLength do\r
222 -- table.insert(bytes, string.byte(str, i))\r
223 -- end\r
224\r
225 -- return bytes\r
226-- end\r
227\r
228-- function convertBytesToString(bytes)\r
229 -- local bytesLength = table.getn(bytes)\r
230 -- local str = ""\r
231 -- for i=1,bytesLength do\r
232 -- str = str .. string.char(bytes[i])\r
233 -- end\r
234\r
235 -- return str\r
236-- end\r
237\r
238-- function convertHexStringToBytes(str)\r
239 -- local bytes = {}\r
240 -- local strLength = string.len(str)\r
241 -- for k=2,strLength,2 do\r
242 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
243 -- table.insert(bytes, hex.to_dec(hexString))\r
244 -- end\r
245\r
246 -- return bytes\r
247-- end\r
248\r
249-- function convertBytesToHexString(bytes)\r
250 -- local str = ""\r
251 -- local bytesLength = table.getn(bytes)\r
252 -- for i=1,bytesLength do\r
253 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
254 -- if string.len(hexString) == 1 then\r
255 -- hexString = "0" .. hexString\r
256 -- end\r
257 -- str = str .. hexString\r
258 -- end\r
259\r
260 -- return str\r
261-- end\r
262\r
5b1311fb
MHS
263}\r
264return Utils
Impressum, Datenschutz