BigInteger.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage Math
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Support for arbitrary precision mathematics in PHP.
  24. *
  25. * Zend_Crypt_Math_BigInteger is a wrapper across three PHP extensions: bcmath, gmp
  26. * and big_int. Since each offer similar functionality, but availability of
  27. * each differs across installations of PHP, this wrapper attempts to select
  28. * the fastest option available and encapsulate a subset of its functionality
  29. * which all extensions share in common.
  30. *
  31. * This class requires one of the three extensions to be available. BCMATH
  32. * while the slowest, is available by default under Windows, and under Unix
  33. * if PHP is compiled with the flag "--enable-bcmath". GMP requires the gmp
  34. * library from http://www.swox.com/gmp/ and PHP compiled with the "--with-gmp"
  35. * flag. BIG_INT support is available from a big_int PHP library available from
  36. * only from PECL (a Windows port is not available).
  37. *
  38. * @category Zend
  39. * @package Zend_Crypt
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Crypt_Math_BigInteger
  44. {
  45. /**
  46. * Holds an instance of one of the three arbitrary precision wrappers.
  47. *
  48. * @var Zend_Crypt_Math_BigInteger_Interface
  49. */
  50. protected $_math = null;
  51. /**
  52. * Constructor; a Factory which detects a suitable PHP extension for
  53. * arbitrary precision math and instantiates the suitable wrapper
  54. * object.
  55. *
  56. * @param string $extension
  57. * @throws Zend_Crypt_Math_BigInteger_Exception
  58. */
  59. public function __construct($extension = null)
  60. {
  61. if ($extension !== null && !in_array($extension, array('bcmath', 'gmp', 'bigint'))) {
  62. #require_once('Zend/Crypt/Math/BigInteger/Exception.php');
  63. throw new Zend_Crypt_Math_BigInteger_Exception('Invalid extension type; please use one of bcmath, gmp or bigint');
  64. }
  65. $this->_loadAdapter($extension);
  66. }
  67. /**
  68. * Redirect all public method calls to the wrapped extension object.
  69. *
  70. * @param string $methodName
  71. * @param array $args
  72. * @return mixed
  73. * @throws Zend_Crypt_Math_BigInteger_Exception
  74. */
  75. public function __call($methodName, $args)
  76. {
  77. if(!method_exists($this->_math, $methodName)) {
  78. #require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  79. throw new Zend_Crypt_Math_BigInteger_Exception('invalid method call: ' . get_class($this->_math) . '::' . $methodName . '() does not exist');
  80. }
  81. return call_user_func_array(array($this->_math, $methodName), $args);
  82. }
  83. /**
  84. * @param string $extension
  85. * @throws Zend_Crypt_Math_BigInteger_Exception
  86. */
  87. protected function _loadAdapter($extension = null)
  88. {
  89. if ($extension === null) {
  90. if (extension_loaded('gmp')) {
  91. $extension = 'gmp';
  92. //} elseif (extension_loaded('big_int')) {
  93. // $extension = 'big_int';
  94. } else {
  95. $extension = 'bcmath';
  96. }
  97. }
  98. if($extension == 'gmp' && extension_loaded('gmp')) {
  99. #require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
  100. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp();
  101. //} elseif($extension == 'bigint' && extension_loaded('big_int')) {
  102. // #require_once 'Zend/Crypt_Math/BigInteger/Bigint.php';
  103. // $this->_math = new Zend_Crypt_Math_BigInteger_Bigint();
  104. } elseif ($extension == 'bcmath' && extension_loaded('bcmath')) {
  105. #require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  106. $this->_math = new Zend_Crypt_Math_BigInteger_Bcmath();
  107. } else {
  108. #require_once 'Zend/Crypt/Math/BigInteger/Exception.php';
  109. throw new Zend_Crypt_Math_BigInteger_Exception($extension . ' big integer precision math support not detected');
  110. }
  111. }
  112. }