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