-
Notifications
You must be signed in to change notification settings - Fork 8
/
dpt.parse.common.lua
182 lines (155 loc) · 4.81 KB
/
dpt.parse.common.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
-- Parse package
-- This package provides reusable parsing utilities for individual elements of messages
-- Package header
local master = diffusion or {}
if master.parseCommon ~= nil then
return master.parseCommon
end
local clientTypesByChar = master.const.clientTypesByChar
-- Decode the varint used by command serialiser
-- Takes a range containing the varint
-- Returns: a range containing the varint, a range excluding the varint, the
-- numeric value of the varint
local function varint( range )
local sum = 0
local idx = 0
local shift = 0
if range:len() == 1 then
local r = range:range( 0, 1 )
return r, range:range( 0, 0 ), r:uint()
end
while shift < 32 do
local byte = range:range( idx, 1 ):uint()
sum = bit32.bor( sum, bit32.lshift( bit32.band( byte, 0x7F ), shift ) )
idx = idx + 1
if bit32.band( byte, 0x80 ) == 0 then
break
end
shift = shift + 7;
end
if idx == range:len() then
return range:range( 0, idx ), range:range( 0, 0 ), sum
else
return range:range( 0, idx ), range:range( idx ), sum
end
end
-- Decode the varint used by command serialiser
-- Takes a range containing the varint
-- Returns: a range containing the varint, a range excluding the varint, the UInt64 value of the varint
local function varint64( range )
if range:len() == 1 then
local r = range:range( 0, 1 )
return r, range:range( 0, 0 ), UInt64.new( r:uint(), 0 )
end
local idx = 0
local shift = 0
local sum = UInt64.new( 0, 0 )
while shift < 64 do
local byte = range:range( idx, 1 ):uint()
sum = sum:bor( UInt64.new( bit32.band( byte, 0x7F ), 0 ):lshift( shift ) )
idx = idx + 1
if bit32.band( byte, 0x80 ) == 0 then
break
end
shift = shift + 7;
end
return range:range( 0, idx ), range:range( idx ), sum
end
local function lengthPrefixedString( range )
if range ~= nil then
local lengthRange, rRange, length = varint( range )
local fullLength = lengthRange:len() + length
if length == 0 then
return { range = range:range( 0, 0 ), fullRange = lengthRange, string = "", remaining = rRange }
end
local stringRange = rRange:range( 0, length )
if rRange:len() > length then
local remainingRange = rRange:range( length )
return { range = stringRange, remaining = remainingRange, fullRange = range( 0, fullLength ), string = stringRange:string() }
else
return { range = stringRange, fullRange = range( 0, fullLength ), string = stringRange:string() }
end
end
end
local function lengthPrefixedBytes( range )
if range ~= nil then
local lengthRange, rRange, length = varint( range )
local fullLength = lengthRange:len() + length
if length == 0 then
return { range = range:range( 0, 0 ), fullRange = lengthRange, remaining = rRange }
end
local bytesRange = rRange:range( 0, length )
if rRange:len() > length then
local remainingRange = rRange:range( length )
return { range = bytesRange, remaining = remainingRange, fullRange = range( 0, fullLength ) }
else
return { range = bytesRange, fullRange = range( 0, fullLength ) }
end
end
end
local function lookupClientTypeByChar( clientType )
local type = clientTypesByChar[clientType]
if type == nil then
return "Unknown client type"
else
return type
end
end
-- Parse a session ID that uses fixed length encoding
local function parseSessionId( tvb )
local serverIdentity = tvb( 0, 8 ):uint64()
local clientIdentity = tvb( 8, 8 ):uint64()
return {
serverIdentity = serverIdentity,
clientIdentity = clientIdentity,
range = tvb( 0, 16 ),
clientId = string.format(
"%s-%s",
string.upper( serverIdentity:tohex() ),
string.upper( clientIdentity:tohex() )
)
}
end
-- Parse a session ID that uses variable length encoding
-- Currently parses the correct length but not value
local function parseVarSessionId( tvb )
local serverIdentityRange, remaining, serverIdentity = varint64( tvb )
local clientIdentityRange, remaining, clientIdentity = varint64( remaining )
return {
serverIdentity = serverIdentity,
clientIdentity = clientIdentity,
range = tvb( 0, serverIdentityRange:len() + clientIdentityRange:len() ),
clientId = string.format(
"%s-%s",
string.upper( serverIdentity:tohex() ),
string.upper( clientIdentity:tohex() )
)
}, remaining
end
local function parseOptional( tvb, task, absentTask )
local optionRange = tvb:range( 0, 1 )
local option = optionRange:int()
if option == 0x00 then
if absentTask ~= nil then
absentTask( tvb:range( 1 ) )
else
return {
remaining = tvb:range( 1 )
}
end
else
return task( tvb:range( 1 ) )
end
end
-- Package footer
master.parseCommon = {
varint = varint,
lengthPrefixedString = lengthPrefixedString,
lengthPrefixedBytes = lengthPrefixedBytes,
lookupClientTypeByChar = lookupClientTypeByChar,
parseSessionId = parseSessionId,
parseVarSessionId = parseVarSessionId,
parseOptional = parseOptional
}
diffusion = master
return master.parseCommon