forked from sean-lin/protoc-gen-lua
-
Notifications
You must be signed in to change notification settings - Fork 57
/
decoder.lua
337 lines (307 loc) · 11.3 KB
/
decoder.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
--
--------------------------------------------------------------------------------
-- FILE: decoder.lua
-- DESCRIPTION: protoc-gen-lua
-- Google's Protocol Buffers project, ported to lua.
-- https://code.google.com/p/protoc-gen-lua/
--
-- Copyright (c) 2010 , 林卓毅 (Zhuoyi Lin) netsnail@gmail.com
-- All rights reserved.
--
-- Use, modification and distribution are subject to the "New BSD License"
-- as listed at <url: http://www.opensource.org/licenses/bsd-license.php >.
--
-- COMPANY: NetEase
-- CREATED: 2010年07月29日 19时30分51秒 CST
--------------------------------------------------------------------------------
--
local string = string
local table = table
local assert = assert
local ipairs = ipairs
local error = error
local print = print
local pb = require "pb"
local encoder = require "protobuf/encoder"
local wire_format = require "protobuf/wire_format"
module "protobuf/decoder"
local _DecodeVarint = pb.varint_decoder
local _DecodeSignedVarint = pb.signed_varint_decoder
local _DecodeVarint32 = pb.varint_decoder
local _DecodeSignedVarint32 = pb.signed_varint_decoder
ReadTag = pb.read_tag
local function _SimpleDecoder(wire_type, decode_value)
return function(field_number, is_repeated, is_packed, key, new_default)
if is_packed then
local DecodeVarint = _DecodeVarint
return function (buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
local endpoint
endpoint, pos = DecodeVarint(buffer, pos)
endpoint = endpoint + pos
if endpoint > pend then
error('Truncated message.')
end
local element
while pos < endpoint do
element, pos = decode_value(buffer, pos)
value[#value + 1] = element
end
if pos > endpoint then
value:remove(#value)
error('Packed element was truncated.')
end
return pos
end
elseif is_repeated then
local tag_bytes = encoder.TagBytes(field_number, wire_type)
local tag_len = #tag_bytes
local sub = string.sub
return function(buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
while 1 do
local element, new_pos = decode_value(buffer, pos)
value:append(element)
pos = new_pos + tag_len
if sub(buffer, new_pos+1, pos) ~= tag_bytes or new_pos >= pend then
if new_pos > pend then
error('Truncated message.')
end
return new_pos
end
end
end
else
return function (buffer, pos, pend, message, field_dict)
field_dict[key], pos = decode_value(buffer, pos)
if pos > pend then
field_dict[key] = nil
error('Truncated message.')
end
return pos
end
end
end
end
local function _ModifiedDecoder(wire_type, decode_value, modify_value)
local InnerDecode = function (buffer, pos)
local result, new_pos = decode_value(buffer, pos)
return modify_value(result), new_pos
end
return _SimpleDecoder(wire_type, InnerDecode)
end
local function _StructPackDecoder(wire_type, value_size, format)
local struct_unpack = pb.struct_unpack
function InnerDecode(buffer, pos)
local new_pos = pos + value_size
local result = struct_unpack(format, buffer, pos)
return result, new_pos
end
return _SimpleDecoder(wire_type, InnerDecode)
end
local function _Boolean(value)
return value ~= 0
end
Int32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint32)
EnumDecoder = Int32Decoder
Int64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint)
UInt32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32)
UInt64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint)
SInt32Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32, wire_format.ZigZagDecode32)
SInt64Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, wire_format.ZigZagDecode64)
Fixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I'))
Fixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q'))
SFixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i'))
SFixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q'))
FloatDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f'))
DoubleDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d'))
BoolDecoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, _Boolean)
function StringDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
-- local unicode = unicode
assert(not is_packed)
if is_repeated then
local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local tag_len = #tag_bytes
return function (buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
while 1 do
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated string.')
end
value:append(sub(buffer, pos+1, new_pos))
pos = new_pos + tag_len
if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then
return new_pos
end
end
end
else
return function (buffer, pos, pend, message, field_dict)
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated string.')
end
field_dict[key] = sub(buffer, pos + 1, new_pos)
return new_pos
end
end
end
function BytesDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
assert(not is_packed)
if is_repeated then
local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local tag_len = #tag_bytes
return function (buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
while 1 do
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated string.')
end
value:append(sub(buffer, pos + 1, new_pos))
pos = new_pos + tag_len
if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then
return new_pos
end
end
end
else
return function(buffer, pos, pend, message, field_dict)
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated string.')
end
field_dict[key] = sub(buffer, pos + 1, new_pos)
return new_pos
end
end
end
function MessageDecoder(field_number, is_repeated, is_packed, key, new_default)
local DecodeVarint = _DecodeVarint
local sub = string.sub
assert(not is_packed)
if is_repeated then
local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local tag_len = #tag_bytes
return function (buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
while 1 do
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated message.')
end
if value:add():_InternalParse(buffer, pos, new_pos) ~= new_pos then
error('Unexpected end-group tag.')
end
pos = new_pos + tag_len
if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then
return new_pos
end
end
end
else
return function (buffer, pos, pend, message, field_dict)
local value = field_dict[key]
if value == nil then
value = new_default(message)
field_dict[key] = value
end
local size, new_pos
size, pos = DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > pend then
error('Truncated message.')
end
if value:_InternalParse(buffer, pos, new_pos) ~= new_pos then
error('Unexpected end-group tag.')
end
return new_pos
end
end
end
function _SkipVarint(buffer, pos, pend)
local value
value, pos = _DecodeVarint(buffer, pos)
return pos
end
function _SkipFixed64(buffer, pos, pend)
pos = pos + 8
if pos > pend then
error('Truncated message.')
end
return pos
end
function _SkipLengthDelimited(buffer, pos, pend)
local size
size, pos = _DecodeVarint(buffer, pos)
pos = pos + size
if pos > pend then
error('Truncated message.')
end
return pos
end
function _SkipFixed32(buffer, pos, pend)
pos = pos + 4
if pos > pend then
error('Truncated message.')
end
return pos
end
function _RaiseInvalidWireType(buffer, pos, pend)
error('Tag had invalid wire type.')
end
function _FieldSkipper()
WIRETYPE_TO_SKIPPER = {
_SkipVarint,
_SkipFixed64,
_SkipLengthDelimited,
_SkipGroup,
_EndGroup,
_SkipFixed32,
_RaiseInvalidWireType,
_RaiseInvalidWireType,
}
-- wiretype_mask = wire_format.TAG_TYPE_MASK
local ord = string.byte
local sub = string.sub
return function (buffer, pos, pend, tag_bytes)
local wire_type = ord(sub(tag_bytes, 1, 1)) % 8 + 1
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, pend)
end
end
SkipField = _FieldSkipper()