-
Notifications
You must be signed in to change notification settings - Fork 253
/
sprotoparser.lua
526 lines (476 loc) · 12 KB
/
sprotoparser.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
local lpeg = require "lpeg"
local table = require "table"
local packbytes
local packvalue
local version = _VERSION:match "5.*"
if version and tonumber(version) >= 5.3 then
function packbytes(str)
return string.pack("<s4",str)
end
function packvalue(id)
id = (id + 1) * 2
return string.pack("<I2",id)
end
else
function packbytes(str)
local size = #str
local a = size % 256
size = math.floor(size / 256)
local b = size % 256
size = math.floor(size / 256)
local c = size % 256
size = math.floor(size / 256)
local d = size
return string.char(a)..string.char(b)..string.char(c)..string.char(d) .. str
end
function packvalue(id)
id = (id + 1) * 2
assert(id >=0 and id < 65536)
local a = id % 256
local b = math.floor(id / 256)
return string.char(a) .. string.char(b)
end
end
local P = lpeg.P
local S = lpeg.S
local R = lpeg.R
local C = lpeg.C
local Ct = lpeg.Ct
local Cg = lpeg.Cg
local Cc = lpeg.Cc
local V = lpeg.V
local function count_lines(_,pos, parser_state)
if parser_state.pos < pos then
parser_state.line = parser_state.line + 1
parser_state.pos = pos
end
return pos
end
local exception = lpeg.Cmt( lpeg.Carg(1) , function ( _ , pos, parser_state)
error(string.format("syntax error at [%s] line (%d)", parser_state.file or "", parser_state.line))
return pos
end)
local eof = P(-1)
local newline = lpeg.Cmt((P"\n" + "\r\n") * lpeg.Carg(1) ,count_lines)
local line_comment = "#" * (1 - newline) ^0 * (newline + eof)
local blank = S" \t" + newline + line_comment
local blank0 = blank ^ 0
local blanks = blank ^ 1
local alpha = R"az" + R"AZ" + "_"
local alnum = alpha + R"09"
local word = alpha * alnum ^ 0
local name = C(word)
local typename = C(word * ("." * word) ^ 0)
local tag = R"09" ^ 1 / tonumber
local mainkey = "(" * blank0 * C((word ^ 0)) * blank0 * ")"
local decimal = "(" * blank0 * C(tag) * blank0 * ")"
local function multipat(pat)
return Ct(blank0 * (pat * blanks) ^ 0 * pat^0 * blank0)
end
local function namedpat(name, pat)
return Ct(Cg(Cc(name), "type") * Cg(pat))
end
local typedef = P {
"ALL",
FIELD = namedpat("field", name * blanks * tag * blank0 * ":" * blank0 * (C"*")^-1 * typename * (mainkey + decimal)^0),
STRUCT = P"{" * multipat(V"FIELD" + V"TYPE") * P"}",
TYPE = namedpat("type", P"." * name * blank0 * V"STRUCT" ),
SUBPROTO = Ct((C"request" + C"response") * blanks * (typename + V"STRUCT")),
PROTOCOL = namedpat("protocol", name * blanks * tag * blank0 * P"{" * multipat(V"SUBPROTO") * P"}"),
ALL = multipat(V"TYPE" + V"PROTOCOL"),
}
local proto = blank0 * typedef * blank0
local convert = {}
function convert.protocol(all, obj)
local result = { tag = obj[2] }
for _, p in ipairs(obj[3]) do
local pt = p[1]
if result[pt] ~= nil then
error(string.format("redefine %s in protocol %s", pt, obj[1]))
end
local typename = p[2]
if type(typename) == "table" then
local struct = typename
typename = obj[1] .. "." .. p[1]
all.type[typename] = convert.type(all, { typename, struct })
end
if typename == "nil" then
if p[1] == "response" then
result.confirm = true
end
else
result[p[1]] = typename
end
end
return result
end
local map_keytypes = {
integer = true,
string = true,
}
function convert.type(all, obj)
local result = {}
local typename = obj[1]
local tags = {}
local names = {}
for _, f in ipairs(obj[2]) do
if f.type == "field" then
local name = f[1]
if names[name] then
error(string.format("redefine %s in type %s", name, typename))
end
names[name] = true
local tag = f[2]
if tags[tag] then
error(string.format("redefine tag %d in type %s", tag, typename))
end
tags[tag] = true
local field = { name = name, tag = tag }
table.insert(result, field)
local fieldtype = f[3]
if fieldtype == "*" then
field.array = true
fieldtype = f[4]
end
local mainkey = f[5]
if mainkey then
if fieldtype == "integer" then
field.decimal = mainkey
else
assert(field.array)
field.key = mainkey
end
end
field.typename = fieldtype
else
assert(f.type == "type") -- nest type
local nesttypename = typename .. "." .. f[1]
f[1] = nesttypename
assert(all.type[nesttypename] == nil, "redefined " .. nesttypename)
all.type[nesttypename] = convert.type(all, f)
end
end
table.sort(result, function(a,b) return a.tag < b.tag end)
return result
end
local function adjust(r)
local result = { type = {} , protocol = {} }
for _, obj in ipairs(r) do
local set = result[obj.type]
local name = obj[1]
assert(set[name] == nil , "redefined " .. name)
set[name] = convert[obj.type](result,obj)
end
return result
end
local buildin_types = {
integer = 0,
boolean = 1,
string = 2,
binary = 2, -- binary is a sub type of string
double = 3,
}
local function checktype(types, ptype, t)
if buildin_types[t] then
return t
end
local fullname = ptype .. "." .. t
if types[fullname] then
return fullname
else
ptype = ptype:match "(.+)%..+$"
if ptype then
return checktype(types, ptype, t)
elseif types[t] then
return t
end
end
end
local function check_protocol(r)
local map = {}
local type = r.type
for name, v in pairs(r.protocol) do
local tag = v.tag
local request = v.request
local response = v.response
local p = map[tag]
if p then
error(string.format("redefined protocol tag %d at %s", tag, name))
end
if request and not type[request] then
error(string.format("Undefined request type %s in protocol %s", request, name))
end
if response and not type[response] then
error(string.format("Undefined response type %s in protocol %s", response, name))
end
map[tag] = v
end
return r
end
local function flattypename(r)
for typename, t in pairs(r.type) do
for _, f in pairs(t) do
local ftype = f.typename
local fullname = checktype(r.type, typename, ftype)
if fullname == nil then
error(string.format("Undefined type %s in type %s", ftype, typename))
end
f.typename = fullname
end
end
return r
end
local function parser(text,filename)
local state = { file = filename, pos = 0, line = 1 }
local r = lpeg.match(proto * -1 + exception , text , 1, state )
return flattypename(check_protocol(adjust(r)))
end
--[[
-- The protocol of sproto
.type {
.field {
name 0 : string
buildin 1 : integer
type 2 : integer
tag 3 : integer
array 4 : boolean
key 5 : integer # If key exists, array must be true
map 6 : boolean # Interpret two fields struct as map when decoding
}
name 0 : string
fields 1 : *field
}
.protocol {
name 0 : string
tag 1 : integer
request 2 : integer # index
response 3 : integer # index
confirm 4 : boolean # true means response nil
}
.group {
type 0 : *type
protocol 1 : *protocol
}
]]
local function packfield(f)
local strtbl = {}
if f.array then
if f.key then
if f.map then
table.insert(strtbl, "\7\0") -- 7 fields
else
table.insert(strtbl, "\6\0") -- 6 fields
end
else
table.insert(strtbl, "\5\0") -- 5 fields
end
else
table.insert(strtbl, "\4\0") -- 4 fields
end
table.insert(strtbl, "\0\0") -- name (tag = 0, ref an object)
if f.buildin then
table.insert(strtbl, packvalue(f.buildin)) -- buildin (tag = 1)
if f.extra then
table.insert(strtbl, packvalue(f.extra)) -- f.buildin can be integer or string
else
table.insert(strtbl, "\1\0") -- skip (tag = 2)
end
table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3)
else
table.insert(strtbl, "\1\0") -- skip (tag = 1)
table.insert(strtbl, packvalue(f.type)) -- type (tag = 2)
table.insert(strtbl, packvalue(f.tag)) -- tag (tag = 3)
end
if f.array then
table.insert(strtbl, packvalue(1)) -- array = true (tag = 4)
if f.key then
table.insert(strtbl, packvalue(f.key)) -- key tag (tag = 5)
if f.map then
table.insert(strtbl, packvalue(f.map)) -- map tag (tag = 6)
end
end
end
table.insert(strtbl, packbytes(f.name)) -- external object (name)
return packbytes(table.concat(strtbl))
end
local function packtype(name, t, alltypes)
local fields = {}
local tmp = {}
for _, f in ipairs(t) do
tmp.array = f.array
tmp.name = f.name
tmp.tag = f.tag
tmp.extra = f.decimal
tmp.buildin = buildin_types[f.typename]
if f.typename == "binary" then
tmp.extra = 1 -- binary is sub type of string
end
local subtype
if not tmp.buildin then
subtype = assert(alltypes[f.typename])
tmp.type = subtype.id
else
tmp.type = nil
end
tmp.map = nil
if f.key then
assert(f.array)
if f.key == "" then
tmp.map = 1
local c = 0
local min_t = math.maxinteger
for n, t in pairs(subtype.fields) do
c = c + 1
if t.tag < min_t then
min_t = t.tag
f.key = n
end
end
if c ~= 2 then
error(string.format("Invalid map definition: %s, must only have two fields", tmp.name))
end
end
local stfield = subtype.fields[f.key]
if not stfield or not stfield.buildin then
error("Invalid map index :" .. f.key)
end
tmp.key = stfield.tag
else
tmp.key = nil
end
table.insert(fields, packfield(tmp))
end
local data
if #fields == 0 then
data = {
"\1\0", -- 1 fields
"\0\0", -- name (id = 0, ref = 0)
packbytes(name),
}
else
data = {
"\2\0", -- 2 fields
"\0\0", -- name (tag = 0, ref = 0)
"\0\0", -- field[] (tag = 1, ref = 1)
packbytes(name),
packbytes(table.concat(fields)),
}
end
return packbytes(table.concat(data))
end
local function packproto(name, p, alltypes)
if p.request then
local request = alltypes[p.request]
if request == nil then
error(string.format("Protocol %s request type %s not found", name, p.request))
end
request = request.id
end
local tmp = {
"\4\0", -- 4 fields
"\0\0", -- name (id=0, ref=0)
packvalue(p.tag), -- tag (tag=1)
}
if p.request == nil and p.response == nil and p.confirm == nil then
tmp[1] = "\2\0" -- only two fields
else
if p.request then
table.insert(tmp, packvalue(alltypes[p.request].id)) -- request typename (tag=2)
else
table.insert(tmp, "\1\0") -- skip this field (request)
end
if p.response then
table.insert(tmp, packvalue(alltypes[p.response].id)) -- request typename (tag=3)
elseif p.confirm then
tmp[1] = "\5\0" -- add confirm field
table.insert(tmp, "\1\0") -- skip this field (response)
table.insert(tmp, packvalue(1)) -- confirm = true
else
tmp[1] = "\3\0" -- only three fields
end
end
table.insert(tmp, packbytes(name))
return packbytes(table.concat(tmp))
end
local function packgroup(t,p)
if next(t) == nil then
assert(next(p) == nil)
return "\0\0"
end
local tt, tp
local alltypes = {}
for name in pairs(t) do
table.insert(alltypes, name)
end
table.sort(alltypes) -- make result stable
for idx, name in ipairs(alltypes) do
local fields = {}
for _, type_fields in ipairs(t[name]) do
fields[type_fields.name] = {
tag = type_fields.tag,
buildin = buildin_types[type_fields.typename]
}
end
alltypes[name] = { id = idx - 1, fields = fields }
end
tt = {}
for _,name in ipairs(alltypes) do
table.insert(tt, packtype(name, t[name], alltypes))
end
tt = packbytes(table.concat(tt))
if next(p) then
local tmp = {}
for name, tbl in pairs(p) do
table.insert(tmp, tbl)
tbl.name = name
end
table.sort(tmp, function(a,b) return a.tag < b.tag end)
tp = {}
for _, tbl in ipairs(tmp) do
table.insert(tp, packproto(tbl.name, tbl, alltypes))
end
tp = packbytes(table.concat(tp))
end
local result
if tp == nil then
result = {
"\1\0", -- 1 field
"\0\0", -- type[] (id = 0, ref = 0)
tt,
}
else
result = {
"\2\0", -- 2fields
"\0\0", -- type array (id = 0, ref = 0)
"\0\0", -- protocol array (id = 1, ref =1)
tt,
tp,
}
end
return table.concat(result)
end
local function encodeall(r)
return packgroup(r.type, r.protocol)
end
local sparser = {}
function sparser.dump(str)
local tmp = ""
for i=1,#str do
tmp = tmp .. string.format("%02X ", string.byte(str,i))
if i % 8 == 0 then
if i % 16 == 0 then
print(tmp)
tmp = ""
else
tmp = tmp .. "- "
end
end
end
print(tmp)
end
function sparser.parse(text, name)
local r = parser(text, name or "=text")
local data = encodeall(r)
return data
end
return sparser