web/lib/Zend/Crypt/Math/BigInteger/Bcmath.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Crypt
    16  * @package    Zend_Crypt
    17  * @subpackage Math
    17  * @subpackage Math
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    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
    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 $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Crypt_Math_BigInteger_Interface
    24  * @see Zend_Crypt_Math_BigInteger_Interface
    25  */
    25  */
    31  * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
    31  * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
    32  * extension.
    32  * extension.
    33  *
    33  *
    34  * @category   Zend
    34  * @category   Zend
    35  * @package    Zend_Crypt
    35  * @package    Zend_Crypt
    36  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  */
    38  */
    39 class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
    39 class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
    40 {
    40 {
    41 
    41 
    42     /**
    42     /**
    43      * Initialise a big integer into an extension specific type. This is not
    43      * Initialise a big integer into an extension specific type. This is not
    44      * applicable to BCMath.
    44      * applicable to BCMath.
       
    45      *
    45      * @param string $operand
    46      * @param string $operand
    46      * @param int $base
    47      * @param int $base
    47      * @return string
    48      * @return string
    48      */
    49      */
    49     public function init($operand, $base = 10)
    50     public function init($operand, $base = 10)
    62     {
    63     {
    63         return bcadd($left_operand, $right_operand);
    64         return bcadd($left_operand, $right_operand);
    64     }
    65     }
    65 
    66 
    66     /**
    67     /**
       
    68      * Subtract one arbitrary precision number from another
       
    69      *
    67      * @param string $left_operand
    70      * @param string $left_operand
    68      * @param string $right_operand
    71      * @param string $right_operand
    69      * @return string
    72      * @return string
    70      */
    73      */
    71     public function subtract($left_operand, $right_operand)
    74     public function subtract($left_operand, $right_operand)
    75 
    78 
    76     /**
    79     /**
    77      * Compare two big integers and returns result as an integer where 0 means
    80      * Compare two big integers and returns result as an integer where 0 means
    78      * both are identical, 1 that left_operand is larger, or -1 that
    81      * both are identical, 1 that left_operand is larger, or -1 that
    79      * right_operand is larger.
    82      * right_operand is larger.
       
    83      *
    80      * @param string $left_operand
    84      * @param string $left_operand
    81      * @param string $right_operand
    85      * @param string $right_operand
    82      * @return int
    86      * @return int
    83      */
    87      */
    84     public function compare($left_operand, $right_operand)
    88     public function compare($left_operand, $right_operand)
    87     }
    91     }
    88 
    92 
    89     /**
    93     /**
    90      * Divide two big integers and return result or NULL if the denominator
    94      * Divide two big integers and return result or NULL if the denominator
    91      * is zero.
    95      * is zero.
       
    96      *
    92      * @param string $left_operand
    97      * @param string $left_operand
    93      * @param string $right_operand
    98      * @param string $right_operand
    94      * @return string|null
    99      * @return string|null
    95      */
   100      */
    96     public function divide($left_operand, $right_operand)
   101     public function divide($left_operand, $right_operand)
    97     {
   102     {
    98         return bcdiv($left_operand, $right_operand);
   103         return bcdiv($left_operand, $right_operand);
    99     }
   104     }
   100 
   105 
   101     /**
   106     /**
   102      * @param string $left_operand
   107      * Get modulus of an arbitrary precision number
   103      * @param string $right_operand
   108      *
       
   109      * @param string $left_operand
       
   110      * @param string $modulus
   104      * @return string
   111      * @return string
   105      */
   112      */
   106     public function modulus($left_operand, $modulus)
   113     public function modulus($left_operand, $modulus)
   107     {
   114     {
   108         return bcmod($left_operand, $modulus);
   115         return bcmod($left_operand, $modulus);
   109     }
   116     }
   110 
   117 
   111     /**
   118     /**
       
   119      * Multiply two arbitrary precision numbers
       
   120      *
   112      * @param string $left_operand
   121      * @param string $left_operand
   113      * @param string $right_operand
   122      * @param string $right_operand
   114      * @return string
   123      * @return string
   115      */
   124      */
   116     public function multiply($left_operand, $right_operand)
   125     public function multiply($left_operand, $right_operand)
   117     {
   126     {
   118         return bcmul($left_operand, $right_operand);
   127         return bcmul($left_operand, $right_operand);
   119     }
   128     }
   120 
   129 
   121     /**
   130     /**
       
   131      * Raise an arbitrary precision number to another
       
   132      *
   122      * @param string $left_operand
   133      * @param string $left_operand
   123      * @param string $right_operand
   134      * @param string $right_operand
   124      * @return string
   135      * @return string
   125      */
   136      */
   126     public function pow($left_operand, $right_operand)
   137     public function pow($left_operand, $right_operand)
   127     {
   138     {
   128         return bcpow($left_operand, $right_operand);
   139         return bcpow($left_operand, $right_operand);
   129     }
   140     }
   130 
   141 
   131     /**
   142     /**
   132      * @param string $left_operand
   143      * Raise an arbitrary precision number to another, reduced by a specified
   133      * @param string $right_operand
   144      * modulus
       
   145      *
       
   146      * @param string $left_operand
       
   147      * @param string $right_operand
       
   148      * @param string $modulus
   134      * @return string
   149      * @return string
   135      */
   150      */
   136     public function powmod($left_operand, $right_operand, $modulus)
   151     public function powmod($left_operand, $right_operand, $modulus)
   137     {
   152     {
   138         return bcpowmod($left_operand, $right_operand, $modulus);
   153         return bcpowmod($left_operand, $right_operand, $modulus);
   139     }
   154     }
   140 
   155 
   141     /**
   156     /**
   142      * @param string $left_operand
   157      * Get the square root of an arbitrary precision number
   143      * @param string $right_operand
   158      *
       
   159      * @param string $operand
   144      * @return string
   160      * @return string
   145      */
   161      */
   146     public function sqrt($operand)
   162     public function sqrt($operand)
   147     {
   163     {
   148         return bcsqrt($operand);
   164         return bcsqrt($operand);
   149     }
   165     }
   150 
   166 
   151 
   167     /**
       
   168      * @param string $operand
       
   169      * @return string
       
   170      */
   152     public function binaryToInteger($operand)
   171     public function binaryToInteger($operand)
   153     {
   172     {
   154         $result = '0';
   173         $result = '0';
   155         while (strlen($operand)) {
   174         while (strlen($operand)) {
   156             $ord = ord(substr($operand, 0, 1));
   175             $ord = ord(substr($operand, 0, 1));
   158             $operand = substr($operand, 1);
   177             $operand = substr($operand, 1);
   159         }
   178         }
   160         return $result;
   179         return $result;
   161     }
   180     }
   162 
   181 
   163 
   182     /**
       
   183      * @param string $operand
       
   184      * @return string
       
   185      */
   164     public function integerToBinary($operand)
   186     public function integerToBinary($operand)
   165     {
   187     {
   166         $cmp = bccomp($operand, 0);
   188         $cmp = bccomp($operand, 0);
   167         $return = '';
   189         $return = '';
   168         if ($cmp == 0) {
   190         if ($cmp == 0) {
   186             $operand = bcdiv($operand, '256');
   208             $operand = bcdiv($operand, '256');
   187         }
   209         }
   188         return $return;
   210         return $return;
   189     }**/ // Prior version for referenced offset
   211     }**/ // Prior version for referenced offset
   190 
   212 
   191 
   213     /**
       
   214      * @param string $operand
       
   215      * @return string
       
   216      */
   192     public function hexToDecimal($operand)
   217     public function hexToDecimal($operand)
   193     {
   218     {
   194         $return = '0';
   219         $return = '0';
   195         while(strlen($hex)) {
   220         while(strlen($hex)) {
   196             $hex = hexdec(substr($operand, 0, 4));
   221             $hex = hexdec(substr($operand, 0, 4));
   197             $dec = bcadd(bcmul($return, 65536), $hex);
   222             $dec = bcadd(bcmul($return, 65536), $hex);
   198             $operand = substr($operand, 4);
   223             $operand = substr($operand, 4);
   199         }
   224         }
   200         return $return;
   225         return $return;
   201     }
   226     }
   202 
       
   203 }
   227 }