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