php-serialize 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1e2ab051accee52d022349801a297fbaa81c21ea025123291ebccb44cf3a28d
4
- data.tar.gz: cceaed0eab97b037777dd37cb2c50f6e98e3d6b0c1a5f3f9f9f9cb9f392a7c5b
3
+ metadata.gz: 5ad6d644735d6cf8749b281aa1bb0135fd383d69afa7bdcd8474da2080fb35b3
4
+ data.tar.gz: 290687c9212981d99039eb169a7cb3dd6817714dc4ac670c11e79485de202b6f
5
5
  SHA512:
6
- metadata.gz: ac582a23b37c737ecd86ffe611af1a5cb569a96462da61f77932dafd7450a7748dbd7155926008b6dda7ee36ca9ccdcc5844eb5f0ccb72670b65570a7bcac731
7
- data.tar.gz: 337d048bc1c69c7ad52553d160d0c591ab9231a4ed6b0bd8f52ae6498e2e1f9d8817b6e3278f5a417e3217b3b548114515f7a059a9e9d7b94e23c86b83d6327c
6
+ metadata.gz: a806b59eeae1a7aeae84d38bad946173eb7245ba5819ab807e7d2fa6543444a90600eec81ba769252389ef16b9d4be213aaf79e484c3880da44c5bd30a2c542d
7
+ data.tar.gz: '096e7d3642c2c2eb85a324422f133d40753244d17df87fa84d4d9328b0fb3da1b185af0f0edd9de3282488192bdff29e8bf31e292609c78f44ec6d4c76b52ec9'
data/lib/php_serialize.rb CHANGED
@@ -163,19 +163,20 @@ module PHP
163
163
  classmap ||= {}
164
164
 
165
165
  ret = nil
166
- string = StringIOReader.new(string)
166
+ original_encoding = string.encoding
167
+ string = StringIOReader.new(string.force_encoding('BINARY'))
167
168
  while string.string[string.pos, 32] =~ /^(\w+)\|/ # session_name|serialized_data
168
169
  ret ||= {}
169
170
  string.pos += $&.size
170
- ret[$1] = PHP.do_unserialize(string, classmap, assoc)
171
+ ret[$1] = PHP.do_unserialize(string, classmap, assoc, original_encoding)
171
172
  end
172
173
 
173
- ret || PHP.do_unserialize(string, classmap, assoc)
174
+ ret || PHP.do_unserialize(string, classmap, assoc, original_encoding)
174
175
  end
175
176
 
176
177
  private
177
178
 
178
- def PHP.do_unserialize(string, classmap, assoc)
179
+ def PHP.do_unserialize(string, classmap, assoc, original_encoding)
179
180
  val = nil
180
181
  # determine a type
181
182
  type = string.read(2)[0,1]
@@ -184,7 +185,7 @@ module PHP
184
185
  count = string.read_until('{').to_i
185
186
  val = Array.new
186
187
  count.times do |i|
187
- val << [do_unserialize(string, classmap, assoc), do_unserialize(string, classmap, assoc)]
188
+ val << [do_unserialize(string, classmap, assoc, original_encoding), do_unserialize(string, classmap, assoc, original_encoding)]
188
189
  end
189
190
  string.read(1) # skip the ending }
190
191
 
@@ -200,6 +201,12 @@ module PHP
200
201
  i += 1
201
202
  end
202
203
 
204
+ val = val.map { |tuple|
205
+ tuple.map { |it|
206
+ it.kind_of?(String) ? it.force_encoding(original_encoding) : it
207
+ }
208
+ }
209
+
203
210
  if array
204
211
  val.map! {|_,value| value }
205
212
  elsif !assoc
@@ -216,8 +223,8 @@ module PHP
216
223
  len = string.read_until('{').to_i
217
224
 
218
225
  len.times do
219
- attr = (do_unserialize(string, classmap, assoc))
220
- attrs << [attr.intern, (attr << '=').intern, do_unserialize(string, classmap, assoc)]
226
+ attr = (do_unserialize(string, classmap, assoc, original_encoding))
227
+ attrs << [attr.intern, (attr << '=').intern, do_unserialize(string, classmap, assoc, original_encoding)]
221
228
  end
222
229
  string.read(1)
223
230
 
@@ -245,7 +252,7 @@ module PHP
245
252
 
246
253
  when 's' # string, s:length:"data";
247
254
  len = string.read_until(':').to_i + 3 # quotes, separator
248
- val = string.read(len)[1...-2] # read it, kill useless quotes
255
+ val = string.read(len)[1...-2].force_encoding(original_encoding) # read it, kill useless quotes
249
256
 
250
257
  when 'i' # integer, i:123
251
258
  val = string.read_until(';').to_i
@@ -76,7 +76,9 @@ class TestPhpSerialize < Test::Unit::TestCase
76
76
  # PHP counts multibyte string, not string length
77
77
  def test_multibyte_string
78
78
  assert_equal "s:6:\"öäü\";", PHP.serialize("öäü")
79
+ assert_equal PHP.unserialize("s:6:\"öäü\";"), "öäü"
79
80
  end
81
+
80
82
  # Verify assoc is passed down calls.
81
83
  # Slightly awkward because hashes don't guarantee order.
82
84
  def test_assoc
@@ -94,6 +96,24 @@ class TestPhpSerialize < Test::Unit::TestCase
94
96
  end
95
97
  end
96
98
 
99
+ # Multibyte version.
100
+ # Verify assoc is passed down calls.
101
+ # Slightly awkward because hashes don't guarantee order.
102
+ def test_assoc_multibyte
103
+ assert_nothing_raised do
104
+ ruby = {'ああ' => ['öäü','漢字'], 'hash' => {'おはよう' => 'smoke'}}
105
+ ruby_assoc = [['ああ', ['öäü','漢字']], ['hash', [['おはよう','smoke']]]]
106
+ phps = [
107
+ 'a:2:{s:6:"ああ";a:2:{i:0;s:6:"öäü";i:1;s:6:"漢字";}s:4:"hash";a:1:{s:12:"おはよう";s:5:"smoke";}}',
108
+ 'a:2:{s:4:"hash";a:1:{s:12:"おはよう";s:5:"smoke";}s:6:"ああ";a:2:{i:0;s:6:"öäü";i:1;s:6:"漢字";}}'
109
+ ]
110
+ serialized = PHP.serialize(ruby, true)
111
+ assert phps.include?(serialized)
112
+ unserialized = PHP.unserialize(serialized, true)
113
+ assert_equal ruby_assoc.sort, unserialized.sort
114
+ end
115
+ end
116
+
97
117
  def test_sessions
98
118
  assert_nothing_raised do
99
119
  ruby = {'session_id' => 42, 'user_data' => {'uid' => 666}}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: php-serialize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Hurst
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-19 00:00:00.000000000 Z
11
+ date: 2024-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,7 @@ homepage: https://github.com/jqr/php-serialize
53
53
  licenses:
54
54
  - MIT
55
55
  metadata: {}
56
- post_install_message:
56
+ post_install_message:
57
57
  rdoc_options: []
58
58
  require_paths:
59
59
  - lib/
@@ -68,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.0.3
72
- signing_key:
71
+ rubygems_version: 3.4.19
72
+ signing_key:
73
73
  specification_version: 4
74
74
  summary: Ruby analogs to PHP's serialize() and unserialize() functions
75
75
  test_files: