P
Pat Patterson
I'm implementing a spec that calls for messages to be deflated, then
base64 encoded, then URL encoded, so they can be passed as parameters to
an HTTP GET. In PHP, this is achieved with:
$encodedMsg = urlencode( base64_encode( gzdeflate( $msg ) ) )
Looking at the docs, the equivalent in Ruby would be something like:
require "cgi"
require "base64"
require "zlib"
encodedMsg = CGI::escape( Base64.encode64( Zlib:
eflate.deflate( msg ) ) )
But this gives me completely different output to my (working) PHP model.
Some googling later, it appears that Zlib:
eflate.deflate prepends and
appends 'stuff' to the deflated data. Investigating, the PHP
$deflated = gzdeflate( "Hello world" );
echo bin2hex( $deflated );
Gives me
f348cdc9c95728cf2fca490100
While the Ruby
deflated = Zlib:
eflate.deflate( "Hello world" )
myhex = ""
1.upto(deflated.length) { |i| myhex << "%02x" % deflated }
puts myhex
Shows
9cf348cdc9c95728cf2fca49010018ab043d00
(BTW - if anyone knows a more succinct way to hex encode a string in
Ruby, that would be useful)
Notice the additional '9c' at the start of the string and '18ab043d00'
at the end. OK - there seems to be a consistent amount of data prepended
and appended, so I can slice those off.
But now Base 64 is acting weird. In PHP:
$base64Encoded = base64_encode( $deflated );
echo $base64Encoded;
Shows
80jNyclXKM8vykkBAA==
While, in Ruby
# Remove extra stuff from deflated string
deflated = deflated[1,deflated.length-6]
base64encoded = Base64.encode64( deflated )
puts base64encoded
Shows
nPNIzcnJVyjPL8pJAQ==
Completely different! Now, if I feed that back through PHP's
base64_unencode, I get
9cf348cdc9c95728cf2fca4901
Notice, again '9c' prepended, but this time the trailing '00' has been
removed.
What is going on here? Any ideas??? Am I missing something really
obvious about string handling in Ruby?
(BTW - this is on ruby 1.8.4 (2005-12-24) [i486-linux])
Cheers,
Pat
base64 encoded, then URL encoded, so they can be passed as parameters to
an HTTP GET. In PHP, this is achieved with:
$encodedMsg = urlencode( base64_encode( gzdeflate( $msg ) ) )
Looking at the docs, the equivalent in Ruby would be something like:
require "cgi"
require "base64"
require "zlib"
encodedMsg = CGI::escape( Base64.encode64( Zlib:
But this gives me completely different output to my (working) PHP model.
Some googling later, it appears that Zlib:
appends 'stuff' to the deflated data. Investigating, the PHP
$deflated = gzdeflate( "Hello world" );
echo bin2hex( $deflated );
Gives me
f348cdc9c95728cf2fca490100
While the Ruby
deflated = Zlib:
myhex = ""
1.upto(deflated.length) { |i| myhex << "%02x" % deflated }
puts myhex
Shows
9cf348cdc9c95728cf2fca49010018ab043d00
(BTW - if anyone knows a more succinct way to hex encode a string in
Ruby, that would be useful)
Notice the additional '9c' at the start of the string and '18ab043d00'
at the end. OK - there seems to be a consistent amount of data prepended
and appended, so I can slice those off.
But now Base 64 is acting weird. In PHP:
$base64Encoded = base64_encode( $deflated );
echo $base64Encoded;
Shows
80jNyclXKM8vykkBAA==
While, in Ruby
# Remove extra stuff from deflated string
deflated = deflated[1,deflated.length-6]
base64encoded = Base64.encode64( deflated )
puts base64encoded
Shows
nPNIzcnJVyjPL8pJAQ==
Completely different! Now, if I feed that back through PHP's
base64_unencode, I get
9cf348cdc9c95728cf2fca4901
Notice, again '9c' prepended, but this time the trailing '00' has been
removed.
What is going on here? Any ideas??? Am I missing something really
obvious about string handling in Ruby?
(BTW - this is on ruby 1.8.4 (2005-12-24) [i486-linux])
Cheers,
Pat