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