3

I am creating and then writing data to a file (a new 'ESRI Shape file') using PHP, fopen, fseek, pack etc. The file spec is here http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.

The file spec states that the data written needs to be in a combination of the following:

  • Integer: Signed 32-bit integer (4 bytes) - Big Endian
  • Integer: Signed 32-bit integer (4 bytes) - Little Endian
  • Double: Signed 64-bit IEEE double-precision floating point number (8 bytes) - Little Endian

I cant seem to find a pack() format that allows for these formats. I don't want to use a machine dependent format as this code may be running on a variety of platforms.

Can anyone advise on what format (or combination of formats) I need to use for these 3 formats?

Many thanks, Steve

5
  • could you not take a leaf out of encryption and use null byte pkcs padding instead of pack()
    – Dave
    Apr 16, 2013 at 14:30
  • 1
    Alternatively you could maybe use the PECL extension to read and write shapefiles?
    – fvu
    Apr 16, 2013 at 14:37
  • @TML, Oh, I thought he said for PHP. Why is this tagged "Perl" at all?!?! And 'N' and 'V' will work perfectly fine to pack signed ints. Next time, test before contradicting someone!
    – ikegami
    Apr 16, 2013 at 15:17
  • Thanks for the comments. I tagged this as 'perl' as the PHP pack extension is very closely related to perl and the formats are broadly the same - i hoped that the issue could be resolved with a specific format.
    – user8262
    Apr 17, 2013 at 9:12
  • I have spent a couple of hours trying to get the PECL extension working and have failed. I'l running a Ubuntu 12 server and am just getting errors on install. I can only assume that the extension is not compatible - last updated in 2009. Shame - it would have been very useful.
    – user8262
    Apr 17, 2013 at 9:15

2 Answers 2

6

You could check the endianness of the machine running the code and reverse the bytes manually as necessary. The code below should work, but you will only be able to convert one int or float at a time.

define('BIG_ENDIAN', pack('L', 1) === pack('N', 1));

function pack_int32s_be($n) {
    if (BIG_ENDIAN) {
        return pack('l', $n); // that's a lower case L
    }
    return strrev(pack('l', $n));
}
function pack_int32s_le($n) {
    if (BIG_ENDIAN) {
        return strrev(pack('l', $n));
    }
    return pack('l', $n); // that's a lower case L
}
function pack_double_be($n) {
    if (BIG_ENDIAN) {
        return pack('d', $n);
    }
    return strrev(pack('d', $n));
}
function pack_double_le($n) {
    if (BIG_ENDIAN) {
        return strrev(pack('d', $n));
    }
    return pack('d', $n);
}
9
  • It also doesn't handle systems that don't use twos complement signed integers and IEEE 64 bit doubles. There are very few systems where that is the case. If they need to be supported, the only way is to write a custom pack function for those systems.
    – mcrumley
    Apr 16, 2013 at 15:23
  • Really? 'l' isn't even guaranteed to return 2's complement? That's dumb if so. That said, I don't think any computer on which PHP will run uses 1s complement, though, so that was silly of you to bring that up. Perl is used on machines with are neither BE nor LE, so I imagine PHP is too, so that is a real problem if he wants portability to wherever PHP runs.
    – ikegami
    Apr 16, 2013 at 15:28
  • As for IEEE 64-bit doubles, they are extremely common, and the number is only going to go up. I don't know if PHP uses them, but your code fails at the requested portability if it does.
    – ikegami
    Apr 16, 2013 at 15:29
  • PHP does use 64 bit IEEE doubles (at least on the machine I tested), and the code does work for them. The problem is that pack makes no guarantee about the size or format ("machine dependent size and representation").
    – mcrumley
    Apr 16, 2013 at 15:33
  • That question wasn't whether PHP can use doubles. The question is whether PHP can use quads. Your testing doesn't reveal anything useful at all. Keep in mind PHP's docs specifically says it doesn't always use doubles.
    – ikegami
    Apr 16, 2013 at 15:51
5

If PHP doesn't support it, you could implement your own.

function pack_int32be($i) {
   if ($i < -2147483648 || $i > 2147483647) {
      die("Out of bounds");
   }
   return pack('C4',
      ($i >> 24) & 0xFF,
      ($i >> 16) & 0xFF,
      ($i >>  8) & 0xFF,
      ($i >>  0) & 0xFF
   );
}

function pack_int32le($i) {
   if ($i < -2147483648 || $i > 2147483647) {
      die("Out of bounds");
   }
   return pack('C4',
      ($i >>  0) & 0xFF,
      ($i >>  8) & 0xFF,
      ($i >> 16) & 0xFF,
      ($i >> 24) & 0xFF
   );
}

The double-precision LE is much harder. Supporting quad-precision system would involve packing the number using d, converting it to a binary string, splitting the binary into fields, truncating the fields to the right size if they're too large, concatenating the fields, then converting from binary to bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.