ruby-php-serialization 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,336 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.4.12
4
+ # from Racc grammer file "".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+ require 'php_serialization/tokenizer'
10
+
11
+ module PhpSerialization
12
+ class Unserializer < Racc::Parser
13
+
14
+ module_eval(<<'...end unserializer.y/module_eval...', 'unserializer.y', 81)
15
+ def initialize(tokenizer_klass = Tokenizer)
16
+ @tokenizer_klass = tokenizer_klass
17
+ end
18
+
19
+ def run(string)
20
+ @tokenizer = @tokenizer_klass.new(string)
21
+ yyparse(@tokenizer, :each)
22
+ return @object
23
+ ensure
24
+ @tokenizer = nil
25
+ end
26
+
27
+ def next_token
28
+ @tokenizer.next_token
29
+ end
30
+ ...end unserializer.y/module_eval...
31
+ ##### State transition tables begin ###
32
+
33
+ racc_action_table = [
34
+ 9, 10, 19, 20, 11, 12, 13, 21, 14, 9,
35
+ 10, 15, 22, 11, 12, 13, 23, 14, 9, 10,
36
+ 15, 24, 11, 12, 13, 25, 14, 26, 46, 15,
37
+ 9, 10, 27, 28, 11, 12, 13, 29, 14, 30,
38
+ 51, 15, 18, 32, 33, 34, 35, 36, 37, 38,
39
+ 39, 40, 41, 43, 17, 47, 16, 49, 31 ]
40
+
41
+ racc_action_check = [
42
+ 0, 0, 4, 5, 0, 0, 0, 6, 0, 45,
43
+ 45, 0, 10, 45, 45, 45, 11, 45, 42, 42,
44
+ 45, 12, 42, 42, 42, 13, 42, 14, 42, 42,
45
+ 50, 50, 15, 16, 50, 50, 50, 22, 50, 23,
46
+ 50, 50, 3, 25, 26, 27, 32, 33, 34, 35,
47
+ 36, 37, 39, 41, 2, 43, 1, 47, 24 ]
48
+
49
+ racc_action_pointer = [
50
+ -3, 56, 52, 40, 0, 1, 5, nil, nil, nil,
51
+ 7, 11, 16, 20, 22, 27, 33, nil, nil, nil,
52
+ nil, nil, 31, 33, 52, 37, 38, 39, nil, nil,
53
+ nil, nil, 41, 42, 43, 39, 40, 39, nil, 47,
54
+ nil, 47, 15, 50, nil, 6, nil, 45, nil, nil,
55
+ 27, nil ]
56
+
57
+ racc_action_default = [
58
+ -18, -18, -18, -18, -18, -18, -18, -6, -7, -8,
59
+ -18, -18, -18, -18, -18, -18, -18, -1, -2, -3,
60
+ -4, -5, -18, -18, -18, -18, -18, -18, 52, -9,
61
+ -10, -11, -18, -18, -18, -18, -18, -18, -12, -18,
62
+ -15, -18, -18, -18, -14, -18, -17, -18, -16, -15,
63
+ -18, -13 ]
64
+
65
+ racc_goto_table = [
66
+ 1, 42, nil, nil, nil, nil, nil, nil, nil, nil,
67
+ 50, nil, nil, nil, nil, nil, nil, nil, nil, nil,
68
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
69
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
70
+ nil, nil, nil, nil, nil, 48 ]
71
+
72
+ racc_goto_check = [
73
+ 1, 9, nil, nil, nil, nil, nil, nil, nil, nil,
74
+ 9, nil, nil, nil, nil, nil, nil, nil, nil, nil,
75
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
76
+ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
77
+ nil, nil, nil, nil, nil, 1 ]
78
+
79
+ racc_goto_pointer = [
80
+ nil, 0, nil, nil, nil, nil, nil, nil, nil, -39,
81
+ nil ]
82
+
83
+ racc_goto_default = [
84
+ nil, 45, 2, 3, 4, 5, 6, 7, 8, nil,
85
+ 44 ]
86
+
87
+ racc_reduce_table = [
88
+ 0, 0, :racc_error,
89
+ 2, 16, :_reduce_1,
90
+ 2, 16, :_reduce_2,
91
+ 2, 16, :_reduce_3,
92
+ 2, 16, :_reduce_4,
93
+ 2, 16, :_reduce_5,
94
+ 1, 16, :_reduce_6,
95
+ 1, 16, :_reduce_7,
96
+ 1, 17, :_reduce_8,
97
+ 3, 18, :_reduce_9,
98
+ 3, 19, :_reduce_10,
99
+ 3, 20, :_reduce_11,
100
+ 5, 21, :_reduce_12,
101
+ 11, 23, :_reduce_13,
102
+ 2, 24, :_reduce_14,
103
+ 0, 24, :_reduce_15,
104
+ 2, 25, :_reduce_16,
105
+ 7, 22, :_reduce_17 ]
106
+
107
+ racc_reduce_n = 18
108
+
109
+ racc_shift_n = 52
110
+
111
+ racc_token_table = {
112
+ false => 0,
113
+ :error => 1,
114
+ ";" => 2,
115
+ "N" => 3,
116
+ "b" => 4,
117
+ ":" => 5,
118
+ :NUMBER => 6,
119
+ "i" => 7,
120
+ "d" => 8,
121
+ "s" => 9,
122
+ :STRING => 10,
123
+ "O" => 11,
124
+ "{" => 12,
125
+ "}" => 13,
126
+ "a" => 14 }
127
+
128
+ racc_nt_base = 15
129
+
130
+ racc_use_result_var = true
131
+
132
+ Racc_arg = [
133
+ racc_action_table,
134
+ racc_action_check,
135
+ racc_action_default,
136
+ racc_action_pointer,
137
+ racc_goto_table,
138
+ racc_goto_check,
139
+ racc_goto_default,
140
+ racc_goto_pointer,
141
+ racc_nt_base,
142
+ racc_reduce_table,
143
+ racc_token_table,
144
+ racc_shift_n,
145
+ racc_reduce_n,
146
+ racc_use_result_var ]
147
+
148
+ Racc_token_to_s_table = [
149
+ "$end",
150
+ "error",
151
+ "\";\"",
152
+ "\"N\"",
153
+ "\"b\"",
154
+ "\":\"",
155
+ "NUMBER",
156
+ "\"i\"",
157
+ "\"d\"",
158
+ "\"s\"",
159
+ "STRING",
160
+ "\"O\"",
161
+ "\"{\"",
162
+ "\"}\"",
163
+ "\"a\"",
164
+ "$start",
165
+ "data",
166
+ "null",
167
+ "bool",
168
+ "integer",
169
+ "double",
170
+ "string",
171
+ "assoc_array",
172
+ "object",
173
+ "attribute_list",
174
+ "attribute" ]
175
+
176
+ Racc_debug_parser = false
177
+
178
+ ##### State transition tables end #####
179
+
180
+ # reduce 0 omitted
181
+
182
+ module_eval(<<'.,.,', 'unserializer.y', 3)
183
+ def _reduce_1(val, _values, result)
184
+ @object = val[0]
185
+ result
186
+ end
187
+ .,.,
188
+
189
+ module_eval(<<'.,.,', 'unserializer.y', 4)
190
+ def _reduce_2(val, _values, result)
191
+ @object = val[0]
192
+ result
193
+ end
194
+ .,.,
195
+
196
+ module_eval(<<'.,.,', 'unserializer.y', 5)
197
+ def _reduce_3(val, _values, result)
198
+ @object = val[0]
199
+ result
200
+ end
201
+ .,.,
202
+
203
+ module_eval(<<'.,.,', 'unserializer.y', 6)
204
+ def _reduce_4(val, _values, result)
205
+ @object = val[0]
206
+ result
207
+ end
208
+ .,.,
209
+
210
+ module_eval(<<'.,.,', 'unserializer.y', 7)
211
+ def _reduce_5(val, _values, result)
212
+ @object = val[0]
213
+ result
214
+ end
215
+ .,.,
216
+
217
+ module_eval(<<'.,.,', 'unserializer.y', 8)
218
+ def _reduce_6(val, _values, result)
219
+ @object = val[0]
220
+ result
221
+ end
222
+ .,.,
223
+
224
+ module_eval(<<'.,.,', 'unserializer.y', 9)
225
+ def _reduce_7(val, _values, result)
226
+ @object = val[0]
227
+ result
228
+ end
229
+ .,.,
230
+
231
+ module_eval(<<'.,.,', 'unserializer.y', 12)
232
+ def _reduce_8(val, _values, result)
233
+ result = nil
234
+ result
235
+ end
236
+ .,.,
237
+
238
+ module_eval(<<'.,.,', 'unserializer.y', 15)
239
+ def _reduce_9(val, _values, result)
240
+ result = Integer(val[2]) > 0
241
+ result
242
+ end
243
+ .,.,
244
+
245
+ module_eval(<<'.,.,', 'unserializer.y', 18)
246
+ def _reduce_10(val, _values, result)
247
+ result = Integer(val[2])
248
+ result
249
+ end
250
+ .,.,
251
+
252
+ module_eval(<<'.,.,', 'unserializer.y', 21)
253
+ def _reduce_11(val, _values, result)
254
+ result = Float(val[2])
255
+ result
256
+ end
257
+ .,.,
258
+
259
+ module_eval(<<'.,.,', 'unserializer.y', 24)
260
+ def _reduce_12(val, _values, result)
261
+ result = val[4]
262
+ result
263
+ end
264
+ .,.,
265
+
266
+ module_eval(<<'.,.,', 'unserializer.y', 29)
267
+ def _reduce_13(val, _values, result)
268
+ if eval("defined?(#{val[4]})")
269
+ result = Object.const_get(val[4]).new
270
+
271
+ val[9].each do |(attr_name, value)|
272
+ # Protected and private attributes will have a \0..\0 prefix
273
+ attr_name = attr_name.gsub(/\A\\0[^\\]+\\0/, '')
274
+ result.instance_variable_set("@#{attr_name}", value)
275
+ end
276
+ else
277
+ klass_name = val[4].gsub(/^Struct::/, '')
278
+ attr_names, values = [], []
279
+
280
+ val[9].each do |(attr_name, value)|
281
+ # Protected and private attributes will have a \0..\0 prefix
282
+ attr_names << attr_name.gsub(/\A\\0[^\\]+\\0/, '')
283
+ values << value
284
+ end
285
+
286
+ result = Struct.new(klass_name, *attr_names).new(*values)
287
+ result.instance_variable_set("@_php_class", klass_name)
288
+ end
289
+
290
+ result
291
+ end
292
+ .,.,
293
+
294
+ module_eval(<<'.,.,', 'unserializer.y', 53)
295
+ def _reduce_14(val, _values, result)
296
+ result = val[0] << val[1]
297
+ result
298
+ end
299
+ .,.,
300
+
301
+ module_eval(<<'.,.,', 'unserializer.y', 54)
302
+ def _reduce_15(val, _values, result)
303
+ result = []
304
+ result
305
+ end
306
+ .,.,
307
+
308
+ module_eval(<<'.,.,', 'unserializer.y', 57)
309
+ def _reduce_16(val, _values, result)
310
+ result = val
311
+ result
312
+ end
313
+ .,.,
314
+
315
+ module_eval(<<'.,.,', 'unserializer.y', 62)
316
+ def _reduce_17(val, _values, result)
317
+ # Checks if the keys are a sequence of integers
318
+ idx = -1
319
+ arr = val[5].all? { |(k,v)| k == (idx += 1) }
320
+
321
+ if arr
322
+ result = val[5].map { |(k,v)| v }
323
+ else
324
+ result = Hash[val[5]]
325
+ end
326
+
327
+ result
328
+ end
329
+ .,.,
330
+
331
+ def _reduce_none(val, _values, result)
332
+ val[0]
333
+ end
334
+
335
+ end # class Unserializer
336
+ end # module PhpSerialization
@@ -0,0 +1,95 @@
1
+ class PhpSerialization::Unserializer
2
+ rule
3
+
4
+ data : null ';' { @object = val[0] }
5
+ | bool ';' { @object = val[0] }
6
+ | integer ';' { @object = val[0] }
7
+ | double ';' { @object = val[0] }
8
+ | string ';' { @object = val[0] }
9
+ | assoc_array { @object = val[0] }
10
+ | object { @object = val[0] }
11
+ ;
12
+
13
+ null : 'N' { result = nil }
14
+ ;
15
+
16
+ bool : 'b' ':' NUMBER { result = Integer(val[2]) > 0 }
17
+ ;
18
+
19
+ integer : 'i' ':' NUMBER { result = Integer(val[2]) }
20
+ ;
21
+
22
+ double : 'd' ':' NUMBER { result = Float(val[2]) }
23
+ ;
24
+
25
+ string : 's' ':' NUMBER ':' STRING { result = val[4] }
26
+ ;
27
+
28
+ object : 'O' ':' NUMBER ':' STRING ':' NUMBER ':' '{' attribute_list '}'
29
+ {
30
+ if eval("defined?(#{val[4]})")
31
+ result = Object.const_get(val[4]).new
32
+
33
+ val[9].each do |(attr_name, value)|
34
+ # Protected and private attributes will have a \0..\0 prefix
35
+ attr_name = attr_name.gsub(/\A\\0[^\\]+\\0/, '')
36
+ result.instance_variable_set("@#{attr_name}", value)
37
+ end
38
+ else
39
+ klass_name = val[4].gsub(/^Struct::/, '')
40
+ attr_names, values = [], []
41
+
42
+ val[9].each do |(attr_name, value)|
43
+ # Protected and private attributes will have a \0..\0 prefix
44
+ attr_names << attr_name.gsub(/\A\\0[^\\]+\\0/, '')
45
+ values << value
46
+ end
47
+
48
+ result = Struct.new(klass_name, *attr_names).new(*values)
49
+ result.instance_variable_set("@_php_class", klass_name)
50
+ end
51
+ }
52
+ ;
53
+
54
+ attribute_list : attribute_list attribute { result = val[0] << val[1] }
55
+ | { result = [] }
56
+ ;
57
+
58
+ attribute : data data { result = val }
59
+ ;
60
+
61
+ assoc_array : 'a' ':' NUMBER ':' '{' attribute_list '}'
62
+ {
63
+ # Checks if the keys are a sequence of integers
64
+ idx = -1
65
+ arr = val[5].all? { |(k,v)| k == (idx += 1) }
66
+
67
+ if arr
68
+ result = val[5].map { |(k,v)| v }
69
+ else
70
+ result = Hash[val[5]]
71
+ end
72
+ }
73
+ ;
74
+
75
+ end
76
+
77
+ ---- header ----
78
+ require 'php_serialization/tokenizer'
79
+
80
+ ---- inner ----
81
+ def initialize(tokenizer_klass = Tokenizer)
82
+ @tokenizer_klass = tokenizer_klass
83
+ end
84
+
85
+ def run(string)
86
+ @tokenizer = @tokenizer_klass.new(string)
87
+ yyparse(@tokenizer, :each)
88
+ return @object
89
+ ensure
90
+ @tokenizer = nil
91
+ end
92
+
93
+ def next_token
94
+ @tokenizer.next_token
95
+ end
@@ -0,0 +1,3 @@
1
+ module PhpSerialization
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'php_serialization/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-php-serialization"
8
+ spec.version = PhpSerialization::VERSION
9
+ spec.authors = ["Rodrigo Kochenburger"]
10
+ spec.email = ["divoxx@gmail.com"]
11
+ spec.summary = %q{Pure Ruby implementation of php's methods: serialize() and unserializer()}
12
+ spec.description = %q{Pure Ruby implementation of php's methods: serialize() and unserializer()}
13
+ spec.homepage = "https://github.com/divoxx/ruby-php-serialization"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0"
24
+
25
+ spec.add_dependency "racc"
26
+ end