]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/lualibs/utils.lua
CHG - some lua functions in utils.lua
[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
111 ------------ CONVERSIONS\r
112 \r
5b1311fb
MHS
113 --\r
114 -- Converts DECIMAL to HEX\r
f91f0ebb 115 ConvertDecToHex = function(IN)\r
5b1311fb
MHS
116 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
117 while IN>0 do\r
118 I=I+1\r
119 IN,D=math.floor(IN/B),math.mod(IN,B)+1\r
120 OUT=string.sub(K,D,D)..OUT\r
121 end\r
122 return OUT\r
123 end,\r
124 ---\r
125 -- Convert Byte array to string of hex\r
f91f0ebb 126 ConvertBytesToHex = function(bytes)\r
cd5767d4 127 if #bytes == 0 then\r
128 return ''\r
129 end\r
130 local s={}\r
5b1311fb 131 for i = 1, #(bytes) do\r
9b989c45 132 s[i] = string.format("%02X",bytes[i]) \r
5b1311fb
MHS
133 end\r
134 return table.concat(s)\r
135 end, \r
cd5767d4 136 -- Convert byte array to string with ascii\r
f91f0ebb 137 ConvertBytesToAscii = function(bytes)\r
cd5767d4 138 if #bytes == 0 then\r
139 return ''\r
140 end\r
141 local s={}\r
142 for i = 1, #(bytes) do\r
143 s[i] = string.char(bytes[i]) \r
144 end\r
145 return table.concat(s) \r
146 end, \r
f91f0ebb 147 ConvertHexToBytes = function(s)\r
9b989c45 148 local t={}\r
cd5767d4 149 if s == nil then return t end\r
150 if #s == 0 then return t end\r
9b989c45 151 for k in s:gmatch"(%x%x)" do\r
152 table.insert(t,tonumber(k,16))\r
153 end\r
154 return t\r
155 end,\r
f91f0ebb 156 ConvertAsciiToBytes = function(s)\r
cd5767d4 157 local t={}\r
158 if s == nil then return t end\r
159 if #s == 0 then return t end\r
160 \r
161 for k in s:gmatch"(.)" do\r
162 table.insert(t, string.byte(k))\r
163 end\r
164 return t\r
165 end,\r
47cbb2d4 166 ConvertHexToAscii = function(s)\r
167 local t={}\r
168 if s == nil then return t end\r
169 if #s == 0 then return t end\r
170 for k in s:gmatch"(%x%x)" do\r
171 table.insert(t, string.char(tonumber(k,16)))\r
172 end\r
173 return table.concat(t) \r
174 end,\r
9b989c45 175 \r
176 -- function convertStringToBytes(str)\r
177 -- local bytes = {}\r
178 -- local strLength = string.len(str)\r
179 -- for i=1,strLength do\r
180 -- table.insert(bytes, string.byte(str, i))\r
181 -- end\r
182\r
183 -- return bytes\r
184-- end\r
185\r
186-- function convertBytesToString(bytes)\r
187 -- local bytesLength = table.getn(bytes)\r
188 -- local str = ""\r
189 -- for i=1,bytesLength do\r
190 -- str = str .. string.char(bytes[i])\r
191 -- end\r
192\r
193 -- return str\r
194-- end\r
195\r
196-- function convertHexStringToBytes(str)\r
197 -- local bytes = {}\r
198 -- local strLength = string.len(str)\r
199 -- for k=2,strLength,2 do\r
200 -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r
201 -- table.insert(bytes, hex.to_dec(hexString))\r
202 -- end\r
203\r
204 -- return bytes\r
205-- end\r
206\r
207-- function convertBytesToHexString(bytes)\r
208 -- local str = ""\r
209 -- local bytesLength = table.getn(bytes)\r
210 -- for i=1,bytesLength do\r
211 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r
212 -- if string.len(hexString) == 1 then\r
213 -- hexString = "0" .. hexString\r
214 -- end\r
215 -- str = str .. hexString\r
216 -- end\r
217\r
218 -- return str\r
219-- end\r
220\r
5b1311fb
MHS
221}\r
222return Utils
Impressum, Datenschutz