/mimula/system/application/libraries/Zend/library/Zend/Crypt/Math/BigInteger/Bcmath.php

https://github.com/alvaropereyra/shrekcms · PHP · 206 lines · 81 code · 20 blank · 105 comment · 5 complexity · 0265da1333f0df78511cf6a8cc422345 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. * 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_Bcmath is a wrapper across the PHP BCMath
  32. * extension.
  33. *
  34. * @category Zend
  35. * @package Zend_Crypt
  36. * @subpackage Math
  37. * @author Pádraic Brady (http://blog.astrumfutura.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
  41. {
  42. /**
  43. * Initialise a big integer into an extension specific type. This is not
  44. * applicable to BCMath.
  45. * @param string $operand
  46. * @param int $base
  47. * @return string
  48. */
  49. public function init($operand, $base = 10)
  50. {
  51. return $operand;
  52. }
  53. /**
  54. * Adds two arbitrary precision numbers
  55. *
  56. * @param string $left_operand
  57. * @param string $right_operand
  58. * @return string
  59. */
  60. public function add($left_operand, $right_operand)
  61. {
  62. return bcadd($left_operand, $right_operand);
  63. }
  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. * @param string $left_operand
  78. * @param string $right_operand
  79. * @return int
  80. */
  81. public function compare($left_operand, $right_operand)
  82. {
  83. return bccomp($left_operand, $right_operand);
  84. }
  85. /**
  86. * Divide two big integers and return result or NULL if the denominator
  87. * is zero.
  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. public function binaryToInteger($operand)
  142. {
  143. $result = '0';
  144. while (strlen($operand)) {
  145. $ord = ord(substr($operand, 0, 1));
  146. $result = bcadd(bcmul($result, 256), $ord);
  147. $operand = substr($operand, 1);
  148. }
  149. return $result;
  150. }
  151. public function integerToBinary($operand)
  152. {
  153. $cmp = bccomp($operand, 0);
  154. $return = '';
  155. if ($cmp == 0) {
  156. return (chr(0));
  157. }
  158. while (bccomp($operand, 0) > 0) {
  159. $return = chr(bcmod($operand, 256)) . $return;
  160. $operand = bcdiv($operand, 256);
  161. }
  162. if (ord($return[0]) > 127) {
  163. $return = chr(0) . $return;
  164. }
  165. return $return;
  166. }
  167. /**public function integerToBinary($operand)
  168. {
  169. $return = '';
  170. while(bccomp($operand, '0')) {
  171. $return .= chr(bcmod($operand, '256'));
  172. $operand = bcdiv($operand, '256');
  173. }
  174. return $return;
  175. }**/ // Prior version for referenced offset
  176. public function hexToDecimal($operand)
  177. {
  178. $return = '0';
  179. while(strlen($hex)) {
  180. $hex = hexdec(substr($operand, 0, 4));
  181. $dec = bcadd(bcmul($return, 65536), $hex);
  182. $operand = substr($operand, 4);
  183. }
  184. return $return;
  185. }
  186. }