You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by jf...@apache.org on 2011/06/03 19:03:01 UTC

svn commit: r1131110 - /thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm

Author: jfarrell
Date: Fri Jun  3 17:03:00 2011
New Revision: 1131110

URL: http://svn.apache.org/viewvc?rev=1131110&view=rev
Log:
Thrift-1171: Perl write/readDouble assumes little-endian platform
Client: perl
Patch: Andy Grundman

The code for handling doubles uses pack 'd' (pack to native byte order) and then reverses the bytes. This works on little-endian systems but will produce backwards data on big-endian systems.


Modified:
    thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm

Modified: thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm?rev=1131110&r1=1131109&r2=1131110&view=diff
==============================================================================
--- thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm (original)
+++ thrift/trunk/lib/perl/lib/Thrift/BinaryProtocol.pm Fri Jun  3 17:03:00 2011
@@ -38,6 +38,7 @@ use base('Thrift::Protocol');
 
 use constant VERSION_MASK   => 0xffff0000;
 use constant VERSION_1      => 0x80010000;
+use constant IS_BIG_ENDIAN  => unpack("h*", pack("s", 1)) =~ /01/;
 
 sub new
 {
@@ -211,7 +212,12 @@ sub writeDouble
     my $value= shift;
 
     my $data = pack('d', $value);
-    $self->{trans}->write(scalar reverse($data), 8);
+    if (IS_BIG_ENDIAN) {
+      $self->{trans}->write($data, 8);
+    }
+    else {
+      $self->{trans}->write(scalar reverse($data), 8);
+    }
     return 8;
 }
 
@@ -434,7 +440,14 @@ sub readDouble
     my $self  = shift;
     my $value = shift;
 
-    my $data = scalar reverse($self->{trans}->readAll(8));
+    my $data;
+    if (IS_BIG_ENDIAN) {
+      $data = $self->{trans}->readAll(8);
+    }
+    else {
+      $data = scalar reverse($self->{trans}->readAll(8));
+    }
+    
     my @arr = unpack('d', $data);
 
     $$value = $arr[0];