PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Crypt/Math/BigInteger/Bcmath.php

https://github.com/mfairchild365/zf2
PHP | 205 lines | 81 code | 16 blank | 108 comment | 5 complexity | dec3391a898f701477fc4863eb16628a 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Crypt\Math\BigInteger;
  25. /**
  26. * Support for arbitrary precision mathematics in PHP.
  27. *
  28. * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
  29. * extension.
  30. *
  31. * @uses Zend\Crypt\Math\BigInteger\BigIntegerCapable
  32. * @category Zend
  33. * @package Zend_Crypt
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Bcmath implements BigIntegerCapable
  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. * @param string $left_operand
  64. * @param string $right_operand
  65. * @return string
  66. */
  67. public function subtract($left_operand, $right_operand)
  68. {
  69. return bcsub($left_operand, $right_operand);
  70. }
  71. /**
  72. * Compare two big integers and returns result as an integer where 0 means
  73. * both are identical, 1 that left_operand is larger, or -1 that
  74. * right_operand is larger.
  75. *
  76. * @param string $left_operand
  77. * @param string $right_operand
  78. * @return int
  79. */
  80. public function compare($left_operand, $right_operand)
  81. {
  82. return bccomp($left_operand, $right_operand);
  83. }
  84. /**
  85. * Divide two big integers and return result or NULL if the denominator
  86. * is zero.
  87. *
  88. * @param string $left_operand
  89. * @param string $right_operand
  90. * @return string|null
  91. */
  92. public function divide($left_operand, $right_operand)
  93. {
  94. return bcdiv($left_operand, $right_operand);
  95. }
  96. /**
  97. * @param string $left_operand
  98. * @param string $right_operand
  99. * @return string
  100. */
  101. public function modulus($left_operand, $modulus)
  102. {
  103. return bcmod($left_operand, $modulus);
  104. }
  105. /**
  106. * @param string $left_operand
  107. * @param string $right_operand
  108. * @return string
  109. */
  110. public function multiply($left_operand, $right_operand)
  111. {
  112. return bcmul($left_operand, $right_operand);
  113. }
  114. /**
  115. * @param string $left_operand
  116. * @param string $right_operand
  117. * @return string
  118. */
  119. public function pow($left_operand, $right_operand)
  120. {
  121. return bcpow($left_operand, $right_operand);
  122. }
  123. /**
  124. * @param string $left_operand
  125. * @param string $right_operand
  126. * @return string
  127. */
  128. public function powmod($left_operand, $right_operand, $modulus)
  129. {
  130. return bcpowmod($left_operand, $right_operand, $modulus);
  131. }
  132. /**
  133. * @param string $left_operand
  134. * @param string $right_operand
  135. * @return string
  136. */
  137. public function sqrt($operand)
  138. {
  139. return bcsqrt($operand);
  140. }
  141. /**
  142. * @param string $operand
  143. * @return integer
  144. */
  145. public function binaryToInteger($operand)
  146. {
  147. $result = '0';
  148. while (strlen($operand)) {
  149. $ord = ord(substr($operand, 0, 1));
  150. $result = bcadd(bcmul($result, 256), $ord);
  151. $operand = substr($operand, 1);
  152. }
  153. return $result;
  154. }
  155. /**
  156. * @param integer $operand
  157. * @return string
  158. */
  159. public function integerToBinary($operand)
  160. {
  161. $cmp = bccomp($operand, 0);
  162. $return = '';
  163. if ($cmp == 0) {
  164. return "\0";
  165. }
  166. while (bccomp($operand, 0) > 0) {
  167. $return = chr(bcmod($operand, 256)) . $return;
  168. $operand = bcdiv($operand, 256);
  169. }
  170. if (ord($return[0]) > 127) {
  171. $return = "\0" . $return;
  172. }
  173. return $return;
  174. }
  175. /**
  176. * @param string $operand
  177. * @return string
  178. */
  179. public function hexToDecimal($operand)
  180. {
  181. $return = '0';
  182. while(strlen($hex)) {
  183. $hex = hexdec(substr($operand, 0, 4));
  184. $dec = bcadd(bcmul($return, 65536), $hex);
  185. $operand = substr($operand, 4);
  186. }
  187. return $return;
  188. }
  189. }