php-serialization 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +72 -0
- data/VERSION +1 -0
- data/features/ruby_php_serialization.feature +9 -0
- data/features/step_definitions/ruby_php_serialization_steps.rb +0 -0
- data/features/support/env.rb +4 -0
- data/lib/php_serialization.rb +17 -0
- data/lib/php_serialization/tokenizer.rb +21 -0
- data/lib/php_serialization/unserializer.rb +343 -0
- data/lib/php_serialization/unserializer.y +87 -0
- data/php-serialization.gemspec +66 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/unserialization_spec.rb +52 -0
- metadata +102 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rodrigo Kochenburger
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= PhpSerialization
|
2
|
+
|
3
|
+
Ruby implementation of PHP's serialization. This is special useful for reading PHP session files.
|
4
|
+
|
5
|
+
== Unserialization examples
|
6
|
+
|
7
|
+
Primitive values
|
8
|
+
|
9
|
+
PhpSerialization.load('i:10;') # => 10
|
10
|
+
PhpSerialization.load('s:4:"Name";') # => "Name"
|
11
|
+
PhpSerialization.load('b:1;') # => true
|
12
|
+
PhpSerialization.load('N;') # => nil
|
13
|
+
|
14
|
+
Array
|
15
|
+
|
16
|
+
PhpSerialization.load('a:2:{i:0;b:1;i:1;s:3:"foo";};') # => [true, "foo"]
|
17
|
+
|
18
|
+
Hash
|
19
|
+
|
20
|
+
PhpSerialization.load('a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};') # => {"name" => "Rodrigo", "age" => 23}
|
21
|
+
|
22
|
+
Object
|
23
|
+
|
24
|
+
class Person
|
25
|
+
attr_accessor :name, :age
|
26
|
+
end
|
27
|
+
|
28
|
+
person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
|
29
|
+
person.name # => "Rodrigo"
|
30
|
+
person.age # => 23
|
31
|
+
|
32
|
+
Object without class will map to a Struct
|
33
|
+
|
34
|
+
person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
|
35
|
+
person.class # => Struct::Person
|
36
|
+
person.name # => "Rodrigo"
|
37
|
+
person.age # => 23
|
38
|
+
|
39
|
+
You can also call unserialize() or restore(), they are alias to the load().
|
40
|
+
|
41
|
+
== Copyright
|
42
|
+
|
43
|
+
Copyright (c) 2009 Rodrigo Kochenburger. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "php-serialization"
|
8
|
+
gem.summary = %Q{PHP's serialization implementation for ruby}
|
9
|
+
gem.description = %Q{Pure Ruby implementation of php's methods: serialize() and unserializer()}
|
10
|
+
gem.email = "divoxx@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/divoxx/ruby-php-serialization"
|
12
|
+
gem.authors = ["Rodrigo Kochenburger"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_development_dependency "cucumber"
|
15
|
+
gem.add_dependency "racc"
|
16
|
+
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
spec.spec_opts = ["--options spec/spec.opts"]
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'cucumber/rake/task'
|
41
|
+
Cucumber::Rake::Task.new(:features)
|
42
|
+
|
43
|
+
task :features => :check_dependencies
|
44
|
+
rescue LoadError
|
45
|
+
task :features do
|
46
|
+
abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
task :default => :spec
|
51
|
+
|
52
|
+
require 'rake/rdoctask'
|
53
|
+
Rake::RDocTask.new do |rdoc|
|
54
|
+
if File.exist?('VERSION')
|
55
|
+
version = File.read('VERSION')
|
56
|
+
else
|
57
|
+
version = ""
|
58
|
+
end
|
59
|
+
|
60
|
+
rdoc.rdoc_dir = 'rdoc'
|
61
|
+
rdoc.title = "Ruby's PhpSerialization #{version}"
|
62
|
+
rdoc.rdoc_files.include('README*')
|
63
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
file 'lib/php_serialization/unserializer.rb' => 'lib/php_serialization/unserializer.y' do |t|
|
68
|
+
`racc -o #{t.name} #{t.prerequisites[0]}`
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Compile all the necessary files, such as .y grammar files"
|
72
|
+
task :compile => ['lib/php_serialization/unserializer.rb']
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.3
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
require 'php_serialization/unserializer'
|
3
|
+
|
4
|
+
module PhpSerialization
|
5
|
+
class << self
|
6
|
+
def unserializer
|
7
|
+
@unserializer ||= Unserializer.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def load(str)
|
11
|
+
unserializer.load(str)
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :unserialize :load
|
15
|
+
alias :restore :load
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module PhpSerialization
|
2
|
+
class Tokenizer
|
3
|
+
def initialize(string)
|
4
|
+
@string = string
|
5
|
+
end
|
6
|
+
|
7
|
+
def each
|
8
|
+
while !@string.empty?
|
9
|
+
token = case @string
|
10
|
+
when /\A[0-9]+(\.[0-9]+)?/m then yield([:NUMBER, $&])
|
11
|
+
when /\A"([^"]*)"/m then yield([:STRING, $1])
|
12
|
+
when /\A[^\s]/m then yield([$&, $&])
|
13
|
+
end
|
14
|
+
|
15
|
+
@string = $'
|
16
|
+
end
|
17
|
+
|
18
|
+
yield([false, '$'])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,343 @@
|
|
1
|
+
#
|
2
|
+
# DO NOT MODIFY!!!!
|
3
|
+
# This file is automatically generated by Racc 1.4.6
|
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', 69)
|
15
|
+
def self.load(string)
|
16
|
+
new.load(string)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(tokenizer_klass = Tokenizer)
|
20
|
+
@tokenizer_klass = tokenizer_klass
|
21
|
+
end
|
22
|
+
|
23
|
+
def load(string)
|
24
|
+
@tokenizer = @tokenizer_klass.new(string)
|
25
|
+
yyparse(@tokenizer, :each)
|
26
|
+
return @object
|
27
|
+
ensure
|
28
|
+
@tokenizer = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def next_token
|
32
|
+
@tokenizer.next_token
|
33
|
+
end
|
34
|
+
...end unserializer.y/module_eval...
|
35
|
+
##### State transition tables begin ###
|
36
|
+
|
37
|
+
racc_action_table = [
|
38
|
+
6, 7, 31, 21, 10, 12, 14, 22, 1, 6,
|
39
|
+
7, 5, 23, 10, 12, 14, 24, 1, 25, 50,
|
40
|
+
5, 6, 7, 26, 27, 10, 12, 14, 28, 1,
|
41
|
+
6, 7, 5, 29, 10, 12, 14, 30, 1, 20,
|
42
|
+
43, 5, 32, 33, 34, 35, 36, 37, 38, 40,
|
43
|
+
42, 19, 46, 47, 18, 17, 51 ]
|
44
|
+
|
45
|
+
racc_action_check = [
|
46
|
+
0, 0, 24, 9, 0, 0, 0, 10, 0, 48,
|
47
|
+
48, 0, 12, 48, 48, 48, 14, 48, 17, 48,
|
48
|
+
48, 47, 47, 18, 19, 47, 47, 47, 20, 47,
|
49
|
+
41, 41, 47, 22, 41, 41, 41, 23, 41, 8,
|
50
|
+
41, 41, 25, 26, 31, 32, 33, 34, 35, 38,
|
51
|
+
40, 7, 42, 45, 5, 1, 49 ]
|
52
|
+
|
53
|
+
racc_action_pointer = [
|
54
|
+
-3, 50, nil, nil, nil, 49, nil, 46, 39, 1,
|
55
|
+
2, nil, 7, nil, 11, nil, nil, 12, 17, 18,
|
56
|
+
28, nil, 27, 31, -4, 37, 38, nil, nil, nil,
|
57
|
+
nil, 39, 35, 34, 37, 43, nil, nil, 43, nil,
|
58
|
+
45, 27, 40, nil, nil, 51, nil, 18, 6, 54,
|
59
|
+
nil, nil ]
|
60
|
+
|
61
|
+
racc_action_default = [
|
62
|
+
-20, -20, -6, -7, -8, -20, -9, -20, -20, -20,
|
63
|
+
-20, -2, -20, -3, -20, -4, -5, -20, -20, -20,
|
64
|
+
-20, -1, -20, -20, -20, -20, -20, -10, 52, -11,
|
65
|
+
-12, -20, -20, -20, -20, -20, -18, -13, -20, -16,
|
66
|
+
-20, -20, -20, -19, -15, -20, -16, -20, -20, -20,
|
67
|
+
-14, -17 ]
|
68
|
+
|
69
|
+
racc_goto_table = [
|
70
|
+
9, 41, 8, 39, nil, nil, nil, nil, 48, nil,
|
71
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
72
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
73
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
74
|
+
nil, nil, nil, nil, nil, nil, nil, 49 ]
|
75
|
+
|
76
|
+
racc_goto_check = [
|
77
|
+
2, 10, 1, 12, nil, nil, nil, nil, 10, nil,
|
78
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
79
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
80
|
+
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
|
81
|
+
nil, nil, nil, nil, nil, nil, nil, 2 ]
|
82
|
+
|
83
|
+
racc_goto_pointer = [
|
84
|
+
nil, 2, 0, nil, nil, nil, nil, nil, nil, nil,
|
85
|
+
-38, nil, -33 ]
|
86
|
+
|
87
|
+
racc_goto_default = [
|
88
|
+
nil, nil, 45, 11, 13, 15, 16, 2, 3, 4,
|
89
|
+
nil, 44, nil ]
|
90
|
+
|
91
|
+
racc_reduce_table = [
|
92
|
+
0, 0, :racc_error,
|
93
|
+
2, 16, :_reduce_1,
|
94
|
+
1, 17, :_reduce_2,
|
95
|
+
1, 17, :_reduce_3,
|
96
|
+
1, 17, :_reduce_4,
|
97
|
+
1, 17, :_reduce_5,
|
98
|
+
1, 17, :_reduce_6,
|
99
|
+
1, 17, :_reduce_7,
|
100
|
+
1, 17, :_reduce_8,
|
101
|
+
1, 18, :_reduce_9,
|
102
|
+
3, 19, :_reduce_10,
|
103
|
+
3, 20, :_reduce_11,
|
104
|
+
3, 21, :_reduce_12,
|
105
|
+
5, 23, :_reduce_13,
|
106
|
+
11, 24, :_reduce_14,
|
107
|
+
2, 25, :_reduce_15,
|
108
|
+
0, 25, :_reduce_16,
|
109
|
+
4, 26, :_reduce_17,
|
110
|
+
0, 27, :_reduce_18,
|
111
|
+
8, 22, :_reduce_19 ]
|
112
|
+
|
113
|
+
racc_reduce_n = 20
|
114
|
+
|
115
|
+
racc_shift_n = 52
|
116
|
+
|
117
|
+
racc_token_table = {
|
118
|
+
false => 0,
|
119
|
+
:error => 1,
|
120
|
+
";" => 2,
|
121
|
+
"N" => 3,
|
122
|
+
"b" => 4,
|
123
|
+
":" => 5,
|
124
|
+
:NUMBER => 6,
|
125
|
+
"i" => 7,
|
126
|
+
"d" => 8,
|
127
|
+
"s" => 9,
|
128
|
+
:STRING => 10,
|
129
|
+
"O" => 11,
|
130
|
+
"{" => 12,
|
131
|
+
"}" => 13,
|
132
|
+
"a" => 14 }
|
133
|
+
|
134
|
+
racc_nt_base = 15
|
135
|
+
|
136
|
+
racc_use_result_var = true
|
137
|
+
|
138
|
+
Racc_arg = [
|
139
|
+
racc_action_table,
|
140
|
+
racc_action_check,
|
141
|
+
racc_action_default,
|
142
|
+
racc_action_pointer,
|
143
|
+
racc_goto_table,
|
144
|
+
racc_goto_check,
|
145
|
+
racc_goto_default,
|
146
|
+
racc_goto_pointer,
|
147
|
+
racc_nt_base,
|
148
|
+
racc_reduce_table,
|
149
|
+
racc_token_table,
|
150
|
+
racc_shift_n,
|
151
|
+
racc_reduce_n,
|
152
|
+
racc_use_result_var ]
|
153
|
+
|
154
|
+
Racc_token_to_s_table = [
|
155
|
+
"$end",
|
156
|
+
"error",
|
157
|
+
"\";\"",
|
158
|
+
"\"N\"",
|
159
|
+
"\"b\"",
|
160
|
+
"\":\"",
|
161
|
+
"NUMBER",
|
162
|
+
"\"i\"",
|
163
|
+
"\"d\"",
|
164
|
+
"\"s\"",
|
165
|
+
"STRING",
|
166
|
+
"\"O\"",
|
167
|
+
"\"{\"",
|
168
|
+
"\"}\"",
|
169
|
+
"\"a\"",
|
170
|
+
"$start",
|
171
|
+
"serialization",
|
172
|
+
"data",
|
173
|
+
"null",
|
174
|
+
"bool",
|
175
|
+
"integer",
|
176
|
+
"double",
|
177
|
+
"array",
|
178
|
+
"string",
|
179
|
+
"object",
|
180
|
+
"attribute_list",
|
181
|
+
"attribute",
|
182
|
+
"@1" ]
|
183
|
+
|
184
|
+
Racc_debug_parser = false
|
185
|
+
|
186
|
+
##### State transition tables end #####
|
187
|
+
|
188
|
+
# reduce 0 omitted
|
189
|
+
|
190
|
+
module_eval(<<'.,.,', 'unserializer.y', 3)
|
191
|
+
def _reduce_1(val, _values, result)
|
192
|
+
@object = val[0]
|
193
|
+
result
|
194
|
+
end
|
195
|
+
.,.,
|
196
|
+
|
197
|
+
module_eval(<<'.,.,', 'unserializer.y', 6)
|
198
|
+
def _reduce_2(val, _values, result)
|
199
|
+
result = val[0]
|
200
|
+
result
|
201
|
+
end
|
202
|
+
.,.,
|
203
|
+
|
204
|
+
module_eval(<<'.,.,', 'unserializer.y', 7)
|
205
|
+
def _reduce_3(val, _values, result)
|
206
|
+
result = val[0]
|
207
|
+
result
|
208
|
+
end
|
209
|
+
.,.,
|
210
|
+
|
211
|
+
module_eval(<<'.,.,', 'unserializer.y', 8)
|
212
|
+
def _reduce_4(val, _values, result)
|
213
|
+
result = val[0]
|
214
|
+
result
|
215
|
+
end
|
216
|
+
.,.,
|
217
|
+
|
218
|
+
module_eval(<<'.,.,', 'unserializer.y', 9)
|
219
|
+
def _reduce_5(val, _values, result)
|
220
|
+
result = val[0]
|
221
|
+
result
|
222
|
+
end
|
223
|
+
.,.,
|
224
|
+
|
225
|
+
module_eval(<<'.,.,', 'unserializer.y', 10)
|
226
|
+
def _reduce_6(val, _values, result)
|
227
|
+
result = val[0]
|
228
|
+
result
|
229
|
+
end
|
230
|
+
.,.,
|
231
|
+
|
232
|
+
module_eval(<<'.,.,', 'unserializer.y', 11)
|
233
|
+
def _reduce_7(val, _values, result)
|
234
|
+
result = val[0]
|
235
|
+
result
|
236
|
+
end
|
237
|
+
.,.,
|
238
|
+
|
239
|
+
module_eval(<<'.,.,', 'unserializer.y', 12)
|
240
|
+
def _reduce_8(val, _values, result)
|
241
|
+
result = val[0]
|
242
|
+
result
|
243
|
+
end
|
244
|
+
.,.,
|
245
|
+
|
246
|
+
module_eval(<<'.,.,', 'unserializer.y', 15)
|
247
|
+
def _reduce_9(val, _values, result)
|
248
|
+
result = nil
|
249
|
+
result
|
250
|
+
end
|
251
|
+
.,.,
|
252
|
+
|
253
|
+
module_eval(<<'.,.,', 'unserializer.y', 18)
|
254
|
+
def _reduce_10(val, _values, result)
|
255
|
+
result = Integer(val[2]) > 0
|
256
|
+
result
|
257
|
+
end
|
258
|
+
.,.,
|
259
|
+
|
260
|
+
module_eval(<<'.,.,', 'unserializer.y', 21)
|
261
|
+
def _reduce_11(val, _values, result)
|
262
|
+
result = Integer(val[2])
|
263
|
+
result
|
264
|
+
end
|
265
|
+
.,.,
|
266
|
+
|
267
|
+
module_eval(<<'.,.,', 'unserializer.y', 24)
|
268
|
+
def _reduce_12(val, _values, result)
|
269
|
+
result = Float(val[2])
|
270
|
+
result
|
271
|
+
end
|
272
|
+
.,.,
|
273
|
+
|
274
|
+
module_eval(<<'.,.,', 'unserializer.y', 27)
|
275
|
+
def _reduce_13(val, _values, result)
|
276
|
+
result = val[4]
|
277
|
+
result
|
278
|
+
end
|
279
|
+
.,.,
|
280
|
+
|
281
|
+
module_eval(<<'.,.,', 'unserializer.y', 32)
|
282
|
+
def _reduce_14(val, _values, result)
|
283
|
+
if Object.const_defined?(val[4])
|
284
|
+
result = Object.const_get(val[4]).new
|
285
|
+
|
286
|
+
val[9].each do |(attr_name, value)|
|
287
|
+
result.instance_variable_set("@#{attr_name}", value)
|
288
|
+
end
|
289
|
+
else
|
290
|
+
result = Struct.new(val[4], *val[9].map { |(k,v)| k.to_sym }).new(*val[9].map { |(k,v)| v })
|
291
|
+
end
|
292
|
+
|
293
|
+
result
|
294
|
+
end
|
295
|
+
.,.,
|
296
|
+
|
297
|
+
module_eval(<<'.,.,', 'unserializer.y', 44)
|
298
|
+
def _reduce_15(val, _values, result)
|
299
|
+
result = val[0] << val[1]
|
300
|
+
result
|
301
|
+
end
|
302
|
+
.,.,
|
303
|
+
|
304
|
+
module_eval(<<'.,.,', 'unserializer.y', 45)
|
305
|
+
def _reduce_16(val, _values, result)
|
306
|
+
result = []
|
307
|
+
result
|
308
|
+
end
|
309
|
+
.,.,
|
310
|
+
|
311
|
+
module_eval(<<'.,.,', 'unserializer.y', 48)
|
312
|
+
def _reduce_17(val, _values, result)
|
313
|
+
@numeric_array = false unless val[0].is_a?(Integer); result = [val[0], val[2]]
|
314
|
+
result
|
315
|
+
end
|
316
|
+
.,.,
|
317
|
+
|
318
|
+
module_eval(<<'.,.,', 'unserializer.y', 51)
|
319
|
+
def _reduce_18(val, _values, result)
|
320
|
+
@numeric_array = true
|
321
|
+
result
|
322
|
+
end
|
323
|
+
.,.,
|
324
|
+
|
325
|
+
module_eval(<<'.,.,', 'unserializer.y', 53)
|
326
|
+
def _reduce_19(val, _values, result)
|
327
|
+
if @numeric_array
|
328
|
+
result = []
|
329
|
+
val[6].each { |(i,v)| result[i] = v }
|
330
|
+
else
|
331
|
+
result = Hash[*val[6].flatten]
|
332
|
+
end
|
333
|
+
|
334
|
+
result
|
335
|
+
end
|
336
|
+
.,.,
|
337
|
+
|
338
|
+
def _reduce_none(val, _values, result)
|
339
|
+
val[0]
|
340
|
+
end
|
341
|
+
|
342
|
+
end # class Unserializer
|
343
|
+
end # module PhpSerialization
|
@@ -0,0 +1,87 @@
|
|
1
|
+
class PhpSerialization::Unserializer
|
2
|
+
rule
|
3
|
+
|
4
|
+
serialization : data ';' { @object = val[0] }
|
5
|
+
;
|
6
|
+
|
7
|
+
data : null { result = val[0] }
|
8
|
+
| bool { result = val[0] }
|
9
|
+
| integer { result = val[0] }
|
10
|
+
| double { result = val[0] }
|
11
|
+
| array { result = val[0] }
|
12
|
+
| string { result = val[0] }
|
13
|
+
| object { result = val[0] }
|
14
|
+
;
|
15
|
+
|
16
|
+
null : 'N' { result = nil }
|
17
|
+
;
|
18
|
+
|
19
|
+
bool : 'b' ':' NUMBER { result = Integer(val[2]) > 0 }
|
20
|
+
;
|
21
|
+
|
22
|
+
integer : 'i' ':' NUMBER { result = Integer(val[2]) }
|
23
|
+
;
|
24
|
+
|
25
|
+
double : 'd' ':' NUMBER { result = Float(val[2]) }
|
26
|
+
;
|
27
|
+
|
28
|
+
string : 's' ':' NUMBER ':' STRING { result = val[4] }
|
29
|
+
;
|
30
|
+
|
31
|
+
object : 'O' ':' NUMBER ':' STRING ':' NUMBER ':' '{' attribute_list '}'
|
32
|
+
{
|
33
|
+
if Object.const_defined?(val[4])
|
34
|
+
result = Object.const_get(val[4]).new
|
35
|
+
|
36
|
+
val[9].each do |(attr_name, value)|
|
37
|
+
result.instance_variable_set("@#{attr_name}", value)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
result = Struct.new(val[4], *val[9].map { |(k,v)| k.to_sym }).new(*val[9].map { |(k,v)| v })
|
41
|
+
end
|
42
|
+
}
|
43
|
+
;
|
44
|
+
|
45
|
+
attribute_list : attribute_list attribute { result = val[0] << val[1] }
|
46
|
+
| { result = [] }
|
47
|
+
;
|
48
|
+
|
49
|
+
attribute : data ';' data ';' { @numeric_array = false unless val[0].is_a?(Integer); result = [val[0], val[2]] }
|
50
|
+
;
|
51
|
+
|
52
|
+
array : 'a' ':' NUMBER ':' '{' { @numeric_array = true } attribute_list '}'
|
53
|
+
{
|
54
|
+
if @numeric_array
|
55
|
+
result = []
|
56
|
+
val[6].each { |(i,v)| result[i] = v }
|
57
|
+
else
|
58
|
+
result = Hash[*val[6].flatten]
|
59
|
+
end
|
60
|
+
}
|
61
|
+
;
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
---- header ----
|
66
|
+
require 'php_serialization/tokenizer'
|
67
|
+
|
68
|
+
---- inner ----
|
69
|
+
def self.load(string)
|
70
|
+
new.load(string)
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize(tokenizer_klass = Tokenizer)
|
74
|
+
@tokenizer_klass = tokenizer_klass
|
75
|
+
end
|
76
|
+
|
77
|
+
def load(string)
|
78
|
+
@tokenizer = @tokenizer_klass.new(string)
|
79
|
+
yyparse(@tokenizer, :each)
|
80
|
+
return @object
|
81
|
+
ensure
|
82
|
+
@tokenizer = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
def next_token
|
86
|
+
@tokenizer.next_token
|
87
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{php-serialization}
|
8
|
+
s.version = "0.2.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rodrigo Kochenburger"]
|
12
|
+
s.date = %q{2009-10-02}
|
13
|
+
s.description = %q{Pure Ruby implementation of php's methods: serialize() and unserializer()}
|
14
|
+
s.email = %q{divoxx@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"features/ruby_php_serialization.feature",
|
27
|
+
"features/step_definitions/ruby_php_serialization_steps.rb",
|
28
|
+
"features/support/env.rb",
|
29
|
+
"lib/php_serialization.rb",
|
30
|
+
"lib/php_serialization/tokenizer.rb",
|
31
|
+
"lib/php_serialization/unserializer.rb",
|
32
|
+
"lib/php_serialization/unserializer.y",
|
33
|
+
"php-serialization.gemspec",
|
34
|
+
"spec/spec.opts",
|
35
|
+
"spec/spec_helper.rb",
|
36
|
+
"spec/unserialization_spec.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/divoxx/ruby-php-serialization}
|
39
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.3.4}
|
42
|
+
s.summary = %q{PHP's serialization implementation for ruby}
|
43
|
+
s.test_files = [
|
44
|
+
"spec/spec_helper.rb",
|
45
|
+
"spec/unserialization_spec.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<cucumber>, [">= 0"])
|
55
|
+
s.add_runtime_dependency(%q<racc>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
+
s.add_dependency(%q<cucumber>, [">= 0"])
|
59
|
+
s.add_dependency(%q<racc>, [">= 0"])
|
60
|
+
end
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
63
|
+
s.add_dependency(%q<cucumber>, [">= 0"])
|
64
|
+
s.add_dependency(%q<racc>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
+
|
3
|
+
describe "Unserialization" do
|
4
|
+
it "should unserialize a integer" do
|
5
|
+
PhpSerialization.load("i:10;").should == 10
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should unserialize a string" do
|
9
|
+
PhpSerialization.load('s:4:"Name";').should == "Name"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should unserialize true" do
|
13
|
+
PhpSerialization.load('b:1;').should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should unserialize false" do
|
17
|
+
PhpSerialization.load('b:0;').should == false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should unserialize nil" do
|
21
|
+
PhpSerialization.load('N;').should == nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should unzerialize an array" do
|
25
|
+
PhpSerialization.load('a:2:{i:0;b:1;i:1;s:3:"foo";};').should == [true, "foo"]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should unserialize a hash" do
|
29
|
+
PhpSerialization.load('a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};').should == {"name" => "Rodrigo", "age" => 23}
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should unserialize object with class existant" do
|
33
|
+
class Person
|
34
|
+
attr_accessor :name, :age
|
35
|
+
end
|
36
|
+
|
37
|
+
person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
|
38
|
+
person.should be_instance_of(Person)
|
39
|
+
person.name.should == "Rodrigo"
|
40
|
+
person.age.should == 23
|
41
|
+
|
42
|
+
|
43
|
+
Object.send(:remove_const, :Person)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should unserialize object without class as a struct" do
|
47
|
+
person = PhpSerialization.load('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
|
48
|
+
person.should be_instance_of(Struct::Person)
|
49
|
+
person.name.should == "Rodrigo"
|
50
|
+
person.age.should == 23
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: php-serialization
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo Kochenburger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-02 00:00:00 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: cucumber
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: racc
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: "Pure Ruby implementation of php's methods: serialize() and unserializer()"
|
46
|
+
email: divoxx@gmail.com
|
47
|
+
executables: []
|
48
|
+
|
49
|
+
extensions: []
|
50
|
+
|
51
|
+
extra_rdoc_files:
|
52
|
+
- LICENSE
|
53
|
+
- README.rdoc
|
54
|
+
files:
|
55
|
+
- .document
|
56
|
+
- .gitignore
|
57
|
+
- LICENSE
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- VERSION
|
61
|
+
- features/ruby_php_serialization.feature
|
62
|
+
- features/step_definitions/ruby_php_serialization_steps.rb
|
63
|
+
- features/support/env.rb
|
64
|
+
- lib/php_serialization.rb
|
65
|
+
- lib/php_serialization/tokenizer.rb
|
66
|
+
- lib/php_serialization/unserializer.rb
|
67
|
+
- lib/php_serialization/unserializer.y
|
68
|
+
- php-serialization.gemspec
|
69
|
+
- spec/spec.opts
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/unserialization_spec.rb
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://github.com/divoxx/ruby-php-serialization
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options:
|
78
|
+
- --charset=UTF-8
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "0"
|
86
|
+
version:
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
version:
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.3.4
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: PHP's serialization implementation for ruby
|
100
|
+
test_files:
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/unserialization_spec.rb
|