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