/library/Zend/Crypt/Math/BigInteger/Bcmath.php
PHP | 203 lines | 81 code | 20 blank | 102 comment | 5 complexity | af4f1b41243ae1bd6093096700aefaeb MD5 | raw file
Possible License(s): AGPL-1.0
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 24594 2012-01-05 21:27:01Z matthew $
21 */
22
23/**
24 * @see Zend_Crypt_Math_BigInteger_Interface
25 */
26require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
27
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 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
38 */
39class Zend_Crypt_Math_BigInteger_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
40{
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 /**
55 * Adds two arbitrary precision numbers
56 *
57 * @param string $left_operand
58 * @param string $right_operand
59 * @return string
60 */
61 public function add($left_operand, $right_operand)
62 {
63 return bcadd($left_operand, $right_operand);
64 }
65
66 /**
67 * @param string $left_operand
68 * @param string $right_operand
69 * @return string
70 */
71 public function subtract($left_operand, $right_operand)
72 {
73 return bcsub($left_operand, $right_operand);
74 }
75
76 /**
77 * 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
79 * right_operand is larger.
80 * @param string $left_operand
81 * @param string $right_operand
82 * @return int
83 */
84 public function compare($left_operand, $right_operand)
85 {
86 return bccomp($left_operand, $right_operand);
87 }
88
89 /**
90 * Divide two big integers and return result or NULL if the denominator
91 * is zero.
92 * @param string $left_operand
93 * @param string $right_operand
94 * @return string|null
95 */
96 public function divide($left_operand, $right_operand)
97 {
98 return bcdiv($left_operand, $right_operand);
99 }
100
101 /**
102 * @param string $left_operand
103 * @param string $right_operand
104 * @return string
105 */
106 public function modulus($left_operand, $modulus)
107 {
108 return bcmod($left_operand, $modulus);
109 }
110
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 /**
122 * @param string $left_operand
123 * @param string $right_operand
124 * @return string
125 */
126 public function pow($left_operand, $right_operand)
127 {
128 return bcpow($left_operand, $right_operand);
129 }
130
131 /**
132 * @param string $left_operand
133 * @param string $right_operand
134 * @return string
135 */
136 public function powmod($left_operand, $right_operand, $modulus)
137 {
138 return bcpowmod($left_operand, $right_operand, $modulus);
139 }
140
141 /**
142 * @param string $left_operand
143 * @param string $right_operand
144 * @return string
145 */
146 public function sqrt($operand)
147 {
148 return bcsqrt($operand);
149 }
150
151
152 public function binaryToInteger($operand)
153 {
154 $result = '0';
155 while (strlen($operand)) {
156 $ord = ord(substr($operand, 0, 1));
157 $result = bcadd(bcmul($result, 256), $ord);
158 $operand = substr($operand, 1);
159 }
160 return $result;
161 }
162
163
164 public function integerToBinary($operand)
165 {
166 $cmp = bccomp($operand, 0);
167 $return = '';
168 if ($cmp == 0) {
169 return "\0";
170 }
171 while (bccomp($operand, 0) > 0) {
172 $return = chr(bcmod($operand, 256)) . $return;
173 $operand = bcdiv($operand, 256);
174 }
175 if (ord($return[0]) > 127) {
176 $return = "\0" . $return;
177 }
178 return $return;
179 }
180
181 /**public function integerToBinary($operand)
182 {
183 $return = '';
184 while(bccomp($operand, '0')) {
185 $return .= chr(bcmod($operand, '256'));
186 $operand = bcdiv($operand, '256');
187 }
188 return $return;
189 }**/ // Prior version for referenced offset
190
191
192 public function hexToDecimal($operand)
193 {
194 $return = '0';
195 while(strlen($hex)) {
196 $hex = hexdec(substr($operand, 0, 4));
197 $dec = bcadd(bcmul($return, 65536), $hex);
198 $operand = substr($operand, 4);
199 }
200 return $return;
201 }
202
203}