2
0

PackUtil.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace PhpZip\Util;
  3. use PhpZip\Exception\ZipException;
  4. /**
  5. * Pack util
  6. *
  7. * @author Ne-Lexa alexey@nelexa.ru
  8. * @license MIT
  9. */
  10. class PackUtil
  11. {
  12. /**
  13. * @param int|string $longValue
  14. * @return string
  15. */
  16. public static function packLongLE($longValue)
  17. {
  18. if (PHP_INT_SIZE === 8 && PHP_VERSION_ID >= 506030) {
  19. return pack("P", $longValue);
  20. }
  21. $left = 0xffffffff00000000;
  22. $right = 0x00000000ffffffff;
  23. $r = ($longValue & $left) >> 32;
  24. $l = $longValue & $right;
  25. return pack('VV', $l, $r);
  26. }
  27. /**
  28. * @param string|int $value
  29. * @return int
  30. * @throws ZipException
  31. */
  32. public static function unpackLongLE($value)
  33. {
  34. if (PHP_INT_SIZE === 8 && PHP_VERSION_ID >= 506030) {
  35. return unpack('P', $value)[1];
  36. }
  37. $unpack = unpack('Va/Vb', $value);
  38. return $unpack['a'] + ($unpack['b'] << 32);
  39. }
  40. /**
  41. * Cast to signed int 32-bit
  42. *
  43. * @param int $int
  44. * @return int
  45. */
  46. public static function toSignedInt32($int)
  47. {
  48. if (PHP_INT_SIZE === 8) {
  49. $int = $int & 0xffffffff;
  50. if ($int & 0x80000000) {
  51. return $int - 0x100000000;
  52. }
  53. }
  54. return $int;
  55. }
  56. }