php_serialize 1.1.3 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/php_serialize.rb +11 -6
  2. data/test.rb +17 -3
  3. metadata +26 -32
@@ -86,10 +86,11 @@ module PHP
86
86
  s << '}'
87
87
 
88
88
  when String, Symbol
89
- s << "s:#{var.to_s.length}:\"#{var.to_s}\";"
89
+ s << "s:#{var.to_s.bytesize}:\"#{var.to_s}\";"
90
90
 
91
- when Fixnum # PHP doesn't have bignums
92
- s << "i:#{var};"
91
+ when Integer
92
+ raise RangeError, "value too big to serialize as integer: #{var}" unless (-2147483648..2147483647).include? var
93
+ s << "i:#{var};"
93
94
 
94
95
  when Float
95
96
  s << "d:#{var};"
@@ -191,7 +192,8 @@ module PHP
191
192
  string = StringIO.new(string)
192
193
  def string.read_until(char)
193
194
  val = ''
194
- while (c = self.read(1)) != char
195
+ # readchar returns a string in 1.9 and a fixnum in 1.8
196
+ while (c = self.readchar) != char[0]
195
197
  val << c
196
198
  end
197
199
  val
@@ -215,6 +217,7 @@ module PHP
215
217
 
216
218
  private
217
219
  def PHP.do_unserialize(string, classmap, assoc)
220
+ encoding = string.external_encoding rescue 'UTF-8'
218
221
  val = nil
219
222
  # determine a type
220
223
  type = string.read(2)[0,1]
@@ -292,8 +295,10 @@ private
292
295
 
293
296
  when 's' # string, s:length:"data";
294
297
  len = string.read_until(':').to_i + 3 # quotes, separator
295
- val = string.read(len)[1...-2] # read it, kill useless quotes
296
-
298
+ val = string.read(len)
299
+ val = val.force_encoding(encoding) if val.respond_to?(:force_encoding)
300
+ val = val[1...-2] # read it, kill useless quotes
301
+
297
302
  when 'i' # integer, i:123
298
303
  val = string.read_until(';').to_i
299
304
 
data/test.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/local/bin/ruby
2
+ # encoding: UTF-8
2
3
 
3
4
  require 'test/unit'
4
5
 
@@ -62,6 +63,7 @@ class TestPhpSerialize < Test::Unit::TestCase
62
63
  test -2147483648, "i:-2147483648;", :name => 'Min Fixnum'
63
64
  test 4.2, 'd:4.2;'
64
65
  test 'test', 's:4:"test";'
66
+ test 'ümläut_test', 's:13:"ümläut_test";'
65
67
  test :test, 's:4:"test";', :name => 'Symbol'
66
68
  test "\"\n\t\"", "s:4:\"\"\n\t\"\";", :name => 'Complex string'
67
69
  test [nil, true, false, 42, 4.2, 'test'], 'a:6:{i:0;N;i:1;b:1;i:2;b:0;i:3;i:42;i:4;d:4.2;i:5;s:4:"test";}',
@@ -72,6 +74,13 @@ class TestPhpSerialize < Test::Unit::TestCase
72
74
  test TestClass.new("Foo", 65), 'O:9:"testclass":2:{s:4:"name";s:3:"Foo";s:5:"value";i:65;}',
73
75
  :name => 'Class'
74
76
 
77
+ def test_unserialize_unknown_class
78
+ php = 'O:12:"unknownclass":2:{s:4:"name";s:3:"Foo";s:5:"value";i:65;}'
79
+ ruby = OpenStruct.new('name' => "Foo", 'value' => 65);
80
+ unserialized = PHP.unserialize(php);
81
+ assert_equal ruby, unserialized
82
+ end
83
+
75
84
  # Verify assoc is passed down calls.
76
85
  # Slightly awkward because hashes don't guarantee order.
77
86
  def test_assoc
@@ -104,6 +113,11 @@ class TestPhpSerialize < Test::Unit::TestCase
104
113
  end
105
114
  end
106
115
 
107
- require 'test/unit/ui/console/testrunner'
108
- Test::Unit::UI::Console::TestRunner.run(TestPhpSerialize)
109
-
116
+ if RUBY_VERSION =~ /1.9/
117
+ require 'test/unit/diff'
118
+ require 'test/unit/autorunner'
119
+ Test::Unit::AutoRunner.default_runner = "console"
120
+ else
121
+ require 'test/unit/ui/console/testrunner'
122
+ Test::Unit::UI::Console::TestRunner.run(TestPhpSerialize)
123
+ end
metadata CHANGED
@@ -1,57 +1,51 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: php_serialize
3
- version: !ruby/object:Gem::Version
4
- version: 1.1.3
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.2'
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Thomas Hurst
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-11-29 00:00:00 +01:00
12
+ date: 2011-06-17 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
-
16
- description: "\tThis module provides two methods: PHP.serialize() and PHP.unserialize(), both\n\
17
- \tof which should be compatible with the similarly named functions in PHP.\n\n\
18
- \tIt can also serialize and unserialize PHP sessions.\n"
15
+ description: ! "\tThis module provides two methods: PHP.serialize() and PHP.unserialize(),
16
+ both\n\tof which should be compatible with the similarly named functions in PHP.\n\n\tIt
17
+ can also serialize and unserialize PHP sessions.\n"
19
18
  email: tom@hur.st
20
19
  executables: []
21
-
22
20
  extensions: []
23
-
24
21
  extra_rdoc_files: []
25
-
26
- files:
22
+ files:
27
23
  - lib/php_serialize.rb
24
+ - test.rb
28
25
  has_rdoc: true
29
26
  homepage: http://www.aagh.net/projects/ruby-php-serialize
30
27
  licenses: []
31
-
32
28
  post_install_message:
33
29
  rdoc_options: []
34
-
35
- require_paths:
30
+ require_paths:
36
31
  - lib/
37
- required_ruby_version: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: "0"
42
- version:
43
- required_rubygems_version: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: "0"
48
- version:
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
49
44
  requirements: []
50
-
51
45
  rubyforge_project:
52
- rubygems_version: 1.3.5
46
+ rubygems_version: 1.6.2
53
47
  signing_key:
54
48
  specification_version: 3
55
49
  summary: Ruby analogs to PHP's serialize() and unserialize() functions
56
- test_files:
50
+ test_files:
57
51
  - test.rb