-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunserialize.lua
161 lines (125 loc) · 4.27 KB
/
unserialize.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
--[[
LUA variant of the php unserialize function
Port of http://phpjs.org/functions/unserialize
]]--
local function unserialize (data)
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 error (type, msg, filename, line)
print ("[Error(" .. type .. ", " .. message ..")]")
end
local function read_until (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 read_chrs(data, offset, length)
local i, buf;
buf = {};
for i = 0, length - 1, 1 do
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,
readdata, readData, ccount, stringlength,
i, key, kprops, kchrs, vprops, vchrs, value,
chrs, typeconvert;
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))
-- print ("dtype " .. dtype .. " offset " ..offset)
dataoffset = offset + 2
if (dtype == 'i') or (dtype == 'd') or (dtype == 'r') then
typeconvert = function(x)
return tonumber(x)
end
readData = read_until(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 = read_until(data, dataoffset, ';');
chrs = tonumber(readData[1]);
readdata = readData[2];
dataoffset = dataoffset + chrs + 1;
elseif dtype == 'n' then
readData = nil
elseif dtype == 's' then
ccount = read_until(data, dataoffset, ':');
chrs = tonumber(ccount[1]);
stringlength = tonumber(ccount[2]);
dataoffset = dataoffset + chrs + 2;
readData = read_chrs(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') or (dtype == 'o') then
readdata = {}
if dtype == 'o' then
ccount = read_until(data, dataoffset, ':');
chrs = tonumber(ccount[1]);
stringlength = tonumber(ccount[2]);
dataoffset = dataoffset + chrs + 2;
readData = read_chrs(data, dataoffset, stringlength);
chrs = readData[1];
readdata['_silly_serialized_object'] = readData[2];
dataoffset = dataoffset + chrs + 2;
if ((chrs ~= stringlength) and (chrs ~= string.length(readdata.length))) then
error('SyntaxError', 'String length mismatch');
end
end
keyandchrs = read_until(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
print(dtype)
print(dataoffset)
error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
end
return {dtype, dataoffset - offset, typeconvert(readdata)};
end
return _unserialize((data .. ''), 1)[3];
end