Skip to content

Commit

Permalink
Remove bcmath requirement and refactor to use Math_BigInteger instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Apr 27, 2015
1 parent 4689010 commit 90cbdcc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
37 changes: 16 additions & 21 deletions framework/Mapi/lib/Horde/Mapi.php
Expand Up @@ -141,12 +141,8 @@ public static function filetimeToUnixtime($ft)
$dtval = substr($ft, 0, 16); // clip overlength string
$dtval = str_pad($dtval, 16, '0'); // pad underlength string
$quad = self::_flipEndian($dtval);
try {
$win64_datetime = self::_hexToBcint($quad);
return self::_win64ToUnix($win64_datetime);
} catch (Horde_Mapi_Exception $e) {
return -1;
}
$win64_datetime = self::_hexToBcint($quad);
return self::_win64ToUnix($win64_datetime);
}

// swap little-endian to big-endian
Expand All @@ -166,39 +162,38 @@ protected static function _flipEndian($str)
// convert hex string to BC-int
protected static function _hexToBcint($str)
{
if (!extension_loaded('bcmath')) {
throw new Horde_Mapi_Exception('bcmath extension not loaded.');
}

$hex = array(
'0'=>'0', '1'=>'1', '2'=>'2', '3'=>'3', '4'=>'4',
'5'=>'5', '6'=>'6', '7'=>'7', '8'=>'8', '9'=>'9',
'a'=>'10', 'b'=>'11', 'c'=>'12', 'd'=>'13', 'e'=>'14', 'f'=>'15',
'A'=>'10', 'B'=>'11', 'C'=>'12', 'D'=>'13', 'E'=>'14', 'F'=>'15'
);

$bci = '0';
$bci = new Math_BigInteger('0');
$len = strlen($str);
for ($i = 0; $i < $len; ++$i) {
$bci = bcmul($bci, '16');
$bci = $bci->multiply(new Math_BigInteger('16'));
$ch = $str[$i];
if (isset($hex[$ch]))
$bci = bcadd($bci, $hex[$ch]);
if (isset($hex[$ch])) {
$bci = $bci->add(new Math_BigInteger($hex[$ch]));
}
}

return $bci;
}

/**
*
* @param Math_BigInteger $bci
* @return string
*/
protected static function _win64ToUnix($bci)
{
if (!extension_loaded('bcmath')) {
throw new Horde_Mapi_Exception('bcmath extension not loaded.');
}

// Unix epoch as a Windows file date-time value
$magicnum = '116444735995904000';
$t = bcsub($bci, $magicnum); // Cast to Unix epoch
return bcdiv($t, '10000000', 0); // Convert from ticks to seconds
$t = $bci->subtract(new Math_BigInteger('116444735995904000')); // Cast to Unix epoch
list($quotient, $remainder) = $t->divide(new Math_BigInteger('10000000'));

return (string)$quotient;
}


Expand Down
20 changes: 11 additions & 9 deletions framework/Mapi/package.xml
Expand Up @@ -10,7 +10,7 @@
<email>mrubinsk@horde.org</email>
<active>yes</active>
</lead>
<date>2015-01-09</date>
<date>2015-04-27</date>
<version>
<release>1.0.5</release>
<api>1.0.0</api>
Expand All @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix Horde_Mapi if bcmath isn&apos;t installed.
* [mjr] Remove bcmath extension requirement and add PEAR Math_BigInteger package in it&apos;s place.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -81,12 +81,14 @@
<max>3.0.0alpha1</max>
<exclude>3.0.0alpha1</exclude>
</package>
<package>
<name>Math_BigInteger</name>
<channel>pear.php.net</channel>
<min>1.0.2</min>
<max>2.0.0</max>
<exclude>2.0.0</exclude>
</package>
</required>
<optional>
<extension>
<name>bcmath</name>
</extension>
</optional>
</dependencies>
<phprelease>
<filelist>
Expand Down Expand Up @@ -177,10 +179,10 @@
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2015-01-09</date>
<date>2015-04-27</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Fix Horde_Mapi if bcmath isn&apos;t installed.
* [mjr] Remove bcmath extension requirement and add PEAR Math_BigInteger package in it&apos;s place.
</notes>
</release>
</changelog>
Expand Down
6 changes: 1 addition & 5 deletions framework/Mapi/test/Horde/Mapi/MapiTest.php
Expand Up @@ -22,11 +22,7 @@ class Horde_Mapi_MapiTest extends PHPUnit_Framework_TestCase
public function testFiletimeToUnixTime()
{
$data = file_get_contents(__DIR__ . '/fixtures/filetime');

$this->assertEquals(
extension_loaded('bcmath') ? 1387818000 : -1,
Horde_Mapi::filetimeToUnixtime($data)
);
$this->assertEquals(Horde_Mapi::filetimeToUnixtime($data), 1387818000);
}

/**
Expand Down

0 comments on commit 90cbdcc

Please sign in to comment.