Bcmath.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2014 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. * @see Zend_Crypt_Math_BigInteger_Interface
  24. */
  25. require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
  26. /**
  27. * Support for arbitrary precision mathematics in PHP.
  28. *
  29. * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
  30. * extension.
  31. *
  32. * @category Zend
  33. * @package Zend_Crypt
  34. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
  38. {
  39. /**
  40. * Initialise a big integer into an extension specific type. This is not
  41. * applicable to BCMath.
  42. *
  43. * @param string $operand
  44. * @param int $base
  45. * @return string
  46. */
  47. public function init($operand, $base = 10)
  48. {
  49. return $operand;
  50. }
  51. /**
  52. * Adds two arbitrary precision numbers
  53. *
  54. * @param string $left_operand
  55. * @param string $right_operand
  56. * @return string
  57. */
  58. public function add($left_operand, $right_operand)
  59. {
  60. return bcadd($left_operand, $right_operand);
  61. }
  62. /**
  63. * Subtract one arbitrary precision number from another
  64. *
  65. * @param string $left_operand
  66. * @param string $right_operand
  67. * @return string
  68. */
  69. public function subtract($left_operand, $right_operand)
  70. {
  71. return bcsub($left_operand, $right_operand);
  72. }
  73. /**
  74. * Compare two big integers and returns result as an integer where 0 means
  75. * both are identical, 1 that left_operand is larger, or -1 that
  76. * right_operand is larger.
  77. *
  78. * @param string $left_operand
  79. * @param string $right_operand
  80. * @return int
  81. */
  82. public function compare($left_operand, $right_operand)
  83. {
  84. return bccomp($left_operand, $right_operand);
  85. }
  86. /**
  87. * Divide two big integers and return result or NULL if the denominator
  88. * is zero.
  89. *
  90. * @param string $left_operand
  91. * @param string $right_operand
  92. * @return string|null
  93. */
  94. public function divide($left_operand, $right_operand)
  95. {
  96. return bcdiv($left_operand, $right_operand);
  97. }
  98. /**
  99. * Get modulus of an arbitrary precision number
  100. *
  101. * @param string $left_operand
  102. * @param string $modulus
  103. * @return string
  104. */
  105. public function modulus($left_operand, $modulus)
  106. {
  107. return bcmod($left_operand, $modulus);
  108. }
  109. /**
  110. * Multiply two arbitrary precision numbers
  111. *
  112. * @param string $left_operand
  113. * @param string $right_operand
  114. * @return string
  115. */
  116. public function multiply($left_operand, $right_operand)
  117. {
  118. return bcmul($left_operand, $right_operand);
  119. }
  120. /**
  121. * Raise an arbitrary precision number to another
  122. *
  123. * @param string $left_operand
  124. * @param string $right_operand
  125. * @return string
  126. */
  127. public function pow($left_operand, $right_operand)
  128. {
  129. return bcpow($left_operand, $right_operand);
  130. }
  131. /**
  132. * Raise an arbitrary precision number to another, reduced by a specified
  133. * modulus
  134. *
  135. * @param string $left_operand
  136. * @param string $right_operand
  137. * @param string $modulus
  138. * @return string
  139. */
  140. public function powmod($left_operand, $right_operand, $modulus)
  141. {
  142. return bcpowmod($left_operand, $right_operand, $modulus);
  143. }
  144. /**
  145. * Get the square root of an arbitrary precision number
  146. *
  147. * @param string $operand
  148. * @return string
  149. */
  150. public function sqrt($operand)
  151. {
  152. return bcsqrt($operand);
  153. }
  154. /**
  155. * @param string $operand
  156. * @return string
  157. */
  158. public function binaryToInteger($operand)
  159. {
  160. $result = '0';
  161. while (strlen($operand)) {
  162. $ord = ord(substr($operand, 0, 1));
  163. $result = bcadd(bcmul($result, 256), $ord);
  164. $operand = substr($operand, 1);
  165. }
  166. return $result;
  167. }
  168. /**
  169. * @param string $operand
  170. * @return string
  171. */
  172. public function integerToBinary($operand)
  173. {
  174. $cmp = bccomp($operand, 0);
  175. $return = '';
  176. if ($cmp == 0) {
  177. return "\0";
  178. }
  179. while (bccomp($operand, 0) > 0) {
  180. $return = chr(bcmod($operand, 256)) . $return;
  181. $operand = bcdiv($operand, 256);
  182. }
  183. if (ord($return[0]) > 127) {
  184. $return = "\0" . $return;
  185. }
  186. return $return;
  187. }
  188. /**public function integerToBinary($operand)
  189. {
  190. $return = '';
  191. while(bccomp($operand, '0')) {
  192. $return .= chr(bcmod($operand, '256'));
  193. $operand = bcdiv($operand, '256');
  194. }
  195. return $return;
  196. }**/ // Prior version for referenced offset
  197. /**
  198. * @param string $operand
  199. * @return string
  200. */
  201. public function hexToDecimal($operand)
  202. {
  203. $return = '0';
  204. while(strlen($hex)) {
  205. $hex = hexdec(substr($operand, 0, 4));
  206. $dec = bcadd(bcmul($return, 65536), $hex);
  207. $operand = substr($operand, 4);
  208. }
  209. return $return;
  210. }
  211. }