]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
CHG: the default_toys.lua has the index bytes in mix endiansness. I tried to clean...
[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
104 -- input parameter is a string
105 -- Swaps the endianess and returns a number,
106 -- IE: 'cd7a' -> '7acd' -> 0x7acd
107 SwapEndianness = function(s, len)
108 if s == nil then return nil end
109 if #s == 0 then return '' end
110 if type(s) ~= 'string' then return nil end
111
112 local retval = 0
113 if len == 16 then
114 local t = s:sub(3,4)..s:sub(1,2)
115 retval = tonumber(t,16)
116 elseif len == 24 then
117 local t = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
118 retval = tonumber(t,16)
119 elseif len == 32 then
120 local t = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
121 retval = tonumber(t,16)
122 end
123 return retval
124 end,
125
126 -- input parameter is a string
127 -- Swaps the endianess and returns a string,
128 -- IE: 'cd7a' -> '7acd' -> 0x7acd
129 SwapEndiannessStr = function(s, len)
130 if s == nil then return nil end
131 if #s == 0 then return '' end
132 if type(s) ~= 'string' then return nil end
133
134 local retval
135 if len == 16 then
136 retval = s:sub(3,4)..s:sub(1,2)
137 elseif len == 24 then
138 retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
139 elseif len == 32 then
140 retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)
141 end
142 return retval
143 end,
144 ------------ CONVERSIONS
145
146 --
147 -- Converts DECIMAL to HEX
148 ConvertDecToHex = function(IN)
149 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
150 while IN>0 do
151 I=I+1
152 IN , D = math.floor(IN/B), math.modf(IN,B)+1
153 OUT = string.sub(K,D,D)..OUT
154 end
155 return OUT
156 end,
157 ---
158 -- Convert Byte array to string of hex
159 ConvertBytesToHex = function(bytes)
160 if #bytes == 0 then
161 return ''
162 end
163 local s={}
164 for i = 1, #(bytes) do
165 s[i] = string.format("%02X",bytes[i])
166 end
167 return table.concat(s)
168 end,
169 -- Convert byte array to string with ascii
170 ConvertBytesToAscii = function(bytes)
171 if #bytes == 0 then
172 return ''
173 end
174 local s={}
175 for i = 1, #(bytes) do
176 s[i] = string.char(bytes[i])
177 end
178 return table.concat(s)
179 end,
180 ConvertHexToBytes = function(s)
181 local t={}
182 if s == nil then return t end
183 if #s == 0 then return t end
184 for k in s:gmatch"(%x%x)" do
185 table.insert(t,tonumber(k,16))
186 end
187 return t
188 end,
189 ConvertAsciiToBytes = function(s, reverse)
190 local t = {}
191 if s == nil then return t end
192 if #s == 0 then return t end
193
194 for k in s:gmatch"(.)" do
195 table.insert(t, string.byte(k))
196 end
197
198 if not reverse then
199 return t
200 end
201
202 local rev = {}
203 if reverse then
204 for i = #t, 1,-1 do
205 table.insert(rev, t[i] )
206 end
207 end
208 return rev
209 end,
210
211 ConvertHexToAscii = function(s)
212 local t={}
213 if s == nil then return t end
214 if #s == 0 then return t end
215 for k in s:gmatch"(%x%x)" do
216 table.insert(t, string.char(tonumber(k,16)))
217 end
218 return table.concat(t)
219 end,
220
221 Chars2num = function(s)
222 return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))
223 end,
224
225 -- use length of string to determine 8,16,32,64 bits
226 bytes_to_int = function(str,endian,signed)
227 local t={str:byte(1,-1)}
228 if endian=="big" then --reverse bytes
229 local tt={}
230 for k=1,#t do
231 tt[#t-k+1]=t[k]
232 end
233 t=tt
234 end
235 local n=0
236 for k=1,#t do
237 n=n+t[k]*2^((k-1)*8)
238 end
239 if signed then
240 n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.
241 end
242 return n
243 end,
244
245 -- function convertStringToBytes(str)
246 -- local bytes = {}
247 -- local strLength = string.len(str)
248 -- for i=1,strLength do
249 -- table.insert(bytes, string.byte(str, i))
250 -- end
251
252 -- return bytes
253 -- end
254
255 -- function convertBytesToString(bytes)
256 -- local bytesLength = table.getn(bytes)
257 -- local str = ""
258 -- for i=1,bytesLength do
259 -- str = str .. string.char(bytes[i])
260 -- end
261
262 -- return str
263 -- end
264
265 -- function convertHexStringToBytes(str)
266 -- local bytes = {}
267 -- local strLength = string.len(str)
268 -- for k=2,strLength,2 do
269 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
270 -- table.insert(bytes, hex.to_dec(hexString))
271 -- end
272
273 -- return bytes
274 -- end
275
276 -- function convertBytesToHexString(bytes)
277 -- local str = ""
278 -- local bytesLength = table.getn(bytes)
279 -- for i=1,bytesLength do
280 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
281 -- if string.len(hexString) == 1 then
282 -- hexString = "0" .. hexString
283 -- end
284 -- str = str .. hexString
285 -- end
286
287 -- return str
288 -- end
289
290 }
291 return Utils
Impressum, Datenschutz