]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
Merge pull request #969 from pwpiwi/gcc10_fixes
[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 if filename == nil then
41 return nil, 'Filename is empty'
42 end
43 if #filename == 0 then
44 return nil, 'Filename length is zero'
45 end
46
47 infile = io.open(filename, "rb")
48 if infile == nil then
49 return nil, string.format("Could not read file %s",filename)
50 end
51 local t = infile:read("*all")
52 len = string.len(t)
53 local _,hex = bin.unpack(("H%d"):format(len),t)
54 io.close(infile)
55 return hex
56 end,
57
58 ------------ string split function
59 Split = function( inSplitPattern, outResults )
60 if not outResults then
61 outResults = {}
62 end
63 local start = 1
64 local splitStart, splitEnd = string.find( self, inSplitPattern, start )
65 while splitStart do
66 table.insert( outResults, string.sub( self, start, splitStart-1 ) )
67 start = splitEnd + 1
68 splitStart, splitEnd = string.find( self, inSplitPattern, start )
69 end
70 table.insert( outResults, string.sub( self, start ) )
71 return outResults
72 end,
73
74
75 ------------ CRC-16 ccitt checksums
76 -- Takes a hex string and calculates a crc16
77 Crc16 = function(s)
78 if s == nil then return nil end
79 if #s == 0 then return nil end
80 if type(s) == 'string' then
81 local utils = require('utils')
82 local asc = utils.ConvertHexToAscii(s)
83 local hash = core.crc16(asc)
84 return hash
85 end
86 return nil
87 end,
88
89 ------------ CRC-64 ecma checksums
90 -- Takes a hex string and calculates a crc64 ecma
91 Crc64 = function(s)
92 if s == nil then return nil end
93 if #s == 0 then return nil end
94 if type(s) == 'string' then
95 local utils = require('utils')
96 local asc = utils.ConvertHexToAscii(s)
97 local hash = core.crc64(asc)
98 return hash
99 end
100 return nil
101 end,
102
103 ------------ SHA1 hash
104 -- Takes a string and calculates a SHA1 hash
105 Sha1 = function(s)
106 if s == nil then return nil end
107 if #s == 0 then return nil end
108 if type(s) == 'string' then
109 local utils = require('utils')
110 --local asc = utils.ConvertHexToAscii(s)
111 local hash = core.sha1(s)
112 return hash
113 end
114 return nil
115 end,
116 -- Takes a hex string and calculates a SHA1 hash
117 Sha1Hex = function(s)
118 if s == nil then return nil end
119 if #s == 0 then return nil end
120 if type(s) == 'string' then
121 local utils = require('utils')
122 local asc = utils.ConvertHexToAscii(s)
123 local hash = core.sha1(asc)
124 return hash
125 end
126 return nil
127 end,
128
129
130 -- input parameter is a string
131 -- Swaps the endianess and returns a number,
132 -- IE: 'cd7a' -> '7acd' -> 0x7acd
133 SwapEndianness = function(s, len)
134 if s == nil then return nil end
135 if #s == 0 then return '' end
136 if type(s) ~= 'string' then return nil end
137
138 local retval = 0
139 if len == 16 then
140 local t = s:sub(3,4)..s:sub(1,2)
141 retval = tonumber(t,16)
142 elseif len == 24 then
143 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
144 retval = tonumber(t,16)
145 elseif len == 32 then
146 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
147 retval = tonumber(t,16)
148 end
149 return retval
150 end,
151
152 -- input parameter is a string
153 -- Swaps the endianess and returns a string,
154 -- IE: 'cd7a' -> '7acd' -> 0x7acd
155 SwapEndiannessStr = function(s, len)
156 if s == nil then return nil end
157 if #s == 0 then return '' end
158 if type(s) ~= 'string' then return nil end
159
160 local retval
161 if len == 16 then
162 retval = s:sub(3,4)..s:sub(1,2)
163 elseif len == 24 then
164 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
165 elseif len == 32 then
166 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
167 end
168 return retval
169 end,
170 ------------ CONVERSIONS
171
172 --
173 -- Converts DECIMAL to HEX
174 ConvertDecToHex = function(IN)
175 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
176 while IN>0 do
177 I=I+1
178 IN , D = math.floor(IN/B), math.modf(IN,B)+1
179 OUT = string.sub(K,D,D)..OUT
180 end
181 return OUT
182 end,
183 ---
184 -- Convert Byte array to string of hex
185 ConvertBytesToHex = function(bytes)
186 if #bytes == 0 then
187 return ''
188 end
189 local s={}
190 for i = 1, #(bytes) do
191 s[i] = string.format("%02X",bytes[i])
192 end
193 return table.concat(s)
194 end,
195 -- Convert byte array to string with ascii
196 ConvertBytesToAscii = function(bytes)
197 if #bytes == 0 then
198 return ''
199 end
200 local s={}
201 for i = 1, #(bytes) do
202 s[i] = string.char(bytes[i])
203 end
204 return table.concat(s)
205 end,
206 ConvertHexToBytes = function(s)
207 local t={}
208 if s == nil then return t end
209 if #s == 0 then return t end
210 for k in s:gmatch"(%x%x)" do
211 table.insert(t,tonumber(k,16))
212 end
213 return t
214 end,
215 ConvertAsciiToBytes = function(s, reverse)
216 local t = {}
217 if s == nil then return t end
218 if #s == 0 then return t end
219
220 for k in s:gmatch"(.)" do
221 table.insert(t, string.byte(k))
222 end
223
224 if not reverse then
225 return t
226 end
227
228 local rev = {}
229 if reverse then
230 for i = #t, 1,-1 do
231 table.insert(rev, t[i] )
232 end
233 end
234 return rev
235 end,
236
237 ConvertHexToAscii = function(s)
238 local t={}
239 if s == nil then return t end
240 if #s == 0 then return t end
241 for k in s:gmatch"(%x%x)" do
242 table.insert(t, string.char(tonumber(k,16)))
243 end
244 return table.concat(t)
245 end,
246
247 Chars2num = function(s)
248 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
249 end,
250
251 -- use length of string to determine 8,16,32,64 bits
252 bytes_to_int = function(str,endian,signed)
253 local t={str:byte(1,-1)}
254 if endian=="big" then --reverse bytes
255 local tt={}
256 for k=1,#t do
257 tt[#t-k+1]=t[k]
258 end
259 t=tt
260 end
261 local n=0
262 for k=1,#t do
263 n=n+t[k]*2^((k-1)*8)
264 end
265 if signed then
266 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
267 end
268 return n
269 end,
270
271 -- function convertStringToBytes(str)
272 -- local bytes = {}
273 -- local strLength = string.len(str)
274 -- for i=1,strLength do
275 -- table.insert(bytes, string.byte(str, i))
276 -- end
277
278 -- return bytes
279 -- end
280
281 -- function convertBytesToString(bytes)
282 -- local bytesLength = table.getn(bytes)
283 -- local str = ""
284 -- for i=1,bytesLength do
285 -- str = str .. string.char(bytes[i])
286 -- end
287
288 -- return str
289 -- end
290
291 -- function convertHexStringToBytes(str)
292 -- local bytes = {}
293 -- local strLength = string.len(str)
294 -- for k=2,strLength,2 do
295 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
296 -- table.insert(bytes, hex.to_dec(hexString))
297 -- end
298
299 -- return bytes
300 -- end
301
302 -- function convertBytesToHexString(bytes)
303 -- local str = ""
304 -- local bytesLength = table.getn(bytes)
305 -- for i=1,bytesLength do
306 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
307 -- if string.len(hexString) == 1 then
308 -- hexString = "0" .. hexString
309 -- end
310 -- str = str .. hexString
311 -- end
312
313 -- return str
314 -- end
315
316 }
317 return Utils
Impressum, Datenschutz