BigInteger.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * This class forms part of a proposal for the Zend Framework. The attached
  16. * copyright will be transferred to Zend Technologies USA Inc. upon future
  17. * acceptance of that proposal:
  18. * http://framework.zend.com/wiki/pages/viewpage.action?pageId=20369
  19. *
  20. * @category Zend
  21. * @package Zend_Crypt
  22. * @subpackage Math
  23. * @copyright Copyright (c) 2007 Pádraic Brady (http://blog.astrumfutura.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. /** Zend_Crypt_Math_BigInteger_Interface */
  27. require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
  28. /**
  29. * Support for arbitrary precision mathematics in PHP.
  30. *
  31. * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp
  32. * and big_int. Since each offer similar functionality, but availability of
  33. * each differs across installations of PHP, this wrapper attempts to select
  34. * the fastest option available and encapsulate a subset of its functionality
  35. * which all extensions share in common.
  36. *
  37. * This class requires one of the three extensions to be available. BCMATH
  38. * while the slowest, is available by default under Windows, and under Unix
  39. * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp
  40. * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp"
  41. * flag. BIG_INT support is available from a big_int PHP library available from
  42. * only from PECL (a Windows port is not available).
  43. *
  44. * @category Zend
  45. * @package Zend_Crypt
  46. * @subpackage Math
  47. * @author Pádraic Brady (http://blog.astrumfutura.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. */
  50. class Zend_Crypt_Math_BigInteger
  51. {
  52. /**
  53. * Holds an instance of one of the three arbitrary precision wrappers.
  54. *
  55. * @var Zend_Math_BigInteger_Interface
  56. */
  57. protected $_math = null;
  58. /**
  59. * Constructor; a Factory which detects a suitable PHP extension for
  60. * arbitrary precision math and instantiates the suitable wrapper
  61. * object.
  62. *
  63. * @throws Zend_Math_BigInteger_Exception
  64. */
  65. public function __construct($extension = null)
  66. {
  67. if (!is_null($extension) && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) {
  68. require_once('Zend/Crypt/Math/BigInteger/Exception.php');
  69. throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint');
  70. }
  71. $this->_loadAdapter($extension);
  72. }
  73. /**
  74. * Redirect all public method calls to the wrapped extension object.
  75. *
  76. * @param string $methodName
  77. * @param array $args
  78. * @throws Zend_Crypt_Math_BigInteger_Exception
  79. */
  80. public function __call($methodName, $args)
  81. {
  82. if(!method_exists($this->_math, $methodName)) {
  83. require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  84. throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
  85. }
  86. return call_user_func_array(array($this->_math, $methodName), $args);
  87. }
  88. protected function _loadAdapter($extension = null)
  89. {
  90. if (is_null($extension)) {
  91. if (extension_loaded('gmp')) {
  92. $extension = 'gmp';
  93. //} elseif (extension_loaded('big_int')) {
  94. // $extension = 'big_int';
  95. } else {
  96. $extension = 'bcmath';
  97. }
  98. }
  99. if($extension == 'gmp' && extension_loaded('gmp')) {
  100. require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
  101. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
  102. //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
  103. // require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
  104. // $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
  105. } elseif ($extension == 'bcmath') {
  106. require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  107. $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
  108. } else {
  109. require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  110. throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');
  111. }
  112. }
  113. }