-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbcmath.php
More file actions
353 lines (292 loc) · 8.8 KB
/
bcmath.php
File metadata and controls
353 lines (292 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
// Bitwise math of arbitrary precision numbers.
// From http://cct.me.ntut.edu.tw/chchting/aiahtm/computer/phphelp/ref.bc.php.htm
// MAX_BASE is the maximum base that can be represented in
// one byte on the host machine. On most modern systems, this
// value can be 256, but if there are still any systems with 7-bit
// bytes out there, you should use 128 for maximum
// portability.
define('MAX_BASE', 256);
//// INTERFACE ROUTINES:
// Bitwise NOT
function bcnot($x) // pushedx
{
return _bcbitwise_internal($x, 0, '_bcnot');
}
// Bitwise AND
function bcand($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcand');
}
// Bitwise OR
function bcor($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcor');
}
// Bitwise XOR
function bcxor($x, $y)
{
return _bcbitwise_internal($x, $y, '_bcxor');
}
// Left shift (<<)
function bcleftshift($num, $shift)
{
return bcmul($num, bcpow(2, $shift), 0);
}
// Right shift (>>)
function bcrightshift($num, $shift)
{
return bcdiv($num, bcpow(2, $shift), 0);
}
// Convert decimal to hex (like PHP's builtin dechex)
function bcdechex($num)
{
$res = "";
while ($num != '0') {
$byte = bcand($num, 255);
$hex = dechex($byte);
if (strlen($hex) == 1) $hex = '0' . $hex;
$res = $hex . $res;
$num = bcrightshift($num, 8);
}
if ($res == '') $res = 0;
return $res;
}
// Convert hex to decimal (like PHP's builtin hexdec)
function bchexdec($hex) {
$res = 0;
for ($i=0; $i<strlen($hex); $i++) {
$res = bcadd(bcleftshift($res, 4), hexdec($hex[$i]));
}
return $res;
}
// Convert args from hex and result to hex of a bcxxx operation.
function bcashex($operation, $x, $y) {
$x = bchexdec($x);
$y = bchexdec($y);
return bcdechex($operation($x, $y));
}
// Hex version of all the operators
function bcandhex($x, $y) {
return bcashex('bcand', $x, $y);
}
function bcorhex($x, $y) {
return bcashex('bcor', $x, $y);
}
function bcxorhex($x, $y) {
return bcashex('bcxor', $x, $y);
}
function bcleftshifthex($x, $y) {
return bcashex('bcleftshift', $x, $y);
}
function bcrightshifthex($x, $y) {
return bcashex('bcrightshift', $x, $y);
}
function bcaddhex($x, $y) {
return bcashex('bcadd', $x, $y);
}
function bcsubhex($x, $y) {
return bcashex('bcsub', $x, $y);
}
function bcmulhex($x, $y) {
return bcashex('bcmul', $x, $y);
}
function bcdivhex($x, $y) {
return bcashex('bcdiv', $x, $y);
}
//// INTERNAL ROUTINES
// These routines operate on only one byte. They are used to
// implement _bcbitwise_internal.
function _bcand($x, $y)
{
return $x & $y;
}
function _bcor($x, $y)
{
return $x | $y;
}
function _bcxor($x, $y)
{
return $x ^ $y;
}
function _bcnot($x, $y) // pushedx
{
return ~$x;
}
// _bcbitwise_internal - The majority of the code that implements
// the bitwise functions bcand, bcor, and bcxor.
//
// arguments - $x and $y are the operands (in decimal format),
// and $op is the name of one of the three
// internal functions, _bcand, _bcor, or _bcxor.
//
//
// see also - The interfaces to this function: bcand, bcor,
// and bcxor
function _bcbitwise_internal($x, $y, $op)
{
$bx = bc2bin($x);
$by = bc2bin($y);
// Pad $bx and $by so that both are the same length.
equalbinpad($bx, $by);
$ix=0;
$ret = '';
for($ix = 0; $ix < strlen($bx); $ix++)
{
$xd = substr($bx, $ix, 1);
$yd = substr($by, $ix, 1);
$ret .= call_user_func($op, $xd, $yd);
}
return bin2bc($ret);
}
// equalbinpad - Pad the operands on the most-significant end
// so they have the same number of bytes.
//
// arguments - $x and $y, binary-format numbers (converted
// from decimal format with bc2bin()), passed
// by reference.
//
// notes - Both operands are modified by this function.
function equalbinpad(&$x, &$y)
{
$xlen = strlen($x);
$ylen = strlen($y);
$length = max($xlen, $ylen);
fixedbinpad($x, $length);
fixedbinpad($y, $length);
}
// fixedbinpad - Pad a binary number up to a certain length
//
// arguments - $num: The operand to be padded.
//
// - $length: The desired minimum length for
// $num
//
// notes - $num is modified by this function.
function fixedbinpad(&$num, $length)
{
$pad = '';
for($ii = 0; $ii < $length-strlen($num); $ii++)
{
$pad .= bc2bin('0');
}
$num = $pad . $num;
}
// bc2bin - Convert a decimal number to the internal
// binary format used by this library.
//
// return value - The binary representation of $num.
function bc2bin($num)
{
return dec2base($num, MAX_BASE);
}
// bin2bc - Reverse of bc2bin
function bin2bc($num)
{
return base2dec($num, MAX_BASE);
}
// convert a decimal value to any other base value
function dec2base($dec,$base,$digits=FALSE) {
if($base<2 or $base>256) die("Invalid Base: ".$base);
bcscale(0);
$value="";
if(!$digits) $digits=digits($base);
while($dec>$base-1) {
$rest=bcmod($dec,$base);
$dec=bcdiv($dec,$base);
$value=$digits[$rest].$value;
}
$value=$digits[intval($dec)].$value;
return (string) $value;
}
// convert another base value to its decimal value
function base2dec($value,$base,$digits=FALSE) {
if($base<2 or $base>256) die("Invalid Base: ".$base);
bcscale(0);
if($base<37) $value=strtolower($value);
if(!$digits) $digits=digits($base);
$size=strlen($value);
$dec="0";
for($loop=0;$loop<$size;$loop++) {
$element=strpos($digits,$value[$loop]);
$power=bcpow($base,$size-$loop-1);
$dec=bcadd($dec,bcmul($element,$power));
}
return (string) $dec;
}
function digits($base) {
if($base>64) {
$digits="";
for($loop=0;$loop<256;$loop++) {
$digits.=chr($loop);
}
} else {
$digits ="0123456789abcdefghijklmnopqrstuvwxyz";
$digits.="ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
}
$digits=substr($digits,0,$base);
return (string) $digits;
}
// returns a binary string of the bytes of 'value'.
// Leading 0x00s are added based on how many are needed.
function bcbytearray($value, $bytes) // pushedx
{
$bin = "";
$parts = str_split(bcdechex($value), 2);
if(count($parts) < $bytes)
{
$arr = array();
for($x = 0; $x < $bytes - count($parts); ++$x)
{
array_push($arr, "0");
}
for($x = 0; $x < count($parts); ++$x)
{
array_push($arr, $parts[$x]);
}
$parts = $arr;
}
for($x = 0; $x < count($parts); ++$x)
{
$bin .= chr(hexdec($parts[count($parts) - $x - 1]));
}
return $bin;
}
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1/Apache 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is LoomClient PHP library
*
* The Initial Developer of the Original Code is
* Bill St. Clair.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Bill St. Clair <bill@billstclair.com>
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), the GNU Lesser General Public License Version 2.1 or later
* (the "LGPL"), or The Apache License Version 2.0 (the "AL"), in
* which case the provisions of the GPL, LGPL, or AL are applicable
* instead of those above. If you wish to allow use of your version of
* this file only under the terms of the GPL, the LGPL, or the AL, and
* not to allow others to use your version of this file under the
* terms of the MPL, indicate your decision by deleting the provisions
* above and replace them with the notice and other provisions
* required by the GPL or the LGPL. If you do not delete the
* provisions above, a recipient may use your version of this file
* under the terms of any one of the MPL, the GPL the LGPL, or the AL.
****** END LICENSE BLOCK ***** */
?>