Index: includes/geo.wkb.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/geo/includes/geo.wkb.inc,v
retrieving revision 1.6
diff -u -r1.6 geo.wkb.inc
--- includes/geo.wkb.inc	7 Apr 2009 21:48:14 -0000	1.6
+++ includes/geo.wkb.inc	9 Apr 2009 18:58:30 -0000
@@ -14,7 +14,7 @@
   return $geo_types;
 }
 
-function geo_wkb_get_data($wkb = NULL, $format = 'text', $fp = NULL, $type = NULL) {
+function geo_wkb_get_data($wkb = NULL, $format = 'text', $fp = NULL, $type = NULL, $byte_order = NULL) {
   $data = array();
 
   if (!$fp) {
@@ -25,7 +25,9 @@
     fseek($fp, 0);
 
     // Fetch the byte order (0 = Big Endian, 1 = Little Endian) and geo type.
-    $data = unpack('cbyte_order/Ltype', fread($fp, 5));
+    $data = unpack('cbyte_order', fread($fp, 1));
+    $byte_order = $data['byte_order'];
+    $data = array_merge($data, unpack(($byte_order ? 'Vtype' : 'Ntype'), fread($fp, 4)));
 
     // Set the geometry type
     $data['type'] = geo_wkb_types($data['type']);
@@ -38,24 +40,30 @@
   switch($type) {
 
     case 'point': // Contains x, y decimal values.
-      $data = array_merge($data, unpack('dx/dy', fread($fp, 16)));
+      $tmp = fread($fp, 16);
+      // Check whether the data's byte order matches the machine's byte order.
+      // If not, we need to reverse the data before PHP can unpack it.
+      if ($byte_order != unpack('c', pack('S', 1))) {
+        $tmp = strrev($tmp);
+      }
+      $data = array_merge($data, unpack('dx/dy', $tmp));
       $data['value'] = $func($data);
       if (function_exists($post = $func .'_post')) $data = $post($data);
       return $data;
 
     case 'linestring': // Contains count * (point) values.
-      $data['count'] = current(unpack('L', fread($fp, 4)));
+      $data['count'] = current(unpack(($byte_order ? 'V' : 'N'), fread($fp, 4)));
       for ($i = 1; $i <= $data['count']; $i++) {
-        $point = geo_wkb_get_data(NULL, $format, $fp, 'point');
+        $point = geo_wkb_get_data(NULL, $format, $fp, 'point', $byte_order);
         $data['value'] = $func($point, $data['value']);
       }
       if (function_exists($post = $func .'_post')) $data = $post($data);
       return $data;
 
     case 'polygon': // Contains count * (linestring) items.
-      $data['count'] = current(unpack('L', fread($fp, 4)));
+      $data['count'] = current(unpack(($byte_order ? 'V' : 'N'), fread($fp, 4)));
       for ($i = 1; $i <= $data['count']; $i++) {
-        $line = geo_wkb_get_data(NULL, $format, $fp, 'linestring');
+        $line = geo_wkb_get_data(NULL, $format, $fp, 'linestring', $byte_order);
         $data['value'] = $func($line, $data['value']);
       }
       if (function_exists($post = $func .'_post')) $data = $post($data);
