Subversion Repositories oidplus

Rev

Rev 212 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 212 Rev 237
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * PHP GMP-Supplement implemented using BCMath
4
 * PHP GMP-Supplement implemented using BCMath
5
 * Copyright 2020 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2020 Daniel Marschall, ViaThinkSoft
6
 * Version 2020-02-29
6
 * Version 2020-04-07
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 160... Line 160...
160
                }
160
                }
161
       
161
 
162
                // gmp_export ( GMP $gmpnumber [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] ) : string
162
                // gmp_export ( GMP $gmpnumber [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] ) : string
163
                // Export to a binary string
163
                // Export to a binary string
164
                function gmp_export($gmpnumber, $word_size = 1, $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) {
164
                function gmp_export($gmpnumber, $word_size = 1, $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) {
165
                        if ($word_size != 1) die("Word size != 1 not implemented");
165
                        if ($word_size != 1) throw new Exception("Word size != 1 not implemented");
166
                        if ($options != GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) die("Different options not implemented");
166
                        if ($options != GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) throw new Exception("Different options not implemented");
167
 
167
 
168
                        bcscale(0);
168
                        bcscale(0);
169
                        $gmpnumber = bcmul($gmpnumber,"1"); // normalize
169
                        $gmpnumber = bcmul($gmpnumber,"1"); // normalize
170
                        return gmp_init(bin2hex($gmpnumber), 16);
170
                        return gmp_init(bin2hex($gmpnumber), 16);
171
                }
171
                }
Line 230... Line 230...
230
                // gmp_import ( string $data [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] ) : GMP
230
                // gmp_import ( string $data [, int $word_size = 1 [, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN ]] ) : GMP
231
                // Import from a binary string
231
                // Import from a binary string
232
                function gmp_import($data, $word_size=1, $options=GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) {
232
                function gmp_import($data, $word_size=1, $options=GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) {
233
                        bcscale(0);
233
                        bcscale(0);
234
 
234
 
235
                        if ($word_size != 1) die("Word size != 1 not implemented");
235
                        if ($word_size != 1) throw new Exception("Word size != 1 not implemented");
236
                        if ($options != GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) die("Different options not implemented");
236
                        if ($options != GMP_MSW_FIRST | GMP_NATIVE_ENDIAN) throw new Exception("Different options not implemented");
237
 
237
 
238
                        return gmp_init(hex2bin(gmp_strval(gmp_init($data), 16)));
238
                        return gmp_init(hex2bin(gmp_strval(gmp_init($data), 16)));
239
                }
239
                }
240
       
240
 
241
                // gmp_init ( mixed $number [, int $base = 0 ] ) : GMP
241
                // gmp_init ( mixed $number [, int $base = 0 ] ) : GMP
Line 758... Line 758...
758
                $ab = bc_dec2bin($a);
758
                $ab = bc_dec2bin($a);
759
                $bb = bc_dec2bin($b);
759
                $bb = bc_dec2bin($b);
760
                $length = max(strlen($ab), strlen($bb));
760
                $length = max(strlen($ab), strlen($bb));
761
                $ab = str_pad($ab, $length, "0", STR_PAD_LEFT);
761
                $ab = str_pad($ab, $length, "0", STR_PAD_LEFT);
762
                $bb = str_pad($bb, $length, "0", STR_PAD_LEFT);
762
                $bb = str_pad($bb, $length, "0", STR_PAD_LEFT);
763
       
-
 
764
                // Do the bitwise binary operation
763
                // Do the bitwise binary operation
765
                $cb = '';
764
                $cb = '';
766
                for ($i=0; $i<$length; $i++) {
765
                for ($i=0; $i<$length; $i++) {
767
                        if ($ab[$i] == 1) return false;
766
                        if ($ab[$i] == 1) return false;
768
                }
767
                }