aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal/GPBUtil.php
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2016-10-25 17:27:05 -0700
committerGravatar GitHub <noreply@github.com>2016-10-25 17:27:05 -0700
commit51c5ff889ccd3836c25f40baafb350f92c9ee103 (patch)
tree39ba93f36167e2ab73c25ac337da0a97a4df4970 /php/src/Google/Protobuf/Internal/GPBUtil.php
parent58580da37357941d502805be3ae520441be77728 (diff)
Fix pure php implementation for 32-bit machine. (#2282)
Diffstat (limited to 'php/src/Google/Protobuf/Internal/GPBUtil.php')
-rw-r--r--php/src/Google/Protobuf/Internal/GPBUtil.php41
1 files changed, 38 insertions, 3 deletions
diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php
index 417a9729..30d7350f 100644
--- a/php/src/Google/Protobuf/Internal/GPBUtil.php
+++ b/php/src/Google/Protobuf/Internal/GPBUtil.php
@@ -37,6 +37,28 @@ use Google\Protobuf\Internal\RepeatedField;
class GPBUtil
{
+ public function divideInt64ToInt32($value, &$high, &$low, $trim = false)
+ {
+ $isNeg = (bccomp($value, 0) < 0);
+ if ($isNeg) {
+ $value = bcsub(0, $value);
+ }
+ $high = (int) bcdiv(bcadd($value, 1), 4294967296);
+ $low = (int) bcmod($value, 4294967296);
+ if ($isNeg) {
+ $high = ~$high;
+ $low = ~$low;
+ $low++;
+ if (!$low) {
+ $high++;
+ }
+ }
+
+ if ($trim) {
+ $high = 0;
+ }
+ }
+
public static function checkString(&$var, $check_utf8)
{
@@ -70,9 +92,14 @@ class GPBUtil
public static function checkUint32(&$var)
{
if (is_numeric($var)) {
- $var = intval($var);
if (PHP_INT_SIZE === 8) {
+ $var = intval($var);
$var |= ((-(($var >> 31) & 0x1)) & ~0xFFFFFFFF);
+ } else {
+ if (bccomp($var, 0x7FFFFFFF) > 0) {
+ $var = bcsub($var, "4294967296");
+ }
+ $var = (int) $var;
}
} else {
trigger_error("Expect integer.", E_USER_ERROR);
@@ -82,7 +109,11 @@ class GPBUtil
public static function checkInt64(&$var)
{
if (is_numeric($var)) {
- $var = intval($var);
+ if (PHP_INT_SIZE == 8) {
+ $var = intval($var);
+ } else {
+ $var = bcdiv($var, 1, 0);
+ }
} else {
trigger_error("Expect integer.", E_USER_ERROR);
}
@@ -91,7 +122,11 @@ class GPBUtil
public static function checkUint64(&$var)
{
if (is_numeric($var)) {
- $var = intval($var);
+ if (PHP_INT_SIZE == 8) {
+ $var = intval($var);
+ } else {
+ $var = bcdiv($var, 1, 0);
+ }
} else {
trigger_error("Expect integer.", E_USER_ERROR);
}