Class: ProtoBuffer::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuffer.rb

Defined Under Namespace

Classes: Param

Class Method Summary collapse

Class Method Details

.encode(structure, hash) ⇒ Object

Encoding



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
# File 'lib/protobuffer.rb', line 96

def self.encode(structure, hash)
	structure = reverse_structures[structure] if structure.is_a? Symbol
	
	hash.to_a.map do |pair|
		output = ''
		type, value = pair
		value = [value] unless value.is_a? Array
		key = type
		
		substructure = structure[type]
		substructure = substructure.first if substructure.is_a? Array
		
		if substructure.is_a? Param
			key = substructure.index
			substructure = substructure.type
		end
		
		if substructure.is_a?(Symbol) && !([:varint, :string, :boolean].include?(substructure))
			substructure = reverse_structures[substructure]
		end

		value.each do |arg|
			if substructure == :varint || arg.is_a?(Fixnum) || arg.is_a?(Bignum)
				output << (key*8).chr
				output << write_varint(arg.to_i)
				
			elsif substructure == :boolean || arg.is_a?(TrueClass) || arg.is_a?(FalseClass)
				output << (key*8).chr
				output << write_varint(arg ? 1 : 0)
				
			elsif substructure == :string || arg.is_a?(String)
				output << (key*8+2).chr
				write_string output, arg
				
			elsif substructure.is_a?(Hash) || substructure.is_a?(Symbol)
				if substructure.is_a? Symbol
					substructure = structures[substructure]
				end
				
				output << (key*8+2).chr
				write_string output, encode(substructure, arg)
			
			else
				puts "Unknown type: #{type}"
				return
			end
		end
		output
	end.sort{|a, b| a[0] <=> b[0]}.join('')
end

.method_missing(type, label = nil) ⇒ Object



215
216
217
218
# File 'lib/protobuffer.rb', line 215

def self.method_missing(type, label=nil)
	label ||= type if type.is_a? Symbol
	Param.new(type, label)
end

.parse(structure, data) ⇒ Object

Decoding



12
13
14
15
16
17
18
19
20
# File 'lib/protobuffer.rb', line 12

def self.parse(structure, data)
	data = StringIO.new(data)
	hash = {}
	structure = structures[structure] if structure.is_a? Symbol
	
	parse_field hash, data, structure until data.eof?
	
	hash
end

.parse_field(parent_args, data, structure) ⇒ Object



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
# File 'lib/protobuffer.rb', line 22

def self.parse_field(parent_args, data, structure)
	key = data.getc / 8
	label = nil
	value = nil
	
	type = substructure = structure[key]
	type = type.first if type.is_a? Array
	
	unless type
		puts "UNEXPECTED KEY: #{key} (expected one of #{structure.keys.join(', ')})"
		return
	end
	
	if type.is_a? Param
		label = type.label
		type = type.type
	end
	
	if type == :varint
		value = read_varint(data)
	
	elsif type == :boolean
		value = read_varint(data) == 1
	
	elsif type == :string
		value = read_string(data)
	
	elsif type.is_a?(Hash) || type.is_a?(Symbol)
		if type.is_a? Symbol
			label ||= type
			type = structures[type]
		end
		
		value = {}
		raw = StringIO.new(read_string(data))
		parse_field value, raw, type until raw.eof?
	
	else
		puts "Unknown type: #{type}"
		return
	end
	
	label ||= key
	if substructure.is_a? Array
		parent_args[label] ||= []
		parent_args[label] << value
	else
		puts "Overwritting a key!" if parent_args[key]
		parent_args[label] = value
	end
end

.read_string(io) ⇒ Object



88
89
90
# File 'lib/protobuffer.rb', line 88

def self.read_string(io)
	io.read read_varint(io)
end

.read_varint(io) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/protobuffer.rb', line 74

def self.read_varint(io)
	index = 0
	value = 0
	while true
		byte = io.getc
		if byte & 0x80 > 0
			value |= (byte & 0x7F) << index
			index += 7
		else
			return value | byte << index
		end
	end
end

.reverse_structuresObject



178
179
180
# File 'lib/protobuffer.rb', line 178

def self.reverse_structures
	@reverse_structures ||= {}
end

.structure(key, structure) ⇒ Object

Yay ugly



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
# File 'lib/protobuffer.rb', line 183

def self.structure(key, structure)
	structure.each_pair do |index, type|
		type = type.first if type.is_a? Array
		type.index = index if type.is_a? Param
	end
	
	structures[key] = structure
	
	reverse = {}
	structure.each_pair do |key2, type|
		reverse[key2] = type # so that indexes still work
		
		type2 = type
		type2 = type.first if type.is_a? Array
		
		label = key2
		if type2.is_a? Param
			if type2.label
				label = type2.label
			elsif type.type.is_a? Symbol
				label = type2.type
			end
		elsif type2.is_a?(Symbol) && !([:varint, :string, :boolean].include?(type2))
			label = type2
		end
		
		reverse[label] = type
	end
	
	reverse_structures[key] = reverse
end

.structuresObject



174
175
176
# File 'lib/protobuffer.rb', line 174

def self.structures
	@structures ||= {}
end

.write_string(io, string) ⇒ Object



156
157
158
# File 'lib/protobuffer.rb', line 156

def self.write_string(io, string)
	io << write_varint(string.size) << string
end

.write_varint(value) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/protobuffer.rb', line 147

def self.write_varint(value)
	bytes = ''
	while value > 0x7F
		bytes << ((value & 0x7F) | 0x80).chr
		value >>= 7
	end
	bytes << value.chr
end