]> git.zerfleddert.de Git - proxmark3-svn/blob - client/lualibs/utils.lua
CHG: added some more token-tags.
[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 -- function convertStringToBytes(str)
195 -- local bytes = {}
196 -- local strLength = string.len(str)
197 -- for i=1,strLength do
198 -- table.insert(bytes, string.byte(str, i))
199 -- end
200
201 -- return bytes
202 -- end
203
204 -- function convertBytesToString(bytes)
205 -- local bytesLength = table.getn(bytes)
206 -- local str = ""
207 -- for i=1,bytesLength do
208 -- str = str .. string.char(bytes[i])
209 -- end
210
211 -- return str
212 -- end
213
214 -- function convertHexStringToBytes(str)
215 -- local bytes = {}
216 -- local strLength = string.len(str)
217 -- for k=2,strLength,2 do
218 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
219 -- table.insert(bytes, hex.to_dec(hexString))
220 -- end
221
222 -- return bytes
223 -- end
224
225 -- function convertBytesToHexString(bytes)
226 -- local str = ""
227 -- local bytesLength = table.getn(bytes)
228 -- for i=1,bytesLength do
229 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
230 -- if string.len(hexString) == 1 then
231 -- hexString = "0" .. hexString
232 -- end
233 -- str = str .. hexString
234 -- end
235
236 -- return str
237 -- end
238
239 }
240 return Utils
Impressum, Datenschutz