-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunserializer.lua
190 lines (147 loc) · 4.46 KB
/
unserializer.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
--[[
Based on:
LUA variant of the php unserialize function
Port of http://phpjs.org/functions/unserialize
https://gist.github.com/cristobal/3647759
and
crowdlab/js-php-unserialize
https://github.com/crowdlab/js-php-unserialize
]]--
local unserializer = {}
local function utf8Overhead(chr)
local code = chr:byte()
if (code < 0x0080) then
return 0
end
if (code < 0x0800) then
return 1
end
return 2
end
local function readUntil(data, offset, stopchr)
local buf, chr, len
buf = {}
chr = data:sub(offset, offset)
len = string.len(data)
while (chr ~= stopchr) do
if (offset > len) then
error('Error', 'Invalid')
end
table.insert(buf, chr)
offset = offset + 1
chr = data:sub(offset, offset)
end
return {#buf, table.concat(buf,'')}
end
local function readChrs(data, offset, length)
local i, buf
buf = {}
for i = 0, length - 1, 1 do
local chr = data:sub(offset + i, offset + i)
table.insert(buf, chr)
length = length - utf8Overhead(chr)
end
return {#buf, table.concat(buf,'')}
end
local function _unserialize(data, offset)
local dtype, dataoffset, keyandchrs, keys
local readdata, readData, ccount, stringlength
local i, key, kprops, kchrs, vprops, vchrs, value, chrs, typeconvert
local chrs = 0
typeconvert = function(x) return x end
if offset == nil then
offset = 1 -- lua offsets starts at 1
end
dtype = string.lower(data:sub(offset, offset))
dataoffset = offset + 2
if (dtype == 'i') or (dtype == 'd') then
typeconvert = function(x)
return tonumber(x)
end
readData = readUntil(data, dataoffset, ';')
chrs = tonumber(readData[1])
readdata = readData[2]
dataoffset = dataoffset + chrs + 1
elseif dtype == 'b' then
typeconvert = function(x)
return tonumber(x) ~= 0
end
readData = readUntil(data, dataoffset, ';')
chrs = tonumber(readData[1])
readdata = readData[2]
dataoffset = dataoffset + chrs + 1
elseif dtype == 'n' then
readData = nil
elseif dtype == 's' then
ccount = readUntil(data, dataoffset, ':')
chrs = tonumber(ccount[1])
stringlength = tonumber(ccount[2])
dataoffset = dataoffset + chrs + 2
readData = readChrs(data, dataoffset, stringlength)
chrs = readData[1]
readdata = readData[2]
dataoffset = dataoffset + chrs + 2
if ((chrs ~= stringlength) and (chrs ~= string.length(readdata.length))) then
error('SyntaxError', 'String length mismatch')
end
elseif dtype == 'a' then
readdata = {}
keyandchrs = readUntil(data, dataoffset, ':')
chrs = tonumber(keyandchrs[1]);
keys = tonumber(keyandchrs[2]);
dataoffset = dataoffset + chrs + 2
for i = 0, keys - 1, 1 do
kprops = _unserialize(data, dataoffset);
kchrs = tonumber(kprops[2])
key = kprops[3]
dataoffset = dataoffset + kchrs
vprops = _unserialize(data, dataoffset)
vchrs = tonumber(vprops[2])
value = vprops[3]
dataoffset = dataoffset + vchrs
readdata[key] = value
end
dataoffset = dataoffset + 1
else
error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
end
return {dtype, dataoffset - offset, typeconvert(readdata)}
end
local function _unserializeSession(data)
local pos = 1
local result = {}
if data == '' then
return nil
end
repeat
local key = ''
local c = data:sub(pos, pos)
while (string.len(data) >= pos and c ~= '|') do
key = key .. c
pos = pos + 1
c = data:sub(pos, pos)
end
if key == '' or key == "\n" or key == "\r" then
break
end
pos = pos + 1
local r = _unserialize(data, pos)
if not r or r[2] == 0 then
return nil
end
pos = pos + r[2]
result[key] = r[3]
until pos >= string.len(data)
return result
end
function unserializer.unserialize(data)
local result = _unserialize(data or "", 1)
if result ~= nil then
return result[3]
end
return nil
end
function unserializer.unserializeSession(data)
return _unserializeSession(data)
end
return unserializer