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
|
# File 'lib/protocol_buffers/runtime/decoder.rb', line 8
def self.decode(io, message)
fields = message.fields
until io.eof?
tag_int = Varint.decode(io)
tag = tag_int >> 3
wire_type = tag_int & 0b111
field = fields[tag]
if field && wire_type != field.wire_type
raise(DecodeError, "incorrect wire type for tag: #{field.tag}")
end
case wire_type
when 0
value = Varint.decode(io)
when 1
value = io.read(8)
when 2
length = Varint.decode(io)
value = LimitedIO.new(io, length)
when 5
value = io.read(4)
when 3, 4
raise(DecodeError, "groups are deprecated and unsupported")
else
raise(DecodeError, "unknown wire type: #{wire_type}")
end
if field
deserialized = field.deserialize(value)
message.merge_field(tag, deserialized, field)
else
value = value.read if wire_type == 2
message.remember_unknown_field(tag_int, value)
end
end
unless message.valid?
raise(DecodeError, "invalid message")
end
return message
rescue TypeError, ArgumentError
raise(DecodeError, "error parsing message")
end
|