local bit = require "bit" local tools = {} local function ToStringEx(value) if type(value)=='table' then return tools.tableToStr(value) elseif type(value)=='string' then return "\'"..value.."\'" else return tostring(value) end end function tools.tableToStr(t) if t == nil then return "" end local retstr= "{" local i = 1 for key,value in pairs(t) do local signal = "," if i==1 then signal = "" end if key == i then retstr = retstr..signal..ToStringEx(value) else if type(key)=='number' or type(key) == 'string' then retstr = retstr..signal..'['..ToStringEx(key).."]="..ToStringEx(value) else if type(key)=='userdata' then retstr = retstr..signal.."*s"..tools.tableToStr(getmetatable(key)).."*e".."="..ToStringEx(value) else retstr = retstr..signal..key.."="..ToStringEx(value) end end end i = i+1 end retstr = retstr.."}" -- log.debug("tools", retstr) return retstr end function tools.strToTable(str) if str == nil or type(str) ~= "string" then return end local res1, res2 = loadstring("return " .. str) return res1() end function tools.strToNumbers(str) local numbers = {} for i = 1, string.len(str), 1 do numbers[i] = string.byte(str, i) end return numbers end local base64Table = {'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'} base64Table[0] = 'A' function tools.b64Encode(data) for i = 1, #data, 1 do log_print.info(string.format("%02X", data[i])) end local patch = (3 - #data % 3) % 3 local result = "" if patch ~= 0 then for i = 1, patch, 1 do table.insert(data, 0) end end for i = 0, #data / 3 - 1, 1 do local tmp = data[i * 3 + 1] * 0x10000 + data[i * 3 + 2] * 0x100 + data[i * 3 + 3] result = result .. base64Table[bit.rshift(bit.band(tmp, 0xfc0000), 18)] result = result .. base64Table[bit.rshift(bit.band(tmp, 0x3f000), 12)] result = result .. base64Table[bit.rshift(bit.band(tmp, 0xfc0), 6)] result = result .. base64Table[bit.band(tmp, 0x3f)] end if patch ~= 3 then result = string.sub(result, 1, #result - patch) .. string.rep('=', patch) end return result end function tools.toBytes(number, len) local bytes = {} if number < 256 and (len or 0) < 2 then bytes[1] = number return bytes end while number > 0 do table.insert(bytes, number % 0x100) number = bit.rshift(number, 8) end if len and #bytes < len then for i = #bytes, len - 1, 1 do table.insert(bytes, 0) end end for i = 1, #bytes / 2, 1 do local tmp = bytes[i] bytes[i] = bytes[#bytes - i + 1] bytes[#bytes - i + 1] = tmp end return bytes end -- function string.split(data, reps) -- if type(reps) == "string" then -- reps = {reps} -- end -- if type(reps) ~= "table" then -- return false, "incorrect reps" -- end -- local result = {} -- local splits = {} -- local poses = {} -- local tdata = data -- for _, v in pairs(reps) do -- local skip = 0 -- data = tdata -- if type(v) == "string" then -- while true do -- local pos = string.find(data, v) -- if pos then -- table.insert(poses, {pos = pos + skip, len = #v}) -- skip = skip + pos + #v - 1 -- data = string.sub(tdata, skip + 1) -- else break -- end -- end -- end -- end -- table.sort(poses, function(a, b) -- return a.pos < b.pos -- end) -- if poses[1].pos ~= 1 then -- splits[1] = {pos = 1, len = 0} -- end -- for k, v in pairs(poses) do -- table.insert(splits, v) -- end -- for i = 1, #splits - 1, 1 do -- local startPos = splits[i].pos + splits[i].len -- local endPos = splits[i + 1].pos - 1 -- if endPos >= startPos then -- local substring = string.sub(tdata, startPos, endPos) -- table.insert(result, substring) -- end -- end -- if splits[#splits].pos < #tdata then -- table.insert(result, string.sub(tdata, splits[#splits].pos + splits[#splits].len)) -- end -- return result -- end function string.split(szFullString, szSeparator) local nFindStartIndex = 1 local nSplitIndex = 1 local nSplitArray = {} while true do local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex) if not nFindLastIndex then nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString)) break end nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1) nFindStartIndex = nFindLastIndex + string.len(szSeparator) nSplitIndex = nSplitIndex + 1 end return nSplitArray end return tools