-
Notifications
You must be signed in to change notification settings - Fork 22
/
serializer.rb
313 lines (272 loc) · 8.94 KB
/
serializer.rb
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
require 'date'
require 'rexml/document'
module AMF
module Pure
module Serializer
class State
def self.from_state(opts)
opts ||= new
end
def initialize(opts = {})
@dynamic = false
# integer cache and float cache are used to avoid
# processing the numbers multiple times
@integer_cache = {}
@float_cache = {}
# string and object cache are used as reference
# tables which are further discussed in the AMF specs
@string_cache = {}
@object_cache = {}
end
attr_accessor :dynamic,
:integer_cache,
:float_cache,
:string_cache,
:object_cache
# if string has been referenced, returns the index of the reference
# in the implicit string reference tabel. If no reference is found
# sets the reference to the next index in the implicit strings table
# and returns nil
def string_cache(str)
index = @string_cache.fetch(str) { |str|
@string_cache[str] = @string_cache.length
nil
}
header_for_cache(index) if index
end
# if object has been referenced, returns the index of the reference
# in the implicit object reference table. If no reference is found
# sets the reference to the next index in the implicit objects table
# and returns nil.
def object_cache(obj)
index = @object_cache.fetch(obj.amf_id) { |amf_id|
@object_cache[amf_id] = @object_cache.length
nil
}
header_for_cache(index) if index
end
def header_for_cache(index)
header = index << 1 # shift value left to leave a low bit of 0
AMF.pack_integer(header, self)
end
end
module SerializerMethods
module NilClass
def to_amf(*)
'' << NULL_MARKER
end
end
module FalseClass
def to_amf(*)
'' << FALSE_MARKER
end
end
module TrueClass
def to_amf(*)
'' << TRUE_MARKER
end
end
module Bignum
def to_amf(state = nil, *)
self.to_f.to_amf(state)
end
end
module Integer
def to_amf(state = nil, *)
if self >= MIN_INTEGER && self <= MAX_INTEGER #check valid range for 29 bits
write_integer(state)
else #overflow to a double
self.to_f.to_amf(state)
end
end
protected
def write_integer(state = nil)
output = ''
output << INTEGER_MARKER
output << AMF.pack_integer(self)
end
end
module Float
def to_amf(state = nil, *)
output = ''
output << DOUBLE_MARKER
output << AMF.pack_double(self, state)
end
end
module String
def to_amf(state = nil, *)
output = ''
output << STRING_MARKER
output << write_string(state)
end
def write_string(state = nil)
output = ''
if self == ''
output << EMPTY_STRING
elsif state && (cache_header = state.string_cache(self))
output << cache_header
else
output << header_for_string(state)
output << self
end
end
private
def header_for_string(state = nil)
header = self.length << 1 # make room for a low bit of 1
header = header | 1 # set the low bit to 1
AMF.pack_integer(header, state)
end
end
module Symbol
def to_amf(state = nil, *)
self.to_s.to_amf(state)
end
end
module Array
def to_amf(state = nil, *)
output = ''
output << ARRAY_MARKER
state = SerializerState.from_state(state)
if cache_header = state.object_cache(self)
output << cache_header
else
output << header_for_array(state)
# AMF only encodes strict, dense arrays by the AMF spec
# so the dynamic portion is empty
output << CLOSE_DYNAMIC_ARRAY
self.each do |val|
output << val.to_amf(state)
end
end
output
end
private
def header_for_array(state = nil)
header = self.length << 1 # make room for a low bit of 1
header = header | 1 # set the low bit to 1
AMF.pack_integer(header, state)
end
end
module Object
def to_amf(state = nil, *)
output = ''
output << OBJECT_MARKER
state = SerializerState.from_state(state)
if cache_header = state.object_cache(self)
output << cache_header
else
if state && !state.dynamic
state.dynamic = true
output << DYNAMIC_OBJECT
end
output << ANONYMOUS_OBJECT
output << serialize_properties(state)
output << CLOSE_DYNAMIC_OBJECT
end
end
protected
def amf_id
object_id
end
private
# unmapped object
#OPTIMIZE: keep a hash of classes that come through here
# and store in a hash keyed by obj.class
# if the obj.class is in the hash, loop over the hash of
# public methods
# find all public methods belonging to this object alone
def serialize_properties(state = nil)
output = ''
self.public_methods(false).each do |method_name|
# and write them to the stream if they take no arguments
method_def = self.method(method_name)
if method_def.arity == 0
output << method_name.to_s.write_string(state)
output << self.send(method_name).to_amf(state)
end
end
output
end
end
module Hash
private
def serialize_properties(state = nil)
output = ''
self.each do |key, value|
output << key.to_s.write_string(state) # easy for both string and symbol keys
output << value.to_amf(state)
end
output
end
end
module Time
def to_amf(state = nil, *)
output = ''
output << DATE_MARKER
self.utc unless self.utc?
seconds = (self.to_f * 1000).to_i
if state && (cache_header = state.object_cache(self))
output << cache_header
else
output << AMF.pack_integer(NULL_MARKER)
output << AMF.pack_double(seconds, state)
end
end
end
module Date
def to_amf(state = nil, *)
output = ''
output << DATE_MARKER
seconds = ((self.strftime("%s").to_i) * 1000).to_i
if state && (cache_header = state.object_cache(self))
output << cache_header
else
output << AMF.pack_integer(NULL_MARKER)
output << AMF.pack_double(seconds, state)
end
end
end
module REXML
class Document
def to_amf(state = nil, *)
AMF.write_xml(self, state)
end
end
end
end
end
end
module_function
def pack_integer(integer, state = nil)
if state
state.integer_cache[integer] ||= pack_integer_helper(integer)
else
pack_integer_helper(integer)
end
end
def pack_integer_helper(integer)
integer = integer & 0x1fffffff
if(integer < 0x80)
[integer].pack('c')
elsif(integer < 0x4000)
[integer >> 7 & 0x7f | 0x80].pack('c')+
[integer & 0x7f].pack('c')
elsif(integer < 0x200000)
[integer >> 14 & 0x7f | 0x80].pack('c') +
[integer >> 7 & 0x7f | 0x80].pack('c') +
[integer & 0x7f].pack('c')
else
[integer >> 22 & 0x7f | 0x80].pack('c')+
[integer >> 15 & 0x7f | 0x80].pack('c')+
[integer >> 8 & 0x7f | 0x80].pack('c')+
[integer & 0xff].pack('c')
end
end
def pack_double(double, state = nil)
if state
(state.float_cache[double] ||= [double].pack('G'))
else
[double].pack('G')
end
end
end