-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.lua
116 lines (97 loc) · 2.54 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
--[[
Copyright 2012 Kengo Nakajima. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local table = require("table")
local string = require("string")
local Bit = require("bit")
local Constants = require("./constants")
local Util = {}
function Util.byteArrayToString(t)
local out = {}
for i,b in ipairs(t) do
out[i] = string.char(b)
end
return table.concat(out, "")
end
-- xor 2 string
function Util.xor(a,b)
assert(#a == #b)
local out={}
for i=1,#a do
out[i] = Bit.bxor( string.byte(a,i), string.byte(b,i) )
end
return Util.byteArrayToString(out)
end
-- convert luvit buffer to binary string
function Util.bufferToString(b)
local t ={}
for i=1,b.length do
t[i] = string.char(b[i])
end
return table.concat(t)
end
function Util.dumpStringBytes(t)
local ttt ={}
for i=1,#t do
table.insert( ttt, string.format( "%x", string.byte( t, i ) ) )
if (i % 8)==0 then
table.insert( ttt, " " )
end
end
print( table.concat( ttt, " " ) )
end
local escapeStringRepl = {
[0] = "\\0",
[8] = "\\b",
[9] = "\\t",
[10] = "\\n",
[13] = "\\r",
[26] = "\\Z",
[34] = "\\\"",
[39] = "\\\'",
[92] = "\\\\"
}
function Util.escapeString(str)
local out = {}
for i=1,#str do
local byte = string.byte( str, i )
local to = escapeStringRepl[byte]
if to then
table.insert( out, to )
else
table.insert( out, string.char(byte) )
end
end
return table.concat( out )
end
function Util.packetToUserObject(packet)
local out = {}
for k,v in pairs(packet) do
local newKey
if k == "errorMessage" then
newKey = "message"
elseif k == "errorNumber" then
newKey = "number"
else
newKey = k
end
out[newKey] = v
end
-- always overwrite number
if packet.errorNumber then out.number = packet.errorNumber end
return out
end
function Util.convertStringDateToTable(s)
local _,_,y,m,d,h,min,sec = string.find(s, "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
return { year = y, month = m, day = d, hour = h, minute = min, second = sec }
end
return Util