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