PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Crypt/Math/BigInteger/Bcmath.php

https://bitbucket.org/gkawka/zend-framework
PHP | 203 lines | 81 code | 20 blank | 102 comment | 5 complexity | 692f50273a35359b9fe9677504da6fdb MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Bcmath.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. * @param string $operand
  43. * @param int $base
  44. * @return string
  45. */
  46. public function init($operand, $base = 10)
  47. {
  48. return $operand;
  49. }
  50. /**
  51. * Adds two arbitrary precision numbers
  52. *
  53. * @param string $left_operand
  54. * @param string $right_operand
  55. * @return string
  56. */
  57. public function add($left_operand, $right_operand)
  58. {
  59. return bcadd($left_operand, $right_operand);
  60. }
  61. /**
  62. * @param string $left_operand
  63. * @param string $right_operand
  64. * @return string
  65. */
  66. public function subtract($left_operand, $right_operand)
  67. {
  68. return bcsub($left_operand, $right_operand);
  69. }
  70. /**
  71. * Compare two big integers and returns result as an integer where 0 means
  72. * both are identical, 1 that left_operand is larger, or -1 that
  73. * right_operand is larger.
  74. * @param string $left_operand
  75. * @param string $right_operand
  76. * @return int
  77. */
  78. public function compare($left_operand, $right_operand)
  79. {
  80. return bccomp($left_operand, $right_operand);
  81. }
  82. /**
  83. * Divide two big integers and return result or NULL if the denominator
  84. * is zero.
  85. * @param string $left_operand
  86. * @param string $right_operand
  87. * @return string|null
  88. */
  89. public function divide($left_operand, $right_operand)
  90. {
  91. return bcdiv($left_operand, $right_operand);
  92. }
  93. /**
  94. * @param string $left_operand
  95. * @param string $right_operand
  96. * @return string
  97. */
  98. public function modulus($left_operand, $modulus)
  99. {
  100. return bcmod($left_operand, $modulus);
  101. }
  102. /**
  103. * @param string $left_operand
  104. * @param string $right_operand
  105. * @return string
  106. */
  107. public function multiply($left_operand, $right_operand)
  108. {
  109. return bcmul($left_operand, $right_operand);
  110. }
  111. /**
  112. * @param string $left_operand
  113. * @param string $right_operand
  114. * @return string
  115. */
  116. public function pow($left_operand, $right_operand)
  117. {
  118. return bcpow($left_operand, $right_operand);
  119. }
  120. /**
  121. * @param string $left_operand
  122. * @param string $right_operand
  123. * @return string
  124. */
  125. public function powmod($left_operand, $right_operand, $modulus)
  126. {
  127. return bcpowmod($left_operand, $right_operand, $modulus);
  128. }
  129. /**
  130. * @param string $left_operand
  131. * @param string $right_operand
  132. * @return string
  133. */
  134. public function sqrt($operand)
  135. {
  136. return bcsqrt($operand);
  137. }
  138. public function binaryToInteger($operand)
  139. {
  140. $result = '0';
  141. while (strlen($operand)) {
  142. $ord = ord(substr($operand, 0, 1));
  143. $result = bcadd(bcmul($result, 256), $ord);
  144. $operand = substr($operand, 1);
  145. }
  146. return $result;
  147. }
  148. public function integerToBinary($operand)
  149. {
  150. $cmp = bccomp($operand, 0);
  151. $return = '';
  152. if ($cmp == 0) {
  153. return "\0";
  154. }
  155. while (bccomp($operand, 0) > 0) {
  156. $return = chr(bcmod($operand, 256)) . $return;
  157. $operand = bcdiv($operand, 256);
  158. }
  159. if (ord($return[0]) > 127) {
  160. $return = "\0" . $return;
  161. }
  162. return $return;
  163. }
  164. /**public function integerToBinary($operand)
  165. {
  166. $return = '';
  167. while(bccomp($operand, '0')) {
  168. $return .= chr(bcmod($operand, '256'));
  169. $operand = bcdiv($operand, '256');
  170. }
  171. return $return;
  172. }**/ // Prior version for referenced offset
  173. public function hexToDecimal($operand)
  174. {
  175. $return = '0';
  176. while(strlen($hex)) {
  177. $hex = hexdec(substr($operand, 0, 4));
  178. $dec = bcadd(bcmul($return, 65536), $hex);
  179. $operand = substr($operand, 4);
  180. }
  181. return $return;
  182. }
  183. }